use of fr.lirmm.graphik.util.Partition in project graal by graphik-team.
the class IDCompilation method getUnification.
@Override
public LinkedList<Partition<Term>> getUnification(Atom father, Atom son) {
LinkedList<Partition<Term>> res = new LinkedList<Partition<Term>>();
Predicate predB = son.getPredicate();
Predicate predH = father.getPredicate();
List<IDCondition> conds = getConditions(predB, predH);
for (IDCondition cond : conds) {
Partition<Term> unif = cond.generateUnification(son.getTerms(), father.getTerms());
if (unif != null) {
res.add(unif);
}
}
return res;
}
use of fr.lirmm.graphik.util.Partition in project graal by graphik-team.
the class IDConditionImpl method generateUnification.
/**
* Return the partition that unify the term of head with the term of body
* according to this. This method returns null the unification is not
* possible (two constants in the same partition class)
*/
@Override
public Partition<Term> generateUnification(List<Term> body, List<Term> head) {
Partition<Term> res = new Partition<Term>();
Term[] map = new Term[body.size()];
// put together term of body that must be unify according to this
for (int i = 0; i < condBody.length; ++i) {
Term t = body.get(i);
if (map[condBody[i]] == null) {
map[condBody[i]] = t;
} else {
res.add(map[condBody[i]], t);
}
}
// according this
for (int i = 0; i < condHead.length; i++) {
Term t = head.get(i);
res.add(map[condHead[i]], t);
}
// check validity (does not contains two different constants)
for (ArrayList<Term> classs : res) {
Term cst = null;
for (Term t : classs) {
if (t.isConstant()) {
if (cst == null)
cst = t;
else if (!cst.equals(t))
return null;
}
}
}
return res;
}
use of fr.lirmm.graphik.util.Partition 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.util.Partition 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;
}
use of fr.lirmm.graphik.util.Partition in project graal by graphik-team.
the class UnifierUtils method preUnifier.
private static LinkedList<Partition<Term>> preUnifier(InMemoryAtomSet p, Rule r, HashMap<Atom, LinkedList<Partition<Term>>> possibleUnification) {
LinkedList<Partition<Term>> res = new LinkedList<Partition<Term>>();
CloseableIteratorWithoutException<Atom> it = p.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (possibleUnification.get(a) != null)
for (Partition<Term> ua : possibleUnification.get(a)) {
InMemoryAtomSet fa = new LinkedListAtomSet();
fa.add(a);
InMemoryAtomSet aBar = null;
aBar = AtomSetUtils.minus(p, fa);
if (!aBar.iterator().hasNext())
res.add(ua);
else {
Partition<Term> part;
for (Partition<Term> u : preUnifier(aBar, r, possibleUnification)) {
part = ua.join(u);
if (part != null && TermPartitionUtils.isAdmissible(part, r)) {
res.add(part);
}
}
}
}
else
return res;
}
return res;
}
Aggregations