Search in sources :

Example 1 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class BackwardChainingTest method Test1.

/**
 * Test 1
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void Test1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("q(X1,X2), ppp(X2) :- r(X1)."));
    rules.add(DlgpParser.parseRule("pp(X) :- ppp(X)."));
    rules.add(DlgpParser.parseRule("p(X) :- pp(X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- q(X, Y), p(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);
}
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 2 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class BackwardChainingTest method getBody2.

/**
 * c(X) :- b(X,X).
 *
 * getBody([X]) => b(X, X).
 *
 * @throws ParseException
 */
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
    boolean isFound = false;
    while (rewIt.hasNext()) {
        ConjunctiveQuery rew = rewIt.next();
        CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
        if (it.hasNext()) {
            Atom a = it.next();
            if (a.getPredicate().equals(new Predicate("q", 2))) {
                isFound = true;
                List<Term> terms = a.getTerms();
                Assert.assertEquals(terms.get(0), terms.get(1));
            }
        }
    }
    Assert.assertTrue("Rewrite not found", isFound);
}
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) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 3 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class BackwardChainingTest method getUnification.

/**
 * c(X) :- p(X,Y,X). p(X,Y,X) :- a(X).
 *
 * ?(X) :- c(X).
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void getUnification(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,Y,X)."));
    rules.add(DlgpParser.parseRule("q(X,Y,X) :- s(X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
    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(3, 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)

Example 4 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class BackwardChainingTest method issue34_2.

@Theory
public void issue34_2(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
    try {
        RuleSet rules = new LinkedListRuleSet();
        rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
        rules.add(DlgpParser.parseRule("r(Z,T) :- q(X,Y,Z)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,X),p(a,b).");
        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(1, 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 5 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter 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)

Aggregations

ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)14 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 Theory (org.junit.experimental.theories.Theory)12 Atom (fr.lirmm.graphik.graal.api.core.Atom)3 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)3 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)3 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)2 Term (fr.lirmm.graphik.graal.api.core.Term)2 UnionOfConjunctiveQueries (fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries)2 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)2 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)2 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)2 DefaultUnionOfConjunctiveQueries (fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries)2 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)2 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)2 UniqFilter (fr.lirmm.graphik.util.stream.filter.UniqFilter)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 ParseException (fr.lirmm.graphik.graal.api.io.ParseException)1