use of uk.gov.gchq.gaffer.traffic.transform.DescriptionTransform in project gaffer-doc by gchq.
the class Transforms method run.
@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
// [generate] Create some edges from the simple data file using our Road Use generator class
// ---------------------------------------------------------
final List<Element> elements = new ArrayList<>();
final RoadAndRoadUseElementGenerator dataGenerator = new RoadAndRoadUseElementGenerator();
for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
Iterables.addAll(elements, dataGenerator._apply(line));
}
// ---------------------------------------------------------
print("Elements generated from the data file.");
for (final Element element : elements) {
print("GENERATED_EDGES", element.toString());
}
print("");
// [graph] Create a graph using our schema and store properties
// ---------------------------------------------------------
final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
// ---------------------------------------------------------
// [user] Create a user
// ---------------------------------------------------------
final User user = new User("user01");
// ---------------------------------------------------------
// [add] Add the edges to the graph
// ---------------------------------------------------------
final AddElements addElements = new AddElements.Builder().input(elements).build();
graph.execute(addElements, user);
// ---------------------------------------------------------
print("The elements have been added.");
// [get simple] get all the edges that contain the vertex "1"
// ---------------------------------------------------------
final GetElements getEdges = new GetElements.Builder().input(new EntitySeed("10")).build();
final CloseableIterable<? extends Element> results = graph.execute(getEdges, user);
// ---------------------------------------------------------
print("\nAll edges containing the vertex 10. The counts and 'things' have been aggregated\n");
for (final Element e : results) {
print("GET_ELEMENTS_RESULT", e.toString());
}
// [transform] Create a description transient property using an element transformer
// ---------------------------------------------------------
final ElementTransformer descriptionTransformer = new ElementTransformer.Builder().select("SOURCE", "DESTINATION", "count").execute(new DescriptionTransform()).project("description").build();
// ---------------------------------------------------------
printJson("TRANSFORM", descriptionTransformer);
// [get] Add the element transformer to the view and run the query
// ---------------------------------------------------------
final GetElements getEdgesWithDescription = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().transientProperty("description", String.class).transformer(descriptionTransformer).build()).build()).build();
final CloseableIterable<? extends Element> resultsWithDescription = graph.execute(getEdgesWithDescription, user);
// ---------------------------------------------------------
printJsonAndPython("GET", getEdgesWithDescription);
print("\nWe can add a new property to the edges that is calculated from the aggregated values of other properties\n");
for (final Element e : resultsWithDescription) {
print("GET_ELEMENTS_WITH_DESCRIPTION_RESULT", e.toString());
}
// [get with no count]
// ---------------------------------------------------------
final GetElements getEdgesWithDescriptionAndNoCount = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().transientProperty("description", String.class).transformer(descriptionTransformer).excludeProperties("count").build()).build()).build();
final CloseableIterable<? extends Element> resultsWithDescriptionAndNoCount = graph.execute(getEdgesWithDescriptionAndNoCount, user);
// ---------------------------------------------------------
printJsonAndPython("GET_WITH_NO_COUNT", getEdgesWithDescriptionAndNoCount);
print("\nAnd the result without the count property:\n");
for (final Element e : resultsWithDescriptionAndNoCount) {
print("GET_ELEMENTS_WITH_DESCRIPTION_AND_NO_COUNT_RESULT", e.toString());
}
return resultsWithDescriptionAndNoCount;
}
Aggregations