Skip to content

Commit

Permalink
STORY-1740: omit implicit parameter in operations and function calls (f…
Browse files Browse the repository at this point in the history
…inos#457)

* Addressed antrl warnings

* Made left operand syntactically optional; cleaned up AST

* Refactored implicit variable AST

* Fixed tests

* Added parsing tests

* Added scoping test for function symbols

* Adjusted Xsemantics validation filter for generated input

* Restored test

* Added validation

* Added validation redirection test

* Fixed scoping and condition code generator

* Corrected UI test

* Removed plugin.xml_gen from source control

* Corrected condition test

* Fixed invalid cardinality warnings

* Added attributes of implicit variable to scope

* Fixed code generator for attributes of implicit variable

* Added validation tests

* Added test for nested item references

* Cleaned

Co-authored-by: Hugo Hills <39260692+hugohills-regnosys@users.noreply.github.com>
  • Loading branch information
SimonCockx and hugohills-regnosys authored Dec 6, 2022
1 parent 6f894db commit 04b3b3d
Show file tree
Hide file tree
Showing 43 changed files with 1,298 additions and 970 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build.acceleo
.metadata/
*.class
.DS_Store
plugin.xml_gen

**/.settings
**/src-gen/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.regnosys.rosetta.generator.java.condition

import org.junit.jupiter.api.^extension.ExtendWith
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import com.regnosys.rosetta.tests.RosettaInjectorProvider
import org.junit.jupiter.api.Test
import javax.inject.Inject
import com.regnosys.rosetta.tests.util.CodeGeneratorTestHelper

import static com.google.common.collect.ImmutableMap.*
import static org.junit.jupiter.api.Assertions.*
import com.regnosys.rosetta.generator.java.rule.ConditionTestHelper

@ExtendWith(InjectionExtension)
@InjectWith(RosettaInjectorProvider)
class ConditionGeneratorTest {
@Inject extension CodeGeneratorTestHelper
@Inject extension ConditionTestHelper

@Test
def void omittedParameterInConditionTest() {
val code = '''
namespace com.rosetta.test.model
version "${project.version}"
type Foo:
a int (0..1)
condition C:
FooIsValid
func FooIsValid:
inputs: foo Foo (1..1)
output: result boolean (1..1)
set result:
foo -> a exists
'''.generateCode
val classes = code.compileToClasses

val foo1 = classes.createInstanceUsingBuilder('Foo', of('a', 42))
assertTrue(classes.runDataRule(foo1, 'FooC').isSuccess)

val foo2 = classes.createInstanceUsingBuilder('Foo', of())
assertFalse(classes.runDataRule(foo2, 'FooC').isSuccess)
}

@Test
def void useImplicitVariableInConditionTest() {
val code = '''
namespace com.rosetta.test.model
version "${project.version}"
type Foo:
a int (0..1)
condition C:
it -> a exists and [it, it] any = it
'''.generateCode
val classes = code.compileToClasses

val foo1 = classes.createInstanceUsingBuilder('Foo', of('a', 42))
assertTrue(classes.runDataRule(foo1, 'FooC').isSuccess)

val foo2 = classes.createInstanceUsingBuilder('Foo', of())
assertFalse(classes.runDataRule(foo2, 'FooC').isSuccess)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class DocReferenceTest {
func Sum:
[docReference Org1 Agr1 name "something"]
inputs: x number (0..*)
output: sum number (1..1)
output: s number (1..1)
'''.parseRosettaWithNoErrors
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.regnosys.rosetta.generator.java.expression.ExpressionGenerator
import com.regnosys.rosetta.generator.java.expression.ExpressionGenerator.ParamMap
import com.regnosys.rosetta.generator.java.util.ImportingStringConcatination
import com.regnosys.rosetta.rosetta.expression.RosettaBinaryOperation
import com.regnosys.rosetta.rosetta.expression.RosettaCallableCall
import com.regnosys.rosetta.rosetta.RosettaCardinality
import com.regnosys.rosetta.rosetta.expression.RosettaExistsExpression
import com.regnosys.rosetta.rosetta.expression.RosettaExpression
Expand All @@ -32,6 +31,7 @@ import com.regnosys.rosetta.rosetta.expression.CardinalityModifier
import com.regnosys.rosetta.rosetta.expression.ComparisonOperation
import com.regnosys.rosetta.rosetta.expression.EqualityOperation
import com.regnosys.rosetta.rosetta.expression.LogicalOperation
import com.regnosys.rosetta.rosetta.expression.RosettaSymbolReference

@ExtendWith(InjectionExtension)
@InjectWith(RosettaInjectorProvider)
Expand Down Expand Up @@ -194,8 +194,8 @@ class ExpressionGeneratorTest {
}

private def RosettaFeatureCall createFeatureCall(Data rosettaClass, String attributeName) {
val mockCallableCall = mock(RosettaCallableCall)
when(mockCallableCall.callable).thenReturn(rosettaClass)
val mockSymbolReference = mock(RosettaSymbolReference)
when(mockSymbolReference.symbol).thenReturn(rosettaClass)

val mockCardinality = mock(RosettaCardinality)
when(mockCardinality.sup).thenReturn(1)
Expand All @@ -209,7 +209,7 @@ class ExpressionGeneratorTest {

val mockFeatureCall = mock(RosettaFeatureCall)
when(mockFeatureCall.feature).thenReturn(mockAttribute)
when(mockFeatureCall.receiver).thenReturn(mockCallableCall)
when(mockFeatureCall.receiver).thenReturn(mockSymbolReference)
return mockFeatureCall
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import static com.google.common.collect.ImmutableMap.*
import static org.hamcrest.CoreMatchers.*
import static org.hamcrest.MatcherAssert.assertThat
import static org.junit.jupiter.api.Assertions.*
import com.regnosys.rosetta.utils.ImplicitVariableUtil

@ExtendWith(InjectionExtension)
@InjectWith(RosettaInjectorProvider)
class ListOperationTest {

@Inject extension FunctionGeneratorHelper
@Inject extension CodeGeneratorTestHelper
@Inject extension ImplicitVariableUtil

@Test
def void shouldGenerateFunctionWithFilterListItemParameter() {
Expand Down Expand Up @@ -96,7 +98,7 @@ class ListOperationTest {
protected List<Foo.FooBuilder> assignOutput(List<Foo.FooBuilder> filteredFoos, List<? extends Foo> foos) {
filteredFoos = toBuilder(MapperC.of(foos)
.filterItem(_item -> (Boolean)areEqual(_item.<Boolean>map("getInclude", __foo -> __foo.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()).getMulti());
.filterItem(_«defaultImplicitVariable.name» -> (Boolean)areEqual(_«defaultImplicitVariable.name».<Boolean>map("getInclude", __foo -> __foo.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()).getMulti());
return Optional.ofNullable(filteredFoos)
.map(o -> o.stream().map(i -> i.prune()).collect(Collectors.toList()))
Expand Down Expand Up @@ -326,8 +328,8 @@ class ListOperationTest {
protected List<Foo2.Foo2Builder> assignOutput(List<Foo2.Foo2Builder> filteredFoos, List<? extends Foo2> foos) {
filteredFoos = toBuilder(MapperC.of(foos)
.filterItem(_item -> (Boolean)areEqual(_item.<Boolean>map("getInclude", __foo2 -> __foo2.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get())
.filterItem(_item -> (Boolean)areEqual(_item.<Boolean>map("getInclude2", __foo2 -> __foo2.getInclude2()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()).getMulti());
.filterItem(_«defaultImplicitVariable.name» -> (Boolean)areEqual(_«defaultImplicitVariable.name».<Boolean>map("getInclude", __foo2 -> __foo2.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get())
.filterItem(_«defaultImplicitVariable.name» -> (Boolean)areEqual(_«defaultImplicitVariable.name».<Boolean>map("getInclude2", __foo2 -> __foo2.getInclude2()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()).getMulti());
return Optional.ofNullable(filteredFoos)
.map(o -> o.stream().map(i -> i.prune()).collect(Collectors.toList()))
Expand Down Expand Up @@ -424,7 +426,7 @@ class ListOperationTest {
protected List<FooWithScheme.FooWithSchemeBuilder> assignOutput(List<FooWithScheme.FooWithSchemeBuilder> filteredFoos, List<? extends FooWithScheme> foos) {
filteredFoos = toBuilder(MapperC.of(foos)
.filterItem(_item -> (Boolean)areEqual(_item.<FieldWithMetaString>map("getAttr", __fooWithScheme -> __fooWithScheme.getAttr()).map("getMeta", a->a.getMeta()).map("getScheme", a->a.getScheme()), MapperS.of("foo-scheme"), CardinalityOperator.All).get()).getMulti());
.filterItem(_«defaultImplicitVariable.name» -> (Boolean)areEqual(_«defaultImplicitVariable.name».<FieldWithMetaString>map("getAttr", __fooWithScheme -> __fooWithScheme.getAttr()).map("getMeta", a->a.getMeta()).map("getScheme", a->a.getScheme()), MapperS.of("foo-scheme"), CardinalityOperator.All).get()).getMulti());
return Optional.ofNullable(filteredFoos)
.map(o -> o.stream().map(i -> i.prune()).collect(Collectors.toList()))
Expand Down Expand Up @@ -592,7 +594,7 @@ class ListOperationTest {
protected List<Boolean> assignOutput(List<Boolean> filteredFoos, List<Boolean> foos) {
filteredFoos = MapperC.of(foos)
.filterItem(_item -> (Boolean)areEqual(_item, MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()).getMulti();
.filterItem(_«defaultImplicitVariable.name» -> (Boolean)areEqual(_«defaultImplicitVariable.name», MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()).getMulti();
return filteredFoos;
}
Expand Down Expand Up @@ -989,12 +991,12 @@ class ListOperationTest {
protected List<Foo.FooBuilder> assignOutput(List<Foo.FooBuilder> foos, Bar bar) {
foos = toBuilder(MapperS.of(bar).<Foo>mapC("getFoos", _bar -> _bar.getFoos())
.mapItem(_item -> (MapperS<Foo>)com.rosetta.model.lib.mapper.MapperUtils.fromDataType(() -> {
if (areEqual(_item.<Boolean>map("getInclude", __foo -> __foo.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()) {
return MapperS.of(create_Foo.evaluate(_item.<Boolean>map("getInclude", __foo -> __foo.getInclude()).get(), MapperMaths.<String, String, String>add(_item.<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_bar")).get()));
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<Foo>)com.rosetta.model.lib.mapper.MapperUtils.fromDataType(() -> {
if (areEqual(_«defaultImplicitVariable.name».<Boolean>map("getInclude", __foo -> __foo.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get()) {
return MapperS.of(create_Foo.evaluate(_«defaultImplicitVariable.name».<Boolean>map("getInclude", __foo -> __foo.getInclude()).get(), MapperMaths.<String, String, String>add(_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_bar")).get()));
}
else {
return _item;
return _«defaultImplicitVariable.name»;
}
})).getMulti());
Expand Down Expand Up @@ -1288,7 +1290,7 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> strings, List<? extends Foo> foos) {
strings = MapperC.of(foos)
.mapItem(_item -> (MapperS<String>)_item.<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
return strings;
}
Expand Down Expand Up @@ -1748,9 +1750,9 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> attrs, List<? extends Bar> bars) {
attrs = MapperC.of(bars)
.mapItemToList(_item -> (MapperC<Foo>)_item.<Foo>mapC("getFoos", __bar -> __bar.getFoos()))
.mapItemToList(_«defaultImplicitVariable.name» -> (MapperC<Foo>)_«defaultImplicitVariable.name».<Foo>mapC("getFoos", __bar -> __bar.getFoos()))
.flattenList()
.mapItem(_item -> (MapperS<String>)_item.<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
return attrs;
}
Expand Down Expand Up @@ -2229,8 +2231,8 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> newFoos, List<? extends Foo> foos) {
newFoos = MapperC.of(foos)
.filterItem(_item -> (Boolean)areEqual(_item.<Boolean>map("getInclude", __foo -> __foo.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get())
.mapItem(_item -> (MapperS<String>)_item.<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
.filterItem(_«defaultImplicitVariable.name» -> (Boolean)areEqual(_«defaultImplicitVariable.name».<Boolean>map("getInclude", __foo -> __foo.getInclude()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All).get())
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
return newFoos;
}
Expand Down Expand Up @@ -2330,8 +2332,8 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> strings, List<? extends Bar> bars) {
strings = MapperC.of(bars)
.mapItem(_item -> (MapperS<Foo>)MapperS.of(getFoo.evaluate(_item.<String>map("getBarAttr", __bar -> __bar.getBarAttr()).get())))
.mapItem(_item -> (MapperS<String>)_item.<String>map("getFooAttr", __foo -> __foo.getFooAttr())).getMulti();
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<Foo>)MapperS.of(getFoo.evaluate(_«defaultImplicitVariable.name».<String>map("getBarAttr", __bar -> __bar.getBarAttr()).get())))
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getFooAttr", __foo -> __foo.getFooAttr())).getMulti();
return strings;
}
Expand Down Expand Up @@ -2409,9 +2411,9 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> strings, List<? extends Bar> bars) {
strings = MapperC.of(bars)
.mapItemToList(_item -> (MapperC<Foo>)_item.<Foo>mapC("getFoos", __bar -> __bar.getFoos()))
.mapItemToList(_«defaultImplicitVariable.name» -> (MapperC<Foo>)_«defaultImplicitVariable.name».<Foo>mapC("getFoos", __bar -> __bar.getFoos()))
.flattenList()
.mapItem(_item -> (MapperS<String>)_item.<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr())).getMulti();
return strings;
}
Expand Down Expand Up @@ -2501,8 +2503,8 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> strings, List<? extends Bar> bars) {
strings = MapperC.of(bars)
.mapItem(_item -> (MapperS<Foo>)MapperS.of(getFoo.evaluate(_item.<String>map("getBarAttr", __bar -> __bar.getBarAttr()).get())))
.mapItem(_item -> (MapperS<String>)_item.<String>map("getFooAttr", __foo -> __foo.getFooAttr())).getMulti();
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<Foo>)MapperS.of(getFoo.evaluate(_«defaultImplicitVariable.name».<String>map("getBarAttr", __bar -> __bar.getBarAttr()).get())))
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getFooAttr", __foo -> __foo.getFooAttr())).getMulti();
return strings;
}
Expand Down Expand Up @@ -2604,8 +2606,8 @@ class ListOperationTest {
protected List<String> assignOutput(List<String> strings, List<? extends Bar> bars) {
strings = MapperC.of(bars)
.mapItem(_item -> (MapperS<Foo>)MapperS.of(getFoo.evaluate(MapperS.of(getBaz.evaluate(_item.<String>map("getBarAttr", __bar -> __bar.getBarAttr()).get())).get())))
.mapItem(_item -> (MapperS<String>)_item.<String>map("getFooAttr", __foo -> __foo.getFooAttr())).getMulti();
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<Foo>)MapperS.of(getFoo.evaluate(MapperS.of(getBaz.evaluate(_«defaultImplicitVariable.name».<String>map("getBarAttr", __bar -> __bar.getBarAttr()).get())).get())))
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getFooAttr", __foo -> __foo.getFooAttr())).getMulti();
return strings;
}
Expand Down Expand Up @@ -2684,15 +2686,15 @@ class ListOperationTest {
strings = com.rosetta.model.lib.mapper.MapperUtils.fromBuiltInType(() -> {
if (areEqual(MapperS.of(test), MapperS.of("a"), CardinalityOperator.All).get()) {
return MapperC.of(foos)
.mapItem(_item -> (MapperS<String>)MapperMaths.<String, String, String>add(_item.<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_a")));
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)MapperMaths.<String, String, String>add(_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_a")));
}
else if (areEqual(MapperS.of(test), MapperS.of("b"), CardinalityOperator.All).get()) {
return MapperC.of(foos)
.mapItem(_item -> (MapperS<String>)MapperMaths.<String, String, String>add(_item.<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_b")));
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)MapperMaths.<String, String, String>add(_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_b")));
}
else if (areEqual(MapperS.of(test), MapperS.of("c"), CardinalityOperator.All).get()) {
return MapperC.of(foos)
.mapItem(_item -> (MapperS<String>)MapperMaths.<String, String, String>add(_item.<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_c")));
.mapItem(_«defaultImplicitVariable.name» -> (MapperS<String>)MapperMaths.<String, String, String>add(_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr()), MapperS.of("_c")));
}
else {
return MapperC.ofNull();
Expand Down Expand Up @@ -3747,7 +3749,7 @@ class ListOperationTest {
protected List<Foo.FooBuilder> assignOutput(List<Foo.FooBuilder> sortedFoos, List<? extends Foo> foos) {
sortedFoos = toBuilder(MapperC.of(foos)
.sort(_item -> (MapperS<String>)_item.<String>map("getAttr", __foo -> __foo.getAttr())).getMulti());
.sort(_«defaultImplicitVariable.name» -> (MapperS<String>)_«defaultImplicitVariable.name».<String>map("getAttr", __foo -> __foo.getAttr())).getMulti());
return Optional.ofNullable(sortedFoos)
.map(o -> o.stream().map(i -> i.prune()).collect(Collectors.toList()))
Expand Down
Loading

0 comments on commit 04b3b3d

Please sign in to comment.