Search in sources :

Example 1 with Concept

use of ai.grakn.engine.controller.response.Concept 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)

Example 2 with Concept

use of ai.grakn.engine.controller.response.Concept in project grakn by graknlabs.

the class JsonConceptBuilder method build.

public static <X extends Concept> X build(String jsonRepresentation) {
    Schema.BaseType baseType = Schema.BaseType.valueOf(Json.read(jsonRepresentation).at("base-type").asString());
    Concept concept;
    try {
        switch(baseType) {
            case ROLE:
                concept = mapper.readValue(jsonRepresentation, Role.class);
                break;
            case RULE:
                concept = mapper.readValue(jsonRepresentation, Rule.class);
                break;
            case TYPE:
                concept = mapper.readValue(jsonRepresentation, MetaConcept.class);
                break;
            case ENTITY_TYPE:
                concept = mapper.readValue(jsonRepresentation, EntityType.class);
                break;
            case ENTITY:
                concept = mapper.readValue(jsonRepresentation, Entity.class);
                break;
            case ATTRIBUTE_TYPE:
                concept = mapper.readValue(jsonRepresentation, AttributeType.class);
                break;
            case ATTRIBUTE:
                concept = mapper.readValue(jsonRepresentation, Attribute.class);
                break;
            case RELATIONSHIP_TYPE:
                concept = mapper.readValue(jsonRepresentation, RelationshipType.class);
                break;
            case RELATIONSHIP:
                concept = mapper.readValue(jsonRepresentation, Relationship.class);
                break;
            default:
                throw new UnsupportedOperationException(String.format("Cannot wrap object of base type {%s}", baseType));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return (X) concept;
}
Also used : MetaConcept(ai.grakn.engine.controller.response.MetaConcept) Concept(ai.grakn.engine.controller.response.Concept) Entity(ai.grakn.engine.controller.response.Entity) Attribute(ai.grakn.engine.controller.response.Attribute) Schema(ai.grakn.util.Schema) RelationshipType(ai.grakn.engine.controller.response.RelationshipType) IOException(java.io.IOException) Role(ai.grakn.engine.controller.response.Role) EntityType(ai.grakn.engine.controller.response.EntityType) AttributeType(ai.grakn.engine.controller.response.AttributeType) Relationship(ai.grakn.engine.controller.response.Relationship) Rule(ai.grakn.engine.controller.response.Rule) MetaConcept(ai.grakn.engine.controller.response.MetaConcept)

Example 3 with Concept

use of ai.grakn.engine.controller.response.Concept in project grakn by graknlabs.

the class JacksonPrinterTest method whenGraqlQueryResultsInConcept_EnsureConceptWrapperIsReturned.

@Test
public void whenGraqlQueryResultsInConcept_EnsureConceptWrapperIsReturned() throws JsonProcessingException {
    ai.grakn.concept.Concept concept = rule.tx().graql().match(var("x").isa("person")).get("x").iterator().next();
    Concept conceptWrapper = ConceptBuilder.build(concept);
    assertWrappersMatch(conceptWrapper, concept);
}
Also used : Concept(ai.grakn.engine.controller.response.Concept) Test(org.junit.Test)

Example 4 with Concept

use of ai.grakn.engine.controller.response.Concept in project grakn by graknlabs.

the class ConceptControllerTest method whenGettingConceptsByLabel_EnsureConceptsAreReturned.

@Test
public void whenGettingConceptsByLabel_EnsureConceptsAreReturned() {
    assertConceptsReturned(REST.WebPath.KEYSPACE_ROLE, Role[].class, "roles", roleWrapper1, roleWrapper2);
    assertConceptsReturned(REST.WebPath.KEYSPACE_RULE, Rule[].class, "rules", ruleWrapper);
    // Manual Check is necessary due to collection containing mixture of concepts
    String request = REST.resolveTemplate(REST.WebPath.KEYSPACE_TYPE, keyspace.getValue());
    List<Json> response = Json.read(RestAssured.when().get(request).body().asString()).at("types").asJsonList();
    Set<Concept> types = response.stream().map(JsonConceptBuilder::<Concept>build).collect(Collectors.toSet());
    assertTrue(String.format("Type {$s} missing from response", relationshipTypeWrapper), types.contains(relationshipTypeWrapper));
    assertTrue(String.format("Type {$s} missing from response", attributeTypeWrapper), types.contains(attributeTypeWrapper));
    assertTrue(String.format("Type {$s} missing from response", entityTypeWrapper), types.contains(entityTypeWrapper));
}
Also used : Role(ai.grakn.engine.controller.response.Role) Concept(ai.grakn.engine.controller.response.Concept) Rule(ai.grakn.engine.controller.response.Rule) ClassRule(org.junit.ClassRule) Matchers.containsString(org.hamcrest.Matchers.containsString) Json(mjson.Json) Test(org.junit.Test)

Example 5 with Concept

use of ai.grakn.engine.controller.response.Concept in project grakn by graknlabs.

the class ConceptController method getConcept.

private String getConcept(Response response, Keyspace keyspace, Function<GraknTx, ai.grakn.concept.Concept> getter) throws JsonProcessingException {
    response.type(APPLICATION_JSON);
    try (GraknTx tx = factory.tx(keyspace, READ);
        Timer.Context context = conceptIdGetTimer.time()) {
        ai.grakn.concept.Concept concept = getter.apply(tx);
        Optional<Concept> conceptWrapper = Optional.ofNullable(concept).map(ConceptBuilder::build);
        if (conceptWrapper.isPresent()) {
            response.status(SC_OK);
            return objectMapper.writeValueAsString(conceptWrapper.get());
        } else {
            response.status(SC_NOT_FOUND);
            return "";
        }
    }
}
Also used : Concept(ai.grakn.engine.controller.response.Concept) GraknTx(ai.grakn.GraknTx) ConceptBuilder(ai.grakn.engine.controller.response.ConceptBuilder) Timer(com.codahale.metrics.Timer)

Aggregations

Concept (ai.grakn.engine.controller.response.Concept)7 Test (org.junit.Test)3 GraknTx (ai.grakn.GraknTx)2 Role (ai.grakn.engine.controller.response.Role)2 Rule (ai.grakn.engine.controller.response.Rule)2 Timer (com.codahale.metrics.Timer)2 Json (mjson.Json)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Keyspace (ai.grakn.Keyspace)1 Attribute (ai.grakn.engine.controller.response.Attribute)1 AttributeType (ai.grakn.engine.controller.response.AttributeType)1 ConceptBuilder (ai.grakn.engine.controller.response.ConceptBuilder)1 Entity (ai.grakn.engine.controller.response.Entity)1 EntityType (ai.grakn.engine.controller.response.EntityType)1 ListResource (ai.grakn.engine.controller.response.ListResource)1 MetaConcept (ai.grakn.engine.controller.response.MetaConcept)1 Relationship (ai.grakn.engine.controller.response.Relationship)1 RelationshipType (ai.grakn.engine.controller.response.RelationshipType)1 Schema (ai.grakn.util.Schema)1 Response (com.jayway.restassured.response.Response)1