1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.reasoner.rulesys.*; 15 import com.hp.hpl.jena.util.iterator.*; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import java.util.*; 20 21 31 public class LPBRuleEngine { 32 33 36 37 protected LPRuleStore ruleStore; 38 39 40 protected BackwardRuleInfGraphI infGraph; 41 42 43 protected boolean traceOn = false; 44 45 46 protected boolean recordDerivations; 47 48 49 protected List activeInterpreters = new ArrayList(); 50 51 53 protected HashMap tabledGoals = new HashMap(); 54 55 56 protected LinkedList agenda = new LinkedList(); 57 60 61 protected HashMap profile; 62 63 65 public static final int CYCLES_BETWEEN_COMPLETION_CHECK = 3; 66 67 static Log logger = LogFactory.getLog(LPBRuleEngine.class); 68 69 72 77 public LPBRuleEngine(BackwardRuleInfGraphI infGraph, LPRuleStore rules) { 78 this.infGraph = infGraph; 79 ruleStore = rules; 80 } 81 82 86 public LPBRuleEngine(BackwardRuleInfGraphI infGraph) { 87 this.infGraph = infGraph; 88 ruleStore = new LPRuleStore(); 89 } 90 91 94 99 public synchronized ExtendedIterator find(TriplePattern goal) { 100 LPInterpreter interpreter = new LPInterpreter(this, goal); 101 activeInterpreters.add(interpreter); 102 return WrappedIterator.create( new LPTopGoalIterator(interpreter)); 103 } 104 105 108 public synchronized void reset() { 109 checkSafeToUpdate(); 110 tabledGoals = new HashMap(); 111 agenda.clear(); 112 } 113 114 119 public synchronized void addRule(Rule rule) { 120 checkSafeToUpdate(); 121 ruleStore.addRule(rule); 122 } 123 124 129 public synchronized void deleteRule(Rule rule) { 130 checkSafeToUpdate(); 131 ruleStore.deleteRule(rule); 132 } 133 134 137 public synchronized List getAllRules() { 138 checkSafeToUpdate(); 139 return ruleStore.getAllRules(); 140 } 141 142 145 public synchronized void deleteAllRules() { 146 checkSafeToUpdate(); 147 ruleStore.deleteAllRules(); 148 } 149 150 153 public synchronized void halt() { 154 ArrayList copy = new ArrayList(activeInterpreters); 155 for (Iterator i = copy.iterator(); i.hasNext(); ) { 157 ((LPInterpreter)i.next()).close(); 158 } 159 } 160 161 165 public void setTraceOn(boolean state) { 166 traceOn = state; 167 } 168 169 172 public boolean isTraceOn() { 173 return traceOn; 174 } 175 176 179 public void setDerivationLogging(boolean recordDerivations) { 180 this.recordDerivations = recordDerivations; 181 } 182 183 186 public boolean getDerivationLogging() { 187 return recordDerivations; 188 } 189 190 191 public LPRuleStore getRuleStore() { 192 return ruleStore; 193 } 194 195 196 public BackwardRuleInfGraphI getInfGraph() { 197 return infGraph; 198 } 199 200 201 public synchronized void detach(LPInterpreter engine) { 202 activeInterpreters.remove(engine); 203 } 204 205 213 public void checkSafeToUpdate() { 214 if (!activeInterpreters.isEmpty()) { 215 ArrayList toClose = new ArrayList(); 216 for (Iterator i = activeInterpreters.iterator(); i.hasNext(); ) { 217 LPInterpreter interpreter = (LPInterpreter)i.next(); 218 if (interpreter.getContext() instanceof LPTopGoalIterator) { 219 toClose.add(interpreter.getContext()); 220 } 221 } 222 for (Iterator i = toClose.iterator(); i.hasNext(); ) { 223 ((LPTopGoalIterator)i.next()).close(); 224 } 225 } 226 } 227 228 229 232 236 public synchronized void tablePredicate(Node predicate) { 237 ruleStore.tablePredicate(predicate); 238 } 239 240 246 public synchronized Generator generatorFor(TriplePattern goal, List clauses) { 247 Generator generator = (Generator) tabledGoals.get(goal); 248 if (generator == null) { 249 LPInterpreter interpreter = new LPInterpreter(this, goal, clauses, false); 250 activeInterpreters.add(interpreter); 251 generator = new Generator(interpreter, goal); 252 schedule(generator); 253 tabledGoals.put(goal, generator); 254 } 255 return generator; 256 } 257 258 263 public synchronized Generator generatorFor(TriplePattern goal) { 264 Generator generator = (Generator) tabledGoals.get(goal); 265 if (generator == null) { 266 LPInterpreter interpreter = new LPInterpreter(this, goal, false); 267 activeInterpreters.add(interpreter); 268 generator = new Generator(interpreter, goal); 269 schedule(generator); 270 tabledGoals.put(goal, generator); 271 } 272 return generator; 273 } 274 275 279 public void schedule(LPAgendaEntry state) { 280 agenda.add(state); 281 } 282 283 286 public synchronized void pump(LPInterpreterContext gen) { 287 Collection batch = null; 289 if (CYCLES_BETWEEN_COMPLETION_CHECK > 0) { 290 batch = new ArrayList(CYCLES_BETWEEN_COMPLETION_CHECK); 291 } 292 int count = 0; 293 while(!gen.isReady()) { 294 if (agenda.isEmpty()) { 295 return; 297 } 298 int chosen = agenda.size() - 1; 302 LPAgendaEntry next = (LPAgendaEntry) agenda.get(chosen); 303 agenda.remove(chosen); 304 next.pump(); 306 count ++; 307 if (CYCLES_BETWEEN_COMPLETION_CHECK > 0) { 308 batch.add(next.getGenerator()); 309 if (count % CYCLES_BETWEEN_COMPLETION_CHECK == 0) { 310 Generator.checkForCompletions(batch); 311 batch.clear(); 312 } 313 } 314 } 315 if (CYCLES_BETWEEN_COMPLETION_CHECK > 0 && !batch.isEmpty()) { 316 Generator.checkForCompletions(batch); 317 } 318 319 } 321 322 325 public void checkForCompletions() { 326 ArrayList contexts = new ArrayList(activeInterpreters.size()); 327 for (Iterator i = activeInterpreters.iterator(); i.hasNext(); ) { 328 LPInterpreter interpreter = (LPInterpreter)i.next(); 329 if (interpreter.getContext() instanceof Generator) { 330 contexts.add(interpreter.getContext()); 331 } 332 } 333 Generator.checkForCompletions(contexts); 334 } 335 336 339 342 public void incrementProfile(RuleClauseCode clause) { 343 if (profile != null) { 344 String index = clause.toString(); 345 Count count = (Count)profile.get(index); 346 if (count == null) { 347 profile.put(index, new Count(clause).inc()); 348 } else { 349 count.inc(); 350 } 351 } 352 } 353 354 359 public void resetProfile(boolean enable) { 360 profile = enable ? new HashMap() : null; 361 } 362 363 366 public void printProfile() { 367 if (profile == null) { 368 System.out.println("No profile collected"); 369 } else { 370 ArrayList counts = new ArrayList(); 371 counts.addAll(profile.values()); 372 Collections.sort(counts); 373 System.out.println("LP engine rule profile"); 374 for (Iterator i = counts.iterator(); i.hasNext(); ) { 375 System.out.println(i.next()); 376 } 377 } 378 } 379 380 383 static class Count implements Comparable { 384 protected int count = 0; 385 protected RuleClauseCode clause; 386 387 388 public Count(RuleClauseCode clause) { 389 this.clause = clause; 390 } 391 392 393 public int getCount() { 394 return count; 395 } 396 397 398 public Count inc() { 399 count++; 400 return this; 401 } 402 403 404 public int compareTo(Object other) { 405 Count otherCount = (Count) other; 406 return (count < otherCount.count) ? -1 : ( (count == otherCount.count) ? 0 : +1); 407 } 408 409 410 public String toString() { 411 return " " + count + "\t - " + clause; 412 } 413 414 } 415 } 416 417 418 419 | Popular Tags |