Search in sources :

Example 6 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 7 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)

Example 8 with IdentifierType

use of uk.gov.gchq.gaffer.data.element.IdentifierType 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();
}
Also used : ArrayList(java.util.ArrayList) PassThroughFunctionContext(uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.Test)

Example 9 with IdentifierType

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

the class SchemaElementDefinition method getValidator.

public ElementFilter getValidator(final boolean includeIsA) {
    final ElementFilter fullValidator = null != validator ? validator.clone() : new ElementFilter();
    for (final Entry<IdentifierType, String> entry : getIdentifierMap().entrySet()) {
        final String key = entry.getKey().name();
        if (includeIsA) {
            addIsAFunction(fullValidator, key, entry.getValue());
        }
        addTypeValidatorFunctions(fullValidator, key, entry.getValue());
    }
    for (final Entry<String, String> entry : getPropertyMap().entrySet()) {
        final String key = entry.getKey();
        if (includeIsA) {
            addIsAFunction(fullValidator, key, entry.getValue());
        }
        addTypeValidatorFunctions(fullValidator, key, entry.getValue());
    }
    return fullValidator;
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType)

Example 10 with IdentifierType

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

the class SchemaElementDefinitionValidator method validateFunctionSelectionTypes.

private boolean validateFunctionSelectionTypes(final SchemaElementDefinition elementDef, 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 Class<?> clazz = elementDef.getClass(key);
        if (null == clazz) {
            final String typeName;
            final IdentifierType idType = IdentifierType.fromName(key);
            if (null == idType) {
                typeName = elementDef.getPropertyTypeName(key);
            } else {
                typeName = elementDef.getIdentifierTypeName(idType);
            }
            if (null != typeName) {
                LOGGER.error("No class type found for type definition " + typeName + " used by " + key + ". Please ensure it is defined in the schema.");
            } else {
                LOGGER.error("No type definition defined for " + key + ". Please ensure it is defined in the schema.");
            }
            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)

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