Search in sources :

Example 81 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery 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 82 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery 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)

Example 83 with ConjunctiveQuery

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

the class BackwardChainingTest method Test2.

/**
 * Test 2
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void Test2(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X,Y) :- q(X,Y)."));
    rules.add(DlgpParser.parseRule("q(X,Y) :- p(Y,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);
    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 84 with ConjunctiveQuery

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

the class BackwardChainingTest method getBody1.

/**
 * c(X) :- b(X,Y,Y).
 *
 * getBody([X]) => b(X, Y, Y).
 *
 * @throws ParseException
 */
@Theory
public void getBody1(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,Y,Y)."));
    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", 3))) {
                isFound = true;
                List<Term> terms = a.getTerms();
                Assert.assertEquals(terms.get(1), terms.get(2));
            }
        }
    }
    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 85 with ConjunctiveQuery

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

the class BackwardChainingTest method queriesCover.

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

ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)113 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)58 Theory (org.junit.experimental.theories.Theory)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)34 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)29 Test (org.junit.Test)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)23 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)22 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)22 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)18 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)14 Term (fr.lirmm.graphik.graal.api.core.Term)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 LinkedList (java.util.LinkedList)13 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)12 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)11 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)10 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)9 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)7