use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RelationshipAtom method materialise.
@Override
public Stream<Answer> materialise() {
RelationshipType relationType = getSchemaConcept().asRelationshipType();
Multimap<Role, Var> roleVarMap = getRoleVarMap();
Answer substitution = getParentQuery().getSubstitution();
Relationship relationship = RelationshipTypeImpl.from(relationType).addRelationshipInferred();
roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.addRolePlayer(key, substitution.get(var).asThing())));
Answer relationSub = getRoleSubstitution().merge(getVarName().isUserDefinedName() ? new QueryAnswer(ImmutableMap.of(getVarName(), relationship)) : new QueryAnswer());
return Stream.of(substitution.merge(relationSub));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ValidatorTest method whenCreatingRelationWithoutLinkingRelates_Throw.
@Test
public void whenCreatingRelationWithoutLinkingRelates_Throw() {
Role hunter = tx.putRole("hunter");
Role monster = tx.putRole("monster");
EntityType stuff = tx.putEntityType("Stuff").plays(hunter).plays(monster);
RelationshipType kills = tx.putRelationshipType("kills").relates(hunter);
Entity myHunter = stuff.addEntity();
Entity myMonster = stuff.addEntity();
Relationship relation = kills.addRelationship().addRolePlayer(hunter, myHunter).addRolePlayer(monster, myMonster);
expectedException.expect(InvalidKBException.class);
expectedException.expectMessage(containsString(ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL.getMessage(relation.getId(), monster.getLabel(), kills.getLabel())));
tx.commit();
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ValidatorTest method whenARoleInARelationIsPlayedAZillionTimes_TheGraphIsValid.
@Test
public void whenARoleInARelationIsPlayedAZillionTimes_TheGraphIsValid() {
Role role1 = tx.putRole("role-1");
Role role2 = tx.putRole("role-2");
RelationshipType relationshipType = tx.putRelationshipType("my-relationship").relates(role1).relates(role2);
EntityType entityType = tx.putEntityType("my-entity").plays(role1);
Relationship relationship = relationshipType.addRelationship();
Set<Thing> things = new HashSet<>();
int oneZillion = 100;
for (int i = 0; i < oneZillion; i++) {
Thing thing = entityType.addEntity();
things.add(thing);
relationship.addRolePlayer(role1, thing);
}
assertEquals(things, relationship.rolePlayers(role1).collect(toSet()));
tx.commit();
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ValidatorTest method whenRemovingInvalidRolePlayers_EnsureValidationPasses.
@Test
public void whenRemovingInvalidRolePlayers_EnsureValidationPasses() {
Role chased = tx.putRole("chased");
Role chaser = tx.putRole("chaser");
RelationshipType chases = tx.putRelationshipType("chases").relates(chased).relates(chaser);
EntityType puppy = tx.putEntityType("puppy").plays(chaser);
Entity dunstan = puppy.addEntity();
Relationship rel = chases.addRelationship();
rel.addRolePlayer(chaser, dunstan);
rel.addRolePlayer(chased, dunstan).removeRolePlayer(chased, dunstan);
tx.commit();
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingRelationshipsWithNoArguments_GetTheExpectedResult.
@Test
public void whenCallingRelationshipsWithNoArguments_GetTheExpectedResult() {
Relationship a = RemoteConcepts.createRelationship(tx, A);
Relationship b = RemoteConcepts.createRelationship(tx, B);
Relationship c = RemoteConcepts.createRelationship(tx, C);
mockConceptMethod(ConceptMethods.GET_RELATIONSHIPS, Stream.of(a, b, c));
assertThat(thing.relationships().collect(toSet()), containsInAnyOrder(a, b, c));
}
Aggregations