Search in sources :

Example 6 with GraknSession

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

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

the class RemoteGraknSessionTest method whenOpeningASessionWithAGivenUriAndKeyspace_TheUriAndKeyspaceAreSet.

@Test
public void whenOpeningASessionWithAGivenUriAndKeyspace_TheUriAndKeyspaceAreSet() {
    try (GraknSession session = RemoteGrakn.session(URI, KEYSPACE)) {
        assertEquals(URI.toString(), session.uri());
        assertEquals(KEYSPACE, session.keyspace());
    }
}
Also used : GraknSession(ai.grakn.GraknSession) Test(org.junit.Test)

Example 8 with GraknSession

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

the class MatchBenchmark method setup.

@Setup
public void setup() throws Throwable {
    GraknSession session = sessionContext.newSession();
    GraknTx graphEntity = session.open(GraknTxType.WRITE);
    EntityType entityType = graphEntity.putEntityType(BENCHMARK_ENTITY_TYPE);
    AttributeType<String> attributeType = graphEntity.putAttributeType(BENCHMARK_ATTRIBUTE_TYPE, AttributeType.DataType.STRING);
    entityType.attribute(attributeType);
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            entityType.addEntity().attribute(attributeType.putAttribute(String.valueOf(i)));
        }
    }
    graphEntity.commit();
    graph = session.open(GraknTxType.WRITE);
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) GraknSession(ai.grakn.GraknSession) Setup(org.openjdk.jmh.annotations.Setup)

Example 9 with GraknSession

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

the class DocTestUtil method getTestGraph.

public static GraknSession getTestGraph(SimpleURI uri, String knowledgeBaseName) {
    Keyspace keyspace = SampleKBLoader.randomKeyspace();
    GraknSession session = Grakn.session(uri, keyspace);
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        Consumer<GraknTx> loader = loaders.get(knowledgeBaseName);
        if (loader == null) {
            throw new IllegalArgumentException("Unknown knowledge base '" + knowledgeBaseName + "'");
        }
        loader.accept(tx);
        tx.commit();
    }
    return session;
}
Also used : GraknTx(ai.grakn.GraknTx) Keyspace(ai.grakn.Keyspace) GraknSession(ai.grakn.GraknSession)

Example 10 with GraknSession

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

the class BenchmarkIT method loadRandomisedRelationInstances.

private void loadRandomisedRelationInstances(String entityLabel, String fromRoleLabel, String toRoleLabel, String relationLabel, int N, GraknSession session, GraknClient graknClient, Keyspace keyspace) {
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        GraknTx tx = session.open(GraknTxType.READ);
        Var entityVar = var().asUserDefined();
        ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
        assertEquals(instances.length, N);
        Role fromRole = tx.getRole(fromRoleLabel);
        Role toRole = tx.getRole(toRoleLabel);
        RelationshipType relationType = tx.getRelationshipType(relationLabel);
        Random rand = new Random();
        Multimap<Integer, Integer> assignmentMap = HashMultimap.create();
        for (int i = 0; i < N; i++) {
            int from = rand.nextInt(N - 1);
            int to = rand.nextInt(N - 1);
            while (to == from && assignmentMap.get(from).contains(to)) to = rand.nextInt(N - 1);
            Var fromRolePlayer = Graql.var();
            Var toRolePlayer = Graql.var();
            Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(relationType.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[from])).and(toRolePlayer.asUserDefined().id(instances[to]));
            loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
        }
        tx.close();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Random(java.util.Random) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Aggregations

GraknSession (ai.grakn.GraknSession)13 GraknTx (ai.grakn.GraknTx)9 Test (org.junit.Test)9 Keyspace (ai.grakn.Keyspace)3 AttributeType (ai.grakn.concept.AttributeType)3 EntityType (ai.grakn.concept.EntityType)2 RelationshipType (ai.grakn.concept.RelationshipType)2 Role (ai.grakn.concept.Role)2 EmbeddedGraknSession (ai.grakn.factory.EmbeddedGraknSession)2 Var (ai.grakn.graql.Var)2 VarPattern (ai.grakn.graql.VarPattern)2 Grakn (ai.grakn.Grakn)1 GraknSystemProperty (ai.grakn.GraknSystemProperty)1 GraknTxType (ai.grakn.GraknTxType)1 BatchExecutorClient (ai.grakn.client.BatchExecutorClient)1 GraknClient (ai.grakn.client.GraknClient)1 ConceptId (ai.grakn.concept.ConceptId)1 Entity (ai.grakn.concept.Entity)1 GetQuery (ai.grakn.graql.GetQuery)1 Graql (ai.grakn.graql.Graql)1