use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class QueryCacheTest method recordRetrieveSingleAnswer.
@Test
public void recordRetrieveSingleAnswer() {
QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
cache.recordAnswer(recordQuery, answer);
assertEquals(cache.getAnswer(recordQuery, new QueryAnswer()), new QueryAnswer());
assertEquals(cache.getAnswer(recordQuery, answer), answer);
assertEquals(cache.getAnswer(recordQuery, retrieveAnswer), answer);
assertEquals(cache.getAnswer(retrieveQuery, new QueryAnswer()), new QueryAnswer());
assertEquals(cache.getAnswer(retrieveQuery, retrieveAnswer), retrieveAnswer);
assertEquals(cache.getAnswer(retrieveQuery, answer), retrieveAnswer);
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class QueryCacheTest method onStartup.
@Before
public void onStartup() {
assumeTrue(GraknTestUtil.usingTinker());
graph = testContext.tx();
String recordPatternString = "{(role1: $x, role2: $y) isa reifiable-relation;}";
String retrievePatternString = "{(role1: $p1, role2: $p2) isa reifiable-relation;}";
Conjunction<VarPatternAdmin> recordPattern = conjunction(recordPatternString, graph);
Conjunction<VarPatternAdmin> retrievePattern = conjunction(retrievePatternString, graph);
recordQuery = ReasonerQueries.atomic(recordPattern, graph);
retrieveQuery = ReasonerQueries.atomic(retrievePattern, graph);
retrieveToRecordUnifier = retrieveQuery.getMultiUnifier(recordQuery).getUnifier();
recordToRetrieveUnifier = retrieveToRecordUnifier.inverse();
Entity entity = graph.getEntityType("anotherNoRoleEntity").instances().findFirst().orElse(null);
singleAnswer = new QueryAnswer(ImmutableMap.of(var("x"), entity, var("y"), entity));
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class TypeInferenceQueryTest method typeInference.
private void typeInference(List<RelationshipType> possibleTypes, String pattern, String subbedPattern, EmbeddedGraknTx<?> graph) {
ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(conjunction(subbedPattern, graph), graph);
RelationshipAtom atom = (RelationshipAtom) query.getAtom();
RelationshipAtom subbedAtom = (RelationshipAtom) subbedQuery.getAtom();
List<Type> relationshipTypes = atom.inferPossibleTypes(new QueryAnswer());
List<Type> subbedRelationshipTypes = subbedAtom.inferPossibleTypes(new QueryAnswer());
if (possibleTypes.size() == 1) {
assertEquals(possibleTypes, relationshipTypes);
assertEquals(relationshipTypes, subbedRelationshipTypes);
assertEquals(atom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
assertEquals(subbedAtom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
} else {
assertTrue(CollectionUtils.isEqualCollection(possibleTypes, relationshipTypes));
assertTrue(CollectionUtils.isEqualCollection(relationshipTypes, subbedRelationshipTypes));
assertEquals(atom.getSchemaConcept(), null);
assertEquals(subbedAtom.getSchemaConcept(), null);
}
typeInferenceQueries(possibleTypes, pattern, graph);
typeInferenceQueries(possibleTypes, subbedPattern, graph);
}
use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.
the class QueryOperationExecutor method insertAll.
private Answer insertAll(Answer results) {
concepts.putAll(results.map());
sortProperties().forEach(property -> property.executor().execute(this));
conceptBuilders.forEach(this::buildConcept);
ImmutableMap.Builder<Var, Concept> allConcepts = ImmutableMap.<Var, Concept>builder().putAll(concepts);
// Make sure to include all equivalent vars in the result
for (Var var : equivalentVars.getNodes()) {
allConcepts.put(var, concepts.get(equivalentVars.componentOf(var)));
}
Map<Var, Concept> namedConcepts = Maps.filterKeys(allConcepts.build(), Var::isUserDefinedName);
return new QueryAnswer(namedConcepts);
}
use of ai.grakn.graql.internal.query.QueryAnswer 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);
}
}
Aggregations