Search in sources :

Example 1 with ElementAggregator

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);
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Example 2 with ElementAggregator

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;
}
Also used : Properties(uk.gov.gchq.gaffer.data.element.Properties) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Example 3 with ElementAggregator

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());
}
Also used : ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.Test)

Example 4 with ElementAggregator

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);
}
Also used : ArrayList(java.util.ArrayList) PassThroughFunctionContext(uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.Test)

Example 5 with ElementAggregator

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();
}
Also used : ArrayList(java.util.ArrayList) PassThroughFunctionContext(uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.Test)

Aggregations

ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)8 Test (org.junit.Test)4 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)4 Properties (uk.gov.gchq.gaffer.data.element.Properties)3 AggregateFunction (uk.gov.gchq.gaffer.function.AggregateFunction)3 PassThroughFunctionContext (uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)2 IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)2 ExampleAggregateFunction (uk.gov.gchq.gaffer.function.ExampleAggregateFunction)2 Value (org.apache.accumulo.core.data.Value)1 AggregationException (uk.gov.gchq.gaffer.accumulostore.key.exception.AggregationException)1 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)1 FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)1 IsA (uk.gov.gchq.gaffer.function.IsA)1 ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)1