1 10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode; 11 12 import com.hp.hpl.jena.mem.GraphMem; 13 import com.hp.hpl.jena.reasoner.rulesys.*; 14 import com.hp.hpl.jena.reasoner.rulesys.impl.*; 15 import com.hp.hpl.jena.reasoner.*; 16 import com.hp.hpl.jena.graph.*; 17 import java.util.*; 18 19 import com.hp.hpl.jena.util.OneToManyMap; 20 import com.hp.hpl.jena.util.iterator.*; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 31 public class BasicBackwardRuleInfGraph extends BaseInfGraph implements BackwardRuleInfGraphI { 32 33 36 37 protected List rules; 38 39 40 protected OneToManyMap derivations; 41 42 43 protected FGraph fschema; 44 45 46 protected FGraph fdeductions; 47 48 49 protected Finder dataFind; 50 51 52 protected BRuleEngine engine; 53 54 55 protected BBRuleContext context; 56 57 58 protected TempNodeCache tempNodecache; 59 60 61 int nRulesTriggered = 0; 62 63 64 long nRulesFired = 0; 65 66 67 long nRulesThreshold = DEFAULT_RULES_THRESHOLD; 68 69 70 boolean traceOn = false; 71 72 73 public static final long DEFAULT_RULES_THRESHOLD = 500000; 74 75 static Log logger = LogFactory.getLog(BasicBackwardRuleInfGraph.class); 76 77 80 90 public BasicBackwardRuleInfGraph(Reasoner reasoner, RuleStore ruleStore, Graph data, Graph schema) { 91 super(data, reasoner); 92 if (schema != null) { 93 fschema = new FGraph(schema); 94 } 95 rules = ruleStore.getAllRules(); 96 engine = new BRuleEngine(this, ruleStore); 98 tempNodecache = new TempNodeCache(this); 99 } 100 101 104 public Graph getSchemaGraph() { 105 return fschema.getGraph(); 106 } 107 108 116 public void prepare() { 117 if (!isPrepared) { 118 fdeductions = new FGraph( new GraphMem() ); 119 extractAxioms(); 120 dataFind = fdata; 121 if (fdeductions != null) { 122 dataFind = FinderUtil.cascade(dataFind, fdeductions); 123 } 124 if (fschema != null) { 125 dataFind = FinderUtil.cascade(dataFind, fschema); 126 } 127 128 context = new BBRuleContext(this); 129 } 130 131 isPrepared = true; 132 } 133 134 141 public void rebind(Graph data) { 142 fdata = new FGraph(data); 143 engine.reset(); 144 isPrepared = false; 145 } 146 147 154 public void rebind() { 155 engine.reset(); 156 isPrepared = false; 157 } 158 159 170 public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) { 171 checkOpen(); 172 if (!isPrepared) prepare(); 173 ExtendedIterator result = null; 174 if (continuation == null) { 175 result = WrappedIterator.create( new TopGoalIterator(engine, pattern) ); 176 } else { 177 result = WrappedIterator.create( new TopGoalIterator(engine, pattern) ) 178 .andThen(continuation.find(pattern)); 179 } 180 return result.filterDrop(Functor.acceptFilter); 181 } 182 183 188 public ExtendedIterator graphBaseFind(Node subject, Node property, Node object) { 189 return findWithContinuation(new TriplePattern(subject, property, object), null); 190 } 191 192 200 public ExtendedIterator find(TriplePattern pattern) { 201 return findWithContinuation(pattern, null); 202 } 203 204 207 public void reset() { 208 engine.reset(); 209 isPrepared = false; 210 } 211 212 216 public synchronized void performAdd(Triple t) { 217 fdata.getGraph().add(t); 218 reset(); 219 } 220 221 224 public void performDelete(Triple t) { 225 fdata.getGraph().delete(t); 226 reset(); 227 } 228 229 232 235 public void setDerivationLogging(boolean recordDerivations) { 236 this.recordDerivations = recordDerivations; 237 if (recordDerivations) { 238 derivations = new OneToManyMap(); 239 } else { 240 derivations = null; 241 } 242 } 243 244 248 public Iterator getDerivation(Triple t) { 249 if (derivations == null) { 250 return new NullIterator(); 251 } else { 252 return derivations.getAll(t); 253 } 254 } 255 256 261 public void setRuleThreshold(long threshold) { 262 nRulesThreshold = threshold; 263 } 264 265 269 public void setTraceOn(boolean state) { 270 traceOn = state; 271 engine.setTraceOn(state); 272 } 273 274 277 public boolean isTraceOn() { 278 return traceOn; 279 } 280 281 285 public void dump() { 286 engine.dump(); 287 } 288 289 292 295 public void logDerivation(Triple t, Object derivation) { 296 derivations.put(t, derivation); 297 } 298 299 303 public ExtendedIterator findDataMatches(TriplePattern pattern) { 304 return dataFind.find(pattern); 305 } 306 307 314 public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) { 315 if (clause instanceof Functor) { 316 context.setEnv(env); 317 context.setRule(rule); 318 return((Functor)clause).evalAsBodyClause(context); 319 } else { 320 throw new ReasonerException("Illegal builtin predicate: " + clause + " in rule " + rule); 321 } 322 } 323 324 327 public void silentAdd(Triple t) { 328 fdeductions.getGraph().add(t); 329 } 330 331 338 public Node getTemp(Node instance, Node prop, Node pclass) { 339 return tempNodecache.getTemp(instance, prop, pclass); 340 } 341 342 345 349 protected void extractAxioms() { 350 Graph axioms = fdeductions.getGraph(); 351 for (Iterator i = rules.iterator(); i.hasNext(); ) { 352 Rule rule = (Rule)i.next(); 353 if (rule.bodyLength() == 0) { 354 for (int j = 0; j < rule.headLength(); j++) { 356 Object axiom = rule.getHeadElement(j); 357 if (axiom instanceof TriplePattern) { 358 axioms.add(((TriplePattern)axiom).asTriple()); 359 } 360 } 361 } 362 } 363 } 364 365 366 } 367 368 369 | Popular Tags |