Search in sources :

Example 1 with Equivalence

use of com.google.common.base.Equivalence in project grakn by graknlabs.

the class RuleUtils method getDependentRules.

/**
 * @param query top query
 * @return all rules that are reachable from the entry types
 */
public static Set<InferenceRule> getDependentRules(ReasonerQueryImpl query) {
    final Equivalence<Atom> equivalence = new Equivalence<Atom>() {

        @Override
        protected boolean doEquivalent(Atom a1, Atom a2) {
            return a1.isAlphaEquivalent(a2);
        }

        @Override
        protected int doHash(Atom a) {
            return a.alphaEquivalenceHashCode();
        }
    };
    Set<InferenceRule> rules = new HashSet<>();
    Set<Equivalence.Wrapper<Atom>> visitedAtoms = new HashSet<>();
    Stack<Equivalence.Wrapper<Atom>> atoms = new Stack<>();
    query.selectAtoms().stream().map(equivalence::wrap).forEach(atoms::push);
    while (!atoms.isEmpty()) {
        Equivalence.Wrapper<Atom> wrappedAtom = atoms.pop();
        Atom atom = wrappedAtom.get();
        if (!visitedAtoms.contains(wrappedAtom) && atom != null) {
            atom.getApplicableRules().peek(rules::add).flatMap(rule -> rule.getBody().selectAtoms().stream()).map(equivalence::wrap).filter(at -> !visitedAtoms.contains(at)).filter(at -> !atoms.contains(at)).forEach(atoms::add);
            visitedAtoms.add(wrappedAtom);
        }
    }
    return rules;
}
Also used : HashSet(java.util.HashSet) Stream(java.util.stream.Stream) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Equivalence(com.google.common.base.Equivalence) Iterator(java.util.Iterator) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraknTx(ai.grakn.GraknTx) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Schema(ai.grakn.util.Schema) Rule(ai.grakn.concept.Rule) Stack(java.util.Stack) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Stack(java.util.Stack) Equivalence(com.google.common.base.Equivalence) HashSet(java.util.HashSet)

Aggregations

GraknTx (ai.grakn.GraknTx)1 Rule (ai.grakn.concept.Rule)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)1 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)1 Schema (ai.grakn.util.Schema)1 Equivalence (com.google.common.base.Equivalence)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Stack (java.util.Stack)1 Stream (java.util.stream.Stream)1