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;
}
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();
}
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);
}
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));
}
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));
}
Aggregations