Search in sources :

Example 11 with While

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);
}
Also used : GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) While(uk.gov.gchq.gaffer.operation.impl.While) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) LinkedHashMap(java.util.LinkedHashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 12 with While

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));
    }
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Store(uk.gov.gchq.gaffer.store.Store) Operation(uk.gov.gchq.gaffer.operation.Operation) While(uk.gov.gchq.gaffer.operation.impl.While) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Test(org.junit.jupiter.api.Test)

Example 13 with While

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;
}
Also used : Output(uk.gov.gchq.gaffer.operation.io.Output) While(uk.gov.gchq.gaffer.operation.impl.While) UnwrapEntityId(uk.gov.gchq.gaffer.data.element.function.UnwrapEntityId)

Example 14 with While

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;
}
Also used : While(uk.gov.gchq.gaffer.operation.impl.While)

Example 15 with While

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);
}
Also used : GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) While(uk.gov.gchq.gaffer.operation.impl.While) Test(org.junit.Test)

Aggregations

While (uk.gov.gchq.gaffer.operation.impl.While)15 Test (org.junit.jupiter.api.Test)12 Context (uk.gov.gchq.gaffer.store.Context)8 Store (uk.gov.gchq.gaffer.store.Store)8 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)7 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)7 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)6 Operation (uk.gov.gchq.gaffer.operation.Operation)4 Predicate (java.util.function.Predicate)3 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 LinkedHashMap (java.util.LinkedHashMap)2 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)2 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)1 UnwrapEntityId (uk.gov.gchq.gaffer.data.element.function.UnwrapEntityId)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1