Search in sources :

Example 1 with CustomFEELFunction

use of org.kie.dmn.feel.runtime.functions.CustomFEELFunction in project drools by kiegroup.

the class FunctionDefNode method evaluate.

@Override
public Object evaluate(EvaluationContext ctx) {
    List<String> params = formalParameters.stream().map(p -> p.evaluate(ctx)).collect(Collectors.toList());
    if (external) {
        try {
            // creating a simple algorithm to find the method in java
            // without using any external libraries in this initial implementation
            Map<String, Object> conf = (Map<String, Object>) this.body.evaluate(ctx);
            Map<String, Object> java = (Map<String, Object>) conf.get("java");
            if (java != null) {
                // this is a java function
                String clazzName = (String) java.get("class");
                String methodSignature = (String) java.get("method signature");
                if (clazzName != null && methodSignature != null) {
                    // might need to explicitly use a classloader here
                    Class<?> clazz = Class.forName(clazzName);
                    if (clazz != null) {
                        String[] mp = parseMethod(methodSignature);
                        if (mp != null) {
                            String methodName = mp[0];
                            String[] paramTypeNames = parseParams(mp[1]);
                            int numberOfParams = paramTypeNames.length;
                            if (numberOfParams == params.size()) {
                                Class[] paramTypes = new Class[numberOfParams];
                                for (int i = 0; i < numberOfParams; i++) {
                                    paramTypes[i] = getType(paramTypeNames[i]);
                                }
                                Method method = clazz.getMethod(methodName, paramTypes);
                                return new JavaFunction(ANONYMOUS, params, clazz, method);
                            } else {
                                ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.PARAMETER_COUNT_MISMATCH_ON_FUNCTION_DEFINITION, getText())));
                                return null;
                            }
                        }
                    }
                }
            }
            ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.UNABLE_TO_FIND_EXTERNAL_FUNCTION_AS_DEFINED_BY, getText())));
        } catch (Exception e) {
            ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, getText()), e));
        }
        return null;
    } else {
        return new CustomFEELFunction(ANONYMOUS, params, body);
    }
}
Also used : Severity(org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) JavaFunction(org.kie.dmn.feel.runtime.functions.JavaFunction) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Matcher(java.util.regex.Matcher) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) Msg(org.kie.dmn.feel.util.Msg) Map(java.util.Map) CustomFEELFunction(org.kie.dmn.feel.runtime.functions.CustomFEELFunction) Pattern(java.util.regex.Pattern) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) Type(org.kie.dmn.feel.lang.Type) Method(java.lang.reflect.Method) JavaFunction(org.kie.dmn.feel.runtime.functions.JavaFunction) Method(java.lang.reflect.Method) CustomFEELFunction(org.kie.dmn.feel.runtime.functions.CustomFEELFunction) Map(java.util.Map)

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 Severity (org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity)1 EvaluationContext (org.kie.dmn.feel.lang.EvaluationContext)1 Type (org.kie.dmn.feel.lang.Type)1 BuiltInType (org.kie.dmn.feel.lang.types.BuiltInType)1 CustomFEELFunction (org.kie.dmn.feel.runtime.functions.CustomFEELFunction)1 JavaFunction (org.kie.dmn.feel.runtime.functions.JavaFunction)1 Msg (org.kie.dmn.feel.util.Msg)1