Search in sources :

Example 26 with Predicate

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

the class PredicateTest method equalsTest.

@Test
public void equalsTest() {
    String label = "label";
    int arity = 5;
    Predicate predicate = new Predicate(label, arity);
    Assert.assertTrue("Predicate not equals itself", predicate.equals(predicate));
    Predicate other = new Predicate(label, arity);
    Assert.assertTrue("Predicate not equals an other predicate", predicate.equals(other));
}
Also used : Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 27 with Predicate

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

the class PredicateTest method constructorTest.

@Test
public void constructorTest() {
    String label = "label";
    int arity = 5;
    Predicate predicate = new Predicate(label, arity);
    Assert.assertTrue(predicate.getIdentifier().equals(label));
    Assert.assertTrue(predicate.getArity() == arity);
}
Also used : Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 28 with Predicate

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

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

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

Predicate (fr.lirmm.graphik.graal.api.core.Predicate)77 Atom (fr.lirmm.graphik.graal.api.core.Atom)35 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)28 Term (fr.lirmm.graphik.graal.api.core.Term)27 Test (org.junit.Test)25 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)16 LinkedList (java.util.LinkedList)16 Theory (org.junit.experimental.theories.Theory)14 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)9 Rule (fr.lirmm.graphik.graal.api.core.Rule)9 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)9 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)7 DefaultURI (fr.lirmm.graphik.util.DefaultURI)7 TreeSet (java.util.TreeSet)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)6 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 Pair (org.apache.commons.lang3.tuple.Pair)6