Search in sources :

Example 1 with RoadAndRoadUseWithTimesElementGenerator

use of uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesElementGenerator in project gaffer-doc by gchq.

the class Aggregation method run.

@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
    // [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] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Get all RoadUse edges
    // ---------------------------------------------------------
    final GetAllElements getAllRoadUseEdges = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> roadUseElements = graph.execute(getAllRoadUseEdges, user);
    // ---------------------------------------------------------
    print("\nAll RoadUse edges in daily time buckets:");
    for (final Element element : roadUseElements) {
        print("GET_ALL_EDGES_RESULT", element.toString());
    }
    // [get all edges summarised] Get all edges summarised (merge all time windows together)
    // This is achieved by overriding the 'groupBy' start and end time properties.
    // ---------------------------------------------------------
    final GetAllElements edgesSummarisedOperation = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().groupBy().build()).build()).build();
    final CloseableIterable<? extends Element> edgesSummarised = graph.execute(edgesSummarisedOperation, user);
    // ---------------------------------------------------------
    print("\nAll edges summarised:");
    for (final Element edge : edgesSummarised) {
        print("GET_ALL_EDGES_SUMMARISED_RESULT", edge.toString());
    }
    // [get all edges summarised in time window] Get all edges summarised over a provided 2 day time period
    // This is achieved by overriding the 'groupBy' start and end time properties
    // and providing a filter.
    // ---------------------------------------------------------
    final GetAllElements edgesSummarisedInTimeWindowOperation = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate").execute(new IsMoreThan(MAY_01_2000, true)).select("endDate").execute(new IsLessThan(MAY_02_2000, false)).build()).groupBy().build()).build()).build();
    final CloseableIterable<? extends Element> edgesSummarisedInTimeWindow = graph.execute(edgesSummarisedInTimeWindowOperation, user);
    // ---------------------------------------------------------
    print("\nEdges in 2 day time window:");
    for (final Element edge : edgesSummarisedInTimeWindow) {
        print("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT", edge.toString());
    }
    // [get all edges summarised in time window with min count] Now a bit more advanced.
    // At query time you can actually override the logic for how Gaffer
    // aggregates properties together. So by default the count property
    // is aggregated with Sum. At query time we could change that, so the
    // count property was aggregated with Max.
    // ---------------------------------------------------------
    final GetAllElements edgesSummarisedInTimeWindowWithMinCountOperation = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate").execute(new IsMoreThan(MAY_01_2000, true)).select("endDate").execute(new IsLessThan(MAY_03_2000, false)).build()).groupBy().aggregator(new ElementAggregator.Builder().select("count").execute(new Min()).build()).build()).build()).build();
    printJson("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT_WITH_MIN_COUNT", edgesSummarisedInTimeWindowWithMinCountOperation);
    final CloseableIterable<? extends Element> edgesSummarisedInTimeWindowWithMinCount = graph.execute(edgesSummarisedInTimeWindowWithMinCountOperation, user);
    // ---------------------------------------------------------
    print("\nEdges in 3 day time window with min count:");
    for (final Element edge : edgesSummarisedInTimeWindowWithMinCount) {
        print("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT_WITH_MIN_COUNT", edge.toString());
    }
    return edgesSummarisedInTimeWindowWithMinCount;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) RoadAndRoadUseWithTimesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesElementGenerator) Element(uk.gov.gchq.gaffer.data.element.Element) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Graph(uk.gov.gchq.gaffer.graph.Graph) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) Min(uk.gov.gchq.koryphe.impl.binaryoperator.Min) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Aggregations

Element (uk.gov.gchq.gaffer.data.element.Element)1 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 RoadAndRoadUseWithTimesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesElementGenerator)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)1 User (uk.gov.gchq.gaffer.user.User)1 Min (uk.gov.gchq.koryphe.impl.binaryoperator.Min)1 IsLessThan (uk.gov.gchq.koryphe.impl.predicate.IsLessThan)1 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)1