Skip to content

Commit 9d3deac

Browse files
authored
Merge pull request #364 from bci-oss/feature/add-generator-for-submodels
Feature/add generator for submodels
2 parents 3491bcf + 28a92a5 commit 9d3deac

File tree

24 files changed

+1075
-296
lines changed

24 files changed

+1075
-296
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
<artifactId>esmf-aspect-model-aas-generator</artifactId>
1414
<name>ESMF Aspect Model AAS Generator</name>
1515

16-
<properties>
17-
<maven.compiler.source>11</maven.compiler.source>
18-
<maven.compiler.target>11</maven.compiler.target>
19-
</properties>
20-
2116
<dependencies>
2217
<dependency>
2318
<groupId>org.eclipse.esmf</groupId>

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

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@
1515
import java.io.ByteArrayOutputStream;
1616
import java.io.IOException;
1717
import java.io.OutputStream;
18+
import java.util.Collections;
19+
import java.util.Map;
1820
import java.util.function.Function;
21+
import java.util.stream.Collectors;
1922

2023
import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
2124
import org.eclipse.digitaltwin.aas4j.v3.dataformat.Serializer;
2225
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXSerializer;
2326
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
2427
import org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.XmlSerializer;
2528
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
26-
29+
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
30+
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEnvironment;
31+
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
2732
import org.eclipse.esmf.metamodel.Aspect;
2833

29-
/** Generator that generates an AASX file containing an AAS submodel for a given Aspect model */
34+
import com.fasterxml.jackson.databind.JsonNode;
35+
36+
/**
37+
* Generator that generates an AASX file containing an AAS submodel for a given Aspect model
38+
*/
3039
public class AspectModelAASGenerator {
3140

3241
/**
@@ -57,6 +66,13 @@ public void generateAasXmlFile(
5766
}
5867
}
5968

69+
public void generateAasXmlFile(
70+
final Aspect aspect, final JsonNode aspectData, final Function<String, OutputStream> nameMapper ) throws IOException {
71+
try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) {
72+
output.write( generateXmlOutput( Map.of( aspect, aspectData ) ).toByteArray() );
73+
}
74+
}
75+
6076
/**
6177
* Generates an AAS JSON file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
6278
*
@@ -71,20 +87,54 @@ public void generateAasJsonFile(
7187
}
7288
}
7389

74-
protected ByteArrayOutputStream generateAasxOutput( Aspect aspect ) throws IOException {
90+
protected ByteArrayOutputStream generateXmlOutput( final Map<Aspect, JsonNode> aspectsWithData ) throws IOException {
91+
final AspectModelAASVisitor visitor = new AspectModelAASVisitor().withPropertyMapper( new LangStringPropertyMapper() );
92+
93+
final Map<Aspect, Environment> aspectEnvironments =
94+
aspectsWithData.entrySet().stream()
95+
.map( aspectWithData -> {
96+
final Submodel submodel = new DefaultSubmodel.Builder().build();
97+
final Environment environment = new DefaultEnvironment.Builder().submodels( Collections.singletonList( submodel ) ).build();
98+
final Context context = new Context( environment, submodel );
99+
context.setEnvironment( environment );
100+
context.setAspectData( aspectWithData.getValue() );
101+
102+
return Map.entry( aspectWithData.getKey(), visitor.visitAspect( aspectWithData.getKey(), context ) );
103+
} )
104+
.collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ) );
105+
final Environment mergedEnvironment = mergeEnvironments( aspectEnvironments );
106+
try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
107+
final XmlSerializer serializer = new XmlSerializer();
108+
serializer.write( out, mergedEnvironment );
109+
return out;
110+
} catch ( final SerializationException e ) {
111+
throw new IOException( e );
112+
}
113+
}
114+
115+
private Environment mergeEnvironments( final Map<Aspect, Environment> aspectEnvironments ) {
116+
final Submodel submodel = new DefaultSubmodel.Builder().build();
117+
return new DefaultEnvironment.Builder()
118+
.assetAdministrationShells( aspectEnvironments.values().stream().flatMap( e -> e.getAssetAdministrationShells().stream() ).toList() )
119+
.submodels( aspectEnvironments.values().stream().flatMap( e -> e.getSubmodels().stream() ).toList() )
120+
.conceptDescriptions( aspectEnvironments.values().stream().flatMap( e -> e.getConceptDescriptions().stream() ).toList() )
121+
.build();
122+
}
123+
124+
protected ByteArrayOutputStream generateAasxOutput( final Aspect aspect ) throws IOException {
75125
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
76-
Environment environment = visitor.visitAspect( aspect, null );
126+
final Environment environment = visitor.visitAspect( aspect, null );
77127

78-
try ( ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
79-
AASXSerializer serializer = new AASXSerializer();
128+
try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
129+
final AASXSerializer serializer = new AASXSerializer();
80130
serializer.write( environment, null, out );
81131
return out;
82-
} catch ( SerializationException e ) {
132+
} catch ( final SerializationException e ) {
83133
throw new IOException( e );
84134
}
85135
}
86136

87-
protected ByteArrayOutputStream generateXmlOutput( Aspect aspect ) throws IOException {
137+
protected ByteArrayOutputStream generateXmlOutput( final Aspect aspect ) throws IOException {
88138
return generate( new XmlSerializer(), aspect );
89139
}
90140

@@ -94,12 +144,12 @@ protected ByteArrayOutputStream generateJsonOutput( Aspect aspect ) throws IOExc
94144

95145
protected ByteArrayOutputStream generate( Serializer serializer, Aspect aspect ) throws IOException {
96146
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
97-
Environment environment = visitor.visitAspect( aspect, null );
147+
final Environment environment = visitor.visitAspect( aspect, null );
98148

99-
try ( ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
149+
try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
100150
serializer.write( out, environment );
101151
return out;
102-
} catch ( SerializationException e ) {
152+
} catch ( final SerializationException e ) {
103153
throw new IOException( e );
104154
}
105155
}

0 commit comments

Comments
 (0)