use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class QueryCache method getAnswer.
/**
* find specific answer to a query in the cache
* @param query input query
* @param ans sought specific answer to the query
* @return found answer if any, otherwise empty answer
*/
public Answer getAnswer(Q query, Answer ans) {
if (ans.isEmpty())
return ans;
CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
if (match != null) {
Q equivalentQuery = match.query();
MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
// NB: only used when checking for materialised answer duplicates
Answer answer = match.cachedElement().stream().flatMap(a -> a.unify(multiUnifier)).filter(a -> a.containsAll(ans)).findFirst().orElse(null);
if (answer != null)
return answer;
}
// TODO should it create a cache entry?
List<Answer> answers = ReasonerQueries.create(query, ans).getQuery().execute();
return answers.isEmpty() ? new QueryAnswer() : answers.iterator().next();
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class ResourceAtom method materialise.
@Override
public Stream<Answer> materialise() {
Answer substitution = getParentQuery().getSubstitution();
AttributeType type = getSchemaConcept().asAttributeType();
Concept owner = substitution.get(getVarName());
Var resourceVariable = getPredicateVariable();
// if the attribute already exists, only attach a new link to the owner, otherwise create a new attribute
if (substitution.containsVar(resourceVariable)) {
Attribute attribute = substitution.get(resourceVariable).asAttribute();
attachAttribute(owner, attribute);
return Stream.of(substitution);
} else {
Attribute attribute = AttributeTypeImpl.from(type).putAttributeInferred(Iterables.getOnlyElement(getMultiPredicate()).getPredicate().equalsValue().get());
attachAttribute(owner, attribute);
return Stream.of(substitution.merge(new QueryAnswer(ImmutableMap.of(resourceVariable, attribute))));
}
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class ExplanationBuilder method buildExplanation.
public static List<Answer> buildExplanation(ai.grakn.graql.admin.Answer queryAnswer) {
final List<Answer> explanation = new ArrayList<>();
queryAnswer.getExplanation().getAnswers().forEach(answer -> {
AnswerExplanation expl = answer.getExplanation();
Atom atom = ((ReasonerAtomicQuery) expl.getQuery()).getAtom();
ai.grakn.graql.admin.Answer inferredAnswer = new QueryAnswer();
if (expl.isLookupExplanation()) {
ReasonerAtomicQuery rewrittenQuery = ReasonerQueries.atomic(atom.rewriteWithRelationVariable());
inferredAnswer = ReasonerQueries.atomic(rewrittenQuery, answer).getQuery().stream().findFirst().orElse(new QueryAnswer());
} else if (expl.isRuleExplanation()) {
Atom headAtom = ((RuleExplanation) expl).getRule().getHead().getAtom();
ReasonerAtomicQuery rewrittenQuery = ReasonerQueries.atomic(headAtom.rewriteWithRelationVariable());
inferredAnswer = headAtom.getMultiUnifier(atom, UnifierType.RULE).stream().map(Unifier::inverse).flatMap(unifier -> rewrittenQuery.materialise(answer.unify(unifier))).findFirst().orElse(new QueryAnswer());
}
explanation.add(Answer.create(inferredAnswer));
});
return explanation;
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class GraqlControllerInsertTest method setupMock.
@Before
public void setupMock() {
when(mockFactory.tx(eq(keyspace), any())).thenReturn(tx);
when(tx.keyspace()).thenReturn(keyspace);
when(printer.graqlString(any())).thenReturn(Json.object().toString());
// Describe expected response to a typical query
Query<Object> query = tx.graql().parser().parseQuery("insert $x isa person;");
Concept person = mock(Concept.class, RETURNS_DEEP_STUBS);
when(person.getId()).thenReturn(ConceptId.of("V123"));
when(person.isThing()).thenReturn(true);
when(person.asThing().type().getLabel()).thenReturn(Label.of("person"));
when(query.execute()).thenReturn(ImmutableList.of(new QueryAnswer(ImmutableMap.of(var("x"), person))));
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class GraqlController method explainGraql.
@GET
@Path("/kb/{keyspace}/explain")
private String explainGraql(Request request, Response response) throws RetryException, ExecutionException {
Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
String queryString = mandatoryQueryParameter(request, QUERY);
response.status(SC_OK);
return executeFunctionWithRetrying(() -> {
try (GraknTx tx = factory.tx(keyspace, GraknTxType.WRITE);
Timer.Context context = executeExplanation.time()) {
Answer answer = tx.graql().infer(true).parser().<GetQuery>parseQuery(queryString).execute().stream().findFirst().orElse(new QueryAnswer());
return mapper.writeValueAsString(ExplanationBuilder.buildExplanation(answer));
}
});
}
Aggregations