Search in sources :

Example 1 with DescriptionTransform

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;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) RoadAndRoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) DescriptionTransform(uk.gov.gchq.gaffer.traffic.transform.DescriptionTransform)

Aggregations

ArrayList (java.util.ArrayList)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 RoadAndRoadUseElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)1 DescriptionTransform (uk.gov.gchq.gaffer.traffic.transform.DescriptionTransform)1 User (uk.gov.gchq.gaffer.user.User)1