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