|
24 | 24 | import java.util.List; |
25 | 25 | import java.util.regex.Pattern; |
26 | 26 |
|
| 27 | +import org.apache.jena.rdf.model.Model; |
27 | 28 | import org.apache.jena.riot.RiotException; |
28 | 29 |
|
| 30 | +import io.openmanufacturing.ame.config.ApplicationSettings; |
29 | 31 | import io.openmanufacturing.ame.exceptions.InvalidAspectModelException; |
30 | 32 | import io.openmanufacturing.ame.exceptions.UrnNotFoundException; |
31 | 33 | import io.openmanufacturing.ame.resolver.inmemory.InMemoryStrategy; |
|
40 | 42 | import io.openmanufacturing.sds.aspectmodel.validation.report.ValidationReportBuilder; |
41 | 43 | import io.openmanufacturing.sds.aspectmodel.validation.services.AspectModelValidator; |
42 | 44 | import io.openmanufacturing.sds.aspectmodel.versionupdate.MigratorService; |
| 45 | +import io.openmanufacturing.sds.metamodel.Aspect; |
| 46 | +import io.openmanufacturing.sds.metamodel.loader.AspectModelLoader; |
43 | 47 | import io.vavr.control.Try; |
| 48 | +import lombok.NoArgsConstructor; |
44 | 49 |
|
| 50 | +@NoArgsConstructor |
45 | 51 | public class ModelUtils { |
46 | 52 |
|
47 | 53 | public static final String TTL = "ttl"; |
48 | 54 | public static final String TTL_EXTENSION = "." + TTL; |
49 | 55 |
|
50 | 56 | public final static Pattern URN_PATTERN = Pattern.compile( |
51 | | - "^urn:[a-z0-9][a-z0-9-]{0,31}:([a-z0-9()+,\\-.:=@;$_!*#']|%[0-9a-f]{2})+$", |
52 | | - Pattern.CASE_INSENSITIVE ); |
53 | | - |
54 | | - private ModelUtils() { |
55 | | - } |
| 57 | + "^urn:[a-z0-9][a-z0-9-]{0,31}:([a-z0-9()+,\\-.:=@;$_!*#']|%[0-9a-f]{2})+$", Pattern.CASE_INSENSITIVE ); |
56 | 58 |
|
57 | 59 | /** |
58 | 60 | * This Method is used to create an in memory strategy for the given Aspect Model. |
@@ -92,34 +94,70 @@ public static Try<VersionedModel> fetchVersionModel( final String aspectModel, f |
92 | 94 | return new AspectModelResolver().resolveAspectModel( inMemoryStrategy, inMemoryStrategy.getAspectModelUrn() ); |
93 | 95 | } |
94 | 96 |
|
95 | | - public static Try<VersionedModel> loadButNotResolveModel( final File inputFile ) { |
96 | | - try ( final InputStream inputStream = new FileInputStream( inputFile ) ) { |
97 | | - final SdsAspectMetaModelResourceResolver metaModelResourceResolver = new SdsAspectMetaModelResourceResolver(); |
98 | | - return TurtleLoader.loadTurtle( inputStream ).flatMap( model -> |
99 | | - metaModelResourceResolver.getBammVersion( model ).flatMap( metaModelVersion -> |
100 | | - metaModelResourceResolver.mergeMetaModelIntoRawModel( model, metaModelVersion ) ) ); |
| 97 | + /** |
| 98 | + * Migrates a model to its latest version. |
| 99 | + * |
| 100 | + * @param aspectModel as a string. |
| 101 | + * @param storagePath stored path to the Aspect Models. |
| 102 | + * @return migrated Aspect Model as a string. |
| 103 | + */ |
| 104 | + public static String migrateModel( final String aspectModel, final String storagePath ) { |
| 105 | + final InMemoryStrategy inMemoryStrategy = ModelUtils.inMemoryStrategy( aspectModel, storagePath ); |
| 106 | + |
| 107 | + final Try<VersionedModel> migratedFile = LoadModelFromStoragePath( inMemoryStrategy ).flatMap( |
| 108 | + new MigratorService()::updateMetaModelVersion ); |
| 109 | + |
| 110 | + final VersionedModel versionedModel = migratedFile.getOrElseThrow( |
| 111 | + e -> new InvalidAspectModelException( "Aspect Model cannot be migrated.", e ) ); |
| 112 | + |
| 113 | + return getPrettyPrintedVersionedModel( versionedModel, inMemoryStrategy.getAspectModelUrn().getUrn() ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates an Aspect instance from an Aspect Model. |
| 118 | + * |
| 119 | + * @param aspectModel as a string. |
| 120 | + * @return the Aspect as an object. |
| 121 | + */ |
| 122 | + public static Aspect resolveAspectFromModel( final String aspectModel ) { |
| 123 | + final InMemoryStrategy inMemoryStrategy = ModelUtils.inMemoryStrategy( aspectModel, |
| 124 | + ApplicationSettings.getMetaModelStoragePath() ); |
| 125 | + |
| 126 | + final VersionedModel versionedModel = ModelUtils.LoadModelFromStoragePath( inMemoryStrategy ).getOrElseThrow( |
| 127 | + e -> new InvalidAspectModelException( "Cannot resolve Aspect Model.", e ) ); |
| 128 | + |
| 129 | + return AspectModelLoader.fromVersionedModelUnchecked( versionedModel ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Load Aspect Model from storage path. |
| 134 | + * |
| 135 | + * @param inMemoryStrategy for the given storage path. |
| 136 | + * @return the resulting {@link VersionedModel} that corresponds to the input Aspect model. |
| 137 | + */ |
| 138 | + public static Try<VersionedModel> LoadModelFromStoragePath( final InMemoryStrategy inMemoryStrategy ) { |
| 139 | + return resolveModel( inMemoryStrategy.model ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Loading the Aspect Model from input file. |
| 144 | + * |
| 145 | + * @param file Aspect Model as a file. |
| 146 | + * @return the resulting {@link VersionedModel} that corresponds to the input Aspect model. |
| 147 | + */ |
| 148 | + public static Try<VersionedModel> loadModelFromFile( final File file ) { |
| 149 | + try ( final InputStream inputStream = new FileInputStream( file ) ) { |
| 150 | + return TurtleLoader.loadTurtle( inputStream ).flatMap( ModelUtils::resolveModel ); |
101 | 151 | } catch ( final IOException exception ) { |
102 | 152 | return Try.failure( exception ); |
103 | 153 | } |
104 | 154 | } |
105 | 155 |
|
106 | | - public static String migrateModel( final String aspectModel, final String storagePath ) { |
107 | | - final InMemoryStrategy inMemoryStrategy = inMemoryStrategy( aspectModel, storagePath ); |
108 | | - |
| 156 | + private static Try<VersionedModel> resolveModel( final Model model ) { |
109 | 157 | final SdsAspectMetaModelResourceResolver metaModelResourceResolver = new SdsAspectMetaModelResourceResolver(); |
110 | 158 |
|
111 | | - final Try<VersionedModel> migratedFile = metaModelResourceResolver.getBammVersion( inMemoryStrategy.model ) |
112 | | - .flatMap( metaModelVersion -> |
113 | | - metaModelResourceResolver.mergeMetaModelIntoRawModel( |
114 | | - inMemoryStrategy.model, |
115 | | - metaModelVersion ) ) |
116 | | - .flatMap( |
117 | | - new MigratorService()::updateMetaModelVersion ); |
118 | | - |
119 | | - final VersionedModel versionedModel = migratedFile.getOrElseThrow( |
120 | | - e -> new InvalidAspectModelException( "AspectModel cannot be migrated.", e ) ); |
121 | | - |
122 | | - return getPrettyPrintedVersionedModel( versionedModel, inMemoryStrategy.getAspectModelUrn().getUrn() ); |
| 159 | + return metaModelResourceResolver.getBammVersion( model ).flatMap( |
| 160 | + metaModelVersion -> metaModelResourceResolver.mergeMetaModelIntoRawModel( model, metaModelVersion ) ); |
123 | 161 | } |
124 | 162 |
|
125 | 163 | /** |
@@ -163,16 +201,14 @@ public static ValidationReport validateModel( final String aspectModel, final St |
163 | 201 | } |
164 | 202 |
|
165 | 203 | private static ValidationReport buildValidationSyntacticReport( final RiotException riotException ) { |
166 | | - return new ValidationReportBuilder() |
167 | | - .withValidationErrors( List.of( new ValidationError.Syntactic( riotException ) ) ) |
168 | | - .buildInvalidReport(); |
| 204 | + return new ValidationReportBuilder().withValidationErrors( |
| 205 | + List.of( new ValidationError.Syntactic( riotException ) ) ).buildInvalidReport(); |
169 | 206 | } |
170 | 207 |
|
171 | 208 | private static ValidationReport buildValidationSemanticReport( final String message, final String focusNode, |
172 | 209 | final String resultPath, final String Severity, final String value ) { |
173 | | - return new ValidationReportBuilder() |
174 | | - .withValidationErrors( List |
175 | | - .of( new ValidationError.Semantic( message, focusNode, resultPath, Severity, value ) ) ) |
176 | | - .buildInvalidReport(); |
| 210 | + final ValidationError.Semantic semantic = new ValidationError.Semantic( message, focusNode, resultPath, Severity, |
| 211 | + value ); |
| 212 | + return new ValidationReportBuilder().withValidationErrors( List.of( semantic ) ).buildInvalidReport(); |
177 | 213 | } |
178 | 214 | } |
0 commit comments