Search in sources :

Example 1 with IdentifierType

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

the class ElementFilterTest method shouldBuildFilter.

@Test
public void shouldBuildFilter() {
    // Given
    final String property1 = "property 1";
    final String property2 = "property 2";
    final String property3a = "property 3a";
    final String property3b = "property 3b";
    final IdentifierType identifierType5 = IdentifierType.VERTEX;
    final FilterFunction func1 = mock(FilterFunction.class);
    final FilterFunction func3 = mock(FilterFunction.class);
    final FilterFunction func4 = mock(FilterFunction.class);
    final FilterFunction func5 = mock(FilterFunction.class);
    // When - check you can build the selection/function in any order,
    // although normally it will be done - select then execute.
    final ElementFilter filter = new ElementFilter.Builder().select(property1).execute(func1).select(property2).select(property3a, property3b).execute(func3).execute(func4).execute(func5).select(identifierType5.name()).build();
    // Then
    int i = 0;
    ConsumerFunctionContext<String, FilterFunction> context = filter.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property1, context.getSelection().get(0));
    assertSame(func1, context.getFunction());
    context = filter.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property2, context.getSelection().get(0));
    context = filter.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 = filter.getFunctions().get(i++);
    assertSame(func4, context.getFunction());
    context = filter.getFunctions().get(i++);
    assertSame(func5, context.getFunction());
    assertEquals(1, context.getSelection().size());
    assertEquals(identifierType5.name(), context.getSelection().get(0));
    assertEquals(i, filter.getFunctions().size());
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) Test(org.junit.Test)

Example 2 with IdentifierType

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

the class ElementTransformerTest method shouldBuildTransformer.

@Test
public void shouldBuildTransformer() {
    // Given
    final String property1 = "property 1";
    final String property2 = "property 2";
    final String property3a = "property 3a";
    final String property3b = "property 3b";
    final IdentifierType identifier5 = IdentifierType.SOURCE;
    final String property1Proj = "property 1 proj";
    final String property2Proj = "property 2 proj";
    final String property3aProj = "property 3a proj";
    final String property3bProj = "property 3b proj";
    final IdentifierType identifier5Proj = IdentifierType.DESTINATION;
    final TransformFunction func1 = mock(TransformFunction.class);
    final TransformFunction func3 = mock(TransformFunction.class);
    final TransformFunction func4 = mock(TransformFunction.class);
    final TransformFunction func5 = mock(TransformFunction.class);
    // When - check you can build the selection/function/projections in any order,
    // although normally it will be done - select, execute then project.
    final ElementTransformer transformer = new ElementTransformer.Builder().select(property1).execute(func1).project(property1Proj).select(property2).project(property2Proj).project(property3aProj, property3bProj).select(property3a, property3b).execute(func3).execute(func4).execute(func5).project(identifier5Proj.name()).select(identifier5.name()).build();
    // Then
    int i = 0;
    ConsumerProducerFunctionContext<String, TransformFunction> context = transformer.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property1, context.getSelection().get(0));
    assertSame(func1, context.getFunction());
    assertEquals(1, context.getProjection().size());
    assertEquals(property1Proj, context.getProjection().get(0));
    context = transformer.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property2, context.getSelection().get(0));
    assertEquals(1, context.getProjection().size());
    assertEquals(property2Proj, context.getProjection().get(0));
    context = transformer.getFunctions().get(i++);
    assertEquals(2, context.getSelection().size());
    assertEquals(property3a, context.getSelection().get(0));
    assertEquals(property3b, context.getSelection().get(1));
    assertSame(func3, context.getFunction());
    assertEquals(2, context.getProjection().size());
    assertEquals(property3aProj, context.getProjection().get(0));
    assertEquals(property3bProj, context.getProjection().get(1));
    context = transformer.getFunctions().get(i++);
    assertSame(func4, context.getFunction());
    context = transformer.getFunctions().get(i++);
    assertSame(func5, context.getFunction());
    assertEquals(1, context.getSelection().size());
    assertEquals(identifier5.name(), context.getSelection().get(0));
    assertEquals(1, context.getProjection().size());
    assertEquals(identifier5Proj.name(), context.getProjection().get(0));
    assertEquals(i, transformer.getFunctions().size());
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) Test(org.junit.Test)

Example 3 with IdentifierType

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

the class SchemaElementDefinitionValidator method validateAggregator.

private boolean validateAggregator(final ElementAggregator aggregator, final SchemaElementDefinition elementDef, final boolean requiresAggregators) {
    if (null == elementDef.getPropertyMap() || elementDef.getPropertyMap().isEmpty()) {
        // if no properties then no aggregation is necessary
        return true;
    }
    if (null == aggregator || null == aggregator.getFunctions()) {
        if (requiresAggregators) {
            LOGGER.error("This framework requires that either all of the defined properties have an aggregator function associated with them, or none of them do.");
            return false;
        }
        // if aggregate functions are not defined then it is valid
        return true;
    }
    // if aggregate functions are defined then check all properties are aggregated
    final Set<String> aggregatedProperties = new HashSet<>();
    if (aggregator.getFunctions() != null) {
        for (final PassThroughFunctionContext<String, AggregateFunction> context : aggregator.getFunctions()) {
            final List<String> selection = context.getSelection();
            if (selection != null) {
                for (final String key : selection) {
                    final IdentifierType idType = IdentifierType.fromName(key);
                    if (null == idType) {
                        aggregatedProperties.add(key);
                    }
                }
            }
        }
    }
    final Set<String> propertyNamesTmp = new HashSet<>(elementDef.getProperties());
    propertyNamesTmp.removeAll(aggregatedProperties);
    if (propertyNamesTmp.isEmpty()) {
        return true;
    }
    LOGGER.error("no aggregator found for properties '" + propertyNamesTmp.toString() + "' in the supplied schema. This framework requires that either all of the defined properties have an aggregator function associated with them, or none of them do.");
    return false;
}
Also used : AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) HashSet(java.util.HashSet)

