use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.
the class WhileScoreResolverTest method shouldGetScoreWithOperationChainAsOperation.
@Test
public void shouldGetScoreWithOperationChainAsOperation() {
// Given
final Object input = new EntitySeed(3);
final int repeats = 3;
final GetElements getElements = mock(GetElements.class);
final Map map = mock(Map.class);
final ToSet toSet = mock(ToSet.class);
final OperationChain transformChain = mock(OperationChain.class);
final List<Operation> transformOps = new LinkedList<>();
transformOps.add(map);
transformOps.add(toSet);
given(transformChain.getOperations()).willReturn(transformOps);
final Conditional conditional = mock(Conditional.class);
given(conditional.getTransform()).willReturn(transformChain);
final While operation = new While.Builder<>().input(input).maxRepeats(repeats).conditional(conditional).operation(getElements).build();
final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Operation.class, 1);
opScores.put(Map.class, 3);
opScores.put(ToSet.class, 1);
opScores.put(GetElements.class, 2);
final WhileScoreResolver resolver = new WhileScoreResolver();
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(18, score);
}
use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.
the class WhileHandlerTest method shouldThrowExceptionWhenMaxConfiguredNumberOfRepeatsExceeded.
@Test
public void shouldThrowExceptionWhenMaxConfiguredNumberOfRepeatsExceeded() throws OperationException {
// Given
final EntitySeed input = mock(EntitySeed.class);
final int maxRepeats = 2500;
final Operation delegate = mock(GetElements.class);
final Context context = mock(Context.class);
final Store store = mock(Store.class);
final While operation = new While.Builder<>().input(input).maxRepeats(maxRepeats).operation(delegate).build();
final WhileHandler handler = new WhileHandler();
// When / Then
try {
handler.doOperation(operation, context, store);
fail("Exception expected");
} catch (final OperationException e) {
assertTrue(e.getMessage().contains("Max repeats of the While operation is too large: " + maxRepeats + " > " + While.MAX_REPEATS));
}
}
use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.
the class GetWalksHandler method executeWhileOperation.
private List<?> executeWhileOperation(final OperationChain<Iterable<Element>> operation, final List<?> seeds, final Integer resultLimit, final Context context, final Store store, final int hops, final AdjacencyMaps adjacencyMaps, final EntityMaps entityMaps) throws OperationException {
List<?> resultSeeds = seeds;
final While whileOp = (While) operation.getOperations().get(0);
if (null != whileOp.getOperation()) {
validateWhileOperation(whileOp);
final OperationHandler opHandler = store.getOperationHandler(While.class);
if (null == opHandler) {
throw new UnsupportedOperationException("While operations are not supported by this store");
}
if (!(opHandler instanceof WhileHandler)) {
throw new UnsupportedOperationException("To use While operations within GetWalks, the While handler must be a " + WhileHandler.class.getName());
}
final WhileHandler whileHandler = (WhileHandler) opHandler;
whileHandler.validateMaxRepeats(whileOp);
// Change the transform to unwrap entity ids first.
if (null != whileOp.getConditional() && null != whileOp.getConditional().getTransform()) {
whileOp.getConditional().setTransform(new OperationChain<>(new Map.Builder<>().first(new IterableFunction.Builder<>().first(new UnwrapEntityId()).build()).build(), whileOp.getConditional().getTransform()));
}
for (int repeatCount = 0; repeatCount < whileOp.getMaxRepeats(); repeatCount++) {
final While whileOpClone = whileOp.shallowClone();
if (!whileHandler.isSatisfied(resultSeeds, whileOpClone, context, store)) {
break;
}
resultSeeds = executeOperation((Output) whileOpClone.getOperation(), resultSeeds, resultLimit, context, store, hops, adjacencyMaps, entityMaps);
}
}
return resultSeeds;
}
use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.
the class WhileHandler method doOperation.
@Override
public Object doOperation(final While operation, final Context context, final Store store) throws OperationException {
validateMaxRepeats(operation);
Object input = operation.getInput();
for (int repeatCount = 0; repeatCount < operation.getMaxRepeats(); repeatCount++) {
final While operationClone = operation.shallowClone();
if (!isSatisfied(input, operationClone, context, store)) {
break;
}
input = doDelegateOperation(input, operationClone.getOperation(), context, store);
}
return input;
}
use of uk.gov.gchq.gaffer.operation.impl.While in project Gaffer by gchq.
the class WhileIT method shouldRepeatedlyAddElements.
@Test
public void shouldRepeatedlyAddElements() throws OperationException {
// Given
final While operation = new While.Builder<>().operation(new AddElements.Builder().input(new Entity.Builder().group(TestGroups.ENTITY).vertex("1").property(TestPropertyNames.COUNT, 2L).property(TestPropertyNames.INT, 2).property(TestPropertyNames.SET, CollectionUtil.treeSet("")).build()).build()).condition(true).maxRepeats(5).build();
// When
graph.execute(operation, getUser());
final List<? extends Element> results = Lists.newArrayList(graph.execute(new GetElements.Builder().input("1").view(new View.Builder().entity(TestGroups.ENTITY).build()).build(), getUser()));
assertThat(results).hasSize(1);
assertThat(results.get(0).getProperty(TestPropertyNames.COUNT)).isEqualTo(10L);
}
Aggregations