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

Commit 2bd797f

Browse files
Refactoring of AML Deserializer
1 parent 6b7a3f5 commit 2bd797f

20 files changed

+89
-53
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.adminshell.aas.v3.dataformat.aml.common;
1717

18-
import io.adminshell.aas.v3.dataformat.aml.serialization.naming.NamingStrategy;
18+
import io.adminshell.aas.v3.dataformat.aml.common.naming.NamingStrategy;
1919
import io.adminshell.aas.v3.dataformat.mapping.Mapper;
2020
import io.adminshell.aas.v3.dataformat.mapping.MappingContext;
2121
import io.adminshell.aas.v3.dataformat.mapping.MappingProvider;

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/naming/AbstractClassNamingStrategy.java renamed to dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/common/naming/AbstractClassNamingStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.adminshell.aas.v3.dataformat.aml.serialization.naming;
16+
package io.adminshell.aas.v3.dataformat.aml.common.naming;
1717

1818
import com.google.common.reflect.TypeToken;
1919
import io.adminshell.aas.v3.dataformat.core.util.MostSpecificTypeTokenComparator;

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/naming/IdClassNamingStrategy.java renamed to dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/common/naming/IdClassNamingStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.adminshell.aas.v3.dataformat.aml.serialization.naming;
16+
package io.adminshell.aas.v3.dataformat.aml.common.naming;
1717

1818
import java.util.UUID;
1919

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/naming/NamingStrategy.java renamed to dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/common/naming/NamingStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.adminshell.aas.v3.dataformat.aml.serialization.naming;
16+
package io.adminshell.aas.v3.dataformat.aml.common.naming;
1717

1818
import java.lang.reflect.Type;
1919

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/naming/NumberingClassNamingStrategy.java renamed to dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/common/naming/NumberingClassNamingStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.adminshell.aas.v3.dataformat.aml.serialization.naming;
16+
package io.adminshell.aas.v3.dataformat.aml.common.naming;
1717

1818
import io.adminshell.aas.v3.dataformat.core.ReflectionHelper;
1919
import java.util.HashMap;
@@ -43,5 +43,4 @@ protected String generateName(Object obj) {
4343
counter.put(type, counter.get(type) + 1);
4444
return type.getSimpleName() + "_" + counter.get(type);
4545
}
46-
4746
}

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/serialization/naming/PropertyNamingStrategy.java renamed to dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/common/naming/PropertyNamingStrategy.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.adminshell.aas.v3.dataformat.aml.serialization.naming;
16+
package io.adminshell.aas.v3.dataformat.aml.common.naming;
1717

1818
import com.google.common.reflect.TypeToken;
1919
import io.adminshell.aas.v3.dataformat.core.util.MostSpecificTypeTokenComparator;
20-
import io.adminshell.aas.v3.model.Qualifier;
20+
2121
import java.lang.reflect.Type;
2222
import java.util.ArrayList;
2323
import java.util.List;
@@ -32,14 +32,25 @@ public class PropertyNamingStrategy implements NamingStrategy {
3232

3333
protected class TypeSafeFunction<T> {
3434

35+
public TypeSafeFunction(Class<T> inputType, BiFunction<T, String, String> nameProvider, BiFunction<T, String, String> refSemanticProvider, String oldName, String newName) {
36+
this.inputType = TypeToken.of(inputType);
37+
this.nameProvider = nameProvider;
38+
this.refSemanticProvider = refSemanticProvider;
39+
this.oldName = oldName;
40+
this.newName = newName;
41+
}
42+
3543
public TypeSafeFunction(Class<T> inputType, BiFunction<T, String, String> nameProvider, BiFunction<T, String, String> refSemanticProvider) {
3644
this.inputType = TypeToken.of(inputType);
3745
this.nameProvider = nameProvider;
3846
this.refSemanticProvider = refSemanticProvider;
3947
}
48+
4049
TypeToken inputType;
4150
BiFunction<T, String, String> nameProvider;
4251
BiFunction<T, String, String> refSemanticProvider;
52+
String oldName;
53+
String newName;
4354
}
4455

4556
public <T> void registerCustomNaming(Class<T> type,
@@ -58,7 +69,8 @@ public void registerCustomNaming(Class<?> type,
5869
String newName) {
5970
customNamings.add(new TypeSafeFunction(type,
6071
(obj, property) -> Objects.equals(oldName, property) ? newName : null,
61-
(obj, property) -> Objects.equals(oldName, property) ? newName : null));
72+
(obj, property) -> Objects.equals(oldName, property) ? newName : null,
73+
oldName, newName));
6274
}
6375

