Search in sources :

Example 21 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.

the class AddOperationsToChainTest method shouldAddIfOperation.

@Test
public void shouldAddIfOperation() throws SerialisationException {
    // Given
    final GetWalks getWalks = new GetWalks();
    final uk.gov.gchq.gaffer.operation.impl.Map map = new uk.gov.gchq.gaffer.operation.impl.Map();
    final ToVertices toVertices = new ToVertices();
    final ToSet toSet = new ToSet();
    final Exists exists = new Exists();
    final Limit limit = new Limit();
    final GetAllElements getAllElements = new GetAllElements();
    final GetElements getElements = new GetElements();
    final Conditional conditional = new Conditional();
    conditional.setPredicate(exists);
    final If ifOp = new If.Builder<>().conditional(conditional).then(getElements).otherwise(getAllElements).build();
    final AddOperationsToChain hook = new AddOperationsToChain();
    final Map<String, List<Operation>> after = new HashMap<>();
    final List<Operation> afterOps = new LinkedList<>();
    afterOps.add(ifOp);
    afterOps.add(limit);
    after.put("uk.gov.gchq.gaffer.operation.impl.output.ToSet", afterOps);
    hook.setAfter(after);
    final OperationChain opChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).build();
    // When
    hook.preExecute(opChain, new Context());
    // Then
    final OperationChain expectedOpChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).then(ifOp).then(limit).build();
    JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Also used : HashMap(java.util.HashMap) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) LinkedList(java.util.LinkedList) List(java.util.List) Context(uk.gov.gchq.gaffer.store.Context) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) LinkedList(java.util.LinkedList) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) HashMap(java.util.HashMap) Map(java.util.Map) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 22 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional 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 23 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.

the class IfScoreResolverTest method shouldGetScoreWithFullyPopulatedOperation.

@Test
public void shouldGetScoreWithFullyPopulatedOperation() {
    // Given
    final Count count = mock(Count.class);
    final GetAllElements getAllElements = mock(GetAllElements.class);
    final GetWalks getWalks = mock(GetWalks.class);
    final Conditional conditional = mock(Conditional.class);
    given(conditional.getTransform()).willReturn(count);
    final If operation = new If.Builder<>().conditional(conditional).then(getWalks).otherwise(getAllElements).build();
    final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Count.class, 1);
    opScores.put(GetAllElements.class, 3);
    opScores.put(GetWalks.class, 4);
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
    final IfScoreResolver resolver = new IfScoreResolver();
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(4, score);
}
Also used : GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Count(uk.gov.gchq.gaffer.operation.impl.Count) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 24 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.

the class If method updateOperations.

@Override
public void updateOperations(final Collection<Operation> operations) {
    if (null == operations || 3 != operations.size()) {
        throw new IllegalArgumentException("Unable to update operations - exactly 3 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);
    }
    then = extractNextOp(itr);
    otherwise = extractNextOp(itr);
}
Also used : Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation)

Example 25 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional in project Gaffer by gchq.

the class GetWalksIT method shouldFilterWalksUsingWalkPredicateWithoutTransform.

@Test
public void shouldFilterWalksUsingWalkPredicateWithoutTransform() throws Exception {
    final Conditional conditional = new Conditional();
    conditional.setPredicate(new WalkPredicate());
    final Iterable<Walk> walks = executeGetWalksApplyingConditional(conditional);
    assertThat(getPaths(walks)).isEqualTo("AED");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Test(org.junit.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