Search in sources :

Example 21 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class BackwardChainingTest method constantInRulesIssue62.

@Theory
public void constantInRulesIssue62(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X,a) :- q(X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- p(X,Y).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
    boolean found = false;
    int i = 0;
    while (it.hasNext()) {
        ConjunctiveQuery next = it.next();
        CloseableIteratorWithoutException<Atom> atomIt = next.getAtomSet().iterator();
        while (atomIt.hasNext()) {
            if (atomIt.next().getPredicate().equals(Predicate.EQUALITY)) {
                found = true;
            }
        }
        ++i;
    }
    Assert.assertTrue(found);
    Assert.assertEquals(2, i);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Theory(org.junit.experimental.theories.Theory)

Example 22 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class BackwardChainingTest method issue34_1.

@Theory
public void issue34_1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
    try {
        RuleSet rules = new LinkedListRuleSet();
        rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
        rules.add(DlgpParser.parseRule("r(X,Z) :- p(X,Y)."));
        rules.add(DlgpParser.parseRule("r(X,Z) :- q(X,Y)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,Y).");
        compilation.compile(rules.iterator());
        PureRewriter bc = new PureRewriter(operator, true);
        CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
        int i = Iterators.count(it);
        Assert.assertEquals(4, i);
    } catch (Throwable t) {
        Assert.assertFalse("There is an error.", true);
    }
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 23 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class PureRewriter method execute.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIteratorWithoutException<ConjunctiveQuery> execute(ConjunctiveQuery query, Iterable<Rule> rules) {
    RuleSet newRulSet = new LinkedListRuleSet(rules);
    RulesCompilation compilation = new IDCompilation();
    compilation.compile(newRulSet.iterator());
    RewritinCloseableIterator it = new RewritinCloseableIterator(true, query, newRulSet, compilation, this.operator);
    it.setProfiler(this.getProfiler());
    return it;
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) IDCompilation(fr.lirmm.graphik.graal.core.compilation.IDCompilation) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation)

Example 24 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class BackwardChainingTest method issue35.

@Theory
public void issue35(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
    try {
        RuleSet rules = new LinkedListRuleSet();
        Predicate p = new Predicate("p", 2);
        Predicate q = new Predicate("q", 3);
        Predicate r = new Predicate("r", 2);
        rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("? :- q(X,Y,Y), r(X,Y).");
        compilation.compile(rules.iterator());
        PureRewriter bc = new PureRewriter(operator, true);
        CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
        int i = 0;
        while (it.hasNext()) {
            ConjunctiveQuery next = it.next();
            InMemoryAtomSet atomSet = next.getAtomSet();
            Set<Predicate> predicates = atomSet.getPredicates();
            Assert.assertTrue(predicates.contains(r));
            if (predicates.contains(p)) {
                Assert.assertEquals(1, atomSet.getTerms().size());
            } else if (predicates.contains(q)) {
                Assert.assertEquals(query, next);
            } else {
                Assert.assertFalse(true);
            }
            ++i;
        }
        Assert.assertEquals(2, i);
    } catch (Exception e) {
        Assert.assertFalse("There is an error.", true);
    }
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) ParseException(fr.lirmm.graphik.graal.api.io.ParseException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 25 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class BackwardChainingTest method forbiddenFoldingTest.

/**
 * folding on answer variables
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void forbiddenFoldingTest(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("q(Y,X) :- p(X,Y)."));
    rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(Y,Z).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
    int i = count(it);
    Assert.assertEquals(4, i);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Aggregations

RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)39 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)37 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)22 Theory (org.junit.experimental.theories.Theory)22 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)13 Atom (fr.lirmm.graphik.graal.api.core.Atom)11 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)11 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)10 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)7 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)7 Test (org.junit.Test)7 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)5 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)5 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)5