use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class HomomorphismTest method test.
@Test
public void test() throws HomomorphismException, IteratorException, AtomSetException {
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b)."));
InMemoryAtomSet positivePart = new LinkedListAtomSet();
positivePart.addAll(DlgpParser.parseAtomSet("p(a,b)."));
InMemoryAtomSet negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(b)."));
DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, Collections.singletonList(negatedPart));
BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts();
CloseableIterator<Substitution> res = h.execute(query, data);
Assert.assertTrue(res.hasNext());
res.next();
Assert.assertFalse(res.hasNext());
res.close();
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class HomomorphismTest method test5.
@Test
public void test5() throws HomomorphismException, IteratorException, AtomSetException {
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b), r(b,b), r(c,c), q(c), q(b)."));
Variable x = DefaultTermFactory.instance().createVariable("X");
InMemoryAtomSet positivePart = new LinkedListAtomSet();
positivePart.addAll(DlgpParser.parseAtomSet("p(a,X),r(W,Y),q(Y)."));
positivePart.iterator().next().setTerm(1, x);
InMemoryAtomSet negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(X)."));
negatedPart.iterator().next().setTerm(0, x);
DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, Collections.singletonList(negatedPart), Collections.<Term>emptyList());
BCC bcc = new BCC(new GraphBaseBackJumping(), true);
BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts(bcc.getBCCScheduler(), StarBootstrapper.instance(), new NFC2(), bcc.getBCCBackJumping());
CloseableIterator<Substitution> res = h.execute(query, data);
Assert.assertFalse(res.hasNext());
res.close();
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class QueryUnifier method aggregate.
/**
* Returns the aggregation of the given unifier and the receiving unifier
*
* @param u
* an unifier
* @return unifier
*/
public QueryUnifier aggregate(QueryUnifier u) {
// we create a piece that is the union of the two pieces
InMemoryAtomSet pieces = new LinkedListAtomSet();
pieces.addAll(getPiece());
pieces.addAll(u.getPiece());
// we create a rule that is the aggregation of the two rules
InMemoryAtomSet b = new LinkedListAtomSet();
InMemoryAtomSet h = new LinkedListAtomSet();
CloseableIteratorWithoutException<Atom> it = getRule().getBody().iterator();
while (it.hasNext()) {
Atom a = it.next();
b.add(a);
}
it = getRule().getHead().iterator();
while (it.hasNext()) {
Atom a = it.next();
h.add(a);
}
it = u.getRule().getBody().iterator();
while (it.hasNext()) {
Atom a = it.next();
b.add(a);
}
it = u.getRule().getHead().iterator();
while (it.hasNext()) {
Atom a = it.next();
h.add(a);
}
Rule rule = DefaultRuleFactory.instance().create(b, h);
// we create the partition which is the join of the two partitions
Partition<Term> part = getPartition().join(u.getPartition());
return new QueryUnifier(pieces, part, rule, getQuery());
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class UnifierUtils method getSinglePieceUnifiersAHR.
/**
* Returns the list of all single-piece unifier between the given query and
* the given atomic-head rule cannot work with IDCompilation ( have to
* conserve the fact that an atom of the query can only been associated by a
* single unification with an atom of the head
*
* <br/>
* AHR: Atomic Header Rule
*
* @param q
* the query that we want unify
* @param r
* the atomic-head rule that we want unify
* @return the ArrayList of all single-piece unifier between the query of
* the receiving object and R an atomic-head rule
*/
public static LinkedList<QueryUnifier> getSinglePieceUnifiersAHR(ConjunctiveQuery q, AtomicHeadRule r, RulesCompilation compilation) {
LinkedList<Atom> unifiableAtoms = getUnifiableAtoms(q, r, compilation);
LinkedList<QueryUnifier> unifiers = new LinkedList<QueryUnifier>();
Iterator<Atom> i = unifiableAtoms.iterator();
while (i.hasNext()) {
InMemoryAtomSet p = new LinkedListAtomSet();
Rule tmpRule = getSafeCopy(r);
AtomicHeadRule copy = new AtomicHeadRule(tmpRule.getBody(), tmpRule.getHead().iterator().next());
Atom toUnif = i.next();
p.add(toUnif);
Partition<Term> partition = new Partition<Term>(toUnif.getTerms(), copy.getHead().getAtom().getTerms());
// compute separating variable
LinkedList<Term> sep = AtomSetUtils.sep(p, q.getAtomSet());
// compute sticky variable
LinkedList<Term> sticky = TermPartitionUtils.getStickyVariable(partition, sep, copy);
InMemoryAtomSet pBar = AtomSetUtils.minus(q.getAtomSet(), p);
while (partition != null && !sticky.isEmpty()) {
CloseableIteratorWithoutException<Atom> ia = pBar.iterator();
while (partition != null && ia.hasNext()) {
Atom a = ia.next();
Iterator<Term> ix = sticky.iterator();
while (partition != null && ix.hasNext()) {
Term x = ix.next();
// all the atoms of Q/P which contain x must be add to P
if (a.getTerms().contains(x)) {
// isMappable
if (compilation.isMappable(a.getPredicate(), copy.getHead().getAtom().getPredicate())) {
p.add(a);
Partition<Term> part = partition.join(new Partition<Term>(a.getTerms(), copy.getHead().getAtom().getTerms()));
if (TermPartitionUtils.isAdmissible(part, copy)) {
partition = part;
} else
partition = null;
} else
partition = null;
}
}
}
if (partition != null) {
sep = AtomSetUtils.sep(p, q.getAtomSet());
pBar = AtomSetUtils.minus(q.getAtomSet(), p);
sticky = TermPartitionUtils.getStickyVariable(partition, sep, copy);
}
}
i.remove();
if (partition != null) {
QueryUnifier u = new QueryUnifier(p, partition, copy, q);
unifiers.add(u);
}
}
return unifiers;
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class UnifierUtils method getSinglePieceUnifiersNAHR.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
public static List<QueryUnifier> getSinglePieceUnifiersNAHR(ConjunctiveQuery q, Rule r, RulesCompilation compilation) {
LinkedList<QueryUnifier> u = new LinkedList<QueryUnifier>();
Rule ruleCopy = getSafeCopy(r);
HashMap<Atom, LinkedList<Partition<Term>>> possibleUnification = new HashMap<Atom, LinkedList<Partition<Term>>>();
// compute possible unification between atoms of Q and head(R)
CloseableIteratorWithoutException<Atom> it = q.iterator();
while (it.hasNext()) {
Atom a = it.next();
CloseableIteratorWithoutException<Atom> it2 = ruleCopy.getHead().iterator();
while (it2.hasNext()) {
Atom b = it2.next();
if (compilation.isMappable(a.getPredicate(), b.getPredicate())) {
Collection<Partition<Term>> unification = compilation.getUnification(a, b);
for (Partition<Term> partition : unification) {
if (TermPartitionUtils.isAdmissible(partition, ruleCopy)) {
if (possibleUnification.get(a) == null)
possibleUnification.put(a, new LinkedList<Partition<Term>>());
possibleUnification.get(a).add(partition);
}
}
}
}
}
LinkedList<Atom> atoms = getUnifiableAtoms(q, r, compilation);
for (Atom a : atoms) {
LinkedList<Partition<Term>> partitionList = possibleUnification.get(a);
if (partitionList != null) {
Iterator<Partition<Term>> i = partitionList.iterator();
while (i.hasNext()) {
Partition<Term> unif = i.next();
InMemoryAtomSet p = new LinkedListAtomSet();
p.add(a);
u.addAll(extend(p, unif, possibleUnification, q, ruleCopy));
i.remove();
}
}
}
return u;
}
Aggregations