Skip to content

Commit

Permalink
[kie-issues#1330] FEEL functions that expect List parameters, should …
Browse files Browse the repository at this point in the history
…coerce single item to a List (apache#5997)

* Fix + Tests

* Fix + Tests

* Fix + Tests

* Tests fixed

* Fix + Tests

* Fix + Tests

* Fix

* Revert

* Fix

* Fix

* Improving scoring

* BaseFEELFunction updated

* BaseFEELFunction updated

* Revert context function

* context function

* Revert some changes + Tests

* Change
  • Loading branch information
yesamer authored and rgdoliveira committed Jul 17, 2024
1 parent c9ec2eb commit 2a41c61
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public FEELFnResult<Boolean> invoke(@ParameterName( "list" ) List list) {
}
}

public FEELFnResult<Boolean> invoke(@ParameterName( "list" ) Boolean single) {
return FEELFnResult.ofResult( single );
}

public FEELFnResult<Boolean> invoke(@ParameterName( "b" ) Object[] list) {
if ( list == null ) {
// Arrays.asList does not accept null as parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public FEELFnResult<Boolean> invoke(@ParameterName( "list" ) List list) {
}
}

public FEELFnResult<Boolean> invoke(@ParameterName( "list" ) Boolean single) {
return FEELFnResult.ofResult( single );
}

public FEELFnResult<Boolean> invoke(@ParameterName( "b" ) Object[] list) {
if ( list == null ) {
// Arrays.asList does not accept null as parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) List list) {

FEELFnResult<BigDecimal> s = sum.invoke( list );

Function<FEELEvent, FEELFnResult<BigDecimal>> ifLeft = (event) -> {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "unable to sum the elements which is required to calculate the mean"));
};
Function<FEELEvent, FEELFnResult<BigDecimal>> ifLeft = event ->
FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "unable to sum the elements which is required to calculate the mean"));

Function<BigDecimal, FEELFnResult<BigDecimal>> ifRight = (sum) -> {
try {
Expand All @@ -62,22 +61,6 @@ public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) List list) {
return s.cata(ifLeft, ifRight);
}

public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) Number single) {
if ( single == null ) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "single", "the single value list cannot be null"));
}

if( single instanceof BigDecimal ) {
return FEELFnResult.ofResult((BigDecimal) single );
}
BigDecimal result = NumberEvalHelper.getBigDecimalOrNull(single );
if ( result != null ) {
return FEELFnResult.ofResult( result );
} else {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list is not a number"));
}
}

public FEELFnResult<BigDecimal> invoke(@ParameterName( "n" ) Object[] list) {
if ( list == null ) {
// Arrays.asList does not accept null as parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,6 @@ public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List list) {
return FEELFnResult.ofResult( product );
}

public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single) {
if ( single == null ) {
// Arrays.asList does not accept null as parameter
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the single value list cannot be null"));
}

if( single instanceof BigDecimal ) {
return FEELFnResult.ofResult((BigDecimal) single );
}
BigDecimal result = NumberEvalHelper.getBigDecimalOrNull( single );
if ( result != null ) {
return FEELFnResult.ofResult( result );
} else {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list not a number"));
}
}

public FEELFnResult<BigDecimal> invoke(@ParameterName("n") Object[] list) {
if ( list == null ) {
// Arrays.asList does not accept null as parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@ public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List<?> list) {
return FEELFnResult.ofResult( SqrtFunction.sqrt( mean ) );
}

public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Object sole) {
if ( sole == null ) {
// Arrays.asList does not accept null as parameter
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "the single value list cannot be null"));
} else if (NumberEvalHelper.getBigDecimalOrNull(sole) == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list",
"the value can not be converted to a number"));
}
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list",
"sample standard deviation of a single sample is undefined"));
}

