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