use of ai.grakn.GraknSession in project grakn by graknlabs.
the class BenchmarkTests method nonRecursiveChainOfRules.
/**
* Executes a scalability test defined in terms of the number of rules in the system. Creates a simple rule chain:
*
* R_i(x, y) := R_{i-1}(x, y); i e [1, N]
*
* with a single initial relation instance R_0(a ,b)
*/
@Test
public void nonRecursiveChainOfRules() {
final int N = 200;
LOG.debug(new Object() {
}.getClass().getEnclosingMethod().getName());
GraknSession graknSession = sessionContext.newSession();
// NB: loading data here as defining it as KB and using graql api leads to circular dependencies
try (GraknTx tx = graknSession.open(GraknTxType.WRITE)) {
Role fromRole = tx.putRole("fromRole");
Role toRole = tx.putRole("toRole");
RelationshipType relation0 = tx.putRelationshipType("relation0").relates(fromRole).relates(toRole);
for (int i = 1; i <= N; i++) {
tx.putRelationshipType("relation" + i).relates(fromRole).relates(toRole);
}
EntityType genericEntity = tx.putEntityType("genericEntity").plays(fromRole).plays(toRole);
Entity fromEntity = genericEntity.addEntity();
Entity toEntity = genericEntity.addEntity();
relation0.addRelationship().addRolePlayer(fromRole, fromEntity).addRolePlayer(toRole, toEntity);
for (int i = 1; i <= N; i++) {
Var fromVar = Graql.var().asUserDefined();
Var toVar = Graql.var().asUserDefined();
VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + i)));
tx.graql().define(rulePattern).execute();
}
tx.commit();
}
try (GraknTx tx = graknSession.open(GraknTxType.READ)) {
final long limit = 1;
String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
String queryString = "match " + queryPattern + " get;";
String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
assertEquals(executeQuery(queryString, tx, "full").size(), limit);
assertEquals(executeQuery(limitedQueryString, tx, "limit").size(), limit);
}
}
use of ai.grakn.GraknSession in project grakn by graknlabs.
the class GraqlConsole method start.
public static boolean start(GraqlShellOptions options, SessionProvider sessionProvider, String historyFile, PrintStream sout, PrintStream serr) throws InterruptedException, IOException {
List<String> queries = null;
// ------- Check option ------------------------
String query = options.getQuery();
// This is a best-effort guess as to whether the user has made a mistake, without parsing the query
if (query != null) {
queries = ImmutableList.of(query);
if (!query.contains("$") && query.trim().startsWith("match")) {
serr.println(ErrorMessage.NO_VARIABLE_IN_QUERY.getMessage());
}
}
// Print usage message if requested or if invalid arguments provided
if (options.displayHelp()) {
options.printUsage(sout);
return true;
}
if (options.displayVersion()) {
sout.println(GraknVersion.VERSION);
return true;
}
// ------- Check option ------------------------
Path path = options.getBatchLoadPath();
if (path != null) {
Keyspace keyspace = options.getKeyspace();
SimpleURI location = options.getUri();
SimpleURI httpUri = location != null ? location : Grakn.DEFAULT_URI;
try {
BatchLoader.sendBatchRequest(httpUri, keyspace, path, sout, serr);
} catch (Exception e) {
sout.println("Batch failed \n" + CommonUtil.simplifyExceptionMessage(e));
return false;
}
return true;
}
// -------- If no option set we start GraqlShell ----------
OutputFormat outputFormat = options.getOutputFormat();
boolean infer = options.shouldInfer();
ConsoleReader console = new ConsoleReader(System.in, sout);
GraknSession session = sessionProvider.getSession(options, console);
try (GraqlShell shell = new GraqlShell(historyFile, session, console, serr, outputFormat, infer)) {
List<Path> filePaths = options.getFiles();
if (filePaths != null) {
queries = loadQueries(filePaths);
}
// Start shell
shell.start(queries);
return !shell.errorOccurred();
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode().equals(Status.Code.UNAVAILABLE)) {
serr.println(ErrorMessage.COULD_NOT_CONNECT.getMessage());
return false;
} else {
throw e;
}
}
}
use of ai.grakn.GraknSession in project grakn by graknlabs.
the class EngineGraknSessionTest method closeGraphWhenOnlyOneTransactionIsOpen.
@Test
public void closeGraphWhenOnlyOneTransactionIsOpen() {
// Tinker does not have any connections to close
assumeFalse(GraknTestUtil.usingTinker());
GraknSession factory = Grakn.session(sparkContext.uri(), SampleKBLoader.randomKeyspace());
GraknTx graph = factory.open(GraknTxType.WRITE);
factory.close();
expectedException.expect(GraknTxOperationException.class);
expectedException.expectMessage(ErrorMessage.SESSION_CLOSED.getMessage(graph.keyspace()));
graph.putEntityType("A thingy");
}
use of ai.grakn.GraknSession in project grakn by graknlabs.
the class RemoteGraknSessionTest method whenClosingASession_ShutdownTheChannel.
@Test
public void whenClosingASession_ShutdownTheChannel() {
ManagedChannel channel = mock(ManagedChannel.class);
GraknSession ignored = RemoteGraknSession.create(KEYSPACE, URI, channel);
ignored.close();
verify(channel).shutdown();
}
use of ai.grakn.GraknSession in project grakn by graknlabs.
the class RemoteGraknSessionTest method whenOpeningATransactionFromASession_ReturnATransactionWithParametersSet.
@Test
public void whenOpeningATransactionFromASession_ReturnATransactionWithParametersSet() {
try (GraknSession session = RemoteGraknSession.create(KEYSPACE, URI, server.channel())) {
try (GraknTx tx = session.open(GraknTxType.READ)) {
assertEquals(session, tx.session());
assertEquals(KEYSPACE, tx.keyspace());
assertEquals(GraknTxType.READ, tx.txType());
}
}
}
Aggregations