/** * 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 (isPrepared) return; isPrepared = 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); } }
/** * 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 (isPrepared) { engine.add(t); } }
/** Set to true to enable derivation caching */ @Override public void setDerivationLogging(boolean recordDerivations) { this.recordDerivations = recordDerivations; engine.setDerivationLogging(recordDerivations); if (recordDerivations) { derivations = new OneToManyMap<Triple, Derivation>(); } else { derivations = 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; } }
/** * Return the number of rules fired since this rule engine instance was created and initialized */ public long getNRulesFired() { return engine.getNRulesFired(); }
/** * Return true if tracing should be acted on - i.e. if traceOn is true and we are past the * bootstrap phase. */ public boolean shouldTrace() { return traceOn && engine.shouldTrace(); }
/** * Attach a compiled rule set to this inference graph. * * @param ruleStore a compiled set of rules (i.e. the result of an FRuleEngine.compile). */ public void setRuleStore(Object ruleStore) { engine.setRuleStore(ruleStore); }