Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1344] Refactoring/reorganizing Functions #6005

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static FEEL newInstance(ClassLoader cl, List<FEELProfile> profiles) {
* Evaluates the given compiled FEEL expression using the
* given EvaluationContext, and returns the result
*
* @param expression a FEEL expression
* @param expr a FEEL expression
* @param ctx the EvaluationContext to be used for defining
* input variables and additional feel event listeners
* contextual to this method call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public class ASTTemporalConstantVisitor extends DefaultedVisitor<ASTNode> {
DateAndTimeFunction.INSTANCE,
DurationFunction.INSTANCE,
org.kie.dmn.feel.runtime.functions.extended.TimeFunction.INSTANCE,
org.kie.dmn.feel.runtime.functions.extended.DateFunction.INSTANCE,
org.kie.dmn.feel.runtime.functions.extended.DurationFunction.INSTANCE);
org.kie.dmn.feel.runtime.functions.extended.DateFunction.INSTANCE);
public static final Set<String> TEMPORAL_FNS_NAMES = TEMPORAL_FNS.stream().map(FEELFunction::getName).collect(Collectors.toSet());

public ASTTemporalConstantVisitor(CompilerContext ctx) {
Expand Down Expand Up @@ -200,10 +199,6 @@ private TemporalConstantNode buildTCNodeForDuration(FunctionInvocationNode n, FE
FEELFnResult<TemporalAmount> invoke = DurationFunction.INSTANCE.invoke(p0);
return invoke.cata(e -> null,
v -> new TemporalConstantNode(n, v, DurationFunction.INSTANCE, Collections.singletonList(p0)));
} else if (fn == org.kie.dmn.feel.runtime.functions.extended.DurationFunction.INSTANCE) {
yesamer marked this conversation as resolved.
Show resolved Hide resolved
FEELFnResult<TemporalAmount> invoke = org.kie.dmn.feel.runtime.functions.extended.DurationFunction.INSTANCE.invoke(p0);
return invoke.cata(e -> null,
v -> new TemporalConstantNode(n, v, org.kie.dmn.feel.runtime.functions.extended.DurationFunction.INSTANCE, Collections.singletonList(p0)));
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public interface Range {

static enum RangeBoundary {
enum RangeBoundary {
OPEN, CLOSED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,20 @@ public class BuiltInFunctions {
OverlapsAfterFunction.INSTANCE,
MeetsFunction.INSTANCE,
MetByFunction.INSTANCE,
ListReplaceFunction.INSTANCE
ListReplaceFunction.INSTANCE,
StringJoinFunction.INSTANCE,

NowFunction.INSTANCE,
TodayFunction.INSTANCE,
ContextPutFunction.INSTANCE,
ContextMergeFunction.INSTANCE,
ContextFunction.INSTANCE,
RoundUpFunction.INSTANCE,
RoundDownFunction.INSTANCE,
RoundHalfUpFunction.INSTANCE,
RoundHalfDownFunction.INSTANCE,

RangeFunction.INSTANCE,
};

public static FEELFunction[] getFunctions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* Proposal DMN14-187
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -25,9 +25,6 @@

import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* Proposal DMN14-187
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.util.HashMap;
import java.util.List;
Expand All @@ -27,9 +27,6 @@
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.feel.lang.types.impl.ImmutableFPAWrappingPOJO;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* Proposal DMN14-187
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public FEELFnResult<TemporalAccessor> invoke(@ParameterName( "from" ) String val
try {
return FEELFnResult.ofResult(LocalDate.from(FEEL_DATE.parse(val)));
} catch (DateTimeException e) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "date", e));
return manageDateTimeException(e, val);
}
}

Expand Down Expand Up @@ -99,4 +99,8 @@ public FEELFnResult<TemporalAccessor> invoke(@ParameterName( "from" ) TemporalAc
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "date-parsing exception", e));
}
}

protected FEELFnResult<TemporalAccessor> manageDateTimeException(DateTimeException e, String val) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "date", e));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public FEELFnResult<TemporalAmount> invoke(@ParameterName( "from" ) String val)
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "cannot be null"));
}

if ( val.indexOf("-") > 0) {
yesamer marked this conversation as resolved.
Show resolved Hide resolved
return FEELFnResult.ofError( new InvalidParametersEvent(Severity.ERROR, "from", "negative values for units are not allowed.") );
}

