use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class Aggregator method cloneFunctions.
/**
* Create a deep copy of the {@link uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext}s executed by this
* <code>Aggregator</code>.
*
* @return Deep copy of {@link uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext}s.
*/
protected List<PassThroughFunctionContext<R, AggregateFunction>> cloneFunctions() {
final List<PassThroughFunctionContext<R, AggregateFunction>> functionClones = new ArrayList<>();
for (final PassThroughFunctionContext<R, AggregateFunction> function : functions) {
final PassThroughFunctionContext<R, AggregateFunction> cloneContext = new PassThroughFunctionContext<>();
cloneContext.setSelection(function.getSelection());
final AggregateFunction af = function.getFunction();
if (af != null) {
cloneContext.setFunction(af.statelessClone());
}
functionClones.add(cloneContext);
}
return functionClones;
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class ElementAggregatorTest method shouldWrapPropertiesInPropertiesTupleAndCallSuper.
@Test
public void shouldWrapPropertiesInPropertiesTupleAndCallSuper() {
// Given
final String reference = "reference1";
final String value = "value";
final ElementAggregator aggregator = new ElementAggregator();
final PassThroughFunctionContext<String, AggregateFunction> functionContext1 = mock(PassThroughFunctionContext.class);
final AggregateFunction function = mock(AggregateFunction.class);
given(functionContext1.getFunction()).willReturn(function);
final List<String> references = Collections.singletonList(reference);
given(functionContext1.getSelection()).willReturn(references);
aggregator.addFunction(functionContext1);
final Properties properties = new Properties(reference, value);
final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
given(functionContext1.select(propertiesTupleCaptor.capture())).willReturn(new String[] { value });
// When
aggregator.aggregate(properties);
// Then
verify(functionContext1, times(2)).getFunction();
assertSame(properties, propertiesTupleCaptor.getValue().getProperties());
final ArgumentCaptor<Object[]> argumentCaptor = ArgumentCaptor.forClass(Object[].class);
verify(function).aggregate(argumentCaptor.capture());
assertEquals(value, argumentCaptor.getValue()[0]);
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class SchemaElementDefinitionValidator method validateAggregator.
private boolean validateAggregator(final ElementAggregator aggregator, final SchemaElementDefinition elementDef, final boolean requiresAggregators) {
if (null == elementDef.getPropertyMap() || elementDef.getPropertyMap().isEmpty()) {
// if no properties then no aggregation is necessary
return true;
}
if (null == aggregator || null == aggregator.getFunctions()) {
if (requiresAggregators) {
LOGGER.error("This framework requires that either all of the defined properties have an aggregator function associated with them, or none of them do.");
return false;
}
// if aggregate functions are not defined then it is valid
return true;
}
// if aggregate functions are defined then check all properties are aggregated
final Set<String> aggregatedProperties = new HashSet<>();
if (aggregator.getFunctions() != null) {
for (final PassThroughFunctionContext<String, AggregateFunction> context : aggregator.getFunctions()) {
final List<String> selection = context.getSelection();
if (selection != null) {
for (final String key : selection) {
final IdentifierType idType = IdentifierType.fromName(key);
if (null == idType) {
aggregatedProperties.add(key);
}
}
}
}
}
final Set<String> propertyNamesTmp = new HashSet<>(elementDef.getProperties());
propertyNamesTmp.removeAll(aggregatedProperties);
if (propertyNamesTmp.isEmpty()) {
return true;
}
LOGGER.error("no aggregator found for properties '" + propertyNamesTmp.toString() + "' in the supplied schema. This framework requires that either all of the defined properties have an aggregator function associated with them, or none of them do.");
return false;
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateAndReturnFalseWhenAPropertyDoesNotHaveAnAggregateFunction.
@Test
public void shouldValidateAndReturnFalseWhenAPropertyDoesNotHaveAnAggregateFunction() {
// Given
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final ElementAggregator aggregator = mock(ElementAggregator.class);
final PassThroughFunctionContext<String, AggregateFunction> context1 = mock(PassThroughFunctionContext.class);
final AggregateFunction function = mock(AggregateFunction.class);
final List<PassThroughFunctionContext<String, AggregateFunction>> contexts = new ArrayList<>();
contexts.add(context1);
given(elementDef.getIdentifiers()).willReturn(new HashSet<IdentifierType>());
given(elementDef.getProperties()).willReturn(new HashSet<>(Arrays.asList(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2)));
given(elementDef.getValidator()).willReturn(mock(ElementFilter.class));
given(elementDef.getAggregator()).willReturn(aggregator);
given(context1.getSelection()).willReturn(Collections.singletonList(TestPropertyNames.PROP_1));
given(function.getInputClasses()).willReturn(new Class[] { String.class, Integer.class });
given(context1.getFunction()).willReturn(function);
given(aggregator.getFunctions()).willReturn(contexts);
given(elementDef.getPropertyClass(TestPropertyNames.PROP_1)).willReturn((Class) String.class);
given(elementDef.getPropertyClass(TestPropertyNames.PROP_2)).willReturn((Class) Integer.class);
// When
final boolean isValid = validator.validate(elementDef, true);
// Then
assertFalse(isValid);
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateAndReturnTrueWhenAggregatorIsValid.
@Test
public void shouldValidateAndReturnTrueWhenAggregatorIsValid() {
// Given
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final ElementAggregator aggregator = mock(ElementAggregator.class);
final PassThroughFunctionContext<String, AggregateFunction> context1 = mock(PassThroughFunctionContext.class);
final AggregateFunction function = mock(AggregateFunction.class);
final List<PassThroughFunctionContext<String, AggregateFunction>> contexts = new ArrayList<>();
contexts.add(context1);
given(elementDef.getIdentifiers()).willReturn(new HashSet<IdentifierType>());
given(elementDef.getProperties()).willReturn(new HashSet<>(Arrays.asList(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2)));
given(elementDef.getValidator()).willReturn(mock(ElementFilter.class));
given(elementDef.getAggregator()).willReturn(aggregator);
given(context1.getSelection()).willReturn(Arrays.asList(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2));
given(function.getInputClasses()).willReturn(new Class[] { String.class, Integer.class });
given(context1.getFunction()).willReturn(function);
given(aggregator.getFunctions()).willReturn(contexts);
given(elementDef.getPropertyClass(TestPropertyNames.PROP_1)).willReturn((Class) String.class);
given(elementDef.getPropertyClass(TestPropertyNames.PROP_2)).willReturn((Class) Integer.class);
given(elementDef.getClass(TestPropertyNames.PROP_1)).willReturn((Class) String.class);
given(elementDef.getClass(TestPropertyNames.PROP_2)).willReturn((Class) Integer.class);
// When
final boolean isValid = validator.validate(elementDef, true);
// Then
assertTrue(isValid);
verify(elementDef).getClass(TestPropertyNames.PROP_1);
verify(elementDef).getClass(TestPropertyNames.PROP_2);
verify(function).getInputClasses();
}
Aggregations