Search in sources :

Example 1 with Casting

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);
}
Also used : EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Casting(ai.grakn.kb.internal.structure.Casting) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 2 with 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());
    }
}
Also used : Casting(ai.grakn.kb.internal.structure.Casting) RoleImpl(ai.grakn.kb.internal.concept.RoleImpl) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) RelationshipType(ai.grakn.concept.RelationshipType) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Thing(ai.grakn.concept.Thing) ThingImpl(ai.grakn.kb.internal.concept.ThingImpl) Test(org.junit.Test)

Example 3 with Casting

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));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Casting(ai.grakn.kb.internal.structure.Casting) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 4 with Casting

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));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Casting(ai.grakn.kb.internal.structure.Casting) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Aggregations

Casting (ai.grakn.kb.internal.structure.Casting)4 RelationshipType (ai.grakn.concept.RelationshipType)3 Test (org.junit.Test)3 EntityType (ai.grakn.concept.EntityType)2 Role (ai.grakn.concept.Role)2 ConceptId (ai.grakn.concept.ConceptId)1 Entity (ai.grakn.concept.Entity)1 Thing (ai.grakn.concept.Thing)1 EntityTypeImpl (ai.grakn.kb.internal.concept.EntityTypeImpl)1 RelationshipImpl (ai.grakn.kb.internal.concept.RelationshipImpl)1 RoleImpl (ai.grakn.kb.internal.concept.RoleImpl)1 ThingImpl (ai.grakn.kb.internal.concept.ThingImpl)1 EdgeElement (ai.grakn.kb.internal.structure.EdgeElement)1 Edge (org.apache.tinkerpop.gremlin.structure.Edge)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1