📄 origfbruleinfgraph.java
字号:
/******************************************************************
* File: FBRuleInfGraph.java
* Created by: Dave Reynolds
* Created on: 28-May-2003
*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
* $Id: OrigFBRuleInfGraph.java,v 1.9 2007/01/02 11:52:33 andy_seaborne Exp $
*****************************************************************/
package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
import com.hp.hpl.jena.reasoner.rulesys.impl.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.reasoner.transitiveReasoner.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.graph.*;
import java.util.*;
//import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.util.OneToManyMap;
import com.hp.hpl.jena.util.iterator.*;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
//import com.hp.hpl.jena.util.PrintUtil;
//import com.hp.hpl.jena.vocabulary.RDF;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* An inference graph that uses a mixture of forward and backward
* chaining rules. The forward rules can create direct deductions from
* the source data and schema and can also create backward rules. A
* query is answered by consulting the union of the raw data, the forward
* derived results and any relevant backward rules (whose answers are tabled
* for future reference).
*
* @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
* @version $Revision: 1.9 $ on $Date: 2007/01/02 11:52:33 $
*/
public class OrigFBRuleInfGraph extends BasicForwardRuleInfGraph implements BackwardRuleInfGraphI {
/** Single context for the reasoner, used when passing information to builtins */
protected BBRuleContext context;
/** A finder that searches across the data, schema, axioms and forward deductions*/
protected Finder dataFind;
/** The core backward rule engine which includes all the memoized results */
protected BRuleEngine bEngine;
/** The original rule set as supplied */
protected List rawRules;
/** The rule list after possible extension by preprocessing hooks */
protected List rules;
/** Static switch from Basic to RETE implementation of the forward component */
public static boolean useRETE = true;
/** Flag, if true then subClass and subProperty lattices will be optimized using TGCs */
protected boolean useTGCCaching = false;
/** Optional precomputed cache of the subClass/subproperty lattices */
protected TransitiveEngine transitiveEngine;
/** Optional list of preprocessing hooks to be run in sequence during preparation time */
protected List preprocessorHooks;
/** Cache of temporary property values inferred through getTemp calls */
protected TempNodeCache tempNodecache;
static Log logger = LogFactory.getLog(FBRuleInfGraph.class);
// =======================================================================
// Constructors
/**
* Constructor.
* @param reasoner the reasoner which created this inf graph instance
* @param schema the (optional) schema graph to be included
*/
public OrigFBRuleInfGraph(Reasoner reasoner, Graph schema) {
super(reasoner, schema);
bEngine = new BRuleEngine(this);
tempNodecache = new TempNodeCache(this);
}
/**
* Constructor.
* @param reasoner the reasoner which created this inf graph instance
* @param rules the rules to process
* @param schema the (optional) schema graph to be included
*/
public OrigFBRuleInfGraph(Reasoner reasoner, List rules, Graph schema) {
super(reasoner, rules, schema);
this.rawRules = rules;
bEngine = new BRuleEngine(this);
tempNodecache = new TempNodeCache(this);
}
/**
* Constructor.
* @param reasoner the reasoner which created this inf graph instance
* @param rules the rules to process
* @param schema the (optional) schema graph to be included
* @param data the data graph to be processed
*/
public OrigFBRuleInfGraph(Reasoner reasoner, List rules, Graph schema, Graph data) {
super(reasoner, rules, schema, data);
this.rawRules = rules;
bEngine = new BRuleEngine(this);
tempNodecache = new TempNodeCache(this);
}
/**
* Instantiate the forward rule engine to use.
* Subclasses can override this to switch to, say, a RETE imlementation.
* @param rules the rule set or null if there are not rules bound in yet.
*/
protected void instantiateRuleEngine(List rules) {
if (rules != null) {
if (useRETE) {
engine = new RETEEngine(this, rules);
} else {
engine = new FRuleEngine(this, rules);
}
} else {
if (useRETE) {
engine = new RETEEngine(this);
} else {
engine = new FRuleEngine(this);
}
}
}
/**
* Instantiate the optional caches for the subclass/suproperty lattices.
* Unless this call is made the TGC caching will not be used.
*/
public void setUseTGCCache() {
useTGCCaching = true;
if (schemaGraph != null) {
transitiveEngine = new TransitiveEngine(((OrigFBRuleInfGraph)schemaGraph).transitiveEngine);
} else {
transitiveEngine = new TransitiveEngine(
new TransitiveGraphCache(ReasonerVocabulary.directSubClassOf.asNode(), RDFS.subClassOf.asNode()),
new TransitiveGraphCache(ReasonerVocabulary.directSubPropertyOf.asNode(), RDFS.subPropertyOf.asNode()));
}
}
// =======================================================================
// Interface between infGraph and the goal processing machinery
/**
* Search the combination of data and deductions graphs for the given triple pattern.
* This may different from the normal find operation in the base of hybrid reasoners
* where we are side-stepping the backward deduction step.
*/
public ExtendedIterator findDataMatches(Node subject, Node predicate, Node object) {
return dataFind.find(new TriplePattern(subject, predicate, object));
}
/**
* Search the combination of data and deductions graphs for the given triple pattern.
* This may different from the normal find operation in the base of hybrid reasoners
* where we are side-stepping the backward deduction step.
*/
public ExtendedIterator findDataMatches(TriplePattern pattern) {
return dataFind.find(pattern);
}
/**
* Process a call to a builtin predicate
* @param clause the Functor representing the call
* @param env the BindingEnvironment for this call
* @param rule the rule which is invoking this call
* @return true if the predicate succeeds
*/
public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) {
if (clause instanceof Functor) {
context.setEnv(env);
context.setRule(rule);
return((Functor)clause).evalAsBodyClause(context);
} else {
throw new ReasonerException("Illegal builtin predicate: " + clause + " in rule " + rule);
}
}
/**
* Adds a new Backward rule as a rusult of a forward rule process. Only some
* infgraphs support this.
*/
public void addBRule(Rule brule) {
// logger.debug("Adding rule " + brule);
bEngine.addRule(brule);
bEngine.reset();
}
/**
* Deletes a new Backward rule as a rules of a forward rule process. Only some
* infgraphs support this.
*/
public void deleteBRule(Rule brule) {
// logger.debug("Deleting rule " + brule);
bEngine.deleteRule(brule);
bEngine.reset();
}
/**
* Adds a set of new Backward rules
*/
public void addBRules(List rules) {
for (Iterator i = rules.iterator(); i.hasNext(); ) {
Rule rule = (Rule)i.next();
// logger.debug("Adding rule " + rule);
bEngine.addRule(rule);
}
bEngine.reset();
}
/**
* Return an ordered list of all registered backward rules. Includes those
* generated by forward productions.
*/
public List getBRules() {
return bEngine.getAllRules();
}
/**
* Return the originally supplied set of rules, may be a mix of forward
* and backward rules.
*/
public List getRules() {
return rules;
}
/**
* Return a compiled representation of all the registered
* forward rules.
*/
private Object getForwardRuleStore() {
return engine.getRuleStore();
}
/**
* Add a new deduction to the deductions graph.
*/
public void addDeduction(Triple t) {
getDeductionsGraph().add(t);
if (useTGCCaching) {
transitiveEngine.add(t);
}
}
/**
* Retrieve or create a bNode representing an inferred property value.
* @param instance the base instance node to which the property applies
* @param prop the property node whose value is being inferred
* @param pclass the (optional, can be null) class for the inferred value.
* @return the bNode representing the property value
*/
public Node getTemp(Node instance, Node prop, Node pclass) {
return tempNodecache.getTemp(instance, prop, pclass);
}
// =======================================================================
// Core inf graph methods
/**
* Add a new rule to the rule set. This should only be used by implementations
* of RuleProprocessHook (which are called during rule system preparation phase).
* If called at other times the rule won't be correctly transferred into the
* underlying engines.
*/
public void addRuleDuringPrepare(Rule rule) {
if (rules == rawRules) {
// Ensure the original is preserved in case we need to do a restart
if (rawRules instanceof ArrayList) {
rules = (ArrayList) ((ArrayList)rawRules).clone();
} else {
rules = new ArrayList(rawRules);
}
// Rebuild the forward engine to use the cloned rules
instantiateRuleEngine(rules);
}
rules.add(rule);
}
/**
* Add a new preprocessing hook defining an operation that
* should be run when the preparation phase is underway.
*/
public void addPreprocessingHook(RulePreprocessHook hook) {
if (preprocessorHooks == null) {
preprocessorHooks = new ArrayList();
}
preprocessorHooks.add(hook);
}
/**
* 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.
*/
public void prepare() {
if (!isPrepared) {
isPrepared = true;
// Restore the original pre-hookProcess rules
rules = rawRules;
// Is there any data to bind in yet?
Graph data = null;
if (fdata != null) data = fdata.getGraph();
// initilize the deductions graph
fdeductions = new FGraph( Factory.createGraphMem() );
dataFind = (data == null) ? fdeductions : FinderUtil.cascade(fdeductions, fdata);
// Initialize the optional TGC caches
if (useTGCCaching) {
if (schemaGraph != null) {
// Check if we can just reuse the copy of the raw
if (
(transitiveEngine.checkOccurance(TransitiveReasoner.subPropertyOf, data) ||
transitiveEngine.checkOccurance(TransitiveReasoner.subClassOf, data) ||
transitiveEngine.checkOccurance(RDFS.domain.asNode(), data) ||
transitiveEngine.checkOccurance(RDFS.range.asNode(), data) )) {
// The data graph contains some ontology knowledge so split the caches
// now and rebuild them using merged data
transitiveEngine.insert(((OrigFBRuleInfGraph)schemaGraph).fdata, fdata);
}
} else {
if (data != null) {
transitiveEngine.insert(null, fdata);
}
}
// Insert any axiomatic statements into the caches
for (Iterator i = rules.iterator(); i.hasNext(); ) {
Rule r = (Rule)i.next();
if (r.bodyLength() == 0) {
// An axiom
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -