13
13
package org .eclipse .esmf .aspectmodel .aas ;
14
14
15
15
import java .io .ByteArrayOutputStream ;
16
+ import java .io .IOException ;
16
17
import java .io .OutputStream ;
17
18
import java .util .List ;
18
19
import java .util .function .Function ;
20
+ import java .util .stream .Stream ;
21
+
19
22
import javax .annotation .Nullable ;
20
23
21
- import org .eclipse .esmf .functions . ThrowingBiConsumer ;
24
+ import org .eclipse .esmf .aspectmodel . generator . AspectGenerator ;
22
25
import org .eclipse .esmf .metamodel .Aspect ;
23
26
24
27
import com .fasterxml .jackson .databind .JsonNode ;
25
28
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .aasx .AASXSerializer ;
29
+ import org .eclipse .digitaltwin .aas4j .v3 .dataformat .core .SerializationException ;
26
30
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .json .JsonSerializer ;
27
31
import org .eclipse .digitaltwin .aas4j .v3 .dataformat .xml .XmlSerializer ;
28
32
import org .eclipse .digitaltwin .aas4j .v3 .model .Environment ;
33
37
/**
34
38
* Generator that generates an AAS file containing an AAS submodel for a given Aspect model.
35
39
*/
36
- public class AspectModelAasGenerator {
37
-
40
+ public class AspectModelAasGenerator extends AspectGenerator <String , byte [], AasGenerationConfig , AasArtifact > {
41
+ /*
42
+ * This should be removed together with the deprecated constructors
43
+ */
44
+ @ Deprecated
38
45
private List <PropertyMapper <?>> propertyMappers = List .of ();
39
46
47
+ public AspectModelAasGenerator ( final Aspect aspect , final AasGenerationConfig config ) {
48
+ super ( aspect , config );
49
+ }
50
+
51
+ @ Deprecated ( forRemoval = true )
40
52
public AspectModelAasGenerator () {
53
+ super ( null , null );
41
54
}
42
55
56
+ @ Deprecated ( forRemoval = true )
43
57
public AspectModelAasGenerator ( final List <PropertyMapper <?>> propertyMappers ) {
58
+ super ( null , null );
44
59
this .propertyMappers = propertyMappers ;
45
60
}
46
61
62
+ @ Override
63
+ public Stream <AasArtifact > generate () {
64
+ final AspectModelAasVisitor visitor = new AspectModelAasVisitor ().withPropertyMapper ( new LangStringPropertyMapper () );
65
+ config .propertyMappers ().forEach ( visitor ::withPropertyMapper );
66
+ final Context context ;
67
+ if ( config .aspectData () != null ) {
68
+ final Submodel submodel = new DefaultSubmodel .Builder ().build ();
69
+ final Environment inputEnvironment = new DefaultEnvironment .Builder ().submodels ( List .of ( submodel ) ).build ();
70
+ context = new Context ( inputEnvironment , submodel );
71
+ context .setEnvironment ( inputEnvironment );
72
+ context .setAspectData ( config .aspectData () );
73
+ } else {
74
+ context = null ;
75
+ }
76
+
77
+ try {
78
+ final Environment environment = visitor .visitAspect ( aspect (), context );
79
+ final byte [] result = switch ( config .format () ) {
80
+ case XML -> new XmlSerializer ().write ( environment ).getBytes ();
81
+ case JSON -> new JsonSerializer ().write ( environment ).getBytes ();
82
+ case AASX -> {
83
+ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
84
+ new AASXSerializer ().write ( environment , null , outputStream );
85
+ yield outputStream .toByteArray ();
86
+ }
87
+ };
88
+ return Stream .of ( new AasArtifact ( aspect ().getName () + "." + config .format (), result ) );
89
+ } catch ( final SerializationException | IOException exception ) {
90
+ throw new AasGenerationException ( exception );
91
+ }
92
+ }
93
+
47
94
/**
48
95
* Generates an AAS file for a given Aspect.
49
96
*
50
97
* @param format the output format
51
98
* @param aspect the Aspect for which an AASX archive shall be generated
52
99
* @return the generated AAS file as byte array
100
+ * @deprecated Use {@link #AspectModelAasGenerator(Aspect, AasGenerationConfig)} and {@link #getContent()} instead
53
101
*/
102
+ @ Deprecated ( forRemoval = true )
54
103
public byte [] generateAsByteArray ( final AasFileFormat format , final Aspect aspect ) {
55
- return generateAsByteArray ( format , aspect , null );
104
+ final AasGenerationConfig generationConfig = AasGenerationConfigBuilder .builder ()
105
+ .format ( format )
106
+ .propertyMappers ( propertyMappers )
107
+ .build ();
108
+ return new AspectModelAasGenerator ( aspect , generationConfig ).getContent ();
56
109
}
57
110
58
111
/**
@@ -62,11 +115,16 @@ public byte[] generateAsByteArray( final AasFileFormat format, final Aspect aspe
62
115
* @param aspect the Aspect for which an AASX archive shall be generated
63
116
* @return the generated AAS file as byte array
64
117
* @throws AasGenerationException in case the generation can not properly be executed
118
+ * @deprecated Use {@link #AspectModelAasGenerator(Aspect, AasGenerationConfig)} and {@link #getContent()} instead
65
119
*/
120
+ @ Deprecated ( forRemoval = true )
66
121
public byte [] generateAsByteArray ( final AasFileFormat format , final Aspect aspect , final JsonNode aspectData ) {
67
- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
68
- generate ( format , aspect , aspectData , name -> baos );
69
- return baos .toByteArray ();
122
+ final AasGenerationConfig generationConfig = AasGenerationConfigBuilder .builder ()
123
+ .format ( format )
124
+ .aspectData ( aspectData )
125
+ .propertyMappers ( propertyMappers )
126
+ .build ();
127
+ return new AspectModelAasGenerator ( aspect , generationConfig ).getContent ();
70
128
}
71
129
72
130
/**
@@ -76,17 +134,15 @@ public byte[] generateAsByteArray( final AasFileFormat format, final Aspect aspe
76
134
* @param aspect the Aspect for which an AASX archive shall be generated
77
135
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
78
136
* @throws AasGenerationException in case the generation can not properly be executed
137
+ * @deprecated Use {@link #AspectModelAasGenerator(Aspect, AasGenerationConfig)} and {@link #generate(Function)} instead
79
138
*/
139
+ @ Deprecated ( forRemoval = true )
80
140
public void generate ( final AasFileFormat format , final Aspect aspect , final Function <String , OutputStream > nameMapper ) {
81
- generate ( format , aspect , null , nameMapper );
82
- }
83
-
84
- private ThrowingBiConsumer <Environment , OutputStream , Throwable > serializer ( final AasFileFormat format ) {
85
- return switch ( format ) {
86
- case AASX -> ( environment , output ) -> new AASXSerializer ().write ( environment , null , output );
87
- case XML -> ( environment , output ) -> output .write ( new XmlSerializer ().write ( environment ).getBytes () );
88
- case JSON -> ( environment , output ) -> output .write ( new JsonSerializer ().write ( environment ).getBytes () );
89
- };
141
+ final AasGenerationConfig generationConfig = AasGenerationConfigBuilder .builder ()
142
+ .format ( format )
143
+ .propertyMappers ( propertyMappers )
144
+ .build ();
145
+ new AspectModelAasGenerator ( aspect , generationConfig ).generate ( nameMapper );
90
146
}
91
147
92
148
/**
@@ -98,27 +154,16 @@ private ThrowingBiConsumer<Environment, OutputStream, Throwable> serializer( fin
98
154
* @param aspectData the JSON data corresponding to the Aspect, may be null
99
155
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
100
156
* @throws AasGenerationException in case the generation can not properly be executed
157
+ * @deprecated Use {@link #AspectModelAasGenerator(Aspect, AasGenerationConfig)} and {@link #generate(Function)} instead
101
158
*/
159
+ @ Deprecated ( forRemoval = true )
102
160
public void generate ( final AasFileFormat format , final Aspect aspect , @ Nullable final JsonNode aspectData ,
103
161
final Function <String , OutputStream > nameMapper ) {
104
- try ( final OutputStream output = nameMapper .apply ( aspect .getName () ) ) {
105
- final AspectModelAasVisitor visitor = new AspectModelAasVisitor ().withPropertyMapper ( new LangStringPropertyMapper () );
106
- propertyMappers .forEach ( visitor ::withPropertyMapper );
107
- final Context context ;
108
- if ( aspectData != null ) {
109
- final Submodel submodel = new DefaultSubmodel .Builder ().build ();
110
- final Environment inputEnvironment = new DefaultEnvironment .Builder ().submodels ( List .of ( submodel ) ).build ();
111
- context = new Context ( inputEnvironment , submodel );
112
- context .setEnvironment ( inputEnvironment );
113
- context .setAspectData ( aspectData );
114
- } else {
115
- context = null ;
116
- }
117
-
118
- final Environment environment = visitor .visitAspect ( aspect , context );
119
- serializer ( format ).accept ( environment , output );
120
- } catch ( final Throwable exception ) {
121
- throw new AasGenerationException ( exception );
122
- }
162
+ final AasGenerationConfig generationConfig = AasGenerationConfigBuilder .builder ()
163
+ .format ( format )
164
+ .aspectData ( aspectData )
165
+ .propertyMappers ( propertyMappers )
166
+ .build ();
167
+ new AspectModelAasGenerator ( aspect , generationConfig ).generate ( nameMapper );
123
168
}
124
169
}
0 commit comments