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

Commit 7640220

Browse files
Working on deserialization, deserialize ValueTypes
1 parent dd04871 commit 7640220

5 files changed

Lines changed: 108 additions & 10 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
7272
mappingProvider.register(new FileMapper());
7373
mappingProvider.register(new RangeMapper());
7474
mappingProvider.register(new ViewMapper());
75+
mappingProvider.register(new PropertyMapper());
7576
AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();
7677

7778
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.beans.Introspector;
2828
import java.beans.PropertyDescriptor;
2929
import java.lang.reflect.InvocationTargetException;
30+
import java.lang.reflect.Method;
3031
import java.util.ArrayList;
3132
import java.util.Arrays;
3233
import java.util.Collection;
@@ -346,6 +347,24 @@ protected String getDataTypeFromAttribute(AttributeType attributeType){
346347
return attributeDataType.substring(attributeDataType.lastIndexOf(":") + 1);
347348
}
348349

350+
protected void setValueDataTypeFromAttributeDataType(AmlParser parser, Object parent, String attributeNameWithAttributeDataType, Class aasClazz) throws MappingException {
351+
if(parser == null || parent == null)return;
352+
353+
AttributeType attributeType = findAttributes(parser.getCurrent(),
354+
x -> x.getName().equalsIgnoreCase(attributeNameWithAttributeDataType)).stream().findFirst().orElse(null);
355+
if(attributeType != null){
356+
try {
357+
String dataType = getDataTypeFromAttribute(attributeType);
358+
List<Method> methods = List.of(aasClazz.getMethods());
359+
Method method = methods.stream().filter(x->x.getName().contains("setValueType")).findFirst().orElse(null);
360+
if(method == null)return;
361+
method.invoke(parent,dataType);
362+
} catch (InvocationTargetException | IllegalAccessException ex) {
363+
throw new MappingException(String.format("error setting value type for property with ID=%s and Name=%s", parser.getCurrent().getID(), parser.getCurrent().getName()), ex);
364+
}
365+
}
366+
}
367+
349368
protected boolean skipProperty(PropertyDescriptor property) {
350369
return ignoredProperties.contains(property.getName());
351370
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.Mapper;
21+
import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext;
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.Property;
25+
import io.adminshell.aas.v3.model.Referable;
26+
27+
import java.beans.PropertyDescriptor;
28+
29+
/**
30+
*
31+
*/
32+
public class PropertyMapper extends DefaultMapper<Property> {
33+
34+
public static final String VALUE_ATTRIBUTE_NAME = "value";
35+
36+
protected static PropertyDescriptor PROPERTY_VALUE_TYPE = AasUtils.getProperty(Property.class, "valueType");
37+
38+
public PropertyMapper() {
39+
super(PROPERTY_VALUE_TYPE.getName());
40+
}
41+
42+
@Override
43+
protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException {
44+
if (parent == null || context == null) {
45+
return;
46+
}
47+
48+
setValueDataTypeFromAttributeDataType(parser,parent,VALUE_ATTRIBUTE_NAME,Property.class);
49+
50+
Mapper<?> mapper = context.getMappingProvider().getMapper(Referable.class);
51+
mapper.map(parser,context);
52+
53+
super.mapProperties(parent, parser, context);
54+
}
55+
56+
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,48 @@
1717

1818
import io.adminshell.aas.v3.dataformat.aml.deserialization.AmlParser;
1919
import io.adminshell.aas.v3.dataformat.aml.deserialization.DefaultMapper;
20+
import io.adminshell.aas.v3.dataformat.aml.deserialization.Mapper;
2021
import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext;
2122
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType;
2223

24+
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
2325
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2426
import io.adminshell.aas.v3.model.*;
2527
import io.adminshell.aas.v3.model.impl.DefaultQualifier;
2628

29+
import java.beans.PropertyDescriptor;
30+
import java.lang.reflect.InvocationTargetException;
31+
import java.lang.reflect.Method;
32+
2733

2834
/**
2935
*
3036
*/
3137
public class QualifierMapper extends DefaultMapper<Qualifier> {
3238

39+
protected static PropertyDescriptor PROPERTY_VALUE_TYPE = AasUtils.getProperty(Property.class, "valueType");
40+
41+
public static final String VALUE_ATTRIBUTE_NAME = "value";
42+
43+
public QualifierMapper() {
44+
super(PROPERTY_VALUE_TYPE.getName());
45+
}
46+
47+
@Override
48+
protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException {
49+
if (parent == null || context == null) {
50+
return;
51+
}
52+
setValueDataTypeFromAttributeDataType(parser,parent,VALUE_ATTRIBUTE_NAME,Qualifier.class);
53+
super.mapProperties(parent, parser, context);
54+
}
55+
56+
3357
@Override
3458
protected Qualifier fromAttribute(AmlParser parser, AttributeType attribute, MappingContext context) throws MappingException {
3559
if (parser == null || attribute == null || context == null) {
3660
return null;
3761
}
38-
3962
Qualifier result = (Qualifier) newInstance(DefaultQualifier.class, context);
4063
mapProperties(result, parser, context);
4164
return result;

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ protected void mapProperties(Object parent, AmlParser parser, MappingContext con
5151
AttributeType attributeTypeMax = findAttributes(parser.getCurrent(),
5252
x -> x.getName().equalsIgnoreCase(MAX_ATTRIBUTE_NAME)).stream().findFirst().orElse(null);
5353

54-
if (attributeTypeMax == null || attributeTypeMin == null)
55-
throw new MappingException(String.format("no min/max attributes are found in range %s %s", parser.getCurrent().getID(), parser.getCurrent().getName()));
54+
if (attributeTypeMax != null || attributeTypeMin != null){
55+
String dataTypeMin = getDataTypeFromAttribute(attributeTypeMin);
56+
String dataTypeMax = getDataTypeFromAttribute(attributeTypeMax);
5657

57-
String dataTypeMin = getDataTypeFromAttribute(attributeTypeMin);
58-
String dataTypeMax = getDataTypeFromAttribute(attributeTypeMax);
58+
//TODO: is that a constraint?
59+
if (!dataTypeMax.equalsIgnoreCase(dataTypeMin))
60+
throw new MappingException(String.format("min/max attributes in range %s %s has different attribute datatypes", parser.getCurrent().getID(), parser.getCurrent().getName()));
5961

60-
//TODO: is that a constraint?
61-
if (!dataTypeMax.equalsIgnoreCase(dataTypeMin))
62-
throw new MappingException(String.format("min/max attributes in range %s %s has different attribute datatypes", parser.getCurrent().getID(), parser.getCurrent().getName()));
63-
64-
((Range) parent).setValueType(dataTypeMax);
62+
((Range) parent).setValueType(dataTypeMax);
63+
}
6564

6665
super.mapProperties(parent, parser, context);
6766
}

0 commit comments

Comments
 (0)