-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enum declaration (without the value reference) implementation as a re…
…ference for my ideas
- Loading branch information
Bogdan Damian
committed
Jun 1, 2024
1 parent
84a7d97
commit 9059414
Showing
8 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...n/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterEnumElementValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterEnumValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...om/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterEnumerationInterpreter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...testing/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterEnumTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
|
||
} |