Skip to content

Commit

Permalink
Fixed Aspect resolving in a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
ysrbo committed Nov 2, 2023
1 parent 75a4ed0 commit 6f5177a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.XSD;
import org.eclipse.esmf.aspectmodel.VersionNumber;
Expand Down Expand Up @@ -405,7 +406,7 @@ public static Try<VersionedModel> loadAndResolveModel( final File input ) {
private static Try<VersionedModel> loadAndResolveModelFromUrnLikeDir( final File input ) {
final AspectModelResolver resolver = new AspectModelResolver();
final File inputFile = input.getAbsoluteFile();
final Try<AspectModelUrn> urnTry = Try.of( () -> fileToUrn( inputFile ) );
final Try<AspectModelUrn> urnTry = fileToUrn( inputFile );
final Try<FileSystemStrategy> strategyTry = getModelRoot( inputFile ).map( FileSystemStrategy::new );
//noinspection unchecked
return urnTry
Expand Down Expand Up @@ -458,7 +459,7 @@ public static Try<Path> getModelRoot( final File inputFile ) {
* @param inputFile the input model file
* @return the URN of the model element that corresponds to the file name and its location inside the models root
*/
public static AspectModelUrn fileToUrn( final File inputFile ) {
public static Try<AspectModelUrn> fileToUrn( final File inputFile ) {
final File versionDirectory = inputFile.getParentFile();
if ( versionDirectory == null ) {
throw new ModelResolutionException( "Could not determine parent directory of " + inputFile );
Expand All @@ -473,9 +474,27 @@ public static AspectModelUrn fileToUrn( final File inputFile ) {
final String namespace = namespaceDirectory.getName();
final String aspectName = FilenameUtils.removeExtension( inputFile.getName() );
final String urn = String.format( "urn:samm:%s:%s#%s", namespace, version, aspectName );
return AspectModelUrn.from( urn ).getOrElse( () -> {
throw new ModelResolutionException( "The URN constructed from the input file path is invalid: " + urn );
} );
return AspectModelUrn.from( urn )
.mapFailure( Case(
$( instanceOf( UrnSyntaxException.class ) ),
e -> new ModelResolutionException( "The URN constructed from the input file path is invalid: " + urn, e ) )
);
}

/**
* Finds URN matched to file in the loaded model.
*
* @param model the loaded version model
* @param file the input model file
* @return the URN of the model element that corresponds to the file name and its location inside the models root
*/
public static AspectModelUrn urnFromModel(final VersionedModel model, final File file) {
final String aspectName = FilenameUtils.removeExtension( file.getName() );
return Streams.stream( model.getRawModel().listSubjects()).filter( s-> aspectName.equals( s.getLocalName() ) )
.findFirst()
.map( Resource::getURI )
.map( AspectModelUrn::fromUrn )
.orElseThrow();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void testLoadLegacyBammModelFromFileExpectSuccess( final KnownVersion met
.getPath() );

final AspectModelUrn testUrn = AspectModelResolver.fileToUrn(
Paths.get( aspectModelsRootDirectory.toString(), "org.eclipse.esmf.test", "2.0.0", "BammTest.ttl" ).toFile() );
Paths.get( aspectModelsRootDirectory.toString(), "org.eclipse.esmf.test", "2.0.0", "BammTest.ttl" ).toFile() ).get();

final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
final Try<VersionedModel> result = resolver.resolveAspectModel( urnStrategy, testUrn );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package org.eclipse.esmf;

import static org.eclipse.esmf.aspectmodel.resolver.AspectModelResolver.fileToUrn;
import static org.eclipse.esmf.aspectmodel.resolver.AspectModelResolver.*;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -57,7 +57,7 @@ protected Try<VersionedModel> loadAndResolveModel( final File input, final Exter
versionedModel = AspectModelResolver.loadAndResolveModel( newInput );
}
} else {
final AspectModelUrn urn = fileToUrn( input.getAbsoluteFile() );
final AspectModelUrn urn = fileToUrn( input.getAbsoluteFile() ).get();
versionedModel = new AspectModelResolver().resolveAspectModel( new ExternalResolverStrategy( resolverConfig.commandLine ), urn );
}
return versionedModel;
Expand All @@ -67,7 +67,7 @@ protected AspectContext loadModelOrFail( final String modelFileName, final Exter
final File inputFile = new File( modelFileName );
final Try<VersionedModel> versionedModel = loadAndResolveModel( inputFile, resolverConfig );
final Try<AspectContext> context = versionedModel.flatMap( model -> {
final AspectModelUrn urn = fileToUrn( inputFile.getAbsoluteFile() );
final AspectModelUrn urn = fileToUrn( inputFile.getAbsoluteFile() ).getOrElse( () -> urnFromModel(model, inputFile) );
return AspectModelLoader.getSingleAspect( model, aspect -> aspect.getName().equals( urn.getName() ) )
.map( aspect -> new AspectContext( model, aspect ) );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class AspectMigrateCommand extends AbstractCommand {
public void run() {
final PrintWriter printWriter = new PrintWriter( getStreamForFile( outputFilePath ) );
final File inputFile = new File( parentCommand.getInput() ).getAbsoluteFile();
final AspectModelUrn aspectModelUrn = fileToUrn( inputFile );
final AspectModelUrn aspectModelUrn = fileToUrn( inputFile ).get();

final MigratorService migratorService = new MigratorService();
loadButNotResolveModel( inputFile ).flatMap( migratorService::updateMetaModelVersion )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void run() {
final PrintWriter printWriter = new PrintWriter( getStreamForFile( outputFilePath ) );
final File file = new File( parentCommand.getInput() );
final File inputFile = file.getAbsoluteFile();
final AspectModelUrn aspectModelUrn = fileToUrn( inputFile );
final AspectModelUrn aspectModelUrn = fileToUrn( inputFile ).get();
loadButNotResolveModel( inputFile ).forEach( versionedModel -> {
new PrettyPrinter( versionedModel, aspectModelUrn, printWriter ).print();
printWriter.flush();
Expand Down

0 comments on commit 6f5177a

Please sign in to comment.