Search in sources :

Example 1 with ToLong

use of uk.gov.gchq.koryphe.impl.function.ToLong 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 ToLong

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

the class GetAllElementsIT method shouldAllowBiFunctionInView.

@Test
@TraitRequirement({ StoreTrait.TRANSFORMATION })
public void shouldAllowBiFunctionInView() throws OperationException {
    final Map<String, Class<?>> transientProperties = new HashMap<>();
    transientProperties.put("propLong", Long.class);
    transientProperties.put("combined", Long.class);
    final List<TupleAdaptedFunction<String, ?, ?>> transformFunctions = new ArrayList<>();
    final TupleAdaptedFunction<String, Integer, Long> convertToLong = new TupleAdaptedFunction<>();
    convertToLong.setSelection(new String[] { TestPropertyNames.INT });
    convertToLong.setFunction((Function) new ToLong());
    convertToLong.setProjection(new String[] { "propLong" });
    final TupleAdaptedFunction<String, Integer, Long> sum = new TupleAdaptedFunction<>();
    sum.setSelection(new String[] { "propLong", TestPropertyNames.COUNT });
    sum.setFunction(new ApplyBiFunction(new Sum()));
    sum.setProjection(new String[] { "combined" });
    transformFunctions.add(convertToLong);
    transformFunctions.add(sum);
    final GetAllElements get = new GetAllElements.Builder().view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().transientProperties(transientProperties).addTransformFunctions(transformFunctions).build()).build()).build();
    final CloseableIterable<? extends Element> results = graph.execute(get, user);
    for (final Element result : results) {
        final Long expectedResult = (Long) result.getProperty("propLong") + (Long) result.getProperty(TestPropertyNames.COUNT);
        final Long combined = (Long) result.getProperty("combined");
        assertThat(combined).isEqualTo(expectedResult);
    }
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) HashMap(java.util.HashMap) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) ApplyBiFunction(uk.gov.gchq.koryphe.impl.function.ApplyBiFunction) TupleAdaptedFunction(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) Test(org.junit.Test) TraitRequirement(uk.gov.gchq.gaffer.integration.TraitRequirement)

Example 3 with ToLong

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

the class RoadTrafficCsvElementGenerator2Test method shouldParseSampleDataWithGenericFunctions.

