📄 fbruleinfgraph.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: FBRuleInfGraph.java,v 1.65 2007/07/13 11:06:50 der Exp $
*****************************************************************/
package com.hp.hpl.jena.reasoner.rulesys;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.reasoner.rulesys.impl.*;
import com.hp.hpl.jena.reasoner.transitiveReasoner.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.shared.ReificationStyle;
import com.hp.hpl.jena.shared.impl.JenaParameters;
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.PrintUtil;
import com.hp.hpl.jena.util.iterator.*;
import com.hp.hpl.jena.vocabulary.*;
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.65 $ on $Date: 2007/07/13 11:06:50 $
*/
public class FBRuleInfGraph 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 LPBRuleEngine 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;
/** Flag, if true then find results will be filtered to remove functors and illegal RDF */
public boolean filterFunctors = true;
/** 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;
/** Table of temp nodes which should be hidden from output listings */
protected Set hiddenNodes;
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 FBRuleInfGraph(Reasoner reasoner, Graph schema) {
super(reasoner, schema);
constructorInit(schema);
}
/**
* 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 FBRuleInfGraph(Reasoner reasoner, List rules, Graph schema) {
this( reasoner, rules, schema, ReificationStyle.Minimal );
}
public FBRuleInfGraph( Reasoner reasoner, List rules, Graph schema, ReificationStyle style ) {
super( reasoner, rules, schema, style );
this.rawRules = rules;
constructorInit( schema );
}
/**
* 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 FBRuleInfGraph( Reasoner reasoner, List rules, Graph schema, Graph data ) {
super(reasoner, rules, schema, data);
this.rawRules = rules;
constructorInit(schema);
}
/**
* Common pieces of initialization code which apply in all constructor cases.
*/
private void constructorInit(Graph schema) {
initLP(schema);
tempNodecache = new TempNodeCache(this);
if (JenaParameters.enableFilteringOfHiddenInfNodes) {
hiddenNodes = new HashSet();
if (schema != null && schema instanceof FBRuleInfGraph) {
hiddenNodes.addAll(((FBRuleInfGraph)schema).hiddenNodes);
}
}
}
/**
* 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);
}
}
}
/**
* Initialize the LP engine, based on an optional schema graph.
*/
private void initLP(Graph schema) {
if (schema != null && schema instanceof FBRuleInfGraph) {
LPRuleStore newStore = new LPRuleStore();
newStore.addAll(((FBRuleInfGraph)schema).bEngine.getRuleStore());
bEngine = new LPBRuleEngine(this, newStore);
} else {
bEngine = new LPBRuleEngine(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;
resetTGCCache();
}
/**
* Rest the transitive graph caches
*/
private void resetTGCCache() {
if (schemaGraph != null) {
transitiveEngine = new TransitiveEngine(((FBRuleInfGraph)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) {
throw new ReasonerException("Internal error in FBLP rule engine, incorrect invocation of builtin in rule " + rule);
// TODO: Remove
// 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) {
if (logger.isDebugEnabled()) {
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) {
if (logger.isDebugEnabled()) {
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;
}
/**
* Set a predicate to be tabled/memoized by the LP engine.
*/
public void setTabled(Node predicate) {
bEngine.tablePredicate(predicate);
if (traceOn) {
logger.info("LP TABLE " + predicate);
}
}
/**
* 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) {
getCurrentDeductionsGraph().add(t);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -