1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.mem.GraphMem; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.reasoner.rulesys.*; 15 import com.hp.hpl.jena.util.PrintUtil; 16 import com.hp.hpl.jena.util.iterator.ClosableIterator; 17 import com.hp.hpl.jena.graph.*; 18 import java.util.*; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 30 public class BFRuleContext implements RuleContext { 31 32 protected BindingStack env; 33 34 35 protected Rule rule; 36 37 38 protected ForwardRuleInfGraphI graph; 39 40 41 protected List stack; 42 43 44 protected List pending; 45 46 47 protected List deletesPending = new ArrayList(); 48 49 50 protected Graph pendingCache; 51 52 protected static Log logger = LogFactory.getLog(BFRuleContext.class); 53 54 58 public BFRuleContext(ForwardRuleInfGraphI graph) { 59 this.graph = graph; 60 env = new BindingStack(); 61 stack = new ArrayList(); 62 pending = new ArrayList(); 63 pendingCache = new GraphMem(); 64 } 65 66 70 public BindingEnvironment getEnv() { 71 return env; 72 } 73 74 80 public BindingStack getEnvStack() { 81 return env; 82 } 83 84 88 public InfGraph getGraph() { 89 return graph; 90 } 91 92 96 public Rule getRule() { 97 return rule; 98 } 99 100 104 public void setRule(Rule rule) { 105 this.rule = rule; 106 } 107 108 111 public void addTriple(Triple t) { 112 if (graph.shouldTrace()) { 113 if (rule != null) { 114 logger.debug("Adding to stack (" + rule.toShortString() + "): " + PrintUtil.print(t)); 115 } else { 116 logger.debug("Adding to stack : " + PrintUtil.print(t)); 117 } 118 } 119 stack.add(t); 120 } 121 122 128 public void add(Triple t) { 129 if (graph.shouldTrace()) { 130 if (rule != null) { 131 logger.debug("Adding to pending (" + rule.toShortString() + "): " + PrintUtil.print(t)); 132 } else { 133 logger.debug("Adding to pending : " + PrintUtil.print(t)); 134 } 135 } 136 pending.add(t); 137 } 139 140 144 public void flushPending() { 145 for (Iterator i = pending.iterator(); i.hasNext(); ) { 146 Triple t = (Triple)i.next(); 147 stack.add(t); 148 graph.addDeduction(t); 149 i.remove(); 150 } 152 pending.clear(); 153 for (Iterator i = deletesPending.iterator(); i.hasNext(); ) { 155 Triple t = (Triple)i.next(); 156 graph.delete(t); 157 } 158 deletesPending.clear(); 159 } 160 161 165 public boolean contains(Triple t) { 166 return contains(t.getSubject(), t.getPredicate(), t.getObject()); 168 } 169 170 174 public boolean contains(Node s, Node p, Node o) { 175 ClosableIterator it = find(s, p, o); 177 boolean result = it.hasNext(); 178 it.close(); 179 return result; 180 } 181 182 187 public ClosableIterator find(Node s, Node p, Node o) { 188 return graph.findDataMatches(s, p, o); 190 } 191 192 197 public Triple getNextTriple() { 198 if (stack.size() > 0) { 199 Triple t = (Triple)stack.remove(stack.size() - 1); 200 return t; 201 } else { 202 return null; 203 } 204 } 205 206 209 public void resetEnv() { 210 env.reset(); 211 } 212 213 216 public void silentAdd(Triple t) { 217 ((SilentAddI)graph).silentAdd(t); 218 } 219 220 223 public void remove(Triple t) { 224 deletesPending.add(t); 225 } 227 228 } 229 230 259 | Popular Tags |