Search in sources :

Example 16 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class QueryCacheTest method recordRetrieveSingleAnswer.

@Test
public void recordRetrieveSingleAnswer() {
    QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
    Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
    Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
    cache.recordAnswer(recordQuery, answer);
    assertEquals(cache.getAnswer(recordQuery, new QueryAnswer()), new QueryAnswer());
    assertEquals(cache.getAnswer(recordQuery, answer), answer);
    assertEquals(cache.getAnswer(recordQuery, retrieveAnswer), answer);
    assertEquals(cache.getAnswer(retrieveQuery, new QueryAnswer()), new QueryAnswer());
    assertEquals(cache.getAnswer(retrieveQuery, retrieveAnswer), retrieveAnswer);
    assertEquals(cache.getAnswer(retrieveQuery, answer), retrieveAnswer);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 17 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class QueryCacheTest method onStartup.

@Before
public void onStartup() {
    assumeTrue(GraknTestUtil.usingTinker());
    graph = testContext.tx();
    String recordPatternString = "{(role1: $x, role2: $y) isa reifiable-relation;}";
    String retrievePatternString = "{(role1: $p1, role2: $p2) isa reifiable-relation;}";
    Conjunction<VarPatternAdmin> recordPattern = conjunction(recordPatternString, graph);
    Conjunction<VarPatternAdmin> retrievePattern = conjunction(retrievePatternString, graph);
    recordQuery = ReasonerQueries.atomic(recordPattern, graph);
    retrieveQuery = ReasonerQueries.atomic(retrievePattern, graph);
    retrieveToRecordUnifier = retrieveQuery.getMultiUnifier(recordQuery).getUnifier();
    recordToRetrieveUnifier = retrieveToRecordUnifier.inverse();
    Entity entity = graph.getEntityType("anotherNoRoleEntity").instances().findFirst().orElse(null);
    singleAnswer = new QueryAnswer(ImmutableMap.of(var("x"), entity, var("y"), entity));
}
Also used : Entity(ai.grakn.concept.Entity) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Before(org.junit.Before)

Example 18 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class TypeInferenceQueryTest method typeInference.

private void typeInference(List<RelationshipType> possibleTypes, String pattern, String subbedPattern, EmbeddedGraknTx<?> graph) {
    ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
    ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(conjunction(subbedPattern, graph), graph);
    RelationshipAtom atom = (RelationshipAtom) query.getAtom();
    RelationshipAtom subbedAtom = (RelationshipAtom) subbedQuery.getAtom();
    List<Type> relationshipTypes = atom.inferPossibleTypes(new QueryAnswer());
    List<Type> subbedRelationshipTypes = subbedAtom.inferPossibleTypes(new QueryAnswer());
    if (possibleTypes.size() == 1) {
        assertEquals(possibleTypes, relationshipTypes);
        assertEquals(relationshipTypes, subbedRelationshipTypes);
        assertEquals(atom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
        assertEquals(subbedAtom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
    } else {
        assertTrue(CollectionUtils.isEqualCollection(possibleTypes, relationshipTypes));
        assertTrue(CollectionUtils.isEqualCollection(relationshipTypes, subbedRelationshipTypes));
        assertEquals(atom.getSchemaConcept(), null);
        assertEquals(subbedAtom.getSchemaConcept(), null);
    }
    typeInferenceQueries(possibleTypes, pattern, graph);
    typeInferenceQueries(possibleTypes, subbedPattern, graph);
}
Also used : Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Example 19 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class QueryOperationExecutor method insertAll.

private Answer insertAll(Answer results) {
    concepts.putAll(results.map());
    sortProperties().forEach(property -> property.executor().execute(this));
    conceptBuilders.forEach(this::buildConcept);
    ImmutableMap.Builder<Var, Concept> allConcepts = ImmutableMap.<Var, Concept>builder().putAll(concepts);
    // Make sure to include all equivalent vars in the result
    for (Var var : equivalentVars.getNodes()) {
        allConcepts.put(var, concepts.get(equivalentVars.componentOf(var)));
    }
    Map<Var, Concept> namedConcepts = Maps.filterKeys(allConcepts.build(), Var::isUserDefinedName);
    return new QueryAnswer(namedConcepts);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 20 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class GrpcServerTest method whenExecutingAQueryRemotelyAndAskingForOneResult_OnlyOneResultIsReturned.

// This tests uses an endless stream, so a failure may cause it to never terminate
@Test(timeout = 1000)
public void whenExecutingAQueryRemotelyAndAskingForOneResult_OnlyOneResultIsReturned() throws InterruptedException {
    Concept conceptX = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptX.getId()).thenReturn(ConceptId.of("V123"));
    when(conceptX.isEntity()).thenReturn(true);
    when(conceptX.asEntity().type().getLabel()).thenReturn(Label.of("L123"));
    Concept conceptY = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptY.getId()).thenReturn(ConceptId.of("V456"));
    when(conceptY.isEntity()).thenReturn(true);
    when(conceptY.asEntity().type().getLabel()).thenReturn(Label.of("L456"));
    ImmutableList<Answer> answers = ImmutableList.of(new QueryAnswer(ImmutableMap.of(Graql.var("x"), conceptX)), new QueryAnswer(ImmutableMap.of(Graql.var("y"), conceptY)));
    // TODO: reduce wtf
    when(query.results(any())).thenAnswer(params -> query.stream().map(params.<GrpcConverter>getArgument(0)::convert));
    // Produce an endless stream of results - this means if the behaviour is not lazy this will never terminate
    when(query.stream()).thenAnswer(params -> Stream.generate(answers::stream).flatMap(Function.identity()));
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(execQueryRequest(QUERY, null));
        IteratorId iterator = tx.receive().ok().getIteratorId();
        tx.send(nextRequest(iterator));
        tx.receive().ok();
        tx.send(nextRequest(iterator));
        tx.receive().ok();
        tx.send(stopRequest(iterator));
        TxResponse response = tx.receive().ok();
        assertEquals(doneResponse(), response);
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

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