Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
Expand Down Expand Up @@ -33,14 +33,12 @@

import io.vavr.control.Try;
import org.apache.jena.rdf.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.jena.riot.RiotException;

/**
* Resolution strategy for Aspect model URNs that finds Aspect model files in the local file system.
*/
public class FileSystemStrategy implements ResolutionStrategy {
private static final Logger LOG = LoggerFactory.getLogger( FileSystemStrategy.class );
protected final ModelsRoot modelsRoot;

/**
Expand Down Expand Up @@ -79,7 +77,7 @@ public FileSystemStrategy( final ModelsRoot modelsRoot ) {
*
* @param aspectModelUrn The model URN
* @return The model on success, {@link IllegalArgumentException} if the model file can not be read,
* {@link org.apache.jena.riot.RiotException} on parser error, {@link MalformedURLException} if the AspectModelUrn is invalid,
* {@link RiotException} on parser error, {@link MalformedURLException} if the AspectModelUrn is invalid,
* {@link FileNotFoundException} if no file containing the element was found
*/
@Override
Expand All @@ -94,7 +92,10 @@ public AspectModelFile apply( final AspectModelUrn aspectModelUrn, final Resolut
new ModelResolutionException.LoadingFailure( aspectModelUrn, namedResourceFile.getAbsolutePath(),
tryFile.getCause().getMessage(), tryFile.getCause() ) );
}
return tryFile.get();
final RawAspectModelFile loadedFile = tryFile.get();
if ( resolutionStrategySupport.containsDefinition( loadedFile, aspectModelUrn ) ) {
return loadedFile;
}
} else {
checkedLocations.add( new ModelResolutionException.LoadingFailure( aspectModelUrn, namedResourceFile.getAbsolutePath(),
"File does not exist" ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
Expand All @@ -14,14 +14,23 @@
package org.eclipse.esmf.aspectmodel.resolver.fs;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class ModelsRoot {

private static final Logger LOG = LoggerFactory.getLogger( ModelsRoot.class );
private static final File EMPTY_FILE = new File( "" );
private final Path root;

protected ModelsRoot( final Path root ) {
Expand All @@ -42,7 +51,31 @@ public Stream<Path> paths() {

public abstract Path directoryForNamespace( final AspectModelUrn urn );

/**
* Determines the aspect model file for the given {@link AspectModelUrn}.
*
* <p>Constructs the file path by resolving the namespace directory.
* Validates the file using its canonical path.
*
* <p>Returns an empty file if the resolution fails.
*
* @param urn the {@link AspectModelUrn} representing the aspect model.
* @return the resolved {@link File}, or an empty file if resolution fails.
*/
public File determineAspectModelFile( final AspectModelUrn urn ) {
return directoryForNamespace( urn ).resolve( urn.getName() + ".ttl" ).toFile();
Path path = directoryForNamespace( urn ).resolve( urn.getName() + ".ttl" );
return resolveByCanonicalPath( path );
}

private static File resolveByCanonicalPath( final Path path ) {
File file = path.toFile();
try {
if ( Objects.equals( path.normalize().toString(), file.getCanonicalPath() ) ) {
return file;
}
} catch ( IOException exception ) {
LOG.error( "Error resolving canonical path for file: {}", file.getPath(), exception );
}
return EMPTY_FILE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.eclipse.esmf.aspectmodel.resolver.fs;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

class ModelsRootTest {

private static final File EMPTY_FILE = new File( "" );

@Test
void resolveByCanonicalPathShouldReturnFileWhenCanonicalPathMatches() throws Exception {
Path testPath = Paths.get( "src/test/resources/resolve", "Aspect.ttl" ).toAbsolutePath();

File result = invokeResolveByCanonicalPath( testPath );

assertThat( result )
.matches( File::exists )
.isEqualTo( testPath.toFile() );
}

@Test
void resolveByCanonicalPathShouldReturnFileWhenCanonicalPathMatchesForSpecificPath() throws Exception {
Path testPath = Paths.get( "src/test/resources/../resources/resolve", "Aspect.ttl" ).toAbsolutePath();

File result = invokeResolveByCanonicalPath( testPath );

assertThat( result )
.matches( File::exists )
.isEqualTo( testPath.toFile() );
}

@Test
void resolveByCanonicalPathShouldReturnEmptyFileWhenCanonicalPathDoesNotMatch() throws Exception {
Path invalidPath = Paths.get( "src/test/resources/resolve", "aspect.ttl" ).toAbsolutePath();

File result = invokeResolveByCanonicalPath( invalidPath );

assertThat( result ).isEqualTo( EMPTY_FILE );
}

@Test
void resolveByCanonicalPathShouldReturnEmptyFileWhenIoExceptionOccurs() throws Exception {
Path pathCausingIOException = Paths.get( "pathCausingIOException" );

File result = invokeResolveByCanonicalPath( pathCausingIOException );

assertThat( result ).isEqualTo( EMPTY_FILE );
}

private static File invokeResolveByCanonicalPath( Path path ) throws Exception {
Method method = ModelsRoot.class.getDeclaredMethod( "resolveByCanonicalPath", Path.class );
method.setAccessible( true );
return (File) method.invoke( null, path );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:1.0.0#> .

:TestAspect a samm:Aspect ;
samm:name "TestAspect" ;
samm:preferredName "Test Aspect"@en ;
samm:properties ( ) ;
samm:operations ( ) .