use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator in project Gaffer by gchq.
the class SchemaElementDefinitionValidator method validate.
/**
* Checks each identifier and property has a type associated with it.
* Checks all {@link uk.gov.gchq.gaffer.function.FilterFunction}s and {@link uk.gov.gchq.gaffer.function.AggregateFunction}s defined are
* compatible with the identifiers and properties - this is done by comparing the function input and output types with
* the identifier and property types.
*
* @param elementDef the {@link uk.gov.gchq.gaffer.data.elementdefinition.ElementDefinition} to validate
* @param requiresAggregators true if aggregators are required
* @return true if the element definition is valid, otherwise false and an error is logged
*/
public boolean validate(final SchemaElementDefinition elementDef, final boolean requiresAggregators) {
final ElementFilter validator = elementDef.getValidator();
final ElementAggregator aggregator = elementDef.getAggregator();
return validateComponentTypes(elementDef) && validateAggregator(aggregator, elementDef, requiresAggregators) && validateFunctionArgumentTypes(validator, elementDef) && validateFunctionArgumentTypes(aggregator, elementDef);
}
use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator in project Gaffer by gchq.
the class CoreKeyGroupByAggregatorIterator method reduce.
@Override
public Properties reduce(final String group, final Key key, final Iterator<Properties> iter) {
if (!iter.hasNext()) {
return new Properties();
}
final Properties properties = iter.next();
if (!iter.hasNext()) {
return properties;
}
final ElementAggregator aggregator = schema.getElement(group).getAggregator();
aggregator.aggregate(properties);
while (iter.hasNext()) {
aggregator.aggregate(iter.next());
}
final Properties aggregatedProperties = new Properties();
aggregator.state(aggregatedProperties);
return aggregatedProperties;
}
use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator in project Gaffer by gchq.
the class SchemaElementDefinitionTest method shouldReturnFullAggregator.
@Test
public void shouldReturnFullAggregator() {
// Given
final T elementDef = createBuilder().property("property", PROPERTY_STRING_TYPE).build();
setupSchema(elementDef);
// When
final ElementAggregator aggregator = elementDef.getAggregator();
// Then
assertEquals(1, aggregator.getFunctions().size());
assertTrue(aggregator.getFunctions().get(0).getFunction() instanceof ExampleAggregateFunction);
assertEquals(Collections.singletonList("property"), aggregator.getFunctions().get(0).getSelection());
}
use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator 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.data.element.function.ElementAggregator 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