Skip to content

Commit

Permalink
[incubator-kie-issues#506] DMN - Add support for iteration of date ra…
Browse files Browse the repository at this point in the history
…nges (apache#5626)

* [incubator-kie-issues#506] Implemented Date iteration. Refactored to remove duplicated code and allow easy extension of use cases

* [incubator-kie-issues#506] WIP

* [incubator-kie-issues#506] Update with main. Add unit tests

* [incubator-kie-issues#506] WIP

* [incubator-kie-issues#506] Restored code-level 11 syntax

* [incubator-kie-issues#506] Merged with main

* [incubator-kie-issues#506] Fix as per PR suggestion

---------

Co-authored-by: BAMOE CI <bamoe.ci@ibm.com>
  • Loading branch information
2 people authored and rgdoliveira committed Jan 16, 2024
1 parent 2ee1a59 commit f6d6a50
Show file tree
Hide file tree
Showing 21 changed files with 1,050 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
*/
package org.kie.dmn.feel.codegen.feel11;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import ch.obermuhlner.math.big.BigDecimalMath;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
Expand All @@ -46,12 +36,14 @@
import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.api.feel.runtime.events.FEELEventListener;
import org.kie.dmn.feel.exceptions.EndpointOfRangeNotValidTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfRangeOfDifferentTypeException;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.ast.ForExpressionNode;
import org.kie.dmn.feel.lang.ast.ForExpressionNode.ForIteration;
import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode;
import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode.QEIteration;
import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode.Quantifier;
import org.kie.dmn.feel.lang.ast.forexpressioniterators.ForIteration;
import org.kie.dmn.feel.lang.impl.SilentWrappingEvaluationContextImpl;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.feel.runtime.FEELFunction;
Expand All @@ -64,8 +56,19 @@
import org.kie.dmn.feel.util.Msg;
import org.kie.dmn.feel.util.MsgUtil;

import java.math.BigDecimal;
import java.math.MathContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;
import static org.kie.dmn.feel.codegen.feel11.Expressions.compiledFeelSemanticMappingsFQN;
import static org.kie.dmn.feel.lang.ast.forexpressioniterators.ForIterationUtils.getForIteration;

public class CompiledFEELSupport {

Expand Down Expand Up @@ -265,7 +268,7 @@ public Object rreturn(Function<EvaluationContext, Object> expression) {
ctx.exitFrame(); // last i-th scope unrolled, see also ForExpressionNode.nextIteration(...)
}
return results;
} catch (EndpointOfRangeNotOfNumberException e) {
} catch (EndpointOfRangeNotValidTypeException | EndpointOfRangeOfDifferentTypeException e) {
// ast error already reported
return null;
} finally {
Expand Down Expand Up @@ -296,26 +299,11 @@ private ForIteration createQuantifiedExpressionIterationContext(EvaluationContex
Iterable values = result instanceof Iterable ? (Iterable) result : Collections.singletonList(result);
fi = new ForIteration(name, values);
} else {
valueMustBeANumber(ctx, result);
BigDecimal start = (BigDecimal) result;
valueMustBeANumber(ctx, rangeEnd);
BigDecimal end = (BigDecimal) rangeEnd;
fi = new ForIteration(name, start, end);
fi = getForIteration(ctx, name, result, rangeEnd);
}
return fi;
}

private void valueMustBeANumber(EvaluationContext ctx, Object value) {
if (!(value instanceof BigDecimal)) {
ctx.notifyEvt(() -> new ASTEventBase(Severity.ERROR, Msg.createMessage(Msg.VALUE_X_NOT_A_VALID_ENDPOINT_FOR_RANGE_BECAUSE_NOT_A_NUMBER, value), null));
throw new EndpointOfRangeNotOfNumberException();
}
}

private static class EndpointOfRangeNotOfNumberException extends RuntimeException {

private static final long serialVersionUID = 1L;
}
}

public static class IterationContextCompiled {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.exceptions;

public class EndpointOfRangeNotOfNumberException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.exceptions;

public class EndpointOfRangeNotValidTypeException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.exceptions;

public class EndpointOfRangeOfDifferentTypeException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
Loading

0 comments on commit f6d6a50

Please sign in to comment.