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.rulesys.Node_RuleVariable; 14 15 import java.util.*; 16 17 30 public class ConsumerChoicePointFrame extends GenericTripleMatchFrame 31 implements LPAgendaEntry, LPInterpreterState { 32 33 34 protected Generator generator; 35 36 37 protected int resultIndex; 38 39 40 protected Node[] pVars; 41 42 43 protected Node_RuleVariable[] trailVars; 44 45 46 protected Node[] trailValues; 47 48 49 protected int trailLength; 50 51 52 protected LPInterpreterContext context; 53 54 59 public ConsumerChoicePointFrame(LPInterpreter interpreter) { 60 init(interpreter); 61 } 62 63 68 public void init(LPInterpreter interpreter) { 69 super.init(interpreter); 70 context = interpreter.getContext(); 71 generator = interpreter.getEngine().generatorFor(goal); 72 generator.addConsumer(this); 73 resultIndex = 0; 74 } 75 76 79 public void preserveState(List trail) { 80 int trailLen = trail.size(); 82 if (trailLen > trailLength) { 83 trailValues = new Node[trailLen]; 84 trailVars = new Node_RuleVariable[trailLen]; 85 } 86 trailLength = trailLen; 87 for (int i = 0; i < trailLen; i++) { 88 Node_RuleVariable var = (Node_RuleVariable) trail.get(i); 89 trailVars[i] = var; 90 trailValues[i] = var.getRawBoundValue(); 91 } 92 Node[] currentPVars = envFrame.pVars; 94 if (currentPVars != null) { 95 if (pVars == null || pVars.length < currentPVars.length) { 96 pVars = new Node[currentPVars.length]; 97 } 98 System.arraycopy(currentPVars, 0, pVars, 0, currentPVars.length); 99 } 100 } 101 102 105 public void restoreState(LPInterpreter interp) { 106 interp.unwindTrail(0); 107 for (int i = 0; i < trailLength; i++) { 108 interp.bind(trailVars[i], trailValues[i]); 109 } 110 if (pVars != null) { 111 System.arraycopy(pVars, 0, envFrame.pVars, 0, pVars.length); 112 } 113 } 114 115 122 public synchronized StateFlag nextMatch(LPInterpreter interpreter) { 123 while (resultIndex < generator.results.size()) { 124 Triple result = (Triple) generator.results.get(resultIndex++); 125 if (resultIndex >= generator.results.size() && generator.isComplete()) { 127 generator.removeConsumer(this); 128 } 129 if (bindResult(result, interpreter)) { 130 return StateFlag.SATISFIED; 131 } 132 } 133 if (generator.isComplete()) { 134 setFinished(); 135 generator.removeConsumer(this); 136 return StateFlag.FAIL; 137 } else { 138 return StateFlag.SUSPEND; 139 } 140 } 141 142 145 public boolean isReady() { 146 return generator.numResults() > resultIndex; 147 } 148 149 152 public void setReady() { 153 context.setReady(this); 154 } 155 156 160 public void setFinished() { 161 context.notifyFinished(this); 162 } 163 164 167 public void pump() { 168 if (context instanceof Generator) { 169 ((Generator)context).pump(this); 170 } else { 171 } 173 } 174 175 178 public Generator getGenerator() { 179 return generator; 180 } 181 182 185 public LPInterpreterContext getConsumingContext() { 186 return context; 187 } 188 189 } 190 191 192 | Popular Tags |