Search in sources :

Example 86 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GraknTxTest method attemptingToUseClosedGraphFailingThenOpeningGraph_EnsureGraphIsUsable.

@Test
public void attemptingToUseClosedGraphFailingThenOpeningGraph_EnsureGraphIsUsable() throws InvalidKBException {
    GraknTx graph = Grakn.session(Grakn.IN_MEMORY, "testingagain").open(GraknTxType.WRITE);
    graph.close();
    boolean errorThrown = false;
    try {
        graph.putEntityType("A Thing");
    } catch (GraknTxOperationException e) {
        if (e.getMessage().equals(ErrorMessage.TX_CLOSED_ON_ACTION.getMessage("closed", graph.keyspace()))) {
            errorThrown = true;
        }
    }
    assertTrue("Graph not correctly closed", errorThrown);
    graph = Grakn.session(Grakn.IN_MEMORY, "testingagain").open(GraknTxType.WRITE);
    graph.putEntityType("A Thing");
}
Also used : GraknTx(ai.grakn.GraknTx) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Test(org.junit.Test)

Example 87 with GraknTx

use of ai.grakn.GraknTx 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 88 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GraknTxTest method whenCreatingAValidSchemaInSeparateThreads_EnsureValidationRulesHold.

@Test
public void whenCreatingAValidSchemaInSeparateThreads_EnsureValidationRulesHold() throws ExecutionException, InterruptedException {
    GraknSession session = Grakn.session(Grakn.IN_MEMORY, "hi");
    ExecutorService executor = Executors.newCachedThreadPool();
    executor.submit(() -> {
        // Resources
        try (GraknTx graph = session.open(GraknTxType.WRITE)) {
            AttributeType<Long> int_ = graph.putAttributeType("int", AttributeType.DataType.LONG);
            AttributeType<Long> foo = graph.putAttributeType("foo", AttributeType.DataType.LONG).sup(int_);
            graph.putAttributeType("bar", AttributeType.DataType.LONG).sup(int_);
            graph.putEntityType("FOO").attribute(foo);
            graph.commit();
        }
    }).get();
    // Relationship Which Has Resources
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        graph.putEntityType("BAR").attribute(graph.getAttributeType("bar"));
        graph.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) AttributeType(ai.grakn.concept.AttributeType) GraknSession(ai.grakn.GraknSession) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 89 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GraknTxTest method whenPassingGraphToAnotherThreadWithoutOpening_Throw.

@Test
public void whenPassingGraphToAnotherThreadWithoutOpening_Throw() throws ExecutionException, InterruptedException {
    ExecutorService pool = Executors.newSingleThreadExecutor();
    GraknTx graph = Grakn.session(Grakn.IN_MEMORY, "testing").open(GraknTxType.WRITE);
    expectedException.expectCause(IsInstanceOf.instanceOf(GraknTxOperationException.class));
    expectedException.expectMessage(GraknTxOperationException.transactionClosed(graph, null).getMessage());
    Future future = pool.submit(() -> {
        graph.putEntityType("A Thing");
    });
    future.get();
}
Also used : GraknTx(ai.grakn.GraknTx) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Test(org.junit.Test)

Example 90 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class GrpcServerIT method whenGettingASchemaConcept_TheInformationOnTheSchemaConceptIsCorrect.

@Test
public void whenGettingASchemaConcept_TheInformationOnTheSchemaConceptIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").label("actor")).get();
        SchemaConcept remoteConcept = query.stream().findAny().get().get("x").asSchemaConcept();
        SchemaConcept localConcept = localTx.getConcept(remoteConcept.getId()).asSchemaConcept();
        assertEquals(localConcept.isImplicit(), remoteConcept.isImplicit());
        assertEquals(localConcept.getLabel(), remoteConcept.getLabel());
        assertEquals(localConcept.sup().getId(), remoteConcept.sup().getId());
        assertEqualConcepts(localConcept, remoteConcept, SchemaConcept::sups);
        assertEqualConcepts(localConcept, remoteConcept, SchemaConcept::subs);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) SchemaConcept(ai.grakn.concept.SchemaConcept) Test(org.junit.Test)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17