use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.
the class While method updateOperations.
@Override
public void updateOperations(final Collection<Operation> operations) {
if (null == operations || 2 != operations.size()) {
throw new IllegalArgumentException("Unable to update operations - exactly 2 operations are required. Received " + (null != operations ? operations.size() : 0) + " operations.");
}
final Iterator<Operation> itr = operations.iterator();
final Operation transform = extractNextOp(itr);
if (null == conditional) {
if (null != transform) {
conditional = new Conditional();
conditional.setTransform(transform);
}
} else {
conditional.setTransform(transform);
}
operation = extractNextOp(itr);
}
use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.
the class IfScoreResolverTest method shouldGetScoreForNestedOperations.
@Test
public void shouldGetScoreForNestedOperations() {
// Given
final Map map = mock(Map.class);
final Conditional conditional = mock(Conditional.class);
given(conditional.getTransform()).willReturn(map);
final GetWalks getWalks = mock(GetWalks.class);
given(getWalks.getOperations()).willReturn(Collections.singletonList(new OperationChain<>(new GetAdjacentIds(), new GetAdjacentIds())));
final GetAllElements getAllElements = new GetAllElements();
final If operation = new If.Builder<>().conditional(conditional).then(getWalks).otherwise(getAllElements).build();
final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Operation.class, 1);
opScores.put(Map.class, 3);
opScores.put(GetAdjacentIds.class, 2);
opScores.put(GetAllElements.class, 3);
final IfScoreResolver resolver = new IfScoreResolver();
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(7, score);
}
use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.
the class IfScoreResolverTest method shouldGetScoreWithOperationChainAsAnOperation.
@Test
public void shouldGetScoreWithOperationChainAsAnOperation() {
// Given
final GetElements getElements = mock(GetElements.class);
final ToMap toMap = mock(ToMap.class);
final Map map = mock(Map.class);
final OperationChain conditionalChain = mock(OperationChain.class);
final List<Operation> conditionalOps = new LinkedList<>();
conditionalOps.add(getElements);
conditionalOps.add(toMap);
conditionalOps.add(map);
given(conditionalChain.getOperations()).willReturn(conditionalOps);
final Conditional conditional = mock(Conditional.class);
given(conditional.getTransform()).willReturn(conditionalChain);
final GetAdjacentIds getAdjacentIds = mock(GetAdjacentIds.class);
final GetAllElements getAllElements = mock(GetAllElements.class);
final If operation = new If.Builder<>().conditional(conditional).then(getAdjacentIds).otherwise(getAllElements).build();
final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Operation.class, 1);
opScores.put(GetElements.class, 2);
opScores.put(ToMap.class, 2);
opScores.put(Map.class, 3);
opScores.put(GetAdjacentIds.class, 3);
opScores.put(GetAllElements.class, 4);
final IfScoreResolver resolver = new IfScoreResolver();
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(11, score);
}
use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.
the class IfScoreResolverTest method shouldThrowErrorWhenNoDefaultResolverConfigured.
@Test
public void shouldThrowErrorWhenNoDefaultResolverConfigured() {
// Given
final IfScoreResolver resolver = new IfScoreResolver();
final If operation = new If.Builder<>().conditional(new Conditional()).then(new GetAllElements()).build();
// When / Then
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> resolver.getScore(operation)).withMessage("Default Score Resolver has not been provided.");
}
use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.
the class WhileScoreResolverTest method shouldGetScoreWithFullyPopulatedOperation.
@Test
public void shouldGetScoreWithFullyPopulatedOperation() {
// Given
final Object input = new EntitySeed(2);
final Count count = mock(Count.class);
final int repeats = 5;
final GetElements getElements = mock(GetElements.class);
final Conditional conditional = mock(Conditional.class);
given(conditional.getTransform()).willReturn(count);
final While operation = new While.Builder<>().input(input).conditional(conditional).maxRepeats(repeats).operation(getElements).build();
final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
opScores.put(Count.class, 1);
opScores.put(GetElements.class, 2);
final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
final WhileScoreResolver resolver = new WhileScoreResolver();
// When
final int score = resolver.getScore(operation, defaultResolver);
// Then
assertEquals(15, score);
}
Aggregations