1 10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode; 11 12 import com.hp.hpl.jena.graph.Triple; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.reasoner.rulesys.*; 15 16 import java.util.*; 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 20 39 public class GoalResults { 40 41 44 45 protected TriplePattern goal; 46 47 48 protected ArrayList resultSet; 49 50 51 protected HashSet resultSetIndex; 52 53 54 protected boolean isComplete; 55 56 57 protected boolean started = false; 58 59 61 protected Set dependents = new HashSet(); 62 63 64 protected BRuleEngine engine; 65 66 67 protected int refCount = 0; 68 69 70 protected boolean isSingleton = false; 71 72 static Log logger = LogFactory.getLog(GoalResults.class); 73 74 77 83 public GoalResults(TriplePattern goal, BRuleEngine ruleEngine) { 84 this.goal = goal; 85 resultSet = new ArrayList(); 86 resultSetIndex = new HashSet(); 87 isComplete = false; 88 engine = ruleEngine; 89 isSingleton = !(goal.getSubject().isVariable() || goal.getPredicate().isVariable() || goal.getObject().isVariable()); 90 } 91 92 96 public boolean isComplete() { 97 return isComplete; 98 } 99 100 103 public int numResults() { 104 if (!started) start(); 105 return resultSet.size(); 106 } 107 108 111 public Triple getResult(int n) { 112 return (Triple)resultSet.get(n); 113 } 114 115 119 public void addDependent(RuleState dependent) { 120 if (!isComplete) dependents.add(dependent); 121 } 122 123 126 public void flushDependents() { 127 for (Iterator i = dependents.iterator(); i.hasNext(); ) { 128 RuleState dep = (RuleState)i.next(); 129 engine.prependToAgenda(dep); 130 } 131 } 133 134 137 public BRuleEngine getEngine() { 138 return engine; 139 } 140 141 144 public void setComplete() { 145 if (!isComplete) { 146 if (engine.isTraceOn()) { 147 logger.debug("Completed " + this); 148 } 149 isComplete = true; 150 resultSetIndex = null; 151 flushDependents(); 152 dependents.clear(); 153 } 154 } 155 156 160 public void setAllComplete() { 161 isComplete = true; 162 dependents.clear(); 163 } 164 165 169 public void start() { 170 List rules = engine.rulesFor(goal); 171 for (Iterator i = rules.iterator(); i.hasNext(); ) { 172 Rule rule = (Rule)i.next(); 173 RuleState rs = RuleState.createInitialState(rule, this); 174 if (rs != null) { 175 engine.appendToAgenda(rs); 176 } 177 } 178 if (refCount <= 0) setComplete(); 179 started = true; 180 } 181 182 186 public boolean addResult(Triple result) { 187 if (!isComplete && !resultSetIndex.contains(result)) { 188 resultSet.add(result); 193 resultSetIndex.add(result); 194 if (isSingleton) { 195 setComplete(); 196 } else { 197 flushDependents(); 198 } 199 return true; 200 } 201 return false; 202 } 203 204 208 public void incRefCount() { 209 refCount++; 210 } 211 212 216 public void decRefCount() { 217 refCount--; 218 if (refCount <= 0) { 219 setComplete(); 220 } 221 } 222 223 226 public String toString() { 227 return "GoalResult for: " + goal; 228 } 229 } 230 231 | Popular Tags |