Search in sources :

Example 31 with GetQuery

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

Example 32 with GetQuery

use of ai.grakn.graql.GetQuery 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 33 with GetQuery

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

the class ExplanationBuilderTest method whenExplainInferred_returnsLinkedExplanation.

// NOTE: This test ix expected to be slower than average.
@Test
public void whenExplainInferred_returnsLinkedExplanation() {
    Label person = Label.of("person");
    Label siblings = Label.of("siblings");
    Label parentship = Label.of("parentship");
    String mainQuery = "match ($x, $y) isa cousins; limit 15; get;";
    genealogyKB.tx().graql().infer(true).parser().<GetQuery>parseQuery(mainQuery).forEach(answer -> {
        String cousin1 = answer.get("x").getId().getValue();
        String cousin2 = answer.get("y").getId().getValue();
        String specificQuery = "match " + "$x id " + cousin1 + ";" + "$y id " + cousin2 + ";" + "(cousin: $x, cousin: $y) isa cousins; limit 1;get;";
        GetQuery query = genealogyKB.tx().graql().infer(true).parse(specificQuery);
        ai.grakn.graql.admin.Answer specificAnswer = query.execute().stream().findFirst().orElse(new QueryAnswer());
        Set<ConceptId> originalEntityIds = specificAnswer.getExplanation().getAnswers().stream().flatMap(ans -> ans.concepts().stream()).map(ai.grakn.concept.Concept::getId).collect(Collectors.toSet());
        List<Answer> explanation = ExplanationBuilder.buildExplanation(specificAnswer);
        Set<ConceptId> entityIds = explanation.stream().flatMap(exp -> exp.conceptMap().values().stream()).filter(c -> c.baseType().equals("ENTITY")).map(Concept::id).collect(Collectors.toSet());
        // ensure we deal with the same entities
        assertEquals(originalEntityIds, entityIds);
        assertEquals(3, explanation.size());
        explanation.forEach(explanationAnswer -> {
            explanationAnswer.conceptMap().values().forEach(concept -> {
                Schema.BaseType baseType = Schema.BaseType.valueOf(concept.baseType());
                Label typeLabel = ((Thing) concept).type().label();
                switch(baseType) {
                    case ENTITY:
                        assertEquals(person, typeLabel);
                        break;
                    case RELATIONSHIP:
                        assertTrue(typeLabel.equals(siblings) || typeLabel.equals(parentship));
                        break;
                }
            });
        });
    });
}
Also used : GenealogyKB(ai.grakn.test.kbs.GenealogyKB) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Label(ai.grakn.concept.Label) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Schema(ai.grakn.util.Schema) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ClassRule(org.junit.ClassRule) Assert.assertEquals(org.junit.Assert.assertEquals) Schema(ai.grakn.util.Schema) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 34 with GetQuery

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

the class GrpcServerIT method whenGettingASchemaConcept_TheInformationOnTheSchemaConceptIsCorrect.

@Test
public void whenGettingASchemaConcept_TheInformationOnTheSchemaConceptIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").label("actor")).get();
        SchemaConcept remoteConcept = query.stream().findAny().get().get("x").asSchemaConcept();
        SchemaConcept localConcept = localTx.getConcept(remoteConcept.getId()).asSchemaConcept();
        assertEquals(localConcept.isImplicit(), remoteConcept.isImplicit());
        assertEquals(localConcept.getLabel(), remoteConcept.getLabel());
        assertEquals(localConcept.sup().getId(), remoteConcept.sup().getId());
        assertEqualConcepts(localConcept, remoteConcept, SchemaConcept::sups);
        assertEqualConcepts(localConcept, remoteConcept, SchemaConcept::subs);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Example 35 with GetQuery

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

the class GrpcServerIT method whenGettingARule_TheInformationOnTheRuleIsCorrect.

@Test
public void whenGettingARule_TheInformationOnTheRuleIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").label("expectation-rule")).get();
        ai.grakn.concept.Rule remoteConcept = query.stream().findAny().get().get("x").asRule();
        ai.grakn.concept.Rule localConcept = localTx.getConcept(remoteConcept.getId()).asRule();
        assertEquals(localConcept.getWhen(), remoteConcept.getWhen());
        assertEquals(localConcept.getThen(), remoteConcept.getThen());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Aggregations

GetQuery (ai.grakn.graql.GetQuery)163 Test (org.junit.Test)161 Answer (ai.grakn.graql.admin.Answer)65 QueryBuilder (ai.grakn.graql.QueryBuilder)61 GraknTx (ai.grakn.GraknTx)51 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)23 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)12 Concept (ai.grakn.concept.Concept)9 List (java.util.List)8 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Set (java.util.Set)6 Label (ai.grakn.concept.Label)5 RelationshipType (ai.grakn.concept.RelationshipType)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)5 Sets (com.google.common.collect.Sets)5 Collectors (java.util.stream.Collectors)5 Collectors.toSet (java.util.stream.Collectors.toSet)5