Search in sources :

Example 61 with Role

use of ai.grakn.concept.Role in project grakn by graknlabs.

the class RelationshipReified method innerToString.

@Override
public String innerToString() {
    StringBuilder description = new StringBuilder();
    description.append("ID [").append(getId()).append("] Type [").append(type().getLabel()).append("] Roles and Role Players: \n");
    for (Map.Entry<Role, Set<Thing>> entry : allRolePlayers().entrySet()) {
        if (entry.getValue().isEmpty()) {
            description.append("    Role [").append(entry.getKey().getLabel()).append("] not played by any instance \n");
        } else {
            StringBuilder instancesString = new StringBuilder();
            for (Thing thing : entry.getValue()) {
                instancesString.append(thing.getId()).append(",");
            }
            description.append("    Role [").append(entry.getKey().getLabel()).append("] played by [").append(instancesString.toString()).append("] \n");
        }
    }
    return description.toString();
}
Also used : Role(ai.grakn.concept.Role) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Map(java.util.Map) Thing(ai.grakn.concept.Thing)

Example 62 with Role

use of ai.grakn.concept.Role in project grakn by graknlabs.

the class GraknTxTest method checkThatMainCentralCacheIsNotAffectedByTransactionModifications.

@Test
public void checkThatMainCentralCacheIsNotAffectedByTransactionModifications() throws InvalidKBException, ExecutionException, InterruptedException {
    // Check Central cache is empty
    assertCacheOnlyContainsMetaTypes();
    Role r1 = tx.putRole("r1");
    Role r2 = tx.putRole("r2");
    EntityType e1 = tx.putEntityType("e1").plays(r1).plays(r2);
    RelationshipType rel1 = tx.putRelationshipType("rel1").relates(r1).relates(r2);
    // Purge the above concepts into the main cache
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    // Check cache is in good order
    Collection<SchemaConcept> cachedValues = tx.getGlobalCache().getCachedTypes().values();
    assertTrue("Type [" + r1 + "] was not cached", cachedValues.contains(r1));
    assertTrue("Type [" + r2 + "] was not cached", cachedValues.contains(r2));
    assertTrue("Type [" + e1 + "] was not cached", cachedValues.contains(e1));
    assertTrue("Type [" + rel1 + "] was not cached", cachedValues.contains(rel1));
    assertThat(e1.plays().collect(toSet()), containsInAnyOrder(r1, r2));
    ExecutorService pool = Executors.newSingleThreadExecutor();
    // Mutate Schema in a separate thread
    pool.submit(() -> {
        GraknTx innerGraph = Grakn.session(Grakn.IN_MEMORY, tx.keyspace()).open(GraknTxType.WRITE);
        EntityType entityType = innerGraph.getEntityType("e1");
        Role role = innerGraph.getRole("r1");
        entityType.deletePlays(role);
    }).get();
    // Check the above mutation did not affect central repo
    SchemaConcept foundE1 = tx.getGlobalCache().getCachedTypes().get(e1.getLabel());
    assertTrue("Main cache was affected by transaction", foundE1.asType().plays().anyMatch(role -> role.equals(r1)));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) CoreMatchers.is(org.hamcrest.CoreMatchers.is) InvalidKBException(ai.grakn.exception.InvalidKBException) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) Entity(ai.grakn.concept.Entity) Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ExecutorService(java.util.concurrent.ExecutorService) Collectors.toSet(java.util.stream.Collectors.toSet) Grakn(ai.grakn.Grakn) IsInstanceOf(org.hamcrest.core.IsInstanceOf) GraknTxType(ai.grakn.GraknTxType) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) Matchers.empty(org.hamcrest.Matchers.empty) VerificationException(org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.VerificationException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Shard(ai.grakn.kb.internal.structure.Shard) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Stream(java.util.stream.Stream) Assert.assertNull(org.junit.Assert.assertNull) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) RelationshipType(ai.grakn.concept.RelationshipType) ExecutorService(java.util.concurrent.ExecutorService) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Example 63 with Role

