Skip to content

Commit 4fa9c2d

Browse files
tarilabsetirelli
authored andcommitted
Extend support for Signavio custom List operations functions. (#2)
1 parent 7051d3e commit 4fa9c2d

File tree

9 files changed

+369
-3
lines changed

9 files changed

+369
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.List;
20+
21+
public class AppendAllFunction
22+
extends BaseFEELFunction {
23+
24+
public AppendAllFunction() {
25+
super("appendAll");
26+
}
27+
28+
public FEELFnResult<List> invoke(@ParameterName("list") Object[] lists) {
29+
return BuiltInFunctions.getFunction(ConcatenateFunction.class).invoke(lists);
30+
}
31+
}

kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/BuiltInFunctions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public class BuiltInFunctions {
9999
new YearAddFunction(),
100100
new MonthAddFunction(),
101101
new DayAddFunction(),
102+
new AppendAllFunction(),
103+
new ZipFunction(),
104+
new SignavioNotContainsAnyFunction(),
105+
new SignavioContainsOnlyFunction(),
106+
new SignavioAreElementsOfFunction(),
107+
// TODO uncomment: new SignavioRemoveFunction(),
108+
new SignavioRemoveAllFunction(),
102109

103110
};
104111

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.List;
20+
21+
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
22+
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
23+
24+
public class SignavioAreElementsOfFunction
25+
extends BaseFEELFunction {
26+
27+
public SignavioAreElementsOfFunction() {
28+
super("areElementsOf");
29+
}
30+
31+
public FEELFnResult<Boolean> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
32+
if (list2 == null) {
33+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list2", "cannot be null"));
34+
}
35+
36+
boolean result = true;
37+
for (Object element : list1) {
38+
result = list2.contains(element) && result;
39+
40+
// optimization: terminate early if an element is not actually contained.
41+
if (!result) {
42+
return FEELFnResult.ofResult(result);
43+
}
44+
}
45+
46+
return FEELFnResult.ofResult( result );
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.List;
20+
21+
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
22+
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
23+
24+
public class SignavioContainsOnlyFunction
25+
extends BaseFEELFunction {
26+
27+
public SignavioContainsOnlyFunction() {
28+
super("containsOnly");
29+
}
30+
31+
public FEELFnResult<Boolean> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
32+
if (list1 == null) {
33+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list1", "cannot be null"));
34+
}
35+
36+
boolean result = true;
37+
for (Object element : list2) {
38+
result = list1.contains(element) && result;
39+
40+
// optimization: terminate early if an element is not actually contained.
41+
if (!result) {
42+
return FEELFnResult.ofResult(result);
43+
}
44+
}
45+
46+
return FEELFnResult.ofResult( result );
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.List;
20+
21+
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
22+
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
23+
24+
public class SignavioNotContainsAnyFunction
25+
extends BaseFEELFunction {
26+
27+
public SignavioNotContainsAnyFunction() {
28+
super("notContainsAny");
29+
}
30+
31+
public FEELFnResult<Boolean> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
32+
if (list1 == null) {
33+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list1", "cannot be null"));
34+
}
35+
36+
boolean result = true;
37+
for (Object element : list2) {
38+
result = (!list1.contains(element)) && result;
39+
40+
// optimization: terminate early if any element is actually contained.
41+
if (!result) {
42+
return FEELFnResult.ofResult(result);
43+
}
44+
}
45+
46+
return FEELFnResult.ofResult( result );
47+
}
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
23+
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
24+
25+
public class SignavioRemoveAllFunction
26+
extends BaseFEELFunction {
27+
28+
public SignavioRemoveAllFunction() {
29+
super("removeAll");
30+
}
31+
32+
public FEELFnResult<List> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
33+
if (list1 == null) {
34+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list1", "cannot be null"));
35+
}
36+
37+
// spec requires us to return a new list
38+
List<Object> result = new ArrayList<Object>(list1);
39+
40+
for (Object element : list2) {
41+
result.remove(element);
42+
}
43+
44+
return FEELFnResult.ofResult( result );
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
23+
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
24+
25+
public class SignavioRemoveFunction
26+
extends BaseFEELFunction {
27+
28+
public SignavioRemoveFunction() {
29+
super( "remove" );
30+
}
31+
32+
public FEELFnResult<List> invoke(@ParameterName("list") List list, @ParameterName("element") Object element) {
33+
if ( list == null ) {
34+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
35+
}
36+
if (element == null) {
37+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "element", "cannot be null"));
38+
}
39+
40+
// spec requires us to return a new list
41+
List<Object> result = new ArrayList<Object>( list );
42+
43+
result.remove(element);
44+
45+
return FEELFnResult.ofResult( result );
46+
}
47+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kie.dmn.feel.runtime.functions;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
25+
import org.kie.dmn.feel.runtime.events.InvalidParametersEvent;
26+
27+
public class ZipFunction
28+
extends BaseFEELFunction {
29+
30+
public ZipFunction() {
31+
super("zip");
32+
}
33+
34+
public FEELFnResult<List> invoke(@ParameterName("attributes") List<?> attributes, @ParameterName("values") Object[] values) {
35+
if (attributes.isEmpty()) {
36+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "attributes", "attributes cannot be empty"));
37+
} else if (!(attributes.get(0) instanceof String)) {
38+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "attributes", "attributes must be a list of string"));
39+
}
40+
41+
if (values.length != attributes.size()) {
42+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "values must be a list of the same size as of attributes"));
43+
}
44+
45+
// spec requires us to return a new list
46+
final List<Map<Object, Object>> result = new ArrayList<>();
47+
48+
for (int aIdx = 0; aIdx < values.length; aIdx++) {
49+
if (!(values[aIdx] instanceof List)) {
50+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "each value must be a list"));
51+
}
52+
List<?> value = (List<?>) values[aIdx];
53+
54+
if (result.isEmpty()) {
55+
// first time init list
56+
value.forEach(x -> result.add(new HashMap<>()));
57+
} else {
58+
if (value.size() != result.size()) {
59+
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "each value must be consistent in size"));
60+
}
61+
}
62+
63+
Object attribute = attributes.get(aIdx);
64+
65+
for (int vIdx = 0; vIdx < value.size(); vIdx++) {
66+
result.get(vIdx).put(attribute, value.get(vIdx));
67+
}
68+
}
69+
70+
return FEELFnResult.ofResult(result);
71+
}
72+
}

0 commit comments

Comments
 (0)