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));
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations