Search in sources :

Example 6 with AggregateFunction

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

Example 7 with AggregateFunction

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

the class ElementAggregatorTest method shouldSetStateOnElement.

@Test
public void shouldSetStateOnElement() {
    // Given
    final Object[] state = { "state1", "state2" };
    final ElementAggregator aggregator = new ElementAggregator();
    final PassThroughFunctionContext<String, AggregateFunction> functionContext1 = mock(PassThroughFunctionContext.class);
    final AggregateFunction function = mock(AggregateFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    given(function.state()).willReturn(state);
    aggregator.addFunction(functionContext1);
    final Edge edge = new Edge("group");
    // When
    aggregator.state(edge);
    // Then
    final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
    verify(functionContext1).project(propertiesTupleCaptor.capture(), Mockito.eq(state));
    assertEquals(edge.getProperties(), propertiesTupleCaptor.getValue().getProperties());
}
Also used : PropertiesTuple(uk.gov.gchq.gaffer.data.element.PropertiesTuple) AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.Test)

Example 8 with AggregateFunction

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

the class ElementAggregatorTest method shouldBuildAggregator.

@Test
public void shouldBuildAggregator() {
    // Given
    final String property1 = "property 1";
    final String property2 = "property 2";
    final String property3a = "property 3a";
    final String property3b = "property 3b";
    final String property5 = "property 5";
    final AggregateFunction func1 = mock(AggregateFunction.class);
    final AggregateFunction func3 = mock(AggregateFunction.class);
    final AggregateFunction func4 = mock(AggregateFunction.class);
    final AggregateFunction func5 = mock(AggregateFunction.class);
    // When - check you can build the selection/function in any order,
    // although normally it will be done - select then execute.
    final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1).execute(func1).select(property2).select(property3a, property3b).execute(func3).execute(func4).execute(func5).select(property5).build();
    // Then
    int i = 0;
    PassThroughFunctionContext<String, AggregateFunction> context = aggregator.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property1, context.getSelection().get(0));
    assertSame(func1, context.getFunction());
    context = aggregator.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property2, context.getSelection().get(0));
    context = aggregator.getFunctions().get(i++);
    assertEquals(2, context.getSelection().size());
    assertEquals(property3a, context.getSelection().get(0));
    assertEquals(property3b, context.getSelection().get(1));
    assertSame(func3, context.getFunction());
    context = aggregator.getFunctions().get(i++);
    assertSame(func4, context.getFunction());
    context = aggregator.getFunctions().get(i++);
    assertSame(func5, context.getFunction());
    assertEquals(1, context.getSelection().size());
    assertEquals(property5, context.getSelection().get(0));
    assertEquals(i, aggregator.getFunctions().size());
}
Also used : AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) Test(org.junit.Test)

Example 9 with AggregateFunction

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

the class ElementAggregatorTest method shouldSetStateOnProperties.

@Test
public void shouldSetStateOnProperties() {
    // Given
    final Object[] state = { "state1", "state2" };
    final ElementAggregator aggregator = new ElementAggregator();
    final PassThroughFunctionContext<String, AggregateFunction> functionContext1 = mock(PassThroughFunctionContext.class);
    final AggregateFunction function = mock(AggregateFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    given(function.state()).willReturn(state);
    aggregator.addFunction(functionContext1);
    final Properties properties = new Properties();
    // When
    aggregator.state(properties);
    // Then
    final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
    verify(functionContext1).project(propertiesTupleCaptor.capture(), Mockito.eq(state));
    assertSame(properties, propertiesTupleCaptor.getValue().getProperties());
}
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 10 with AggregateFunction

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

the class ElementAggregatorTest method shouldWrapElementInElementTupleAndCallSuper.

@Test
public void shouldWrapElementInElementTupleAndCallSuper() {
    // 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 Edge edge = new Edge("group");
    edge.putProperty(reference, value);
    final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
    given(functionContext1.select(propertiesTupleCaptor.capture())).willReturn(new String[] { value });
    // When
    aggregator.aggregate(edge);
    // Then
    assertEquals(edge.getProperties(), propertiesTupleCaptor.getValue().getProperties());
    verify(functionContext1, times(2)).getFunction();
    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) Edge(uk.gov.gchq.gaffer.data.element.Edge) 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