Skip to content

Commit 853182f

Browse files
committed
Merge branch 'comparison-maintainability' into 'dev'
Comparison maintainability See merge request cse2000-software-project/2023-2024/cluster-e/04b/rune-dsl-interpreter!12
2 parents fa8a76f + 4066674 commit 853182f

File tree

3 files changed

+66
-97
lines changed

3 files changed

+66
-97
lines changed

rosetta-lang/src/main/java/com/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterComparisonOperationInterpreter.java

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.regnosys.rosetta.interpreternew.visitors;
22

3+
import java.lang.reflect.InvocationTargetException;
34
import java.util.Arrays;
45
import java.util.List;
56

67
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBaseValue;
78
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBooleanValue;
89
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError;
910
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue;
10-
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterIntegerValue;
1111
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterListValue;
12-
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterNumberValue;
13-
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterStringValue;
1412
import com.regnosys.rosetta.rosetta.expression.ModifiableBinaryOperation;
1513
import com.regnosys.rosetta.rosetta.expression.RosettaExpression;
1614
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;
@@ -21,6 +19,17 @@ public class RosettaInterpreterComparisonOperationInterpreter extends
2119
private static List<String> comparisonOperators =
2220
Arrays.asList("<", "<=", ">", ">=", "=", "<>");
2321

