Search in sources :

Example 11 with Substitution

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

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

the class IDConditionTest method generateBodyTest4.

/**
 * h(X,Y,X,Y) :- b(X,Y).
 *
 * generateBody([X,Y,a,b]) => [a, b].
 */
@Test
public void generateBodyTest4() {
    Term[] body1 = { X, Y };
    Term[] head1 = { X, Y, X, Y };
    IDCondition cond = createCondition(body1, head1);
    Term[] head = { X, Y, A, B };
    Term[] expected = { A, B };
    Pair<List<Term>, Substitution> ret = cond.generateBody(Arrays.asList(head));
    List<Term> computed = ret.getLeft();
    Substitution s = ret.getRight();
    Assert.assertEquals(Arrays.asList(expected), computed);
    Assert.assertEquals(A, s.createImageOf(X));
    Assert.assertEquals(B, s.createImageOf(Y));
    Assert.assertEquals(2, s.getTerms().size());
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) List(java.util.List) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 13 with Substitution

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

the class IDConditionTest method generateBodyTest7.

/**
 * h(X,Y,X) :- b(X,Y).
 *
 * generateBody([X,Y,Y]) => [X, X] || [Y, Y].
 */
@Test
public void generateBodyTest7() {
    Term[] head = { X, Y, X };
    Term[] body = { X, Y };
    Term[] query = { X, Y, Y };
    IDCondition cond = createCondition(body, head);
    Pair<List<Term>, Substitution> ret = cond.generateBody(Arrays.asList(query));
    if (DEBUG) {
        System.out.println(ret);
    }
    List<Term> computed = ret.getLeft();
    Substitution s = ret.getRight();
    Assert.assertTrue(computed.get(0).equals(X) || computed.get(0).equals(Y));
    Assert.assertEquals(computed.get(0), computed.get(1));
    Assert.assertEquals(s.createImageOf(Y), s.createImageOf(X));
    // {X -> Y} || {Y -> X}
    Assert.assertEquals(1, s.getTerms().size());
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) List(java.util.List) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 14 with Substitution

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

the class IDConditionTest method homomorphismIssue35Test.

/**
 * Given an IDCondition[(x,y) -> (x,y,x)] then homomorphism([u,v,w], [a,b])
 * return (u->a, v->b, w->a)
 */
@Test
public void homomorphismIssue35Test() {
    // Assume.assumeTrue(false); // FIXME #35
    Term[] body = { X, Y };
    Term[] head = { X, Y, X };
    IDCondition cond = createCondition(body, head);
    Term[] query = { U, V, W };
    Term[] data = { A, B };
    Substitution h = cond.homomorphism(Arrays.asList(query), Arrays.asList(data));
    Assert.assertEquals(A, h.createImageOf(U));
    Assert.assertEquals(B, h.createImageOf(V));
    Assert.assertEquals(A, h.createImageOf(W));
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 15 with Substitution

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

the class IDConditionTest method homomorphismFailTest1.

/**
 * Given an IDCondition[(x,y) -> (x,y)] then homomorphism([x,x], [a,b])
 * return null
 */
@Test
public void homomorphismFailTest1() {
    Term[] body = { X, Y };
    Term[] head = { X, Y };
    IDCondition cond = createCondition(body, head);
    Term[] query = { U, U };
    Term[] data = { A, B };
    Substitution h = cond.homomorphism(Arrays.asList(query), Arrays.asList(data));
    Assert.assertNull(h);
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)158 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)58 Test (org.junit.Test)55 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)46 Theory (org.junit.experimental.theories.Theory)44 Atom (fr.lirmm.graphik.graal.api.core.Atom)41 Term (fr.lirmm.graphik.graal.api.core.Term)36 LinkedList (java.util.LinkedList)27 Variable (fr.lirmm.graphik.graal.api.core.Variable)25 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)24 Rule (fr.lirmm.graphik.graal.api.core.Rule)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)18 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)16 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)15 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)13 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)13