13
13
14
14
package org .eclipse .esmf .aspectmodel .resolver ;
15
15
16
- import static io .vavr .API .*;
16
+ import static io .vavr .API .$ ;
17
+ import static io .vavr .API .Case ;
17
18
import static io .vavr .Predicates .instanceOf ;
18
19
19
20
import java .io .ByteArrayInputStream ;
36
37
import java .util .stream .Collectors ;
37
38
import java .util .stream .Stream ;
38
39
39
- import org .apache .commons .io .FilenameUtils ;
40
- import org .apache .jena .rdf .model .Model ;
41
- import org .apache .jena .rdf .model .ModelFactory ;
42
- import org .apache .jena .rdf .model .Property ;
43
- import org .apache .jena .rdf .model .RDFNode ;
44
- import org .apache .jena .rdf .model .Resource ;
45
- import org .apache .jena .vocabulary .RDF ;
46
- import org .apache .jena .vocabulary .XSD ;
47
40
import org .eclipse .esmf .aspectmodel .VersionNumber ;
41
+ import org .eclipse .esmf .aspectmodel .resolver .fs .FlatModelsRoot ;
42
+ import org .eclipse .esmf .aspectmodel .resolver .fs .StructuredModelsRoot ;
48
43
import org .eclipse .esmf .aspectmodel .resolver .services .SammAspectMetaModelResourceResolver ;
49
44
import org .eclipse .esmf .aspectmodel .resolver .services .TurtleLoader ;
50
45
import org .eclipse .esmf .aspectmodel .resolver .services .VersionedModel ;
58
53
import org .eclipse .esmf .samm .KnownVersion ;
59
54
60
55
import com .google .common .collect .Streams ;
61
-
62
56
import io .vavr .CheckedFunction1 ;
63
57
import io .vavr .Value ;
64
58
import io .vavr .control .Option ;
65
59
import io .vavr .control .Try ;
60
+ import org .apache .commons .io .FilenameUtils ;
61
+ import org .apache .jena .rdf .model .Model ;
62
+ import org .apache .jena .rdf .model .ModelFactory ;
63
+ import org .apache .jena .rdf .model .Property ;
64
+ import org .apache .jena .rdf .model .RDFNode ;
65
+ import org .apache .jena .rdf .model .Resource ;
66
+ import org .apache .jena .vocabulary .RDF ;
67
+ import org .apache .jena .vocabulary .XSD ;
68
+ import org .slf4j .Logger ;
69
+ import org .slf4j .LoggerFactory ;
66
70
67
71
/**
68
72
* Provides facilities for loading an Aspect model and resolving referenced meta model elements and
69
73
* model elements from other Aspect models
70
74
*/
71
75
public class AspectModelResolver {
76
+ private static final Logger LOG = LoggerFactory .getLogger ( AspectModelResolver .class );
72
77
73
78
private final MigratorService migratorService = MigratorServiceLoader .getInstance ().getMigratorService ();
74
79
private final BammUriRewriter bamm100UriRewriter = new BammUriRewriter ( BammUriRewriter .BAMM_VERSION .BAMM_1_0_0 );
@@ -130,9 +135,7 @@ public Try<VersionedModel> resolveAspectModel( final ResolutionStrategy resoluti
130
135
* @return the resolved model on success
131
136
*/
132
137
public Try <VersionedModel > resolveAspectModel ( final ResolutionStrategy resolutionStrategy , final InputStream inputStream ) {
133
- return TurtleLoader .loadTurtle ( inputStream )
134
- .flatMap ( model -> resolveAspectModel ( FileSystemStrategy .DefaultNamespace .withDefaultNamespace ( resolutionStrategy , model ),
135
- model ) );
138
+ return TurtleLoader .loadTurtle ( inputStream ).flatMap ( model -> resolveAspectModel ( resolutionStrategy , model ) );
136
139
}
137
140
138
141
/**
@@ -185,8 +188,16 @@ public Try<VersionedModel> resolveAspectModel( final Model initialModel, final R
185
188
.map ( bamm200UriRewriter ::migrate );
186
189
187
190
if ( mergedModel .isFailure () ) {
188
- if ( mergedModel .getCause () instanceof FileNotFoundException ) {
189
- return Try .failure ( new ModelResolutionException ( "Could not resolve " + input , mergedModel .getCause () ) );
191
+ if ( mergedModel .getCause () instanceof final FileNotFoundException fileNotFoundException ) {
192
+ final String failedUrns = input .stream ()
193
+ .filter ( urn -> !urn .getElementType ().equals ( ElementType .META_MODEL ) )
194
+ .filter ( urn -> !urn .getElementType ().equals ( ElementType .CHARACTERISTIC ) )
195
+ .filter ( urn -> !urn .getElementType ().equals ( ElementType .ENTITY ) )
196
+ .filter ( urn -> !urn .getElementType ().equals ( ElementType .UNIT ) )
197
+ .map ( AspectModelUrn ::toString )
198
+ .collect ( Collectors .joining ( ", " ) );
199
+ LOG .debug ( "Could not resolve {}" , failedUrns , fileNotFoundException );
200
+ return Try .failure ( new ModelResolutionException ( "Could not resolve " + failedUrns , fileNotFoundException ) );
190
201
}
191
202
return Try .failure ( mergedModel .getCause () );
192
203
}
@@ -236,9 +247,14 @@ public Try<VersionedModel> resolveAspectModel( final Model initialModel, final R
236
247
*/
237
248
public static boolean containsDefinition ( final Model model , final AspectModelUrn urn ) {
238
249
if ( model .getNsPrefixMap ().values ().stream ().anyMatch ( prefixUri -> prefixUri .startsWith ( "urn:bamm:" ) ) ) {
239
- return model .contains ( model .createResource ( urn .toString ().replace ( "urn:samm:" , "urn:bamm:" ) ), RDF .type , (RDFNode ) null );
250
+ final boolean result = model .contains ( model .createResource ( urn .toString ().replace ( "urn:samm:" , "urn:bamm:" ) ), RDF .type ,
251
+ (RDFNode ) null );
252
+ LOG .debug ( "Checking if model contains {}: {}" , urn , result );
253
+ return result ;
240
254
}
241
- return model .contains ( model .createResource ( urn .toString () ), RDF .type , (RDFNode ) null );
255
+ final boolean result = model .contains ( model .createResource ( urn .toString () ), RDF .type , (RDFNode ) null );
256
+ LOG .debug ( "Checking if model contains {}: {}" , urn , result );
257
+ return result ;
242
258
}
243
259
244
260
/**
@@ -262,6 +278,7 @@ private Try<Model> resolve( final Model result, final List<AspectModelUrn> urns,
262
278
final String urnToResolve = unresolvedUrns .pop ();
263
279
final Try <Model > resolvedModel = getModelForUrn ( urnToResolve , resolutionStrategy );
264
280
if ( resolvedModel .isFailure () ) {
281
+ LOG .debug ( "Tried to resolve {} using {}, but it failed" , urnToResolve , resolutionStrategy );
265
282
return resolvedModel ;
266
283
}
267
284
final Model model = resolvedModel .get ();
@@ -399,40 +416,18 @@ private void mergeModels( final Model target, final Model other ) {
399
416
* @return the resolved model on success
400
417
*/
401
418
public static Try <VersionedModel > loadAndResolveModel ( final File input ) {
402
- return loadAndResolveModelFromUrnLikeDir ( input )
403
- .orElse ( () -> loadAndResolveModelFromDir ( input ) );
404
- }
405
-
406
- private static Try <VersionedModel > loadAndResolveModelFromUrnLikeDir ( final File input ) {
407
- final AspectModelResolver resolver = new AspectModelResolver ();
408
- final File inputFile = input .getAbsoluteFile ();
409
- final Try <AspectModelUrn > urnTry = fileToUrn ( inputFile );
410
- final Try <FileSystemStrategy > strategyTry = getModelRoot ( inputFile ).map ( FileSystemStrategy ::new );
411
- //noinspection unchecked
412
- return urnTry
413
- .flatMap ( urn -> strategyTry
414
- .flatMap ( strategy -> resolver .resolveAspectModel ( strategy , urn ) )
415
- .mapFailure (
416
- Case ( $ ( instanceOf ( IOException .class ) ),
417
- e -> new ModelResolutionException ( "Could not load model " + urn + " from file " + input , e ) )
418
- )
419
- );
420
- }
421
-
422
- private static Try <VersionedModel > loadAndResolveModelFromDir ( final File input ) {
423
419
final AspectModelResolver resolver = new AspectModelResolver ();
424
420
final File inputFile = input .getAbsoluteFile ();
425
- final Try <Path > modelsRoot = Try .of ( () -> inputFile .getParentFile ().toPath () );
426
- final Try <FileSystemStrategy > strategyTry = modelsRoot .map ( FileSystemStrategy .DefaultNamespace ::new );
427
-
428
- //noinspection unchecked
429
- return strategyTry
430
- .flatMapTry ( strategy -> Try
431
- .withResources ( () -> new FileInputStream ( input ) )
432
- .of ( stream -> resolver .resolveAspectModel ( strategy , stream ) ) )
433
- .flatMap ( Function .identity () )
434
- .mapFailure ( Case ( $ ( instanceOf ( IOException .class ) ),
435
- e -> new ModelResolutionException ( "Could not open file " + input , e ) ) );
421
+ final ResolutionStrategy fromSameDirectory = new FileSystemStrategy ( new FlatModelsRoot ( inputFile .getParentFile ().toPath () ) );
422
+
423
+ // Construct the resolution strategy: Models should be searched in the structured folder (if it exists) and then in the
424
+ // same directory. If the structured folder can not be resolved, directly search in the same directory.
425
+ final ResolutionStrategy resolutionStrategy = getModelRoot ( inputFile ).map (
426
+ modelsRoot -> new FileSystemStrategy ( new StructuredModelsRoot ( modelsRoot ) ) )
427
+ .<ResolutionStrategy > map ( structured -> new EitherStrategy ( structured , fromSameDirectory ) ).getOrElse ( fromSameDirectory );
428
+ return Try .withResources ( () -> new FileInputStream ( input ) )
429
+ .of ( stream -> resolver .resolveAspectModel ( resolutionStrategy , stream ) )
430
+ .flatMap ( Function .identity () );
436
431
}
437
432
438
433
/**
@@ -488,9 +483,9 @@ public static Try<AspectModelUrn> fileToUrn( final File inputFile ) {
488
483
* @param file the input model file
489
484
* @return the URN of the model element that corresponds to the file name and its location inside the models root
490
485
*/
491
- public static AspectModelUrn urnFromModel (final VersionedModel model , final File file ) {
486
+ public static AspectModelUrn urnFromModel ( final VersionedModel model , final File file ) {
492
487
final String aspectName = FilenameUtils .removeExtension ( file .getName () );
493
- return Streams .stream ( model .getRawModel ().listSubjects ()).filter ( s -> aspectName .equals ( s .getLocalName () ) )
488
+ return Streams .stream ( model .getRawModel ().listSubjects () ).filter ( s -> aspectName .equals ( s .getLocalName () ) )
494
489
.findFirst ()
495
490
.map ( Resource ::getURI )
496
491
.map ( AspectModelUrn ::fromUrn )
0 commit comments