1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.reasoner.rulesys.*; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.graph.*; 15 import java.util.*; 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 26 public class RETETerminal implements RETESinkNode { 27 28 29 protected RETERuleContext context; 30 31 protected static Log logger = LogFactory.getLog(FRuleEngine.class); 32 33 39 public RETETerminal(Rule rule, RETEEngine engine, ForwardRuleInfGraphI graph) { 40 context = new RETERuleContext(graph, engine); 41 context.rule = rule; 42 } 43 44 50 private RETETerminal(RETERuleContext context) { 51 this.context = context; 52 } 53 54 57 public void setContext(RETEEngine engine, ForwardRuleInfGraphI graph) { 58 Rule rule = context.getRule(); 59 context = new RETERuleContext(graph, engine); 60 context.setRule(rule); 61 } 62 63 68 public void fire(BindingVector env, boolean isAdd) { 69 Rule rule = context.getRule(); 70 context.setEnv(env); 71 72 for (int i = 0; i < rule.bodyLength(); i++) { 74 Object clause = rule.getBodyElement(i); 75 if (clause instanceof Functor) { 76 if (isAdd) { 78 if (!((Functor)clause).evalAsBodyClause(context)) { 79 return; 81 } 82 } else { 83 if (!((Functor)clause).safeEvalAsBodyClause(context)) { 85 return; 87 } 88 } 89 } 90 } 91 92 ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI)context.getGraph(); 94 if (infGraph.shouldTrace()) { 95 logger.info("Fired rule: " + rule.toShortString()); 96 } 97 RETEEngine engine = context.getEngine(); 98 engine.incRuleCount(); 99 List matchList = null; 100 if (infGraph.shouldLogDerivations() && isAdd) { 101 matchList = new ArrayList(rule.bodyLength()); 103 for (int i = 0; i < rule.bodyLength(); i++) { 104 Object clause = rule.getBodyElement(i); 105 if (clause instanceof TriplePattern) { 106 matchList.add(env.instantiate((TriplePattern)clause)); 107 } 108 } 109 } 110 for (int i = 0; i < rule.headLength(); i++) { 111 Object hClause = rule.getHeadElement(i); 112 if (hClause instanceof TriplePattern) { 113 Triple t = env.instantiate((TriplePattern) hClause); 114 if (!t.getSubject().isLiteral()) { 115 if (isAdd) { 119 if ( ! context.contains(t) ) { 120 engine.addTriple(t, true); 121 if (infGraph.shouldLogDerivations()) { 122 infGraph.logDerivation(t, new RuleDerivation(rule, t, matchList, infGraph)); 123 } 124 } 125 } else { 126 if ( context.contains(t)) { 127 engine.deleteTriple(t, true); 129 } 130 } 131 } 132 } else if (hClause instanceof Functor && isAdd) { 133 Functor f = (Functor)hClause; 134 Builtin imp = f.getImplementor(); 135 if (imp != null) { 136 imp.headAction(f.getBoundArgs(env), f.getArgLength(), context); 137 } else { 138 throw new ReasonerException("Invoking undefined Functor " + f.getName() +" in " + rule.toShortString()); 139 } 140 } else if (hClause instanceof Rule) { 141 Rule r = (Rule)hClause; 142 if (r.isBackward()) { 143 if (isAdd) { 144 infGraph.addBRule(r.instantiate(env)); 145 } else { 146 infGraph.deleteBRule(r.instantiate(env)); 147 } 148 } else { 149 throw new ReasonerException("Found non-backward subrule : " + r); 150 } 151 } 152 } 153 } 154 155 160 public RETENode clone(Map netCopy, RETERuleContext contextIn) { 161 RETETerminal clone = (RETETerminal)netCopy.get(this); 162 if (clone == null) { 163 RETERuleContext newContext = new RETERuleContext((ForwardRuleInfGraphI)contextIn.getGraph(), contextIn.getEngine()); 164 newContext.setRule(context.getRule()); 165 clone = new RETETerminal(newContext); 166 netCopy.put(this, clone); 167 } 168 return clone; 169 } 170 171 } 172 173 174 | Popular Tags |