use of fr.lirmm.graphik.util.TreeMapEquivalentRelation in project graal by graphik-team.
the class Rules method getPieces.
/**
* Compute and return the set of pieces of the head according to the
* frontier. On Rules with Existential Variables: Walking the Decidability
* Line Jean-François Baget, Michel Leclère, Marie-Laure Mugnier, Eric
* Salvat
*
* @return a Collection ofInMemoryAtomSet representing the set of pieces of the head of the specified rule.
*/
public static Collection<InMemoryAtomSet> getPieces(Rule rule) {
Set<Variable> existentials = rule.getExistentials();
Collection<InMemoryAtomSet> pieces = new LinkedList<InMemoryAtomSet>();
// compute equivalent classes
EquivalentRelation<Term> classes = new TreeMapEquivalentRelation<Term>();
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom a = it.next();
Term representant = null;
for (Term t : a) {
if (existentials.contains(t)) {
if (representant == null)
representant = t;
else
classes.mergeClasses(representant, t);
}
}
}
// init pieces for equivalent classes
Map<Integer, InMemoryAtomSet> tmpPieces = new TreeMap<Integer, InMemoryAtomSet>();
for (Term e : existentials) {
if (tmpPieces.get(classes.getIdClass(e)) == null) {
tmpPieces.put(classes.getIdClass(e), DefaultAtomSetFactory.instance().create());
}
}
// Affect atoms to one pieces
boolean isAffected;
InMemoryAtomSet atomset;
Term e;
it = rule.getHead().iterator();
while (it.hasNext()) {
Atom a = it.next();
isAffected = false;
Iterator<Variable> it2 = existentials.iterator();
while (it2.hasNext() && !isAffected) {
e = it2.next();
if (a.getTerms().contains(e)) {
tmpPieces.get(classes.getIdClass(e)).add(a);
isAffected = true;
}
}
if (!isAffected) {
// does not contain existential variable
atomset = DefaultAtomSetFactory.instance().create();
atomset.add(a);
pieces.add(atomset);
}
}
pieces.addAll(tmpPieces.values());
return pieces;
}
Aggregations