Skip to content

Commit 6e92aef

Browse files
Merge branch 'main' into feature/add-generator-for-submodels
# Conflicts: # core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGenerator.java
2 parents 0c9166b + ca6712b commit 6e92aef

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

core/esmf-aspect-model-aas-generator/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
3535
<artifactId>dataformat-aasx</artifactId>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
39+
<artifactId>dataformat-json</artifactId>
40+
</dependency>
3741
<dependency>
3842
<groupId>org.eclipse.esmf</groupId>
3943
<artifactId>esmf-test-resources</artifactId>

core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGenerator.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121
import java.util.stream.Collectors;
2222

2323
import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
24+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.Serializer;
2425
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXSerializer;
26+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
2527
import org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.XmlSerializer;
2628
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
2729
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
2830
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEnvironment;
2931
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
32+
import org.eclipse.esmf.metamodel.Aspect;
3033

3134
import com.fasterxml.jackson.databind.JsonNode;
3235

33-
import org.eclipse.esmf.metamodel.Aspect;
34-
3536
/**
3637
* Generator that generates an AASX file containing an AAS submodel for a given Aspect model
3738
*/
@@ -54,7 +55,7 @@ public void generateAASXFile( final Aspect aspect, final Function<String, Output
5455
/**
5556
* Generates an AAS XML archive file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
5657
*
57-
* @param aspect the Aspect for which an AASX archive shall be generated
58+
* @param aspect the Aspect for which an xml file shall be generated
5859
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
5960
* @throws IOException in case the generation can not properly be executed
6061
*/
@@ -72,6 +73,20 @@ public void generateAasXmlFile(
7273
}
7374
}
7475

76+
/**
77+
* Generates an AAS JSON file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
78+
*
79+
* @param aspect the Aspect for which an JSON shall be generated
80+
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
81+
* @throws IOException in case the generation can not properly be executed
82+
*/
83+
public void generateAasJsonFile(
84+
final Aspect aspect, final Function<String, OutputStream> nameMapper ) throws IOException {
85+
try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) {
86+
output.write( generateJsonOutput( aspect ).toByteArray() );
87+
}
88+
}
89+
7590
protected ByteArrayOutputStream generateXmlOutput( final Map<Aspect, JsonNode> aspectsWithData ) throws IOException {
7691
final AspectModelAASVisitor visitor = new AspectModelAASVisitor().withPropertyMapper( new LangStringPropertyMapper() );
7792

@@ -122,11 +137,18 @@ protected ByteArrayOutputStream generateAasxOutput( final Aspect aspect ) throws
122137
}
123138

