Skip to content

Commit

Permalink
Merge pull request #653 from bci-oss/595-correctly-translate-idta-sub…
Browse files Browse the repository at this point in the history
…model-templates-to-samm

Update URN rules
  • Loading branch information
Yauhenikapl authored Oct 1, 2024
2 parents 74c3d0f + a2b4e94 commit 08b6df0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -172,28 +173,29 @@ public List<String> getSubmodelNames() {
.collect( Collectors.toList() );
}

protected String escapeUrnNamespacePart( final String namespacePart ) {
if ( namespacePart.matches( AspectModelUrn.NAMESPACE_REGEX_PART ) ) {
return namespacePart;
private String iriToReversedHostNameNotation( final IRI iri ) {
final URI uri;
try {
uri = URI.create( iri.toString().contains( "://" ) ? iri.toString() : "https://" + iri );
} catch ( IllegalArgumentException e ) {
throw new IllegalArgumentException( "Incorrect IRI: " + iri, e );
}
throw new AspectModelGenerationException( "Encountered URI with invalid namespace part: " + namespacePart );
}

private <T> Collector<T, ArrayDeque<T>, ArrayDeque<T>> reverseOrder() {
return Collector.of( ArrayDeque::new, ArrayDeque::addFirst, ( deque1, deque2 ) -> {
deque2.addAll( deque1 );
return deque2;
} );
}
if ( uri.getHost() == null ) {
throw new IllegalArgumentException( "URI doesn't contain host: " + uri );
}

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( "/" ) ) )
final String[] hostParts = uri.getHost().split( "\\." );
final List<String> 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 )
.map( this::escapeUrnNamespacePart )
.collect( Collectors.joining( "." ) );

return reversedHost + ( path.isEmpty() ? "" : "." + path );
}

private Optional<IRI> iri( final String lexicalRepresentation ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down

0 comments on commit 08b6df0

Please sign in to comment.