Search in sources :

Example 1 with RoadAndRoadUseElementGenerator

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

the class MultipleEdges 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 related to vertex 10
    // ---------------------------------------------------------
    final GetElements getElements = new GetElements.Builder().input(new EntitySeed("10")).build();
    final CloseableIterable<? extends Element> results = graph.execute(getElements, user);
    // ---------------------------------------------------------
    print("\nAll elements containing vertex 10");
    print("\nNotice that the edges are aggregated within their groups");
    for (final Element e : results) {
        print("GET_ELEMENTS_RESULT", e.toString());
    }
    // [get] Rerun the previous query with a View to specify which subset of results we want
    // ---------------------------------------------------------
    final View view = new View.Builder().edge("RoadHasJunction").build();
    final GetElements getRoadHasJunctionEdges = new GetElements.Builder().input(new EntitySeed("10")).view(view).build();
    final CloseableIterable<? extends Element> filteredResults = graph.execute(getRoadHasJunctionEdges, user);
    // ---------------------------------------------------------
    print("\nAll RoadHasJunction edges containing vertex 10\n");
    for (final Element e : filteredResults) {
        print("GET_ROAD_HAS_JUNCTION_EDGES_RESULT", e.toString());
    }
    return filteredResults;
}
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) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 2 with RoadAndRoadUseElementGenerator

use of uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator 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)

Example 3 with RoadAndRoadUseElementGenerator

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

the class OperationChains method run.

@Override
public Iterable<? extends String> 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 RoadAndRoadUseElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Create and execute an operation chain consisting of 2 operations:
    // GetAdjacentIds - starting at vertex M5 get all adjacent vertices (vertices at other end of outbound edges)
    // GetElements - get outbound edges
    // ---------------------------------------------------------
    final OperationChain<? extends Iterable<? extends Element>> opChain = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("M5")).inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GetElements.Builder().inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).build();
    final Iterable<? extends Element> results = graph.execute(opChain, user);
    // ---------------------------------------------------------
    print("\n2nd hop edges\n");
    for (final Element result : results) {
        print("RESULT", result.toString());
    }
    // [get and convert] Create and execute an operation chain consisting of 3 operations:
    // GetAdjacentIds - starting at vertex 1 get all adjacent vertices (vertices at other end of outbound edges)
    // GetElements - get outbound edges
    // GenerateObjects - convert the edges into csv
    // ---------------------------------------------------------
    final OperationChain<Iterable<? extends String>> opChainWithCSV = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("M5")).inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GetElements.Builder().inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GenerateObjects.Builder<String>().generator(new RoadUseCsvGenerator()).build()).build();
    final Iterable<? extends String> csvResults = graph.execute(opChainWithCSV, user);
    // ---------------------------------------------------------
    print("\n2nd hop edges converted back into comma separated strings.\n");
    for (final String result : csvResults) {
        print("CSV_RESULT", result);
    }
    return csvResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) RoadAndRoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) RoadUseCsvGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadUseCsvGenerator) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 4 with RoadAndRoadUseElementGenerator

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

the class Filtering 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.");
    print("\nRoadUse edges related to vertex 10. The counts have been aggregated\n");
    // [get simple] get all the edges that contain the vertex "10"
    // ---------------------------------------------------------
    final GetElements getElements = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> results = graph.execute(getElements, user);
    // ---------------------------------------------------------
    printJsonAndPython("GET_SIMPLE", getElements);
    for (final Element e : results) {
        print("GET_ELEMENTS_RESULT", e.toString());
    }
    // [get] rerun previous query with a filter to return only edges with a count more than 2
    // ---------------------------------------------------------
    final GetElements getEdgesWithCountMoreThan2 = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(2L)).build()).build()).build()).build();
    final CloseableIterable<? extends Element> filteredResults = graph.execute(getEdgesWithCountMoreThan2, user);
    // ---------------------------------------------------------
    printJsonAndPython("GET", getEdgesWithCountMoreThan2);
    print("\nAll edges containing the vertex 10 with an aggregated count more than than 2\n");
    for (final Element e : filteredResults) {
        print("GET_ELEMENTS_WITH_COUNT_MORE_THAN_2_RESULT", e.toString());
    }
    return filteredResults;
}
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) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Graph(uk.gov.gchq.gaffer.graph.Graph) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Aggregations

Element (uk.gov.gchq.gaffer.data.element.Element)4 RoadAndRoadUseElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator)4 Graph (uk.gov.gchq.gaffer.graph.Graph)4 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)4 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 User (uk.gov.gchq.gaffer.user.User)4 ArrayList (java.util.ArrayList)3 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 RoadUseCsvGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadUseCsvGenerator)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)1 DescriptionTransform (uk.gov.gchq.gaffer.traffic.transform.DescriptionTransform)1 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)1