Search in sources :

Example 1 with DefineQuery

use of ai.grakn.graql.DefineQuery 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 DefineQuery

use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.

the class DefineQueryTest method whenExecutingADefineQuery_ResultContainsAllInsertedVars.

@Test
public void whenExecutingADefineQuery_ResultContainsAllInsertedVars() {
    Var type = var("type");
    Var type2 = var("type2");
    // Note that two variables refer to the same type. They should both be in the result
    DefineQuery query = qb.define(type.label("my-type").sub("entity"), type2.label("my-type"));
    Answer result = query.execute();
    assertThat(result.vars(), containsInAnyOrder(type, type2));
    assertEquals(result.get(type), result.get(type2));
}
Also used : Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) DefineQuery(ai.grakn.graql.DefineQuery) Test(org.junit.Test)

Example 3 with DefineQuery

use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.

the class QueryParserTest method whenParsingDefineQuery_ResultIsSameAsJavaGraql.

@Test
public void whenParsingDefineQuery_ResultIsSameAsJavaGraql() {
    DefineQuery expected = define(label("pokemon").sub(Schema.MetaSchema.ENTITY.getLabel().getValue()), label("evolution").sub(Schema.MetaSchema.RELATIONSHIP.getLabel().getValue()), label("evolves-from").sub(Schema.MetaSchema.ROLE.getLabel().getValue()), label("evolves-to").sub(Schema.MetaSchema.ROLE.getLabel().getValue()), label("evolution").relates("evolves-from").relates("evolves-to"), label("pokemon").plays("evolves-from").plays("evolves-to").has("name"));
    DefineQuery parsed = parse("define " + "'pokemon' sub entity;" + "evolution sub " + Schema.MetaSchema.RELATIONSHIP.getLabel() + ";" + "evolves-from sub role;" + "label \"evolves-to\" sub role;" + "evolution relates evolves-from, relates evolves-to;" + "pokemon plays evolves-from plays evolves-to has name;");
    assertEquals(expected, parsed);
}
Also used : DefineQuery(ai.grakn.graql.DefineQuery) Test(org.junit.Test)

Example 4 with DefineQuery

use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.

the class QueryParserTest method whenParsingAsInDefine_ResultIsSameAsSub.

@Test
public void whenParsingAsInDefine_ResultIsSameAsSub() {
    DefineQuery expected = define(label("parent").sub("role"), label("child").sub("role"), label("parenthood").sub("relationship").relates(var().label("parent")).relates(var().label("child")), label("fatherhood").sub("parenthood").relates(label("father"), label("parent")).relates(label("son"), label("child")));
    DefineQuery parsed = parse("define " + "parent sub role;\n" + "child sub role;\n" + "parenthood sub relationship, relates parent, relates child;\n" + "fatherhood sub parenthood, relates father as parent, relates son as child;");
    assertEquals(expected, parsed);
    assertEquals(expected, parse(expected.toString()));
}
Also used : DefineQuery(ai.grakn.graql.DefineQuery) Test(org.junit.Test)

Example 5 with DefineQuery

use of ai.grakn.graql.DefineQuery in project grakn by graknlabs.

the class DefineQueryTest method whenCallingToStringOfDefineQuery_ReturnCorrectRepresentation.

@Test
public void whenCallingToStringOfDefineQuery_ReturnCorrectRepresentation() {
    String queryString = "define label my-entity sub entity;";
    DefineQuery defineQuery = parse(queryString);
    assertEquals(queryString, defineQuery.toString());
}
Also used : DefineQuery(ai.grakn.graql.DefineQuery) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

DefineQuery (ai.grakn.graql.DefineQuery)5 Test (org.junit.Test)5 Answer (ai.grakn.graql.admin.Answer)2 GraknTx (ai.grakn.GraknTx)1 Var (ai.grakn.graql.Var)1 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)1 GrpcGrakn (ai.grakn.rpc.generated.GrpcGrakn)1 QueryResult (ai.grakn.rpc.generated.GrpcGrakn.QueryResult)1 TxResponse (ai.grakn.rpc.generated.GrpcGrakn.TxResponse)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1