Search in sources :

Example 1 with GraknSession

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);
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Var(ai.grakn.graql.Var) GraknSession(ai.grakn.GraknSession) RelationshipType(ai.grakn.concept.RelationshipType) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 2 with GraknSession

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;
        }
    }
}
Also used : Path(java.nio.file.Path) ConsoleReader(jline.console.ConsoleReader) SimpleURI(ai.grakn.util.SimpleURI) GraknSession(ai.grakn.GraknSession) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) Keyspace(ai.grakn.Keyspace) StatusRuntimeException(io.grpc.StatusRuntimeException)

Example 3 with GraknSession

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");
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test)

Example 4 with GraknSession

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();
}
Also used : GraknSession(ai.grakn.GraknSession) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 5 with GraknSession

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());
        }
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test)

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