@Test
public void shouldParseSampleDataWithGenericFunctions() throws IOException {
    // Given
    CsvLinesToMaps parseCsv = new CsvLinesToMaps().header("Region Name (GO)", "ONS LACode", "ONS LA Name", "CP", "S Ref E", "S Ref N", "Road", "A-Junction", "A Ref E", "A Ref N", "B-Junction", "B Ref E", "B Ref N", "RCat", "iDir", "Year", "dCount", "Hour", "PC", "2WMV", "CAR", "BUS", "LGV", "HGVR2", "HGVR3", "HGVR4", "HGVA3", "HGVA5", "HGVA6", "HGV", "AMV").firstRow(1);
    IterableFunction<Map<String, Object>, Tuple<String>> toTuples = new IterableFunction<>(new MapToTuple<String>());
    IterableFunction<Tuple<String>, Tuple<String>> transformTuples = new IterableFunction(new FunctionChain.Builder<>().execute(new String[] { "Road", "A-Junction" }, new Concat(":"), new String[] { "A-Junction" }).execute(new String[] { "Road", "B-Junction" }, new Concat(":"), new String[] { "B-Junction" }).execute(new String[] { "A Ref E", "A Ref N" }, new Concat(), new String[] { "A-Location" }).execute(new String[] { "B Ref E", "B Ref N" }, new Concat(), new String[] { "B-Location" }).execute(new String[] { "THIS" }, new CreateRoadTrafficFreqMap(), new String[] { "countByVehicleType" }).execute(new String[] { "countByVehicleType" }, new CallMethod("getTotal"), new String[] { "total-count" }).execute(new String[] { "dCount" }, new ParseTime().timeZone("UTC"), new String[] { "timestamp" }).execute(new String[] { "Hour" }, new FunctionChain(new ToInteger(), new MultiplyBy(60 * 60 * 1000), new ToLong()), new String[] { "hoursInMilliseconds" }).execute(new String[] { "timestamp", "hoursInMilliseconds" }, new FunctionChain(new ApplyBiFunction<>(new Sum()), new ToString(), new ParseDate()), new String[] { "startDate" }).execute(new String[] { "startDate" }, new DateToTimeBucketEnd(TimeBucket.HOUR), new String[] { "endDate" }).build());
    TuplesToElements toElements = new TuplesToElements().element(new ElementTupleDefinition("RegionContainsLocation").source("Region Name (GO)").destination("ONS LA Name")).element(new ElementTupleDefinition("LocationContainsRoad").source("ONS LA Name").destination("Road")).element(new ElementTupleDefinition("RoadHasJunction").source("Road").destination("A-Junction")).element(new ElementTupleDefinition("RoadHasJunction").source("Road").destination("B-Junction")).element(new ElementTupleDefinition("JunctionLocatedAt").source("A-Junction").destination("A-Location")).element(new ElementTupleDefinition("JunctionLocatedAt").source("B-Junction").destination("B-Location")).element(new ElementTupleDefinition("RoadUse").source("A-Junction").destination("B-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count")).element(new ElementTupleDefinition("JunctionUse").vertex("A-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count")).element(new ElementTupleDefinition("JunctionUse").vertex("B-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count"));
    HyperLogLogPlusEntityGenerator addCardinalities = new HyperLogLogPlusEntityGenerator().countProperty("count").edgeGroupProperty("edgeGroup").cardinalityPropertyName("hllp");
    // Apply functions
    final FunctionChain<List<String>, Iterable<Element>> generator2 = new FunctionChain.Builder<List<String>, Iterable<Element>>().execute(parseCsv).execute(toTuples).execute(transformTuples).execute(toElements).execute(addCardinalities).build();
    // Uncomment the following for debugging
    // System.out.println(new String(JSONSerialiser.serialise(generator2, true)));
    final List<String> lines = IOUtils.readLines(createInputStream());
    final List<Element> elements2 = Lists.newArrayList(generator2.apply(lines));
    // Then - the results should be the same as those generated using the original element generator
    final RoadTrafficStringElementGenerator generator1 = new RoadTrafficStringElementGenerator();
    final List<Element> elements1;
    try (final InputStream inputStream = createInputStream()) {
        elements1 = Lists.newArrayList(generator1.apply(() -> new LineIterator(new InputStreamReader(inputStream))));
    }
    JSONSerialiser.getMapper();
    SimpleClassNameCache.setUseFullNameForSerialisation(false);
    elements1.forEach(e -> e.removeProperty("hllp"));
    elements2.forEach(e -> e.removeProperty("hllp"));
    ElementUtil.assertElementEquals(elements1, elements2);
}
Also used : MultiplyBy(uk.gov.gchq.koryphe.impl.function.MultiplyBy) DateToTimeBucketEnd(uk.gov.gchq.gaffer.time.function.DateToTimeBucketEnd) Element(uk.gov.gchq.gaffer.data.element.Element) ParseDate(uk.gov.gchq.koryphe.impl.function.ParseDate) ToString(uk.gov.gchq.koryphe.impl.function.ToString) LineIterator(org.apache.commons.io.LineIterator) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Concat(uk.gov.gchq.koryphe.impl.function.Concat) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) CsvLinesToMaps(uk.gov.gchq.koryphe.impl.function.CsvLinesToMaps) List(java.util.List) TuplesToElements(uk.gov.gchq.gaffer.data.element.function.TuplesToElements) FunctionChain(uk.gov.gchq.koryphe.impl.function.FunctionChain) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) HyperLogLogPlusEntityGenerator(uk.gov.gchq.gaffer.sketches.clearspring.cardinality.HyperLogLogPlusEntityGenerator) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) ElementTupleDefinition(uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition) ParseTime(uk.gov.gchq.koryphe.impl.function.ParseTime) ToString(uk.gov.gchq.koryphe.impl.function.ToString) FreqMap(uk.gov.gchq.gaffer.types.FreqMap) Map(java.util.Map) MapToTuple(uk.gov.gchq.koryphe.impl.function.MapToTuple) Tuple(uk.gov.gchq.koryphe.tuple.Tuple) Test(org.junit.Test)

Example 4 with ToLong

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

the class JoinIT method shouldLeftSideOuterJoinUsingKeyFunctionMatch.

