Skip to content

Commit

Permalink
Extend support for Signavio custom List operations functions. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarilabs authored and etirelli committed Oct 5, 2017
1 parent 7051d3e commit 4fa9c2d
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

import java.util.List;

public class AppendAllFunction
extends BaseFEELFunction {

public AppendAllFunction() {
super("appendAll");
}

public FEELFnResult<List> invoke(@ParameterName("list") Object[] lists) {
return BuiltInFunctions.getFunction(ConcatenateFunction.class).invoke(lists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public class BuiltInFunctions {
new YearAddFunction(),
new MonthAddFunction(),
new DayAddFunction(),
new AppendAllFunction(),
new ZipFunction(),
new SignavioNotContainsAnyFunction(),
new SignavioContainsOnlyFunction(),
new SignavioAreElementsOfFunction(),
// TODO uncomment: new SignavioRemoveFunction(),
new SignavioRemoveAllFunction(),

};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

import java.util.List;

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

public class SignavioAreElementsOfFunction
extends BaseFEELFunction {

public SignavioAreElementsOfFunction() {
super("areElementsOf");
}

public FEELFnResult<Boolean> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
if (list2 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list2", "cannot be null"));
}

boolean result = true;
for (Object element : list1) {
result = list2.contains(element) && result;

// optimization: terminate early if an element is not actually contained.
if (!result) {
return FEELFnResult.ofResult(result);
}
}

return FEELFnResult.ofResult( result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

import java.util.List;

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

public class SignavioContainsOnlyFunction
extends BaseFEELFunction {

public SignavioContainsOnlyFunction() {
super("containsOnly");
}

public FEELFnResult<Boolean> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
if (list1 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list1", "cannot be null"));
}

boolean result = true;
for (Object element : list2) {
result = list1.contains(element) && result;

// optimization: terminate early if an element is not actually contained.
if (!result) {
return FEELFnResult.ofResult(result);
}
}

return FEELFnResult.ofResult( result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

import java.util.List;

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

public class SignavioNotContainsAnyFunction
extends BaseFEELFunction {

public SignavioNotContainsAnyFunction() {
super("notContainsAny");
}

public FEELFnResult<Boolean> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
if (list1 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list1", "cannot be null"));
}

boolean result = true;
for (Object element : list2) {
result = (!list1.contains(element)) && result;

// optimization: terminate early if any element is actually contained.
if (!result) {
return FEELFnResult.ofResult(result);
}
}

return FEELFnResult.ofResult( result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

import java.util.ArrayList;
import java.util.List;

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

public class SignavioRemoveAllFunction
extends BaseFEELFunction {

public SignavioRemoveAllFunction() {
super("removeAll");
}

public FEELFnResult<List> invoke(@ParameterName("list1") List list1, @ParameterName("list2") List list2) {
if (list1 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list1", "cannot be null"));
}

// spec requires us to return a new list
List<Object> result = new ArrayList<Object>(list1);

for (Object element : list2) {
result.remove(element);
}

return FEELFnResult.ofResult( result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

import java.util.ArrayList;
import java.util.List;

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

public class SignavioRemoveFunction
extends BaseFEELFunction {

public SignavioRemoveFunction() {
super( "remove" );
}

public FEELFnResult<List> invoke(@ParameterName("list") List list, @ParameterName("element") Object element) {
if ( list == null ) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
}
if (element == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "element", "cannot be null"));
}

// spec requires us to return a new list
List<Object> result = new ArrayList<Object>( list );

result.remove(element);

return FEELFnResult.ofResult( result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime.functions;

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

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

public class ZipFunction
extends BaseFEELFunction {

public ZipFunction() {
super("zip");
}

public FEELFnResult<List> invoke(@ParameterName("attributes") List<?> attributes, @ParameterName("values") Object[] values) {
if (attributes.isEmpty()) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "attributes", "attributes cannot be empty"));
} else if (!(attributes.get(0) instanceof String)) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "attributes", "attributes must be a list of string"));
}

if (values.length != attributes.size()) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "values must be a list of the same size as of attributes"));
}

// spec requires us to return a new list
final List<Map<Object, Object>> result = new ArrayList<>();

for (int aIdx = 0; aIdx < values.length; aIdx++) {
if (!(values[aIdx] instanceof List)) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "each value must be a list"));
}
List<?> value = (List<?>) values[aIdx];

if (result.isEmpty()) {
// first time init list
value.forEach(x -> result.add(new HashMap<>()));
} else {
if (value.size() != result.size()) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "each value must be consistent in size"));
}
}

Object attribute = attributes.get(aIdx);

for (int vIdx = 0; vIdx < value.size(); vIdx++) {
result.get(vIdx).put(attribute, value.get(vIdx));
}
}

return FEELFnResult.ofResult(result);
}
}
Loading

0 comments on commit 4fa9c2d

Please sign in to comment.