Search in sources :

Example 21 with Label

use of ai.grakn.concept.Label in project grakn by graknlabs.

the class RemoteGraknTxTest method whenPuttingRelationshipType_EnsureCorrectRequestIsSent.

@Test
public void whenPuttingRelationshipType_EnsureCorrectRequestIsSent() {
    ConceptId id = ConceptId.of(V123.getValue());
    Label label = Label.of("foo");
    try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
        // The open request
        verify(server.requests()).onNext(any());
        Concept concept = RemoteConcepts.createRelationshipType(tx, id);
        server.setResponse(GrpcUtil.putRelationshipTypeRequest(label), GrpcUtil.conceptResponse(concept));
        assertEquals(concept, tx.putRelationshipType(label));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 22 with Label

use of ai.grakn.concept.Label 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 23 with Label

use of ai.grakn.concept.Label 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 24 with Label

use of ai.grakn.concept.Label 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 25 with Label

use of ai.grakn.concept.Label in project grakn by graknlabs.

the class RelationshipProperty method checkValidProperty.

@Override
public void checkValidProperty(GraknTx graph, VarPatternAdmin var) throws GraqlQueryException {
    Set<Label> roleTypes = relationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).collect(toSet());
    Optional<Label> maybeLabel = var.getProperty(IsaProperty.class).map(IsaProperty::type).flatMap(VarPatternAdmin::getTypeLabel);
    maybeLabel.ifPresent(label -> {
        SchemaConcept schemaConcept = graph.getSchemaConcept(label);
        if (schemaConcept == null || !schemaConcept.isRelationshipType()) {
            throw GraqlQueryException.notARelationType(label);
        }
    });
    // Check all role types exist
    roleTypes.forEach(roleId -> {
        SchemaConcept schemaConcept = graph.getSchemaConcept(roleId);
        if (schemaConcept == null || !schemaConcept.isRole()) {
            throw GraqlQueryException.notARoleType(roleId);
        }
    });
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Aggregations

Label (ai.grakn.concept.Label)87 Test (org.junit.Test)41 Role (ai.grakn.concept.Role)25 GraknTx (ai.grakn.GraknTx)22 ConceptId (ai.grakn.concept.ConceptId)21 Concept (ai.grakn.concept.Concept)20 Set (java.util.Set)20 RelationshipType (ai.grakn.concept.RelationshipType)19 SchemaConcept (ai.grakn.concept.SchemaConcept)19 AttributeType (ai.grakn.concept.AttributeType)17 EntityType (ai.grakn.concept.EntityType)17 Relationship (ai.grakn.concept.Relationship)14 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 Type (ai.grakn.concept.Type)12 Stream (java.util.stream.Stream)12 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)11 Schema (ai.grakn.util.Schema)11 Collection (java.util.Collection)11 Pattern (ai.grakn.graql.Pattern)10