/** * 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 { BasicForwardRuleInfGraph graph = new BasicForwardRuleInfGraph(this, rules, schemaGraph); graph.setDerivationLogging(recordDerivations); graph.setTraceOn(traceOn); graph.rebind(data); return graph; }
/** * Return the Graph containing all the static deductions available so far. * Will force a prepare. */ @Override public Graph getDeductionsGraph() { prepare(); return safeDeductions; }
/** * Free all resources, any further use of this Graph is an error. */ @Override public void close() { if (!closed) { bEngine.halt(); bEngine = null; transitiveEngine = null; super.close(); } }
/** * Perform a rule test on the raw RETE engine. This requires some fiddling * with dummy parent graphs. */ private void doRuleTest(String rules, Triple[] adds, Triple[] expected) { List<Rule> ruleList = Rule.parseRules(rules); BasicForwardRuleInfGraph infgraph = new BasicForwardRuleInfGraph(null, new ArrayList<Rule>(), null, Factory.createGraphMem()); // infgraph.setTraceOn(true); RETEEngine engine = new RETEEngine(infgraph, ruleList); infgraph.prepare(); engine.init(true, new FGraph(Factory.createGraphMem())); for ( Triple add : adds ) { engine.addTriple( add, true ); } engine.runAll(); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), expected); }
/** * 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 { InfGraph graph = new BasicForwardRuleInfGraph(this, rules, null, tbox); return new BasicForwardRuleReasoner(rules, graph, factory); }
/** * Perform any initial processing and caching. This call is optional. Most * engines either have negligable set up work or will perform an implicit * "prepare" if necessary. The call is provided for those occasions where * substantial preparation work is possible (e.g. running a forward chaining * rule system) and where an application might wish greater control over when * this prepration is done. */ @Override public synchronized void prepare() { if (this.isPrepared()) return; this.setPreparedState(true); // initilize the deductions graph fdeductions = new FGraph( createDeductionsGraph() ); boolean rulesLoaded = false; if (schemaGraph != null) { rulesLoaded = preloadDeductions(schemaGraph); } if (rulesLoaded) { engine.fastInit(fdata); } else { engine.init(true, fdata); } }
InfGraph graph = null; if (mode == FORWARD) { graph = new BasicForwardRuleInfGraph(this, rules, schemaArg); ((BasicForwardRuleInfGraph)graph).setTraceOn(traceOn); } else if (mode == FORWARD_RETE) { graph = new RETERuleInfGraph(this, rules, schemaArg); ((BasicForwardRuleInfGraph)graph).setTraceOn(traceOn); ((BasicForwardRuleInfGraph)graph).setFunctorFiltering(filterFunctors); } else if (mode == BACKWARD) { graph = new LPBackwardRuleInfGraph(this, getBruleStore(), data, schemaArg);
/** * Return the number of triples in the inferred graph */ @Override public int graphBaseSize() { checkOpen(); this.requirePrepared(); int baseSize = fdata.getGraph().size(); int dedSize = fdeductions.getGraph().size(); // System.err.println( ">> BasicForwardRuleInfGraph::size = " + baseSize + "(base) + " + dedSize + "(deductions)" ); return baseSize + dedSize; }
/** * Basic pattern lookup interface. * This implementation assumes that the underlying findWithContinuation * will have also consulted the raw data. * @param pattern a TriplePattern to be matched against the data * @return a ExtendedIterator over all Triples in the data set * that match the pattern */ @Override public ExtendedIterator<Triple> find(TriplePattern pattern) { return findWithContinuation(pattern, null); }
/** * 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 return true if the rule set has also been loaded */ protected boolean preloadDeductions(Graph preloadIn) { Graph d = fdeductions.getGraph(); BasicForwardRuleInfGraph preload = (BasicForwardRuleInfGraph) 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.find(null, null, null); i.hasNext(); ) { d.add(i.next()); } engine.setRuleStore(preload.engine.getRuleStore()); return true; } else { return false; } }
/** * Add one triple to the data graph, run any rules triggered by * the new data item, recursively adding any generated triples. */ @Override public synchronized void performAdd(Triple t) { version++; fdata.getGraph().add(t); if (this.isPrepared()) { engine.add(t); } }
/** * Constructor. Creates a new inference graph to which a (compiled) rule set * and a data graph can be attached. This separation of binding is useful to allow * any configuration parameters (such as logging) to be set before the data is added. * Note that until the data is added using {@link #rebind rebind} then any operations * like add, remove, find will result in errors. * * @param reasoner the parent reasoner * @param schema the (optional) schema data which is being processed */ public BasicForwardRuleInfGraph(Reasoner reasoner, Graph schema) { super(null, reasoner); instantiateRuleEngine(null); this.schemaGraph = schema; }
/** * Add a new deduction to the deductions graph. */ @Override public void addDeduction(Triple t) { getDeductionsGraph().add(t); }
/** * Perform a rule test on the raw RETE engine. This requires some fiddling * with dummy parent graphs. */ private void doRuleTest(String rules, Triple[] adds, Triple[] expected) { List<Rule> ruleList = Rule.parseRules(rules); BasicForwardRuleInfGraph infgraph = new BasicForwardRuleInfGraph(null, new ArrayList<Rule>(), null, Factory.createGraphMem()); // infgraph.setTraceOn(true); RETEEngine engine = new RETEEngine(infgraph, ruleList); infgraph.prepare(); engine.init(true, new FGraph(Factory.createGraphMem())); for ( Triple add : adds ) { engine.addTriple( add, true ); } engine.runAll(); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), expected); }
/** * 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 { InfGraph graph = new BasicForwardRuleInfGraph(this, rules, null, tbox); return new BasicForwardRuleReasoner(rules, graph, factory); }
/** * Perform any initial processing and caching. This call is optional. Most * engines either have negligable set up work or will perform an implicit * "prepare" if necessary. The call is provided for those occasions where * substantial preparation work is possible (e.g. running a forward chaining * rule system) and where an application might wish greater control over when * this prepration is done. */ @Override public synchronized void prepare() { if (this.isPrepared()) return; this.setPreparedState(true); // initilize the deductions graph fdeductions = new FGraph( createDeductionsGraph() ); boolean rulesLoaded = false; if (schemaGraph != null) { rulesLoaded = preloadDeductions(schemaGraph); } if (rulesLoaded) { engine.fastInit(fdata); } else { engine.init(true, fdata); } }
InfGraph graph = null; if (mode == FORWARD) { graph = new BasicForwardRuleInfGraph(this, rules, schemaArg); ((BasicForwardRuleInfGraph)graph).setTraceOn(traceOn); } else if (mode == FORWARD_RETE) { graph = new RETERuleInfGraph(this, rules, schemaArg); ((BasicForwardRuleInfGraph)graph).setTraceOn(traceOn); ((BasicForwardRuleInfGraph)graph).setFunctorFiltering(filterFunctors); } else if (mode == BACKWARD) { graph = new LPBackwardRuleInfGraph(this, getBruleStore(), data, schemaArg);
/** * Return the number of triples in the inferred graph */ @Override public int graphBaseSize() { checkOpen(); this.requirePrepared(); int baseSize = fdata.getGraph().size(); int dedSize = fdeductions.getGraph().size(); // System.err.println( ">> BasicForwardRuleInfGraph::size = " + baseSize + "(base) + " + dedSize + "(deductions)" ); return baseSize + dedSize; }
/** * Basic pattern lookup interface. * This implementation assumes that the underlying findWithContinuation * will have also consulted the raw data. * @param pattern a TriplePattern to be matched against the data * @return a ExtendedIterator over all Triples in the data set * that match the pattern */ @Override public ExtendedIterator<Triple> find(TriplePattern pattern) { return findWithContinuation(pattern, null); }
/** * 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 return true if the rule set has also been loaded */ protected boolean preloadDeductions(Graph preloadIn) { Graph d = fdeductions.getGraph(); BasicForwardRuleInfGraph preload = (BasicForwardRuleInfGraph) 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.find(null, null, null); i.hasNext(); ) { d.add(i.next()); } engine.setRuleStore(preload.engine.getRuleStore()); return true; } else { return false; } }