22+
private static List<String> comparableClasses =
23+
Arrays.asList(
24+
"com.regnosys.rosetta.interpreternew.values."
25+
+ "RosettaInterpreterBooleanValue",
26+
"com.regnosys.rosetta.interpreternew.values."
27+
+ "RosettaInterpreterIntegerValue",
28+
"com.regnosys.rosetta.interpreternew.values."
29+
+ "RosettaInterpreterNumberValue",
30+
"com.regnosys.rosetta.interpreternew.values."
31+
+ "RosettaInterpreterStringValue");
32+
2433
/**
2534
* Interprets a comparison operation, evaluating the comparison between two operands.
2635
*
@@ -77,32 +86,9 @@ private RosettaInterpreterBaseValue compareAny(RosettaInterpreterValue leftValue
7786
//list vs list case:
7887
if (leftValue instanceof RosettaInterpreterListValue
7988
&& rightValue instanceof RosettaInterpreterListValue) {
80-
81-
//only way this is allowed is if rightValue has a length of 1
82-
// and left has length more than 1
83-
RosettaInterpreterListValue rgtList =
84-
(RosettaInterpreterListValue) rightValue;
85-
RosettaInterpreterListValue lfList =
86-
(RosettaInterpreterListValue) leftValue;
87-
if (rgtList.getExpressions().size() == 1
88-
&& lfList.getExpressions().size() > 1) {
89-
90-
91-
//for all elements in left list, check if the comparison
92-
// between them and right-hand side is true
93-
boolean anyTrue = false;
94-
for (RosettaInterpreterValue e : lfList.getExpressions()) {
95-
anyTrue |= checkComparableTypes(e,
96-
rgtList.getExpressions().get(0),
97-
operator);
98-
}
99-
return new RosettaInterpreterBooleanValue(anyTrue);
100-
}
101-
else {
102-
return new RosettaInterpreterErrorValue(
103-
new RosettaInterpreterError(
104-
"cannot compare two lists"));
105-
}
89+
return new RosettaInterpreterErrorValue(
90+
new RosettaInterpreterError(
91+
"cannot compare two lists"));
10692
}
10793

10894
//list vs element case:
@@ -141,32 +127,9 @@ private RosettaInterpreterBaseValue compareAll(RosettaInterpreterValue leftValue
141127
//list vs list case:
142128
if (leftValue instanceof RosettaInterpreterListValue
143129
&& rightValue instanceof RosettaInterpreterListValue) {
144-
145-
//only way this is allowed is if rightValue has a length of 1
146-
// and left has length more than 1
147-
RosettaInterpreterListValue rgtList =
148-
(RosettaInterpreterListValue) rightValue;
149-
RosettaInterpreterListValue lfList =
150-
(RosettaInterpreterListValue) leftValue;
151-
if (rgtList.getExpressions().size() == 1
152-
&& lfList.getExpressions().size() > 1) {
153-
154-
155-
//for all elements in left list, check if the comparison
156-
// between them and right-hand side is true
157-
boolean allTrue = true;
158-
for (RosettaInterpreterValue e : lfList.getExpressions()) {
159-
allTrue &= checkComparableTypes(e,
160-
rgtList.getExpressions().get(0),
161-
operator);
162-
}
163-
return new RosettaInterpreterBooleanValue(allTrue);
164-
}
165-
else {
166-
return new RosettaInterpreterErrorValue(
167-
new RosettaInterpreterError(
168-
"cannot compare two lists"));
169-
}
130+
return new RosettaInterpreterErrorValue(
131+
new RosettaInterpreterError(
132+
"cannot compare two lists"));
170133
}
171134

172135
//list vs element case:
@@ -204,56 +167,52 @@ private boolean checkComparableTypes(RosettaInterpreterValue leftValue,
204167
String operator) {
205168
int comparisonResult = 2;
206169

207-
//compare integers
208-
if (leftValue instanceof RosettaInterpreterIntegerValue
209-
&& rightValue instanceof RosettaInterpreterIntegerValue) {
210-
RosettaInterpreterIntegerValue leftInt =
211-
(RosettaInterpreterIntegerValue) leftValue;
212-
RosettaInterpreterIntegerValue rightInt =
213-
(RosettaInterpreterIntegerValue) rightValue;
214-
215-
comparisonResult = leftInt.compareTo(rightInt);
216-
}
217170

218-
//compare booleans
219-
else if (leftValue instanceof RosettaInterpreterBooleanValue
220-
&& rightValue instanceof RosettaInterpreterBooleanValue) {
221-
RosettaInterpreterBooleanValue leftBool =
222-
(RosettaInterpreterBooleanValue) leftValue;
223-
RosettaInterpreterBooleanValue rightBool =
224-
(RosettaInterpreterBooleanValue) rightValue;
225-
226-
comparisonResult = leftBool.compareTo(rightBool);
227-
}
171+
boolean isComparable = false;
228172

229-
//compare strings
230-
else if (leftValue instanceof RosettaInterpreterStringValue
231-
&& rightValue instanceof RosettaInterpreterStringValue) {
232-
RosettaInterpreterStringValue leftString =
233-
(RosettaInterpreterStringValue) leftValue;
234-
RosettaInterpreterStringValue rightString =
235-
(RosettaInterpreterStringValue) rightValue;
236-
237-
comparisonResult = leftString.compareTo(rightString);
238-
}
173+
//left and right will be of type comparableClazz
174+
Class<?> comparableClazz = null;
239175

240-
//compare numbers
241-
else if (leftValue instanceof RosettaInterpreterNumberValue
242-
&& rightValue instanceof RosettaInterpreterNumberValue) {
243-
RosettaInterpreterNumberValue leftNumber =
244-
(RosettaInterpreterNumberValue) leftValue;
245-
RosettaInterpreterNumberValue rightNumber =
246-
(RosettaInterpreterNumberValue) rightValue;
247-
248-
comparisonResult = leftNumber.compareTo(rightNumber);
176+
try {
177+
for (String clazzString : comparableClasses) {
178+
Class<?> clazz = Class.forName(clazzString);
179+
180+
//check that both expressions are of the same type
181+
boolean isInstance = (clazz.isInstance(leftValue)
182+
&& clazz.isInstance(rightValue));
183+
if (isInstance) {
184+
comparableClazz = clazz;
185+
}
186+
isComparable |= isInstance;
187+
}
188+
} catch (ClassNotFoundException e) {
189+
// TODO Auto-generated catch block
190+
System.out.println("Not a class");
191+
e.printStackTrace();
249192
}
250193

251-
194+
if (isComparable) {
195+
try {
196+
//compare the two expressions
197+
comparisonResult = (int) comparableClazz
198+
.getDeclaredMethod("compareTo",comparableClazz)
199+
.invoke(leftValue, rightValue);
200+
} catch (IllegalAccessException | IllegalArgumentException
201+
| InvocationTargetException | NoSuchMethodException
202+
| SecurityException e) {
203+
// TODO Auto-generated catch block
204+
e.printStackTrace();
205+
}
206+
}
207+
252208
return compareComparableValues(comparisonResult,
253209
operator);
254210
}
255211

256212
private boolean compareComparableValues(int comparisonResult, String operator) {
213+
if (comparisonResult == 2) {
214+
return false;
215+
}
257216
switch (operator) {
258217
case "=":
259218
return comparisonResult == 0;

rosetta-testing/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterComparisonTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ public void booleanLessEqualTest() {
6767

6868
@Test
6969
public void cardinalityAllListsTest() {
70+
RosettaInterpreterErrorValue expectedError = new RosettaInterpreterErrorValue(
71+
new RosettaInterpreterError(
72+
"cannot compare two lists"));
7073
RosettaExpression expr = parser.parseExpression("[1,2,3] all >= [0]");
7174
RosettaInterpreterValue val = interpreter.interp(expr);
72-
assertTrue(((RosettaInterpreterBooleanValue)val).getValue());
75+
assertEquals(expectedError.getErrors().get(0).getMessage(),
76+
((RosettaInterpreterErrorValue)val)
77+
.getErrors().get(0).getMessage());
7378
}
7479

7580
@Test

rosetta-testing/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterEqualityTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,14 @@ public void equalityAnyFalseTest() {
9292

9393
@Test
9494
public void equalityAnyTwoListsTest() {
95+
RosettaInterpreterErrorValue expectedError = new RosettaInterpreterErrorValue(
96+
new RosettaInterpreterError(
97+
"cannot compare two lists"));
9598
RosettaExpression expr = parser.parseExpression("[1,2,2] any = [1]");
9699
RosettaInterpreterValue val = interpreter.interp(expr);
97-
assertTrue(((RosettaInterpreterBooleanValue)val).getValue());
100+
assertEquals(expectedError.getErrors().get(0).getMessage(),
101+
((RosettaInterpreterErrorValue)val)
102+
.getErrors().get(0).getMessage());
98103
}
99104

100105
@Test

0 commit comments

Comments
 (0)