15
15
import java .io .ByteArrayOutputStream ;
16
16
import java .io .IOException ;
17
17
import java .io .OutputStream ;
18
+ import java .util .Collections ;
19
+ import java .util .Map ;
18
20
import java .util .function .Function ;
21
+ import java .util .stream .Collectors ;
19
22
20
23
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .SerializationException ;
21
24
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .Serializer ;
22
25
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .aasx .AASXSerializer ;
23
26
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .json .JsonSerializer ;
24
27
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .xml .XmlSerializer ;
25
28
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 ;
27
32
import org .eclipse .esmf .metamodel .Aspect ;
28
33
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
+ */
30
39
public class AspectModelAASGenerator {
31
40
32
41
/**
@@ -57,6 +66,13 @@ public void generateAasXmlFile(
57
66
}
58
67
}
59
68
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
+
60
76
/**
61
77
* Generates an AAS JSON file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
62
78
*
@@ -71,20 +87,54 @@ public void generateAasJsonFile(
71
87
}
72
88
}
73
89
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 {
75
125
final AspectModelAASVisitor visitor = new AspectModelAASVisitor ();
76
- Environment environment = visitor .visitAspect ( aspect , null );
126
+ final Environment environment = visitor .visitAspect ( aspect , null );
77
127
78
- try ( ByteArrayOutputStream out = new ByteArrayOutputStream () ) {
79
- AASXSerializer serializer = new AASXSerializer ();
128
+ try ( final ByteArrayOutputStream out = new ByteArrayOutputStream () ) {
129
+ final AASXSerializer serializer = new AASXSerializer ();
80
130
serializer .write ( environment , null , out );
81
131
return out ;
82
- } catch ( SerializationException e ) {
132
+ } catch ( final SerializationException e ) {
83
133
throw new IOException ( e );
84
134
}
85
135
}
86
136
87
- protected ByteArrayOutputStream generateXmlOutput ( Aspect aspect ) throws IOException {
137
+ protected ByteArrayOutputStream generateXmlOutput ( final Aspect aspect ) throws IOException {
88
138
return generate ( new XmlSerializer (), aspect );
89
139
}
90
140
@@ -94,12 +144,12 @@ protected ByteArrayOutputStream generateJsonOutput( Aspect aspect ) throws IOExc
94
144
95
145
protected ByteArrayOutputStream generate ( Serializer serializer , Aspect aspect ) throws IOException {
96
146
final AspectModelAASVisitor visitor = new AspectModelAASVisitor ();
97
- Environment environment = visitor .visitAspect ( aspect , null );
147
+ final Environment environment = visitor .visitAspect ( aspect , null );
98
148
99
- try ( ByteArrayOutputStream out = new ByteArrayOutputStream () ) {
149
+ try ( final ByteArrayOutputStream out = new ByteArrayOutputStream () ) {
100
150
serializer .write ( out , environment );
101
151
return out ;
102
- } catch ( SerializationException e ) {
152
+ } catch ( final SerializationException e ) {
103
153
throw new IOException ( e );
104
154
}
105
155
}
0 commit comments