use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.
the class GetWalksHandler method doOperation.
@Override
public Iterable<Walk> doOperation(final GetWalks getWalks, final Context context, final Store store) throws OperationException {
// Check input
if (null == getWalks.getInput()) {
return null;
}
// Check there are some operations
if (null == getWalks.getOperations()) {
return new EmptyClosableIterable<>();
}
final Integer resultLimit = getWalks.getResultsLimit();
final int hops = getWalks.getNumberOfGetEdgeOperations();
final LimitedCloseableIterable limitedInputItr = new LimitedCloseableIterable<>(getWalks.getInput(), 0, resultLimit, false);
final List<EntityId> originalInput = Lists.newArrayList(limitedInputItr);
// Check hops and maxHops (if set)
if (hops == 0) {
return new EmptyClosableIterable<>();
} else if (maxHops != null && hops > maxHops) {
throw new OperationException("GetWalks operation contains " + hops + " hops. The maximum number of hops is: " + maxHops);
}
final AdjacencyMaps adjacencyMaps = prune && !getWalks.isIncludePartial() ? new PrunedAdjacencyMaps() : new SimpleAdjacencyMaps();
final EntityMaps entityMaps = new SimpleEntityMaps();
List<?> seeds = originalInput;
// Execute the operations
for (final OperationChain<Iterable<Element>> operation : getWalks.getOperations()) {
if (isWhileOperation(operation)) {
seeds = executeWhileOperation(operation, seeds, resultLimit, context, store, hops, adjacencyMaps, entityMaps);
} else {
seeds = executeOperation(operation, seeds, resultLimit, context, store, hops, adjacencyMaps, entityMaps);
}
}
// requested by the user.
if (entityMaps.size() == adjacencyMaps.size()) {
entityMaps.add(new EntityMap());
}
final GraphWindow graphWindow = new GraphWindow(adjacencyMaps, entityMaps);
// Track/recombine the edge objects and convert to return type
final Stream<Walk> walks = Streams.toStream(originalInput).flatMap(seed -> walk(seed.getVertex(), null, graphWindow, new LinkedList<>(), new LinkedList<>(), hops, getWalks.isIncludePartial()).stream());
return applyConditionalFiltering(walks, getWalks, context, store);
}
use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.
the class GetWalksHandlerTest method shouldHandleNullInput.
@Test
public void shouldHandleNullInput() throws Exception {
// Given
final GetElements getElements = new GetElements.Builder().view(new View.Builder().edge(TestGroups.EDGE).build()).build();
final GetWalks operation = new GetWalks.Builder().operations(getElements).build();
final GetWalksHandler handler = new GetWalksHandler();
// When
final Iterable<Walk> result = handler.doOperation(operation, null, null);
// Then
assertThat(result).isNull();
}
use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.
the class ExtractWalkEntitiesTest method shouldReturnEntitiesFromWalkObject.
@Test
public void shouldReturnEntitiesFromWalkObject() {
// Given
final Function<Walk, Iterable<Set<Entity>>> function = new ExtractWalkEntities();
final Walk walk = new Walk.Builder().entity(ENTITY_A).edge(EDGE_AB).entity(ENTITY_B).edge(EDGE_BC).entity(ENTITY_C).edge(EDGE_CA).entity(ENTITY_A).build();
// When
final Iterable<Set<Entity>> results = function.apply(walk);
// Then
assertThat(results).containsOnly(Sets.newHashSet(ENTITY_A), Sets.newHashSet(ENTITY_B), Sets.newHashSet(ENTITY_C), Sets.newHashSet(ENTITY_A));
}
use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.
the class GetWalksIT method shouldGetPathsWithEntities.
@Test
public void shouldGetPathsWithEntities() throws Exception {
// Given
final GetElements getEntities = new GetElements.Builder().directedType(DirectedType.DIRECTED).view(new View.Builder().entity(TestGroups.ENTITY).build()).build();
final GetElements getElements = new GetElements.Builder().directedType(DirectedType.DIRECTED).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().entity(TestGroups.ENTITY).edge(TestGroups.EDGE).build()).build();
final GetWalks op = new GetWalks.Builder().input(seedA).operations(getElements, getElements, getEntities).build();
// When
final List<Walk> results = Lists.newArrayList(graph.execute(op, getUser()));
// Then
assertThat(getPaths(results)).isEqualTo("AED,ABC");
results.forEach(r -> r.getEntities().forEach(l -> {
assertThat(l).isNotEmpty();
}));
}
use of uk.gov.gchq.gaffer.data.graph.Walk in project Gaffer by gchq.
the class GetWalksIT method shouldGetPathsWithMultipleEdgeTypes.
@Test
public void shouldGetPathsWithMultipleEdgeTypes() throws Exception {
// Given
final GetElements operation = new GetElements.Builder().directedType(DirectedType.DIRECTED).view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().properties(TestPropertyNames.COUNT).build()).edge(TestGroups.EDGE_2, new ViewElementDefinition.Builder().properties(TestPropertyNames.COUNT).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build();
final GetWalks op = new GetWalks.Builder().input(seedA).operations(operation, operation).build();
// When
final Iterable<Walk> results = graph.execute(op, getUser());
// Then
assertThat(getPaths(results)).isEqualTo("AED,AEF,ABC");
}
Aggregations