/** * Run a datatype range check on all literal values of all properties with a range declaration. * @param report */ protected void performDatatypeRangeValidation(StandardValidityReport report) { HashMap<Node, List<RDFDatatype>> dtRange = getDTRange(); for ( Node prop : dtRange.keySet() ) { for ( Iterator<Triple> i = find( null, prop, null ); i.hasNext(); ) { Triple triple = i.next(); report.add( checkLiteral( prop, triple ) ); } } }
/** * Adds a set of precomputed triples to the deductions store. These do not, themselves, * fire any rules but provide additional axioms that might enable future rule * firing when real data is added. Used to implement bindSchema processing * in the parent Reasoner. * @return true if the preload was able to load rules as well */ @Override protected boolean preloadDeductions(Graph preloadIn) { Graph d = fdeductions.getGraph(); FBRuleInfGraph preload = (FBRuleInfGraph)preloadIn; // If the rule set is the same we can reuse those as well if (preload.rules == rules) { // Load raw deductions for (Iterator<Triple> i = preload.getDeductionsGraph().find(null, null, null); i.hasNext(); ) { d.add( i.next() ); } // Load backward rules addBRules(preload.getBRules()); // Load forward rules engine.setRuleStore(preload.getForwardRuleStore()); // Add access to raw data return true; } else { return false; } }
/** * Get the single static precomputed rule closure. */ @Override public synchronized InfGraph getPreload() { // Method synchronized - synchronize with other FBRulereasoer sync methods // synchronized block - sync on static synchronized (OWLFBRuleReasoner.class) { if (staticPreload == null) { boolean prior = JenaParameters.enableFilteringOfHiddenInfNodes; try { JenaParameters.enableFilteringOfHiddenInfNodes = true; staticPreload = new FBRuleInfGraph(this, rules, null); staticPreload.prepare(); } finally { JenaParameters.enableFilteringOfHiddenInfNodes = prior; } } return staticPreload; } }
/** * 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 { Graph schemaArg = schemaGraph == null ? getPreload() : (FBRuleInfGraph) schemaGraph; FBRuleInfGraph graph = new FBRuleInfGraph( this, rules, schemaArg ); graph.setDerivationLogging( recordDerivations ); graph.setTraceOn( traceOn ); graph.rebind( data ); return graph; }
/** * 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 { checkArgGraph(data); FBRuleInfGraph graph = null; InfGraph schemaArg = schemaGraph == null ? getPreload() : (FBRuleInfGraph)schemaGraph; List<Rule> baseRules = ((FBRuleInfGraph)schemaArg).getRules(); graph = new FBRuleInfGraph(this, baseRules, schemaArg); graph.addPreprocessingHook(new OWLRuleTranslationHook()); graph.setDerivationLogging(recordDerivations); graph.setTraceOn(traceOn); graph.rebind(data); graph.setDatatypeRangeValidation(true); return graph; }
"[r2: (?p rdf:type s) -> [r2b: (?x ?p ?x) <- (?x rdf:type t)]]"; FBRuleInfGraph infgraph = (FBRuleInfGraph) createInfGraph(rules, data); infgraph.setDerivationLogging(true); infgraph.prepare(); assertTrue("Forward rule count", infgraph.getNRulesFired() == 3); TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), new Object[] { new Triple(a, p, a), new Triple(a, p, b), Iterator<Derivation> di = infgraph.getDerivation(new Triple(b, p, a)); assertTrue(di.hasNext()); RuleDerivation d = (RuleDerivation)di.next();
((LPBackwardRuleInfGraph)graph).setTraceOn(traceOn); } else { List<Rule> ruleSet = ((FBRuleInfGraph)schemaArg).getRules(); FBRuleInfGraph fbgraph = new FBRuleInfGraph(this, ruleSet, schemaArg); graph = fbgraph; if (enableTGCCaching) fbgraph.setUseTGCCache(); fbgraph.setTraceOn(traceOn); fbgraph.setFunctorFiltering(filterFunctors); if (preprocessorHooks!= null) { for ( RulePreprocessHook preprocessorHook : preprocessorHooks ) fbgraph.addPreprocessingHook( preprocessorHook );
if (this.isPrepared()) return; this.setPreparedState(true); oldDeductions.clear(); } else { fdeductions = new FGraph( createDeductionsGraph() ); resetTGCCache(); if (schemaGraph != null) { dataFind = FinderUtil.cascade(dataFind, new FGraph(rawPreload)); rulesLoaded = preloadDeductions(schemaGraph); } else { addBRules(extractPureBackwardRules(rules)); engine.init(true, dataSource);
/** * Precompute the implications of a schema graph. The statements in the graph * will be combined with the data when the final InfGraph is created. */ @Override public Reasoner bindSchema(Graph tbox) throws ReasonerException { if (schemaGraph != null) { throw new ReasonerException("Can only bind one schema at a time to an RDFSRuleReasoner"); } FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); if (enableTGCCaching) (graph).setUseTGCCache(); graph.prepare(); RDFSRuleReasoner grr = new RDFSRuleReasoner(graph, factory); grr.setDerivationLogging(recordDerivations); grr.setTraceOn(traceOn); grr.setTransitiveClosureCaching(enableTGCCaching); grr.setFunctorFiltering(filterFunctors); if (preprocessorHooks != null) { for (RulePreprocessHook rulePreprocessHook : preprocessorHooks) { grr.addPreprocessingHook(rulePreprocessHook); } } return grr; }
Graph schemaArg = schemaGraph == null ? getPreload() : schemaGraph; InfGraph graph = null; List<Rule> ruleSet = ((FBRuleInfGraph)schemaArg).getRules(); FBRuleInfGraph fbgraph = new RDFSRuleInfGraph(this, ruleSet, schemaArg); graph = fbgraph; if (enableTGCCaching) fbgraph.setUseTGCCache(); fbgraph.setTraceOn(traceOn); if (preprocessorHooks!= null) { for (RulePreprocessHook rulePreprocessHook : preprocessorHooks) fbgraph.addPreprocessingHook(rulePreprocessHook);
/** * Return a map from property nodes to a list of RDFDatatype objects * which have been declared as the range of that property. */ protected HashMap<Node, List<RDFDatatype>> getDTRange() { if (dtRange == null) { dtRange = new HashMap<>(); for (Iterator<Triple> i = find(null, RDFS.range.asNode(), null); i.hasNext(); ) { Triple triple = i.next(); Node prop = triple.getSubject(); Node rangeValue = triple.getObject(); if (rangeValue.isURI()) { RDFDatatype dt = TypeMapper.getInstance().getTypeByName(rangeValue.getURI()); if (dt != null) { List<RDFDatatype> range = dtRange.get(prop); if (range == null) { range = new ArrayList<>(); dtRange.put(prop, range); } range.add(dt); } } } } return dtRange; }
/** * Precompute the implications of a schema graph. The statements in the graph * will be combined with the data when the final InfGraph is created. */ @Override public Reasoner bindSchema(Graph tbox) throws ReasonerException { checkArgGraph(tbox); if (schemaGraph != null) { throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner"); } FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); graph.addPreprocessingHook(new OWLRuleTranslationHook()); graph.prepare(); return new OWLFBRuleReasoner(this, graph); }
/** * Return an inference graph working over the given rule set and raw data. * Can be overridden by subclasses of this test class. * @param rules the rule set to use * @param data the graph of triples to process * @param tabled an array of predicates that should be tabled */ public static InfGraph makeInfGraph(List<Rule> rules, Graph data, Node[] tabled) { FBRuleReasoner reasoner = new FBRuleReasoner(rules); FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data); for ( Node aTabled : tabled ) { infgraph.setTabled( aTabled ); } // infgraph.setTraceOn(true); infgraph.setDerivationLogging(true); return infgraph; }
/** * This method is invoked when the builtin is called in a rule body. * @param args the array of argument values for the builtin, this is an array * of Nodes, some of which may be Node_RuleVariables. * @param length the length of the argument list, may be less than the length of the args array * for some rule engines * @param context an execution context giving access to other relevant data */ @Override public void headAction(Node[] args, int length, RuleContext context) { InfGraph infgraph = context.getGraph(); if (infgraph instanceof FBRuleInfGraph) { ((FBRuleInfGraph)infgraph).setTabled(Node.ANY); } else if (infgraph instanceof LPBackwardRuleInfGraph) { ((LPBackwardRuleInfGraph)infgraph).setTabled(Node.ANY); } else { // Quietly ignore as an irrelevant directive // Could log or throw exception but currently I want to be able to use // the same rule base from different contexts which do and do not need // to know about this. } } }
/** * Helper function to list the interesting parts of an FBInfGraph. */ public void listFBGraph(String message, FBRuleInfGraph graph) { logger.info(message); logger.info("Raw graph data"); listGraph(graph.getRawGraph()); logger.info("Static deductions"); listGraph(graph.getDeductionsGraph()); }
/** * Extended find interface used in situations where the implementator * may or may not be able to answer the complete query. It will * attempt to answer the pattern but if its answers are not known * to be complete then it will also pass the request on to the nested * Finder to append more results. * @param pattern a TriplePattern to be matched against the data * @param continuation either a Finder or a normal Graph which * will be asked for additional match results if the implementor * may not have completely satisfied the query. */ @Override public ExtendedIterator<Triple> findWithContinuation(TriplePattern pattern, Finder continuation) { checkOpen(); this.requirePrepared(); ExtendedIterator<Triple> result =bEngine.find(pattern).filterKeep( new UniqueFilter<Triple>()); if (continuation != null) { result = result.andThen(continuation.find(pattern)); } if (filterFunctors) { // return result.filterDrop(Functor.acceptFilter); return result.filterDrop( t -> FBRuleInfGraph.this.accept( t ) ); } else { return result; } }
/** * Check cost of creating an empty OWL closure. */ public void temp() { Graph data = Factory.createGraphMem(); Graph data2 = Factory.createGraphMem(); Reasoner reasoner = new OWLFBRuleReasoner(OWLFBRuleReasonerFactory.theInstance()); FBRuleInfGraph infgraph = (FBRuleInfGraph)reasoner.bind(data); FBRuleInfGraph infgraph2 = (FBRuleInfGraph)reasoner.bind(data2); long t1 = System.currentTimeMillis(); infgraph.prepare(); long t2 = System.currentTimeMillis(); System.out.println("Prepare on empty graph = " + (t2-t1) +"ms"); t1 = System.currentTimeMillis(); infgraph2.prepare(); t2 = System.currentTimeMillis(); System.out.println("Prepare on empty graph = " + (t2-t1) +"ms"); }
/** * Get the single static precomputed rule closure. */ @Override protected synchronized InfGraph getPreload() { // We only support this in HYBRID mode if (cachePreload && preload == null && mode == HYBRID) { preload = new FBRuleInfGraph( this, rules, null, Factory.createDefaultGraph() ); if (enableTGCCaching) ((FBRuleInfGraph)preload).setUseTGCCache(); preload.prepare(); } return preload; }
Model data = FileManager.get().loadModel("file:testing/reasoners/bugs/loop.owl"); InfModel infmodel = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), data); ((FBRuleInfGraph)infmodel.getGraph()).setTraceOn(true); String baseURI = "http://jena.hpl.hp.com/eg#"; Resource C = infmodel.getResource(baseURI + "C");
/** * Get the single static precomputed rule closure. */ protected synchronized InfGraph getPreload() { if (cachePreload && preload == null) { preload = (new FBRuleInfGraph(this, rules, null)); preload.prepare(); } return preload; }