Search in sources :

Example 1 with ToEntitySeeds

use of uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds in project gaffer-doc by gchq.

the class Subgraphs method run.

@Override
public Iterable<? 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 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Create a sub graph
    // Start getting related edges with the given seeds.
    // Then update the export with the results
    // Between each hop we need to extract the destination vertices of the
    // previous edges and convert them to EntitySeeds.
    // Finally finish off by returning all the edges in the export.
    // ---------------------------------------------------------
    final OperationChain<Iterable<?>> opChain = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("M5")).inOutType(IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new ExportToSet<>()).then(new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.DESTINATION).build()).then(new ToEntitySeeds()).then(new GetElements.Builder().inOutType(IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(1L)).build()).build()).build()).build()).then(new ExportToSet<>()).then(new DiscardOutput()).then(new GetSetExport()).build();
    // ---------------------------------------------------------
    final Iterable<? extends Element> subGraph = (Iterable<? extends Element>) graph.execute(opChain, user);
    print("\nSub graph:");
    for (final Element edge : subGraph) {
        print("SUB_GRAPH", edge.toString());
    }
    return subGraph;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) ExportToSet(uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet) ToEntitySeeds(uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) GetSetExport(uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Example 2 with ToEntitySeeds

use of uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds in project Gaffer by gchq.

the class ToEntitySeedsHandlerTest method shouldHandleNullInput.

@Test
public void shouldHandleNullInput() throws OperationException {
    // Given
    final ToEntitySeedsHandler handler = new ToEntitySeedsHandler();
    final ToEntitySeeds operation = mock(ToEntitySeeds.class);
    given(operation.getInput()).willReturn(null);
    // When
    final Iterable<EntitySeed> results = handler.doOperation(operation, new Context(), null);
    // Then
    assertThat(results).isNull();
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) ToEntitySeeds(uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Test(org.junit.jupiter.api.Test)

Example 3 with ToEntitySeeds

use of uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds in project Gaffer by gchq.

the class ToEntitySeedsHandlerTest method shouldBeAbleToIterableOverTheResultsMultipleTimes.

@Test
public void shouldBeAbleToIterableOverTheResultsMultipleTimes() throws OperationException {
    // Given
    final Object vertex1 = "vertex1";
    final Object vertex2 = "vertex2";
    final Iterable originalResults = new WrappedCloseableIterable<>(Arrays.asList(vertex1, vertex2));
    final ToEntitySeedsHandler handler = new ToEntitySeedsHandler();
    final ToEntitySeeds operation = mock(ToEntitySeeds.class);
    given(operation.getInput()).willReturn(originalResults);
    // When
    final Iterable<EntitySeed> results = handler.doOperation(operation, new Context(), null);
    // Then
    final Set<Object> set1 = Sets.newHashSet(results);
    final Set<Object> set2 = Sets.newHashSet(results);
    assertEquals(Sets.newHashSet(new EntitySeed(vertex1), new EntitySeed(vertex2)), set1);
    assertEquals(set1, set2);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) ToEntitySeeds(uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Test(org.junit.jupiter.api.Test)

Example 4 with ToEntitySeeds

use of uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds in project Gaffer by gchq.

the class ToEntitySeedsHandlerTest method shouldConvertVerticesToEntitySeeds.

@Test
public void shouldConvertVerticesToEntitySeeds() throws OperationException {
    // Given
    final Object vertex1 = "vertex1";
    final Object vertex2 = "vertex2";
    final Iterable originalResults = new WrappedCloseableIterable<>(Arrays.asList(vertex1, vertex2));
    final ToEntitySeedsHandler handler = new ToEntitySeedsHandler();
    final ToEntitySeeds operation = mock(ToEntitySeeds.class);
    given(operation.getInput()).willReturn(originalResults);
    // When
    final Iterable<EntitySeed> results = handler.doOperation(operation, new Context(), null);
    // Then
    assertThat(results).containsOnly(new EntitySeed(vertex1), new EntitySeed(vertex2));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) ToEntitySeeds(uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Test(org.junit.jupiter.api.Test)

Example 5 with ToEntitySeeds

use of uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds in project Gaffer by gchq.

the class ForEachIT method shouldExecuteForEachOperationOnGetElementsWithValidResults.

@Test
public void shouldExecuteForEachOperationOnGetElementsWithValidResults() throws OperationException {
    // Given
    final ForEach<String, Iterable<String>> op = new ForEach.Builder<String, Iterable<String>>().input(Collections.singletonList(SOURCE_DIR_1)).operation(new OperationChain.Builder().first(new ToSingletonList<>()).then(new ToEntitySeeds()).then(new GetElements()).then(new ToList<Element>()).then(new ToCsv.Builder().includeHeader(false).generator(new CsvGenerator.Builder().vertex("vertex").destination("dest").build()).build()).build()).build();
    // When
    final List<Iterable<String>> results = Lists.newArrayList(graph.execute(op, getUser()));
    // Then
    assertThat(results).hasSize(1);
    assertThat(Sets.newHashSet(results.get(0))).isEqualTo(Sets.newHashSet(SOURCE_DIR_1 + ",", "," + DEST_DIR_1));
}
Also used : ToEntitySeeds(uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) CsvGenerator(uk.gov.gchq.gaffer.data.generator.CsvGenerator) ForEach(uk.gov.gchq.gaffer.operation.impl.ForEach) ToCsv(uk.gov.gchq.gaffer.operation.impl.output.ToCsv) ToSingletonList(uk.gov.gchq.gaffer.operation.impl.output.ToSingletonList) Test(org.junit.Test)

Aggregations

ToEntitySeeds (uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds)5 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)4 Test (org.junit.jupiter.api.Test)3 Context (uk.gov.gchq.gaffer.store.Context)3 WrappedCloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)2 Test (org.junit.Test)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 CsvGenerator (uk.gov.gchq.gaffer.data.generator.CsvGenerator)1 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)1 ForEach (uk.gov.gchq.gaffer.operation.impl.ForEach)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 ExportToSet (uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet)1 GetSetExport (uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport)1 ToCsv (uk.gov.gchq.gaffer.operation.impl.output.ToCsv)1