6476
public void registerCustomNaming(Class<?> type,
@@ -67,7 +79,8 @@ public void registerCustomNaming(Class<?> type,
6779
String newRefSemantic) {
6880
customNamings.add(new TypeSafeFunction(type,
6981
(obj, property) -> Objects.equals(oldName, property) ? newName : null,
70-
(obj, property) -> Objects.equals(oldName, property) ? newRefSemantic : null));
82+
(obj, property) -> Objects.equals(oldName, property) ? newRefSemantic : null,
83+
oldName,newName));
7184
}
7285

7386
public <T> void registerCustomNaming(Class<T> type,
@@ -92,6 +105,15 @@ private List<TypeSafeFunction> getCustomNaming(Type type, String property) {
92105
.collect(Collectors.toList());
93106
}
94107

108+
public String getOldName(Type type, Object obj, String property){
109+
TypeSafeFunction typeSafeFunction = customNamings.stream()
110+
.filter(x -> x.inputType.isSupertypeOf(type) && x.newName.equalsIgnoreCase(property))
111+
.findFirst()
112+
.orElse(null);
113+
if(typeSafeFunction==null)return null;
114+
return typeSafeFunction.oldName;
115+
}
116+
95117
@Override
96118
public String getName(Type type, Object obj, String property) {
97119
for (TypeSafeFunction customNaming : getCustomNaming(type, property)) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import io.adminshell.aas.v3.dataformat.aml.AmlDeserializationConfig;
2020
import io.adminshell.aas.v3.dataformat.aml.AmlDocumentInfo;
2121
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXFile;
22-
import io.adminshell.aas.v3.dataformat.aml.serialization.naming.AbstractClassNamingStrategy;
23-
import io.adminshell.aas.v3.dataformat.aml.serialization.naming.NumberingClassNamingStrategy;
24-
import io.adminshell.aas.v3.dataformat.aml.serialization.naming.PropertyNamingStrategy;
22+
import io.adminshell.aas.v3.dataformat.aml.common.naming.AbstractClassNamingStrategy;
23+
import io.adminshell.aas.v3.dataformat.aml.common.naming.NumberingClassNamingStrategy;
24+
import io.adminshell.aas.v3.dataformat.aml.common.naming.PropertyNamingStrategy;
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;
@@ -82,12 +82,11 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
8282
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();
8383
propertyNamingStrategy.registerCustomNaming(Referable.class, "descriptions", "description");
8484
propertyNamingStrategy.registerCustomNaming(MultiLanguageProperty.class, "values", "value");
85-
//propertyNamingStrategy.registerCustomNaming(Qualifier.class, x -> "qualifier:" + x.getType() + "=" + x.getValue(), false);
8685
propertyNamingStrategy.registerCustomNaming(Qualifiable.class, "qualifiers", "qualifier","qualifier");
8786
MappingContext context = new MappingContext(mappingProvider, classNamingStrategy, propertyNamingStrategy, config.getTypeFactory());
8887
context.setDocumentInfo(AmlDocumentInfo.fromFile(aml));
8988
AssetAdministrationShellEnvironment result = context.map(AssetAdministrationShellEnvironment.class, parser);
9089
parser.resolveIdsToReferences(result);
91-
return (AssetAdministrationShellEnvironment) result;
90+
return result;
9291
}
9392
}

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

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

