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.TriplePattern; 14 import com.hp.hpl.jena.reasoner.rulesys.Functor; 15 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; 16 17 28 public class GenericTripleMatchFrame extends GenericChoiceFrame { 29 30 31 Node_RuleVariable subjectVar; 32 33 34 Node_RuleVariable predicateVar; 35 36 37 Node_RuleVariable objectVar; 38 39 40 Functor objectFunctor; 41 42 43 TriplePattern goal; 44 45 49 public boolean bindResult(Triple triple, LPInterpreter interpreter) { 50 if (objectVar != null) interpreter.bind(objectVar, triple.getObject()); 51 int mark = interpreter.trail.size(); 52 if (objectFunctor != null) { 53 if (! functorMatch(triple, interpreter)) { 54 interpreter.unwindTrail(mark); 55 return false; 56 } 57 } 58 if (subjectVar != null) { 59 if (! interpreter.unify(subjectVar, triple.getSubject()) ) { 60 interpreter.unwindTrail(mark); 61 return false; 62 } 63 } 64 if (predicateVar != null) { 65 if (! interpreter.unify(predicateVar, triple.getPredicate()) ) { 66 interpreter.unwindTrail(mark); 67 return false; 68 } 69 70 } 71 return true; 72 } 73 74 78 public boolean functorMatch(Triple t, LPInterpreter interpreter) { 79 Node o = t.getObject(); 80 if (!Functor.isFunctor(o)) return false; 81 Functor f = (Functor)o.getLiteral().getValue(); 82 if ( ! f.getName().equals(objectFunctor.getName())) return false; 83 if ( f.getArgLength() != objectFunctor.getArgLength()) return false; 84 Node[] fargs = f.getArgs(); 85 Node[] oFargs = objectFunctor.getArgs(); 86 for (int i = 0; i < fargs.length; i++) { 87 if (!interpreter.unify(oFargs[i], fargs[i])) return false; 88 } 89 return true; 90 } 91 92 97 public void init(LPInterpreter interpreter) { 98 super.init(interpreter); 99 Node s = LPInterpreter.deref(interpreter.argVars[0]); 100 subjectVar = (s instanceof Node_RuleVariable) ? (Node_RuleVariable) s : null; 101 Node p = LPInterpreter.deref(interpreter.argVars[1]); 102 predicateVar = (p instanceof Node_RuleVariable) ? (Node_RuleVariable) p : null; 103 Node o = LPInterpreter.deref(interpreter.argVars[2]); 104 objectVar = (o instanceof Node_RuleVariable) ? (Node_RuleVariable) o : null; 105 if (Functor.isFunctor(o)) { 106 objectFunctor = (Functor)o.getLiteral().getValue(); 107 goal = new TriplePattern(s, p, null); 108 } else { 109 objectFunctor = null; 110 goal = new TriplePattern(s, p, o); 111 } 112 } 113 114 } 115 116 117 | Popular Tags |