1 10 package com.hp.hpl.jena.reasoner.rulesys; 11 12 import com.hp.hpl.jena.mem.GraphMem; 13 import com.hp.hpl.jena.reasoner.rulesys.impl.*; 14 import com.hp.hpl.jena.reasoner.*; 15 import com.hp.hpl.jena.graph.*; 16 import java.util.*; 17 18 import com.hp.hpl.jena.util.OneToManyMap; 19 import com.hp.hpl.jena.util.iterator.*; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 30 public class LPBackwardRuleInfGraph extends BaseInfGraph implements BackwardRuleInfGraphI { 31 32 35 36 protected LPBRuleEngine engine; 37 38 39 protected OneToManyMap derivations; 40 41 42 protected FGraph fschema; 43 44 45 protected FGraph fdeductions; 46 47 48 protected Finder dataFind; 49 50 51 protected TempNodeCache tempNodecache; 52 53 static Log logger = LogFactory.getLog(LPBackwardRuleInfGraph.class); 54 55 58 68 public LPBackwardRuleInfGraph(Reasoner reasoner, LPRuleStore ruleStore, Graph data, Graph schema) { 69 super(data, reasoner); 70 if (schema != null) { 71 fschema = new FGraph(schema); 72 } 73 engine = new LPBRuleEngine(this, ruleStore); 74 tempNodecache = new TempNodeCache(this); 75 } 76 77 80 public Graph getSchemaGraph() { 81 return fschema.getGraph(); 82 } 83 84 92 public void prepare() { 93 if (!isPrepared) { 94 fdeductions = new FGraph( new GraphMem() ); 95 extractAxioms(); 96 dataFind = fdata; 97 if (fdeductions != null) { 98 dataFind = FinderUtil.cascade(dataFind, fdeductions); 99 } 100 if (fschema != null) { 101 dataFind = FinderUtil.cascade(dataFind, fschema); 102 } 103 } 104 105 isPrepared = true; 106 } 107 108 115 public synchronized void rebind(Graph data) { 116 engine.checkSafeToUpdate(); 117 fdata = new FGraph(data); 118 isPrepared = false; 119 } 120 121 128 public synchronized void rebind() { 129 engine.checkSafeToUpdate(); 130 isPrepared = false; 131 } 132 133 136 public synchronized void reset() { 137 engine.checkSafeToUpdate(); 138 engine.reset(); 139 } 140 141 152 public synchronized ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) { 153 checkOpen(); 154 if (!isPrepared) prepare(); 155 ExtendedIterator result = engine.find(pattern); 156 if (continuation != null) { 157 result = result.andThen(continuation.find(pattern)); 158 } 159 return result.filterDrop(Functor.acceptFilter); 160 } 161 162 167 public ExtendedIterator graphBaseFind(Node subject, Node property, Node object) { 168 return findWithContinuation(new TriplePattern(subject, property, object), null); 169 } 170 171 179 public ExtendedIterator find(TriplePattern pattern) { 180 return findWithContinuation(pattern, null); 181 } 182 183 187 public synchronized void performAdd(Triple t) { 188 engine.checkSafeToUpdate(); 189 fdata.getGraph().add(t); 190 isPrepared = false; 191 } 192 193 196 public synchronized void performDelete(Triple t) { 197 engine.checkSafeToUpdate(); 198 fdata.getGraph().delete(t); 199 isPrepared = false; 200 } 201 202 205 public void setTabled(Node predicate) { 206 engine.tablePredicate(predicate); 207 if (isTraceOn()) { 208 logger.info("LP TABLE " + predicate); 209 } 210 } 211 212 215 218 public void setDerivationLogging(boolean recordDerivations) { 219 engine.setDerivationLogging(recordDerivations); 220 if (recordDerivations) { 221 derivations = new OneToManyMap(); 222 } else { 223 derivations = null; 224 } 225 } 226 227 231 public Iterator getDerivation(Triple t) { 232 if (derivations == null) { 233 return new NullIterator(); 234 } else { 235 return derivations.getAll(t); 236 } 237 } 238 239 243 public void setTraceOn(boolean state) { 244 engine.setTraceOn(state); 245 } 246 247 250 public boolean isTraceOn() { 251 return engine.isTraceOn(); 252 } 253 254 257 260 public void logDerivation(Triple t, Object derivation) { 261 derivations.put(t, derivation); 262 } 263 264 268 public ExtendedIterator findDataMatches(TriplePattern pattern) { 269 return dataFind.find(pattern); 270 } 271 272 279 public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) { 280 throw new ReasonerException("Internal error in FBLP rule engine, incorrect invocation of building in rule " + rule); 281 } 282 283 286 public void silentAdd(Triple t) { 287 fdeductions.getGraph().add(t); 288 } 289 290 297 public Node getTemp(Node instance, Node prop, Node pclass) { 298 return tempNodecache.getTemp(instance, prop, pclass); 299 } 300 301 304 308 protected void extractAxioms() { 309 Graph axioms = fdeductions.getGraph(); 310 BBRuleContext contextForBuiltins = null; 311 for (Iterator i = engine.getRuleStore().getAllRules().iterator(); i.hasNext(); ) { 312 Rule rule = (Rule)i.next(); 313 if (rule.bodyLength() == 0) { 314 for (int j = 0; j < rule.headLength(); j++) { 316 ClauseEntry axiom = rule.getHeadElement(j); 317 if (axiom instanceof TriplePattern) { 318 axioms.add(((TriplePattern)axiom).asTriple()); 319 } else if (axiom instanceof Functor) { 320 if (contextForBuiltins == null) { 321 contextForBuiltins = new BBRuleContext(this); 322 } 323 Functor f = (Functor)axiom; 324 Builtin implementation = f.getImplementor(); 325 if (implementation == null) { 326 throw new ReasonerException("Attempted to invoke undefined functor: " + f); 327 } 328 Node[] args = f.getArgs(); 329 contextForBuiltins.setEnv(new BindingVector(args)); 330 contextForBuiltins.setRule(rule); 331 implementation.headAction(args, args.length, contextForBuiltins); 332 } 333 } 334 } 335 } 336 } 337 338 } 339 340 341 342 | Popular Tags |