@Test
public void shouldLeftSideOuterJoinUsingKeyFunctionMatch() throws OperationException {
    // Given
    final Map map = new Map.Builder<>().input(Lists.newArrayList(4L, 1L, 2L)).first(new Identity()).build();
    final ArrayList<Entity> input = Lists.newArrayList(getJoinEntity(TestGroups.ENTITY, 4), getJoinEntity(TestGroups.ENTITY_2, 4), getJoinEntity(TestGroups.ENTITY_3, 2), getJoinEntity(TestGroups.ENTITY_4, 5), getJoinEntity(TestGroups.ENTITY_4, 6), getJoinEntity(TestGroups.ENTITY_5, 5));
    final Join<Object> leftJoin = new Join.Builder<>().flatten(true).matchKey(MatchKey.LEFT).joinType(JoinType.OUTER).operation(map).matchMethod(new KeyFunctionMatch(new ExtractProperty("count"), new ToLong())).input(input).build();
    // When
    final Iterable<? extends MapTuple> results = graph.execute(leftJoin, user);
    final List<java.util.Map> loadedResults = new ArrayList<>();
    results.forEach(e -> loadedResults.add(e.getValues()));
    // Then
    assertThat(loadedResults).hasSize(3);
    assertThat(loadedResults.get(0)).containsEntry(MatchKey.LEFT.name(), getJoinEntity(TestGroups.ENTITY_4, 5)).containsEntry(MatchKey.RIGHT.name(), null);
    assertThat(loadedResults.get(1)).containsEntry(MatchKey.LEFT.name(), getJoinEntity(TestGroups.ENTITY_4, 6)).containsEntry(MatchKey.RIGHT.name(), null);
    assertThat(loadedResults.get(2)).containsEntry(MatchKey.LEFT.name(), getJoinEntity(TestGroups.ENTITY_5, 5)).containsEntry(MatchKey.RIGHT.name(), null);
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) Entity(uk.gov.gchq.gaffer.data.element.Entity) ArrayList(java.util.ArrayList) Join(uk.gov.gchq.gaffer.operation.impl.join.Join) KeyFunctionMatch(uk.gov.gchq.gaffer.store.operation.handler.join.match.KeyFunctionMatch) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) Identity(uk.gov.gchq.koryphe.impl.function.Identity) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.Test)

Example 5 with ToLong

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

the class ElementTransformerTest method shouldJsonSerialiseAndDeserialise.

@Test
@Override
public void shouldJsonSerialiseAndDeserialise() throws IOException {
    // Given
    final ElementTransformer ElementTransformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(new ToLong()).project(TestPropertyNames.PROP_2).build();
    final Properties testProperties = new Properties();
    testProperties.put(TestPropertyNames.PROP_1, 1);
    // When
    final String json = new String(JSONSerialiser.serialise(ElementTransformer));
    ElementTransformer deserialisedElementTransformer = JSONSerialiser.deserialise(json, ElementTransformer.class);
    // Then
    assertEquals(ElementTransformer, deserialisedElementTransformer);
    assertEquals("{\"functions\":[{\"selection\":[\"property1\"],\"function\":{\"class\":\"uk.gov.gchq.koryphe.impl.function.ToLong\"},\"projection\":[\"property2\"]}]}", json);
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) Properties(uk.gov.gchq.gaffer.data.element.Properties) FunctionTest(uk.gov.gchq.koryphe.function.FunctionTest) Test(org.junit.jupiter.api.Test)

Aggregations

ToLong (uk.gov.gchq.koryphe.impl.function.ToLong)10 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 Entity (uk.gov.gchq.gaffer.data.element.Entity)3 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)3 Element (uk.gov.gchq.gaffer.data.element.Element)2 Properties (uk.gov.gchq.gaffer.data.element.Properties)2 If (uk.gov.gchq.gaffer.operation.impl.If)2 Map (uk.gov.gchq.gaffer.operation.impl.Map)2 Join (uk.gov.gchq.gaffer.operation.impl.join.Join)2 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)2 KeyFunctionMatch (uk.gov.gchq.gaffer.store.operation.handler.join.match.KeyFunctionMatch)2 FunctionTest (uk.gov.gchq.koryphe.function.FunctionTest)2 Sum (uk.gov.gchq.koryphe.impl.binaryoperator.Sum)2 Identity (uk.gov.gchq.koryphe.impl.function.Identity)2 ToInteger (uk.gov.gchq.koryphe.impl.function.ToInteger)2 ToList (uk.gov.gchq.koryphe.impl.function.ToList)2 ToString (uk.gov.gchq.koryphe.impl.function.ToString)2 IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)2