use of ai.grakn.concept.Role in project grakn by graknlabs.
the class ShortestPathTest method testMultiplePathsSharing1Instance.
@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
ConceptId startId;
ConceptId endId;
Set<List<ConceptId>> correctPaths = new HashSet<>();
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType = graph.putEntityType(thing);
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType.plays(role1).plays(role2);
RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
entityType.plays(role3).plays(role4);
RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
Entity start = entityType.addEntity();
Entity end = entityType.addEntity();
Entity middle = entityType.addEntity();
startId = start.getId();
endId = end.getId();
ConceptId middleId = middle.getId();
ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
assertEquals(correctPaths.size(), allPaths.size());
Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
assertEquals(correctPaths, computedPaths);
}
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class GrpcUtil method convert.
public static Map<Role, Set<Thing>> convert(GrpcConceptConverter converter, RolePlayers allRolePlayers) {
ImmutableSetMultimap.Builder<Role, Thing> map = ImmutableSetMultimap.builder();
for (GrpcConcept.RolePlayer rolePlayer : allRolePlayers.getRolePlayerList()) {
Role role = converter.convert(rolePlayer.getRole()).asRole();
Thing player = converter.convert(rolePlayer.getPlayer()).asThing();
map.put(role, player);
}
return Multimaps.asMap(map.build());
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class ValidateGlobalRules method validateRelationTypesToRolesSchema.
/**
* @param relationshipType the {@link RelationshipType} to be validated
* @return Error messages if the role type sub structure does not match the {@link RelationshipType} sub structure
*/
static Set<String> validateRelationTypesToRolesSchema(RelationshipType relationshipType) {
RelationshipTypeImpl superRelationType = (RelationshipTypeImpl) relationshipType.sup();
if (Schema.MetaSchema.isMetaLabel(superRelationType.getLabel()) || superRelationType.isAbstract()) {
// If super type is a meta type no validation needed
return Collections.emptySet();
}
Set<String> errorMessages = new HashSet<>();
Collection<Role> superRelates = superRelationType.relates().collect(Collectors.toSet());
Collection<Role> relates = relationshipType.relates().collect(Collectors.toSet());
Set<Label> relatesLabels = relates.stream().map(SchemaConcept::getLabel).collect(Collectors.toSet());
// Check 1) Every role of relationTypes is the sub of a role which is in the relates of it's supers
if (!superRelationType.isAbstract()) {
Set<Label> allSuperRolesPlayed = new HashSet<>();
superRelationType.sups().forEach(rel -> rel.relates().forEach(roleType -> allSuperRolesPlayed.add(roleType.getLabel())));
for (Role relate : relates) {
boolean validRoleTypeFound = SchemaConceptImpl.from(relate).sups().anyMatch(superRole -> allSuperRolesPlayed.contains(superRole.getLabel()));
if (!validRoleTypeFound) {
errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(relate.getLabel(), relationshipType.getLabel(), "super", "super", superRelationType.getLabel()));
}
}
}
// Check 2) Every role of superRelationType has a sub role which is in the relates of relationTypes
for (Role superRelate : superRelates) {
boolean subRoleNotFoundInRelates = superRelate.subs().noneMatch(sub -> relatesLabels.contains(sub.getLabel()));
if (subRoleNotFoundInRelates) {
errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.getLabel(), superRelationType.getLabel(), "sub", "sub", relationshipType.getLabel()));
}
}
return errorMessages;
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class ValidateGlobalRules method validatePlaysAndRelatesStructure.
/**
* This method checks if the plays edge has been added between the roleplayer's {@link Type} and
* the {@link Role} being played.
*
* It also checks if the {@link Role} of the {@link Casting} has been linked to the {@link RelationshipType} of the
* {@link Relationship} which the {@link Casting} connects to.
*
* @return Specific errors if any are found
*/
static Set<String> validatePlaysAndRelatesStructure(Casting casting) {
Set<String> errors = new HashSet<>();
// Gets here to make sure we traverse/read only once
Thing thing = casting.getRolePlayer();
Role role = casting.getRole();
Relationship relationship = casting.getRelationship();
// Actual checks
roleNotAllowedToBePlayed(role, thing).ifPresent(errors::add);
roleNotLinkedToRelationShip(role, relationship.type(), relationship).ifPresent(errors::add);
return errors;
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class RuleTest method initTx.
private void initTx(GraknTx graph) {
AttributeType<Integer> res1 = graph.putAttributeType("res1", AttributeType.DataType.INTEGER);
AttributeType<Integer> res2 = graph.putAttributeType("res2", AttributeType.DataType.INTEGER);
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
Role role3 = graph.putRole("role3");
graph.putEntityType("entity1").attribute(res1).attribute(res2).plays(role1).plays(role2);
graph.putRelationshipType("relation1").relates(role1).relates(role2).relates(role3).plays(role1).plays(role2);
graph.putRelationshipType("relation2").relates(role3);
}
Aggregations