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();
}
}
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());
}
}
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);
}
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;
}
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();
}
}
Aggregations