Skip to content

Commit 17e21a4

Browse files
committed
Simplify AspectModelFile API (include filename() method)
1 parent faa5246 commit 17e21a4

File tree

6 files changed

+80
-24
lines changed

6 files changed

+80
-24
lines changed

core/esmf-aspect-meta-model-interface/src/main/java/org/eclipse/esmf/aspectmodel/AspectModelFile.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.Optional;
1919

20+
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
2021
import org.eclipse.esmf.metamodel.ModelElement;
2122
import org.eclipse.esmf.metamodel.ModelElementGroup;
2223
import org.eclipse.esmf.metamodel.Namespace;
@@ -68,6 +69,25 @@ default Optional<String> spdxLicenseIdentifier() {
6869
*/
6970
Optional<URI> sourceLocation();
7071

72+
/**
73+
* Returns the local file name ("something.ttl") of this AspectModelFile, based on its source location.
74+
*
75+
* @return the local Aspect Model file
76+
*/
77+
default Optional<String> filename() {
78+
return sourceLocation().flatMap( uri -> {
79+
final String path = uri.toString();
80+
if ( !path.contains( "/" ) ) {
81+
return Optional.empty();
82+
}
83+
final String filename = path.substring( path.lastIndexOf( "/" ) + 1 );
84+
if ( filename.endsWith( ".ttl" ) && filename.length() >= 5 ) {
85+
return Optional.of( filename );
86+
}
87+
return Optional.empty();
88+
} );
89+
}
90+
7191
/**
7292
* Returns the {@link Namespace} this AspectModelFile is a part of
7393
*
@@ -77,6 +97,15 @@ default Namespace namespace() {
7797
throw new UnsupportedOperationException( "Uninitialized Aspect Model" );
7898
}
7999

100+
/**
101+
* Returns the URN of the namespace of this file
102+
*
103+
* @return the namespace URN
104+
*/
105+
default AspectModelUrn namespaceUrn() {
106+
return namespace().urn();
107+
}
108+
80109
/**
81110
* Lists the model elements that are contained in this AspectModelFile
82111
*

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/ModelSource.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.esmf.aspectmodel.resolver;
1515

1616
import java.net.URI;
17+
import java.util.Iterator;
1718
import java.util.stream.Stream;
1819

1920
import org.eclipse.esmf.aspectmodel.AspectModelFile;
@@ -22,7 +23,7 @@
2223
/**
2324
* A model source is one place where {@link AspectModelFile}s are stored and can be retrieved from.
2425
*/
25-
public interface ModelSource {
26+
public interface ModelSource extends Iterable<AspectModelFile> {
2627
/**
2728
* Lists all URIs of Aspect Model files in this model source
2829
*
@@ -52,4 +53,14 @@ public interface ModelSource {
5253
* @return the Aspect Model files
5354
*/
5455
Stream<AspectModelFile> loadContentsForNamespace( AspectModelUrn namespace );
56+
57+
/**
58+
* Makes the contents of this model sources iterable
59+
*
60+
* @return an iterator for the AspectModelFiles in this model source
61+
*/
62+
@Override
63+
default Iterator<AspectModelFile> iterator() {
64+
return loadContents().iterator();
65+
}
5566
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/NamespacePackage.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@
3939
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
4040
import org.eclipse.esmf.aspectmodel.resolver.modelfile.RawAspectModelFile;
4141
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
42+
import org.eclipse.esmf.aspectmodel.serializer.SerializationException;
4243
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
4344
import org.eclipse.esmf.aspectmodel.versionupdate.MetaModelVersionMigrator;
4445
import org.eclipse.esmf.metamodel.AspectModel;
4546
import org.eclipse.esmf.metamodel.Namespace;
4647

4748
import com.google.common.collect.ImmutableList;
49+
import lombok.Getter;
4850
import org.apache.commons.io.IOUtils;
4951
import org.slf4j.Logger;
5052
import org.slf4j.LoggerFactory;
@@ -61,6 +63,7 @@ public class NamespacePackage implements ResolutionStrategy, Artifact<URI, byte[
6163
private static final String ASPECT_MODELS_FOLDER = "aspect-models";
6264

6365
// Fields that are always set
66+
@Getter
6467
private final String modelsRoot;
6568
private final List<AspectModelFile> files;
6669

@@ -261,19 +264,7 @@ private void addFile( final ZipOutputStream zipOutputStream, final String path,
261264
}
262265

263266
private String outputFileName( final AspectModelFile aspectModelFile ) {
264-
return aspectModelFile.sourceLocation()
265-
.flatMap( uri -> {
266-
final String path = uri.toString();
267-
if ( !path.contains( "/" ) ) {
268-
return Optional.empty();
269-
}
270-
final String filename = path.substring( path.lastIndexOf( "/" ) + 1 );
271-
if ( filename.endsWith( ".ttl" ) && filename.length() >= 5 ) {
272-
return Optional.of( filename );
273-
}
274-
return Optional.empty();
275-
} )
276-
.orElseGet( () -> "definitions" + aspectModelFile.hashCode() + ".ttl" );
267+
return aspectModelFile.filename().orElseGet( () -> "definitions" + aspectModelFile.hashCode() + ".ttl" );
277268
}
278269

279270
@Override
@@ -304,10 +295,21 @@ public byte[] serialize() {
304295
addFile( out, filePath, fileContent );
305296
}
306297
}
307-
} catch ( final IOException e ) {
308-
throw new RuntimeException( e );
298+
} catch ( final IOException exception ) {
299+
throw new SerializationException( exception );
309300
}
310301

311302
return buffer.toByteArray();
312303
}
304+
305+
/**
306+
* Returns the location of the namespace package, if it was created from an external location. If it was created from an in-memory
307+
* {@link AspectModel}, the returned location is null.
308+
*
309+
* @return the location if set, or null
310+
*/
311+
@SuppressWarnings( { "LombokGetterMayBeUsed", "RedundantSuppression" } )
312+
public URI getLocation() {
313+
return location;
314+
}
313315
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/modelfile/DefaultAspectModelFile.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@
2222
import org.eclipse.esmf.metamodel.ModelElement;
2323
import org.eclipse.esmf.metamodel.Namespace;
2424

25+
import lombok.Setter;
2526
import org.apache.jena.rdf.model.Model;
2627

28+
/**
29+
* Default implementation of an AspectModelFile that not only provides access to the underlying RDF graph but also the object representation
30+
* of the model elements in this file
31+
*/
2732
public final class DefaultAspectModelFile implements AspectModelFile {
2833
private final Model sourceModel;
2934
private final List<String> headerComment;
3035
private final Optional<URI> sourceLocation;
36+
@Setter
3137
private List<ModelElement> elements;
38+
@Setter
3239
private Namespace namespace = null;
3340

3441
public DefaultAspectModelFile( final Model sourceModel, final List<String> headerComment, final Optional<URI> sourceLocation ) {
@@ -68,14 +75,6 @@ public Namespace namespace() {
6875
return namespace;
6976
}
7077

71-
public void setElements( final List<ModelElement> elements ) {
72-
this.elements = elements;
73-
}
74-
75-
public void setNamespace( final Namespace namespace ) {
76-
this.namespace = namespace;
77-
}
78-
7978
@Override
8079
public boolean equals( final Object obj ) {
8180
if ( obj == this ) {

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/modelfile/MetaModelFile.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ public enum MetaModelFileType {
7575
@Getter
7676
private final MetaModelFileType metaModelFileType;
7777
private final Model sourceModel;
78+
private final String filename;
7879

7980
MetaModelFile( final String section, final String filename, final RdfNamespace rdfNamespace,
8081
final MetaModelFileType metaModelFileType ) {
82+
this.filename = filename;
8183
this.rdfNamespace = rdfNamespace;
8284
this.metaModelFileType = metaModelFileType;
8385
sourceModel = TurtleLoader.loadTurtle( url( section, filename ) )
@@ -147,6 +149,11 @@ public Optional<URI> sourceLocation() {
147149
return Optional.of( URI.create( rdfNamespace.getUri() ) );
148150
}
149151

152+
@Override
153+
public Optional<String> filename() {
154+
return Optional.of( filename );
155+
}
156+
150157
public static List<MetaModelFile> getElementDefinitionsFiles() {
151158
return Arrays.stream( values() ).filter( file -> file.metaModelFileType == MetaModelFileType.ELEMENT_DEFINITION ).toList();
152159
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/resolver/modelfile/RawAspectModelFile.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Optional;
1919

2020
import org.eclipse.esmf.aspectmodel.AspectModelFile;
21+
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
22+
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
2123

2224
import io.soabase.recordbuilder.core.RecordBuilder;
2325
import org.apache.jena.rdf.model.Model;
@@ -55,4 +57,10 @@ public record RawAspectModelFile(
5557
public String toString() {
5658
return sourceLocation().map( URI::toString ).orElse( "(unknown file)" );
5759
}
60+
61+
@Override
62+
public AspectModelUrn namespaceUrn() {
63+
return AspectModelUrn.from( sourceModel().getNsPrefixMap().get( "" ).replace( "#", "" ) )
64+
.getOrElseThrow( () -> new ModelResolutionException( "Aspect Model File " + this + " does not contain a '@prefix :'" ) );
65+
}
5866
}

0 commit comments

Comments
 (0)