Search in sources :

Example 6 with RulesCompilation

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

the class ConjunctiveQueryWithCompilation method issueWithAtom2SubstitutionConverter.

@Theory
public void issueWithAtom2SubstitutionConverter(RulesCompilationFactory factory, AtomSet store) throws Exception {
    Assume.assumeFalse(store instanceof TripleStore);
    HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = AtomicQueryHomomorphism.instance();
    store.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X,Y,X) :- <P>(X,Y)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- <Q>(X,Y,Y).");
    CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
    Assert.assertTrue(results.hasNext());
    results.close();
    query = DlgpParser.parseQuery("?(Y) :- <Q>(X,Y,Y).");
    results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
    Assert.assertTrue(results.hasNext());
    results.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 7 with RulesCompilation

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

the class ConjunctiveQueryWithCompilation method test1.

// /////////////////////////////////////////////////////////////////////////
// TEST CASES
// /////////////////////////////////////////////////////////////////////////
@Theory
public void test1(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
    Assume.assumeFalse(store instanceof TripleStore);
    Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
    HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
    store.add(DlgpParser.parseAtom("<P>(a)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X) :- <P>(X)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    CloseableIterator<Substitution> results = h.execute(DlgpParser.parseQuery("?(X) :- <Q>(X)."), store, comp);
    Assert.assertTrue(results.hasNext());
    Substitution next = results.next();
    Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), next.createImageOf(DefaultTermFactory.instance().createVariable("X")));
    Assert.assertFalse(results.hasNext());
    results.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismWithCompilation(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation) Theory(org.junit.experimental.theories.Theory)

Example 8 with RulesCompilation

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

the class IDCompilationTest method test.

/**
 * Given p(X,Y) -> q(X,Y,X) <br>
 * Then rew(q(U,V,W)) <br>
 * Return q(U,V,W)-{} AND (p(U,V)-{W->U} || p(W,V)-{U->W})
 */
@Test
public void test() {
    Predicate predicateQ = new Predicate("q", 3);
    Predicate predicateP = new Predicate("p", 2);
    Atom body = new DefaultAtom(predicateP, X, Y);
    Atom head = new DefaultAtom(predicateQ, X, Y, X);
    Atom query = new DefaultAtom(predicateQ, U, V, W);
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DefaultRuleFactory.instance().create(body, head));
    RulesCompilation comp = new IDCompilation();
    comp.compile(rules.iterator());
    Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
    boolean rew1 = false;
    boolean rew2 = false;
    for (Pair<Atom, Substitution> p : rewritingOf) {
        Atom a = p.getLeft();
        Substitution s = p.getRight();
        if (a.getPredicate().equals(predicateQ)) {
            rew1 = true;
            Assert.assertEquals(U, a.getTerm(0));
            Assert.assertEquals(V, a.getTerm(1));
            Assert.assertEquals(W, a.getTerm(2));
            Assert.assertEquals(0, s.getTerms().size());
        } else {
            rew2 = true;
            Assert.assertEquals(predicateP, a.getPredicate());
            Assert.assertTrue(a.getTerm(0).equals(U) || a.getTerm(0).equals(W));
            Assert.assertEquals(V, a.getTerm(1));
            Assert.assertEquals(1, s.getTerms().size());
        }
    }
    Assert.assertTrue(rew1 && rew2);
    Assert.assertEquals(2, rewritingOf.size());
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 9 with RulesCompilation

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

the class IDCompilationTest method test3.

/**
 * Given p(X,Y) -> q(X,Y,X,Y,X) <br>
 * Then rew(q(U,V,A,B,V)) <br>
 * Return q(U,V,A,B,V)-{}
 */
@Test
public void test3() {
    Predicate predicateQ = new Predicate("q", 5);
    Predicate predicateP = new Predicate("p", 2);
    Atom body = new DefaultAtom(predicateP, X, Y);
    Atom head = new DefaultAtom(predicateQ, X, Y, X, Y, X);
    Atom query = new DefaultAtom(predicateQ, U, V, A, B, V);
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DefaultRuleFactory.instance().create(body, head));
    RulesCompilation comp = new IDCompilation();
    comp.compile(rules.iterator());
    Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
    Assert.assertEquals(1, rewritingOf.size());
    Pair<Atom, Substitution> p = rewritingOf.iterator().next();
    Atom a = p.getLeft();
    Substitution s = p.getRight();
    Assert.assertEquals(predicateQ, a.getPredicate());
    Assert.assertEquals(U, a.getTerm(0));
    Assert.assertEquals(V, a.getTerm(1));
    Assert.assertEquals(A, a.getTerm(2));
    Assert.assertEquals(B, a.getTerm(3));
    Assert.assertEquals(V, a.getTerm(4));
    Assert.assertEquals(0, s.getTerms().size());
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 10 with RulesCompilation

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

the class IDCompilationTest method test2.

/**
 * Given p(X,Y) -> q(X,Y,X) <br>
 * Then rew(q(U,U,U)) <br>
 * Return q(U,U,U)-{} AND p(U,U)-{})
 */
@Test
public void test2() {
    Predicate predicateQ = new Predicate("q", 3);
    Predicate predicateP = new Predicate("p", 2);
    Atom body = new DefaultAtom(predicateP, X, Y);
    Atom head = new DefaultAtom(predicateQ, X, Y, X);
    Atom query = new DefaultAtom(predicateQ, U, U, U);
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DefaultRuleFactory.instance().create(body, head));
    RulesCompilation comp = new IDCompilation();
    comp.compile(rules.iterator());
    Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
    boolean rew1 = false;
    boolean rew2 = false;
    for (Pair<Atom, Substitution> p : rewritingOf) {
        Atom a = p.getLeft();
        Substitution s = p.getRight();
        if (a.getPredicate().equals(predicateQ)) {
            rew1 = true;
            Assert.assertEquals(U, a.getTerm(0));
            Assert.assertEquals(U, a.getTerm(1));
            Assert.assertEquals(U, a.getTerm(2));
            Assert.assertEquals(0, s.getTerms().size());
        } else {
            rew2 = true;
            Assert.assertEquals(predicateP, a.getPredicate());
            Assert.assertEquals(U, a.getTerm(0));
            Assert.assertEquals(U, a.getTerm(1));
            Assert.assertEquals(0, s.getTerms().size());
        }
    }
    Assert.assertTrue(rew1 && rew2);
    Assert.assertEquals(2, rewritingOf.size());
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Aggregations

RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)13 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)13 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)13 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)11 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)9 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)9 Theory (org.junit.experimental.theories.Theory)8 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)7 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)7 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)6 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)5 Atom (fr.lirmm.graphik.graal.api.core.Atom)4 Test (org.junit.Test)4 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)3 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)3 IDCompilation (fr.lirmm.graphik.graal.core.compilation.IDCompilation)3 Pair (org.apache.commons.lang3.tuple.Pair)3 Variable (fr.lirmm.graphik.graal.api.core.Variable)1 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)1