Skip to content

Commit bd55ea9

Browse files
authored
Merge branch 'main' into 481-samm-cli-aas-generation-mapping-of-set-characteristics
2 parents 017177f + c937607 commit bd55ea9

File tree

86 files changed

+2717
-1076
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+2717
-1076
lines changed

core/esmf-aspect-meta-model-interface/src/main/java/org/eclipse/esmf/metamodel/Namespace.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.eclipse.esmf.aspectmodel.AspectModelFile;
1919
import org.eclipse.esmf.aspectmodel.VersionNumber;
20+
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
2021

2122
/**
2223
* Represents the namespace the model elements are contained in
@@ -29,14 +30,28 @@ public interface Namespace extends ModelElementGroup, HasDescription {
2930

3031
/**
3132
* The package part of the model namespace is an identifier given in
32-
* <a href="https://en.wikipedia.org/wiki/Reverse_domain_name_notation">reverse domain name notation</a>, e.g., com.example.myapp.
33+
* <a href="https://en.wikipedia.org/wiki/Reverse_domain_name_notation">reverse domain name notation</a>, e.g.,
34+
* {@code com.example.myapp}.
3335
*
3436
* @return the package part of the namespace
37+
* @deprecated use {@link #namespaceMainPart()} instead
3538
*/
36-
String packagePart();
39+
@Deprecated( forRemoval = true )
40+
default String packagePart() {
41+
return namespaceMainPart();
42+
}
43+
44+
/**
45+
* The package part of the model namespace is an identifier given in
46+
* <a href="https://en.wikipedia.org/wiki/Reverse_domain_name_notation">reverse domain name notation</a>, e.g.,
47+
* {@code com.example.myapp}.
48+
*
49+
* @return the package part of the namespace
50+
*/
51+
String namespaceMainPart();
3752

