use of ai.grakn.kb.internal.structure.Casting in project grakn by graknlabs.
the class RelationshipReified method putRolePlayerEdge.
/**
* If the edge does not exist then it adds a {@link Schema.EdgeLabel#ROLE_PLAYER} edge from
* this {@link Relationship} to a target {@link Thing} which is playing some {@link Role}.
*
* If the edge does exist nothing is done.
*
* @param role The {@link Role} being played by the {@link Thing} in this {@link Relationship}
* @param toThing The {@link Thing} playing a {@link Role} in this {@link Relationship}
*/
public void putRolePlayerEdge(Role role, Thing toThing) {
// Checking if the edge exists
GraphTraversal<Vertex, Edge> traversal = vertex().tx().getTinkerTraversal().V().has(Schema.VertexProperty.ID.name(), this.getId().getValue()).outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()).has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), this.type().getLabelId().getValue()).has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), role.getLabelId().getValue()).as("edge").inV().has(Schema.VertexProperty.ID.name(), toThing.getId()).select("edge");
if (traversal.hasNext()) {
return;
}
// Role player edge does not exist create a new one
EdgeElement edge = this.addEdge(ConceptVertex.from(toThing), Schema.EdgeLabel.ROLE_PLAYER);
edge.property(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID, this.type().getLabelId().getValue());
edge.property(Schema.EdgeProperty.ROLE_LABEL_ID, role.getLabelId().getValue());
Casting casting = Casting.create(edge, owner, role, toThing);
vertex().tx().txCache().trackForValidation(casting);
}
use of ai.grakn.kb.internal.structure.Casting in project grakn by graknlabs.
the class ValidateGlobalRulesTest method testValidatePlaysStructure.
@Test
public void testValidatePlaysStructure() throws Exception {
EntityTypeImpl wolf = (EntityTypeImpl) tx.putEntityType("wolf");
EntityTypeImpl creature = (EntityTypeImpl) tx.putEntityType("creature");
EntityTypeImpl hunter = (EntityTypeImpl) tx.putEntityType("hunter");
RelationshipType hunts = tx.putRelationshipType("hunts");
RoleImpl witcher = (RoleImpl) tx.putRole("witcher");
RoleImpl monster = (RoleImpl) tx.putRole("monster");
Thing geralt = hunter.addEntity();
ThingImpl werewolf = (ThingImpl) wolf.addEntity();
RelationshipImpl assertion = (RelationshipImpl) hunts.addRelationship().addRolePlayer(witcher, geralt).addRolePlayer(monster, werewolf);
assertion.reified().get().castingsRelation().forEach(rolePlayer -> assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty()));
hunter.plays(witcher);
boolean[] flags = { false, false };
int count = 0;
for (Casting casting : assertion.reified().get().castingsRelation().collect(Collectors.toSet())) {
flags[count] = !ValidateGlobalRules.validatePlaysAndRelatesStructure(casting).isEmpty();
count++;
}
assertTrue(flags[0] && flags[1]);
wolf.sup(creature);
creature.plays(monster);
for (Casting casting : assertion.reified().get().castingsRelation().collect(Collectors.toSet())) {
assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(casting).isEmpty());
}
}
use of ai.grakn.kb.internal.structure.Casting in project grakn by graknlabs.
the class TxCacheTest method whenCreatingRelations_EnsureRolePlayersAreCached.
@Test
public void whenCreatingRelations_EnsureRolePlayersAreCached() {
Role r1 = tx.putRole("r1");
Role r2 = tx.putRole("r2");
EntityType t1 = tx.putEntityType("t1").plays(r1).plays(r2);
RelationshipType rt1 = tx.putRelationshipType("rel1").relates(r1).relates(r2);
Entity e1 = t1.addEntity();
Entity e2 = t1.addEntity();
assertThat(tx.txCache().getModifiedCastings(), empty());
Set<Casting> castings = ((RelationshipImpl) rt1.addRelationship().addRolePlayer(r1, e1).addRolePlayer(r2, e2)).reified().get().castingsRelation().collect(toSet());
assertTrue(tx.txCache().getModifiedCastings().containsAll(castings));
}
use of ai.grakn.kb.internal.structure.Casting in project grakn by graknlabs.
the class EntityTest method whenDeletingInstanceInRelationShip_TheInstanceAndCastingsAreDeletedAndTheRelationRemains.
@Test
public void whenDeletingInstanceInRelationShip_TheInstanceAndCastingsAreDeletedAndTheRelationRemains() throws GraknTxOperationException {
// Schema
EntityType type = tx.putEntityType("Concept Type");
RelationshipType relationshipType = tx.putRelationshipType("relationTypes");
Role role1 = tx.putRole("role1");
Role role2 = tx.putRole("role2");
Role role3 = tx.putRole("role3");
// Data
ThingImpl<?, ?> rolePlayer1 = (ThingImpl) type.addEntity();
ThingImpl<?, ?> rolePlayer2 = (ThingImpl) type.addEntity();
ThingImpl<?, ?> rolePlayer3 = (ThingImpl) type.addEntity();
relationshipType.relates(role1);
relationshipType.relates(role2);
relationshipType.relates(role3);
// Check Structure is in order
RelationshipImpl relation = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role1, rolePlayer1).addRolePlayer(role2, rolePlayer2).addRolePlayer(role3, rolePlayer3);
Casting rp1 = rolePlayer1.castingsInstance().findAny().get();
Casting rp2 = rolePlayer2.castingsInstance().findAny().get();
Casting rp3 = rolePlayer3.castingsInstance().findAny().get();
assertThat(relation.reified().get().castingsRelation().collect(toSet()), containsInAnyOrder(rp1, rp2, rp3));
// Delete And Check Again
ConceptId idOfDeleted = rolePlayer1.getId();
rolePlayer1.delete();
assertNull(tx.getConcept(idOfDeleted));
assertThat(relation.reified().get().castingsRelation().collect(toSet()), containsInAnyOrder(rp2, rp3));
}
Aggregations