Search in sources :

Example 1 with Sum

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;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) Element(uk.gov.gchq.gaffer.data.element.Element) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) Graph(uk.gov.gchq.gaffer.graph.Graph) IsTrue(uk.gov.gchq.koryphe.impl.predicate.IsTrue) RoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadUseElementGenerator) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Example 2 with Sum

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);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Test(org.junit.jupiter.api.Test)

Example 3 with Sum

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.");
}
Also used : StringConcat(uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with Sum

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);
    }
}
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 5 with Sum

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

Aggregations

Sum (uk.gov.gchq.koryphe.impl.binaryoperator.Sum)10 Element (uk.gov.gchq.gaffer.data.element.Element)8 Schema (uk.gov.gchq.gaffer.store.schema.Schema)8 Test (org.junit.jupiter.api.Test)7 Aggregate (uk.gov.gchq.gaffer.operation.impl.function.Aggregate)5 AggregatePair (uk.gov.gchq.gaffer.operation.util.AggregatePair)5 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)4 ArrayList (java.util.ArrayList)3 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 Entity (uk.gov.gchq.gaffer.data.element.Entity)3 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)2 Max (uk.gov.gchq.koryphe.impl.binaryoperator.Max)2 StringConcat (uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat)2 ToLong (uk.gov.gchq.koryphe.impl.function.ToLong)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1