Search in sources :

Example 1 with TriplePattern

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;
    }
}
Also used : TriplePattern(org.apache.jena.reasoner.TriplePattern)

Example 2 with TriplePattern

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);
            }
        }
    }
}
Also used : ReasonerException(org.apache.jena.reasoner.ReasonerException) Triple(org.apache.jena.graph.Triple) TriplePattern(org.apache.jena.reasoner.TriplePattern)

Example 3 with TriplePattern

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);
    }
}
Also used : Node_RuleVariable(org.apache.jena.reasoner.rulesys.Node_RuleVariable) TriplePattern(org.apache.jena.reasoner.TriplePattern)

Example 4 with TriplePattern

use of org.apache.jena.reasoner.TriplePattern in project jena by apache.

the class TestTransitiveGraphCache method testBug1.

/**
 * Test a a case where an earlier version had a bug due to removing
 * a link which was required rather than redundant.
 */
public void testBug1() {
    TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
    cache.addRelation(new Triple(a, closedP, b));
    cache.addRelation(new Triple(c, closedP, a));
    cache.addRelation(new Triple(c, closedP, b));
    cache.addRelation(new Triple(a, closedP, c));
    TestUtil.assertIteratorValues(this, cache.find(new TriplePattern(a, directP, null)), new Object[] { new Triple(a, closedP, a), new Triple(a, closedP, b), new Triple(a, closedP, c) });
}
Also used : Triple(org.apache.jena.graph.Triple) TriplePattern(org.apache.jena.reasoner.TriplePattern) TransitiveGraphCache(org.apache.jena.reasoner.transitiveReasoner.TransitiveGraphCache)

Example 5 with TriplePattern

use of org.apache.jena.reasoner.TriplePattern in project jena by apache.

the class TestTransitiveGraphCache method testCycle.

/**
 * Test cycle detection.
 */
public void testCycle() {
    TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
    cache.addRelation(new Triple(a, closedP, b));
    cache.addRelation(new Triple(b, closedP, c));
    cache.addRelation(new Triple(a, closedP, c));
    cache.addRelation(new Triple(c, closedP, b));
    TestUtil.assertIteratorValues(this, cache.find(new TriplePattern(a, directP, null)), new Object[] { new Triple(a, closedP, a), new Triple(a, closedP, b), new Triple(a, closedP, c) });
}
Also used : Triple(org.apache.jena.graph.Triple) TriplePattern(org.apache.jena.reasoner.TriplePattern) TransitiveGraphCache(org.apache.jena.reasoner.transitiveReasoner.TransitiveGraphCache)

Aggregations

TriplePattern (org.apache.jena.reasoner.TriplePattern)16 Triple (org.apache.jena.graph.Triple)13 TransitiveGraphCache (org.apache.jena.reasoner.transitiveReasoner.TransitiveGraphCache)9 Graph (org.apache.jena.graph.Graph)2 InfGraph (org.apache.jena.reasoner.InfGraph)2 Node (org.apache.jena.graph.Node)1 ReasonerException (org.apache.jena.reasoner.ReasonerException)1 BuiltinException (org.apache.jena.reasoner.rulesys.BuiltinException)1 Node_RuleVariable (org.apache.jena.reasoner.rulesys.Node_RuleVariable)1 BindingVector (org.apache.jena.reasoner.rulesys.impl.BindingVector)1 JenaException (org.apache.jena.shared.JenaException)1