Skip to content

Commit c2c2f31

Browse files
authored
Fix problems with referenced models (#19)
1 parent d8f009a commit c2c2f31

File tree

5 files changed

+64
-27
lines changed

5 files changed

+64
-27
lines changed

src/main/java/io/openmanufacturing/ame/repository/strategy/LocalFolderResolverStrategy.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
package io.openmanufacturing.ame.repository.strategy;
1515

16-
import static io.openmanufacturing.ame.services.utils.ModelUtils.fetchVersionModel;
16+
import static io.openmanufacturing.ame.services.utils.ModelUtils.inMemoryStrategy;
1717
import static java.util.stream.Collectors.toList;
1818

1919
import java.io.File;
@@ -32,8 +32,6 @@
3232
import org.apache.commons.io.FileUtils;
3333
import org.apache.commons.io.FilenameUtils;
3434
import org.apache.commons.lang3.StringUtils;
35-
import org.apache.jena.rdf.model.StmtIterator;
36-
import org.apache.jena.vocabulary.RDF;
3735
import org.slf4j.Logger;
3836
import org.slf4j.LoggerFactory;
3937
import org.springframework.stereotype.Service;
@@ -47,14 +45,9 @@
4745
import io.openmanufacturing.ame.repository.model.ValidFile;
4846
import io.openmanufacturing.ame.repository.strategy.utils.LocalFolderResolverUtils;
4947
import io.openmanufacturing.ame.services.utils.ModelUtils;
50-
import io.openmanufacturing.sds.aspectmetamodel.KnownVersion;
5148
import io.openmanufacturing.sds.aspectmodel.resolver.services.ExtendedXsdDataType;
5249
import io.openmanufacturing.sds.aspectmodel.resolver.services.SdsAspectMetaModelResourceResolver;
53-
import io.openmanufacturing.sds.aspectmodel.resolver.services.VersionedModel;
5450
import io.openmanufacturing.sds.aspectmodel.urn.AspectModelUrn;
55-
import io.openmanufacturing.sds.aspectmodel.vocabulary.BAMM;
56-
import io.vavr.NotImplementedError;
57-
import io.vavr.control.Try;
5851

5952
@Service
6053
public class LocalFolderResolverStrategy implements ModelResolverStrategy {
@@ -117,7 +110,7 @@ private String getFilePath( final String urn, final String turtleData, final Str
117110
return getFilePathBasedOnTurtleData( turtleData, storagePath ) + applicationSettings.getFileType();
118111
}
119112

120-
if ( ":latest.ttl".equalsIgnoreCase( urn ) ) {
113+
if ( ":latest.ttl".equalsIgnoreCase( urn ) || urn.contains( applicationSettings.getFileType() ) ) {
121114
return LocalFolderResolverUtils.extractFilePath( urn ).toString();
122115
}
123116

@@ -365,15 +358,16 @@ protected File getFileInstance( @Nonnull final String filePath ) {
365358
return storeFile;
366359
}
367360

368-
private Try<AspectModelUrn> getRootElementUrn( final VersionedModel versionedModel ) {
369-
final BAMM bamm = new BAMM( KnownVersion.getLatest() );
370-
final StmtIterator stmtIterator = versionedModel.getModel().listStatements( null, RDF.type, bamm.Aspect() );
371-
372-
if ( stmtIterator.hasNext() ) {
373-
return Try.success( AspectModelUrn.fromUrn( stmtIterator.next().getSubject().getURI() ) );
374-
}
375-
376-
return Try.failure( new NotImplementedError( "AspectModelUrn cannot be found." ) );
361+
/**
362+
* Extract Aspect Model urn from turtle data.
363+
*
364+
* @param turtleData - file used to extract filePath.
365+
* @param storagePath - path to storage files.
366+
* @return Aspect Model urn
367+
*/
368+
protected AspectModelUrn getAspectModelUrn( @Nonnull final String turtleData,
369+
final @Nonnull String storagePath ) {
370+
return inMemoryStrategy( turtleData, storagePath ).getAspectModelUrn();
377371
}
378372

379373
/**
@@ -385,9 +379,7 @@ private Try<AspectModelUrn> getRootElementUrn( final VersionedModel versionedMod
385379
*/
386380
protected String getFilePathBasedOnTurtleData( @Nonnull final String turtleData,
387381
final @Nonnull String storagePath ) {
388-
final Try<AspectModelUrn> urnTemp = fetchVersionModel( turtleData, storagePath ).flatMap(
389-
this::getRootElementUrn );
390-
final AspectModelUrn aspectModelUrn = urnTemp.get();
382+
final AspectModelUrn aspectModelUrn = getAspectModelUrn( turtleData, storagePath );
391383

392384
return aspectModelUrn.getNamespace() + File.separator + aspectModelUrn.getVersion() + File.separator +
393385
aspectModelUrn.getName();

src/main/java/io/openmanufacturing/ame/services/GenerateService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package io.openmanufacturing.ame.services;
1515

16+
import static io.openmanufacturing.ame.services.utils.ModelUtils.inMemoryStrategy;
17+
1618
import java.io.ByteArrayOutputStream;
1719
import java.io.IOException;
1820
import java.util.Optional;
@@ -27,6 +29,7 @@
2729

2830
import io.openmanufacturing.ame.config.ApplicationSettings;
2931
import io.openmanufacturing.ame.exceptions.InvalidAspectModelException;
32+
import io.openmanufacturing.ame.resolver.inmemory.InMemoryStrategy;
3033
import io.openmanufacturing.ame.services.utils.ModelUtils;
3134
import io.openmanufacturing.sds.aspectmodel.generator.docu.AspectModelDocumentationGenerator;
3235
import io.openmanufacturing.sds.aspectmodel.generator.json.AspectModelJsonPayloadGenerator;
@@ -104,8 +107,9 @@ public String sampleJSONPayload( final String aspectModel ) {
104107
}
105108

106109
private Try<VersionedModel> getVersionModel( final String aspectModel, final Optional<String> storagePath ) {
107-
return ModelUtils.fetchVersionModel( aspectModel,
110+
final InMemoryStrategy inMemoryStrategy = inMemoryStrategy( aspectModel,
108111
storagePath.orElse( ApplicationSettings.getMetaModelStoragePath() ) );
112+
return ModelUtils.fetchVersionModel( inMemoryStrategy );
109113
}
110114

111115
public String generateYamlOpenApiSpec( final String aspectModel, final String baseUrl, final boolean includeQueryApi,

src/main/java/io/openmanufacturing/ame/services/utils/ModelUtils.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ public static String getPrettyPrintedVersionedModel( final VersionedModel versio
8585
/**
8686
* Method to resolve a given AspectModelUrn using a suitable ResolutionStrategy.
8787
*
88-
* @param aspectModel as a string.
89-
* @param storagePath stored path to the Aspect Models.
88+
* @param inMemoryStrategy strategy of the backend.
9089
* @return The resolved model on success.
9190
*/
92-
public static Try<VersionedModel> fetchVersionModel( final String aspectModel, final String storagePath ) {
93-
final InMemoryStrategy inMemoryStrategy = inMemoryStrategy( aspectModel, storagePath );
91+
public static Try<VersionedModel> fetchVersionModel( final InMemoryStrategy inMemoryStrategy ) {
9492
return new AspectModelResolver().resolveAspectModel( inMemoryStrategy, inMemoryStrategy.getAspectModelUrn() );
9593
}
9694

@@ -175,7 +173,7 @@ public static ValidationReport validateModel( final String aspectModel, final St
175173
final AspectModelValidator aspectModelValidator ) {
176174
try {
177175
final InMemoryStrategy inMemoryStrategy = inMemoryStrategy( aspectModel, storagePath );
178-
final Try<VersionedModel> versionedModel = ModelUtils.fetchVersionModel( aspectModel, storagePath );
176+
final Try<VersionedModel> versionedModel = ModelUtils.fetchVersionModel( inMemoryStrategy );
179177

180178
if ( versionedModel.isFailure() ) {
181179
final String message = versionedModel.getCause().toString();

src/test/java/io/openmanufacturing/ame/repository/strategy/LocalFolderResolverStrategyTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.File;
2121
import java.io.IOException;
22+
import java.nio.file.Files;
2223
import java.nio.file.Path;
2324
import java.nio.file.Paths;
2425
import java.util.Collections;
@@ -66,6 +67,9 @@ public class LocalFolderResolverStrategyTest {
6667
private static final String NAMESPACE = "com.test.example:1.0.0:AspectDefault.ttl";
6768
private static final String TTL_FILE_CONTENT = "new result ttl file";
6869
private static final String TTL_FILE_EXTENSION = ".ttl";
70+
private static final String TTL_FILE_WITH_EXT_REF =
71+
"io.openmanufacturing" + File.separator + "1.0.0" + File.separator
72+
+ "AspectModelWithExternalRef" + TTL_FILE_EXTENSION;
6973
private static final String COM_TEST_EXAMPLE_1_2_0 =
7074
"com" + File.separator + "test" + File.separator + "example" + File.separator + "1.2.0";
7175
private static final String COM_TEST_EXAMPLE_1_0_0_ASPECT_DEFAULT =
@@ -184,6 +188,16 @@ public void testSaveModel() throws Exception {
184188
assertEquals( COM_TEST_EXAMPLE_1_0_0_ASPECT_DEFAULT + TTL_FILE_EXTENSION, result );
185189
}
186190

191+
@Test
192+
public void testGetFilePathBasedOnTurtleData() throws Exception {
193+
final Path extRefAspectModel = Path.of( resourcesPath.toAbsolutePath().toString(), TTL_FILE_WITH_EXT_REF );
194+
final AspectModelUrn aspectModelUrn = localFolderResolverStrategy.getAspectModelUrn(
195+
Files.readString( extRefAspectModel ),
196+
resourcesPath.toString() );
197+
198+
assertEquals( "urn:bamm:io.openmanufacturing:1.0.0#AspectModelWithExternalRef", aspectModelUrn.toString() );
199+
}
200+
187201
@Test( expected = FileWriteException.class )
188202
public void testSaveModelCanNotWriteToFile() throws Exception {
189203
doReturn( COM_TEST_EXAMPLE_1_0_0_ASPECT_DEFAULT ).when( localFolderResolverStrategy )
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:2.0.0#> .
13+
@prefix bamm-c: <urn:bamm:io.openmanufacturing:characteristic:2.0.0#> .
14+
@prefix bamm-e: <urn:bamm:io.openmanufacturing:entity:2.0.0#> .
15+
@prefix unit: <urn:bamm:io.openmanufacturing:unit:2.0.0#> .
16+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
17+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
18+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
19+
@prefix : <urn:bamm:io.openmanufacturing:1.0.0#> .
20+
@prefix ext-one: <urn:bamm:io.openmanufacturing.one:1.0.0#> .
21+
@prefix ext-two: <urn:bamm:io.openmanufacturing.two:1.0.0#> .
22+
@prefix ext-three: <urn:bamm:io.openmanufacturing.three:1.0.0#> .
23+
24+
:AspectModelWithExternalRef a bamm:Aspect ;
25+
bamm:properties (:property ext-one:property2 ext-two:property3) ;
26+
bamm:operations () .
27+
28+
:property a bamm:Property;
29+
bamm:characteristic ext-three:Characteristic .

0 commit comments

Comments
 (0)