From da23fe5264d4c9bf8470427cdbbcae063ee097e5 Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Thu, 29 Aug 2024 17:58:05 +0300 Subject: [PATCH 1/8] Update URN rules --- .../aas/AasToAspectModelGenerator.java | 34 ++++++++++++++++--- .../aspectmodel/urn/AspectModelUrnTest.java | 13 +++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index 6c59e7798..1e271c16d 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -172,7 +172,11 @@ public List getSubmodelNames() { .collect( Collectors.toList() ); } - protected String escapeUrnNamespacePart( final String namespacePart ) { + protected String escapeUrnNamespacePart( String namespacePart ) { + if ( namespacePart.contains( "-" ) ) { + namespacePart = namespacePart.replaceAll( "-", "." ); + return namespacePart; + } if ( namespacePart.matches( AspectModelUrn.NAMESPACE_REGEX_PART ) ) { return namespacePart; } @@ -186,14 +190,34 @@ private Collector, ArrayDeque> reverseOrder() { } ); } +// private String iriToReversedHostNameNotation( final IRI iri ) { +// final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); +// return Stream.concat( +// Arrays.stream( uri.getHost().split( "\\." ) ).collect( reverseOrder() ).stream(), +// Arrays.stream( uri.getPath().split( "/" ) ) ) +// .filter( StringUtils::isNotBlank ) +// .map( this::escapeUrnNamespacePart ) +// .collect( Collectors.joining( "." ) ); +// } + private String iriToReversedHostNameNotation( final IRI iri ) { final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); - return Stream.concat( - Arrays.stream( uri.getHost().split( "\\." ) ).collect( reverseOrder() ).stream(), - Arrays.stream( uri.getPath().split( "/" ) ) ) + + String[] hostParts = uri.getHost().split( "\\." ); + String[] pathParts = uri.getPath().split( "/" ); + + String reversedHost = Arrays.stream( hostParts ) + .collect( reverseOrder() ) + .stream() + .map( part -> part.replaceAll( "-", "." ) ) + .collect( Collectors.joining( "." ) ); + + String path = Arrays.stream( pathParts ) .filter( StringUtils::isNotBlank ) - .map( this::escapeUrnNamespacePart ) + .limit( pathParts.length - 2 ) .collect( Collectors.joining( "." ) ); + + return reversedHost + "." + path; } private Optional iri( final String lexicalRepresentation ) { diff --git a/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java b/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java index 2226dd9e1..8efa739ef 100644 --- a/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java +++ b/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java @@ -342,4 +342,17 @@ void validNamespaceTest() throws URISyntaxException { assertThat( elementUrnWithDash.getNamespaceMainPart() ).isNotEmpty(); assertThat( elementUrnWithDash.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf-test" ); } + + @Test + void invalidNamespaceTest() throws URISyntaxException { + final URI validNamespaceUrnUnderscore = new URI( "urn:samm:com.bosch.nexeed.digitaltwin:aspect-model:Er?ors:1.1.0#TestAspect" ); + final AspectModelUrn elementUrnWithUnderscore = AspectModelUrn.fromUrn( validNamespaceUrnUnderscore ); + assertThat( elementUrnWithUnderscore.getNamespaceMainPart() ).isNotEmpty(); + assertThat( elementUrnWithUnderscore.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf_test" ); + + final URI invalidNamespaceUrnDash = new URI( "urn:samm:org.eclipse.esmf-test:0.0.1#TestAspect" ); + final AspectModelUrn elementUrnWithDash = AspectModelUrn.fromUrn( invalidNamespaceUrnDash ); + assertThat( elementUrnWithDash.getNamespaceMainPart() ).isNotEmpty(); + assertThat( elementUrnWithDash.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf-test" ); + } } From b213479816a1a65e07e756c872f0b19207c999e2 Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 10 Sep 2024 18:55:02 +0300 Subject: [PATCH 2/8] Update getting valid urn in AAS Generator --- .../aas/AasToAspectModelGenerator.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index 1e271c16d..faa572530 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -190,27 +190,14 @@ private Collector, ArrayDeque> reverseOrder() { } ); } -// private String iriToReversedHostNameNotation( final IRI iri ) { -// final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); -// return Stream.concat( -// Arrays.stream( uri.getHost().split( "\\." ) ).collect( reverseOrder() ).stream(), -// Arrays.stream( uri.getPath().split( "/" ) ) ) -// .filter( StringUtils::isNotBlank ) -// .map( this::escapeUrnNamespacePart ) -// .collect( Collectors.joining( "." ) ); -// } - private String iriToReversedHostNameNotation( final IRI iri ) { final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); String[] hostParts = uri.getHost().split( "\\." ); String[] pathParts = uri.getPath().split( "/" ); - String reversedHost = Arrays.stream( hostParts ) - .collect( reverseOrder() ) - .stream() - .map( part -> part.replaceAll( "-", "." ) ) - .collect( Collectors.joining( "." ) ); + String reversedHost = String.join( ".", Arrays.stream( hostParts ) + .collect( reverseOrder() ) ); String path = Arrays.stream( pathParts ) .filter( StringUtils::isNotBlank ) From 3134131215d8a78048221c17aecbc3c00eaf3b38 Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 10 Sep 2024 19:02:50 +0300 Subject: [PATCH 3/8] Refactoring --- .../aas/AasToAspectModelGenerator.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index faa572530..1a76acb79 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -172,17 +172,6 @@ public List getSubmodelNames() { .collect( Collectors.toList() ); } - protected String escapeUrnNamespacePart( String namespacePart ) { - if ( namespacePart.contains( "-" ) ) { - namespacePart = namespacePart.replaceAll( "-", "." ); - return namespacePart; - } - if ( namespacePart.matches( AspectModelUrn.NAMESPACE_REGEX_PART ) ) { - return namespacePart; - } - throw new AspectModelGenerationException( "Encountered URI with invalid namespace part: " + namespacePart ); - } - private Collector, ArrayDeque> reverseOrder() { return Collector.of( ArrayDeque::new, ArrayDeque::addFirst, ( deque1, deque2 ) -> { deque2.addAll( deque1 ); @@ -193,13 +182,13 @@ private Collector, ArrayDeque> reverseOrder() { private String iriToReversedHostNameNotation( final IRI iri ) { final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); - String[] hostParts = uri.getHost().split( "\\." ); - String[] pathParts = uri.getPath().split( "/" ); + final String[] hostParts = uri.getHost().split( "\\." ); + final String[] pathParts = uri.getPath().split( "/" ); - String reversedHost = String.join( ".", Arrays.stream( hostParts ) + final String reversedHost = String.join( ".", Arrays.stream( hostParts ) .collect( reverseOrder() ) ); - String path = Arrays.stream( pathParts ) + final String path = Arrays.stream( pathParts ) .filter( StringUtils::isNotBlank ) .limit( pathParts.length - 2 ) .collect( Collectors.joining( "." ) ); From c197f1673748c12202c9ef85ab344e6d2dca951f Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 10 Sep 2024 19:22:00 +0300 Subject: [PATCH 4/8] Fix tests --- .../aas/AasToAspectModelGenerator.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index 1a76acb79..c688f90e4 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -19,6 +19,7 @@ import java.net.URI; import java.util.ArrayDeque; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -180,20 +181,29 @@ private Collector, ArrayDeque> reverseOrder() { } private String iriToReversedHostNameNotation( final IRI iri ) { - final URI uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); + URI uri; + try { + uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); + } catch ( IllegalArgumentException e ) { + throw new IllegalArgumentException( "Incorrect IRI: " + iri, e ); + } - final String[] hostParts = uri.getHost().split( "\\." ); - final String[] pathParts = uri.getPath().split( "/" ); + if ( uri.getHost() == null ) { + throw new IllegalArgumentException( "URI doesn't contain host: " + uri ); + } - final String reversedHost = String.join( ".", Arrays.stream( hostParts ) - .collect( reverseOrder() ) ); + final String[] hostParts = uri.getHost().split( "\\." ); + final List hostPartsList = Arrays.asList( hostParts ); + Collections.reverse( hostPartsList ); + final String reversedHost = String.join( ".", hostPartsList ); + final String[] pathParts = uri.getPath().split( "/" ); final String path = Arrays.stream( pathParts ) .filter( StringUtils::isNotBlank ) - .limit( pathParts.length - 2 ) + .limit( Math.max( 0, pathParts.length - 2 ) ) .collect( Collectors.joining( "." ) ); - return reversedHost + "." + path; + return reversedHost + ( path.isEmpty() ? "" : "." + path ); } private Optional iri( final String lexicalRepresentation ) { From 137dc61a1ab13720dcafaf619f6eec8eb952822d Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Thu, 29 Aug 2024 17:58:05 +0300 Subject: [PATCH 5/8] Update URN rules --- .../aspectmodel/aas/AasToAspectModelGenerator.java | 3 +-- .../esmf/aspectmodel/urn/AspectModelUrnTest.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index c688f90e4..c986ef778 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -200,10 +200,9 @@ private String iriToReversedHostNameNotation( final IRI iri ) { final String[] pathParts = uri.getPath().split( "/" ); final String path = Arrays.stream( pathParts ) .filter( StringUtils::isNotBlank ) - .limit( Math.max( 0, pathParts.length - 2 ) ) .collect( Collectors.joining( "." ) ); - return reversedHost + ( path.isEmpty() ? "" : "." + path ); + return reversedHost + "." + path; } private Optional iri( final String lexicalRepresentation ) { diff --git a/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java b/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java index b2d97b2c8..61c9bf9e0 100644 --- a/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java +++ b/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java @@ -343,6 +343,19 @@ void validNamespaceTest() throws URISyntaxException { assertThat( elementUrnWithDash.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf-test" ); } + @Test + void invalidNamespaceTest() throws URISyntaxException { + final URI validNamespaceUrnUnderscore = new URI( "urn:samm:com.bosch.nexeed.digitaltwin:aspect-model:Er?ors:1.1.0#TestAspect" ); + final AspectModelUrn elementUrnWithUnderscore = AspectModelUrn.fromUrn( validNamespaceUrnUnderscore ); + assertThat( elementUrnWithUnderscore.getNamespaceMainPart() ).isNotEmpty(); + assertThat( elementUrnWithUnderscore.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf_test" ); + + final URI invalidNamespaceUrnDash = new URI( "urn:samm:org.eclipse.esmf-test:0.0.1#TestAspect" ); + final AspectModelUrn elementUrnWithDash = AspectModelUrn.fromUrn( invalidNamespaceUrnDash ); + assertThat( elementUrnWithDash.getNamespaceMainPart() ).isNotEmpty(); + assertThat( elementUrnWithDash.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf-test" ); + } + @Test void invalidModelElementNameTest() throws URISyntaxException { final URI invalidRootModelElementName = new URI( sammBaseUri + "aspect-model:Er?ors:1.1.0#TestAspect" ); From ec56f76b7ec572dd4fcc4c180eda16f009dd63ce Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 24 Sep 2024 13:41:30 +0300 Subject: [PATCH 6/8] Update PR --- .../esmf/aspectmodel/aas/AasToAspectModelGenerator.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index c986ef778..7dca497b1 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -173,15 +173,8 @@ public List getSubmodelNames() { .collect( Collectors.toList() ); } - private Collector, ArrayDeque> reverseOrder() { - return Collector.of( ArrayDeque::new, ArrayDeque::addFirst, ( deque1, deque2 ) -> { - deque2.addAll( deque1 ); - return deque2; - } ); - } - private String iriToReversedHostNameNotation( final IRI iri ) { - URI uri; + final URI uri; try { uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri ); } catch ( IllegalArgumentException e ) { From 494adda24d3c71b326290c5eb7d77b3250666df3 Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 24 Sep 2024 13:45:09 +0300 Subject: [PATCH 7/8] Rollback changes --- .../esmf/aspectmodel/urn/AspectModelUrnTest.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java b/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java index 61c9bf9e0..b2d97b2c8 100644 --- a/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java +++ b/core/esmf-aspect-model-urn/src/test/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrnTest.java @@ -343,19 +343,6 @@ void validNamespaceTest() throws URISyntaxException { assertThat( elementUrnWithDash.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf-test" ); } - @Test - void invalidNamespaceTest() throws URISyntaxException { - final URI validNamespaceUrnUnderscore = new URI( "urn:samm:com.bosch.nexeed.digitaltwin:aspect-model:Er?ors:1.1.0#TestAspect" ); - final AspectModelUrn elementUrnWithUnderscore = AspectModelUrn.fromUrn( validNamespaceUrnUnderscore ); - assertThat( elementUrnWithUnderscore.getNamespaceMainPart() ).isNotEmpty(); - assertThat( elementUrnWithUnderscore.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf_test" ); - - final URI invalidNamespaceUrnDash = new URI( "urn:samm:org.eclipse.esmf-test:0.0.1#TestAspect" ); - final AspectModelUrn elementUrnWithDash = AspectModelUrn.fromUrn( invalidNamespaceUrnDash ); - assertThat( elementUrnWithDash.getNamespaceMainPart() ).isNotEmpty(); - assertThat( elementUrnWithDash.getNamespaceMainPart() ).isEqualTo( "org.eclipse.esmf-test" ); - } - @Test void invalidModelElementNameTest() throws URISyntaxException { final URI invalidRootModelElementName = new URI( sammBaseUri + "aspect-model:Er?ors:1.1.0#TestAspect" ); From b47a97a46af9aa4cf891d9bbc4f97e976130dd5b Mon Sep 17 00:00:00 2001 From: Yauhenikapl Date: Tue, 24 Sep 2024 14:05:46 +0300 Subject: [PATCH 8/8] Update and refactoring --- .../esmf/aspectmodel/aas/AasToAspectModelGenerator.java | 2 +- .../esmf/aspectmodel/aas/AasToAspectModelGeneratorTest.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java index 7dca497b1..532dbf9d9 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGenerator.java @@ -195,7 +195,7 @@ private String iriToReversedHostNameNotation( final IRI iri ) { .filter( StringUtils::isNotBlank ) .collect( Collectors.joining( "." ) ); - return reversedHost + "." + path; + return reversedHost + ( path.isEmpty() ? "" : "." + path ); } private Optional iri( final String lexicalRepresentation ) { diff --git a/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGeneratorTest.java b/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGeneratorTest.java index 303a086a2..7c8fbdc06 100644 --- a/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGeneratorTest.java +++ b/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AasToAspectModelGeneratorTest.java @@ -36,11 +36,10 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -public class AasToAspectModelGeneratorTest { +class AasToAspectModelGeneratorTest { - // IDTA-provided sample files can currently not be read with AAS4J @Test - @Disabled + @Disabled( "IDTA-provided sample files can currently not be read with AAS4J" ) void testTranslateDigitalNameplate() { final InputStream aasx = AasToAspectModelGeneratorTest.class.getClassLoader() .getResourceAsStream( "Sample_ZVEI_Digital_Nameplate_V10.aasx" );