Skip to content

Commit 3e33ccf

Browse files
author
JelteMX
committed
Updates: Validation, better domain model
1 parent 1266cde commit 3e33ccf

File tree

13 files changed

+193
-71
lines changed

13 files changed

+193
-71
lines changed

dist/MathModule.mpk

3.21 KB
Binary file not shown.

src/MathModule.mpk

-404 KB
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package mathmodule;
2+
3+
import java.math.BigDecimal;
4+
5+
import com.mendix.core.Core;
6+
import com.mendix.systemwideinterfaces.core.IContext;
7+
import com.mendix.systemwideinterfaces.core.IMendixObject;
8+
9+
import mathmodule.proxies.SimpleExpressionResult;
10+
import mathmodule.proxies.ComplexExpressionResult;
11+
import mathmodule.proxies.ExpressionResult;
12+
13+
public class Misc {
14+
15+
public static final double MAX_DECIMAL_VALUE = Math.pow(10, 20) - 1;
16+
public static final double MAX_INT_VALUE = Math.pow(2, 31) - 1;
17+
public static final double MAX_LONG_VALUE = Math.pow(2, 63) - 1;
18+
19+
public static IMendixObject getComplexExpressionResult(IContext ctx, double value) {
20+
IMendixObject result = Core.instantiate(ctx, ComplexExpressionResult.getType());
21+
22+
String attrIsValid = ComplexExpressionResult.MemberNames.IsValid.toString();
23+
String attrValue = ComplexExpressionResult.MemberNames.Value.toString();
24+
String attrStringValue = ComplexExpressionResult.MemberNames.StringValue.toString();
25+
String attrHasDecimal = ComplexExpressionResult.MemberNames.HasDecimal.toString();
26+
27+
return setExpressionResultObject(ctx, result, value, attrIsValid, attrStringValue, attrValue, attrHasDecimal);
28+
}
29+
30+
public static IMendixObject getSimpleExpressionResult(IContext ctx, double value) {
31+
IMendixObject result = Core.instantiate(ctx, SimpleExpressionResult.getType());
32+
33+
String attrIsValid = SimpleExpressionResult.MemberNames.IsValid.toString();
34+
String attrValue = SimpleExpressionResult.MemberNames.Value.toString();
35+
String attrStringValue = SimpleExpressionResult.MemberNames.StringValue.toString();
36+
String attrHasDecimal = SimpleExpressionResult.MemberNames.HasDecimal.toString();
37+
38+
return setExpressionResultObject(ctx, result, value, attrIsValid, attrStringValue, attrValue, attrHasDecimal);
39+
}
40+
41+
private static IMendixObject setExpressionResultObject(IContext ctx, IMendixObject result, double value, String attrIsValid, String attrStringValue, String attrValue, String attrHasDecimal) {
42+
result.setValue(ctx, attrIsValid, true);
43+
result.setValue(ctx, attrStringValue, String.format("%.16f", value));
44+
45+
if (value > MAX_DECIMAL_VALUE || value < (0 - MAX_DECIMAL_VALUE)) {
46+
result.setValue(ctx, attrHasDecimal, false);
47+
result.setValue(ctx, attrValue, null);
48+
} else {
49+
result.setValue(ctx, attrHasDecimal, true);
50+
result.setValue(ctx, attrValue, new BigDecimal(value));
51+
}
52+
53+
return result;
54+
}
55+
56+
public static IMendixObject getExpressionError(IContext ctx, String resultType, String error) {
57+
IMendixObject result = Core.instantiate(ctx, resultType);
58+
59+
String attrIsValid = ExpressionResult.MemberNames.IsValid.toString();
60+
String attrError = ExpressionResult.MemberNames.ErrorMessage.toString();
61+
62+
result.setValue(ctx, attrIsValid, false);
63+
result.setValue(ctx, attrError, error);
64+
65+
return result;
66+
}
67+
}

src/mathmodule/actions/ComplexExpression.java renamed to src/javasource/mathmodule/actions/ComplexExpression.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
package mathmodule.actions;
1111

12+
import com.mendix.core.Core;
1213
import com.mendix.systemwideinterfaces.core.IContext;
1314
import com.mendix.webui.CustomJavaAction;
1415
import mathmodule.Misc;
16+
import mathmodule.proxies.ComplexExpressionResult;
1517
import mathmodule.proxies.ExpressionUseEnum;
1618
import com.mendix.systemwideinterfaces.core.IMendixObject;
1719
import org.mariuszgromada.math.mxparser.Expression;
@@ -43,7 +45,7 @@ public IMendixObject executeAction() throws Exception
4345
IContext ctx = this.getContext();
4446

4547
if (this.Expression == "" || this.Expression == null) {
46-
return Misc.getExpressionError(ctx, "Expression cannot be empty");
48+
return Misc.getExpressionError(ctx, ComplexExpressionResult.getType(), "Expression cannot be empty");
4749
}
4850

