Search in sources :

Example 1 with InvalidKBException

use of ai.grakn.exception.InvalidKBException in project grakn by graknlabs.

the class MigratorTestUtils method load.

public static void load(GraknSession factory, File schema) {
    try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
        graph.graql().parse(Files.readLines(schema, StandardCharsets.UTF_8).stream().collect(joining("\n"))).execute();
        graph.commit();
    } catch (IOException | InvalidKBException e) {
        throw new RuntimeException(e);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) InvalidKBException(ai.grakn.exception.InvalidKBException) IOException(java.io.IOException)

Example 2 with InvalidKBException

use of ai.grakn.exception.InvalidKBException 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 3 with InvalidKBException

use of ai.grakn.exception.InvalidKBException in project grakn by graknlabs.

the class ShortestPathTest method testMultiplePathsSharing1Instance.

@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
    ConceptId startId;
    ConceptId endId;
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        entityType.plays(role3).plays(role4);
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        Entity middle = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        ConceptId middleId = middle.getId();
        ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
        ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
        ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
        ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) List(java.util.List) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with InvalidKBException

use of ai.grakn.exception.InvalidKBException in project grakn by graknlabs.

the class ShortestPathTest method testMultipleIndependentShortestPaths.

@Test
public void testMultipleIndependentShortestPaths() throws InvalidKBException {
    Set<List<ConceptId>> validPaths = new HashSet<>();
    ConceptId startId;
    ConceptId endId;
    int numberOfPaths = 3;
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        // create N identical length paths
        for (int i = 0; i < numberOfPaths; i++) {
            List<ConceptId> validPath = new ArrayList<>();
            validPath.add(startId);
            Entity middle = entityType.addEntity();
            ConceptId middleId = middle.getId();
            ConceptId assertion1 = relationshipType.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
            validPath.add(assertion1);
            validPath.add(middleId);
            ConceptId assertion2 = relationshipType.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
            validPath.add(assertion2);
            validPath.add(endId);
            validPaths.add(validPath);
        }
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(numberOfPaths, allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(validPaths, computedPaths);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with InvalidKBException

use of ai.grakn.exception.InvalidKBException in project grakn by graknlabs.

the class ShortestPathTest method testMultiplePathsSharing3Instances.

@Test
public void testMultiplePathsSharing3Instances() throws InvalidKBException {
    ConceptId startId;
    ConceptId endId;
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        Role role5 = graph.putRole("role5");
        entityType.plays(role3).plays(role4).plays(role5);
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4).relates(role5);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        Entity middle = entityType.addEntity();
        Entity middleA = entityType.addEntity();
        Entity middleB = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        ConceptId middleId = middle.getId();
        ConceptId middleAId = middleA.getId();
        ConceptId middleBId = middleB.getId();
        ConceptId assertion1 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
        ConceptId assertion2 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, middleA).addRolePlayer(role5, middleB).getId();
        ConceptId assertion1A = relationshipType1.addRelationship().addRolePlayer(role1, middleA).addRolePlayer(role2, end).getId();
        ConceptId assertion1B = relationshipType1.addRelationship().addRolePlayer(role1, middleB).addRolePlayer(role2, end).getId();
        List<ConceptId> sharedPath = Lists.newArrayList(startId, assertion1, middleId, assertion2);
        List<ConceptId> path1 = new ArrayList<>(sharedPath);
        path1.addAll(Lists.newArrayList(middleAId, assertion1A, endId));
        List<ConceptId> path2 = new ArrayList<>(sharedPath);
        path2.addAll(Lists.newArrayList(middleBId, assertion1B, endId));
        correctPaths.add(path1);
        correctPaths.add(path2);
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

InvalidKBException (ai.grakn.exception.InvalidKBException)7 Test (org.junit.Test)6 GraknTx (ai.grakn.GraknTx)5 GraknTxType (ai.grakn.GraknTxType)5 Attribute (ai.grakn.concept.Attribute)5 AttributeType (ai.grakn.concept.AttributeType)5 EntityType (ai.grakn.concept.EntityType)5 Schema (ai.grakn.util.Schema)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 GraknSession (ai.grakn.GraknSession)4 Concept (ai.grakn.concept.Concept)4 ConceptId (ai.grakn.concept.ConceptId)4 Entity (ai.grakn.concept.Entity)4 Label (ai.grakn.concept.Label)4 RelationshipType (ai.grakn.concept.RelationshipType)4 Role (ai.grakn.concept.Role)4 GraknTestUtil (ai.grakn.util.GraknTestUtil)4 Matchers.empty (org.hamcrest.Matchers.empty)4