use of ai.grakn.graql.admin.Answer 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.admin.Answer 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.admin.Answer in project grakn by graknlabs.
the class LazyQueryCache method getAnswersWithUnifier.
@Override
public Pair<LazyAnswerIterator, MultiUnifier> getAnswersWithUnifier(Q query) {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
if (match != null) {
Q equivalentQuery = match.query();
MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
LazyAnswerIterator unified = match.cachedElement().unify(multiUnifier);
return new Pair<>(unified, multiUnifier);
}
Stream<Answer> answerStream = record(query, query.getQuery().stream().map(a -> a.explain(new LookupExplanation(query))));
return new Pair<>(new LazyAnswerIterator(answerStream), new MultiUnifierImpl());
}
use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.
the class LazyQueryCache method remove.
@Override
public void remove(Cache<Q, LazyAnswerIterator> c2, Set<Q> queries) {
c2.getQueries().stream().filter(queries::contains).filter(this::contains).forEach(q -> {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(q);
Set<Answer> s = match.cachedElement().stream().collect(Collectors.toSet());
s.removeAll(c2.getAnswerStream(q).collect(Collectors.toSet()));
this.putEntry(match.query(), new LazyAnswerIterator(s.stream()));
});
}
use of ai.grakn.graql.admin.Answer 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