Search in sources :

Example 21 with Range

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

the class CompiledFEELSupport method invoke.

public static Object invoke(EvaluationContext feelExprCtx, Object function, Object params) {
    if (function == null) {
        feelExprCtx.notifyEvt(() -> new ASTEventBase(Severity.ERROR, Msg.createMessage(Msg.FUNCTION_NOT_FOUND, function), null));
        return null;
    }
    if (function instanceof FEELFunction) {
        Object[] invocationParams = toFunctionParams(params);
        FEELFunction f = (FEELFunction) function;
        if (function instanceof CompiledCustomFEELFunction) {
            CompiledCustomFEELFunction ff = (CompiledCustomFEELFunction) function;
            if (ff.isProperClosure()) {
                return ff.invokeReflectively(ff.getEvaluationContext(), invocationParams);
            }
        }
        return f.invokeReflectively(feelExprCtx, invocationParams);
    } else if (function instanceof UnaryTest) {
        return ((UnaryTest) function).apply(feelExprCtx, ((List) params).get(0));
    } else if (function instanceof Range) {
        // alignment to FunctionInvocationNode
        List<?> ps = (List<?>) params;
        if (ps.size() == 1) {
            return ((Range) function).includes(ps.get(0));
        } else {
            feelExprCtx.notifyEvt(() -> new ASTEventBase(Severity.ERROR, Msg.createMessage(Msg.CAN_T_INVOKE_AN_UNARY_TEST_WITH_S_PARAMETERS_UNARY_TESTS_REQUIRE_1_SINGLE_PARAMETER, ps.size()), null));
        }
    }
    feelExprCtx.notifyEvt(() -> new ASTEventBase(Severity.ERROR, Msg.createMessage(Msg.CANNOT_INVOKE, MsgUtil.clipToString(function, 50)), null));
    return null;
}
Also used : FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) ASTEventBase(org.kie.dmn.feel.runtime.events.ASTEventBase) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) Range(org.kie.dmn.feel.runtime.Range) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest)

Example 22 with Range

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

the class FEELSchemaEnum method parseNumberAllowedValuesIntoSchema.

public static void parseNumberAllowedValuesIntoSchema(Schema schema, List<DMNUnaryTest> list) {
    // we leverage the property of the *base* FEEL grammar(non visited by ASTVisitor, only the ParseTree->AST Visitor) that `>x` is a Range
    List<Object> uts = evaluateUnaryTests(list);
    if (uts.size() <= 2 && uts.stream().allMatch(o -> o instanceof Range)) {
        // cast intentional.
        Range range = consolidateRanges((List) uts);
        if (range != null) {
            if (range.getLowEndPoint() != null) {
                schema.minimum((BigDecimal) range.getLowEndPoint());
                schema.exclusiveMinimum(range.getLowBoundary() == RangeBoundary.OPEN);
            }
            if (range.getHighEndPoint() != null) {
                schema.maximum((BigDecimal) range.getHighEndPoint());
                schema.exclusiveMaximum(range.getHighBoundary() == RangeBoundary.OPEN);
            }
        }
    } else if (uts.stream().allMatch(o -> o instanceof Number)) {
        schema.enumeration(uts);
    } else {
        LOG.warn("Unable to parse generic allowed value into the JSON Schema for enumeration");
    }
}
Also used : FEEL(org.kie.dmn.feel.FEEL) BigDecimal(java.math.BigDecimal) List(java.util.List) RangeBoundary(org.kie.dmn.feel.runtime.Range.RangeBoundary) UnaryTestImpl(org.kie.dmn.feel.runtime.UnaryTestImpl) Logger(org.slf4j.Logger) Schema(org.eclipse.microprofile.openapi.models.media.Schema) DMNUnaryTest(org.kie.dmn.api.core.DMNUnaryTest) LoggerFactory(org.slf4j.LoggerFactory) RangeImpl(org.kie.dmn.feel.runtime.impl.RangeImpl) Collectors(java.util.stream.Collectors) Range(org.kie.dmn.feel.runtime.Range) Range(org.kie.dmn.feel.runtime.Range)

Example 23 with Range

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

the class FEELSchemaEnumTest method testBasic.

@Test
public void testBasic() {
    List<Range> list = new ArrayList<>();
    list.add(new RangeImpl(RangeBoundary.CLOSED, 0, null, RangeBoundary.CLOSED));
    list.add(new RangeImpl(RangeBoundary.CLOSED, null, 100, RangeBoundary.CLOSED));
    Range result = FEELSchemaEnum.consolidateRanges(list);
    Assertions.assertThat(result).isNotNull().isEqualTo(new RangeImpl(RangeBoundary.CLOSED, 0, 100, RangeBoundary.CLOSED));
}
Also used : ArrayList(java.util.ArrayList) Range(org.kie.dmn.feel.runtime.Range) RangeImpl(org.kie.dmn.feel.runtime.impl.RangeImpl) Test(org.junit.Test)

Aggregations

Range (org.kie.dmn.feel.runtime.Range)23 RangeImpl (org.kie.dmn.feel.runtime.impl.RangeImpl)14 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 DMNContext (org.kie.dmn.api.core.DMNContext)4 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)4 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)4 ChronoPeriod (java.time.chrono.ChronoPeriod)3 TemporalAccessor (java.time.temporal.TemporalAccessor)3 Duration (java.time.Duration)2 ZoneId (java.time.ZoneId)2 LinkedList (java.util.LinkedList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EvaluatorResultImpl (org.kie.dmn.core.ast.EvaluatorResultImpl)2 IterableRange (org.kie.dmn.core.util.IterableRange)2 FEELFunction (org.kie.dmn.feel.runtime.FEELFunction)2 UnaryTest (org.kie.dmn.feel.runtime.UnaryTest)2 IterableRange (org.kie.dmn.trisotech.core.util.IterableRange)2