use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingRelationshipsWithRoles_GetTheExpectedResult.
@Test
public void whenCallingRelationshipsWithRoles_GetTheExpectedResult() {
Role foo = RemoteConcepts.createRole(tx, ConceptId.of("foo"));
Role bar = RemoteConcepts.createRole(tx, ConceptId.of("bar"));
Role baz = RemoteConcepts.createRole(tx, ConceptId.of("baz"));
Relationship a = RemoteConcepts.createRelationship(tx, A);
Relationship b = RemoteConcepts.createRelationship(tx, B);
Relationship c = RemoteConcepts.createRelationship(tx, C);
mockConceptMethod(ConceptMethods.getRelationshipsByRoles(foo, bar, baz), Stream.of(a, b, c));
assertThat(thing.relationships(foo, bar, baz).collect(toSet()), containsInAnyOrder(a, b, c));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingAddAttributeRelationshipOnThing_ExecuteAConceptMethod.
@Test
public void whenCallingAddAttributeRelationshipOnThing_ExecuteAConceptMethod() {
Attribute<Long> attribute = RemoteConcepts.createAttribute(tx, A);
Relationship relationship = RemoteConcepts.createRelationship(tx, C);
mockConceptMethod(ConceptMethods.setAttribute(attribute), relationship);
assertEquals(relationship, thing.attributeRelationship(attribute));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class GrpcServerIT method whenDefiningASchema_TheSchemaIsDefined.
@Test
public void whenDefiningASchema_TheSchemaIsDefined() {
try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
EntityType animal = tx.putEntityType("animal");
EntityType dog = tx.putEntityType("dog").sup(animal);
EntityType cat = tx.putEntityType("cat");
animal.sub(cat);
cat.setLabel(Label.of("feline"));
dog.setAbstract(true).setAbstract(false);
cat.setAbstract(true);
RelationshipType chases = tx.putRelationshipType("chases");
Role chased = tx.putRole("chased");
Role chaser = tx.putRole("chaser");
chases.relates(chased).relates(chaser);
Role pointlessRole = tx.putRole("pointless-role");
tx.putRelationshipType("pointless").relates(pointlessRole);
chases.relates(pointlessRole).deleteRelates(pointlessRole);
dog.plays(chaser);
cat.plays(chased);
AttributeType<String> name = tx.putAttributeType("name", DataType.STRING);
AttributeType<String> id = tx.putAttributeType("id", DataType.STRING).setRegex("(good|bad)-dog");
AttributeType<Long> age = tx.putAttributeType("age", DataType.LONG);
animal.attribute(name);
animal.key(id);
dog.attribute(age).deleteAttribute(age);
cat.key(age).deleteKey(age);
cat.plays(chaser).deletePlays(chaser);
Entity dunstan = dog.addEntity();
Attribute<String> dunstanId = id.putAttribute("good-dog");
assertNotNull(dunstan.attributeRelationship(dunstanId));
Attribute<String> dunstanName = name.putAttribute("Dunstan");
dunstan.attribute(dunstanName).deleteAttribute(dunstanName);
chases.addRelationship().addRolePlayer(chaser, dunstan);
tx.commit();
}
try (GraknTx tx = localSession.open(GraknTxType.READ)) {
EntityType animal = tx.getEntityType("animal");
EntityType dog = tx.getEntityType("dog");
EntityType cat = tx.getEntityType("feline");
RelationshipType chases = tx.getRelationshipType("chases");
Role chased = tx.getRole("chased");
Role chaser = tx.getRole("chaser");
AttributeType<String> name = tx.getAttributeType("name");
AttributeType<String> id = tx.getAttributeType("id");
Entity dunstan = Iterators.getOnlyElement(dog.instances().iterator());
Relationship aChase = Iterators.getOnlyElement(chases.instances().iterator());
assertEquals(animal, dog.sup());
assertEquals(animal, cat.sup());
assertEquals(ImmutableSet.of(chased, chaser), chases.relates().collect(toSet()));
assertEquals(ImmutableSet.of(chaser), dog.plays().filter(role -> !role.isImplicit()).collect(toSet()));
assertEquals(ImmutableSet.of(chased), cat.plays().filter(role -> !role.isImplicit()).collect(toSet()));
assertEquals(ImmutableSet.of(name, id), animal.attributes().collect(toSet()));
assertEquals(ImmutableSet.of(id), animal.keys().collect(toSet()));
assertEquals(ImmutableSet.of(name, id), dog.attributes().collect(toSet()));
assertEquals(ImmutableSet.of(id), dog.keys().collect(toSet()));
assertEquals(ImmutableSet.of(name, id), cat.attributes().collect(toSet()));
assertEquals(ImmutableSet.of(id), cat.keys().collect(toSet()));
assertEquals("good-dog", Iterables.getOnlyElement(dunstan.keys(id).collect(toSet())).getValue());
ImmutableMap<Role, ImmutableSet<?>> expectedRolePlayers = ImmutableMap.of(chaser, ImmutableSet.of(dunstan), chased, ImmutableSet.of());
assertEquals(expectedRolePlayers, aChase.allRolePlayers());
assertEquals("(good|bad)-dog", id.getRegex());
assertFalse(dog.isAbstract());
assertTrue(cat.isAbstract());
}
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class GrpcServerIT method whenGettingARelationship_TheInformationOnTheRelationshipIsCorrect.
@Test
public void whenGettingARelationship_TheInformationOnTheRelationshipIsCorrect() {
try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
GraknTx localTx = localSession.open(GraknTxType.READ)) {
GetQuery query = remoteTx.graql().match(var("x").isa("has-cast")).get();
Relationship remoteConcept = query.stream().findAny().get().get("x").asRelationship();
Relationship localConcept = localTx.getConcept(remoteConcept.getId()).asRelationship();
assertEqualConcepts(localConcept, remoteConcept, Relationship::rolePlayers);
ImmutableMultimap.Builder<ConceptId, ConceptId> localRolePlayers = ImmutableMultimap.builder();
localConcept.allRolePlayers().forEach((role, players) -> {
for (Thing player : players) {
localRolePlayers.put(role.getId(), player.getId());
}
});
ImmutableMultimap.Builder<ConceptId, ConceptId> remoteRolePlayers = ImmutableMultimap.builder();
remoteConcept.allRolePlayers().forEach((role, players) -> {
for (Thing player : players) {
remoteRolePlayers.put(role.getId(), player.getId());
}
});
assertEquals(localRolePlayers.build(), remoteRolePlayers.build());
}
}
use of ai.grakn.concept.Relationship 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;
}
Aggregations