try {
// try to parse as days/hours/minute/seconds
return FEELFnResult.ofResult( Duration.parse( val ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.extended.ContextPutFunction;

public class GetEntriesFunction extends BaseFEELFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.time.ZonedDateTime;
import java.time.temporal.TemporalAccessor;

import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;

public class NowFunction
extends BaseFEELFunction {

public static final NowFunction INSTANCE = new NowFunction();

public NowFunction() {
super( "now" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.chrono.ChronoPeriod;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

import org.antlr.v4.runtime.tree.ParseTree;
import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.FEELDialect;
import org.kie.dmn.feel.lang.ast.*;
import org.kie.dmn.feel.lang.ast.AtLiteralNode;
import org.kie.dmn.feel.lang.ast.BaseNode;
import org.kie.dmn.feel.lang.ast.FunctionInvocationNode;
import org.kie.dmn.feel.lang.ast.NullNode;
import org.kie.dmn.feel.lang.ast.NumberNode;
import org.kie.dmn.feel.lang.ast.StringNode;
import org.kie.dmn.feel.lang.impl.EvaluationContextImpl;
import org.kie.dmn.feel.lang.impl.FEELEventListenersManager;
import org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor;
Expand All @@ -31,24 +50,14 @@
import org.kie.dmn.feel.runtime.Range;
import org.kie.dmn.feel.runtime.Range.RangeBoundary;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;
import org.kie.dmn.feel.runtime.impl.RangeImpl;

import java.time.*;
import java.time.chrono.ChronoPeriod;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

public class RangeFunction extends BaseFEELFunction {

public static final RangeFunction INSTANCE = new RangeFunction();
// Defaulting FEELDialect to FEEL
private static final EvaluationContext STUBBED = new EvaluationContextImpl(Thread.currentThread().getContextClassLoader(), new FEELEventListenersManager(), 0, FEELDialect.FEEL);

private static EvaluationContext STUBBED;


private static final List<Predicate<BaseNode>> ALLOWED_NODES = Arrays.asList(baseNode -> baseNode instanceof NullNode,
baseNode -> baseNode instanceof NumberNode,
Expand Down Expand Up @@ -111,12 +120,12 @@ public FEELFnResult<Range> invoke(@ParameterName("from") String from) {
if (!nodeIsAllowed(rightNode)) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "right endpoint is not a recognised valid literal"));
}
Object left = leftNode.evaluate(STUBBED);
Object left = leftNode.evaluate(getStubbed());
if (!nodeValueIsAllowed(left)) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "left endpoint is not a valid value " + left.getClass()));
}

Object right = rightNode.evaluate(STUBBED);
Object right = rightNode.evaluate(getStubbed());
if (!nodeValueIsAllowed(right)) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "right endpoint is not a valid value " + right.getClass()));
}
Expand Down Expand Up @@ -169,4 +178,13 @@ protected BaseNode parseNotEmptyInput(String input) {
return expr;
}

private EvaluationContext getStubbed() {
if (STUBBED == null) {
// Defaulting FEELDialect to FEEL
STUBBED = new EvaluationContextImpl(Thread.currentThread().getContextClassLoader(),
new FEELEventListenersManager(), 0, FEELDialect.FEEL);
}
return STUBBED;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* provisional access for DMN14-126
*/
public class RoundDownFunction
extends BaseFEELFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* provisional access for DMN14-126
*/
public class RoundHalfDownFunction
extends BaseFEELFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* provisional access for DMN14-126
*/
public class RoundHalfUpFunction
extends BaseFEELFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

/**
* provisional access for DMN14-126
*/
public class RoundUpFunction
extends BaseFEELFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.dmn.feel.runtime.functions.extended;
package org.kie.dmn.feel.runtime.functions;

import java.util.List;
import java.util.StringJoiner;

import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
import org.kie.dmn.feel.runtime.functions.BaseFEELFunction;
import org.kie.dmn.feel.runtime.functions.FEELFnResult;
import org.kie.dmn.feel.runtime.functions.ParameterName;

public class StringJoinFunction extends BaseFEELFunction {

Expand Down
Loading
Loading