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

Commit 9879b22

Browse files
Merge pull request #53 from admin-shell-io/bugfix/serialization
Bugfix/serialization
2 parents e31c54b + d14bc2e commit 9879b22

File tree

13 files changed

+2325
-101
lines changed

13 files changed

+2325
-101
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2626
import io.adminshell.aas.v3.dataformat.mapping.MappingProvider;
2727
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
28+
import io.adminshell.aas.v3.model.AssetInformation;
2829
import io.adminshell.aas.v3.model.Identifiable;
2930
import io.adminshell.aas.v3.model.MultiLanguageProperty;
3031
import io.adminshell.aas.v3.model.Referable;
3132
import io.adminshell.aas.v3.model.Qualifiable;
3233

33-
3434
import java.util.List;
3535

3636
/**
@@ -64,8 +64,8 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
6464
mappingProvider.register(new LangStringCollectionMapper());
6565
mappingProvider.register(new RelationshipElementMapper());
6666
mappingProvider.register(new ReferenceElementMapper());
67-
mappingProvider.register(new ReferableMapper<Referable>());
68-
mappingProvider.register(new IdentifiableMapper<Identifiable>());
67+
mappingProvider.register(new ReferableMapper<>());
68+
mappingProvider.register(new IdentifiableMapper<>());
6969
mappingProvider.register(new ConstraintCollectionMapper());
7070
mappingProvider.register(new QualifierMapper());
7171
mappingProvider.register(new OperationCollectionMapper());
@@ -77,12 +77,15 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
7777
mappingProvider.register(new EmbeddedDataSpecificationCollectionMapper());
7878
mappingProvider.register(new DataSpecificationIEC61360Mapper());
7979
mappingProvider.register(new EnumDataTypeIEC61360Mapper());
80+
mappingProvider.register(new IdentifierKeyValuePairCollectionMapper());
81+
8082
AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();
8183

8284
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();
8385
propertyNamingStrategy.registerCustomNaming(Referable.class, "descriptions", "description");
8486
propertyNamingStrategy.registerCustomNaming(MultiLanguageProperty.class, "values", "value");
85-
propertyNamingStrategy.registerCustomNaming(Qualifiable.class, "qualifiers", "qualifier","qualifier");
87+
propertyNamingStrategy.registerCustomNaming(Qualifiable.class, "qualifiers", "qualifier", "qualifier");
88+
propertyNamingStrategy.registerCustomNaming(AssetInformation.class, "specificAssetIds", "specificAssetId");
8689
MappingContext context = new MappingContext(mappingProvider, classNamingStrategy, propertyNamingStrategy, config.getTypeFactory());
8790
context.setDocumentInfo(AmlDocumentInfo.fromFile(aml));
8891
AssetAdministrationShellEnvironment result = context.map(AssetAdministrationShellEnvironment.class, parser);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public AssetAdministrationShellEnvironment map(AmlParser parser, MappingContext
7373
CAEXFile.SystemUnitClassLib systemUnitClassLib = parser.getContent().getSystemUnitClassLib().stream()
7474
.filter(x -> x.getName().equalsIgnoreCase(ASSET_ADMINISTRATION_SHELL_SYSTEM_UNIT_CLASSES))
7575
.findFirst()
76-
.orElse(null);
76+
.orElse(CAEXFile.SystemUnitClassLib.builder().build());
7777

7878
List<SystemUnitFamilyType> systemUnitFamilyTypeShells = systemUnitClassLib.getSystemUnitClass().stream()
7979
.filter(x ->x.getSupportedRoleClass().get(0).getRefRoleClassPath().equalsIgnoreCase(ROLE_CLASS_LIB_ASSET_ADMINISTRATION_SHELL))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.adminshell.aas.v3.dataformat.aml.deserialization.mappers;
17+
18+
import io.adminshell.aas.v3.dataformat.aml.deserialization.AmlParser;
19+
import io.adminshell.aas.v3.dataformat.aml.deserialization.DefaultMapper;
20+
import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext;
21+
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType;
22+
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXObject;
23+
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
24+
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
25+
import io.adminshell.aas.v3.model.IdentifierKeyValuePair;
26+
import java.beans.PropertyDescriptor;
27+
28+
import java.lang.reflect.InvocationTargetException;
29+
import java.util.ArrayList;
30+
import java.util.Collection;
31+
import java.util.List;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
34+
35+
public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> {
36+
37+
@Override
38+
protected Collection mapCollectionValueProperty(AmlParser parser, MappingContext context) throws MappingException {
39+
Collection result = new ArrayList<>();
40+
CAEXObject current = parser.getCurrent();
41+
AttributeType parent = findAttribute(current, context.getProperty(), context);
42+
if (parent == null) {
43+
return result;
44+
}
45+
List<AttributeType> attributes = findAttributes(parent, x -> x.getName().startsWith("specificAssetId"));
46+
for (AttributeType attribute : attributes) {
47+
parser.setCurrent(attribute);
48+
try {
49+
IdentifierKeyValuePair element = context.getTypeFactory().newInstance(IdentifierKeyValuePair.class);
50+
for (PropertyDescriptor property : AasUtils.getAasProperties(IdentifierKeyValuePair.class)) {
51+
Object propertyValue = context
52+
.with(element)
53+
.with(property)
54+
.withoutType()
55+
.map(property.getReadMethod().getGenericReturnType(), parser);
56+
if (propertyValue != null) {
57+
try {
58+
property.getWriteMethod().invoke(element, propertyValue);
59+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
60+
throw new MappingException(String.format("error setting property value for property %s", property.getName()), ex);
61+
}
62+
}
63+
}
64+
parser.setCurrent(attribute);
65+
result.add(element);
66+
} catch (NoSuchMethodException ex) {
67+
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
68+
} catch (InstantiationException ex) {
69+
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
70+
} catch (IllegalAccessException ex) {
71+
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
72+
} catch (IllegalArgumentException ex) {
73+
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
74+
} catch (InvocationTargetException ex) {
75+
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
76+
}
77+
}
78+
parser.setCurrent(parent);
79+
return result;
80+
}
81+
82+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.ViewMapper;
3939
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXFile;
4040
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.ConceptDescriptionMapper;
41+
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.IdentifierKeyValuePairCollectionMapper;
4142
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.PropertyMapper;
4243
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.RangeMapper;
4344
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.ReferenceMapper;
@@ -99,6 +100,7 @@ public CAEXFile map(AssetAdministrationShellEnvironment env, AmlSerializationCon
99100
mappingProvider.register(new PropertyMapper());
100101
mappingProvider.register(new RangeMapper());
101102
mappingProvider.register(new ConceptDescriptionMapper());
103+
mappingProvider.register(new IdentifierKeyValuePairCollectionMapper());
102104
MappingContext context = new MappingContext(
103105
mappingProvider,
104106
classNamingStrategy,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.adminshell.aas.v3.dataformat.aml.serialization.mappers;
17+
18+
import io.adminshell.aas.v3.dataformat.aml.serialization.DefaultMapper;
19+
import io.adminshell.aas.v3.dataformat.aml.serialization.AmlGenerator;
20+
import io.adminshell.aas.v3.dataformat.aml.serialization.MappingContext;
21+
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType;
22+
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
23+
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
24+
import io.adminshell.aas.v3.model.AssetInformation;
25+
import io.adminshell.aas.v3.model.IdentifierKeyValuePair;
26+
import java.beans.PropertyDescriptor;
27+
import java.lang.reflect.InvocationTargetException;
28+
import java.util.ArrayList;
29+
import java.util.Collection;
30+
import java.util.List;
31+
32+
public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> {
33+
34+
private static final String ATTRIBUTE_NAME = "specificAssetId";
35+
36+
@Override
37+
public void map(Collection<IdentifierKeyValuePair> value, AmlGenerator generator, MappingContext context) throws MappingException {
38+
if (value == null || context == null || value.isEmpty()) {
39+
return;
40+
}
41+
AttributeType.Builder wrapperBuilder = AttributeType.builder()
42+
.withName(ATTRIBUTE_NAME)
43+
.withRefSemantic(generator.refSemantic(AssetInformation.class, ATTRIBUTE_NAME));
44+
List<AttributeType> attributes = new ArrayList<>();
45+
for (IdentifierKeyValuePair element : value) {
46+
AttributeType.Builder builder = AttributeType.builder()
47+
.withName(ATTRIBUTE_NAME + (value.size() > 1 ? "_" + (attributes.size() + 1) : ""));
48+
for (PropertyDescriptor property : AasUtils.getAasProperties(element.getClass())) {
49+
if (!skipProperty(property)) {
50+
context.with(property)
51+
.map(property.getReadMethod().getGenericReturnType(),
52+
getElemenetPropertyValue(element, property, context),
53+
generator.with(builder));
54+
}
55+
}
56+
attributes.add(builder.build());
57+
}
58+
attributes.forEach(x -> wrapperBuilder.addAttribute(x));
59+
generator.add(wrapperBuilder.build());
60+
}
61+
62+
protected Object getElemenetPropertyValue(IdentifierKeyValuePair elemenet, PropertyDescriptor property, MappingContext context) throws MappingException {
63+
try {
64+
return property.getReadMethod().invoke(elemenet);
65+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
66+
throw new MappingException("failed to get property value for property " + property.getName(), ex);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)