Search in sources :

Example 1 with TreeMapEquivalentRelation

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) TreeMapEquivalentRelation(fr.lirmm.graphik.util.TreeMapEquivalentRelation)

Aggregations

Atom (fr.lirmm.graphik.graal.api.core.Atom)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 Term (fr.lirmm.graphik.graal.api.core.Term)1 Variable (fr.lirmm.graphik.graal.api.core.Variable)1 TreeMapEquivalentRelation (fr.lirmm.graphik.util.TreeMapEquivalentRelation)1 LinkedList (java.util.LinkedList)1 TreeMap (java.util.TreeMap)1