Search in sources :

Example 1 with FirstItem

use of uk.gov.gchq.koryphe.impl.function.FirstItem in project Gaffer by gchq.

the class KeyFunctionMatchTest method shouldThrowExceptionFromFunctionIfInputIsInvalid.

@Test
public void shouldThrowExceptionFromFunctionIfInputIsInvalid() {
    // given
    // Performing a FirstItem on null should throw IllegalArgumentException
    List<Long> testList = Lists.newArrayList(100L, 200L, 300L, null);
    // when
    KeyFunctionMatch match = new KeyFunctionMatch.Builder().firstKeyFunction(new FunctionComposite(Lists.newArrayList(new CallMethod("getValue"), new ToInteger()))).secondKeyFunction(new FunctionComposite(Lists.newArrayList(new ToInteger(), new DivideBy(10), new FirstItem<>()))).build();
    // then
    try {
        match.init(testList);
    } catch (final IllegalArgumentException e) {
        // copied from docs of FirstItem
        assertEquals("Input cannot be null", e.getMessage());
    }
}
Also used : DivideBy(uk.gov.gchq.koryphe.impl.function.DivideBy) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Test(org.junit.jupiter.api.Test)

Example 2 with FirstItem

use of uk.gov.gchq.koryphe.impl.function.FirstItem in project Gaffer by gchq.

the class KeyFunctionMatchTest method shouldJsonSerialiseAndDeserialiseWithKeyFunctions.

@Test
public void shouldJsonSerialiseAndDeserialiseWithKeyFunctions() throws SerialisationException {
    // given
    KeyFunctionMatch match = new KeyFunctionMatch.Builder().firstKeyFunction(new FunctionComposite(Lists.newArrayList(new DivideBy(20), new FirstItem()))).secondKeyFunction(new ExtractProperty("count")).build();
    // when / then
    String expected = "{\n" + "  \"class\" : \"uk.gov.gchq.gaffer.store.operation.handler.join.match.KeyFunctionMatch\",\n" + "  \"firstKeyFunction\" : {\n" + "    \"class\" : \"uk.gov.gchq.koryphe.function.FunctionComposite\",\n" + "    \"functions\" : [ {\n" + "      \"class\" : \"uk.gov.gchq.koryphe.impl.function.DivideBy\",\n" + "      \"by\" : 20\n" + "    }, {\n" + "      \"class\" : \"uk.gov.gchq.koryphe.impl.function.FirstItem\"\n" + "    } ]\n" + "  },\n" + "  \"secondKeyFunction\" : {\n" + "    \"class\" : \"uk.gov.gchq.gaffer.data.element.function.ExtractProperty\",\n" + "    \"name\" : \"count\"\n" + "  }\n" + "}";
    assertEquals(expected, new String(JSONSerialiser.serialise(match, true)));
    assertEquals(match, JSONSerialiser.deserialise(expected, KeyFunctionMatch.class));
}
Also used : DivideBy(uk.gov.gchq.koryphe.impl.function.DivideBy) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) Test(org.junit.jupiter.api.Test)

Example 3 with FirstItem

use of uk.gov.gchq.koryphe.impl.function.FirstItem in project Gaffer by gchq.

the class KeyFunctionMatchTest method shouldMatchObjectsBasedOnKeyFunctions.

@Test
public void shouldMatchObjectsBasedOnKeyFunctions() {
    // given
    TypeSubTypeValue testValue = new TypeSubTypeValue("myType", "mySubType", "30");
    List<Long> testList = Lists.newArrayList(100L, 200L, 300L, 400L);
    // when
    KeyFunctionMatch match = new KeyFunctionMatch.Builder().firstKeyFunction(new FunctionComposite(Lists.newArrayList(new CallMethod("getValue"), new ToInteger()))).secondKeyFunction(new FunctionComposite(Lists.newArrayList(new ToInteger(), new DivideBy(10), new FirstItem<>()))).build();
    match.init(testList);
    // then
    List<Long> expected = Lists.newArrayList(300L);
    assertEquals(expected, match.matching(testValue));
}
Also used : DivideBy(uk.gov.gchq.koryphe.impl.function.DivideBy) TypeSubTypeValue(uk.gov.gchq.gaffer.types.TypeSubTypeValue) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Test(org.junit.jupiter.api.Test)

