Search in sources :

Example 1 with AggregateFunction

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;
}
Also used : AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) ArrayList(java.util.ArrayList) PassThroughFunctionContext(uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext)

Example 2 with AggregateFunction

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

Example 3 with AggregateFunction

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;
}
Also used : AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) HashSet(java.util.HashSet)

Example 4 with AggregateFunction

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

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

AggregateFunction (uk.gov.gchq.gaffer.function.AggregateFunction)13 Test (org.junit.Test)11 PropertiesTuple (uk.gov.gchq.gaffer.data.element.PropertiesTuple)4 PassThroughFunctionContext (uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext)4 ArrayList (java.util.ArrayList)3 IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)3 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)3 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)3 Edge (uk.gov.gchq.gaffer.data.element.Edge)2 Properties (uk.gov.gchq.gaffer.data.element.Properties)2 HashSet (java.util.HashSet)1 ExampleAggregateFunction (uk.gov.gchq.gaffer.function.ExampleAggregateFunction)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