use of ai.grakn.concept.Role in project grakn by graknlabs.
the class RelationshipAtom method validateRelationPlayers.
private Set<String> validateRelationPlayers(Rule rule) {
Set<String> errors = new HashSet<>();
getRelationPlayers().forEach(rp -> {
VarPatternAdmin role = rp.getRole().orElse(null);
if (role == null) {
errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.getThen(), rule.getLabel()));
} else {
Label roleLabel = role.getTypeLabel().orElse(null);
if (roleLabel == null) {
errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.getThen(), rule.getLabel()));
} else {
if (Schema.MetaSchema.isMetaLabel(roleLabel)) {
errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.getThen(), rule.getLabel()));
}
Role roleType = tx().getRole(roleLabel.getValue());
if (roleType != null && roleType.isImplicit()) {
errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_IMPLICIT_ROLE.getMessage(rule.getThen(), rule.getLabel()));
}
}
}
});
return errors;
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class StatisticsTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = tx.putEntityType(thing);
EntityType entityType2 = tx.putEntityType(anotherThing);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType2.addEntity();
entityId1 = entity1.getId();
entityId2 = entity2.getId();
entityId3 = entity3.getId();
entityId4 = entity4.getId();
Role relation1 = tx.putRole("relation1");
Role relation2 = tx.putRole("relation2");
entityType1.plays(relation1).plays(relation2);
entityType2.plays(relation1).plays(relation2);
RelationshipType related = tx.putRelationshipType("related").relates(relation1).relates(relation2);
related.addRelationship().addRolePlayer(relation1, entity1).addRolePlayer(relation2, entity2);
related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity3);
related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity4);
AttributeType<Double> attribute1 = tx.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE);
AttributeType<Long> attribute2 = tx.putAttributeType(resourceType2, AttributeType.DataType.LONG);
AttributeType<Long> attribute3 = tx.putAttributeType(resourceType3, AttributeType.DataType.LONG);
AttributeType<String> attribute4 = tx.putAttributeType(resourceType4, AttributeType.DataType.STRING);
AttributeType<Long> attribute5 = tx.putAttributeType(resourceType5, AttributeType.DataType.LONG);
AttributeType<Double> attribute6 = tx.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE);
AttributeType<Double> attribute7 = tx.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE);
entityType1.attribute(attribute1);
entityType1.attribute(attribute2);
entityType1.attribute(attribute3);
entityType1.attribute(attribute4);
entityType1.attribute(attribute5);
entityType1.attribute(attribute6);
entityType1.attribute(attribute7);
entityType2.attribute(attribute1);
entityType2.attribute(attribute2);
entityType2.attribute(attribute3);
entityType2.attribute(attribute4);
entityType2.attribute(attribute5);
entityType2.attribute(attribute6);
entityType2.attribute(attribute7);
tx.commit();
}
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class StatisticsTest method addResourceRelations.
private void addResourceRelations() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
Entity entity1 = graph.getConcept(entityId1);
Entity entity2 = graph.getConcept(entityId2);
Entity entity3 = graph.getConcept(entityId3);
Entity entity4 = graph.getConcept(entityId4);
Role resourceOwner1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType1)));
Role resourceOwner2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType2)));
Role resourceOwner3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType3)));
Role resourceOwner4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType4)));
Role resourceOwner5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType5)));
Role resourceOwner6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType6)));
Role resourceValue1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType1)));
Role resourceValue2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType2)));
Role resourceValue3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType3)));
Role resourceValue4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType4)));
Role resourceValue5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType5)));
Role resourceValue6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType6)));
RelationshipType relationshipType1 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType1)));
relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.2));
relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.5));
relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity3).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.8));
RelationshipType relationshipType2 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType2)));
relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(4L));
relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(-1L));
relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity4).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(0L));
graph.<Long>getAttributeType(resourceType3).putAttribute(100L);
RelationshipType relationshipType5 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType5)));
relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity1).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity2).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity4).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
RelationshipType relationshipType6 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType6)));
relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity1).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity2).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity4).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
// some resources in, but not connect them to any instances
graph.<Double>getAttributeType(resourceType1).putAttribute(2.8);
graph.<Long>getAttributeType(resourceType2).putAttribute(-5L);
graph.<Long>getAttributeType(resourceType5).putAttribute(10L);
graph.<Double>getAttributeType(resourceType6).putAttribute(0.8);
graph.commit();
}
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class GraqlTest method testStatisticsMethods.
@Test
public void testStatisticsMethods() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
Label resourceTypeId = Label.of("my-resource");
AttributeType<Long> resource = graph.putAttributeType(resourceTypeId, AttributeType.DataType.LONG);
EntityType thingy = graph.putEntityType("thingy");
thingy.attribute(resource);
Entity theResourceOwner = thingy.addEntity();
Role resourceOwner = graph.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(resourceTypeId).getValue());
Role resourceValue = graph.getRole(Schema.ImplicitType.HAS_VALUE.getLabel(resourceTypeId).getValue());
RelationshipType relationshipType = graph.getRelationshipType(Schema.ImplicitType.HAS.getLabel(resourceTypeId).getValue());
relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(1L));
relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(2L));
relationshipType.addRelationship().addRolePlayer(resourceOwner, theResourceOwner).addRolePlayer(resourceValue, resource.putAttribute(3L));
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
// use graql to compute various statistics
Optional<? extends Number> result = graph.graql().<SumQuery>parse("compute sum of my-resource;").execute();
assertEquals(Optional.of(6L), result);
result = graph.graql().<MinQuery>parse("compute min of my-resource;").execute();
assertEquals(Optional.of(1L), result);
result = graph.graql().<MaxQuery>parse("compute max of my-resource;").execute();
assertEquals(Optional.of(3L), result);
result = graph.graql().<MeanQuery>parse("compute mean of my-resource;").execute();
assert result.isPresent();
assertEquals(2.0, (Double) result.get(), 0.1);
result = graph.graql().<MedianQuery>parse("compute median of my-resource;").execute();
assertEquals(Optional.of(2L), result);
}
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class AnalyticsTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = graph.putEntityType(thingy);
EntityType entityType2 = graph.putEntityType(anotherThing);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType2.addEntity();
entityId1 = entity1.getId().getValue();
entityId2 = entity2.getId().getValue();
entityId3 = entity3.getId().getValue();
entityId4 = entity4.getId().getValue();
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType1.plays(role1).plays(role2);
entityType2.plays(role1).plays(role2);
RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
relationId12 = relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2).getId().getValue();
relationId24 = relationshipType.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity4).getId().getValue();
graph.commit();
}
}
Aggregations