Search in sources :

Example 81 with Atom

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

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

the class QueryUnifier method isCompatible.

/**
 * Returns true if the given unifier is compatible with the receiving
 * unifier
 *
 * @param u
 *            a unifier
 * @return boolean
 */
public boolean isCompatible(QueryUnifier u) {
    // if the pieces of the two unifiers have atom in common the unifiers
    // are not compatible
    CloseableIteratorWithoutException<Atom> it1 = u.getPiece().iterator();
    CloseableIteratorWithoutException<Atom> it2 = this.getPiece().iterator();
    while (it1.hasNext()) {
        Atom a1 = it1.next();
        while (it2.hasNext()) {
            Atom a2 = it2.next();
            if (a1.equals(a2)) {
                return false;
            }
        }
    }
    return TermPartitionUtils.getAssociatedSubstitution(this.getPartition().join(u.getPartition()), null) != null;
}
Also used : Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 83 with Atom

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

the class UnifierUtils method extend.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private static Collection<? extends QueryUnifier> extend(InMemoryAtomSet p, Partition<Term> unif, HashMap<Atom, LinkedList<Partition<Term>>> possibleUnification, ConjunctiveQuery q, Rule r) {
    LinkedList<QueryUnifier> u = new LinkedList<QueryUnifier>();
    // compute separating variable
    LinkedList<Term> sep = AtomSetUtils.sep(p, q.getAtomSet());
    // compute sticky variable
    LinkedList<Term> sticky = TermPartitionUtils.getStickyVariable(unif, sep, r);
    if (sticky.isEmpty()) {
        u.add(new QueryUnifier(p, unif, r, q));
    } else {
        // compute Pext the atoms of Pbar linked to P by the sticky
        // variables
        InMemoryAtomSet pBar = AtomSetUtils.minus(q.getAtomSet(), p);
        InMemoryAtomSet pExt = new LinkedListAtomSet();
        InMemoryAtomSet toRemove = new LinkedListAtomSet();
        for (Term t : sticky) {
            pBar.removeAll(toRemove);
            toRemove.clear();
            CloseableIteratorWithoutException<Atom> ib = pBar.iterator();
            while (ib.hasNext()) {
                Atom b = ib.next();
                if (b.getTerms().contains(t)) {
                    pExt.add(b);
                    toRemove.add(b);
                }
            }
        }
        Partition<Term> part;
        for (Partition<Term> uExt : preUnifier(pExt, r, possibleUnification)) {
            part = unif.join(uExt);
            if (part != null && TermPartitionUtils.isAdmissible(part, r)) {
                u.addAll(extend(AtomSetUtils.union(p, pExt), part, possibleUnification, q, r));
            }
        }
    }
    return u;
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 84 with Atom

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

the class UnifierUtils method getUnifiableAtoms.

/**
 * Returns the list of the atoms of the query that can be unify with the
 * head of R
 *
 * @param query
 *            the query to unify
 *
 * @param r
 *            the rule whose has the head to unify
 * @return the list of the atoms of the query that have the same predicate
 *         as the head atom of R
 */
public static LinkedList<Atom> getUnifiableAtoms(ConjunctiveQuery query, Rule r, RulesCompilation compilation) {
    LinkedList<Atom> answer = new LinkedList<Atom>();
    CloseableIteratorWithoutException<Atom> it = query.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        CloseableIteratorWithoutException<Atom> it2 = r.getHead().iterator();
        while (it2.hasNext()) {
            Atom b = it2.next();
            // isUnifiable just made a call to isMappable
            if (compilation.isMappable(a.getPredicate(), b.getPredicate()))
                answer.add(a);
        }
    }
    return answer;
}
Also used : Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList)

Example 85 with Atom

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

the class AtomTest method testContainsTrue.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.core.DefaultAtom#contains(fr.lirmm.graphik.graal.api.core.Term)}.
 */
@Theory
public void testContainsTrue(AtomFactory factory) {
    // given
    Atom a = factory.create(TestUtils.pXA);
    // when
    boolean contains = a.contains(TestUtils.X);
    // then
    Assert.assertTrue(contains);
}
Also used : Atom(fr.lirmm.graphik.graal.api.core.Atom) Theory(org.junit.experimental.theories.Theory)

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