Search in sources :

Example 16 with Concept

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));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 17 with Concept

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));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Pattern(ai.grakn.graql.Pattern) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 18 with Concept

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());
}
Also used : Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 19 with Concept

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()));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 20 with Concept

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));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Aggregations

Concept (ai.grakn.concept.Concept)91 Test (org.junit.Test)56 ConceptId (ai.grakn.concept.ConceptId)26 GraknTx (ai.grakn.GraknTx)25 Answer (ai.grakn.graql.admin.Answer)25 SchemaConcept (ai.grakn.concept.SchemaConcept)19 Label (ai.grakn.concept.Label)18 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)18 QueryBuilder (ai.grakn.graql.QueryBuilder)17 Var (ai.grakn.graql.Var)15 Set (java.util.Set)15 Role (ai.grakn.concept.Role)14 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 HashSet (java.util.HashSet)13 List (java.util.List)12 AttributeType (ai.grakn.concept.AttributeType)11 EntityType (ai.grakn.concept.EntityType)11 Schema (ai.grakn.util.Schema)10 Collectors (java.util.stream.Collectors)10 Stream (java.util.stream.Stream)10