3853
/**
39-
* The version part of the namespace. This is always a semantic version, e.g. 1.2.3.
54+
* The version part of the namespace. This is always a semantic version, e.g. {@code 1.2.3}.
4055
*
4156
* @return the version part
4257
*/
@@ -54,8 +69,8 @@ public interface Namespace extends ModelElementGroup, HasDescription {
5469
*
5570
* @return the identifier
5671
*/
57-
default String urn() {
58-
return String.format( "urn:samm:%s:%s", packagePart(), version() );
72+
default AspectModelUrn urn() {
73+
return AspectModelUrn.fromUrn( String.format( "urn:samm:%s:%s", namespaceMainPart(), version() ) );
5974
}
6075

6176
/**

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static java.util.stream.Collectors.toSet;
1717

1818
import java.util.HashMap;
19+
import java.util.List;
1920
import java.util.Map;
2021
import java.util.Set;
2122
import java.util.function.Function;
@@ -27,6 +28,9 @@
2728
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
2829

2930
import com.google.common.collect.Streams;
31+
import org.apache.jena.query.ParameterizedSparqlString;
32+
import org.apache.jena.query.QueryExecution;
33+
import org.apache.jena.query.QueryExecutionFactory;
3034
import org.apache.jena.rdf.model.Literal;
3135
import org.apache.jena.rdf.model.Model;
3236
import org.apache.jena.rdf.model.ModelFactory;
@@ -140,4 +144,25 @@ public static void cleanPrefixes( final Model model ) {
140144
public static Model createModel( final String ttlRepresentation ) {
141145
return TurtleLoader.loadTurtle( ttlRepresentation ).get();
142146
}
147+
148+
/**
149+
* Returns the List of named resources that transitively point to a given node. Transtively here means that the given node
150+
* is reachable from the named resource via any number of anonymous nodes inbetween.
151+
*
152+
* @param node the target node
153+
* @return the list of resources that point to the node
154+
*/
155+
public static List<Resource> getNamedElementsReferringTo( final RDFNode node ) {
156+
final ParameterizedSparqlString sparqlString = new ParameterizedSparqlString();
157+
sparqlString.setCommandText( "select ?s where { ?s (<>|!<>)* ?node . }" );
158+
sparqlString.setParam( "node", node );
159+
try ( final QueryExecution queryExecution = QueryExecutionFactory.create( sparqlString.asQuery(), node.getModel() ) ) {
160+
return Streams.stream( queryExecution.execSelect() ).flatMap( querySolution -> {
161+
final RDFNode subject = querySolution.get( "s" );
162+
return subject.isURIResource() && !subject.equals( node )
163+
? Stream.of( subject.asResource() )
164+
: Stream.empty();
165+
} ).toList();
166+
}
167+
}
143168
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/edit/AspectChangeManagerConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public record AspectChangeManagerConfig(
2222
List<String> defaultFileHeader,
2323
boolean detailedChangeReport
2424
) {
25+
public static final AspectChangeManagerConfig DEFAULT = AspectChangeManagerConfigBuilder.builder().build();
26+
2527
public AspectChangeManagerConfig {
2628
if ( defaultFileHeader == null ) {
2729
defaultFileHeader = List.of();

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/edit/ChangeReport.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,75 @@
1616
import java.util.List;
1717
import java.util.Map;
1818

19+
import org.apache.jena.rdf.model.Model;
20+
1921
/**
2022
* A structured representation of a number of {@link Change}s
2123
*/
22-
public interface ChangeReport {
23-
ChangeReport NO_CHANGES = new ChangeReport() {
24-
};
24+
public sealed interface ChangeReport {
25+
ChangeReport NO_CHANGES = new NoChanges();
26+
27+
/**
28+
* Indicates that no changes are performed
29+
*/
30+
record NoChanges( ) implements ChangeReport {
31+
@Override
32+
public <T, C> T accept( final Visitor<T, C> visitor, final C context ) {
33+
return visitor.visitNoChanges( this, context );
34+
}
35+
}
2536

37+
/**
38+
* Simple textual description of a change
39+
*
40+
* @param text the description text
41+
*/
2642
record SimpleEntry( String text ) implements ChangeReport {
43+
@Override
44+
public <T, C> T accept( final Visitor<T, C> visitor, final C context ) {
45+
return visitor.visitSimpleEntry( this, context );
46+
}
2747
}
2848

29-
record EntryWithDetails( String summary, Map<String, Object> details ) implements ChangeReport {
49+
/**
50+
* Description of adding/removing parts of RDF models
51+
*
52+
* @param summary the summary
53+
* @param details detailed descriptions of models used for changes
54+
*/
55+
record EntryWithDetails( String summary, Map<String, Model> details ) implements ChangeReport {
56+
@Override
57+
public <T, C> T accept( final Visitor<T, C> visitor, final C context ) {
58+
return visitor.visitEntryWithDetails( this, context );
59+
}
3060
}
3161

62+
/**
63+
* Structures multiple changes with an optional summary
64+
*
65+
* @param summary the summary
66+
* @param entries the nested reports
67+
*/
3268
record MultipleEntries( String summary, List<ChangeReport> entries ) implements ChangeReport {
3369
public MultipleEntries( final List<ChangeReport> entries ) {
3470
this( null, entries );
3571
}
72+
73+
@Override
74+
public <T, C> T accept( final Visitor<T, C> visitor, final C context ) {
75+
return visitor.visitMultipleEntries( this, context );
76+
}
77+
}
78+
79+
<T, C> T accept( Visitor<T, C> visitor, C context );
80+
81+
interface Visitor<T, C> {
82+
T visitNoChanges( NoChanges noChanges, C context );
83+
84+
T visitSimpleEntry( SimpleEntry simpleEntry, C context );
85+
86+
T visitEntryWithDetails( EntryWithDetails entryWithDetails, C context );
87+
88+
T visitMultipleEntries( MultipleEntries multipleEntries, C context );
3689
}
3790
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/edit/ChangeReportFormatter.java

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,109 +26,110 @@
2626
/**
2727
* Takes a {@link ChangeReport} as an input and renders it as a string
2828
*/
29-
public class ChangeReportFormatter implements BiFunction<ChangeReport, AspectChangeManagerConfig, String> {
29+
public class ChangeReportFormatter implements ChangeReport.Visitor<String, ChangeReportFormatter.Context>,
30+
BiFunction<ChangeReport, AspectChangeManagerConfig, String> {
3031
public static final ChangeReportFormatter INSTANCE = new ChangeReportFormatter();
3132

3233
private ChangeReportFormatter() {
3334
}
3435

35-
private void handleSimpleEntry( final StringBuilder builder, final ChangeReport.SimpleEntry simpleEntry, final String indent ) {
36-
builder.append( indent );
37-
builder.append( "- " );
38-
builder.append( simpleEntry.text() );
39-
builder.append( "\n" );
36+
public record Context( int indentationLevel, AspectChangeManagerConfig config ) {
37+
public Context indent() {
38+
return new Context( indentationLevel() + 1, config() );
39+
}
4040
}
4141

42-
private void handleMultipleEntries( final StringBuilder builder, final ChangeReport.MultipleEntries multipleEntries, final String indent,
43-
final int indentationLevel, final AspectChangeManagerConfig config ) {
44-
if ( multipleEntries.summary() != null ) {
45-
builder.append( indent );
46-
builder.append( "- " );
47-
builder.append( multipleEntries.summary() );
48-
builder.append( "\n" );
49-
}
50-
final List<ChangeReport> entries = multipleEntries.entries();
51-
for ( int i = 0; i < entries.size(); i++ ) {
52-
final ChangeReport entry = entries.get( i );
53-
final int entryIndentation = multipleEntries.summary() == null
54-
? indentationLevel
55-
: indentationLevel + 1;
56-
append( builder, entry, config, entryIndentation );
57-
if ( i < entries.size() - 1 ) {
58-
builder.append( "\n" );
59-
}
60-
}
42+
@Override
43+
public String apply( final ChangeReport changeReport, final AspectChangeManagerConfig aspectChangeManagerConfig ) {
44+
return changeReport.accept( this, new Context( 0, aspectChangeManagerConfig ) );
45+
}
46+
47+
@Override
48+
public String visitNoChanges( final ChangeReport.NoChanges noChanges, final Context context ) {
49+
return "";
50+
}
51+
52+
@Override
53+
public String visitSimpleEntry( final ChangeReport.SimpleEntry simpleEntry, final Context context ) {
54+
return simpleEntry.text();
6155
}
6256

63-
private void handleEntryWithDetails( final StringBuilder builder, final ChangeReport.EntryWithDetails entryWithDetails,
64-
final String indent, final AspectChangeManagerConfig config ) {
57+
@Override
58+
public String visitEntryWithDetails( final ChangeReport.EntryWithDetails entryWithDetails, final Context context ) {
59+
final String indent = " ".repeat( context.indentationLevel() );
60+
final StringBuilder builder = new StringBuilder();
61+
6562
builder.append( indent );
6663
builder.append( "- " );
6764
builder.append( entryWithDetails.summary() );
6865
builder.append( "\n" );
69-
for ( final Map.Entry<String, Object> entry : entryWithDetails.details().entrySet() ) {
70-
if ( config.detailedChangeReport() && entry.getValue() instanceof final Model model ) {
66+
67+
for ( final Map.Entry<String, Model> entry : entryWithDetails.details().entrySet() ) {
68+
if ( context.config().detailedChangeReport() ) {
7169
builder.append( indent );
7270
builder.append( " - " );
7371
builder.append( entry.getKey() );
7472
builder.append( ": " );
7573
builder.append( "\n" );
76-
show( model ).lines()
74+
show( entry.getValue() ).lines()
7775
.forEach( line -> {
7876
builder.append( indent );
7977
builder.append( " " );
8078
builder.append( line );
8179
builder.append( "\n" );
8280
} );
83-
} else if ( !config.detailedChangeReport() && entry.getValue() instanceof final Model model ) {
84-
final int numberOfStatements = model.listStatements().toList().size();
81+
} else {
82+
final int numberOfStatements = entry.getValue().listStatements().toList().size();
83+
final int numberOfPrefixes = entry.getValue().getNsPrefixMap().size();
8584
if ( numberOfStatements > 0 ) {
8685
builder.append( indent );
8786
builder.append( " - " );
8887
builder.append( entry.getKey() );
8988
builder.append( ": " );
9089
builder.append( numberOfStatements );
91-
builder.append( " RDF statements" );
90+
builder.append( " RDF statements and " );
91+
builder.append( numberOfPrefixes );
92+
builder.append( " prefixes" );
9293
builder.append( "\n" );
9394
}
94-
} else {
95-
builder.append( indent );
96-
builder.append( " - " );
97-
builder.append( entry.getKey() );
98-
builder.append( ": " );
99-
builder.append( entry.getValue().toString() );
100-
builder.append( "\n" );
10195
}
10296
}
97+
98+
return builder.toString();
10399
}
104100

105-
private void append( final StringBuilder builder, final ChangeReport report, final AspectChangeManagerConfig config,
106-
final int indentationLevel ) {
107-
final String indent = " ".repeat( indentationLevel );
108-
if ( report instanceof final ChangeReport.SimpleEntry simpleEntry ) {
109-
handleSimpleEntry( builder, simpleEntry, indent );
110-
} else if ( report instanceof final ChangeReport.MultipleEntries multipleEntries ) {
111-
handleMultipleEntries( builder, multipleEntries, indent, indentationLevel, config );
112-
} else if ( report instanceof final ChangeReport.EntryWithDetails entryWithDetails ) {
113-
handleEntryWithDetails( builder, entryWithDetails, indent, config );
101+
@Override
102+
public String visitMultipleEntries( final ChangeReport.MultipleEntries multipleEntries, final Context context ) {
103+
final String indent = " ".repeat( context.indentationLevel() );
104+
final StringBuilder builder = new StringBuilder();
105+
if ( multipleEntries.summary() != null ) {
106+
builder.append( indent );
107+
builder.append( "- " );
108+
builder.append( multipleEntries.summary() );
109+
builder.append( "\n" );
114110
}
111+
final List<ChangeReport> entries = multipleEntries.entries();
112+
for ( int i = 0; i < entries.size(); i++ ) {
113+
final ChangeReport entry = entries.get( i );
114+
final Context nestedContext = multipleEntries.summary() == null ? context : context.indent();
115+
builder.append( entry.accept( this, nestedContext ) );
116+
if ( i < entries.size() - 1 ) {
117+
builder.append( "\n" );
118+
}
119+
}
120+
return builder.toString();
115121
}
116122

117123
private String show( final Model model ) {
118124
final Model copy = ModelFactory.createDefaultModel();
119125
copy.add( model );
120-
RdfUtil.cleanPrefixes( copy );
126+
if ( !copy.listStatements().toList().isEmpty() ) {
127+
RdfUtil.cleanPrefixes( copy );
128+
}
121129
final StringWriter stringWriter = new StringWriter();
122130
stringWriter.append( "--------------------\n" );
123131
copy.write( stringWriter, "TURTLE" );
124132
stringWriter.append( "--------------------\n" );
125133
return stringWriter.toString();
126134
}
127-
128-
@Override
129-
public String apply( final ChangeReport changeReport, final AspectChangeManagerConfig config ) {
130-
final StringBuilder builder = new StringBuilder();
131-
append( builder, changeReport, config, 0 );
132-
return builder.toString();
133-
}
134135
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/edit/change/AbstractChange.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ protected String show( final AspectModelFile aspectModelFile ) {
2424
return show( aspectModelFile.sourceLocation() );
2525
}
2626

27+
protected String show( final URI sourceLocation ) {
28+
return sourceLocation.toString();
29+
}
30+
2731
protected String show( final Optional<URI> sourceLocation ) {
28-
return sourceLocation.map( URI::toString ).orElse( "(unknown file)" );
32+
return sourceLocation.map( this::show ).orElse( "(unknown file)" );
2933
}
3034
}

0 commit comments

Comments
 (0)