use of org.apache.jena.reasoner.TriplePattern in project jena by apache.
the class TestBackchainer method testUnify.
/**
* Test goal/head unify operation.
*/
public void testUnify() {
Node_RuleVariable xg = new Node_RuleVariable("?x", 0);
Node_RuleVariable yg = new Node_RuleVariable("?y", 1);
Node_RuleVariable zg = new Node_RuleVariable("?z", 2);
Node_RuleVariable xh = new Node_RuleVariable("?x", 0);
Node_RuleVariable yh = new Node_RuleVariable("?y", 1);
Node_RuleVariable zh = new Node_RuleVariable("?z", 2);
TriplePattern g1 = new TriplePattern(xg, p, yg);
TriplePattern g2 = new TriplePattern(xg, p, xg);
TriplePattern g3 = new TriplePattern(a, p, xg);
TriplePattern g4 = new TriplePattern(a, p, b);
TriplePattern h1 = new TriplePattern(xh, p, yh);
TriplePattern h2 = new TriplePattern(xh, p, xh);
TriplePattern h3 = new TriplePattern(a, p, xh);
TriplePattern h4 = new TriplePattern(a, p, b);
TriplePattern h5 = new TriplePattern(xh, p, a);
doTestUnify(g1, h1, true, new Node[] { null, null });
doTestUnify(g1, h2, true, new Node[] { null, null });
doTestUnify(g1, h3, true, new Node[] { null, null });
doTestUnify(g1, h4, true, new Node[] { null, null });
doTestUnify(g1, h5, true, new Node[] { null, null });
doTestUnify(g2, h1, true, new Node[] { null, xh });
doTestUnify(g2, h2, true, new Node[] { null, null });
doTestUnify(g2, h3, true, new Node[] { a, null });
doTestUnify(g2, h4, false, null);
doTestUnify(g2, h5, true, new Node[] { a, null });
doTestUnify(g3, h1, true, new Node[] { a, null });
doTestUnify(g3, h2, true, new Node[] { a, null });
doTestUnify(g3, h3, true, new Node[] { null, null });
doTestUnify(g3, h4, true, new Node[] { null, null });
doTestUnify(g3, h5, true, new Node[] { a, null });
doTestUnify(g4, h1, true, new Node[] { a, b });
doTestUnify(g4, h2, false, null);
doTestUnify(g4, h3, true, new Node[] { b });
doTestUnify(g4, h4, true, null);
doTestUnify(g4, h5, false, null);
// Recursive case
doTestUnify(h1, h1, true, new Node[] { null, null });
// Wildcard case
doTestUnify(new TriplePattern(null, null, null), h2, true, new Node[] { null, null });
// Test functor cases as well!
TriplePattern gf = new TriplePattern(xg, p, Functor.makeFunctorNode("f", new Node[] { xg, b }));
TriplePattern hf1 = new TriplePattern(yh, p, Functor.makeFunctorNode("f", new Node[] { zh, b }));
TriplePattern hf2 = new TriplePattern(yh, p, Functor.makeFunctorNode("f", new Node[] { a, yh }));
TriplePattern hf3 = new TriplePattern(yh, p, Functor.makeFunctorNode("f", new Node[] { b, yh }));
doTestUnify(gf, hf1, true, new Node[] { null, null, yh });
doTestUnify(gf, hf2, false, null);
doTestUnify(gf, hf3, true, new Node[] { null, b });
// Check binding environment use
BindingVector env = BindingVector.unify(g2, h1, MAX_VARS);
env.bind(xh, c);
assertEquals(env.getBinding(yh), c);
env = BindingVector.unify(g2, h1, MAX_VARS);
env.bind(yh, c);
assertEquals(env.getBinding(xh), c);
}
use of org.apache.jena.reasoner.TriplePattern in project jena by apache.
the class EnvironmentFrameWithDerivation method noteMatch.
/** Instantiate and record a matched subgoal */
public void noteMatch(TriplePattern pattern, int pc) {
TriplePattern match = pattern;
int term = clause.termIndex(pc);
if (term >= 0) {
matches[term] = match;
}
}
use of org.apache.jena.reasoner.TriplePattern in project jena by apache.
the class GenericTripleMatchFrame method init.
/**
* Initialize the triple match to preserve the current context of the given
* LPInterpreter and search for the match defined by the current argument registers
* @param interpreter the interpreter instance whose env, trail and arg values are to be preserved
*/
@Override
public void init(LPInterpreter interpreter) {
super.init(interpreter);
Node s = LPInterpreter.deref(interpreter.argVars[0]);
subjectVar = (s instanceof Node_RuleVariable) ? (Node_RuleVariable) s : null;
Node p = LPInterpreter.deref(interpreter.argVars[1]);
predicateVar = (p instanceof Node_RuleVariable) ? (Node_RuleVariable) p : null;
Node o = LPInterpreter.deref(interpreter.argVars[2]);
objectVar = (o instanceof Node_RuleVariable) ? (Node_RuleVariable) o : null;
if (Functor.isFunctor(o)) {
objectFunctor = (Functor) o.getLiteralValue();
goal = new TriplePattern(s, p, null);
} else {
objectFunctor = null;
goal = new TriplePattern(s, p, o);
}
}
use of org.apache.jena.reasoner.TriplePattern in project jena by apache.
the class RETEConflictSet method execute.
/**
* Execute a single rule firing.
*/
public static void execute(RETERuleContext context, boolean isAdd) {
Rule rule = context.getRule();
BindingEnvironment env = context.getEnv();
ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI) context.getGraph();
if (infGraph.shouldTrace()) {
logger.info("Fired rule: " + rule.toShortString());
}
RETEEngine engine = context.getEngine();
engine.incRuleCount();
List<Triple> matchList = null;
if (infGraph.shouldLogDerivations() && isAdd) {
// Create derivation record
matchList = new ArrayList<>(rule.bodyLength());
for (int i = 0; i < rule.bodyLength(); i++) {
Object clause = rule.getBodyElement(i);
if (clause instanceof TriplePattern) {
matchList.add(env.instantiate((TriplePattern) clause));
}
}
}
for (int i = 0; i < rule.headLength(); i++) {
Object hClause = rule.getHeadElement(i);
if (hClause instanceof TriplePattern) {
Triple t = env.instantiate((TriplePattern) hClause);
// that we can't record in RDF
if (isAdd) {
if (!context.contains(t)) {
engine.addTriple(t, true);
if (infGraph.shouldLogDerivations()) {
infGraph.logDerivation(t, new RuleDerivation(rule, t, matchList, infGraph));
}
}
} else {
if (context.contains(t)) {
// Remove the generated triple
engine.deleteTriple(t, true);
}
}
// }
} else if (hClause instanceof Functor && isAdd) {
Functor f = (Functor) hClause;
Builtin imp = f.getImplementor();
if (imp != null) {
imp.headAction(f.getBoundArgs(env), f.getArgLength(), context);
} else {
throw new ReasonerException("Invoking undefined Functor " + f.getName() + " in " + rule.toShortString());
}
} else if (hClause instanceof Rule) {
Rule r = (Rule) hClause;
if (r.isBackward()) {
if (isAdd) {
infGraph.addBRule(r.instantiate(env));
} else {
infGraph.deleteBRule(r.instantiate(env));
}
} else {
throw new ReasonerException("Found non-backward subrule : " + r);
}
}
}
}
use of org.apache.jena.reasoner.TriplePattern in project jena by apache.
the class TestTransitiveGraphCache method testDirect.
/**
* Test direct link case with adverse ordering.
*/
public void testDirect() {
TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
cache.addRelation(new Triple(a, closedP, b));
cache.addRelation(new Triple(c, closedP, d));
cache.addRelation(new Triple(a, closedP, d));
cache.addRelation(new Triple(b, closedP, c));
TestUtil.assertIteratorValues(this, cache.find(new TriplePattern(a, directP, null)), new Object[] { new Triple(a, closedP, a), new Triple(a, closedP, b) });
}
Aggregations