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

Commit 294742a

Browse files
Working on deserialization, add deserialization of concept descriptions
1 parent 7640220 commit 294742a

11 files changed

Lines changed: 407 additions & 19 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
7373
mappingProvider.register(new RangeMapper());
7474
mappingProvider.register(new ViewMapper());
7575
mappingProvider.register(new PropertyMapper());
76+
mappingProvider.register(new ConceptDescriptionMapper());
77+
mappingProvider.register(new EmbeddedDataSpecificationCollectionMapper());
78+
mappingProvider.register(new DataSpecificationIEC61360Mapper());
79+
mappingProvider.register(new EnumDataTypeIEC61360Mapper());
7680
AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();
7781

7882
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class AmlParser {
4242
private final Map<String, Reference> idToReference = new HashMap<>();
4343
private final Map<ObjectLocationByProperty, List<String>> idsToResolve = new HashMap<>();
4444

45+
public static final String DEFAULT_REFSEMANTIC_PREFIX = "AAS";
46+
private String refSemanticPrefix = DEFAULT_REFSEMANTIC_PREFIX;
47+
4548
public AmlParser(CAEXFile content) {
4649
this.content = content;
4750
}
@@ -177,4 +180,31 @@ private ObjectLocationByProperty(Object parent, PropertyDescriptor property) {
177180
public Map<String, Reference> getIdToReference() {
178181
return idToReference;
179182
}
183+
184+
/**
185+
* Gets the prefix for the RefSemantic
186+
*
187+
* @return
188+
*/
189+
public String getRefSemanticPrefix() {
190+
return refSemanticPrefix;
191+
}
192+
193+
/**
194+
* Sets the prefix for the RefSemantic
195+
*
196+
* @param refSemanticPrefix
197+
*/
198+
public void setRefSemanticPrefix(String refSemanticPrefix) {
199+
this.refSemanticPrefix = refSemanticPrefix;
200+
}
201+
202+
/**
203+
* Sets the prefix for the RefSemantic to the default value
204+
*
205+
*/
206+
public void setRefSemanticPrefixToDefault() {
207+
this.refSemanticPrefix = DEFAULT_REFSEMANTIC_PREFIX;
208+
}
209+
180210
}

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected Class<?> typeFromAttribute(AttributeType attribute) throws MappingExce
239239
// alternative way to discover property but more expensive as all properties are considered and not only those defined on class
240240
// Optional<PropertyDescriptor> property = AasUtils.getAasProperties(type.get()).stream().filter(x -> x.getName().equals(type)).findFirst();
241241
try {
242-
//TODO: equals instead of contains. Use old name of PropertyNamingStrategy
242+
//TODO: use equals instead of contains. Use old name of PropertyNamingStrategy
243243
Optional<PropertyDescriptor> property = Stream.of(Introspector.getBeanInfo(type).getPropertyDescriptors())
244244
.filter(x -> x.getName().contains(attributePathElements[1])).findFirst();
245245
if (property.isPresent()) {
@@ -369,8 +369,22 @@ protected boolean skipProperty(PropertyDescriptor property) {
369369
return ignoredProperties.contains(property.getName());
370370
}
371371

372+
protected String findInternalLinkTarget(InternalElementType internalElement, PropertyDescriptor property) throws MappingException {
373+
Optional<SystemUnitClassType.InternalLink> internalLink = internalElement.getInternalLink().stream()
374+
.filter(x -> x.getName().equals(property.getName()))
375+
.findFirst();
376+
if (internalLink.isPresent()) {
377+
return internalLink.get().getRefPartnerSideB().substring(0, internalLink.get().getRefPartnerSideB().indexOf(":"));
378+
}
379+
return null;
380+
}
381+
372382
protected AttributeType findAttribute(CAEXObject parent, PropertyDescriptor property, MappingContext context) throws MappingException {
373-
List<AttributeType> attributes = findAttributes(parent, property, context);
383+
return findAttribute(parent, property,context,AmlParser.DEFAULT_REFSEMANTIC_PREFIX);
384+
}
385+
386+
protected AttributeType findAttribute(CAEXObject parent, PropertyDescriptor property, MappingContext context, String refSemanticPrefix) throws MappingException {
387+
List<AttributeType> attributes = findAttributes(parent, property, context, refSemanticPrefix);
374388
if (attributes.isEmpty()) {
375389
return null;
376390
}
@@ -380,23 +394,18 @@ protected AttributeType findAttribute(CAEXObject parent, PropertyDescriptor prop
380394
throw new MappingException(String.format("found multiple Attribute for property %s in element with name: %s", property.getName(), parent.getName()));
381395
}
382396

383-
protected String findInternalLinkTarget(InternalElementType internalElement, PropertyDescriptor property) throws MappingException {
384-
Optional<SystemUnitClassType.InternalLink> internalLink = internalElement.getInternalLink().stream()
385-
.filter(x -> x.getName().equals(property.getName()))
386-
.findFirst();
387-
if (internalLink.isPresent()) {
388-
return internalLink.get().getRefPartnerSideB().substring(0, internalLink.get().getRefPartnerSideB().indexOf(":"));
389-
}
390-
return null;
391-
}
397+
392398

393399
// TODO not working because of missing property renaming strategy
394400
protected List<AttributeType> findAttributes(CAEXObject parent, PropertyDescriptor property, MappingContext context) {
401+
return findAttributes(parent, property,context, AmlParser.DEFAULT_REFSEMANTIC_PREFIX);
402+
}
403+
404+
protected List<AttributeType> findAttributes(CAEXObject parent, PropertyDescriptor property, MappingContext context, String refSemanticPrefix) {
395405
if (property == null) {
396406
return List.of();
397407
}
398-
399-
String refSemantic = "AAS:" + property.getReadMethod().getDeclaringClass().getSimpleName() + "/"
408+
String refSemantic = refSemanticPrefix + ":" + property.getReadMethod().getDeclaringClass().getSimpleName() + "/"
400409
+ context.getPropertyNamingStrategy().getNameForRefSemantic(property.getReadMethod().getDeclaringClass(), null, property.getName());
401410
return findAttributes(parent, x -> x.getRefSemantic().stream()
402411
.anyMatch(y -> y.getCorrespondingAttributePath().equals(refSemantic)));

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2424
import io.adminshell.aas.v3.model.AssetAdministrationShell;
2525
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
26+
import io.adminshell.aas.v3.model.ConceptDescription;
2627
import io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment;
2728
import java.util.List;
2829
import java.util.function.Predicate;
@@ -48,6 +49,20 @@ public AssetAdministrationShellEnvironment map(AmlParser parser, MappingContext
4849
Logger.getLogger(AssetAdministrationShellEnvironmentMapper.class.getName()).log(Level.SEVERE, null, ex);
4950
}
5051
});
52+
53+
List<InternalElementType> conceptDescriptions = parser.getContent().getInstanceHierarchy().stream()
54+
.flatMap(x -> x.getInternalElement().stream().filter(filterByRole(ConceptDescription.class)))
55+
.collect(Collectors.toList());
56+
conceptDescriptions.forEach(x-> {
57+
parser.setCurrent(x);
58+
try {
59+
ConceptDescription conceptDescription = context.with(result).map(ConceptDescription.class, parser);
60+
result.getConceptDescriptions().add(conceptDescription);
61+
} catch (MappingException ex){
62+
Logger.getLogger(AssetAdministrationShellEnvironmentMapper.class.getName()).log(Level.SEVERE, null, ex);
63+
}
64+
});
65+
5166
// TODO parse SystemUnitClasses
5267
// add template AAS and Submodels (only if not already present)
5368
return result;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.ConceptDescription;
26+
import io.adminshell.aas.v3.model.Identifier;
27+
import io.adminshell.aas.v3.model.Property;
28+
import io.adminshell.aas.v3.model.Reference;
29+
30+
import java.beans.PropertyDescriptor;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
/**
35+
*
36+
*/
37+
public class ConceptDescriptionMapper extends DefaultMapper<ConceptDescription> {
38+
39+
protected static PropertyDescriptor PROPERTY_ISCASEOF = AasUtils.getProperty(ConceptDescription.class, "isCaseOfs");
40+
41+
public ConceptDescriptionMapper() {
42+
super(PROPERTY_ISCASEOF.getName());
43+
}
44+
45+
@Override
46+
protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException {
47+
if (parent == null || context == null) {
48+
return;
49+
}
50+
51+
CAEXObject temp = parser.getCurrent();
52+
List<AttributeType> attributeTypeList = findAttributes(parser.getCurrent(), x->x.getName().equalsIgnoreCase("isCaseOf"));
53+
List<Reference> referenceList = new ArrayList<>();
54+
if(!attributeTypeList.isEmpty()){
55+
//TODO How to handle multiple references in isCaseOf?
56+
AttributeType attribute = attributeTypeList.get(0);
57+
parser.setCurrent(attribute);
58+
Reference reference = context.map(Reference.class, parser);
59+
parser.setCurrent(temp);
60+
referenceList.add(reference);
61+
}
62+
63+
((ConceptDescription) parent).setIsCaseOfs(referenceList);
64+
65+
super.mapProperties(parent, parser, context);
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.aml.model.caex.InterfaceClassType;
24+
import io.adminshell.aas.v3.dataformat.aml.model.caex.InternalElementType;
25+
import io.adminshell.aas.v3.dataformat.core.DataSpecificationManager;
26+
import io.adminshell.aas.v3.dataformat.core.ReflectionHelper;
27+
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
28+
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
29+
import io.adminshell.aas.v3.model.DataSpecificationContent;
30+
import io.adminshell.aas.v3.model.DataSpecificationIEC61360;
31+
import io.adminshell.aas.v3.model.DataTypeIEC61360;
32+
import io.adminshell.aas.v3.model.File;
33+
import io.adminshell.aas.v3.model.impl.DefaultDataSpecificationIEC61360;
34+
35+
import java.beans.IntrospectionException;
36+
import java.beans.Introspector;
37+
import java.beans.PropertyDescriptor;
38+
import java.lang.reflect.InvocationTargetException;
39+
import java.lang.reflect.Type;
40+
import java.util.List;
41+
import java.util.Optional;
42+
import java.util.stream.Collectors;
43+
import java.util.stream.Stream;
44+
45+
/**
46+
*
47+
*/
48+
public class DataSpecificationIEC61360Mapper extends DefaultMapper<DataSpecificationIEC61360> {
49+
50+
@Override
51+
protected Class<?> typeFromInternalElement(InternalElementType internalElement) throws MappingException {
52+
if (internalElement.getRoleRequirements() == null || internalElement.getRoleRequirements().getRefBaseRoleClassPath() == null) {
53+
throw new MappingException(String.format("missing required RoleRequirements/RefBaseRoleClassPath in InternalElement with ID %s", internalElement.getID()));
54+
}
55+
return DefaultDataSpecificationIEC61360.class;
56+
}
57+
58+
@Override
59+
protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException {
60+
if (parent == null || context == null) {
61+
return;
62+
}
63+
64+
CAEXObject current = parser.getCurrent();
65+
66+
List<AttributeType> attributeTypes = findAttributes(parser.getCurrent(), x->true);
67+
List<PropertyDescriptor> propertyDescriptors = AasUtils.getAasProperties(DefaultDataSpecificationIEC61360.class);
68+
69+
parser.setRefSemanticPrefix(DataSpecificationManager.DATA_SPECIFICATION_IEC61360_PREFIX);
70+
71+
for(AttributeType attributeType : attributeTypes){
72+
PropertyDescriptor propertyDescriptor = propertyDescriptors.stream().filter(x -> x.getName().contains(attributeType.getName())).findFirst().orElse(null);
73+
if(propertyDescriptor != null){
74+
try {
75+
Object o;
76+
Class type = propertyDescriptor.getReadMethod().getReturnType();
77+
if(DataTypeIEC61360.class.isAssignableFrom(type) ||
78+
List.class.isAssignableFrom(type)){
79+
o = context.with(propertyDescriptor).map(propertyDescriptor.getReadMethod().getGenericReturnType(),parser);
80+
} else {
81+
parser.setCurrent(attributeType);
82+
o = map(parser, context);
83+
}
84+
propertyDescriptor.getWriteMethod().invoke(parent, o);
85+
} catch (IllegalAccessException | InvocationTargetException ex) {
86+
throw new MappingException(String.format("error setting property value for property %s", propertyDescriptor.getName()), ex);
87+
}
88+
}
89+
90+
}
91+
parser.setCurrent(current);
92+
parser.setRefSemanticPrefixToDefault();
93+
94+
}
95+
96+
@Override
97+
protected Class<?> typeFromAttribute(AttributeType attribute) {
98+
List<PropertyDescriptor> propertyDescriptors = AasUtils.getAasProperties(DefaultDataSpecificationIEC61360.class);
99+
PropertyDescriptor propertyDescriptor = propertyDescriptors.stream().filter(x -> x.getName().contains(attribute.getName())).findFirst().orElse(null);
100+
101+
if(propertyDescriptor != null){
102+
return propertyDescriptor.getReadMethod().getReturnType();
103+
}
104+
return null;
105+
}
106+
107+
@Override
108+
protected boolean isAasType(Class<?> type) {
109+
boolean is_interface=false;
110+
boolean is_enum=false;
111+
is_interface = Stream.concat(ReflectionHelper.INTERFACES.stream(),
112+
ReflectionHelper.INTERFACES_WITHOUT_DEFAULT_IMPLEMENTATION.stream())
113+
.anyMatch(x -> x.equals(type));
114+
is_enum = ReflectionHelper.ENUMS.stream().anyMatch(x->x.equals(type));
115+
return is_interface || is_enum;
116+
}
117+
118+
@Override
119+
protected Object fromAttribute(AmlParser parser, AttributeType attribute, MappingContext context) throws MappingException {
120+
if (parser == null || attribute == null || context == null) {
121+
return null;
122+
}
123+
Class<?> type = typeFromAttribute(attribute);
124+
if (isAasType(type)) {
125+
return context.getMappingProvider().getMapper(type).map(parser,context);
126+
}
127+
return attribute.getValue();
128+
}
129+
130+
131+
}

0 commit comments

Comments
 (0)