Example 4 with IdentifierType

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

the class ViewValidator method validateFunctionSelectionTypes.

private boolean validateFunctionSelectionTypes(final ViewElementDefinition viewElDef, final SchemaElementDefinition schemaElDef, final ConsumerFunctionContext<String, ? extends ConsumerFunction> context) {
    final ConsumerFunction function = context.getFunction();
    final Class<?>[] inputTypes = function.getInputClasses();
    if (null == inputTypes || 0 == inputTypes.length) {
        LOGGER.error("Function " + function.getClass().getName() + " is invalid. Input types have not been set.");
        return false;
    }
    if (inputTypes.length != context.getSelection().size()) {
        LOGGER.error("Input types for function " + function.getClass().getName() + " are not equal to the selection property types.");
        return false;
    }
    int i = 0;
    for (final String key : context.getSelection()) {
        final IdentifierType idType = IdentifierType.fromName(key);
        final Class<?> clazz;
        if (null != idType) {
            clazz = schemaElDef.getIdentifierClass(idType);
        } else {
            final Class<?> schemaClazz = schemaElDef.getPropertyClass(key);
            if (null != schemaClazz) {
                clazz = schemaClazz;
            } else {
                clazz = viewElDef.getTransientPropertyClass(key);
            }
        }
        if (null == clazz) {
            if (null != idType) {
                final String typeName = schemaElDef.getIdentifierTypeName(idType);
                if (null != typeName) {
                    LOGGER.error("No class type found for type definition " + typeName + " used by identifier " + idType + ". Please ensure it is defined in the schema.");
                } else {
                    LOGGER.error("No type definition defined for identifier " + idType + ". Please ensure it is defined in the schema.");
                }
            } else {
                final String typeName = schemaElDef.getPropertyTypeName(key);
                if (null != typeName) {
                    LOGGER.error("No class type found for type definition " + typeName + " used by property " + key + ". Please ensure it is defined in the schema.");
                } else {
                    LOGGER.error("No class type found for transient property " + key + ". Please ensure it is defined in the view.");
                }
            }
            return false;
        }
        if (!inputTypes[i].isAssignableFrom(clazz)) {
            LOGGER.error("Function " + function.getClass().getName() + " is not compatible with selection types. Function input type " + inputTypes[i].getName() + " is not assignable from selection type " + clazz.getName() + ".");
            return false;
        }
        i++;
    }
    return true;
}
Also used : ConsumerFunction(uk.gov.gchq.gaffer.function.ConsumerFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType)

Example 5 with IdentifierType

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

the class ViewValidator method validateFunctionProjectionTypes.

private boolean validateFunctionProjectionTypes(final ViewElementDefinition viewElDef, final SchemaElementDefinition schemaElDef, final ConsumerProducerFunctionContext<String, ? extends ConsumerFunction> consumerProducerContext) {
    final ConsumerProducerFunction function = consumerProducerContext.getFunction();
    final Class<?>[] outputTypes = function.getOutputClasses();
    if (null == outputTypes || 0 == outputTypes.length) {
        LOGGER.error("Function " + function.getClass().getName() + " is invalid. Output types have not been set.");
        return false;
    }
    if (outputTypes.length != consumerProducerContext.getProjection().size()) {
        LOGGER.error("Output types for function " + function.getClass().getName() + " are not equal to the projection property types.");
        return false;
    }
    int i = 0;
    for (final String key : consumerProducerContext.getProjection()) {
        final Class<?> clazz;
        final IdentifierType idType = IdentifierType.fromName(key);
        if (null != idType) {
            clazz = schemaElDef.getIdentifierClass(idType);
        } else {
            final Class<?> schemaClazz = schemaElDef.getPropertyClass(key);
            if (null != schemaClazz) {
                clazz = schemaClazz;
            } else {
                clazz = viewElDef.getTransientPropertyClass(key);
            }
        }
        if (null == clazz || !outputTypes[i].isAssignableFrom(clazz)) {
            LOGGER.error("Function " + function.getClass().getName() + " is not compatible with output types. Function output type " + outputTypes[i].getName() + " is not assignable from projection type " + (null != clazz ? clazz.getName() : "with a null class."));
            return false;
        }
        i++;
    }
    return true;
}
Also used : IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) ConsumerProducerFunction(uk.gov.gchq.gaffer.function.ConsumerProducerFunction)

Aggregations

IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)11 Test (org.junit.Test)5 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)3 AggregateFunction (uk.gov.gchq.gaffer.function.AggregateFunction)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)2 ConsumerFunction (uk.gov.gchq.gaffer.function.ConsumerFunction)2 PassThroughFunctionContext (uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext)2 ConsumerProducerFunction (uk.gov.gchq.gaffer.function.ConsumerProducerFunction)1 FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)1 TransformFunction (uk.gov.gchq.gaffer.function.TransformFunction)1