Search in sources :

Example 6 with QueryAnswer

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();
}
Also used : List(java.util.List) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Stream(java.util.stream.Stream) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Example 7 with QueryAnswer

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))));
    }
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Var(ai.grakn.graql.Var)

Example 8 with QueryAnswer

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;
}
Also used : QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ArrayList(java.util.ArrayList) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation)

Example 9 with QueryAnswer

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))));
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Before(org.junit.Before)

Example 10 with QueryAnswer

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));
        }
    });
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Timer(com.codahale.metrics.Timer) GetQuery(ai.grakn.graql.GetQuery) Keyspace(ai.grakn.Keyspace) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)29 Answer (ai.grakn.graql.admin.Answer)15 Concept (ai.grakn.concept.Concept)13 Test (org.junit.Test)13 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)9 Var (ai.grakn.graql.Var)8 GetQuery (ai.grakn.graql.GetQuery)6 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ConceptId (ai.grakn.concept.ConceptId)4 Type (ai.grakn.concept.Type)4 Unifier (ai.grakn.graql.admin.Unifier)4 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)4 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)4 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)3 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 List (java.util.List)3 Set (java.util.Set)3