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;
}
Aggregations