Skip to content

Commit

Permalink
Enum declaration (without the value reference) implementation as a re…
Browse files Browse the repository at this point in the history
…ference for my ideas
  • Loading branch information
Bogdan Damian committed Jun 1, 2024
1 parent 84a7d97 commit 9059414
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rosetta-lang/model/Rosetta.xcore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import com.regnosys.rosetta.rosetta.simple.Attribute
import com.regnosys.rosetta.rosetta.simple.RosettaRuleReference
import com.regnosys.rosetta.rosetta.expression.RosettaExpression


import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue
import com.regnosys.rosetta.rosetta.interpreter.InterpreterVisitor

import com.regnosys.rosetta.rosetta.RosettaInterpreterBaseEnvironment

class RosettaModel extends RosettaDefinable {
String name
String version = "0.0.0"
Expand Down Expand Up @@ -143,6 +149,10 @@ class RosettaEnumeration extends RosettaRootElement, RosettaType, RosettaDefinab
refers RosettaEnumeration superType
contains RosettaSynonym[] synonyms
contains RosettaEnumValue[] enumValues opposite enumeration

op RosettaInterpreterBaseEnvironment accept(InterpreterVisitor v, RosettaInterpreterBaseEnvironment nv) {
v.interp(this,nv)
}
}

class RosettaEnumValue extends RosettaNamed, RosettaDefinable, RosettaFeature, References {
Expand Down
2 changes: 2 additions & 0 deletions rosetta-lang/model/RosettaInterpreter.xcore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.regnosys.rosetta.rosetta.expression.ComparisonOperation

import com.regnosys.rosetta.rosetta.expression.RosettaSymbolReference
import com.regnosys.rosetta.rosetta.RosettaInterpreterBaseEnvironment
import com.regnosys.rosetta.rosetta.RosettaEnumeration
import com.regnosys.rosetta.rosetta.expression.ArithmeticOperation
import com.regnosys.rosetta.rosetta.expression.RosettaContainsExpression
import com.regnosys.rosetta.rosetta.expression.RosettaDisjointExpression
Expand Down Expand Up @@ -65,6 +66,7 @@ interface InterpreterVisitor {
op RosettaInterpreterValue interp (DistinctOperation exp, RosettaInterpreterBaseEnvironment env)
op RosettaInterpreterValue interp (ReverseOperation exp, RosettaInterpreterBaseEnvironment env)
op RosettaInterpreterValue interp (SumOperation exp, RosettaInterpreterBaseEnvironment env)
op RosettaInterpreterBaseEnvironment interp (RosettaEnumeration exp, RosettaInterpreterBaseEnvironment env)


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.inject.Inject;

import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.RosettaInterpreterBaseEnvironment;
import com.regnosys.rosetta.rosetta.expression.RosettaExpression;
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;
Expand All @@ -27,4 +28,9 @@ public RosettaInterpreterValue interp(RosettaExpression expression,
RosettaInterpreterBaseEnvironment env) {
return expression.accept(visitor, env);
}

public RosettaInterpreterEnvironment interp(RosettaEnumeration expression,
RosettaInterpreterBaseEnvironment env) {
return (RosettaInterpreterEnvironment) expression.accept(visitor, env);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.regnosys.rosetta.rosetta.expression.ArithmeticOperation;
import com.regnosys.rosetta.rosetta.expression.LogicalOperation;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.RosettaInterpreterBaseEnvironment;
import com.regnosys.rosetta.rosetta.expression.ReverseOperation;
import com.regnosys.rosetta.rosetta.expression.ComparisonOperation;
Expand Down Expand Up @@ -31,6 +32,7 @@
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue;
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterComparisonOperationInterpreter;
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterEnumerationInterpreter;
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterListLiteralInterpreter;
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterRosettaArithmeticOperationsInterpreter;
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterListOperationsInterpreter;
Expand Down Expand Up @@ -212,5 +214,12 @@ public RosettaInterpreterValue interp(SumOperation exp,
return new RosettaInterpreterListOperatorInterpreter().interp(exp,
(RosettaInterpreterEnvironment) env);
}

@Override
public RosettaInterpreterEnvironment interp(RosettaEnumeration exp,
RosettaInterpreterBaseEnvironment env) {
return new RosettaInterpreterEnumerationInterpreter().interp(exp,
(RosettaInterpreterEnvironment) env);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.regnosys.rosetta.interpreternew.values;

import java.util.Objects;
import java.util.stream.Stream;

import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;

public class RosettaInterpreterEnumElementValue extends RosettaInterpreterBaseValue {

private String enumName;
private String value;

/**
* Constructor for an Enum Element Value.
*
* @param n Name of the Enum
* @param v The String value
*/
public RosettaInterpreterEnumElementValue(String n, String v) {
super();
this.enumName = n;
this.value = v;
}

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public String toString() {
return "RosettaInterpreterEnumElementValue [value=" + value + "]";
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
RosettaInterpreterEnumElementValue other = (RosettaInterpreterEnumElementValue) obj;
return Objects.equals(value, other.value) && Objects.equals(enumName, other.enumName);
}

public String getValue() { return value; }

public String getEnumName() { return enumName; }

@Override
public Stream<Object> toElementStream() {
return Stream.of(value);
}

@Override
public Stream<RosettaInterpreterValue> toValueStream() {
return Stream.of(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.regnosys.rosetta.interpreternew.values;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;

public class RosettaInterpreterEnumValue extends RosettaInterpreterBaseValue {

private String name;
private List<RosettaInterpreterValue> values;

/**
* Constructor for an Enum Value.
*
* @param name Name of the Enum
* @param values A list of all the String values that the Enum accepts
*/
public RosettaInterpreterEnumValue(String name, List<RosettaInterpreterValue> values) {
super();
this.name = name;
this.values = values;
}

@Override
public int hashCode() {
return Objects.hash(values);
}

@Override
public String toString() {
return "RosettaInterpreterListValue [name = " + name + ", values=" + values.toString() + "]";
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
RosettaInterpreterEnumValue other = (RosettaInterpreterEnumValue) obj;
return Objects.equals(values, other.values) && Objects.equals(name, other.name);
}

public List<RosettaInterpreterValue> getValues() { return values; }


public String getName() { return name; }

@Override
public Stream<Object> toElementStream() {
return Stream.of(values.toArray());
}

@Override
public Stream<RosettaInterpreterValue> toValueStream() {
return values.stream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.regnosys.rosetta.interpreternew.visitors;

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

import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnumElementValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnumValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
import com.regnosys.rosetta.rosetta.RosettaEnumValue;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue;

public class RosettaInterpreterEnumerationInterpreter
extends RosettaInterpreterConcreteInterpreter {

public RosettaInterpreterEnumerationInterpreter() {
super();
}

/**
* Interprets a list literal, evaluating it to a list value.
*
* @param exp the expression to be interpreted
* @return the list value it represents
*/
public RosettaInterpreterEnvironment interp(RosettaEnumeration exp,
RosettaInterpreterEnvironment env) {
String enumName = exp.getName();
List<RosettaInterpreterValue> values = new ArrayList<>();
for (RosettaEnumValue v : exp.getEnumValues()) {
values.add(new RosettaInterpreterEnumElementValue(
enumName, v.getName()));
}
RosettaInterpreterEnumValue enumeration =
new RosettaInterpreterEnumValue(enumName, values);
env.addValue(enumName, enumeration);

return env;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.regnosys.rosetta.interpreternew;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import javax.inject.Inject;

import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnumValue;
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnumElementValue;
import com.regnosys.rosetta.rosetta.RosettaModel;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
//import com.regnosys.rosetta.rosetta.expression.RosettaExpression;
//import com.regnosys.rosetta.rosetta.simple.impl.FunctionImpl;
import com.regnosys.rosetta.tests.RosettaInjectorProvider;
import com.regnosys.rosetta.tests.util.ModelHelper;

@ExtendWith(InjectionExtension.class)
@InjectWith(RosettaInjectorProvider.class)
public class RosettaInterpreterEnumTest {

@Inject
RosettaInterpreterNew interpreter;

@Inject
ModelHelper mh;

// private ExpressionFactory exFactory;
//
// @BeforeEach
// public void setup() {
// exFactory = ExpressionFactoryImpl.init();
//
// }

@Test
public void enumAddsToEnvironmentTest() {
RosettaModel model = mh.parseRosettaWithNoErrors("enum Foo:\r\n"
+ " VALU_E1 displayName \"VALU.E1\"\r\n"
+ " VALUE2\r\n"
+ "\r\n"
+ "func MyTest:\r\n"
+ " output: result Foo (1..1)\r\n"
+ " set result:\r\n"
+ " Foo -> VALU_E1");
RosettaInterpreterEnvironment expectedEnv =
new RosettaInterpreterEnvironment();
expectedEnv.addValue("Foo",
new RosettaInterpreterEnumValue("Foo",
List.of(new RosettaInterpreterEnumElementValue("Foo", "VALU_E1"),
new RosettaInterpreterEnumElementValue("Foo", "VALUE2"))));
RosettaInterpreterEnvironment actualEnv =
new RosettaInterpreterEnvironment();
RosettaEnumeration enumeration = (RosettaEnumeration) model.getElements().get(0);
RosettaInterpreterEnvironment env = (RosettaInterpreterEnvironment)
interpreter.interp(enumeration, actualEnv);
assertEquals((RosettaInterpreterEnvironment) env,
(RosettaInterpreterEnvironment) expectedEnv);
}

// @Test
// public void enumRefTest() {
// RosettaModel model = mh.parseRosettaWithNoErrors("enum Foo:\r\n"
// + " VALU_E1 displayName \"VALU.E1\"\r\n"
// + " VALUE2\r\n"
// + "\r\n"
// + "func MyTest:\r\n"
// + " output: result Foo (1..1)\r\n"
// + " set result:\r\n"
// + " Foo -> VALU_E1");
// RosettaInterpreterEnvironment expectedEnv =
// new RosettaInterpreterEnvironment();
// expectedEnv.addValue("Foo",
// new RosettaInterpreterEnumValue("Foo",
// List.of(new RosettaInterpreterEnumElementValue("Foo", "VALU_E1"),
// new RosettaInterpreterEnumElementValue("Foo", "VALUE2"))));
// RosettaInterpreterEnvironment actualEnv =
// new RosettaInterpreterEnvironment();
// RosettaEnumeration enumeration = (RosettaEnumeration) model.getElements().get(0);
// RosettaExpression refCall = ((FunctionImpl) model.getElements().get(1)).getOperations()
// .get(0).getExpression();
// RosettaInterpreterValue env = (RosettaInterpreterEnvironment)
// interpreter.interp(refCall, actualEnv);
// assertEquals((RosettaInterpreterEnvironment) env,
// (RosettaInterpreterEnvironment) expectedEnv);
// }

}

0 comments on commit 9059414

Please sign in to comment.