Skip to content

Commit

Permalink
[incubator-kie-issues#1344] Refactoring/reorganizing Functions (#6005)
Browse files Browse the repository at this point in the history
* [incubator-kie-issues#1344] Rrefactoring/reorganizing Functions

* [incubator-kie-issues#1344] Fixed as per PR suggestion

* [incubator-kie-issues#1344] Hiding constructors from functions, that are singleton by design

* [incubator-kie-issues#1344] Fixing tests

---------

Co-authored-by: Gabriele-Cardosi <[email protected]>
  • Loading branch information
gitgabrio and Gabriele-Cardosi authored Jul 1, 2024
1 parent 4d5262a commit c69dc49
Show file tree
Hide file tree
Showing 173 changed files with 2,265 additions and 2,688 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
public class DMNFeelExpressionEvaluator extends AbstractExpressionEvaluator {

private final ClassLoader classLoader;
private final CodeFunction codeFunction = new CodeFunction();
private final CodeFunction codeFunction = CodeFunction.INSTANCE;

public DMNFeelExpressionEvaluator(ClassLoader classLoader) {
this.classLoader = classLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,19 @@ private Object getAsFEELBuiltinType(Object value, DMNType resultType) {
case UNKNOWN:
return value;
case DATE:
return new DateFunction().invoke((String) value).getOrElseThrow(FailedConversionException::new);
return DateFunction.INSTANCE.invoke((String) value).getOrElseThrow(FailedConversionException::new);
case TIME:
return new TimeFunction().invoke((String) value).getOrElseThrow(FailedConversionException::new);
return TimeFunction.INSTANCE.invoke((String) value).getOrElseThrow(FailedConversionException::new);
case DATE_TIME:
return new DateAndTimeFunction().invoke((String) value).getOrElseThrow(FailedConversionException::new);
return DateAndTimeFunction.INSTANCE.invoke((String) value).getOrElseThrow(FailedConversionException::new);
case BOOLEAN:
return value;
case NUMBER:
return NumberEvalHelper.getBigDecimalOrNull(value);
case STRING:
return value;
case DURATION:
return new DurationFunction().invoke((String) value).getOrElseThrow(FailedConversionException::new);
return DurationFunction.INSTANCE.invoke((String) value).getOrElseThrow(FailedConversionException::new);
default:
throw new IllegalArgumentException();
}
Expand Down
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 @@ -452,12 +452,7 @@ public BlockStmt add(TemporalConstantNode n) {
Class fnClass = fn.getClass();
ClassOrInterfaceType fn_CT = parseClassOrInterfaceType(fnClass.getCanonicalName());
Expression fn_N = new NameExpr(fnClass.getCanonicalName());
if (fnClass.getPackageName().equals(EXTENDED_FUNCTION_PACKAGE)) {
addVariableDeclaratorWithWithFieldAccess(fn_CT, INSTANCE_S, fn_N);
} else {
addVariableDeclaratorWithObjectCreation(fn_CT,
NodeList.nodeList());
}
addVariableDeclaratorWithWithFieldAccess(fn_CT, INSTANCE_S, fn_N);
fnVariableNameExpression = new NameExpr(lastVariableName.get());
} else {
fnVariableNameExpression = new NullLiteralExpr();
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) {
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 @@ -30,7 +30,7 @@ public class AbsFunction
extends BaseFEELFunction {
public static final AbsFunction INSTANCE = new AbsFunction();

AbsFunction() {
private AbsFunction() {
super( "abs" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import java.util.List;

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

Expand All @@ -28,7 +29,7 @@ public class AllFunction

public static final AllFunction INSTANCE = new AllFunction();

public AllFunction() {
private AllFunction() {
super( "all" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import java.util.List;

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

Expand All @@ -28,7 +29,7 @@ public class AnyFunction

public static final AnyFunction INSTANCE = new AnyFunction();

public AnyFunction() {
private AnyFunction() {
super( "any" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

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

Expand All @@ -30,7 +31,7 @@ public class AppendFunction

public static final AppendFunction INSTANCE = new AppendFunction();

public AppendFunction() {
private AppendFunction() {
super( "append" );
}

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 @@ -23,14 +23,13 @@

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.FEELFnResult;

public class CeilingFunction
extends BaseFEELFunction {

public static final CeilingFunction INSTANCE = new CeilingFunction();

public CeilingFunction() {
private CeilingFunction() {
super( "ceiling" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

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

Expand All @@ -29,7 +30,7 @@ public class ConcatenateFunction

public static final ConcatenateFunction INSTANCE = new ConcatenateFunction();

public ConcatenateFunction() {
private ConcatenateFunction() {
super( "concatenate" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ContainsFunction

public static final ContainsFunction INSTANCE = new ContainsFunction();

public ContainsFunction() {
private ContainsFunction() {
super( "contains" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,21 @@
* 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
* Experimental for DMN14-183, an inverse of `get entries()`
* See also: DMN14-181, DMN14-182
*/

public class ContextFunction extends BaseFEELFunction {

public static final ContextFunction INSTANCE = new ContextFunction();

public ContextFunction() {
private ContextFunction() {
super("context");
}

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,20 +25,12 @@

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
* Experimental for DMN14-182
* See also: DMN14-181, DMN14-183
*/
public class ContextMergeFunction extends BaseFEELFunction {

public static final ContextMergeFunction INSTANCE = new ContextMergeFunction();

public ContextMergeFunction() {
private ContextMergeFunction() {
super("context merge");
}

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,20 +27,13 @@
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
* Experimental for DMN14-181
* See also: DMN14-182, DMN14-183
*/

public class ContextPutFunction extends BaseFEELFunction {

public static final ContextPutFunction INSTANCE = new ContextPutFunction();

public ContextPutFunction() {
private ContextPutFunction() {
super("context put");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@

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.FEELFnResult;

public class CountFunction
extends BaseFEELFunction {

public static final CountFunction INSTANCE = new CountFunction();

public CountFunction() {
private CountFunction() {
super( "count" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class DateAndTimeFunction
.toFormatter();
}

public DateAndTimeFunction() {
private DateAndTimeFunction() {
super(FEELConversionFunctionNames.DATE_AND_TIME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class DateFunction
.withResolverStyle(ResolverStyle.STRICT);
}

public DateFunction() {
protected DateFunction() {
super(FEELConversionFunctionNames.DATE);
}

Expand All @@ -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 @@ -29,7 +29,7 @@
public class DayOfWeekFunction extends BaseFEELFunction {
public static final DayOfWeekFunction INSTANCE = new DayOfWeekFunction();

DayOfWeekFunction() {
private DayOfWeekFunction() {
super("day of week");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class DayOfYearFunction extends BaseFEELFunction {
public static final DayOfYearFunction INSTANCE = new DayOfYearFunction();

DayOfYearFunction() {
private DayOfYearFunction() {
super("day of year");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@

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.FEELFnResult;

public class DecimalFunction
extends BaseFEELFunction {

public static final DecimalFunction INSTANCE = new DecimalFunction();

public DecimalFunction() {
private DecimalFunction() {
super( "decimal" );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class DecisionTableFunction

private static final Logger LOG = LoggerFactory.getLogger( DecisionTableFunction.class );

public DecisionTableFunction() {
private DecisionTableFunction() {
super( "decision table" );
}

Expand Down
Loading

0 comments on commit c69dc49

Please sign in to comment.