Skip to content

Commit e2452ed

Browse files
committed
Merge branch 'dev' into 'variable-support'
# Conflicts: # rosetta-lang/model/RosettaInterpreter.xcore # rosetta-lang/src/main/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterVisitor.java
2 parents b1ea203 + 5489290 commit e2452ed

File tree

8 files changed

+456
-12
lines changed

8 files changed

+456
-12
lines changed

rosetta-lang/model/RosettaExpression.xcore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,15 @@ class FlattenOperation extends ListOperation, CanHandleListOfLists {
403403
}
404404

405405
class DistinctOperation extends ListOperation {
406+
op RosettaInterpreterValue accept(InterpreterVisitor v) {
407+
v.interp(this)
408+
}
406409
}
407410

408411
class ReverseOperation extends ListOperation {
412+
op RosettaInterpreterValue accept(InterpreterVisitor v) {
413+
v.interp(this)
414+
}
409415
}
410416

411417
class FirstOperation extends ListOperation {
@@ -427,6 +433,9 @@ class LastOperation extends ListOperation {
427433
}
428434

429435
class SumOperation extends ListOperation {
436+
op RosettaInterpreterValue accept(InterpreterVisitor v) {
437+
v.interp(this)
438+
}
430439
}
431440

432441
class AsKeyOperation extends RosettaUnaryOperation {

rosetta-lang/model/RosettaInterpreter.xcore

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import com.regnosys.rosetta.rosetta.expression.RosettaAbsentExpression
2727
import com.regnosys.rosetta.rosetta.expression.RosettaCountOperation
2828
import com.regnosys.rosetta.rosetta.expression.FirstOperation
2929
import com.regnosys.rosetta.rosetta.expression.LastOperation
30+
import com.regnosys.rosetta.rosetta.expression.DistinctOperation
31+
import com.regnosys.rosetta.rosetta.expression.ReverseOperation
32+
import com.regnosys.rosetta.rosetta.expression.SumOperation
3033

3134
class RosettaInterpreterBaseError{
3235
String message
@@ -56,8 +59,11 @@ interface InterpreterVisitor {
5659
op RosettaInterpreterValue interp (RosettaExistsExpression exp)
5760
op RosettaInterpreterValue interp (RosettaAbsentExpression exp)
5861
op RosettaInterpreterValue interp (RosettaCountOperation exp)
59-
op RosettaInterpreterValue interp (FirstOperation exp)
60-
op RosettaInterpreterValue interp (LastOperation exp)
62+
op RosettaInterpreterValue interp (FirstOperation exp)
63+
op RosettaInterpreterValue interp (LastOperation exp)
64+
op RosettaInterpreterValue interp (DistinctOperation exp)
65+
op RosettaInterpreterValue interp (ReverseOperation exp)
66+
op RosettaInterpreterValue interp (SumOperation exp)
6167

6268

6369
op RosettaInterpreterValue interp (RosettaBooleanLiteral exp, RosettaInterpreterBaseEnvironment env)
@@ -81,4 +87,9 @@ interface InterpreterVisitor {
8187
op RosettaInterpreterValue interp (RosettaCountOperation exp, RosettaInterpreterBaseEnvironment env)
8288
op RosettaInterpreterValue interp (FirstOperation exp, RosettaInterpreterBaseEnvironment env)
8389
op RosettaInterpreterValue interp (LastOperation exp, RosettaInterpreterBaseEnvironment env)
90+
op RosettaInterpreterValue interp (DistinctOperation exp, RosettaInterpreterBaseEnvironment env)
91+
op RosettaInterpreterValue interp (ReverseOperation exp, RosettaInterpreterBaseEnvironment env)
92+
op RosettaInterpreterValue interp (SumOperation exp, RosettaInterpreterBaseEnvironment env)
93+
94+
8495
}

rosetta-lang/src/main/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterVisitor.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.regnosys.rosetta.rosetta.expression.ArithmeticOperation;
44
import com.regnosys.rosetta.rosetta.expression.LogicalOperation;
55
import com.regnosys.rosetta.rosetta.RosettaInterpreterBaseEnvironment;
6+
import com.regnosys.rosetta.rosetta.expression.ReverseOperation;
67
import com.regnosys.rosetta.rosetta.expression.ComparisonOperation;
8+
import com.regnosys.rosetta.rosetta.expression.DistinctOperation;
79
import com.regnosys.rosetta.rosetta.expression.EqualityOperation;
810
import com.regnosys.rosetta.rosetta.expression.FirstOperation;
911
import com.regnosys.rosetta.rosetta.expression.JoinOperation;
@@ -22,15 +24,16 @@
2224
import com.regnosys.rosetta.rosetta.expression.RosettaPatternLiteral;
2325
import com.regnosys.rosetta.rosetta.expression.RosettaStringLiteral;
2426
import com.regnosys.rosetta.rosetta.expression.RosettaSymbolReference;
27+
import com.regnosys.rosetta.rosetta.expression.SumOperation;
2528
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
26-
2729
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterLogicalOperationInterpreter;
2830
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError;
2931
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue;
3032
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterComparisonOperationInterpreter;
3133
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterListLiteralInterpreter;
3234
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterRosettaArithmeticOperationsInterpreter;
3335
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterListOperationsInterpreter;
36+
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterListOperatorInterpreter;
3437
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterRosettaBooleanLiteralInterpreter;
3538
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterRosettaConditionalExpressionInterpreter;
3639
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterRosettaIntLiteralInterpreter;
@@ -265,5 +268,36 @@ public RosettaInterpreterValue interp(LastOperation exp,
265268
throw new UnsupportedOperationException();
266269
}
267270

268-
271+
@Override
272+
public RosettaInterpreterValue interp(DistinctOperation exp) {
273+
return interp(exp, new RosettaInterpreterEnvironment());
274+
}
275+
276+
@Override
277+
public RosettaInterpreterValue interp(DistinctOperation exp,
278+
RosettaInterpreterBaseEnvironment env) {
279+
return new RosettaInterpreterListOperatorInterpreter().interp(exp, env);
280+
}
281+
282+
@Override
283+
public RosettaInterpreterValue interp(ReverseOperation exp) {
284+
return interp(exp, new RosettaInterpreterEnvironment());
285+
}
286+
287+
@Override
288+
public RosettaInterpreterValue interp(ReverseOperation exp,
289+
RosettaInterpreterBaseEnvironment env) {
290+
return new RosettaInterpreterListOperatorInterpreter().interp(exp, env);
291+
}
292+
293+
@Override
294+
public RosettaInterpreterValue interp(SumOperation exp) {
295+
return interp(exp, new RosettaInterpreterEnvironment());
296+
}
297+
298+
@Override
299+
public RosettaInterpreterValue interp(SumOperation exp,
300+
RosettaInterpreterBaseEnvironment env) {
301+
return new RosettaInterpreterListOperatorInterpreter().interp(exp, env);
302+
}
269303
}

rosetta-lang/src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterBaseValue.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.regnosys.rosetta.interpreternew.values;
22

33
import java.lang.reflect.InvocationTargetException;
4-
4+
import java.util.List;
5+
import java.util.stream.Collectors;
56
import java.util.stream.Stream;
67

78
import org.eclipse.emf.common.notify.Adapter;
@@ -65,6 +66,20 @@ public static Stream<RosettaInterpreterValue> valueStream(RosettaInterpreterValu
6566
return ((RosettaInterpreterBaseValue)val).toValueStream();
6667
}
6768

69+
/**
70+
* Converts a rosetta value to a list of itself or values it contains.
71+
*
72+
* @param val - value to convert
73+
* @return - list of value or its contained values
74+
*/
75+
public static List<RosettaInterpreterValue> toValueList(RosettaInterpreterValue val){
76+
if (!(val instanceof RosettaInterpreterBaseValue)) {
77+
throw new RosettaInterpreterNewException("Cannot take value stream"
78+
+ "of RosettaInterpreterValue");
79+
}
80+
return valueStream(val).collect(Collectors.toList());
81+
}
82+
6883
@Override
6984
public EClass eClass() {
7085
// TODO Auto-generated method stub

rosetta-lang/src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterErrorValue.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ public static boolean errorsExist(RosettaInterpreterValue val1,
7171
}
7272

7373
/**
74-
* Checks if the supplied value is an error.
74+
* Checks if the supplied value is an error
75+
* or if the contained list contains an error.
7576
*
7677
* @param val1 - value to check
7778
* @return true iff value is an error value
7879
*/
7980
public static boolean errorsExist(RosettaInterpreterValue val1) {
80-
return errorsExist(List.of(val1));
81+
return errorsExist(RosettaInterpreterBaseValue.toValueList(val1));
8182
}
8283

8384
/**
@@ -115,7 +116,7 @@ public static RosettaInterpreterErrorValue merge(List<RosettaInterpreterValue> v
115116
}
116117

117118
public static RosettaInterpreterErrorValue merge(RosettaInterpreterValue val) {
118-
return merge(List.of(val));
119+
return merge(RosettaInterpreterBaseValue.toValueList(val));
119120
}
120121

121122
public static RosettaInterpreterErrorValue merge(RosettaInterpreterValue val1,
@@ -150,14 +151,12 @@ public boolean equals(Object obj) {
150151

151152
@Override
152153
public Stream<Object> toElementStream() {
153-
throw new RosettaInterpreterNewException("You should not be trying to take"
154-
+ " a stream of element of an error value");
154+
return Stream.of(errors);
155155
}
156156

157157
@Override
158158
public Stream<RosettaInterpreterValue> toValueStream() {
159-
throw new RosettaInterpreterNewException("You should not be trying to take"
160-
+ " a stream of an error value");
159+
return Stream.of(this);
161160
}
162161

163162
}

rosetta-lang/src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterIntegerValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public RosettaInterpreterIntegerValue(BigInteger value) {
3636
this.value = value;
3737
}
3838

39+
public RosettaInterpreterIntegerValue(int value) {
40+
super();
41+
this.value = BigInteger.valueOf(value);
42+
}
43+
3944
public BigInteger getValue() { return value; }
4045

4146
@Override
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.regnosys.rosetta.interpreternew.visitors;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBaseValue;
9+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError;
10+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue;
11+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterIntegerValue;
12+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterListValue;
13+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterNumberValue;
14+
import com.regnosys.rosetta.rosetta.expression.DistinctOperation;
15+
import com.regnosys.rosetta.rosetta.expression.ReverseOperation;
16+
import com.regnosys.rosetta.rosetta.expression.RosettaExpression;
17+
import com.regnosys.rosetta.rosetta.expression.SumOperation;
18+
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;
19+
import com.rosetta.model.lib.RosettaNumber;
20+
21+
public class RosettaInterpreterListOperatorInterpreter
22+
extends RosettaInterpreterConcreteInterpreter {
23+
24+
/**
25+
* Interprets a Distinct Operation.
26+
* Returns a list where duplicate elements are removed
27+
* such that only one copy of them exists in the resulting
28+
* list
29+
*
30+
* @param exp - distinct operation to interpret
31+
* @return - list Value of distinct values
32+
*/
33+
public RosettaInterpreterValue interp(DistinctOperation exp) {
34+
RosettaExpression expression = exp.getArgument();
35+
RosettaInterpreterValue val = expression.accept(visitor);
36+
37+
if (RosettaInterpreterErrorValue.errorsExist(val)) {
38+
return RosettaInterpreterErrorValue.merge(val);
39+
}
40+
41+
List<RosettaInterpreterValue> distinct =
42+
RosettaInterpreterBaseValue.valueStream(val)
43+
.distinct()
44+
.collect(Collectors.toList());
45+
46+
return new RosettaInterpreterListValue(distinct);
47+
}
48+
49+
/**
50+
* Interprets a Reverse operation.
51+
* Reverses the order of the elements in the list and returns it
52+
*
53+
* @param exp - Reverse operation to interpret
54+
* @return - Reversed list
55+
*/
56+
public RosettaInterpreterValue interp(ReverseOperation exp) {
57+
RosettaExpression expression = exp.getArgument();
58+
RosettaInterpreterValue val = expression.accept(visitor);
59+
60+
if (RosettaInterpreterErrorValue.errorsExist(val)) {
61+
return RosettaInterpreterErrorValue.merge(val);
62+
}
63+
64+
List<RosettaInterpreterValue> values =
65+
RosettaInterpreterBaseValue.toValueList(val);
66+
Collections.reverse(values);
67+
68+
return new RosettaInterpreterListValue(values);
69+
}
70+
71+
/**
72+
* Interprets the sum operation.
73+
* Returns a sum of all the summable elements of the list
74+
* If the elements are not summable returns an error
75+
*
76+
* @param exp - Sum operation to interpret
77+
* @return sum of elements or error if elements are not summable
78+
*/
79+
public RosettaInterpreterValue interp(SumOperation exp) {
80+
RosettaExpression expression = exp.getArgument();
81+
RosettaInterpreterValue val = expression.accept(visitor);
82+
83+
if (RosettaInterpreterErrorValue.errorsExist(val)) {
84+
return RosettaInterpreterErrorValue.merge(val);
85+
}
86+
87+
// In the compiler, this returns a null rather than an error
88+
// So I'm not exactly sure how to handle it
89+
if (RosettaInterpreterBaseValue.toValueList(val).size() < 1) {
90+
return new RosettaInterpreterErrorValue(
91+
new RosettaInterpreterError("Cannot take sum"
92+
+ " of empty list"));
93+
}
94+
95+
List<RosettaInterpreterValue> values =
96+
RosettaInterpreterBaseValue.toValueList(val);
97+
98+
// Check that all values are numbers, and convert ints
99+
// to numbers for further simplicity
100+
for (int i = 0; i < values.size(); i++) {
101+
RosettaInterpreterValue v = values.get(i);
102+
if (v instanceof RosettaInterpreterIntegerValue) {
103+
RosettaInterpreterIntegerValue valInt =
104+
(RosettaInterpreterIntegerValue)v;
105+
values.set(i, new RosettaInterpreterNumberValue(
106+
BigDecimal.valueOf(valInt.getValue().longValue())));
107+
}
108+
else if (!(v instanceof RosettaInterpreterNumberValue)) {
109+
return new RosettaInterpreterErrorValue(
110+
new RosettaInterpreterError("Cannot take sum"
111+
+ "of non-number value"));
112+
}
113+
}
114+
115+
RosettaNumber result = values.stream()
116+
.map(x -> ((RosettaInterpreterNumberValue)x).getValue())
117+
.reduce(RosettaNumber.valueOf(BigDecimal.valueOf(0)), (x, y) -> x.add(y));
118+
119+
return new RosettaInterpreterNumberValue(result);
120+
}
121+
122+
123+
}

0 commit comments

Comments
 (0)