use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class RemoteGraknTxTest method whenPuttingAttributeType_EnsureCorrectRequestIsSent.
@Test
public void whenPuttingAttributeType_EnsureCorrectRequestIsSent() {
ConceptId id = ConceptId.of(V123.getValue());
Label label = Label.of("foo");
AttributeType.DataType<?> dataType = AttributeType.DataType.STRING;
try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
// The open request
verify(server.requests()).onNext(any());
Concept concept = RemoteConcepts.createAttributeType(tx, id);
server.setResponse(GrpcUtil.putAttributeTypeRequest(label, dataType), GrpcUtil.conceptResponse(concept));
assertEquals(concept, tx.putAttributeType(label, dataType));
}
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class RemoteGraknTxTest method whenPuttingRule_EnsureCorrectRequestIsSent.
@Test
public void whenPuttingRule_EnsureCorrectRequestIsSent() {
ConceptId id = ConceptId.of(V123.getValue());
Label label = Label.of("foo");
Pattern when = var("x").isa("person");
Pattern then = var("y").isa("person");
try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
// The open request
verify(server.requests()).onNext(any());
Concept concept = RemoteConcepts.createRule(tx, id);
server.setResponse(GrpcUtil.putRuleRequest(label, when, then), GrpcUtil.conceptResponse(concept));
assertEquals(concept, tx.putRule(label, when, then));
}
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class QueryAnswer method unify.
@Override
public Answer unify(Unifier unifier) {
if (unifier.isEmpty())
return this;
Map<Var, Concept> unified = new HashMap<>();
for (Map.Entry<Var, Concept> e : this.entrySet()) {
Var var = e.getKey();
Concept con = e.getValue();
Collection<Var> uvars = unifier.get(var);
if (uvars.isEmpty() && !unifier.values().contains(var)) {
Concept put = unified.put(var, con);
if (put != null && !put.equals(con))
return new QueryAnswer();
} else {
for (Var uv : uvars) {
Concept put = unified.put(uv, con);
if (put != null && !put.equals(con))
return new QueryAnswer();
}
}
}
return new QueryAnswer(unified, this.getExplanation());
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class GrpcServerTest method whenGettingALabel_TheLabelIsReturned.
@Test
public void whenGettingALabel_TheLabelIsReturned() throws InterruptedException {
ConceptId id = ConceptId.of("V123456");
Label label = Label.of("Dunstan");
Concept concept = mock(Concept.class, RETURNS_DEEP_STUBS);
when(tx.getConcept(id)).thenReturn(concept);
when(concept.isSchemaConcept()).thenReturn(true);
when(concept.asSchemaConcept().getLabel()).thenReturn(label);
try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
tx.send(openRequest(MYKS, GraknTxType.READ));
tx.receive().ok();
tx.send(GrpcUtil.runConceptMethodRequest(id, ConceptMethods.GET_LABEL));
assertEquals(label, ConceptMethods.GET_LABEL.get(conceptConverter, client, tx.receive().ok()));
}
}
use of ai.grakn.concept.Concept 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));
}
}
Aggregations