/** * Constructor method that builds an instance of the associated Reasoner * @param configuration a set of arbitrary configuration information to be * passed the reasoner, encoded as RDF properties of a base configuration resource, * can be null in no custom configuration is required. */ @Override public Reasoner create( Resource configuration ) { return new GenericRuleReasoner( this, configuration ); }
if (parameter.equals(ReasonerVocabulary.PROPenableFunctorFiltering)) { filterFunctors = Util.convertBooleanPredicateArg(parameter, value); } else if (isHasRule( parameter )) { addRuleFromString( value.toString() ); } else if (parameter.equals(ReasonerVocabulary.PROPenableOWLTranslation)) { enableOWLTranslation = Util.convertBooleanPredicateArg(parameter, value); if (enableOWLTranslation) { addPreprocessingHook(owlTranslator); } else { removePreprocessingHook(owlTranslator); addRules( loadRules( (String)value ) ); } else { throw new IllegalParameterException("PROPruleSet value should be a URI string. Was a " + value.getClass());
/** * Attach the reasoner to a set of RDF data to process. * The reasoner may already have been bound to specific rules or ontology * axioms (encoded in RDF) through earlier bindRuleset calls. * * @param data the RDF data to be processed, some reasoners may restrict * the range of RDF which is legal here (e.g. syntactic restrictions in OWL). * @return an inference graph through which the data+reasoner can be queried. * @throws ReasonerException if the data is ill-formed according to the * constraints imposed by this reasoner. */ @Override public InfGraph bind(Graph data) throws ReasonerException { InfGraph graph = super.bind(data); ((FBRuleInfGraph)graph).setDatatypeRangeValidation(true); return graph; }
@Override protected boolean doSetResourceParameter( Property parameter, Resource value ) { if (isRuleSetURL( parameter )) addRules( Rule.rulesFromURL( value.getURI() ) ); else if (isRuleSet( parameter )) { addRulesFromURLs( value ); addRulesFromStrings( value ); } else return false; return true; }
private void addRulesFromURLs( Resource value ) { Iterator<Statement> that = getRuleSetURLStatements( value ); while (that.hasNext()) addRules( Rule.rulesFromURL( that.next().getResource().getURI() ) ); }
} else { List<Rule> ruleSet = rules; graph = new FBRuleInfGraph(this, ruleSet, getPreload(), tbox); if (enableTGCCaching) ((FBRuleInfGraph)graph).setUseTGCCache(); ((FBRuleInfGraph)graph).prepare(); GenericRuleReasoner grr = new GenericRuleReasoner(rules, graph, factory, mode); grr.setDerivationLogging(recordDerivations); grr.setTraceOn(traceOn); grr.setTransitiveClosureCaching(enableTGCCaching); grr.setFunctorFiltering(filterFunctors); if (preprocessorHooks != null) { for ( RulePreprocessHook preprocessorHook : preprocessorHooks ) grr.addPreprocessingHook( preprocessorHook );
private void testLoadsMultipleRuleSetsViaRuleSetNode( String ns ) { String whereA = "file:testing/modelspecs/example.rules"; String whereB = "file:testing/modelspecs/extra.rules"; Resource r = resourceInModel( "x <ns>:ruleSet _a; _a <ns>:ruleSetURL <whereA>; _a <ns>:ruleSetURL <whereB>".replaceAll( "<ns>", ns ).replaceAll( "<whereA>", whereA ).replaceAll( "<whereB>", whereB ) ); GenericRuleReasoner grr = new GenericRuleReasoner( null, r ); assertEquals( rulesFromTwoPlaces( whereA, whereB ), new HashSet<>( grr.getRules() ) ); }
/** * Test chainging rules from axioms which broke while trying to * fix about test case. */ public void testFactChainRules() { Model facts = ModelFactory.createDefaultModel(); String NS = PrintUtil.egNS; Property mother = facts.createProperty(NS + "mother"); Resource female = facts.createProperty(NS + "Female"); mother.addProperty(RDFS.range, female); List<Rule> rules = Rule.parseRules( "-> tableAll(). \n" + "[rdfs6: (?p rdfs:subPropertyOf ?q), notEqual(?p,?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ] \n" + "-> (eg:range rdfs:subPropertyOf rdfs:range). \n" + "-> (rdfs:range rdfs:subPropertyOf eg:range). \n" ); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); reasoner.setTransitiveClosureCaching(true); InfModel inf = ModelFactory.createInfModel(reasoner, facts); Property egRange = inf.createProperty(NS + "range"); TestUtil.assertIteratorValues(this, inf.listStatements(null, egRange, (RDFNode)null), new Object[] {inf.createStatement(mother, egRange, female)} ); }
GenericRuleReasoner article=new GenericRuleReasoner(rules); article.setMode(GenericRuleReasoner.FORWARD_RETE); Model input=ModelFactory.createDefaultModel(); input.add(input.createResource(), RDF.type,input.createResource("http://example.com/Renegade"));
/** * Test early detection of illegal backward rules. */ public void testBRuleErrorHandling() { Graph data = Factory.createGraphMem(); List<Rule> rules = Rule.parseRules( "[a1: -> [(?x eg:p ?y) (?x eg:q ?y) <- (?x eg:r ?y)]]" ); boolean foundException = false; try { GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null); reasoner.setRules(rules); reasoner.setMode(GenericRuleReasoner.HYBRID); InfGraph infgraph = reasoner.bind(data); infgraph.prepare(); } catch (ReasonerException e) { foundException = true; } assertTrue("Catching use of multi-headed brules", foundException); }
/** * Minimal rule tester to check basic pattern match, forward style. */ public void testForward() { Graph test = Factory.createGraphMem(); test.add(new Triple(a, p, b)); test.add(new Triple(b, p, c)); GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null); reasoner.setRules(ruleList); reasoner.setMode(GenericRuleReasoner.FORWARD); // Check data bind version InfGraph infgraph = reasoner.bind(test); TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), ans); // Check schema bind version infgraph = reasoner.bindSchema(test).bind(Factory.createGraphMem()); TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), ans); }
/** * Test rebindAll reconsults a changed ruleset */ public void testRebindAll() { String NS = "http://jena.hpl.hp.com/example#"; List<Rule> rules1 = Rule.parseRules( "(?x http://jena.hpl.hp.com/example#p ?y) -> (?x http://jena.hpl.hp.com/example#q ?y)." ); List<Rule> rules2 = Rule.parseRules( "(?x http://jena.hpl.hp.com/example#q ?y) -> (?x http://jena.hpl.hp.com/example#r ?y)." ); Model m = ModelFactory.createDefaultModel(); Property p = m.createProperty(NS + "p"); Property q = m.createProperty(NS + "q"); Property r = m.createProperty(NS + "r"); Resource a = m.createResource(NS + "a"); Resource b = m.createResource(NS + "b"); Statement s1 = m.createStatement(a, p, b); Statement s2 = m.createStatement(a, q, b); Statement s3 = m.createStatement(a, r, b); m.add(s1); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules1); InfModel infModel = ModelFactory.createInfModel(reasoner, m); reasoner.addRules(rules2); TestUtil.assertIteratorValues(this, infModel.listStatements(a, null, (RDFNode)null), new Object[] {s1, s2}); ((FBRuleInfGraph)infModel.getGraph()).rebindAll(); TestUtil.assertIteratorValues(this, infModel.listStatements(a, null, (RDFNode)null), new Object[] {s1, s2, s3}); }
data2.add(new Triple(b, q, d)); GenericRuleReasoner reasoner = new GenericRuleReasoner(ruleList); reasoner.setMode(GenericRuleReasoner.FORWARD_RETE); Reasoner boundReasoner = reasoner.bindSchema(schema); InfGraph infgraph1 = boundReasoner.bind(data1); InfGraph infgraph2 = boundReasoner.bind(data2);
/** * Test control of functor filtering */ public void testHybridFunctorFilter() { Graph data = Factory.createGraphMem(); data.add(new Triple(a, r, b)); data.add(new Triple(a, p, s)); List<Rule> rules = Rule.parseRules( "[r0: (?x r ?y) (?x p ?z) -> (?x q func(?y, ?z)) ]" ); GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null); reasoner.setRules(rules); reasoner.setMode(GenericRuleReasoner.HYBRID); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(null, q, null), new Object[] { } ); reasoner.setFunctorFiltering(false); infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(null, q, null), new Object[] { new Triple(a, q, Functor.makeFunctorNode("func", new Node[]{b, s})) } ); }
reasoner.setRules(rules); reasoner.setMode(GenericRuleReasoner.HYBRID); reasoner.setTransitiveClosureCaching(useTGC); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(a, ty, null), new Object[] {
GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(configuration); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), new Object[] { reasoner.addDescription(m2, newConfig); TestUtil.assertIteratorValues(this, newConfig.listProperties(), new Statement[] { m2.createStatement(newConfig, ReasonerVocabulary.PROPderivationLogging, "true"), reasoner.setParameter(ReasonerVocabulary.PROPderivationLogging, "false"); newConfig = m2.createResource(); reasoner.addDescription(m2, newConfig); TestUtil.assertIteratorValues(this, newConfig.listProperties(), new Statement[] { m2.createStatement(newConfig, ReasonerVocabulary.PROPderivationLogging, "false"), reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(configuration); infgraph = reasoner.bind(Factory.createGraphMem()); Node an = NodeFactory.createURI(PrintUtil.egNS + "a"); Node C = NodeFactory.createURI(PrintUtil.egNS + "C");
@Override public Reasoner create( Resource configuration ) { GenericRuleReasoner result = (GenericRuleReasoner) r.create( configuration ); result.addRules( rules ); return result; }
} else { List<Rule> ruleSet = rules; graph = new FBRuleInfGraph(this, ruleSet, getPreload(), tbox); if (enableTGCCaching) ((FBRuleInfGraph)graph).setUseTGCCache(); ((FBRuleInfGraph)graph).prepare(); GenericRuleReasoner grr = new GenericRuleReasoner(rules, graph, factory, mode); grr.setDerivationLogging(recordDerivations); grr.setTraceOn(traceOn); grr.setTransitiveClosureCaching(enableTGCCaching); grr.setFunctorFiltering(filterFunctors); if (preprocessorHooks != null) { for ( RulePreprocessHook preprocessorHook : preprocessorHooks ) grr.addPreprocessingHook( preprocessorHook );
private void testLoadsSingleRuleViaURL( String ns ) { String where = "file:testing/modelspecs/example.rules"; Resource r = resourceInModel( "x <ns>:ruleSetURL <where>".replaceAll( "<ns>", ns ).replaceAll( "<where>", where ) ); List<Rule> rules = Rule.rulesFromURL( where ); GenericRuleReasoner grr = new GenericRuleReasoner( null, r ); assertEquals( rules, grr.getRules() ); }
/** * Test chainging rules from axioms which broke while trying to * fix about test case. */ public void testFactChainRules() { Model facts = ModelFactory.createDefaultModel(); String NS = PrintUtil.egNS; Property mother = facts.createProperty(NS + "mother"); Resource female = facts.createProperty(NS + "Female"); mother.addProperty(RDFS.range, female); List<Rule> rules = Rule.parseRules( "-> tableAll(). \n" + "[rdfs6: (?p rdfs:subPropertyOf ?q), notEqual(?p,?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ] \n" + "-> (eg:range rdfs:subPropertyOf rdfs:range). \n" + "-> (rdfs:range rdfs:subPropertyOf eg:range). \n" ); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); reasoner.setTransitiveClosureCaching(true); InfModel inf = ModelFactory.createInfModel(reasoner, facts); Property egRange = inf.createProperty(NS + "range"); TestUtil.assertIteratorValues(this, inf.listStatements(null, egRange, (RDFNode)null), new Object[] {inf.createStatement(mother, egRange, female)} ); }