From 97d48c84d81d4701644c3f84a669b89aa3ec106b Mon Sep 17 00:00:00 2001 From: Simon Cockx Date: Mon, 20 Nov 2023 15:47:16 +0100 Subject: [PATCH] Fixed implicit item bug --- rosetta-lang/model/RosettaExpression.xcore | 22 ++++--------------- .../RosettaDerivedStateComputer.java | 17 ++++++++++++++ .../RosettaTransientValueService.java | 3 ++- .../rosetta/tests/RosettaParsingTest.xtend | 1 - .../validation/RosettaValidatorTest.xtend | 15 +++++++++++++ 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/rosetta-lang/model/RosettaExpression.xcore b/rosetta-lang/model/RosettaExpression.xcore index 5a1ca82d5..88268612a 100644 --- a/rosetta-lang/model/RosettaExpression.xcore +++ b/rosetta-lang/model/RosettaExpression.xcore @@ -116,16 +116,17 @@ class ListLiteral extends RosettaExpression { abstract class RosettaReference extends RosettaExpression { } -class RosettaSymbolReference extends RosettaReference, HasGeneratedInput { +class RosettaSymbolReference extends RosettaReference { refers RosettaSymbol symbol boolean explicitArguments contains RosettaExpression[] rawArgs - contains RosettaImplicitVariable implicitArgument + boolean implicitVariableIsInContext + contains RosettaExpression implicitArgument op RosettaExpression[] getArgs() { - if (!explicitArguments && symbol instanceof RosettaCallableWithArgs && (symbol as RosettaCallableWithArgs).numberOfParameters == 1) { + if (!explicitArguments && implicitVariableIsInContext && symbol instanceof RosettaCallableWithArgs && (symbol as RosettaCallableWithArgs).numberOfParameters == 1) { if (implicitArgument === null) { val def = ExpressionFactory.eINSTANCE.createRosettaImplicitVariable(); def.setName("item"); @@ -136,21 +137,6 @@ class RosettaSymbolReference extends RosettaReference, HasGeneratedInput { } return rawArgs } - op void setGeneratedInputIfAbsent(RosettaExpression e) { - if (e === null) { - if (generatedInputWasSet) { - implicitArgument = null - generatedInputWasSet = false - } - } else { - if (!generatedInputWasSet) { - generatedInputWasSet = true - } - } - } - op boolean needsGeneratedInput() { - false - } } class RosettaImplicitVariable extends RosettaReference, RosettaNamed { diff --git a/rosetta-lang/src/main/java/com/regnosys/rosetta/derivedstate/RosettaDerivedStateComputer.java b/rosetta-lang/src/main/java/com/regnosys/rosetta/derivedstate/RosettaDerivedStateComputer.java index 77f6154b2..568b9ccd4 100644 --- a/rosetta-lang/src/main/java/com/regnosys/rosetta/derivedstate/RosettaDerivedStateComputer.java +++ b/rosetta-lang/src/main/java/com/regnosys/rosetta/derivedstate/RosettaDerivedStateComputer.java @@ -13,6 +13,7 @@ import com.regnosys.rosetta.rosetta.expression.ListLiteral; import com.regnosys.rosetta.rosetta.expression.RosettaConditionalExpression; import com.regnosys.rosetta.rosetta.expression.RosettaStringLiteral; +import com.regnosys.rosetta.rosetta.expression.RosettaSymbolReference; import com.regnosys.rosetta.utils.ImplicitVariableUtil; /** @@ -43,6 +44,8 @@ public void setDerivedState(EObject obj) { this.setDefaultElseToEmpty((RosettaConditionalExpression)obj); } else if (obj instanceof JoinOperation) { this.setDefaultJoinSeparator((JoinOperation)obj); + } else if (obj instanceof RosettaSymbolReference) { + this.setImplicitVariableInContextOfSymbolReference((RosettaSymbolReference)obj); } if (obj instanceof HasGeneratedInput) { this.setDefaultInput((HasGeneratedInput)obj); @@ -62,6 +65,8 @@ public void removeDerivedState(EObject obj) { this.discardDefaultElse((RosettaConditionalExpression)obj); } else if (obj instanceof JoinOperation) { this.discardDefaultJoinSeparator((JoinOperation)obj); + } else if (obj instanceof RosettaSymbolReference) { + this.discardImplicitVariableInContextOfSymbolReference((RosettaSymbolReference)obj); } if (obj instanceof HasGeneratedInput) { this.discardDefaultInput((HasGeneratedInput)obj); @@ -118,4 +123,16 @@ private void discardDefaultJoinSeparator(JoinOperation expr) { } } } + + private void setImplicitVariableInContextOfSymbolReference(RosettaSymbolReference expr) { + if (implicitVariableUtil.implicitVariableExistsInContext(expr)) { + expr.setImplicitVariableIsInContext(true); + } + } + private void discardImplicitVariableInContextOfSymbolReference(RosettaSymbolReference expr) { + if (expr.isImplicitVariableIsInContext()) { + expr.setImplicitVariableIsInContext(false); + expr.setImplicitArgument(null); + } + } } diff --git a/rosetta-lang/src/main/java/com/regnosys/rosetta/serialization/RosettaTransientValueService.java b/rosetta-lang/src/main/java/com/regnosys/rosetta/serialization/RosettaTransientValueService.java index 5655955cd..2d34fb88a 100644 --- a/rosetta-lang/src/main/java/com/regnosys/rosetta/serialization/RosettaTransientValueService.java +++ b/rosetta-lang/src/main/java/com/regnosys/rosetta/serialization/RosettaTransientValueService.java @@ -11,6 +11,7 @@ public class RosettaTransientValueService extends DefaultTransientValueService { private EStructuralFeature generatedInputWasSetFeature = ExpressionPackage.eINSTANCE.getHasGeneratedInput_GeneratedInputWasSet(); + private EStructuralFeature implicitVariableIsInContextFeature = ExpressionPackage.eINSTANCE.getRosettaSymbolReference_ImplicitVariableIsInContext(); @Override public boolean isCheckElementsIndividually(EObject owner, EStructuralFeature feature) { @@ -22,7 +23,7 @@ public boolean isTransient(EObject owner, EStructuralFeature feature, int index) if (super.isTransient(owner, feature, index)) { return true; } - if (feature.equals(generatedInputWasSetFeature)) { + if (feature.equals(generatedInputWasSetFeature) || feature.equals(implicitVariableIsInContextFeature)) { return true; } Object value = owner.eGet(feature); diff --git a/rosetta-testing/src/test/java/com/regnosys/rosetta/tests/RosettaParsingTest.xtend b/rosetta-testing/src/test/java/com/regnosys/rosetta/tests/RosettaParsingTest.xtend index d9c67290d..ccabc07b2 100644 --- a/rosetta-testing/src/test/java/com/regnosys/rosetta/tests/RosettaParsingTest.xtend +++ b/rosetta-testing/src/test/java/com/regnosys/rosetta/tests/RosettaParsingTest.xtend @@ -353,7 +353,6 @@ class RosettaParsingTest { model.elements.head as Function => [ operations.head.expression as RosettaSymbolReference => [ assertTrue(explicitArguments) - assertFalse(needsGeneratedInput) ] ] } diff --git a/rosetta-testing/src/test/java/com/regnosys/rosetta/validation/RosettaValidatorTest.xtend b/rosetta-testing/src/test/java/com/regnosys/rosetta/validation/RosettaValidatorTest.xtend index e85ebe31a..68c0a08f8 100644 --- a/rosetta-testing/src/test/java/com/regnosys/rosetta/validation/RosettaValidatorTest.xtend +++ b/rosetta-testing/src/test/java/com/regnosys/rosetta/validation/RosettaValidatorTest.xtend @@ -29,7 +29,22 @@ class RosettaValidatorTest implements RosettaIssueCodes { @Inject extension ValidationTestHelper @Inject extension ModelHelper + + @Test + def void testCannotCallFuncWithoutInput() { + val model = ''' + func Foo: + inputs: a int (1..1) + output: result int (1..1) + set result: + Foo + '''.parseRosetta + model.assertError(ROSETTA_SYMBOL_REFERENCE, null, + "Invalid number of arguments. Expecting 1 but passed 0." + ) + } + @Test def void testOrderDoesNotMatter() { val model1 = '''