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());
}
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());
}
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());
}
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));
}
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);
}
Aggregations