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

Commit 16f4d1a

Browse files
authored
Merge pull request #26 from admin-shell-io/feature/aml-deserializer
Bugfixes AML De-/Serializer
2 parents 87440a2 + af11589 commit 16f4d1a

10 files changed

Lines changed: 137 additions & 63 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
2424
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2525
import io.adminshell.aas.v3.model.File;
26+
2627
import java.beans.PropertyDescriptor;
2728
import java.util.List;
2829

@@ -35,8 +36,8 @@ public class FileMapper extends DefaultMapper<File> {
3536
protected static PropertyDescriptor PROPERTY_MIME_TYPE = AasUtils.getProperty(File.class, "mimeType");
3637

3738
private static final String FILE_DATA_REFERENCE = "AssetAdministrationShellInterfaceClassLib/FileDataReference";
38-
private static final String MIME_TYPE_ATTRIBUTE_NAME = "MIMEType";
39-
private static final String REF_URI_ATTRIBUTE_NAME = "refUri";
39+
private static final String MIME_TYPE_ATTRIBUTE_PATH = "AAS:File/MIMEType";
40+
private static final String REF_URI_ATTRIBUTE_PATH = "AAS:File/refURI";
4041

4142
public FileMapper() {
4243
super(PROPERTY_VALUE.getName(), PROPERTY_MIME_TYPE.getName());
@@ -56,11 +57,12 @@ protected void mapProperties(Object parent, AmlParser parser, MappingContext con
5657
throw new MappingException(String.format("multiple external interfaces are found in file %s %s", parser.getCurrent().getID(), parser.getCurrent().getName()));
5758

5859
List<AttributeType> attributeTypes = externalInterfaces.get(0).getAttribute();
60+
5961
AttributeType mimeTypeAttribute = attributeTypes.stream()
60-
.filter(x -> x.getName().equalsIgnoreCase(MIME_TYPE_ATTRIBUTE_NAME))
62+
.filter(x -> x.getRefSemantic().get(0).getCorrespondingAttributePath().equalsIgnoreCase(MIME_TYPE_ATTRIBUTE_PATH))
6163
.findFirst().orElse(null);
6264
AttributeType refUriAttribute = attributeTypes.stream()
63-
.filter(x -> x.getName().equalsIgnoreCase(REF_URI_ATTRIBUTE_NAME)).findFirst().orElse(null);
65+
.filter(x -> x.getRefSemantic().get(0).getCorrespondingAttributePath().equalsIgnoreCase(REF_URI_ATTRIBUTE_PATH)).findFirst().orElse(null);
6466

6567
if (refUriAttribute != null)
6668
((File) parent).setValue(refUriAttribute.getValue() == null ? null : refUriAttribute.getValue().toString());

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
public class ViewMapper extends DefaultMapper<View> {
3333

3434
private static final String CONTAINED_ELEMENTS = "containedElements";
35-
private static final PropertyDescriptor PROPERTY_VALUE_TYPE = AasUtils.getProperty(View.class, "idShort");
36-
37-
public ViewMapper() {
38-
super(PROPERTY_VALUE_TYPE.getName());
39-
}
4035

4136
@Override
4237
protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException {
@@ -47,9 +42,6 @@ protected void mapProperties(Object parent, AmlParser parser, MappingContext con
4742
if (!InternalElementType.class.isAssignableFrom(parser.getCurrent().getClass())) return;
4843
InternalElementType internalElementType_View = (InternalElementType) parser.getCurrent();
4944

50-
//TODO remove after adjustment of serializer
51-
((View) parent).setIdShort(internalElementType_View.getName());
52-
5345
List<InternalElementType> internalElementTypeList = internalElementType_View.getInternalElement();
5446
internalElementTypeList.stream().forEach(x -> {
5547
String idToReference = x.getRefBaseSystemUnitPath();

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.lang.reflect.InvocationTargetException;
2626
import java.util.Arrays;
2727
import java.util.HashSet;
28-
import java.util.List;
2928
import java.util.Set;
30-
import java.util.stream.Collectors;
3129

3230
public class DefaultMapper<T> implements Mapper<T> {
3331

@@ -117,7 +115,9 @@ protected String getAttributeName(T value, MappingContext context) {
117115
}
118116

119117
protected AttributeType.Builder toAttribute(T value, AmlGenerator generator, MappingContext context) throws MappingException {
120-
Class<?> aasType = ReflectionHelper.getAasInterface(value.getClass());
118+
Class<?> aasType = value != null
119+
? ReflectionHelper.getAasInterface(value.getClass())
120+
: null;
121121
AttributeType.Builder builder = AttributeType.builder();
122122
if (context.getProperty() != null) {
123123
builder = builder
@@ -127,7 +127,6 @@ protected AttributeType.Builder toAttribute(T value, AmlGenerator generator, Map
127127
if (aasType != null) {
128128
mapProperties(value, generator.with(builder), context);
129129
} else {
130-
131130
builder = builder.withValue(value);
132131
}
133132
return builder;
@@ -138,7 +137,6 @@ protected boolean skipProperty(PropertyDescriptor property) {
138137
}
139138

140139
protected void mapProperties(T value, AmlGenerator generator, MappingContext context) throws MappingException {
141-
List<String> collect = AasUtils.getAasProperties(value.getClass()).stream().map(x -> x.getName()).collect(Collectors.toList());
142140
for (PropertyDescriptor property : AasUtils.getAasProperties(value.getClass())) {
143141
if (!skipProperty(property)) {
144142
context.with(property)

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,26 @@ protected InternalElementType.Builder toInternalElement(T value, AmlGenerator ge
6363
.copyOf(original)
6464
.withAttribute(untouchedAttributes);
6565
for (String property : typedProperties) {
66+
AttributeType.Builder typedAttributeBuilder;
6667
Optional<AttributeType> attributeToType = original.getAttribute().stream()
6768
.filter(x -> property.equals(x.getName()))
6869
.findFirst();
6970
if (attributeToType.isPresent()) {
70-
AttributeType.Builder typedAttributeBuilder = AttributeType.copyOf(attributeToType.get());
71-
if (valueTypeProperty.isPresent()) {
72-
try {
73-
typedAttributeBuilder = typedAttributeBuilder.withAttributeDataType(
74-
PROPERTY_VALUE_TYPE_NAMESPACE_PREFIX + valueTypeProperty.get().getReadMethod().invoke(value));
75-
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
76-
throw new MappingException(String.format("error reading property %s", PROPERTY_VALUE_TYPE_NAME));
77-
}
71+
typedAttributeBuilder = AttributeType.copyOf(attributeToType.get());
72+
} else {
73+
typedAttributeBuilder = super.toAttribute(null, generator, context.with(AasUtils.getProperty(value, property)));
74+
}
75+
Object type = null;
76+
if (valueTypeProperty.isPresent()) {
77+
try {
78+
type = valueTypeProperty.get().getReadMethod().invoke(value);
79+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
80+
throw new MappingException(String.format("error reading property %s", PROPERTY_VALUE_TYPE_NAME));
81+
}
82+
}
83+
if (attributeToType.isPresent() || type != null) {
84+
if (type != null) {
85+
typedAttributeBuilder = typedAttributeBuilder.withAttributeDataType(PROPERTY_VALUE_TYPE_NAMESPACE_PREFIX + type);
7886
}
7987
builder = builder.addAttribute(typedAttributeBuilder.build());
8088
}
@@ -95,18 +103,27 @@ protected AttributeType.Builder toAttribute(T value, AmlGenerator generator, Map
95103
.copyOf(original)
96104
.withAttribute(untouchedAttributes);
97105
for (String property : typedProperties) {
106+
AttributeType.Builder typedAttributeBuilder;
98107
Optional<AttributeType> attributeToType = original.getAttribute().stream()
99108
.filter(x -> property.equals(x.getName()))
100109
.findFirst();
101110
if (attributeToType.isPresent()) {
102-
AttributeType.Builder typedAttributeBuilder = AttributeType.copyOf(attributeToType.get());
103-
if (valueTypeProperty.isPresent()) {
104-
try {
105-
typedAttributeBuilder = typedAttributeBuilder.withAttributeDataType(
106-
PROPERTY_VALUE_TYPE_NAMESPACE_PREFIX + valueTypeProperty.get().getReadMethod().invoke(value));
107-
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
108-
throw new MappingException(String.format("error reading property %s", PROPERTY_VALUE_TYPE_NAME));
109-
}
111+
typedAttributeBuilder = AttributeType.copyOf(attributeToType.get());
112+
} else {
113+
typedAttributeBuilder = super.toAttribute(null, generator, context
114+
.with(AasUtils.getProperty(value, property)));
115+
}
116+
Object type = null;
117+
if (valueTypeProperty.isPresent()) {
118+
try {
119+
type = valueTypeProperty.get().getReadMethod().invoke(value);
120+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
121+
throw new MappingException(String.format("error reading property %s", PROPERTY_VALUE_TYPE_NAME));
122+
}
123+
}
124+
if (attributeToType.isPresent() || type != null) {
125+
if (type != null) {
126+
typedAttributeBuilder = typedAttributeBuilder.withAttributeDataType(PROPERTY_VALUE_TYPE_NAMESPACE_PREFIX + type);
110127
}
111128
builder = builder.addAttribute(typedAttributeBuilder.build());
112129
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ protected InternalElementType.Builder toInternalElement(File value, AmlGenerator
4646
.addAttribute(AttributeType.builder()
4747
.withName(ATTRIBUTE_MIMETYPE_NAME)
4848
.withAttributeDataType(ATTRIBUTE_MIMETYPE_DATATYPE)
49+
.withRefSemantic(generator.refSemantic(File.class, ATTRIBUTE_MIMETYPE_NAME))
4950
.withValue(value.getMimeType())
5051
.build())
5152
.addAttribute(AttributeType.builder()
5253
.withName(ATTRIBUTE_REFURI_NAME)
5354
.withValue(value.getValue())
5455
.withAttributeDataType(ATTRIBUTE_REFURI_DATATYPE)
56+
.withRefSemantic(generator.refSemantic(File.class, ATTRIBUTE_REFURI_NAME))
5557
.build())
5658
.build());
5759
}

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ public void map(Qualifier value, AmlGenerator generator, MappingContext context)
3030

3131
@Override
3232
protected String getAttributeName(Qualifier value, MappingContext context) {
33-
return context.getPropertyNamingStrategy().getName(
34-
Qualifier.class,
35-
value,
36-
context.getProperty().getName());
33+
if (value != null && Qualifier.class.isAssignableFrom(value.getClass())) {
34+
return context.getPropertyNamingStrategy().getName(
35+
Qualifier.class,
36+
value,
37+
context.getProperty().getName());
38+
}
39+
return super.getAttributeName(value, context);
3740
}
3841

3942
@Override
4043
protected AttributeType.RefSemantic getRefSemantic(Qualifier value, AmlGenerator generator, MappingContext context) {
41-
return generator.refSemantic(
42-
context.getProperty(),
43-
context.getPropertyNamingStrategy().getNameForRefSemantic(
44-
Qualifier.class,
45-
value,
46-
context.getProperty().getName()));
44+
if (value != null && Qualifier.class.isAssignableFrom(value.getClass())) {
45+
return generator.refSemantic(
46+
context.getProperty(),
47+
context.getPropertyNamingStrategy().getNameForRefSemantic(
48+
Qualifier.class,
49+
value,
50+
context.getProperty().getName()));
51+
}
52+
return super.getRefSemantic(value, generator, context);
4753
}
4854
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ public void map(View view, AmlGenerator generator, MappingContext context) throw
3939
if (view == null) {
4040
return;
4141
}
42-
InternalElementType.Builder builder = InternalElementType.builder();
43-
builder = builder.withName(context.getClassNamingStrategy().getName(
44-
view.getClass(),
45-
view,
46-
null))
47-
.withRoleRequirements(generator.roleRequirement(ReflectionHelper.getModelType(view.getClass())));
42+
InternalElementType.Builder builder = toInternalElement(view, generator, context);
4843
generator.with(builder).appendReferenceTargetInterfaceIfRequired(view, context);
4944
for (Reference reference : view.getContainedElements()) {
5045
Referable referable = AasUtils.resolve(reference, context.getEnvironment());

dataformat-aml/src/test/java/io/adminshell/aas/v3/dataformat/aml/deserialize/AmlDeserializerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ private void swapSubmodelIdx(AssetAdministrationShellEnvironment env, int idxSrc
7474
private void adaptSubmodels(AssetAdministrationShellEnvironment env) {
7575
//non referenced submodels are not considered in AML
7676
List<String> submodelIds = new ArrayList<>();
77-
env.getAssetAdministrationShells().stream().forEach(x -> x.getSubmodels().stream().forEach(y -> y.getKeys().stream().forEach(z ->submodelIds.add(z.getValue()))));
77+
env.getAssetAdministrationShells().stream().forEach(
78+
x -> x.getSubmodels().stream()
79+
.forEach(y -> y.getKeys().stream().forEach(z ->submodelIds.add(z.getValue()))));
7880

7981
List<Submodel> referencedSubmodels = env.getSubmodels().stream().filter(x -> submodelIds.contains(x.getIdentification().getIdentifier())).collect(Collectors.toList());
8082
env.setSubmodels(referencedSubmodels);

dataformat-aml/src/test/java/io/adminshell/aas/v3/dataformat/aml/fixtures/FullExample.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,14 @@ public class FullExample {
874874
.value(new DefaultProperty.Builder()
875875
.idShort("ExampleProperty")
876876
.value(null)
877-
//.valueType("string")
877+
.valueType("string")
878878
.build())
879879
.value(new DefaultMultiLanguageProperty.Builder()
880880
.idShort("ExampleMultiLanguageProperty")
881881
.build())
882882
.value(new DefaultRange.Builder()
883883
.idShort("ExampleRange")
884-
//.valueType("int")
884+
.valueType("int")
885885
.min(null)
886886
.max(null)
887887
.build())
@@ -1064,7 +1064,7 @@ public class FullExample {
10641064
.build())
10651065
.build())
10661066
.qualifier(new DefaultQualifier.Builder()
1067-
//.valueType("string")
1067+
.valueType("string")
10681068
.type("http://acplt.org/Qualifier/ExampleQualifier")
10691069
.build())
10701070
.value("exampleValue")
@@ -1085,7 +1085,7 @@ public class FullExample {
10851085
.build())
10861086
.build())
10871087
.qualifier(new DefaultQualifier.Builder()
1088-
//.valueType("string")
1088+
.valueType("string")
10891089
.type("http://acplt.org/Qualifier/ExampleQualifier")
10901090
.build())
10911091
.value("exampleValue")
@@ -1106,7 +1106,7 @@ public class FullExample {
11061106
.build())
11071107
.build())
11081108
.qualifier(new DefaultQualifier.Builder()
1109-
//.valueType("string")
1109+
.valueType("string")
11101110
.type("http://acplt.org/Qualifier/ExampleQualifier")
11111111
.build())
11121112
.value("exampleValue")
@@ -1182,7 +1182,7 @@ public class FullExample {
11821182
.build())
11831183
.build())
11841184
.qualifier(new DefaultQualifier.Builder()
1185-
//.valueType("string")
1185+
.valueType("string")
11861186
.type("http://acplt.org/Qualifier/ExampleQualifier")
11871187
.build())
11881188
.value("exampleValue")
@@ -1634,7 +1634,7 @@ public class FullExample {
16341634

16351635
public final static ConceptDescription CONCEPT_DESCRIPTION_1 = new DefaultConceptDescription.Builder()
16361636
.idShort("TestConceptDescription")
1637-
.description(new LangString("An example concept description for the test application", "en-us"))
1637+
.description(new LangString("An example concept description for the test application", "en-us"))
16381638
.description(new LangString("Ein Beispiel-ConceptDescription für eine Test-Anwendung", "de"))
16391639
.identification(new DefaultIdentifier.Builder()
16401640
.idType(IdentifierType.IRI)
@@ -1663,7 +1663,7 @@ public class FullExample {
16631663

16641664
public final static ConceptDescription CONCEPT_DESCRIPTION_3 = new DefaultConceptDescription.Builder()
16651665
.idShort("TestConceptDescription1")
1666-
.description(new LangString("An example concept description for the test application", "en-us"))
1666+
.description(new LangString("An example concept description for the test application", "en-us"))
16671667
.description(new LangString("Ein Beispiel-ConceptDescription für eine Test-Anwendung", "de"))
16681668
.identification(new DefaultIdentifier.Builder()
16691669
.idType(IdentifierType.IRI)

0 commit comments

Comments
 (0)