use of uk.gov.gchq.koryphe.impl.binaryoperator.Sum in project gaffer-doc by gchq.
the class TheBasics method run.
@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
// [generate] Create some edges from the simple data file using our Road Use generator class
// ---------------------------------------------------------
final List<Element> elements = new ArrayList<>();
final RoadUseElementGenerator dataGenerator = new RoadUseElementGenerator();
for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
elements.add(dataGenerator._apply(line));
}
// ---------------------------------------------------------
print("Elements generated from the data file.");
for (final Element element : elements) {
print("GENERATED_EDGES", element.toString());
}
print("");
Graph graph;
// [graph from files] Create a graph using config, schema and store properties files
// ---------------------------------------------------------
graph = new Graph.Builder().config(StreamUtil.openStream(getClass(), graphConfigPath)).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(StreamUtil.openStream(getClass(), storePropertiesPath)).build();
// ---------------------------------------------------------
// [graph] Create a graph using config, schema and store properties from java
// ---------------------------------------------------------
final GraphConfig config = new GraphConfig.Builder().graphId(getClass().getSimpleName()).build();
final Schema schema = new Schema.Builder().edge("RoadUse", new SchemaEdgeDefinition.Builder().description("A directed edge representing vehicles moving from junction A to junction B.").source("junction").destination("junction").directed("true").property("count", "count.long").build()).type("junction", new TypeDefinition.Builder().description("A road junction represented by a String.").clazz(String.class).build()).type("count.long", new TypeDefinition.Builder().description("A long count that must be greater than or equal to 0.").clazz(Long.class).validateFunctions(new IsMoreThan(0L, true)).aggregateFunction(new Sum()).build()).type("true", new TypeDefinition.Builder().description("A simple boolean that must always be true.").clazz(Boolean.class).validateFunctions(new IsTrue()).build()).build();
final AccumuloProperties properties = new AccumuloProperties();
properties.setStoreClass(SingleUseMockAccumuloStore.class);
properties.setInstance("instance1");
properties.setZookeepers("zookeeper1");
properties.setUser("user01");
properties.setPassword("password");
graph = new Graph.Builder().config(config).addSchema(schema).storeProperties(properties).build();
// ---------------------------------------------------------
// [user] Create a user
// ---------------------------------------------------------
final User user = new User("user01");
// ---------------------------------------------------------
// [add] Add the edges to the graph
// ---------------------------------------------------------
final AddElements addElements = new AddElements.Builder().input(elements).build();
graph.execute(addElements, user);
// ---------------------------------------------------------
print("The elements have been added.");
// [get] Get all the edges that contain the vertex "10"
// ---------------------------------------------------------
final GetElements query = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse").build()).build();
final CloseableIterable<? extends Element> results = graph.execute(query, user);
// ---------------------------------------------------------
print("\nAll edges containing the vertex 10. The counts have been aggregated.");
for (final Element e : results) {
print("GET_ELEMENTS_RESULT", e.toString());
}
return results;
}
use of uk.gov.gchq.koryphe.impl.binaryoperator.Sum in project Gaffer by gchq.
the class AggregateHandlerTest method shouldAggregateElementsWhenNoGroupByInSchema.
@Test
public void shouldAggregateElementsWhenNoGroupByInSchema() throws OperationException {
// Given
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().build()).build();
given(store.getSchema()).willReturn(schema);
input.add(entity);
input.add(entity1);
input.add(entity2);
final AggregatePair pair = new AggregatePair(new String[] { "timestamp" }, new ElementAggregator.Builder().select("count").execute(new Sum()).build());
entities.put(TestGroups.ENTITY, pair);
final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 3L).property("count", 5).build();
expected.add(expectedEntity);
expected.add(entity1);
final Aggregate aggregate = new Aggregate.Builder().input(input).entities(entities).build();
// When
final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
final Set<Element> resultsSet = Sets.newHashSet(results);
// Then
assertEquals(expected, resultsSet);
}
use of uk.gov.gchq.koryphe.impl.binaryoperator.Sum in project Gaffer by gchq.
the class AccumuloStoreTest method shouldFindInconsistentVertexSerialiser.
@Test
public void shouldFindInconsistentVertexSerialiser() throws StoreException {
final Schema inconsistentSchema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().source("string").destination("string").directed("false").property(TestPropertyNames.INT, "int").groupBy(TestPropertyNames.INT).build()).type("string", new TypeDefinition.Builder().clazz(String.class).serialiser(new JavaSerialiser()).aggregateFunction(new StringConcat()).build()).type("int", new TypeDefinition.Builder().clazz(Integer.class).serialiser(new JavaSerialiser()).aggregateFunction(new Sum()).build()).type("false", Boolean.class).vertexSerialiser(new JavaSerialiser()).build();
final AccumuloStore store = new AccumuloStore();
// When & Then
assertThatExceptionOfType(SchemaException.class).isThrownBy(() -> store.preInitialise("graphId", inconsistentSchema, PROPERTIES)).withMessage("Vertex serialiser is inconsistent. This store requires vertices to be serialised in a consistent way.");
// When & Then
assertThatExceptionOfType(SchemaException.class).isThrownBy(() -> store.validateSchemas()).withMessage("Vertex serialiser is inconsistent. This store requires vertices to be serialised in a consistent way.");
}
use of uk.gov.gchq.koryphe.impl.binaryoperator.Sum 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.binaryoperator.Sum 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);
}
Aggregations