use of uk.gov.gchq.koryphe.impl.function.IterableFunction 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.IterableFunction in project gaffer-doc by gchq.
the class IterableFunctionExample method applyFunctionIteratively.
public void applyFunctionIteratively() {
// ---------------------------------------------------------
final IterableFunction<Integer, Integer> function = new IterableFunction<>(new MultiplyBy(2));
// ---------------------------------------------------------
runExample(function, null, Lists.newArrayList(1, 2, 3), Lists.newArrayList(5, 10, 15), Lists.newArrayList(7, 9, 11), Lists.newArrayList(1, null, 3), null);
}
use of uk.gov.gchq.koryphe.impl.function.IterableFunction in project gaffer-doc by gchq.
the class IterableFunctionExample method applyMultipleFunctions.
public void applyMultipleFunctions() {
// ---------------------------------------------------------
final IterableFunction<Integer, Integer> function = new IterableFunction.Builder<Integer>().first(new MultiplyBy(2)).then(new MultiplyBy(4)).build();
// ---------------------------------------------------------
runExample(function, "Here we build a chain of functions using the IterableFunction's Builder, whereby the output of one function is the input to the next.", Lists.newArrayList(2, 4, 10), Lists.newArrayList(3, 9, 11), Lists.newArrayList(1, null, 3), null);
}
Aggregations