Search in sources :

Example 66 with GraknTx

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

the class GraknTxTinkerFactoryTest method whenGettingGraphFromFactoryClosingItAndGettingItAgain_ReturnGraph.

@Test
public void whenGettingGraphFromFactoryClosingItAndGettingItAgain_ReturnGraph() {
    TxFactoryTinker factory = new TxFactoryTinker(session);
    GraknTx graph1 = factory.open(GraknTxType.WRITE);
    graph1.close();
    GraknTxTinker graph2 = factory.open(GraknTxType.WRITE);
    assertEquals(graph1, graph2);
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GraknTxTinker(ai.grakn.kb.internal.GraknTxTinker) Test(org.junit.Test)

Example 67 with GraknTx

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

the class GraknTxTinkerTest method addRandomEntity.

private synchronized void addRandomEntity() {
    try (GraknTx graph = Grakn.session(Grakn.IN_MEMORY, tx.keyspace()).open(GraknTxType.WRITE)) {
        graph.getEntityType("Thing").addEntity();
        graph.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx)

Example 68 with GraknTx

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

the class ConceptController method getTypeInstances.

private String getTypeInstances(Request request, Response response) throws JsonProcessingException {
    response.type(APPLICATION_JSON);
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    Label label = Label.of(mandatoryPathParameter(request, LABEL_PARAMETER));
    int offset = getOffset(request);
    int limit = getLimit(request);
    try (GraknTx tx = factory.tx(keyspace, READ);
        Timer.Context context = instancesGetTimer.time()) {
        Type type = tx.getType(label);
        if (type == null) {
            response.status(SC_NOT_FOUND);
            return "";
        }
        // Get the wrapper
        Things things = ConceptBuilder.buildThings(type, offset, limit);
        response.status(SC_OK);
        return objectMapper.writeValueAsString(things);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Type(ai.grakn.concept.Type) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Things(ai.grakn.engine.controller.response.Things) Label(ai.grakn.concept.Label)

Example 69 with GraknTx

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

the class ConceptController method getConceptCollection.

private <X extends ai.grakn.concept.Concept> String getConceptCollection(Request request, Response response, String key, Function<GraknTx, X> getter, Function<X, Stream<Jacksonisable>> collector) throws JsonProcessingException {
    response.type(APPLICATION_JSON);
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    try (GraknTx tx = factory.tx(keyspace, READ);
        Timer.Context context = labelGetTimer.time()) {
        X concept = getter.apply(tx);
        // If the concept was not found return;
        if (concept == null) {
            response.status(SC_NOT_FOUND);
            return "[]";
        }
        List<Jacksonisable> list = collector.apply(concept).collect(Collectors.toList());
        Link link = Link.create(request.pathInfo());
        ListResource<Jacksonisable> listResource = ListResource.create(link, key, list);
        return objectMapper.writeValueAsString(listResource);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Jacksonisable(ai.grakn.engine.Jacksonisable) Link(ai.grakn.engine.controller.response.Link)

Example 70 with GraknTx

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

the class ConceptController method getConcepts.

private String getConcepts(Request request, Response response, String key, Function<GraknTx, Stream<? extends ai.grakn.concept.Concept>> getter) throws JsonProcessingException {
    response.type(APPLICATION_JSON);
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    try (GraknTx tx = factory.tx(keyspace, READ);
        Timer.Context context = labelGetTimer.time()) {
        List<Concept> concepts = getter.apply(tx).map(ConceptBuilder::<Concept>build).collect(Collectors.toList());
        ListResource list = ListResource.create(Requests.selfLink(request), key, concepts);
        response.status(SC_OK);
        return objectMapper.writeValueAsString(list);
    }
}
Also used : Concept(ai.grakn.engine.controller.response.Concept) GraknTx(ai.grakn.GraknTx) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) ListResource(ai.grakn.engine.controller.response.ListResource)

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