4951
Expression expression = new Expression(this.Expression);
@@ -59,14 +61,14 @@ public IMendixObject executeAction() throws Exception
5961
Argument arg = new Argument(name, value.doubleValue());
6062
expression.addArguments(arg);
6163
} else if (enumeration.equals(ExpressionUseEnum.String.name()) && stringValue != null) {
62-
double doubleValue = new Double(stringValue);
64+
double doubleValue = Double.parseDouble(stringValue);
6365
if (!Double.isNaN(doubleValue)) {
6466
Argument arg = new Argument(name, doubleValue);
6567
expression.addArguments(arg);
6668
}
6769
}
6870
}
69-
71+
7072
boolean syntaxValid = expression.checkSyntax();
7173
if (syntaxValid) {
7274

@@ -75,15 +77,31 @@ public IMendixObject executeAction() throws Exception
7577

7678
if (afterCalculateSatus) {
7779
if (Double.isNaN(expressionResult)) {
78-
return Misc.getExpressionError(ctx, "Expression result is not a number");
80+
return Misc.getExpressionError(ctx, ComplexExpressionResult.getType(), "Expression result is not a number");
7981
}
80-
return Misc.getExpressionResult(ctx, expressionResult);
82+
return Misc.getComplexExpressionResult(ctx, expressionResult);
8183
} else {
82-
return Misc.getExpressionError(ctx, expression.getErrorMessage());
84+
return Misc.getExpressionError(ctx, ComplexExpressionResult.getType(), expression.getErrorMessage());
8385
}
8486

8587
} else {
86-
return Misc.getExpressionError(ctx, expression.getErrorMessage());
88+
String[] missingArguments = expression.getMissingUserDefinedArguments();
89+
IMendixObject complexResult = Misc.getExpressionError(ctx, ComplexExpressionResult.getType(), expression.getErrorMessage());
90+
91+
if (missingArguments.length > 0) {
92+
String hasMissing = ComplexExpressionResult.MemberNames.HasMissingArguments.toString();
93+
complexResult.setValue(ctx, hasMissing, true);
94+
95+
for (int i = 0; i < missingArguments.length; i++) {
96+
String missing = missingArguments[i];
97+
IMendixObject missingObject = Core.instantiate(ctx, mathmodule.proxies.Argument.getType());
98+
missingObject.setValue(ctx, mathmodule.proxies.Argument.MemberNames.ArgumentName.toString(), missing);
99+
missingObject.setValue(ctx, mathmodule.proxies.Argument.MemberNames.Missing.toString(), complexResult.getId());
100+
Core.commit(ctx, missingObject);
101+
}
102+
}
103+
104+
return complexResult;
87105
}
88106

89107
// END USER CODE

src/mathmodule/actions/RecursiveFunction.java renamed to src/javasource/mathmodule/actions/RecursiveFunction.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
package mathmodule.actions;
1111

12-
import org.mariuszgromada.math.mxparser.Argument;
1312
import org.mariuszgromada.math.mxparser.Expression;
1413
import org.mariuszgromada.math.mxparser.RecursiveArgument;
1514
import com.mendix.systemwideinterfaces.core.IContext;
1615
import com.mendix.webui.CustomJavaAction;
1716
import mathmodule.Misc;
17+
import mathmodule.proxies.SimpleExpressionResult;
1818
import com.mendix.systemwideinterfaces.core.IMendixObject;
1919

2020
public class RecursiveFunction extends CustomJavaAction<IMendixObject>
@@ -40,11 +40,11 @@ public IMendixObject executeAction() throws Exception
4040
IContext ctx = getContext();
4141

4242
if (this.Function == null || this.Function == "") {
43-
return Misc.getExpressionError(ctx, "Function cannot be empty");
43+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), "Function cannot be empty");
4444
} else if (this.ValueIndex0 == null || this.ValueIndex1 == null) {
45-
return Misc.getExpressionError(ctx, "For the recursive function you will need a value for n=0 and n=1");
45+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), "For the recursive function you will need a value for n=0 and n=1");
4646
} else if (this.FunctionCall == null) {
47-
return Misc.getExpressionError(ctx, "The function needs a function call");
47+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), "The function needs a function call");
4848
}
4949

5050
RecursiveArgument recursive = new RecursiveArgument(this.Function);
@@ -59,20 +59,20 @@ public IMendixObject executeAction() throws Exception
5959
boolean expressionValid = expression.checkSyntax();
6060

6161
if (!expressionValid) {
62-
return Misc.getExpressionError(ctx, expression.getErrorMessage());
62+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), expression.getErrorMessage());
6363
}
6464

6565
double value = expression.calculate();
6666

6767
if (Double.isNaN(value)) {
68-
return Misc.getExpressionError(ctx, "Function does not return a number");
68+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), "Function does not return a number");
6969
}
7070

7171
// Not sure if needed, maybe clear up memory
7272
recursive.resetAllCases();
73-
return Misc.getExpressionResult(ctx, value);
73+
return Misc.getSimpleExpressionResult(ctx, value);
7474
} else {
75-
return Misc.getExpressionError(ctx, recursive.getErrorMessage());
75+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), recursive.getErrorMessage());
7676
}
7777
// END USER CODE
7878
}