1818
import io.adminshell.aas.v3.dataformat.aml.model.caex.*;
19+
import io.adminshell.aas.v3.dataformat.aml.common.naming.PropertyNamingStrategy;
1920
import io.adminshell.aas.v3.dataformat.core.ReflectionHelper;
2021
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
2122
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
@@ -35,7 +36,6 @@
3536
import java.util.stream.Collectors;
3637
import java.util.stream.Stream;
3738

38-
import io.adminshell.aas.v3.model.Reference;
3939
import org.apache.xerces.dom.ElementNSImpl;
4040

4141
/**
@@ -89,7 +89,7 @@ protected Object fromAttribute(AmlParser parser, AttributeType attribute, Mappin
8989
if (parser == null || attribute == null || context == null) {
9090
return null;
9191
}
92-
Class<?> type = typeFromAttribute(attribute);
92+
Class<?> type = typeFromAttribute(attribute, context);
9393
if (isAasType(type)) {
9494
Object result = newInstance(type, context);
9595
mapProperties(result, parser, context);
@@ -234,7 +234,7 @@ protected boolean isAasType(Class<?> type) {
234234
* @throws MappingException if type information is missing or invalid or
235235
* type could not be resolved
236236
*/
237-
protected Class<?> typeFromAttribute(AttributeType attribute) throws MappingException {
237+
protected Class<?> typeFromAttribute(AttributeType attribute, MappingContext context) throws MappingException {
238238
if (attribute.getRefSemantic() == null || attribute.getRefSemantic().isEmpty()) {
239239
throw new MappingException(String.format("missing required refSemantic in attribute %s", attribute.getName()));
240240
}
@@ -262,9 +262,10 @@ protected Class<?> typeFromAttribute(AttributeType attribute) throws MappingExce
262262
// alternative way to discover property but more expensive as all properties are considered and not only those defined on class
263263
// Optional<PropertyDescriptor> property = AasUtils.getAasProperties(type.get()).stream().filter(x -> x.getName().equals(type)).findFirst();
264264
try {
265-
//TODO: use equals instead of contains. Use old name of PropertyNamingStrategy
265+
266+
String oldPropertyName = ((PropertyNamingStrategy)context.getPropertyNamingStrategy()).getOldName(type, attribute,attributePathElements[1]);
266267
Optional<PropertyDescriptor> property = Stream.of(Introspector.getBeanInfo(type).getPropertyDescriptors())
267-
.filter(x -> x.getName().contains(attributePathElements[1])).findFirst();
268+
.filter(x -> x.getName().equalsIgnoreCase(oldPropertyName == null ? attributePathElements[1] : oldPropertyName)).findFirst();
268269
if (property.isPresent()) {
269270
return property.get().getReadMethod().getReturnType();
270271
} else {
@@ -413,11 +414,11 @@ protected String getDataTypeFromAttribute(AttributeType attributeType){
413414
return attributeDataType.substring(attributeDataType.lastIndexOf(":") + 1);
414415
}
415416

416-
protected void setValueDataTypeFromAttributeDataType(AmlParser parser, Object parent, String attributeNameWithAttributeDataType, Class aasClazz) throws MappingException {
417+
protected void setValueDataTypeFromAttributeDataType(AmlParser parser, Object parent, String attributeRefWithAttributeDataType, Class aasClazz) throws MappingException {
417418
if(parser == null || parent == null)return;
418419

419-
AttributeType attributeType = findAttributes(parser.getCurrent(),
420-
x -> x.getName().equalsIgnoreCase(attributeNameWithAttributeDataType)).stream().findFirst().orElse(null);
420+
AttributeType attributeType = findAttributesByCorrespondingAttributePath(parser.getCurrent(),
421+
attributeRefWithAttributeDataType).stream().findFirst().orElse(null);
421422
if(attributeType != null){
422423
try {
423424
String dataType = getDataTypeFromAttribute(attributeType);
@@ -472,8 +473,12 @@ protected List<AttributeType> findAttributes(CAEXObject parent, PropertyDescript
472473
}
473474
String refSemantic = refSemanticPrefix + ":" + property.getReadMethod().getDeclaringClass().getSimpleName() + "/"
474475
+ context.getPropertyNamingStrategy().getNameForRefSemantic(property.getReadMethod().getDeclaringClass(), null, property.getName());
475-
return findAttributes(parent, x -> x.getRefSemantic().stream()
476-
.anyMatch(y -> y.getCorrespondingAttributePath().equals(refSemantic)));
476+
return findAttributesByCorrespondingAttributePath(parent, refSemantic);
477+
}
478+
479+
protected List<AttributeType> findAttributesByCorrespondingAttributePath(CAEXObject parent, String correspondingAttributePath){
480+
return findAttributes(parent,
481+
x -> x.getRefSemantic().stream().anyMatch(y -> y.getCorrespondingAttributePath().equalsIgnoreCase(correspondingAttributePath)));
477482
}
478483

479484
protected List<AttributeType> findAttributes(CAEXObject parent, Predicate<AttributeType> filter) {
@@ -557,6 +562,11 @@ protected List<InternalElementType> findInternalElements(InternalElementType par
557562
return parent.getInternalElement().stream().filter(filter).collect(Collectors.toList());
558563
}
559564

565+
private void getAllMatchingInternalElements(Predicate<InternalElementType> filter, List<InternalElementType> resultList){
566+
567+
}
568+
569+
560570
protected List<InterfaceClassType> findExternalInterface(CAEXObject parent, Predicate<InterfaceClassType> filter) {
561571
if (parent == null || filter == null) {
562572
return List.of();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import io.adminshell.aas.v3.dataformat.aml.AmlDocumentInfo;
1919
import io.adminshell.aas.v3.dataformat.aml.common.AbstractMappingContext;
20-
import io.adminshell.aas.v3.dataformat.aml.serialization.naming.NamingStrategy;
20+
import io.adminshell.aas.v3.dataformat.aml.common.naming.NamingStrategy;
2121
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2222
import io.adminshell.aas.v3.dataformat.mapping.MappingProvider;
2323
import io.adminshell.aas.v3.model.Reference;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
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.ConceptDescription;
26-
import io.adminshell.aas.v3.model.Identifier;
27-
import io.adminshell.aas.v3.model.Property;
2826
import io.adminshell.aas.v3.model.Reference;
2927

3028
import java.beans.PropertyDescriptor;
@@ -36,6 +34,7 @@
3634
*/
3735
public class ConceptDescriptionMapper extends DefaultMapper<ConceptDescription> {
3836

37+
public static final String ATTRIBUTE_PATH_ISCASEOF = "AAS:ConceptDescription/isCaseOf";
3938
protected static PropertyDescriptor PROPERTY_ISCASEOF = AasUtils.getProperty(ConceptDescription.class, "isCaseOfs");
4039

4140
public ConceptDescriptionMapper() {
@@ -49,7 +48,7 @@ protected void mapProperties(Object parent, AmlParser parser, MappingContext con
4948
}
5049

5150
CAEXObject temp = parser.getCurrent();
52-
List<AttributeType> attributeTypeList = findAttributes(parser.getCurrent(), x->x.getName().equalsIgnoreCase("isCaseOf"));
51+
List<AttributeType> attributeTypeList = findAttributesByCorrespondingAttributePath(parser.getCurrent(), ATTRIBUTE_PATH_ISCASEOF);
5352
List<Reference> referenceList = new ArrayList<>();
5453
if(!attributeTypeList.isEmpty()){
5554
//TODO How to handle multiple references in isCaseOf?

0 commit comments

Comments
 (0)