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));
}
});
}
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());
}
}
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;
}
});
});
});
}
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);
}
}
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());
}
}
Aggregations