Search in sources :

Example 6 with TxResponse

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

the class GrpcServerTest method whenExecutingAQueryRemotely_AResultIsReturned.

@Test
public void whenExecutingAQueryRemotely_AResultIsReturned() throws InterruptedException {
    Concept conceptX = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptX.getId()).thenReturn(ConceptId.of("V123"));
    when(conceptX.isRelationship()).thenReturn(true);
    when(conceptX.asRelationship().type().getLabel()).thenReturn(Label.of("L123"));
    Concept conceptY = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptY.getId()).thenReturn(ConceptId.of("V456"));
    when(conceptY.isAttribute()).thenReturn(true);
    when(conceptY.asAttribute().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));
    when(query.stream()).thenAnswer(params -> answers.stream());
    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));
        TxResponse response1 = tx.receive().ok();
        GrpcConcept.Concept rpcX = GrpcConcept.Concept.newBuilder().setId(V123).setBaseType(BaseType.Relationship).build();
        GrpcGrakn.Answer.Builder answerX = GrpcGrakn.Answer.newBuilder().putAnswer("x", rpcX);
        QueryResult.Builder resultX = QueryResult.newBuilder().setAnswer(answerX);
        assertEquals(TxResponse.newBuilder().setQueryResult(resultX).build(), response1);
        tx.send(nextRequest(iterator));
        TxResponse response2 = tx.receive().ok();
        GrpcConcept.Concept rpcY = GrpcConcept.Concept.newBuilder().setId(V456).setBaseType(BaseType.Attribute).build();
        GrpcGrakn.Answer.Builder answerY = GrpcGrakn.Answer.newBuilder().putAnswer("y", rpcY);
        QueryResult.Builder resultY = QueryResult.newBuilder().setAnswer(answerY);
        assertEquals(TxResponse.newBuilder().setQueryResult(resultY).build(), response2);
        tx.send(nextRequest(iterator));
        TxResponse response3 = tx.receive().ok();
        TxResponse expected = doneResponse();
        assertEquals(expected, response3);
        tx.send(stopRequest(iterator));
    }
}
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) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 7 with TxResponse

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

the class ConceptResponseType method createStreamable.

public static <T> ConceptResponseType<Stream<? extends T>> createStreamable(BiFunction<GrpcConceptConverter, TxResponse, T> getter, Function<T, TxResponse> setter) {
    return create((converter, client, response) -> {
        IteratorId iteratorId = response.getIteratorId();
        Iterable<T> iterable = () -> new GraknGrpcIterator<T>(client, iteratorId) {

            @Override
            protected T getNextFromResponse(TxResponse response) {
                return getter.apply(converter, response);
            }
        };
        return StreamSupport.stream(iterable.spliterator(), false);
    }, (builder, iterators, val) -> {
        Stream<TxResponse> responses = val.map(setter);
        IteratorId iteratorId = iterators.add(responses.iterator());
        builder.setIteratorId(iteratorId);
    });
}
Also used : IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

Example 8 with TxResponse

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

the class GrpcClient method getAttributesByValue.

public Stream<? extends Concept> getAttributesByValue(Object value) {
    communicator.send(GrpcUtil.getAttributesByValueRequest(value));
    IteratorId iteratorId = responseOrThrow().getIteratorId();
    Iterable<Concept> iterable = () -> new GraknGrpcIterator<Concept>(this, iteratorId) {

        @Override
        protected Concept getNextFromResponse(TxResponse response) {
            return conceptConverter.convert(response.getConcept());
        }
    };
    return StreamSupport.stream(iterable.spliterator(), false);
}
Also used : Concept(ai.grakn.concept.Concept) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

Example 9 with TxResponse

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

the class GrpcClient method execQuery.

public Iterator<Object> execQuery(Query<?> query) {
    communicator.send(GrpcUtil.execQueryRequest(query.toString(), query.inferring()));
    IteratorId iteratorId = responseOrThrow().getIteratorId();
    return new GraknGrpcIterator<Object>(this, iteratorId) {

        @Override
        protected Object getNextFromResponse(TxResponse response) {
            return convert(response.getQueryResult());
        }
    };
}
Also used : IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

Example 10 with TxResponse

use of ai.grakn.rpc.generated.GrpcGrakn.TxResponse 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

TxResponse (ai.grakn.rpc.generated.GrpcGrakn.TxResponse)21 Test (org.junit.Test)10 IteratorId (ai.grakn.rpc.generated.GrpcIterator.IteratorId)9 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)8 Concept (ai.grakn.concept.Concept)6 QueryResult (ai.grakn.rpc.generated.GrpcGrakn.QueryResult)6 Answer (ai.grakn.graql.admin.Answer)5 GraknTx (ai.grakn.GraknTx)4 TxGrpcCommunicator (ai.grakn.grpc.TxGrpcCommunicator)4 GetQuery (ai.grakn.graql.GetQuery)3 GrpcGrakn (ai.grakn.rpc.generated.GrpcGrakn)3 Attribute (ai.grakn.concept.Attribute)2 Role (ai.grakn.concept.Role)2 DefineQuery (ai.grakn.graql.DefineQuery)2 QueryBuilder (ai.grakn.graql.QueryBuilder)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 TxRequest (ai.grakn.rpc.generated.GrpcGrakn.TxRequest)2 AttributeType (ai.grakn.concept.AttributeType)1 EntityType (ai.grakn.concept.EntityType)1 Label (ai.grakn.concept.Label)1