124139
protected ByteArrayOutputStream generateXmlOutput( final Aspect aspect ) throws IOException {
140+
return generate( new XmlSerializer(), aspect );
141+
}
142+
143+
protected ByteArrayOutputStream generateJsonOutput( Aspect aspect ) throws IOException {
144+
return generate( new JsonSerializer(), aspect );
145+
}
146+
147+
protected ByteArrayOutputStream generate( Serializer serializer, Aspect aspect ) throws IOException {
125148
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
126149
final Environment environment = visitor.visitAspect( aspect, null );
127150

128151
try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
129-
final XmlSerializer serializer = new XmlSerializer();
130152
serializer.write( out, environment );
131153
return out;
132154
} catch ( final SerializationException e ) {

documentation/developer-guide/modules/tooling-guide/pages/samm-cli.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ The available options and their meaning can also be seen in the help text of the
9595
| _--custom-resolver_ : use an external resolver for the resolution of the model elements |
9696
.4+| aspect <model> to aas | Generate Asset Administration Shell (AAS) submodel template for an Aspect Model | `samm aspect AspectModel.ttl to aas`
9797
| _--output, -o_ : output file path (default: stdout) |
98-
| _--format, -f_ : output file format (xml or aasx, default: xml)|
98+
| _--format, -f_ : output file format (xml, json, or aasx, default: xml)|
9999
| _--custom-resolver_ : use an external resolver for the resolution of the model elements |
100100
|===
101101

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<parent>
1919
<groupId>org.eclipse.esmf</groupId>
2020
<artifactId>esmf-parent</artifactId>
21-
<version>3</version>
21+
<version>4</version>
2222
</parent>
2323

2424
<artifactId>esmf-sdk-parent</artifactId>

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToAasCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class AspectToAasCommand extends AbstractCommand {
3535
public static final String COMMAND_NAME = "aas";
3636
public static final String AASX = "aasx";
3737
public static final String XML = "xml";
38+
public static final String JSON = "json";
39+
3840

3941
@CommandLine.Option(
4042
names = { "--output", "-o" },
@@ -43,7 +45,7 @@ public class AspectToAasCommand extends AbstractCommand {
4345

4446
@CommandLine.Option(
4547
names = { "--format", "-f" },
46-
description = "The file format the AAS is to be generated. Valid options are \"" + AASX + "\" and \"" + XML + "\". Default is \"" + XML + "\"." )
48+
description = "The file format the AAS is to be generated. Valid options are \"" + AASX + "\", \"" + JSON + "\", and \"" + XML + "\". Default is \"" + XML + "\"." )
4749
private String format = XML;
4850

4951
@CommandLine.ParentCommand
@@ -63,9 +65,14 @@ public void run() {
6365
// we intentionally override the name of the generated artifact here to the name explicitly
6466
// desired by the user (outputFilePath), as opposed to what the model thinks it should be
6567
// called (name)
66-
if ( format.equals( AASX ) ) {
68+
switch ( format ) {
69+
case AASX:
6770
generator.generateAASXFile( aspect, name -> getStreamForFile( outputFilePath ) );
68-
} else {
71+
break;
72+
case JSON:
73+
generator.generateAasJsonFile( aspect, name -> getStreamForFile( outputFilePath ) );
74+
break;
75+
default:
6976
generator.generateAasXmlFile( aspect, name -> getStreamForFile( outputFilePath ) );
7077
}
7178
} catch ( final IOException e ) {

tools/samm-cli/src/test/java/org/eclipse/esmf/SammCliTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void testAspectToAasXmlToStdout() {
219219
}
220220

221221
@Test
222-
public void testAspectToAasAasxToFile() throws TikaException, IOException {
222+
public void testAspectToAasAasxToFile() {
223223
final File targetFile = outputFile( "output.aasx" );
224224
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "aas", "--format", "aasx", "-o",
225225
targetFile.getAbsolutePath() );
@@ -230,12 +230,30 @@ public void testAspectToAasAasxToFile() throws TikaException, IOException {
230230
}
231231

232232
@Test
233-
public void testAspectToAasAasxToStdout() throws TikaException, IOException {
233+
public void testAspectToAasAasxToStdout() {
234234
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "aas", "--format", "aasx" );
235235
assertThat( result.stderr() ).isEmpty();
236236
assertThat( contentType( result.stdoutRaw() ) ).isEqualTo( MediaType.application( "x-tika-ooxml" ) );
237237
}
238238

239+
@Test
240+
public void testAspectToAasJsonToFile() {
241+
final File targetFile = outputFile( "output.json" );
242+
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "aas", "--format", "json", "-o",
243+
targetFile.getAbsolutePath() );
244+
assertThat( result.stdout() ).isEmpty();
245+
assertThat( result.stderr() ).isEmpty();
246+
assertThat( targetFile ).exists();
247+
assertThat( contentType( targetFile ) ).isEqualTo( MediaType.text( "plain" ) );
248+
}
249+
250+
@Test
251+
public void testAspectToAasJsonToStdout() {
252+
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "aas", "--format", "json" );
253+
assertThat( result.stderr() ).isEmpty();
254+
assertThat( contentType( result.stdoutRaw() ) ).isEqualTo( MediaType.text( "plain" ) );
255+
}
256+
239257
@Test
240258
public void testAspectToDotWithDefaultLanguage() {
241259
final File targetFile = outputFile( "output.dot" );
@@ -585,7 +603,7 @@ public void testAspectToOpenApiWithResourcePathAndCustomResolver() {
585603
}
586604

587605
@Test
588-
public void testAspectToPngWithDefaultLanguage() throws TikaException, IOException {
606+
public void testAspectToPngWithDefaultLanguage() {
589607
final File targetFile = outputFile( "output.png" );
590608
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "png", "-o",
591609
targetFile.getAbsolutePath() );
@@ -596,7 +614,7 @@ public void testAspectToPngWithDefaultLanguage() throws TikaException, IOExcepti
596614
}
597615

598616
@Test
599-
public void testAspectToPngWithGivenLanguage() throws TikaException, IOException {
617+
public void testAspectToPngWithGivenLanguage() {
600618
final File targetFile = outputFile( "output.png" );
601619
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "png", "-o",
602620
targetFile.getAbsolutePath(),
@@ -619,7 +637,7 @@ public void testAspectToPngWithNonExistentLanguage() {
619637
}
620638

621639
@Test
622-
public void testAspectToPngToStdout() throws TikaException, IOException {
640+
public void testAspectToPngToStdout() {
623641
final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "png" );
624642
assertThat( result.stderr() ).isEmpty();
625643
assertThat( contentType( result.stdoutRaw() ) ).isEqualTo( MediaType.image( "png" ) );

0 commit comments

Comments
 (0)