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.*; 14 import com.hp.hpl.jena.reasoner.rulesys.impl.*; 15 import com.hp.hpl.jena.graph.*; 16 17 import java.util.*; 18 19 import com.hp.hpl.jena.util.OneToManyMap; 20 import com.hp.hpl.jena.util.iterator.*; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 37 public class BasicForwardRuleInfGraph extends BaseInfGraph implements ForwardRuleInfGraphI { 38 39 42 43 protected OneToManyMap derivations; 44 45 46 protected FGraph fdeductions; 47 48 49 protected Graph schemaGraph; 50 51 52 protected FRuleEngineI engine; 53 54 55 protected List rules; 56 57 58 protected boolean traceOn = false; 59 60 protected static Log logger = LogFactory.getLog(BasicForwardRuleInfGraph.class); 61 62 65 75 public BasicForwardRuleInfGraph(Reasoner reasoner, Graph schema) { 76 super(null, reasoner); 77 instantiateRuleEngine(null); 78 this.schemaGraph = schema; 79 } 80 81 92 public BasicForwardRuleInfGraph(Reasoner reasoner, List rules, Graph schema) { 93 super(null, reasoner); 94 instantiateRuleEngine(rules); 95 this.rules = rules; 96 this.schemaGraph = schema; 97 } 98 99 108 public BasicForwardRuleInfGraph(Reasoner reasoner, List rules, Graph schema, Graph data) { 109 this(reasoner, rules, schema); 110 rebind(data); 111 } 112 113 118 protected void instantiateRuleEngine(List rules) { 119 if (rules != null) { 120 engine = new FRuleEngine(this, rules); 121 } else { 122 engine = new FRuleEngine(this); 123 } 124 } 125 126 130 public void setRuleStore(Object ruleStore) { 131 engine.setRuleStore(ruleStore); 132 } 133 134 141 public void rebind(Graph data) { 142 fdata = new FGraph( data ); 143 rebind(); 144 } 145 146 153 public void rebind() { 154 isPrepared = false; 155 } 156 157 160 public Graph getSchemaGraph() { 161 return schemaGraph; 162 } 163 164 172 public synchronized void prepare() { 173 if (isPrepared) return; 174 isPrepared = true; 175 fdeductions = new FGraph( new GraphMem() ); 177 boolean rulesLoaded = false; 178 if (schemaGraph != null) { 179 rulesLoaded = preloadDeductions(schemaGraph); 180 } 181 if (rulesLoaded) { 182 engine.fastInit(fdata); 183 } else { 184 engine.init(true, fdata); 185 } 186 } 187 188 195 protected boolean preloadDeductions(Graph preloadIn) { 196 Graph d = fdeductions.getGraph(); 197 BasicForwardRuleInfGraph preload = (BasicForwardRuleInfGraph)preloadIn; 198 if (preload.rules == rules) { 200 for (Iterator i = preload.find(null, null, null); i.hasNext(); ) { 202 d.add((Triple)i.next()); 203 } 204 engine.setRuleStore(preload.engine.getRuleStore()); 205 return true; 206 } else { 207 return false; 208 } 209 } 210 211 214 public void addDeduction(Triple t) { 215 getDeductionsGraph().add(t); 216 } 217 218 229 public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) { 230 checkOpen(); 231 if (!isPrepared) prepare(); 232 ExtendedIterator result = null; 233 if (fdata == null) { 234 result = fdeductions.findWithContinuation(pattern, continuation); 235 } else { 236 if (continuation == null) { 237 result = fdata.findWithContinuation(pattern, fdeductions); 238 } else { 239 result = fdata.findWithContinuation(pattern, FinderUtil.cascade(fdeductions, continuation) ); 240 } 241 } 242 return result.filterDrop(Functor.acceptFilter); 243 } 244 245 250 public ExtendedIterator graphBaseFind(Node subject, Node property, Node object) { 251 return findWithContinuation(new TriplePattern(subject, property, object), null); 252 } 253 254 262 public ExtendedIterator find(TriplePattern pattern) { 263 return findWithContinuation(pattern, null); 264 } 265 266 267 271 public synchronized void performAdd(Triple t) { 272 fdata.getGraph().add(t); 273 if (isPrepared) { 274 engine.add(t); 275 } 276 } 277 278 281 public int graphBaseSize() { 282 checkOpen(); 283 if (!isPrepared) { 284 prepare(); 285 } 286 return fdata.getGraph().size() + fdeductions.getGraph().size(); 287 } 288 289 292 public void performDelete(Triple t) { 293 if (fdata != null) { 294 Graph data = fdata.getGraph(); 295 if (data != null) { 296 data.delete(t); 297 } 298 } 299 if (isPrepared) { 300 fdeductions.getGraph().delete(t); 301 } 302 } 303 304 307 public void close() { 308 if (!closed) { 309 engine = null; 310 fdeductions = null; 311 rules = null; 312 schemaGraph = null; 313 super.close(); 314 } 315 } 316 317 321 325 public void addBRule(Rule brule) { 326 throw new ReasonerException("Forward reasoner does not support hybrid rules - " + brule.toShortString()); 327 } 328 329 333 public void deleteBRule(Rule brule) { 334 throw new ReasonerException("Forward reasoner does not support hybrid rules - " + brule.toShortString()); 335 } 336 337 340 public Graph getDeductionsGraph() { 341 prepare(); 342 return fdeductions.getGraph(); 343 } 344 345 349 public Graph getCurrentDeductionsGraph() { 350 return fdeductions.getGraph(); 351 } 352 353 358 public ExtendedIterator findDataMatches(Node subject, Node predicate, Node object) { 359 return find(subject, predicate, object); 360 } 361 362 363 366 public void logDerivation(Triple t, Object derivation) { 367 derivations.put(t, derivation); 368 } 369 370 373 public void silentAdd(Triple t) { 374 fdeductions.getGraph().add(t); 375 } 376 377 380 383 public void setDerivationLogging(boolean recordDerivations) { 384 this.recordDerivations = recordDerivations; 385 engine.setDerivationLogging(recordDerivations); 386 if (recordDerivations) { 387 derivations = new OneToManyMap(); 388 } else { 389 derivations = null; 390 } 391 } 392 393 396 public boolean shouldLogDerivations() { 397 return recordDerivations; 398 } 399 400 404 public Iterator getDerivation(Triple t) { 405 if (derivations == null) { 406 return new NullIterator(); 407 } else { 408 return derivations.getAll(t); 409 } 410 } 411 412 416 public void setTraceOn(boolean state) { 417 traceOn = state; 418 } 419 420 424 public boolean shouldTrace() { 425 return traceOn && engine.shouldTrace(); 426 } 427 428 432 public long getNRulesFired() { 433 return engine.getNRulesFired(); 434 } 435 436 } 437 438 467 | Popular Tags |