Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 4b6fbba

Browse files
committed
-refactoring
- added unit test to AML serializer
1 parent 013c983 commit 4b6fbba

27 files changed

Lines changed: 3953 additions & 19043 deletions

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/AmlDeserializationConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.adminshell.aas.v3.dataformat.aml.deserialization.AasTypeFactory;
1919

2020
public class AmlDeserializationConfig {
21+
2122
public static Builder builder() {
2223
return new Builder();
2324
}
@@ -27,6 +28,7 @@ public static Builder builder() {
2728
private AmlDeserializationConfig(AasTypeFactory typeFactory) {
2829
this.typeFactory = typeFactory;
2930
}
31+
3032
public AasTypeFactory getTypeFactory() {
3133
return typeFactory;
3234
}

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/deserialization/Aml2AasMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public Aml2AasMapper(AmlDeserializationConfig config) {
3636
}
3737

3838
/**
39-
* Maps an AML file (represented by a CAEXFile) to an AssetAdministrationShellEnvironment
39+
* Maps an AML file (represented by a CAEXFile) to an
40+
* AssetAdministrationShellEnvironment
41+
*
4042
* @param aml the AML source file to map
4143
* @return AssetAdministrationShellEnvironment representation
4244
* @throws MappingException if the mapping fails

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/deserialization/AmlParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public CAEXObject getCurrent() {
5353
}
5454

5555
/**
56-
* Sets the CAEXObject that currently is being processed.
56+
* Sets the CAEXObject that currently is being processed.
5757
*
5858
* @param current the object currently being processed
5959
*/

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/deserialization/Mapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package io.adminshell.aas.v3.dataformat.aml.deserialization;
1717

1818
import io.adminshell.aas.v3.dataformat.mapping.TargetBasedMapper;
19+
1920
/**
2021
* Utility interface for all AML to AAS mappers
22+
*
2123
* @param <T> type to be deserialized
2224
*/
2325
public interface Mapper<T> extends TargetBasedMapper<T, AmlParser, MappingContext> {

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/AasToAmlMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public class AasToAmlMapper {
5656
private static final org.slf4j.Logger log = LoggerFactory.getLogger(AasToAmlMapper.class);
5757

5858
/**
59-
* Maps an AssetAdministrationShellEnvironment to an AML file (represented by a CAEXFile)
59+
* Maps an AssetAdministrationShellEnvironment to an AML file (represented
60+
* by a CAEXFile)
61+
*
6062
* @param env the AssetAdministrationShellEnvironment to map
6163
* @param config the serialization conig
6264
* @return an AML representation of the AssetAdministrationShellEnvironment

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/AmlGenerator.java

Lines changed: 140 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
3838

39+
/**
40+
* Immutable class supporting creation of AML file by encalsulating different
41+
* common tasks and functionality, e.g. id generation and caching. To create new
42+
* instances with modified properties use provided with...(...) functions.
43+
*/
3944
public class AmlGenerator {
4045

4146
private static final Logger log = LoggerFactory.getLogger(AmlGenerator.class);
4247
public static final String REFERABLE_REFERENCE_INTERFACE_CLASS = "ReferableReference";
4348
private static final String DEFAULT_REF_SEMANTIC_PREFIX = "AAS";
49+
50+
public static Builder builder() {
51+
return new Builder();
52+
}
4453
private final String refSemanticPrefix;
4554
private final CAEXObject.Builder current;
4655
private final AmlDocumentInfo documentInfo;
@@ -66,6 +75,12 @@ private AmlGenerator(
6675
this.reference = reference;
6776
}
6877

78+
/**
79+
* Creates a new instance with new current CAEXObject.Builder
80+
*
81+
* @param current new current CAEXObject.Builder
82+
* @return new immutable instance with CAEXObject.Builder
83+
*/
6984
public AmlGenerator with(CAEXObject.Builder current) {
7085
return new AmlGenerator(documentInfo,
7186
idGenerator,
@@ -76,6 +91,12 @@ public AmlGenerator with(CAEXObject.Builder current) {
7691
reference);
7792
}
7893

94+
/**
95+
* Creates a new instance with reference to new parent object in AAS
96+
*
97+
* @param parent reference to new parent element
98+
* @return new immutable instance with new parent
99+
*/
79100
public AmlGenerator with(Referable parent) {
80101
return new AmlGenerator(
81102
documentInfo,
@@ -87,6 +108,12 @@ public AmlGenerator with(Referable parent) {
87108
AasUtils.toReference(reference, parent));
88109
}
89110

111+
/**
112+
* Creates a new instance with given prefix for refSemantic tags
113+
*
114+
* @param value new prefix for refSemantic tags
115+
* @return new immutable instance given refSemantic prefix
116+
*/
90117
public AmlGenerator withRefSemanticPrefix(String value) {
91118
return new AmlGenerator(
92119
documentInfo,
@@ -98,62 +125,36 @@ public AmlGenerator withRefSemanticPrefix(String value) {
98125
reference);
99126
}
100127

128+
/**
129+
* Adds additional information to the AML file
130+
*
131+
* @param additionalInformation additional information to add
132+
*/
101133
public void addAdditionalInformation(List<Object> additionalInformation) {
102134
fileBuilder.addAdditionalInformation(additionalInformation);
103135
}
104136

105-
public static Builder builder() {
106-
return new Builder();
107-
}
108-
109-
public static class Builder {
110-
111-
private String refSemanticPrefix = DEFAULT_REF_SEMANTIC_PREFIX;
112-
private CAEXObject.Builder current;
113-
private CAEXFile.Builder fileBuilder = CAEXFile.builder();
114-
private AmlDocumentInfo documentInfo = new AmlDocumentInfo();
115-
private IdGenerator idGenerator = new UuidGenerator();
116-
117-
public AmlGenerator build() {
118-
return new AmlGenerator(documentInfo,
119-
idGenerator,
120-
new HashMap<>(),
121-
refSemanticPrefix,
122-
fileBuilder,
123-
current,
124-
new DefaultReference.Builder().build());
125-
}
126-
127-
public Builder refSemanticPrefix(String value) {
128-
this.refSemanticPrefix = value;
129-
return this;
130-
}
131-
132-
public Builder current(CAEXObject.Builder value) {
133-
this.current = value;
134-
return this;
135-
}
136-
137-
public Builder idGenerator(IdGenerator value) {
138-
this.idGenerator = value;
139-
return this;
140-
}
141-
142-
public Builder file(CAEXFile.Builder value) {
143-
this.fileBuilder = value;
144-
return this;
145-
}
146-
147-
public Builder documentInfo(AmlDocumentInfo value) {
148-
this.documentInfo = value;
149-
return this;
150-
}
151-
}
152-
137+
/**
138+
* Generates a new ID without caching it.
139+
*
140+
* @return a new ID
141+
*/
153142
public String newId() {
154143
return idGenerator.next();
155144
}
156145

146+
/**
147+
* Gets the ID for given object. If obj is an instance of Referable it is
148+
* first converted to reference pointing to that element. In that case, or
149+
* if the obj is already an instance of Reference, an already existing ID
150+
* will be returned if available, otherwise a new one is created and cached.
151+
* If obj is neither an instance of Reference nor Referable, a new ID is
152+
* generated and not cached.
153+
*
154+
* @param obj object to generate an ID for
155+
* @return an ID that might or might not be cached (depending of type of
156+
* obj)
157+
*/
157158
public String getId(Object obj) {
158159
if (obj == null) {
159160
return idGenerator.next();
@@ -177,6 +178,11 @@ public String getId(Object obj) {
177178
return result;
178179
}
179180

181+
/**
182+
* Adds an object to the current element of the AML document
183+
*
184+
* @param caexObject the object to add
185+
*/
180186
public void add(CAEXObject caexObject) {
181187
if (caexObject == null) {
182188
return;
@@ -190,6 +196,11 @@ public void add(CAEXObject caexObject) {
190196
}
191197
}
192198

199+
/**
200+
* Adds an attribute to the current element of the AML document
201+
*
202+
* @param attribute the attribute to add
203+
*/
193204
public void addAttribute(AttributeType attribute) {
194205
if (attribute == null) {
195206
return;
@@ -221,14 +232,26 @@ private void addExternalInterface(RoleClassType.ExternalInterface externalInterf
221232
}
222233
}
223234

224-
public void addExternalInterfaceForReference(MappingContext context) {
235+
/**
236+
* Adds an external interface with name and roleCall ReferableReference to
237+
* the current element of the AML document
238+
*/
239+
public void addExternalInterfaceForReference() {
225240
addExternalInterface(RoleClassType.ExternalInterface.builder()
226241
.withID(newId())
227242
.withName(REFERABLE_REFERENCE_INTERFACE_CLASS)
228243
.withRefBaseClassPath(documentInfo.getAssetAdministrationShellInterfaceClassLib() + "/" + REFERABLE_REFERENCE_INTERFACE_CLASS)
229244
.build(), false);
230245
}
231246

247+
/**
248+
* Adds an internal link with given namen pointing from source to target to
249+
* the current element of the AML document
250+
*
251+
* @param name name if the link
252+
* @param source source element of the link
253+
* @param target reference pointing to the target element
254+
*/
232255
public void addInternalLink(String name, Referable source, Reference target) {
233256
if (InternalElementType.Builder.class.isAssignableFrom(current.getClass())) {
234257
InternalElementType.Builder builder = (InternalElementType.Builder) current;
@@ -243,7 +266,15 @@ public void addInternalLink(String name, Referable source, Reference target) {
243266
}
244267
}
245268

246-
public void addExternalInterfaceForUnresolvableReference(String name, Reference reference, MappingContext context) {
269+
/**
270+
* Adds an external interface of type ReferableReference with given name
271+
* with unresolvabled reference as value to the current element of the AML
272+
* document
273+
*
274+
* @param name name of the interface
275+
* @param reference unresolvable target reference
276+
*/
277+
public void addExternalInterfaceForUnresolvableReference(String name, Reference reference) {
247278
addExternalInterface(RoleClassType.ExternalInterface.builder()
248279
.withID(idGenerator.next())
249280
.withName(name)
@@ -258,6 +289,12 @@ public void addExternalInterfaceForUnresolvableReference(String name, Reference
258289
false);
259290
}
260291

292+
/**
293+
* Clears the ID cache. This should be used with caution as it may cause
294+
* reference resolution mechanism to fail. Typically it will only be called
295+
* after processing the InstanceHierarchy elements and before creating
296+
* custom SystemUnitClasses
297+
*/
261298
public void clearIdCache() {
262299
idCache.clear();
263300
}
@@ -344,23 +381,66 @@ public AttributeType.RefSemantic refSemantic(Class<?> type, String propertyName)
344381
.build();
345382
}
346383

384+
/**
385+
* Creates a refSemantic object for a given property
386+
*
387+
* @param property the property
388+
* @return the generated refSemantic object
389+
*/
347390
public AttributeType.RefSemantic refSemantic(PropertyDescriptor property) {
348391
return refSemantic(property.getReadMethod().getDeclaringClass(), property.getName());
349392
}
350393

351-
public String refBaseSystemUnitPath(Object value, MappingContext context) {
352-
return documentInfo.getAssetAdministrationShellSystemUnitClassLib()
353-
+ "/" + context.getInternalElementNamingStrategy().getName(
354-
value.getClass(),
355-
value,
356-
null);
357-
}
358-
394+
/**
395+
* Gets to AML document info
396+
*
397+
* @return the AML document info
398+
*/
359399
public AmlDocumentInfo getDocumentInfo() {
360400
return documentInfo;
361401
}
362402

363-
public Reference getReference() {
364-
return reference;
403+
public static class Builder {
404+
405+
private String refSemanticPrefix = DEFAULT_REF_SEMANTIC_PREFIX;
406+
private CAEXObject.Builder current;
407+
private CAEXFile.Builder fileBuilder = CAEXFile.builder();
408+
private AmlDocumentInfo documentInfo = new AmlDocumentInfo();
409+
private IdGenerator idGenerator = new UuidGenerator();
410+
411+
public AmlGenerator build() {
412+
return new AmlGenerator(documentInfo,
413+
idGenerator,
414+
new HashMap<>(),
415+
refSemanticPrefix,
416+
fileBuilder,
417+
current,
418+
new DefaultReference.Builder().build());
419+
}
420+
421+
public Builder refSemanticPrefix(String value) {
422+
this.refSemanticPrefix = value;
423+
return this;
424+
}
425+
426+
public Builder current(CAEXObject.Builder value) {
427+
this.current = value;
428+
return this;
429+
}
430+
431+
public Builder idGenerator(IdGenerator value) {
432+
this.idGenerator = value;
433+
return this;
434+
}
435+
436+
public Builder file(CAEXFile.Builder value) {
437+
this.fileBuilder = value;
438+
return this;
439+
}
440+
441+
public Builder documentInfo(AmlDocumentInfo value) {
442+
this.documentInfo = value;
443+
return this;
444+
}
365445
}
366446
}

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/DefaultMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import io.adminshell.aas.v3.dataformat.core.ReflectionHelper;
2121
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
2222
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
23+
import io.adminshell.aas.v3.model.MultiLanguageProperty;
2324
import io.adminshell.aas.v3.model.Referable;
2425
import java.beans.PropertyDescriptor;
2526
import java.lang.reflect.InvocationTargetException;
2627
import java.util.Arrays;
2728
import java.util.HashSet;
29+
import java.util.List;
2830
import java.util.Set;
31+
import java.util.stream.Collectors;
2932

3033
public class DefaultMapper<T> implements Mapper<T> {
3134

@@ -137,6 +140,7 @@ protected boolean skipProperty(PropertyDescriptor property) {
137140
}
138141

139142
protected void mapProperties(T value, AmlGenerator generator, MappingContext context) throws MappingException {
143+
List<String> collect = AasUtils.getAasProperties(value.getClass()).stream().map(x -> x.getName()).collect(Collectors.toList());
140144
for (PropertyDescriptor property : AasUtils.getAasProperties(value.getClass())) {
141145
if (!skipProperty(property)) {
142146
context.with(property)

0 commit comments

Comments
 (0)