Example 4 with FirstItem

use of uk.gov.gchq.koryphe.impl.function.FirstItem in project Gaffer by gchq.

the class MapHandlerTest method shouldProcessWalksWithEdgeExtraction.

@Test
public void shouldProcessWalksWithEdgeExtraction() throws OperationException {
    // Given
    final Iterable<Walk> walks = Arrays.asList(walk, walk1);
    final Map<Iterable<Walk>, Iterable<Edge>> map = new Map.Builder<Iterable<Walk>>().input(walks).first(new IterableFunction.Builder<Walk>().first(new ExtractWalkEdgesFromHop(1)).then(new FirstItem<>()).build()).build();
    final ToVertices toVertices = new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.SOURCE).build();
    final ToSet<Object> toSet = new ToSet<>();
    final OperationChain<Set<?>> opChain = new OperationChain.Builder().first(map).then(toVertices).then(toSet).build();
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers);
    given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_BC, EDGE_BD));
    given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("B", "B"));
    given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("B", "B"));
    // When
    final Iterable<?> results = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertThat((Iterable<String>) results).contains("B");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Set(java.util.Set) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) ExtractWalkEdgesFromHop(uk.gov.gchq.gaffer.data.graph.function.walk.ExtractWalkEdgesFromHop) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 5 with FirstItem

use of uk.gov.gchq.koryphe.impl.function.FirstItem in project Gaffer by gchq.

the class MapHandlerTest method shouldProcessWalksInOperationChain.

@Test
public void shouldProcessWalksInOperationChain() throws OperationException {
    // Given
    final Iterable<Iterable<Set<Edge>>> walks = Arrays.asList(walk, walk1);
    final Map<Iterable<Iterable<Set<Edge>>>, Iterable<Edge>> map = new Map.Builder<Iterable<Iterable<Set<Edge>>>>().input(walks).first(new IterableFunction.Builder<Iterable<Set<Edge>>>().first(new FirstItem<>()).then(new FirstItem<>()).build()).build();
    final ToVertices toVertices = new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.SOURCE).build();
    final ToSet<Object> toSet = new ToSet<>();
    final OperationChain<Set<?>> opChain = new OperationChain.Builder().first(map).then(toVertices).then(toSet).build();
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers);
    given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_AB, EDGE_CB));
    given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("A", "C"));
    given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("A", "C"));
    // When
    final Iterable<?> results = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertThat((Iterable<String>) results).containsOnly("A", "C");
}
Also used : ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Set(java.util.Set) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) Edge(uk.gov.gchq.gaffer.data.element.Edge) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 FirstItem (uk.gov.gchq.koryphe.impl.function.FirstItem)5 FunctionComposite (uk.gov.gchq.koryphe.function.FunctionComposite)3 DivideBy (uk.gov.gchq.koryphe.impl.function.DivideBy)3 Set (java.util.Set)2 Map (uk.gov.gchq.gaffer.operation.impl.Map)2 ToSet (uk.gov.gchq.gaffer.operation.impl.output.ToSet)2 ToVertices (uk.gov.gchq.gaffer.operation.impl.output.ToVertices)2 OperationChainValidator (uk.gov.gchq.gaffer.store.operation.OperationChainValidator)2 OperationChainOptimiser (uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser)2 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)2 CallMethod (uk.gov.gchq.koryphe.impl.function.CallMethod)2 IterableFunction (uk.gov.gchq.koryphe.impl.function.IterableFunction)2 ToInteger (uk.gov.gchq.koryphe.impl.function.ToInteger)2 ToLong (uk.gov.gchq.koryphe.impl.function.ToLong)2 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)1 Walk (uk.gov.gchq.gaffer.data.graph.Walk)1 ExtractWalkEdgesFromHop (uk.gov.gchq.gaffer.data.graph.function.walk.ExtractWalkEdgesFromHop)1 TypeSubTypeValue (uk.gov.gchq.gaffer.types.TypeSubTypeValue)1