Search in sources :

Example 1 with If

use of uk.gov.gchq.gaffer.operation.impl.If 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 2 with If

use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.

the class IfScoreResolverTest method shouldGetDefaultScoreWithNoOperationScores.

@Test
public void shouldGetDefaultScoreWithNoOperationScores() {
    // Given
    final IfScoreResolver resolver = new IfScoreResolver();
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(new LinkedHashMap<>());
    final If operation = new If();
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(2, score);
}
Also used : If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 3 with If

use of uk.gov.gchq.gaffer.operation.impl.If 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 If

use of uk.gov.gchq.gaffer.operation.impl.If 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 If

use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.

the class AddOperationsToChainTest method shouldHandleNestedOperationChain.

@Test
public void shouldHandleNestedOperationChain() throws SerialisationException {
    // Given
    AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH);
    Operation discardOutput = new DiscardOutput();
    Operation splitStore = new SplitStoreFromFile();
    Operation validate = new Validate();
    Operation getAdjacentIds = new GetAdjacentIds();
    Operation count = new Count<>();
    Operation countGroups = new CountGroups();
    Operation getElements = new GetElements();
    If ifOp = new If.Builder<>().conditional(new Conditional(new Exists(), new GetElements())).then(new GetElements()).otherwise(new GetAllElements()).build();
    Operation getAllElements = new GetAllElements();
    Operation limit = new Limit<>();
    final OperationChain opChain = new OperationChain.Builder().first(getAdjacentIds).then(new OperationChain.Builder().first(getElements).then(getAllElements).build()).then(ifOp).build();
    // When
    hook.preExecute(opChain, new Context(new User()));
    // Then
    final OperationChain expectedOpChain = new OperationChain.Builder().first(discardOutput).then(splitStore).then(validate).then(getAdjacentIds).then(count).then(discardOutput).then((Operation) new OperationChain.Builder().first(countGroups).then(getElements).then(getAllElements).then(limit).then(validate).build()).then(new If.Builder<>().conditional(new Conditional(new Exists(), new OperationChain<>(new CountGroups(), new GetElements()))).then(new OperationChain<>(new CountGroups(), new GetElements())).otherwise(new OperationChain<>(new GetAllElements(), new Limit<>(), new Validate())).build()).then(new Count()).build();
    JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) Count(uk.gov.gchq.gaffer.operation.impl.Count) SplitStoreFromFile(uk.gov.gchq.gaffer.operation.impl.SplitStoreFromFile) CountGroups(uk.gov.gchq.gaffer.operation.impl.CountGroups) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Aggregations

If (uk.gov.gchq.gaffer.operation.impl.If)20 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)16 Test (org.junit.jupiter.api.Test)14 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)10 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)10 Operation (uk.gov.gchq.gaffer.operation.Operation)7 GetWalks (uk.gov.gchq.gaffer.operation.impl.GetWalks)7 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)6 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)6 Test (org.junit.Test)5 ToList (uk.gov.gchq.koryphe.impl.function.ToList)5 IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)5 LinkedHashMap (java.util.LinkedHashMap)4 Count (uk.gov.gchq.gaffer.operation.impl.Count)4 CountGroups (uk.gov.gchq.gaffer.operation.impl.CountGroups)3 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)3 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)3 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)3 Context (uk.gov.gchq.gaffer.store.Context)3 User (uk.gov.gchq.gaffer.user.User)3