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