use of ai.grakn.concept.Role in project grakn by graknlabs.

the class ValidateGlobalRulesTest method testValidatePlaysStructureUnique.

@Test
public void testValidatePlaysStructureUnique() {
    Role role1 = tx.putRole("role1");
    Role role2 = tx.putRole("role2");
    RelationshipType relationshipType = tx.putRelationshipType("rt").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("et");
    ((EntityTypeImpl) entityType).plays(role1, true);
    ((EntityTypeImpl) entityType).plays(role2, false);
    Entity other1 = entityType.addEntity();
    Entity other2 = entityType.addEntity();
    EntityImpl entity = (EntityImpl) entityType.addEntity();
    RelationshipImpl relation1 = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role2, other1).addRolePlayer(role1, entity);
    // Valid with only a single relation
    relation1.reified().get().castingsRelation().forEach(rolePlayer -> assertTrue(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty()));
    RelationshipImpl relation2 = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role2, other2).addRolePlayer(role1, entity);
    // Invalid with multiple relations
    relation1.reified().get().castingsRelation().forEach(rolePlayer -> {
        if (rolePlayer.getRole().equals(role1)) {
            assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty());
        }
    });
    relation2.reified().get().castingsRelation().forEach(rolePlayer -> {
        if (rolePlayer.getRole().equals(role1)) {
            assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty());
        }
    });
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) EntityImpl(ai.grakn.kb.internal.concept.EntityImpl) RelationshipType(ai.grakn.concept.RelationshipType) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Test(org.junit.Test)

Example 64 with Role

use of ai.grakn.concept.Role in project grakn by graknlabs.

the class ValidateGlobalRulesTest method testAbstractConceptValidation.

@Test
public void testAbstractConceptValidation() {
    Role role = tx.putRole("relates");
    RelationshipType relationshipType = tx.putRelationshipType("relationTypes");
    assertTrue(ValidateGlobalRules.validateHasSingleIncomingRelatesEdge(role).isPresent());
    assertTrue(ValidateGlobalRules.validateHasMinimumRoles(relationshipType).isPresent());
    relationshipType.setAbstract(true);
    assertTrue(ValidateGlobalRules.validateHasSingleIncomingRelatesEdge(role).isPresent());
    assertFalse(ValidateGlobalRules.validateHasMinimumRoles(relationshipType).isPresent());
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 65 with Role

use of ai.grakn.concept.Role in project grakn by graknlabs.

the class ValidateGlobalRulesTest method testValidateRelationTypeRelates.

@Test
public void testValidateRelationTypeRelates() throws Exception {
    Role hunter = tx.putRole("hunter");
    RelationshipType kills = tx.putRelationshipType("kills");
    assertTrue(ValidateGlobalRules.validateHasMinimumRoles(kills).isPresent());
    kills.relates(hunter);
    assertFalse(ValidateGlobalRules.validateHasMinimumRoles(kills).isPresent());
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Aggregations

Role (ai.grakn.concept.Role)189 Test (org.junit.Test)124 RelationshipType (ai.grakn.concept.RelationshipType)114 EntityType (ai.grakn.concept.EntityType)92 Entity (ai.grakn.concept.Entity)55 GraknTx (ai.grakn.GraknTx)48 Relationship (ai.grakn.concept.Relationship)37 Set (java.util.Set)36 ConceptId (ai.grakn.concept.ConceptId)33 Label (ai.grakn.concept.Label)33 Thing (ai.grakn.concept.Thing)32 HashSet (java.util.HashSet)31 Var (ai.grakn.graql.Var)25 AttributeType (ai.grakn.concept.AttributeType)21 Concept (ai.grakn.concept.Concept)20 Schema (ai.grakn.util.Schema)17 Collectors (java.util.stream.Collectors)17 Attribute (ai.grakn.concept.Attribute)16 SchemaConcept (ai.grakn.concept.SchemaConcept)16 Type (ai.grakn.concept.Type)16