use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ReasonerTest method testParsingQueryWithResourceVariable.
@Test
public void testParsingQueryWithResourceVariable() {
String patternString = "{$x isa person, has firstname $y;}";
String patternString2 = "{$x isa person;$x has firstname $y;}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, snbKB.tx()), snbKB.tx());
ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, snbKB.tx()), snbKB.tx());
assertEquals(query, query2);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class QueryTest method testAlphaEquivalence_indirectTypes.
// tests alpha-equivalence of queries with indirect types
@Test
public void testAlphaEquivalence_indirectTypes() {
EmbeddedGraknTx<?> graph = geoKB.tx();
String patternString = "{(entity-location: $x2, geo-entity: $x1) isa is-located-in;" + "$x1 isa $t1; $t1 sub geoObject;}";
String patternString2 = "{(geo-entity: $y1, entity-location: $y2) isa is-located-in;" + "$y1 isa $t2; $t2 sub geoObject;}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, graph), graph);
queryEquivalence(query, query2, true);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class RelationshipAtom method getExplicitRoles.
private Stream<Role> getExplicitRoles() {
ReasonerQueryImpl parent = (ReasonerQueryImpl) getParentQuery();
GraknTx graph = parent.tx();
return getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).map(graph::<Role>getSchemaConcept);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResourceAtom method isRuleApplicableViaAtom.
@Override
public boolean isRuleApplicableViaAtom(Atom ruleAtom) {
// findbugs complains about cast without it
if (!(ruleAtom instanceof ResourceAtom))
return false;
ResourceAtom childAtom = (ResourceAtom) ruleAtom;
ReasonerQueryImpl childQuery = (ReasonerQueryImpl) childAtom.getParentQuery();
// check type bindings compatiblity
Type parentType = this.getParentQuery().getVarTypeMap().get(this.getVarName());
Type childType = childQuery.getVarTypeMap().get(childAtom.getVarName());
if (parentType != null && childType != null && areDisjointTypes(parentType, childType) || !childQuery.isTypeRoleCompatible(ruleAtom.getVarName(), parentType))
return false;
// check value predicate compatibility
if (childAtom.getMultiPredicate().isEmpty() || getMultiPredicate().isEmpty())
return true;
for (ValuePredicate childPredicate : childAtom.getMultiPredicate()) {
Iterator<ValuePredicate> parentIt = getMultiPredicate().iterator();
boolean predicateCompatible = false;
while (parentIt.hasNext() && !predicateCompatible) {
ValuePredicate parentPredicate = parentIt.next();
predicateCompatible = parentPredicate.isCompatibleWith(childPredicate);
}
if (!predicateCompatible)
return false;
}
return true;
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl 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