Search in sources :

Example 6 with EvaluationContext

use of org.kie.dmn.feel.lang.EvaluationContext in project drools by kiegroup.

the class DecisionTableFunction method toDecisionRule.

/**
 * Convert row to DTDecisionRule
 * @param mainCtx the main context is used to identify the hosted FEELEventManager
 * @param embeddedFEEL a possibly cached embedded FEEL to compile the output expression, error will be reported up to the mainCtx
 * @param index
 * @param rule
 * @param inputSize
 * @return
 */
private static DTDecisionRule toDecisionRule(EvaluationContext mainCtx, FEEL embeddedFEEL, int index, List<?> rule, int inputSize) {
    // TODO should be check indeed block of inputSize n inputs, followed by block of outputs.
    DTDecisionRule dr = new DTDecisionRule(index);
    for (int i = 0; i < rule.size(); i++) {
        Object o = rule.get(i);
        if (i < inputSize) {
            dr.getInputEntry().add(toUnaryTest(mainCtx, o));
        } else {
            FEELEventListener ruleListener = event -> mainCtx.notifyEvt(() -> new FEELEventBase(event.getSeverity(), Msg.createMessage(Msg.ERROR_COMPILE_EXPR_DT_FUNCTION_RULE_IDX, index + 1, event.getMessage()), event.getSourceException()));
            embeddedFEEL.addListener(ruleListener);
            CompiledExpression compiledExpression = embeddedFEEL.compile((String) o, embeddedFEEL.newCompilerContext());
            dr.getOutputEntry().add(compiledExpression);
            embeddedFEEL.removeListener(ruleListener);
        }
    }
    return dr;
}
Also used : IntStream(java.util.stream.IntStream) FEEL(org.kie.dmn.feel.FEEL) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) Logger(org.slf4j.Logger) DTOutputClause(org.kie.dmn.feel.runtime.decisiontables.DTOutputClause) HitPolicy(org.kie.dmn.feel.runtime.decisiontables.HitPolicy) LoggerFactory(org.slf4j.LoggerFactory) DTInputClause(org.kie.dmn.feel.runtime.decisiontables.DTInputClause) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Range(org.kie.dmn.feel.runtime.Range) ArrayList(java.util.ArrayList) List(java.util.List) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) Msg(org.kie.dmn.feel.util.Msg) DecisionTableImpl(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl) Collections(java.util.Collections) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 7 with EvaluationContext

use of org.kie.dmn.feel.lang.EvaluationContext 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

EvaluationContext (org.kie.dmn.feel.lang.EvaluationContext)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 UnaryTest (org.kie.dmn.feel.runtime.UnaryTest)4 Msg (org.kie.dmn.feel.util.Msg)4 Collections (java.util.Collections)3 IntStream (java.util.stream.IntStream)3 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)3 Severity (org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity)3 FEEL (org.kie.dmn.feel.FEEL)3 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)3 FEELEventBase (org.kie.dmn.feel.runtime.events.FEELEventBase)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 Map (java.util.Map)2 UUID (java.util.UUID)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2