Search in sources :

Example 1 with FunctionComposite

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

the class KeyFunctionMatchTest method shouldMatchElementsOfDifferentGroupsBasedOnKeyFunctions.

@Test
public void shouldMatchElementsOfDifferentGroupsBasedOnKeyFunctions() {
    // given
    Entity testItem = new Entity.Builder().group(TEST_ENTITY_GROUP).vertex("test").property(PROP_1, 2L).build();
    List<Entity> testList = Lists.newArrayList(new Entity.Builder().group(TEST_ENTITY_GROUP_2).vertex("test2").property(PROP_2, 2).build(), new Entity.Builder().group(TEST_ENTITY_GROUP).vertex("test3").property(PROP_1, 3L).build(), new Entity.Builder().group(TEST_ENTITY_GROUP).vertex("test1").property(PROP_1, 4L).build(), new Entity.Builder().group(TEST_ENTITY_GROUP_2).vertex("test3").property(PROP_2, 2).build());
    // when
    KeyFunctionMatch match = new KeyFunctionMatch.Builder().firstKeyFunction(new ExtractProperty(PROP_1)).secondKeyFunction(new FunctionComposite(Lists.newArrayList(new ExtractProperty(PROP_2), new ToLong()))).build();
    match.init(testList);
    // then
    ArrayList<Entity> expected = Lists.newArrayList(new Entity.Builder().group(TEST_ENTITY_GROUP_2).vertex("test2").property(PROP_2, 2).build(), new Entity.Builder().group(TEST_ENTITY_GROUP_2).vertex("test3").property(PROP_2, 2).build());
    assertEquals(expected, match.matching(testItem));
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) Entity(uk.gov.gchq.gaffer.data.element.Entity) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) Test(org.junit.jupiter.api.Test)

Example 2 with FunctionComposite

use of uk.gov.gchq.koryphe.function.FunctionComposite 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 3 with FunctionComposite

use of uk.gov.gchq.koryphe.function.FunctionComposite 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 4 with FunctionComposite

use of uk.gov.gchq.koryphe.function.FunctionComposite 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 5 with FunctionComposite

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

the class TypeValueToTupleTest method shouldGetAndSetUsingCompositeFunction.

@Test
public void shouldGetAndSetUsingCompositeFunction() {
    // Given
    final TypeValue typeValue = new TypeValue("type", "value");
    final Function<Object, Object> compositeFunction = new FunctionComposite(Lists.newArrayList(new TypeValueToTuple(), new TupleAdaptedFunctionComposite.Builder().select(new String[] { "value" }).execute(new FunctionComposite(Arrays.asList(new Length(), new ToString()))).project(new String[] { "type" }).build()));
    // When
    compositeFunction.apply(typeValue);
    // Then
    assertEquals(new TypeValue("5", "value"), typeValue);
}
Also used : Length(uk.gov.gchq.koryphe.impl.function.Length) TypeValue(uk.gov.gchq.gaffer.types.TypeValue) ToString(uk.gov.gchq.koryphe.impl.function.ToString) ToString(uk.gov.gchq.koryphe.impl.function.ToString) TupleAdaptedFunctionComposite(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunctionComposite) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) FunctionTest(uk.gov.gchq.koryphe.function.FunctionTest) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 FunctionComposite (uk.gov.gchq.koryphe.function.FunctionComposite)6 DivideBy (uk.gov.gchq.koryphe.impl.function.DivideBy)3 FirstItem (uk.gov.gchq.koryphe.impl.function.FirstItem)3 ToLong (uk.gov.gchq.koryphe.impl.function.ToLong)3 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)2 TypeSubTypeValue (uk.gov.gchq.gaffer.types.TypeSubTypeValue)2 FunctionTest (uk.gov.gchq.koryphe.function.FunctionTest)2 CallMethod (uk.gov.gchq.koryphe.impl.function.CallMethod)2 Length (uk.gov.gchq.koryphe.impl.function.Length)2 ToInteger (uk.gov.gchq.koryphe.impl.function.ToInteger)2 ToString (uk.gov.gchq.koryphe.impl.function.ToString)2 TupleAdaptedFunctionComposite (uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunctionComposite)2 Entity (uk.gov.gchq.gaffer.data.element.Entity)1 TypeValue (uk.gov.gchq.gaffer.types.TypeValue)1