Search in sources :

Example 21 with Answer

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();
}
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 22 with Answer

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))));
    }
}
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 23 with Answer

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());
}
Also used : 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) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Answer(ai.grakn.graql.admin.Answer) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 24 with Answer

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()));
    });
}
Also used : Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Example 25 with Answer

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));
        }
    });
}
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

Answer (ai.grakn.graql.admin.Answer)193 Test (org.junit.Test)164 QueryBuilder (ai.grakn.graql.QueryBuilder)104 GetQuery (ai.grakn.graql.GetQuery)69 GraknTx (ai.grakn.GraknTx)47 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)37 Concept (ai.grakn.concept.Concept)32 Var (ai.grakn.graql.Var)21 Set (java.util.Set)21 Unifier (ai.grakn.graql.admin.Unifier)19 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)18 Collectors (java.util.stream.Collectors)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 Sets (com.google.common.collect.Sets)16 List (java.util.List)16 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)15 Stream (java.util.stream.Stream)15 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)14 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)12