Search in sources :

Example 71 with Atom

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

the class DomainRestrictedProperty method check.

@Override
public int check(Rule rule) {
    boolean none;
    boolean all;
    Set<Variable> bodyVars = rule.getBody().getVariables();
    CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        all = none = true;
        for (Term t : bodyVars) {
            if (a.getTerms().contains(t)) {
                none = false;
            } else {
                all = false;
            }
            if (!none && !all) {
                return -1;
            }
        }
    }
    return 1;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 72 with Atom

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

the class GuardedProperty method check.

@Override
public int check(Rule rule) {
    Set<Variable> bodyVars = rule.getBody().getVariables();
    boolean isGuarded = true;
    CloseableIteratorWithoutException<Atom> it = rule.getBody().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        isGuarded = true;
        for (Term v : bodyVars) {
            if (!a.getTerms().contains(v)) {
                isGuarded = false;
                break;
            }
        }
        if (isGuarded) {
            break;
        }
    }
    if (isGuarded)
        return 1;
    return -1;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 73 with Atom

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

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

Example 75 with Atom

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

the class AtomTest method constructorTest.

@Test
public void constructorTest() {
    Predicate predicate = new Predicate("pred", 3);
    Term[] terms = new Term[3];
    terms[0] = new DefaultVariable("X");
    terms[1] = new DefaultConstant("a");
    terms[2] = new DefaultConstant("b");
    Atom atom = new DefaultAtom(predicate, Arrays.asList(terms));
    Assert.assertTrue(atom.getPredicate().equals(predicate));
    Assert.assertTrue(atom.getTerm(0).equals(terms[0]));
    Assert.assertTrue(atom.getTerm(1).equals(terms[1]));
    Assert.assertTrue(atom.getTerm(2).equals(terms[2]));
    Assert.assertTrue("The list eiuae ", atom.getTerms().equals(Arrays.asList(terms)));
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Aggregations

Atom (fr.lirmm.graphik.graal.api.core.Atom)269 Test (org.junit.Test)97 Term (fr.lirmm.graphik.graal.api.core.Term)86 Rule (fr.lirmm.graphik.graal.api.core.Rule)64 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)41 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)41 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)40 LinkedList (java.util.LinkedList)40 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)36 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)35 Theory (org.junit.experimental.theories.Theory)35 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)33 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)31 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)28 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)27 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13