Skip to content

Commit b6822f4

Browse files
committed
ST6RI-886 Implemented remaining functions in ControlFunctions package.
- Also implemented DataFunctions::min and DataFunctions::max, used in the implementation of ControlFunctions::minimize and maximize. - Also removed old overrides of getInput and getOutput in ExpressionImpl.
1 parent a83e872 commit b6822f4

15 files changed

Lines changed: 634 additions & 53 deletions

File tree

org.omg.sysml.execution/src/org/omg/sysml/execution/expressions/LibraryFunctionFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
package org.omg.sysml.execution.expressions;
2323

24+
import org.omg.sysml.execution.expressions.functions.data.*;
25+
import org.omg.sysml.execution.expressions.functions.control.*;
2426
import org.omg.sysml.execution.expressions.functions.numerical.*;
2527
import org.omg.sysml.execution.expressions.functions.sequence.*;
2628
import org.omg.sysml.execution.expressions.functions.string.*;
@@ -33,6 +35,19 @@ public class LibraryFunctionFactory extends org.omg.sysml.expressions.ModelLevel
3335
protected void initializeFunctionMap() {
3436
super.initializeFunctionMap();
3537

38+
// ControlFunctions
39+
put(new ExistsFunction());
40+
put(new ForAllFunction());
41+
put(new MinimizeFunction());
42+
put(new MaximizeFunction());
43+
put(new ReduceFunction());
44+
put(new RejectFunction());
45+
put(new SelectOneFunction());
46+
47+
// DataFunctions
48+
put(new MaxFunction());
49+
put(new MinFunction());
50+
3651
// NumericalFunctions
3752
put(new SumFunction());
3853
put(new ProdFunction());
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Model Driven Solutions, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*
20+
*******************************************************************************/
21+
package org.omg.sysml.execution.expressions.functions.control;
22+
23+
import org.eclipse.emf.common.util.EList;
24+
import org.omg.sysml.expressions.ModelLevelExpressionEvaluator;
25+
import org.omg.sysml.lang.sysml.Element;
26+
import org.omg.sysml.lang.sysml.InvocationExpression;
27+
import org.omg.sysml.util.EvaluationUtil;
28+
29+
public class ExistsFunction extends ForAllFunction {
30+
31+
@Override
32+
public String getOperatorName() {
33+
return "exists";
34+
}
35+
36+
@Override
37+
public EList<Element> invoke(InvocationExpression invocation, Element target,
38+
ModelLevelExpressionEvaluator evaluator) {
39+
Boolean result = forAll(invocation, target, evaluator, false);
40+
return result == null? EvaluationUtil.singletonList(invocation): EvaluationUtil.booleanResult(!result);
41+
}
42+
43+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Model Driven Solutions, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*
20+
*******************************************************************************/
21+
package org.omg.sysml.execution.expressions.functions.control;
22+
23+
import org.eclipse.emf.common.util.EList;
24+
import org.omg.sysml.expressions.ModelLevelExpressionEvaluator;
25+
import org.omg.sysml.expressions.functions.control.ControlFunction;
26+
import org.omg.sysml.lang.sysml.Element;
27+
import org.omg.sysml.lang.sysml.Expression;
28+
import org.omg.sysml.lang.sysml.InvocationExpression;
29+
import org.omg.sysml.util.EvaluationUtil;
30+
31+
public class ForAllFunction extends ControlFunction {
32+
33+
@Override
34+
public String getOperatorName() {
35+
return "forAll";
36+
}
37+
38+
public Boolean forAll(InvocationExpression invocation, Element target,
39+
ModelLevelExpressionEvaluator evaluator, Boolean test) {
40+
EList<Element> list = evaluator.evaluateArgument(invocation, 0, target);
41+
Element expr = evaluator.argumentValue(invocation, 1, target);
42+
if (list == null || !(expr instanceof Expression)) {
43+
return null;
44+
} else {
45+
for (Element value: list) {
46+
if (value == null) {
47+
return null;
48+
} else {
49+
EList<Element> exprValue = evaluator.evaluateExpression((Expression)expr, target, value);
50+
if (exprValue == null || exprValue.size() != 1) {
51+
return null;
52+
} else if (!test.equals(EvaluationUtil.valueOf(exprValue.get(0)))) {
53+
return false;
54+
}
55+
}
56+
}
57+
return true;
58+
}
59+
}
60+
61+
@Override
62+
public EList<Element> invoke(InvocationExpression invocation, Element target,
63+
ModelLevelExpressionEvaluator evaluator) {
64+
Boolean result = forAll(invocation, target, evaluator, true);
65+
return result == null? EvaluationUtil.singletonList(invocation): EvaluationUtil.booleanResult(result);
66+
}
67+
68+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Model Driven Solutions, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*
20+
*******************************************************************************/
21+
package org.omg.sysml.execution.expressions.functions.control;
22+
23+
import java.util.function.BiFunction;
24+
25+
import org.eclipse.emf.common.util.EList;
26+
import org.omg.sysml.expressions.ModelLevelExpressionEvaluator;
27+
import org.omg.sysml.lang.sysml.Element;
28+
import org.omg.sysml.lang.sysml.Expression;
29+
import org.omg.sysml.lang.sysml.InvocationExpression;
30+
import org.omg.sysml.lang.sysml.Type;
31+
import org.omg.sysml.lang.sysml.util.SysMLLibraryUtil;
32+
import org.omg.sysml.util.EvaluationUtil;
33+
34+
public class MaximizeFunction extends ReduceFunction {
35+
private static final String MAX_FUNCTION = "DataFunctions::max";
36+
37+
@Override
38+
public String getOperatorName() {
39+
return "maximize";
40+
}
41+
42+
@Override
43+
public EList<Element> invoke(InvocationExpression invocation, Element target,
44+
ModelLevelExpressionEvaluator evaluator) {
45+
EList<Element> list = evaluator.evaluateArgument(invocation, 0, target);
46+
Element expr = evaluator.argumentValue(invocation, 1, target);
47+
if (list == null || !(expr instanceof Expression)) {
48+
return EvaluationUtil.singletonList(invocation);
49+
} else if (list.isEmpty()) {
50+
return EvaluationUtil.nullList();
51+
} else {
52+
return reduce(invocation, list, new BiFunction<>() {
53+
@Override
54+
public Element apply(Element result, Element value) {
55+
EList<Element> exprValue = evaluator.evaluateExpression((Expression)expr, target, value);
56+
if (exprValue == null || exprValue.size() != 1) {
57+
return null;
58+
} else if (result == null) {
59+
return exprValue.get(0);
60+
} else {
61+
Type maxFunction = SysMLLibraryUtil.getLibraryType(expr, MAX_FUNCTION);
62+
InvocationExpression maxInvocation = EvaluationUtil.createInvocationOf(maxFunction, result, exprValue.get(0));
63+
EList<Element> newResult = evaluator.evaluateInvocation(maxInvocation, target);
64+
return newResult == null || newResult.size() != 1? null: newResult.get(0);
65+
}
66+
}
67+
});
68+
}
69+
}
70+
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Model Driven Solutions, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*
20+
*******************************************************************************/
21+
package org.omg.sysml.execution.expressions.functions.control;
22+
23+
import java.util.function.BiFunction;
24+
25+
import org.eclipse.emf.common.util.EList;
26+
import org.omg.sysml.expressions.ModelLevelExpressionEvaluator;
27+
import org.omg.sysml.lang.sysml.Element;
28+
import org.omg.sysml.lang.sysml.Expression;
29+
import org.omg.sysml.lang.sysml.InvocationExpression;
30+
import org.omg.sysml.lang.sysml.Type;
31+
import org.omg.sysml.lang.sysml.util.SysMLLibraryUtil;
32+
import org.omg.sysml.util.EvaluationUtil;
33+
34+
public class MinimizeFunction extends ReduceFunction {
35+
private static final String MIN_FUNCTION = "DataFunctions::min";
36+
37+
@Override
38+
public String getOperatorName() {
39+
return "minimize";
40+
}
41+
42+
@Override
43+
public EList<Element> invoke(InvocationExpression invocation, Element target,
44+
ModelLevelExpressionEvaluator evaluator) {
45+
EList<Element> list = evaluator.evaluateArgument(invocation, 0, target);
46+
Element expr = evaluator.argumentValue(invocation, 1, target);
47+
if (list == null || !(expr instanceof Expression)) {
48+
return EvaluationUtil.singletonList(invocation);
49+
} else if (list.isEmpty()) {
50+
return EvaluationUtil.nullList();
51+
} else {
52+
return reduce(invocation, list, new BiFunction<>() {
53+
@Override
54+
public Element apply(Element result, Element value) {
55+
EList<Element> exprValue = evaluator.evaluateExpression((Expression)expr, target, value);
56+
if (exprValue == null || exprValue.size() != 1) {
57+
return null;
58+
} else if (result == null) {
59+
return exprValue.get(0);
60+
} else {
61+
Type maxFunction = SysMLLibraryUtil.getLibraryType(expr, MIN_FUNCTION);
62+
InvocationExpression maxInvocation = EvaluationUtil.createInvocationOf(maxFunction, result, exprValue.get(0));
63+
EList<Element> newResult = evaluator.evaluateInvocation(maxInvocation, target);
64+
return newResult == null || newResult.size() != 1? null: newResult.get(0);
65+
}
66+
}
67+
});
68+
}
69+
}
70+
71+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Model Driven Solutions, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*
20+
*******************************************************************************/
21+
package org.omg.sysml.execution.expressions.functions.control;
22+
23+
import java.util.function.BiFunction;
24+
25+
import org.eclipse.emf.common.util.EList;
26+
import org.omg.sysml.expressions.ModelLevelExpressionEvaluator;
27+
import org.omg.sysml.expressions.functions.control.ControlFunction;
28+
import org.omg.sysml.lang.sysml.Element;
29+
import org.omg.sysml.lang.sysml.Expression;
30+
import org.omg.sysml.lang.sysml.InvocationExpression;
31+
import org.omg.sysml.util.EvaluationUtil;
32+
33+
public class ReduceFunction extends ControlFunction {
34+
35+
@Override
36+
public String getOperatorName() {
37+
return "reduce";
38+
}
39+
40+
protected EList<Element> reduce(InvocationExpression invocation, EList<Element> list,
41+
BiFunction<Element, Element, Element> compute) {
42+
Element result = null;
43+
for (Element value: list) {
44+
if (value == null) {
45+
return EvaluationUtil.singletonList(invocation);
46+
} else {
47+
result = compute.apply(result, value);
48+
if (result == null) {
49+
return EvaluationUtil.singletonList(invocation);
50+
}
51+
}
52+
}
53+
return EvaluationUtil.singletonList(result);
54+
}
55+
56+
@Override
57+
public EList<Element> invoke(InvocationExpression invocation, Element target,
58+
ModelLevelExpressionEvaluator evaluator) {
59+
EList<Element> list = evaluator.evaluateArgument(invocation, 0, target);
60+
Element expr = evaluator.argumentValue(invocation, 1, target);
61+
if (list == null || !(expr instanceof Expression)) {
62+
return EvaluationUtil.singletonList(invocation);
63+
} else if (list.isEmpty()) {
64+
return EvaluationUtil.nullList();
65+
} else {
66+
return reduce(invocation, list, new BiFunction<>() {
67+
@Override
68+
public Element apply(Element result, Element value) {
69+
if (result == null) {
70+
return value;
71+
} else {
72+
EList<Element> exprValue = evaluator.evaluateExpression((Expression)expr, target, result, value);
73+
return exprValue == null || exprValue.size() != 1? null: exprValue.get(0);
74+
}
75+
}
76+
});
77+
}
78+
}
79+
80+
}

0 commit comments

Comments
 (0)