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