public FEELFnResult<BigDecimal> invoke(@ParameterName("n") Object[] list) {
if ( list == null ) {
// Arrays.asList does not accept null as parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,6 @@ public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List list) {
return FEELFnResult.ofResult( sum );
}

public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single) {
if ( single == null ) {
// Arrays.asList does not accept null as parameter
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the single value list cannot be null"));
}

if( single instanceof BigDecimal ) {
return FEELFnResult.ofResult((BigDecimal) single );
}
BigDecimal result = NumberEvalHelper.getBigDecimalOrNull( single );
if ( result != null ) {
return FEELFnResult.ofResult( result );
} else {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list not a number"));
}
}

public FEELFnResult<BigDecimal> invoke(@ParameterName("n") Object[] list) {
if ( list == null ) {
// Arrays.asList does not accept null as parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ private static Collection<Object[]> data() {
{ "stddev( [ 47 ] )", null, FEELEvent.Severity.ERROR },
{ "stddev( 47 )", null, FEELEvent.Severity.ERROR },
{ "stddev( [ ] )", null, FEELEvent.Severity.ERROR },
{"mode( 6, 3, 9, 6, 6 )", List.of(BigDecimal.valueOf(6)), null },
{ "mode( 6 )", List.of(BigDecimal.valueOf(6)), null },
{ "mode( 6, 3, 9, 6, 6 )", List.of(BigDecimal.valueOf(6)), null },
{ "mode( [6, 1, 9, 6, 1] )", Arrays.asList(BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 6 ) ), null },
{"mode( [ ] )", List.of(), null },
{ "mode( [ ] )", List.of(), null },
};
return addAdditionalParameters(cases, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,37 @@ private static Collection<Object[]> data() {
{ "replace(\"0123456789\",\"(\\d{3})(\\d{3})(\\d{4})\",\"($1) $2-$3\")", "(012) 345-6789" , null},
{ "list contains([1, 2, 3], 2)", Boolean.TRUE , null},
{ "list contains([1, 2, 3], 5)", Boolean.FALSE , null},
{ "list contains(1, 1)", Boolean.TRUE , null},
{ "list contains(2, 1)", Boolean.FALSE , null},
{ "count( 1 )", BigDecimal.valueOf( 1 ) , null},
{ "count([1, 2, 3])", BigDecimal.valueOf( 3 ) , null},
{ "count( 1, 2, 3 )", BigDecimal.valueOf( 3 ) , null},
{ "min( \"a\" )", "a" , null},
{ "min( \"a\", \"b\", \"c\" )", "a" , null},
{ "min([ \"a\", \"b\", \"c\" ])", "a" , null},
{ "max( 1 )", BigDecimal.valueOf( 1 ) , null},
{ "max( 1, 2, 3 )", BigDecimal.valueOf( 3 ) , null},
{ "max([ 1, 2, 3 ])", BigDecimal.valueOf( 3 ) , null},
{ "max(duration(\"PT1H6M\"), duration(\"PT1H5M\"))", Duration.parse("PT1H6M"), null},
{ "max(duration(\"P6Y\"), duration(\"P5Y\"))", ComparablePeriod.parse("P6Y"), null},
{ "sum( 1 )", BigDecimal.valueOf( 1 ) , null},
{ "sum( [null] )", null , FEELEvent.Severity.ERROR},
{ "sum( null )", null , FEELEvent.Severity.ERROR},
{ "sum( 1, 2, 3 )", BigDecimal.valueOf( 6 ) , null},
{ "sum([ 1, 2, 3 ])", BigDecimal.valueOf( 6 ) , null},
{ "sum([])", null, null},
{ "product( [2, 3, 4] )", BigDecimal.valueOf( 24 ) , null},
{ "product( 2, 3, 4 )", BigDecimal.valueOf( 24 ) , null},
{ "product( 1 )", BigDecimal.valueOf( 1 ) , null},
{ "product( [null] )", null , FEELEvent.Severity.ERROR},
{ "product( null )", null , FEELEvent.Severity.ERROR},
{ "product([ 2, 3, 4 ])", BigDecimal.valueOf( 24 ) , null},
{ "product([])", null, FEELEvent.Severity.ERROR},
{ "mean( [1, 2, 3] )", BigDecimal.valueOf( 2 ) , null},
{ "mean( 1, 2, 3 )", BigDecimal.valueOf( 2 ) , null},
{ "mean( 2 )", BigDecimal.valueOf( 2 ) , null},
{ "mean( [null] )", null , FEELEvent.Severity.ERROR},
{ "mean( null )", null , FEELEvent.Severity.ERROR},
{ "mean([ 1, 2, 3 ])", BigDecimal.valueOf( 2 ) , null},
{ "sublist( [1, 2, 3, 4, 5 ], 3, 2 )", Arrays.asList( BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ), null},
{ "sublist( [1, 2, 3, 4, 5 ], -2, 1 )", Collections.singletonList(BigDecimal.valueOf(4)), null},
Expand All @@ -114,16 +130,21 @@ private static Collection<Object[]> data() {
{ "sublist( [1, 2, 3, 4, 5 ], -6, 3 )", null , FEELEvent.Severity.ERROR},
{ "sublist( [1, 2, 3, 4, 5 ], -5, 3 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ) ) , null},
{ "sublist( [1, 2, 3, 4, 5 ], 1, 3 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ) ) , null},
{ "sublist( 1, 1, 1 )", Arrays.asList( BigDecimal.valueOf( 1 ) ) , null},
{ "sublist( 1, 1 )", Arrays.asList( BigDecimal.valueOf( 1 ) ) , null},
{ "append( [1, 2], 3, 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "append( [], 3, 4 )", Arrays.asList( BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "append( 1, 3, 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "append( [1, 2] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ) ) , null},
{ "append( [1, 2], null, 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), null, BigDecimal.valueOf( 4 ) ) , null},
{ "append( null, 1, 2 )", null , FEELEvent.Severity.ERROR},
{ "append( 0, 1, 2 )", Arrays.asList( BigDecimal.valueOf( 0 ), BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ) ), null},
{ "concatenate( [1, 2], [3] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ) ) , null},
{ "concatenate( 1, [3] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 3 ) ) , null},
{ "concatenate( [1, 2], 3, [4] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "concatenate( [1, 2], null )", null , FEELEvent.Severity.ERROR},
{ "insert before( [1, 2, 3], 1, 4 )", Arrays.asList( BigDecimal.valueOf( 4 ), BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ) ) , null},
{ "insert before( 1, 1, 4 )", Arrays.asList( BigDecimal.valueOf( 4 ), BigDecimal.valueOf( 1 ) ) , null},
{ "insert before( [1, 2, 3], 3, 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 4 ), BigDecimal.valueOf( 3 ) ) , null},
{ "insert before( [1, 2, 3], 3, null )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), null, BigDecimal.valueOf( 3 ) ) , null},
{ "insert before( null, 3, 4 )", null , FEELEvent.Severity.ERROR},
Expand All @@ -133,21 +154,28 @@ private static Collection<Object[]> data() {
{ "insert before( [1, 2, 3], 0, 4 )", null , FEELEvent.Severity.ERROR},
{ "insert before( [1, 2, 3], -4, 4 )", null , FEELEvent.Severity.ERROR},
{ "remove( [1, 2, 3], 1 )", Arrays.asList( BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ) ) , null},
{ "remove( 1, 1 )", Arrays.asList() , null},
{ "remove( [1, 2, 3], 3 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ) ) , null},
{ "remove( [1, 2, 3], -1 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ) ) , null},
{ "remove( [1, 2, 3], -3 )", Arrays.asList( BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ) ) , null},
{ "remove( [1, 2, 3], 4 )", null , FEELEvent.Severity.ERROR},
{ "remove( [1, 2, 3], -4 )", null , FEELEvent.Severity.ERROR},
{ "remove( [1, 2, 3], 0 )", null , FEELEvent.Severity.ERROR},
{ "reverse( [1, 2, 3] )", Arrays.asList( BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 1 ) ) , null},
{ "reverse( 1 )", Arrays.asList( BigDecimal.valueOf( 1 ) ) , null},
{ "reverse( null )", null , FEELEvent.Severity.ERROR},
{ "index of( 1, 1 )", Arrays.asList( BigDecimal.valueOf( 1 ) ) , null},
{ "index of( [1, 2, 3, 2], 2 )", Arrays.asList( BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 4 ) ) , null},
{ "index of( [1, 2, null, null], null )", Arrays.asList( BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "index of( [1, 2, null, null], 1 )", Collections.singletonList(BigDecimal.valueOf(1)), null},
{ "index of( null, 1 )", null , FEELEvent.Severity.ERROR},
{ "union( 1, [2, 3], 2, 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "union( 1 )", Arrays.asList( BigDecimal.valueOf( 1 ) ) , null},
{ "union( [1, 2, 1], [2, 3], 2, 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "union( [1, 2, null], 4 )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), null, BigDecimal.valueOf( 4 ) ) , null},
{ "union( null, 4 )", Arrays.asList( null, BigDecimal.valueOf(4) ), null},
{ "flatten( [[1,2],[[3]], 4] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ), null},
{ "flatten( 1 )", Arrays.asList( BigDecimal.valueOf( 1 ) ), null},
{ "distinct values( [1, 2, 3, 2, 4] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), BigDecimal.valueOf( 3 ), BigDecimal.valueOf( 4 ) ) , null},
{ "distinct values( [1, 2, null, 2, 4] )", Arrays.asList( BigDecimal.valueOf( 1 ), BigDecimal.valueOf( 2 ), null, BigDecimal.valueOf( 4 ) ) , null},
{ "distinct values( 1 )", Collections.singletonList(BigDecimal.valueOf(1)), null},
Expand Down Expand Up @@ -240,6 +268,7 @@ private static Collection<Object[]> data() {
{ "week of year( date(2005, 1, 1) )", BigDecimal.valueOf( 53 ), null},
{ "median( 8, 2, 5, 3, 4 )", new BigDecimal("4") , null},
{ "median( [6, 1, 2, 3] )", new BigDecimal("2.5") , null},
{ "median( 1 )", new BigDecimal("1") , null},
{ "median( [ ] ) ", null, null}, // DMN spec, Table 69: Semantics of list functions

{ "0-max( 1, 2, 3 )", BigDecimal.valueOf( -3 ) , null},
Expand All @@ -264,6 +293,8 @@ private static Collection<Object[]> data() {
{ "if list contains ([2.2, 3, 4], 3.000) then \"OK\" else \"NOT_OK\"", "OK" , null},
{"list replace ( null, 3, 6)", null , FEELEvent.Severity.ERROR},
{"list replace ( [2, 4, 7, 8], null, 6)", null , FEELEvent.Severity.ERROR},
{"list replace ( [1], 1, 7)", Arrays.asList(BigDecimal.valueOf(7)), null},
{"list replace ( 1, 1, 7)", Arrays.asList(BigDecimal.valueOf(7)), null},
{"list replace ( [2, 4, 7, 8], 3, 6)", Arrays.asList(BigDecimal.valueOf(2), BigDecimal.valueOf(4), BigDecimal.valueOf(6), BigDecimal.valueOf(8)), null},
{"list replace ( [2, 4, 7, 8], -3, 6)", Arrays.asList(BigDecimal.valueOf(2), BigDecimal.valueOf(6), BigDecimal.valueOf(7), BigDecimal.valueOf(8)), null},
{"list replace ( [2, 4, 7, 8], function(item, newItem) item + newItem, 6)", null , FEELEvent.Severity.ERROR},
Expand Down
Loading

0 comments on commit 2a41c61

Please sign in to comment.