use of ai.grakn.graql.Var in project grakn by graknlabs.
the class RelatesProperty method define.
@Override
public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException {
Var roleVar = role().var();
PropertyExecutor.Method relatesMethod = executor -> {
Role role = executor.get(roleVar).asRole();
executor.get(var).asRelationshipType().relates(role);
};
PropertyExecutor relatesExecutor = PropertyExecutor.builder(relatesMethod).requires(var, roleVar).build();
// This allows users to skip stating `$roleVar sub role` when they say `$var relates $roleVar`
PropertyExecutor.Method isRoleMethod = executor -> executor.builder(roleVar).isRole();
PropertyExecutor isRoleExecutor = PropertyExecutor.builder(isRoleMethod).produces(roleVar).build();
VarPatternAdmin superRoleVarPattern = superRole();
if (superRoleVarPattern != null) {
Var superRoleVar = superRoleVarPattern.var();
PropertyExecutor.Method subMethod = executor -> {
Role superRole = executor.get(superRoleVar).asRole();
executor.builder(roleVar).sub(superRole);
};
PropertyExecutor subExecutor = PropertyExecutor.builder(subMethod).requires(superRoleVar).produces(roleVar).build();
return ImmutableSet.of(relatesExecutor, isRoleExecutor, subExecutor);
} else {
return ImmutableSet.of(relatesExecutor, isRoleExecutor);
}
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class ReasonerQueryImpl method getVarTypeMap.
private Map<Var, Type> getVarTypeMap(Stream<IsaAtomBase> isas) {
HashMap<Var, Type> map = new HashMap<>();
isas.map(at -> new Pair<>(at.getVarName(), at.getSchemaConcept())).filter(p -> Objects.nonNull(p.getValue())).filter(p -> p.getValue().isType()).forEach(p -> {
Var var = p.getKey();
Type newType = p.getValue().asType();
Type type = map.get(var);
if (type == null)
map.put(var, newType);
else {
boolean isSubType = type.subs().anyMatch(t -> t.equals(newType));
if (isSubType)
map.put(var, newType);
}
});
return map;
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class ReasonerQueryImpl method getRoleSubstitution.
public Answer getRoleSubstitution() {
Map<Var, Concept> roleSub = new HashMap<>();
getAtoms(RelationshipAtom.class).flatMap(RelationshipAtom::getRolePredicates).forEach(p -> {
Concept concept = tx().getConcept(p.getPredicate());
if (concept == null)
throw GraqlQueryException.idNotFound(p.getPredicate());
roleSub.put(p.getVarName(), concept);
});
return new QueryAnswer(roleSub);
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class AtomicState method materialisedAnswer.
private Answer materialisedAnswer(Answer baseAnswer, InferenceRule rule, Unifier unifier) {
Answer answer = baseAnswer;
ReasonerAtomicQuery query = getQuery();
QueryCache<ReasonerAtomicQuery> cache = getCache();
ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(query, answer);
ReasonerAtomicQuery ruleHead = ReasonerQueries.atomic(rule.getHead(), answer);
Set<Var> queryVars = query.getVarNames().size() < ruleHead.getVarNames().size() ? unifier.keySet() : ruleHead.getVarNames();
boolean queryEquivalentToHead = subbedQuery.isEquivalent(ruleHead);
// check if the specific answer to ruleHead already in cache/db
Answer headAnswer = cache.getAnswer(ruleHead, answer).project(queryVars).unify(unifier);
// if not and query different than rule head do the same with the query
Answer queryAnswer = headAnswer.isEmpty() && queryEquivalentToHead ? cache.getAnswer(query, answer) : new QueryAnswer();
// ensure no duplicates created - only materialise answer if it doesn't exist in the db
if (headAnswer.isEmpty() && queryAnswer.isEmpty()) {
Answer materialisedSub = ruleHead.materialise(answer).findFirst().orElse(null);
if (!queryEquivalentToHead)
cache.recordAnswer(ruleHead, materialisedSub);
answer = materialisedSub.project(queryVars).unify(unifier);
} else {
answer = headAnswer.isEmpty() ? queryAnswer : headAnswer;
}
if (answer.isEmpty())
return answer;
return answer.merge(query.getSubstitution()).explain(new RuleExplanation(query, rule));
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class DefineQueryTest method whenExecutingADefineQuery_ResultContainsAllInsertedVars.
@Test
public void whenExecutingADefineQuery_ResultContainsAllInsertedVars() {
Var type = var("type");
Var type2 = var("type2");
// Note that two variables refer to the same type. They should both be in the result
DefineQuery query = qb.define(type.label("my-type").sub("entity"), type2.label("my-type"));
Answer result = query.execute();
assertThat(result.vars(), containsInAnyOrder(type, type2));
assertEquals(result.get(type), result.get(type2));
}
Aggregations