Search in sources :

Example 11 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateHandlerTest method shouldAggregateAMixOfEdgesAndEntities.

@Test
public void shouldAggregateAMixOfEdgesAndEntities() throws OperationException {
    // Given
    final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("turns").execute(new Max()).build()).build()).entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().aggregator(new ElementAggregator.Builder().select("count").execute(new Sum()).build()).build()).build();
    given(store.getSchema()).willReturn(schema);
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    input.add(entity);
    input.add(entity1);
    input.add(entity2);
    input.add(entity3);
    final AggregatePair edgePair = new AggregatePair(new String[] { "timestamp" });
    final AggregatePair entityPair = new AggregatePair(new String[] { "timestamp" });
    edges.put(TestGroups.EDGE, edgePair);
    entities.put(TestGroups.ENTITY, entityPair);
    final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 6).property("count", 2L).build();
    final Edge expectedEdge1 = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 1L).property("turns", 9).property("count", 5L).build();
    final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 2L).property("count", 6).build();
    final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 3L).property("count", 5).build();
    expected.add(expectedEdge);
    expected.add(expectedEdge1);
    expected.add(expectedEntity);
    expected.add(expectedEntity1);
    final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).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) Max(uk.gov.gchq.koryphe.impl.binaryoperator.Max) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Edge(uk.gov.gchq.gaffer.data.element.Edge) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.jupiter.api.Test)

Example 12 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateHandlerTest method shouldAggregateEdgesFromMultipleGroups.

@Test
public void shouldAggregateEdgesFromMultipleGroups() throws OperationException {
    final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("turns").execute(new Max()).build()).groupBy("timestamp").build()).edge(TestGroups.EDGE_2, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("count").execute(new Sum()).select("length").execute(new Sum()).build()).groupBy("timestamp").build()).build();
    given(store.getSchema()).willReturn(schema);
    input.add(edge);
    input.add(edge1);
    input.add(edge2);
    input.add(edge3);
    input.add(edge4);
    final AggregatePair pair = new AggregatePair();
    final AggregatePair pair1 = new AggregatePair();
    edges.put(TestGroups.EDGE, pair);
    edges.put(TestGroups.EDGE_2, pair1);
    final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 6).property("count", 2L).build();
    final Edge expectedEdge1 = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 1L).property("turns", 9).property("count", 5L).build();
    final Edge expectedEdge2 = new Edge.Builder().group(TestGroups.EDGE_2).property("timestamp", 4L).property("length", 40).property("count", 5L).build();
    expected.add(expectedEdge);
    expected.add(expectedEdge1);
    expected.add(expectedEdge2);
    final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).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 : Max(uk.gov.gchq.koryphe.impl.binaryoperator.Max) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Element(uk.gov.gchq.gaffer.data.element.Element) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Edge(uk.gov.gchq.gaffer.data.element.Edge) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.jupiter.api.Test)

Example 13 with AggregatePair

use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.

the class AggregateValidator method validateElementAggregator.

private ValidationResult validateElementAggregator(final Map.Entry<String, ?> entry, final Schema schema) {
    final ValidationResult result = new ValidationResult();
    List<TupleAdaptedBinaryOperator<String, ?>> aggregateFunctions = new ArrayList<>();
    final SchemaElementDefinition schemaElement = schema.getElement(entry.getKey());
    if (null != schemaElement) {
        aggregateFunctions = schemaElement.getOriginalAggregateFunctions();
    }
    List<BinaryOperator<?>> schemaOperators = new ArrayList<>();
    if (null != aggregateFunctions) {
        schemaOperators = Streams.toStream(aggregateFunctions).map(AdaptedBinaryOperator::getBinaryOperator).collect(Collectors.toList());
    }
    if (schemaOperators.contains(null)) {
        result.addError("Schema contains an ElementAggregator with a null function.");
    }
    final AggregatePair pair = (AggregatePair) entry.getValue();
    final ElementAggregator aggregator = pair.getElementAggregator();
    if (null != aggregator && null != aggregator.getComponents()) {
        for (final TupleAdaptedBinaryOperator<String, ?> adaptedFunction : aggregator.getComponents()) {
            if (null == adaptedFunction.getBinaryOperator()) {
                result.addError(aggregator.getClass().getSimpleName() + " contains a null function.");
            }
        }
    }
    return result;
}
Also used : TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) AdaptedBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.AdaptedBinaryOperator) ArrayList(java.util.ArrayList) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) AdaptedBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.AdaptedBinaryOperator) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Aggregations

AggregatePair (uk.gov.gchq.gaffer.operation.util.AggregatePair)13 Aggregate (uk.gov.gchq.gaffer.operation.impl.function.Aggregate)9 Test (org.junit.jupiter.api.Test)8 Schema (uk.gov.gchq.gaffer.store.schema.Schema)8 Element (uk.gov.gchq.gaffer.data.element.Element)5 Sum (uk.gov.gchq.koryphe.impl.binaryoperator.Sum)5 HashMap (java.util.HashMap)4 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)4 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)4 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 Entity (uk.gov.gchq.gaffer.data.element.Entity)3 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 SchemaEntityDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition)2 Max (uk.gov.gchq.koryphe.impl.binaryoperator.Max)2 HashSet (java.util.HashSet)1 BinaryOperator (java.util.function.BinaryOperator)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1