Search in sources :

Example 1 with Signature

use of uk.gov.gchq.koryphe.signature.Signature in project Gaffer by gchq.

the class GraphConfigurationService method getFilterFunctions.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Set<Class> getFilterFunctions(final String inputClass) {
    if (StringUtils.isEmpty(inputClass)) {
        return getFilterFunctions();
    }
    final Class<?> clazz;
    try {
        clazz = Class.forName(SimpleClassNameIdResolver.getClassName(inputClass));
    } catch (final Exception e) {
        throw new IllegalArgumentException("Input class was not recognised: " + inputClass, e);
    }
    final Set<Class> classes = new HashSet<>();
    for (final Class functionClass : ReflectionUtil.getSubTypes(Predicate.class)) {
        try {
            final Predicate function = (Predicate) functionClass.newInstance();
            final Signature signature = Signature.getInputSignature(function);
            if (null == signature.getNumClasses() || (1 == signature.getNumClasses() && (Signature.UnknownGenericType.class.isAssignableFrom(signature.getClasses()[0]) || signature.getClasses()[0].isAssignableFrom(clazz)))) {
                classes.add(functionClass);
            }
        } catch (final Exception e) {
            // just add the function.
            classes.add(functionClass);
        }
    }
    return classes;
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) HashSet(java.util.HashSet) Predicate(java.util.function.Predicate) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with Signature

use of uk.gov.gchq.koryphe.signature.Signature in project Gaffer by gchq.

the class GraphConfigurationServiceV2 method getFilterFunction.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Response getFilterFunction(final String inputClass) {
    if (StringUtils.isEmpty(inputClass)) {
        return getFilterFunction();
    }
    final Class<?> clazz;
    try {
        clazz = Class.forName(SimpleClassNameIdResolver.getClassName(inputClass));
    } catch (final Exception e) {
        throw new IllegalArgumentException("Input class was not recognised: " + inputClass, e);
    }
    final Set<Class> classes = new HashSet<>();
    for (final Class functionClass : ReflectionUtil.getSubTypes(Predicate.class)) {
        try {
            final Predicate function = (Predicate) functionClass.newInstance();
            final Signature signature = Signature.getInputSignature(function);
            if (null == signature.getNumClasses() || (1 == signature.getNumClasses() && (Signature.UnknownGenericType.class.isAssignableFrom(signature.getClasses()[0]) || signature.getClasses()[0].isAssignableFrom(clazz)))) {
                classes.add(functionClass);
            }
        } catch (final Exception e) {
            // just add the function.
            classes.add(functionClass);
        }
    }
    return Response.ok(classes).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) HashSet(java.util.HashSet) Predicate(java.util.function.Predicate) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with Signature

use of uk.gov.gchq.koryphe.signature.Signature in project Gaffer by gchq.

the class GraphConfigurationController method getFilterFunctions.

@Override
public Set<Class> getFilterFunctions(@PathVariable("inputClass") final String inputClass) {
    if (StringUtils.isEmpty(inputClass)) {
        return getFilterFunctions();
    }
    final Class<?> clazz;
    try {
        clazz = Class.forName(SimpleClassNameIdResolver.getClassName(inputClass));
    } catch (final ClassNotFoundException e) {
        throw new IllegalArgumentException("Could not find input class: " + inputClass, e);
    }
    final Set<Class> applicablePredicates = new HashSet<>();
    for (final Class predicateClass : ReflectionUtil.getSubTypes(Predicate.class)) {
        Predicate predicate;
        try {
            predicate = (Predicate) predicateClass.newInstance();
        } catch (final IllegalAccessException | InstantiationException e) {
            LOGGER.warn("Failed to create new instance of " + predicateClass, e);
            LOGGER.warn("Skipping");
            continue;
        }
        Signature inputSignature = Signature.getInputSignature(predicate);
        if (null == inputSignature.getNumClasses() || (1 == inputSignature.getNumClasses() && (Signature.UnknownGenericType.class.isAssignableFrom(inputSignature.getClasses()[0]) || inputSignature.getClasses()[0].isAssignableFrom(clazz)))) {
            applicablePredicates.add(predicateClass);
        }
    }
    return applicablePredicates;
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) HashSet(java.util.HashSet) Predicate(java.util.function.Predicate)

Example 4 with Signature

use of uk.gov.gchq.koryphe.signature.Signature in project Gaffer by gchq.

the class SchemaElementDefinitionValidator method validateFunctionArgumentTypes.

protected ValidationResult validateFunctionArgumentTypes(final ElementAggregator aggregator, final SchemaElementDefinition schemaElDef) {
    final ValidationResult result = new ValidationResult();
    if (null != aggregator && null != aggregator.getComponents()) {
        for (final TupleAdaptedBinaryOperator<String, ?> adaptedFunction : aggregator.getComponents()) {
            if (null == adaptedFunction.getBinaryOperator()) {
                result.addError(aggregator.getClass().getSimpleName() + " contains a null function.");
            } else {
                final Signature inputSig = Signature.getInputSignature(adaptedFunction.getBinaryOperator());
                result.add(inputSig.assignable(getTypeClasses(adaptedFunction.getSelection(), schemaElDef)));
                final Signature outputSig = Signature.getOutputSignature(adaptedFunction.getBinaryOperator());
                result.add(outputSig.assignable(getTypeClasses(adaptedFunction.getSelection(), schemaElDef)));
            }
        }
    }
    return result;
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 5 with Signature

use of uk.gov.gchq.koryphe.signature.Signature in project Gaffer by gchq.

the class ViewValidator method validateFunctionArgumentTypes.

private ValidationResult validateFunctionArgumentTypes(final ElementAggregator aggregator, final ViewElementDefinition viewElDef, final SchemaElementDefinition schemaElDef) {
    final ValidationResult result = new ValidationResult();
    if (null != aggregator && null != aggregator.getComponents()) {
        for (final TupleAdaptedBinaryOperator<String, ?> adaptedFunction : aggregator.getComponents()) {
            if (null == adaptedFunction.getBinaryOperator()) {
                result.addError(aggregator.getClass().getSimpleName() + " contains a null function.");
            } else {
                final Class[] inputTypeClasses = getTypeClasses(adaptedFunction.getSelection(), viewElDef, schemaElDef);
                if (!ArrayUtils.contains(inputTypeClasses, null)) {
                    final Signature inputSig = Signature.getInputSignature(adaptedFunction.getBinaryOperator());
                    result.add(inputSig.assignable(inputTypeClasses));
                }
            }
        }
    }
    return result;
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Aggregations

Signature (uk.gov.gchq.koryphe.signature.Signature)13 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)8 Predicate (java.util.function.Predicate)4 HashSet (java.util.HashSet)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 ArrayList (java.util.ArrayList)2 TupleAdaptedBinaryOperator (uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator)2 TupleAdaptedPredicate (uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate)2 BinaryOperator (java.util.function.BinaryOperator)1 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)1 OperationException (uk.gov.gchq.gaffer.operation.OperationException)1 AdaptedBinaryOperator (uk.gov.gchq.koryphe.binaryoperator.AdaptedBinaryOperator)1 AdaptedPredicate (uk.gov.gchq.koryphe.predicate.AdaptedPredicate)1 TupleAdaptedFunction (uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction)1 IntegerTupleAdaptedPredicate (uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate)1