Search in sources :

Example 1 with PassThroughFunctionContext

use of uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext 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 PassThroughFunctionContext

use of uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext 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 3 with PassThroughFunctionContext

use of uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext 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)

Example 4 with PassThroughFunctionContext

use of uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext in project Gaffer by gchq.

the class SchemaTest method testLoadingSchemaFromJson.

@Test
public void testLoadingSchemaFromJson() {
    // Edge definitions
    SchemaElementDefinition edgeDefinition = schema.getEdge(TestGroups.EDGE);
    assertNotNull(edgeDefinition);
    assertEquals(EDGE_DESCRIPTION, edgeDefinition.getDescription());
    final Map<String, String> propertyMap = edgeDefinition.getPropertyMap();
    assertEquals(3, propertyMap.size());
    assertEquals("prop.string", propertyMap.get(TestPropertyNames.PROP_2));
    assertEquals("prop.date", propertyMap.get(TestPropertyNames.DATE));
    assertEquals("timestamp", propertyMap.get(TestPropertyNames.TIMESTAMP));
    assertEquals(Sets.newLinkedHashSet(Collections.singletonList(TestPropertyNames.DATE)), edgeDefinition.getGroupBy());
    // Check validator
    ElementFilter validator = edgeDefinition.getValidator();
    final List<ConsumerFunctionContext<String, FilterFunction>> valContexts = validator.getFunctions();
    int index = 0;
    ConsumerFunctionContext<String, FilterFunction> valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.SOURCE.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.DESTINATION.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.DIRECTED.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof ExampleFilterFunction);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.DIRECTED.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.PROP_2, valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof ExampleFilterFunction);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.PROP_2, valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.DATE, valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.TIMESTAMP, valContext.getSelection().get(0));
    assertEquals(index, valContexts.size());
    TypeDefinition type = edgeDefinition.getPropertyTypeDef(TestPropertyNames.DATE);
    assertEquals(Date.class, type.getClazz());
    assertEquals(DATE_TYPE_DESCRIPTION, type.getDescription());
    assertNull(type.getSerialiser());
    assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
    // Entity definitions
    SchemaElementDefinition entityDefinition = schema.getEntity(TestGroups.ENTITY);
    assertNotNull(entityDefinition);
    assertEquals(ENTITY_DESCRIPTION, entityDefinition.getDescription());
    assertTrue(entityDefinition.containsProperty(TestPropertyNames.PROP_1));
    type = entityDefinition.getPropertyTypeDef(TestPropertyNames.PROP_1);
    assertEquals(0, entityDefinition.getGroupBy().size());
    assertEquals(STRING_TYPE_DESCRIPTION, type.getDescription());
    assertEquals(String.class, type.getClazz());
    assertNull(type.getSerialiser());
    assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
    ElementAggregator aggregator = edgeDefinition.getAggregator();
    List<PassThroughFunctionContext<String, AggregateFunction>> aggContexts = aggregator.getFunctions();
    assertEquals(3, aggContexts.size());
    PassThroughFunctionContext<String, AggregateFunction> aggContext = aggContexts.get(0);
    assertTrue(aggContext.getFunction() instanceof ExampleAggregateFunction);
    assertEquals(1, aggContext.getSelection().size());
    assertEquals(TestPropertyNames.PROP_2, aggContext.getSelection().get(0));
    aggContext = aggContexts.get(1);
    assertTrue(aggContext.getFunction() instanceof ExampleAggregateFunction);
    assertEquals(1, aggContext.getSelection().size());
    assertEquals(TestPropertyNames.DATE, aggContext.getSelection().get(0));
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) PassThroughFunctionContext(uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsA(uk.gov.gchq.gaffer.function.IsA) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.Test)

Aggregations

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