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