Search in sources :

Example 1 with Conditional

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

Example 2 with Conditional

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);
}
Also used : GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) ToMap(uk.gov.gchq.gaffer.operation.impl.output.ToMap) LinkedHashMap(java.util.LinkedHashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 3 with Conditional

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

Example 4 with Conditional

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.");
}
Also used : GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 5 with Conditional

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

Aggregations

Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)30 Test (org.junit.jupiter.api.Test)18 If (uk.gov.gchq.gaffer.operation.impl.If)16 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)14 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)10 Test (org.junit.Test)9 Operation (uk.gov.gchq.gaffer.operation.Operation)9 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)9 GetWalks (uk.gov.gchq.gaffer.operation.impl.GetWalks)7 Map (uk.gov.gchq.gaffer.operation.impl.Map)6 While (uk.gov.gchq.gaffer.operation.impl.While)6 LinkedHashMap (java.util.LinkedHashMap)5 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)5 Context (uk.gov.gchq.gaffer.store.Context)5 ToList (uk.gov.gchq.koryphe.impl.function.ToList)5 IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)5 HashMap (java.util.HashMap)4 Walk (uk.gov.gchq.gaffer.data.graph.Walk)4 LinkedList (java.util.LinkedList)3 Predicate (java.util.function.Predicate)3