src/mathmodule/actions/SimpleExpression.java renamed to src/javasource/mathmodule/actions/SimpleExpression.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.mendix.webui.CustomJavaAction;
1414
import com.mendix.systemwideinterfaces.core.IMendixObject;
1515
import mathmodule.Misc;
16+
import mathmodule.proxies.SimpleExpressionResult;
1617
import org.mariuszgromada.math.mxparser.Expression;
1718

1819
public class SimpleExpression extends CustomJavaAction<IMendixObject>
@@ -31,7 +32,7 @@ public IMendixObject executeAction() throws Exception
3132
// BEGIN USER CODE
3233
IContext ctx = this.getContext();
3334
if (this.Expression == "" || this.Expression == null) {
34-
return Misc.getExpressionError(ctx, "Expression cannot be empty");
35+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), "Expression cannot be empty");
3536
}
3637
Expression simpleExpression = new Expression(this.Expression);
3738
boolean syntaxValid = simpleExpression.checkSyntax();
@@ -42,15 +43,15 @@ public IMendixObject executeAction() throws Exception
4243
boolean afterCalculateSatus = simpleExpression.checkSyntax();
4344
if (afterCalculateSatus) {
4445
if (Double.isNaN(expressionResult)) {
45-
return Misc.getExpressionError(ctx, "Expression result is not a number");
46+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), "Expression result is not a number");
4647
}
47-
return Misc.getExpressionResult(ctx, expressionResult);
48+
return Misc.getSimpleExpressionResult(ctx, expressionResult);
4849
} else {
49-
return Misc.getExpressionError(ctx, simpleExpression.getErrorMessage());
50+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), simpleExpression.getErrorMessage());
5051
}
5152

5253
} else {
53-
return Misc.getExpressionError(ctx, simpleExpression.getErrorMessage());
54+
return Misc.getExpressionError(ctx, SimpleExpressionResult.getType(), simpleExpression.getErrorMessage());
5455
}
5556
// END USER CODE
5657
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// This file was generated by Mendix Studio Pro.
2+
//
3+
// WARNING: Only the following code will be retained when actions are regenerated:
4+
// - the import list
5+
// - the code between BEGIN USER CODE and END USER CODE
6+
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7+
// Other code you write will be lost the next time you deploy the project.
8+
// Special characters, e.g., é, ö, à, etc. are supported in comments.
9+
10+
package mathmodule.actions;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import org.mariuszgromada.math.mxparser.Expression;
15+
import com.mendix.core.Core;
16+
import com.mendix.systemwideinterfaces.core.IContext;
17+
import com.mendix.webui.CustomJavaAction;
18+
import mathmodule.Misc;
19+
import mathmodule.proxies.ComplexValidation;
20+
import com.mendix.systemwideinterfaces.core.IMendixObject;
21+
22+
public class ValidateComplexExpression extends CustomJavaAction<IMendixObject>
23+
{
24+
private java.lang.String Expression;
25+
26+
public ValidateComplexExpression(IContext context, java.lang.String Expression)
27+
{
28+
super(context);
29+
this.Expression = Expression;
30+
}
31+
32+
@java.lang.Override
33+
public IMendixObject executeAction() throws Exception
34+
{
35+
// BEGIN USER CODE
36+
IContext ctx = this.getContext();
37+
38+
if (this.Expression == "" || this.Expression == null) {
39+
return Misc.getExpressionError(ctx, ComplexValidation.getType(), "Expression cannot be empty");
40+
}
41+
42+
Expression expression = new Expression(this.Expression);
43+
44+
boolean status = expression.checkLexSyntax();
45+
if (status) {
46+
expression.checkSyntax();
47+
48+
IMendixObject result = Core.instantiate(ctx, ComplexValidation.getType());
49+
List<IMendixObject> argumentsList = new ArrayList<IMendixObject>();
50+
51+
result.setValue(ctx, ComplexValidation.MemberNames.IsValid.toString(), true);
52+
53+
String[] usedArguments = expression.getMissingUserDefinedArguments();
54+
if (usedArguments.length > 0) {
55+
for (int i = 0; i < usedArguments.length; i++) {
56+
String arg = usedArguments[i];
57+
IMendixObject argObj = Core.instantiate(ctx, mathmodule.proxies.Argument.getType());
58+
59+
argObj.setValue(ctx, mathmodule.proxies.Argument.MemberNames.ArgumentName.toString(), arg);
60+
argObj.setValue(ctx, mathmodule.proxies.Argument.MemberNames.Needed.toString(), result.getId());
61+
62+
argumentsList.add(argObj);
63+
}
64+
Core.commit(ctx, argumentsList);
65+
}
66+
67+
return result;
68+
} else {
69+
String error = expression.getErrorMessage();
70+
return Misc.getExpressionError(ctx, ComplexValidation.getType(), error);
71+
}
72+
// END USER CODE
73+
}
74+
75+
/**
76+
* Returns a string representation of this action
77+
*/
78+
@java.lang.Override
79+
public java.lang.String toString()
80+
{
81+
return "ValidateComplexExpression";
82+
}
83+
84+
// BEGIN EXTRA CODE
85+
// END EXTRA CODE
86+
}

0 commit comments

Comments
 (0)