From 7b561c87e10d5d33aae3355c910029d993d46351 Mon Sep 17 00:00:00 2001 From: Ioannis Rousochatzakis Date: Fri, 20 Oct 2023 13:38:14 +0200 Subject: [PATCH] Sequences of labels now render distinct values --- pom.xml | 2 +- .../ted/efx/sdk1/EfxTemplateTranslatorV1.java | 60 ++++++++++-------- .../ted/efx/sdk2/EfxTemplateTranslatorV2.java | 62 +++++++++++-------- .../efx/sdk1/EfxTemplateTranslatorV1Test.java | 16 ++--- .../efx/sdk2/EfxTemplateTranslatorV2Test.java | 16 ++--- 5 files changed, 85 insertions(+), 71 deletions(-) diff --git a/pom.xml b/pom.xml index 561e15a..c0b128a 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ ${project.build.directory}/eforms-sdk/antlr4 - 1.0.6-SNAPSHOT + 1.2.0-SNAPSHOT 4.9.3 diff --git a/src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java b/src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java index 460a4eb..983a397 100644 --- a/src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java +++ b/src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java @@ -303,36 +303,42 @@ private void shorthandIndirectLabelReference(final String fieldId) { this.script.composeVariableReference("item", StringExpression.class)); switch (fieldType) { case "indicator": - this.stack.push(this.markup.renderLabelFromExpression(this.script.composeForExpression( - this.script.composeIteratorList( - List.of( - this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), - this.script.composeStringConcatenation( - List.of(this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_INDICATOR), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_WHEN), - this.script.getStringLiteralFromUnquotedString("-"), - new StringExpression(loopVariable.referenceExpression.getScript()), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString(fieldId))), - StringSequenceExpression.class))); + this.stack.push(this.markup.renderLabelFromExpression( + this.script.composeDistinctValuesFunction( + this.script.composeForExpression( + this.script.composeIteratorList( + List.of( + this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), + this.script.composeStringConcatenation( + List.of(this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_INDICATOR), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_WHEN), + this.script.getStringLiteralFromUnquotedString("-"), + new StringExpression(loopVariable.referenceExpression.getScript()), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString(fieldId))), + StringSequenceExpression.class), + StringSequenceExpression.class))); break; case "code": case "internal-code": - this.stack.push(this.markup.renderLabelFromExpression(this.script.composeForExpression( - this.script.composeIteratorList( - List.of( - this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), - this.script.composeStringConcatenation(List.of( - this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_CODE), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_NAME), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString( - this.symbols.getRootCodelistOfField(fieldId)), - this.script.getStringLiteralFromUnquotedString("."), - new StringExpression(loopVariable.referenceExpression.getScript()))), - StringSequenceExpression.class))); + this.stack.push(this.markup.renderLabelFromExpression( + this.script.composeDistinctValuesFunction( + this.script.composeForExpression( + this.script.composeIteratorList( + List.of( + this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), + this.script.composeStringConcatenation(List.of( + this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_CODE), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_NAME), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString( + this.symbols.getRootCodelistOfField(fieldId)), + this.script.getStringLiteralFromUnquotedString("."), + new StringExpression(loopVariable.referenceExpression.getScript()))), + StringSequenceExpression.class), + StringSequenceExpression.class))); break; default: throw new ParseCancellationException(String.format( diff --git a/src/main/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2.java b/src/main/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2.java index ed39da2..650843b 100644 --- a/src/main/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2.java +++ b/src/main/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2.java @@ -347,36 +347,44 @@ private void shorthandIndirectLabelReference(final String fieldId, final Numeric this.script.composeVariableReference("item", StringExpression.class)); switch (fieldType) { case "indicator": - this.stack.push(this.markup.renderLabelFromExpression(this.script.composeForExpression( - this.script.composeIteratorList( - List.of( - this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), - this.script.composeStringConcatenation( - List.of(this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_INDICATOR), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_WHEN), - this.script.getStringLiteralFromUnquotedString("-"), - new StringExpression(loopVariable.referenceExpression.getScript()), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString(fieldId))), - StringSequenceExpression.class), quantity)); + this.stack.push(this.markup.renderLabelFromExpression( + this.script.composeDistinctValuesFunction( + this.script.composeForExpression( + this.script.composeIteratorList( + List.of( + this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), + this.script.composeStringConcatenation( + List.of(this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_INDICATOR), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_WHEN), + this.script.getStringLiteralFromUnquotedString("-"), + new StringExpression(loopVariable.referenceExpression.getScript()), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString(fieldId))), + StringSequenceExpression.class), + StringSequenceExpression.class), + quantity)); break; case "code": case "internal-code": - this.stack.push(this.markup.renderLabelFromExpression(this.script.composeForExpression( - this.script.composeIteratorList( - List.of( - this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), - this.script.composeStringConcatenation(List.of( - this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_CODE), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_NAME), - this.script.getStringLiteralFromUnquotedString("|"), - this.script.getStringLiteralFromUnquotedString( - this.symbols.getRootCodelistOfField(fieldId)), - this.script.getStringLiteralFromUnquotedString("."), - new StringExpression(loopVariable.referenceExpression.getScript()))), - StringSequenceExpression.class), quantity)); + this.stack.push(this.markup.renderLabelFromExpression( + this.script.composeDistinctValuesFunction( + this.script.composeForExpression( + this.script.composeIteratorList( + List.of( + this.script.composeIteratorExpression(loopVariable.declarationExpression, valueReference))), + this.script.composeStringConcatenation(List.of( + this.script.getStringLiteralFromUnquotedString(ASSET_TYPE_CODE), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString(LABEL_TYPE_NAME), + this.script.getStringLiteralFromUnquotedString("|"), + this.script.getStringLiteralFromUnquotedString( + this.symbols.getRootCodelistOfField(fieldId)), + this.script.getStringLiteralFromUnquotedString("."), + new StringExpression(loopVariable.referenceExpression.getScript()))), + StringSequenceExpression.class), + StringSequenceExpression.class), + quantity)); break; default: throw new ParseCancellationException(String.format( diff --git a/src/test/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1Test.java b/src/test/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1Test.java index a98697b..7d3b3d9 100644 --- a/src/test/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1Test.java +++ b/src/test/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1Test.java @@ -203,42 +203,42 @@ void testShorthandBtLabelReference_MissingLabelType() { @Test void testShorthandIndirectLabelReferenceForIndicator() { assertEquals( - "let block01() -> { label(for $item in ../IndicatorField return concat('indicator', '|', 'when', '-', $item, '|', 'BT-00-Indicator')) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../IndicatorField return concat('indicator', '|', 'when', '-', $item, '|', 'BT-00-Indicator'))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-Indicator}")); } @Test void testShorthandIndirectLabelReferenceForCode() { assertEquals( - "let block01() -> { label(for $item in ../CodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../CodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-Code}")); } @Test void testShorthandIndirectLabelReferenceForInternalCode() { assertEquals( - "let block01() -> { label(for $item in ../InternalCodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../InternalCodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-Internal-Code}")); } @Test void testShorthandIndirectLabelReferenceForCodeAttribute() { assertEquals( - "let block01() -> { label(for $item in ../CodeField/@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../CodeField/@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-CodeAttribute}")); } @Test void testShorthandIndirectLabelReferenceForCodeAttribute_WithSameAttributeInContext() { assertEquals( - "let block01() -> { label(for $item in ../@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/CodeField/@attribute).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/CodeField/@attribute).call(block01())", translateTemplate("{BT-00-CodeAttribute} #{BT-00-CodeAttribute}")); } @Test void testShorthandIndirectLabelReferenceForCodeAttribute_WithSameElementInContext() { assertEquals( - "let block01() -> { label(for $item in ./@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/CodeField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ./@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/CodeField).call(block01())", translateTemplate("{BT-00-Code} #{BT-00-CodeAttribute}")); } @@ -297,7 +297,7 @@ void testShorthandLabelReferenceFromContext_WithNodeContext() { @Test void testShorthandIndirectLabelReferenceFromContextField() { assertEquals( - "let block01() -> { label(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/CodeField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/CodeField).call(block01())", translateTemplate("{BT-00-Code} #value")); } @@ -318,7 +318,7 @@ void testShorthandFieldValueReferenceFromContextField() { @Test void testShorthandFieldValueReferenceFromContextField_WithText() { assertEquals( - "let block01() -> { text('blah ')label(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))text(' ')text('blah ')eval(./normalize-space(text()))text(' ')text('blah') }\nfor-each(/*/PathNode/CodeField).call(block01())", + "let block01() -> { text('blah ')label(distinct-values(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)))text(' ')text('blah ')eval(./normalize-space(text()))text(' ')text('blah') }\nfor-each(/*/PathNode/CodeField).call(block01())", translateTemplate("{BT-00-Code} blah #value blah $value blah")); } diff --git a/src/test/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2Test.java b/src/test/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2Test.java index f78cc07..127f911 100644 --- a/src/test/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2Test.java +++ b/src/test/java/eu/europa/ted/efx/sdk2/EfxTemplateTranslatorV2Test.java @@ -292,42 +292,42 @@ void testShorthandBtLabelReference_MissingLabelType() { @Test void testShorthandIndirectLabelReferenceForIndicator() { assertEquals( - "let block01() -> { label(for $item in ../IndicatorField return concat('indicator', '|', 'when', '-', $item, '|', 'BT-00-Indicator')) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../IndicatorField return concat('indicator', '|', 'when', '-', $item, '|', 'BT-00-Indicator'))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-Indicator}")); } @Test void testShorthandIndirectLabelReferenceForCode() { assertEquals( - "let block01() -> { label(for $item in ../CodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../CodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-Code}")); } @Test void testShorthandIndirectLabelReferenceForInternalCode() { assertEquals( - "let block01() -> { label(for $item in ../InternalCodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../InternalCodeField/normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-Internal-Code}")); } @Test void testShorthandIndirectLabelReferenceForCodeAttribute() { assertEquals( - "let block01() -> { label(for $item in ../CodeField/@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/TextField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../CodeField/@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())", translateTemplate("{BT-00-Text} #{BT-00-CodeAttribute}")); } @Test void testShorthandIndirectLabelReferenceForCodeAttribute_WithSameAttributeInContext() { assertEquals( - "let block01() -> { label(for $item in ../@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/CodeField/@attribute).call(block01())", + "let block01() -> { label(distinct-values(for $item in ../@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/CodeField/@attribute).call(block01())", translateTemplate("{BT-00-CodeAttribute} #{BT-00-CodeAttribute}")); } @Test void testShorthandIndirectLabelReferenceForCodeAttribute_WithSameElementInContext() { assertEquals( - "let block01() -> { label(for $item in ./@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/CodeField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ./@attribute return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/CodeField).call(block01())", translateTemplate("{BT-00-Code} #{BT-00-CodeAttribute}")); } @@ -386,7 +386,7 @@ void testShorthandLabelReferenceFromContext_WithNodeContext() { @Test void testShorthandIndirectLabelReferenceFromContextField() { assertEquals( - "let block01() -> { label(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)) }\nfor-each(/*/PathNode/CodeField).call(block01())", + "let block01() -> { label(distinct-values(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))) }\nfor-each(/*/PathNode/CodeField).call(block01())", translateTemplate("{BT-00-Code} #value")); } @@ -407,7 +407,7 @@ void testShorthandFieldValueReferenceFromContextField() { @Test void testShorthandFieldValueReferenceFromContextField_WithText() { assertEquals( - "let block01() -> { text('blah ')label(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item))text(' blah ')eval(./normalize-space(text()))text(' blah') }\nfor-each(/*/PathNode/CodeField).call(block01())", + "let block01() -> { text('blah ')label(distinct-values(for $item in ./normalize-space(text()) return concat('code', '|', 'name', '|', 'main-activity', '.', $item)))text(' blah ')eval(./normalize-space(text()))text(' blah') }\nfor-each(/*/PathNode/CodeField).call(block01())", translateTemplate("{BT-00-Code} blah #value blah $value blah")); }