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