Skip to content

Commit ca6712b

Browse files
authored
Merge pull request #293 from bci-oss/add_json_support_to_aas_generator
Add json support to aas generator
2 parents d0127a8 + 18f6dc1 commit ca6712b

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
4040
<artifactId>dataformat-aasx</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
44+
<artifactId>dataformat-json</artifactId>
45+
</dependency>
4246
<dependency>
4347
<groupId>org.eclipse.esmf</groupId>
4448
<artifactId>esmf-test-resources</artifactId>

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import java.util.function.Function;
1919

2020
import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
21+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.Serializer;
2122
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXSerializer;
23+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
2224
import org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.XmlSerializer;
2325
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
2426

@@ -44,7 +46,7 @@ public void generateAASXFile( final Aspect aspect, final Function<String, Output
4446
/**
4547
* Generates an AAS XML archive file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
4648
*
47-
* @param aspect the Aspect for which an AASX archive shall be generated
49+
* @param aspect the Aspect for which an xml file shall be generated
4850
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
4951
* @throws IOException in case the generation can not properly be executed
5052
*/
@@ -55,6 +57,20 @@ public void generateAasXmlFile(
5557
}
5658
}
5759

60+
/**
61+
* Generates an AAS JSON file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
62+
*
63+
* @param aspect the Aspect for which an JSON shall be generated
64+
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
65+
* @throws IOException in case the generation can not properly be executed
66+
*/
67+
public void generateAasJsonFile(
68+
final Aspect aspect, final Function<String, OutputStream> nameMapper ) throws IOException {
69+
try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) {
70+
output.write( generateJsonOutput( aspect ).toByteArray() );
71+
}
72+
}
73+
5874
protected ByteArrayOutputStream generateAasxOutput( Aspect aspect ) throws IOException {
5975
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
6076
Environment environment = visitor.visitAspect( aspect, null );
@@ -69,11 +85,18 @@ protected ByteArrayOutputStream generateAasxOutput( Aspect aspect ) throws IOExc
6985
}
7086

7187
protected ByteArrayOutputStream generateXmlOutput( Aspect aspect ) throws IOException {
88+
return generate( new XmlSerializer(), aspect );
89+
}
90+
91+
protected ByteArrayOutputStream generateJsonOutput( Aspect aspect ) throws IOException {
92+
return generate( new JsonSerializer(), aspect );
93+
}
94+
95+
protected ByteArrayOutputStream generate( Serializer serializer, Aspect aspect ) throws IOException {
7296
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
7397
Environment environment = visitor.visitAspect( aspect, null );
7498

7599
try ( ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
76-
XmlSerializer serializer = new XmlSerializer();
77100
serializer.write( out, environment );
78101
return out;
79102
} catch ( 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)