Search in sources :

Example 1 with QueryResult

use of ai.grakn.rpc.generated.GrpcGrakn.QueryResult in project grakn by graknlabs.

the class RemoteGraknTxTest method whenExecutingAQueryWithASingleAnswer_GetAnAnswerBack.

@Test
public void whenExecutingAQueryWithASingleAnswer_GetAnAnswerBack() {
    Query<?> query = define(label("person").sub("entity"));
    String queryString = query.toString();
    GrpcConcept.Concept v123 = GrpcConcept.Concept.newBuilder().setId(V123).build();
    GrpcGrakn.Answer grpcAnswer = GrpcGrakn.Answer.newBuilder().putAnswer("x", v123).build();
    QueryResult queryResult = QueryResult.newBuilder().setAnswer(grpcAnswer).build();
    TxResponse response = TxResponse.newBuilder().setQueryResult(queryResult).build();
    server.setResponseSequence(GrpcUtil.execQueryRequest(query), response);
    Answer answer;
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        answer = tx.graql().<DefineQuery>parse(queryString).execute();
    }
    assertEquals(answer.vars(), ImmutableSet.of(var("x")));
    assertEquals(ConceptId.of("V123"), answer.get(var("x")).getId());
}
Also used : Answer(ai.grakn.graql.admin.Answer) GraknTx(ai.grakn.GraknTx) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) GrpcGrakn(ai.grakn.rpc.generated.GrpcGrakn) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) DefineQuery(ai.grakn.graql.DefineQuery) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 2 with QueryResult

use of ai.grakn.rpc.generated.GrpcGrakn.QueryResult in project grakn by graknlabs.

the class RemoteGraknTxTest method whenStreamingAQueryWithInfiniteAnswers_Terminate.

@Test(timeout = 5_000)
public void whenStreamingAQueryWithInfiniteAnswers_Terminate() {
    Query<?> query = match(var("x").sub("thing")).get();
    String queryString = query.toString();
    GrpcConcept.Concept v123 = GrpcConcept.Concept.newBuilder().setId(V123).build();
    GrpcGrakn.Answer grpcAnswer = GrpcGrakn.Answer.newBuilder().putAnswer("x", v123).build();
    QueryResult queryResult = QueryResult.newBuilder().setAnswer(grpcAnswer).build();
    TxResponse response = TxResponse.newBuilder().setQueryResult(queryResult).build();
    server.setResponse(GrpcUtil.execQueryRequest(query), GrpcUtil.iteratorResponse(ITERATOR));
    server.setResponse(GrpcUtil.nextRequest(ITERATOR), response);
    List<Answer> answers;
    int numAnswers = 10;
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        answers = tx.graql().<GetQuery>parse(queryString).stream().limit(numAnswers).collect(toList());
    }
    assertEquals(10, answers.size());
    for (Answer answer : answers) {
        assertEquals(answer.vars(), ImmutableSet.of(var("x")));
        assertEquals(ConceptId.of("V123"), answer.get(var("x")).getId());
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Answer(ai.grakn.graql.admin.Answer) GraknTx(ai.grakn.GraknTx) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) GrpcGrakn(ai.grakn.rpc.generated.GrpcGrakn) GetQuery(ai.grakn.graql.GetQuery) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 3 with QueryResult

use of ai.grakn.rpc.generated.GrpcGrakn.QueryResult in project grakn by graknlabs.

the class RemoteGraknTxTest method whenExecutingAQuery_GetAResultBack.

@Test
public void whenExecutingAQuery_GetAResultBack() {
    Query<?> query = match(var("x").isa("person")).get();
    String queryString = query.toString();
    GrpcConcept.Concept v123 = GrpcConcept.Concept.newBuilder().setId(V123).build();
    GrpcGrakn.Answer grpcAnswer = GrpcGrakn.Answer.newBuilder().putAnswer("x", v123).build();
    QueryResult queryResult = QueryResult.newBuilder().setAnswer(grpcAnswer).build();
    TxResponse response = TxResponse.newBuilder().setQueryResult(queryResult).build();
    server.setResponseSequence(GrpcUtil.execQueryRequest(query), response);
    List<Answer> results;
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        results = tx.graql().<GetQuery>parse(queryString).execute();
    }
    Answer answer = Iterables.getOnlyElement(results);
    assertEquals(answer.vars(), ImmutableSet.of(var("x")));
    assertEquals(ConceptId.of("V123"), answer.get(var("x")).getId());
}
Also used : Answer(ai.grakn.graql.admin.Answer) GraknTx(ai.grakn.GraknTx) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) GrpcGrakn(ai.grakn.rpc.generated.GrpcGrakn) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) GetQuery(ai.grakn.graql.GetQuery) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 4 with QueryResult

use of ai.grakn.rpc.generated.GrpcGrakn.QueryResult in project grakn by graknlabs.

the class TxObserver method execQuery.

private void execQuery(ExecQuery request) {
    String queryString = request.getQuery().getValue();
    QueryBuilder graql = tx().graql();
    if (request.hasInfer()) {
        graql = graql.infer(request.getInfer().getValue());
    }
    Stream<QueryResult> queryResultStream = graql.parse(queryString).results(GrpcConverter.get());
    Stream<TxResponse> txResponseStream = queryResultStream.map(queryResult -> TxResponse.newBuilder().setQueryResult(queryResult).build());
    Iterator<TxResponse> iterator = txResponseStream.iterator();
    IteratorId iteratorId = grpcIterators.add(iterator);
    responseObserver.onNext(TxResponse.newBuilder().setIteratorId(iteratorId).build());
}
Also used : QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) QueryBuilder(ai.grakn.graql.QueryBuilder) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

Aggregations

QueryResult (ai.grakn.rpc.generated.GrpcGrakn.QueryResult)4 TxResponse (ai.grakn.rpc.generated.GrpcGrakn.TxResponse)4 GraknTx (ai.grakn.GraknTx)3 Answer (ai.grakn.graql.admin.Answer)3 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)3 GrpcGrakn (ai.grakn.rpc.generated.GrpcGrakn)3 Test (org.junit.Test)3 GetQuery (ai.grakn.graql.GetQuery)2 DefineQuery (ai.grakn.graql.DefineQuery)1 QueryBuilder (ai.grakn.graql.QueryBuilder)1 IteratorId (ai.grakn.rpc.generated.GrpcIterator.IteratorId)1