Search in sources :

Example 6 with ElementAggregator

use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator 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 ElementAggregator

use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator in project Gaffer by gchq.

the class AggregatorIterator method reduce.

@Override
public Value reduce(final Key key, final Iterator<Value> iter) {
    // Get first Value. If this is the only Value then return it straight
    // away;
    Value value = iter.next();
    if (!iter.hasNext()) {
        return value;
    }
    final String group;
    try {
        group = new String(key.getColumnFamilyData().getBackingArray(), CommonConstants.UTF_8);
    } catch (final UnsupportedEncodingException e) {
        throw new AggregationException("Failed to recreate a graph element from a key and value", e);
    }
    Properties properties;
    final ElementAggregator aggregator;
    try {
        properties = elementConverter.getPropertiesFromValue(group, value);
    } catch (final AccumuloElementConversionException e) {
        throw new AggregationException("Failed to recreate a graph element from a key and value", e);
    }
    aggregator = schema.getElement(group).getAggregator();
    aggregator.aggregate(properties);
    while (iter.hasNext()) {
        value = iter.next();
        try {
            properties = elementConverter.getPropertiesFromValue(group, value);
        } catch (final AccumuloElementConversionException e) {
            throw new AggregationException("Failed to recreate a graph element from a key and value", e);
        }
        aggregator.aggregate(properties);
    }
    properties = new Properties();
    aggregator.state(properties);
    try {
        return elementConverter.getValueFromProperties(group, properties);
    } catch (final AccumuloElementConversionException e) {
        throw new AggregationException("Failed to create an accumulo value from an elements properties", e);
    }
}
Also used : AggregationException(uk.gov.gchq.gaffer.accumulostore.key.exception.AggregationException) Value(org.apache.accumulo.core.data.Value) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(uk.gov.gchq.gaffer.data.element.Properties) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 8 with ElementAggregator

use of uk.gov.gchq.gaffer.data.element.function.ElementAggregator in project Gaffer by gchq.

the class AccumuloKeyValueReducer method reduceMultiValue.

private Value reduceMultiValue(final Key key, final Iterator<Value> iter, final Value firstValue) {
    final String group;
    try {
        group = new String(key.getColumnFamilyData().getBackingArray(), CommonConstants.UTF_8);
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    ElementAggregator aggregator;
    Properties firstPropertySet;
    try {
        firstPropertySet = elementConverter.getPropertiesFromValue(group, firstValue);
        aggregator = schema.getElement(group).getAggregator();
        aggregator.aggregate(firstPropertySet);
        while (iter.hasNext()) {
            aggregator.aggregate(elementConverter.getPropertiesFromValue(group, iter.next()));
        }
    } catch (final AccumuloElementConversionException e) {
        throw new IllegalArgumentException("Failed to get Properties from an accumulo value", e);
    }
    final Properties properties = new Properties();
    aggregator.state(properties);
    try {
        return elementConverter.getValueFromProperties(group, properties);
    } catch (final AccumuloElementConversionException e) {
        throw new IllegalArgumentException("Failed to get Properties from an accumulo value", e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(uk.gov.gchq.gaffer.data.element.Properties) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

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