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

Commit 529d659

Browse files
authored
Merge pull request #11 from admin-shell-io/feature/aml-deserializer
Feature/aml deserializer
2 parents 3b74895 + 6b7a3f5 commit 529d659

52 files changed

Lines changed: 2547 additions & 303 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dataformat-aml/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
<artifactId>jaxb-api</artifactId>
3434
<version>${jaxb.version}</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>xerces</groupId>
38+
<artifactId>xercesImpl</artifactId>
39+
<version>2.12.1</version>
40+
<exclusions>
41+
<exclusion>
42+
<artifactId>xml-apis</artifactId>
43+
<groupId>xml-apis</groupId>
44+
</exclusion>
45+
</exclusions>
46+
</dependency>
3647
<dependency>
3748
<groupId>org.eclipse.persistence</groupId>
3849
<artifactId>org.eclipse.persistence.moxy</artifactId>
@@ -61,6 +72,11 @@
6172
<version>${xmlunit.version}</version>
6273
<scope>test</scope>
6374
</dependency>
75+
<dependency>
76+
<groupId>com.google.inject</groupId>
77+
<artifactId>guice</artifactId>
78+
<version>5.0.1</version>
79+
</dependency>
6480
</dependencies>
6581
<build>
6682
<plugins>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.adminshell.aas.v3.dataformat.DeserializationException;
1919
import io.adminshell.aas.v3.dataformat.Deserializer;
20+
import io.adminshell.aas.v3.dataformat.aml.deserialization.AasTypeFactory;
2021
import io.adminshell.aas.v3.dataformat.aml.deserialization.Aml2AasMapper;
2122
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXFile;
2223
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
@@ -34,14 +35,17 @@
3435
public class AmlDeserializer implements Deserializer {
3536

3637
private static final Logger log = LoggerFactory.getLogger(AmlDeserializer.class);
38+
private AasTypeFactory typeFactory = new AasTypeFactory();
3739

3840
@Override
3941
public AssetAdministrationShellEnvironment read(String value) throws DeserializationException {
4042
try {
4143
Unmarshaller unmarshaller = JAXBContextFactory.createContext(new Class[]{CAEXFile.class}, null).createUnmarshaller();
4244
StringReader reader = new StringReader(value);
4345
CAEXFile aml = (CAEXFile) unmarshaller.unmarshal(reader);
44-
Aml2AasMapper mapper = new Aml2AasMapper(new AmlDeserializationConfig.Builder().build());
46+
Aml2AasMapper mapper = new Aml2AasMapper(new AmlDeserializationConfig.Builder()
47+
.typeFactory(typeFactory)
48+
.build());
4549
return mapper.map(aml);
4650
} catch (JAXBException ex) {
4751
throw new DeserializationException("error deserializing AssetAdministrationShellEnvironment", ex);
@@ -52,6 +56,6 @@ public AssetAdministrationShellEnvironment read(String value) throws Deserializa
5256

5357
@Override
5458
public <T> void useImplementation(Class<T> aasInterface, Class<? extends T> implementation) {
55-
throw new UnsupportedOperationException("Not supported yet.");
59+
typeFactory.useImplementation(aasInterface, implementation);
5660
}
5761
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.common;
17+
18+
import io.adminshell.aas.v3.dataformat.aml.serialization.naming.NamingStrategy;
19+
import io.adminshell.aas.v3.dataformat.mapping.Mapper;
20+
import io.adminshell.aas.v3.dataformat.mapping.MappingContext;
21+
import io.adminshell.aas.v3.dataformat.mapping.MappingProvider;
22+
import java.beans.PropertyDescriptor;
23+
24+
/**
25+
* Abstract base class for MappingContext used for mapping between AML and AAS
26+
*
27+
* @param <T>
28+
*/
29+
public class AbstractMappingContext<T extends Mapper> extends MappingContext<T> {
30+
31+
protected final NamingStrategy classNamingStrategy;
32+
protected final NamingStrategy propertyNamingStrategy;
33+
protected final PropertyDescriptor property;
34+
35+
protected AbstractMappingContext(MappingProvider<T> mappingProvider,
36+
NamingStrategy classNamingStrategy,
37+
NamingStrategy propertyNamingStrategy,
38+
PropertyDescriptor property) {
39+
super(mappingProvider);
40+
this.classNamingStrategy = classNamingStrategy;
41+
this.propertyNamingStrategy = propertyNamingStrategy;
42+
this.property = property;
43+
}
44+
45+
/**
46+
* Gets the class naming strategy. This is used to resolve naming
47+
* differences in class names between AML and AAS.
48+
*
49+
* @return the naming strategy
50+
*/
51+
public NamingStrategy getClassNamingStrategy() {
52+
return classNamingStrategy;
53+
}
54+
55+
/**
56+
* Gets the property naming strategy. This is used to resolve naming
57+
* differences in property namings between AML and AAS.
58+
*
59+
* @return
60+
*/
61+
public NamingStrategy getPropertyNamingStrategy() {
62+
return propertyNamingStrategy;
63+
}
64+
65+
/**
66+
* Gets the currently processed property if present, otherwise null
67+
*
68+
* @return the currently processed property if present, otherwise null
69+
*/
70+
public PropertyDescriptor getProperty() {
71+
return property;
72+
}
73+
74+
}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public <T> void useImplementation(Class<T> aasInterface, Class<? extends T> impl
5050
* Creates a new instance for a given aasInterface. If the
5151
*
5252
* @param <T> type to create
53-
* @param aasInterface class to find instantiate
53+
* @param aasInterfaceType class to find instantiate
5454
* @return an instance of aasInterface
5555
* @throws NoSuchMethodException
5656
* @throws InstantiationException
@@ -59,19 +59,34 @@ public <T> void useImplementation(Class<T> aasInterface, Class<? extends T> impl
5959
* concrete type to instantiate could be found
6060
* @throws InvocationTargetException
6161
*/
62-
public <T> T newInstance(Class<T> aasInterface) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
63-
if (aasInterface == null) {
62+
public <T> T newInstance(Class<T> aasInterfaceType) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
63+
Constructor<?> constructor = getImplementationType(aasInterfaceType).getConstructor();
64+
constructor.setAccessible(true);
65+
return (T) constructor.newInstance();
66+
}
67+
68+
/**
69+
* Gets the concrete implementation type to use for a given type. If there
70+
* is no explicit type mapping and the provided type is concrete, the type
71+
* itself is returned.
72+
*
73+
* @param <T> type information
74+
* @param aasInterfaceType the type to find a concrete implementation type
75+
* for
76+
* @throws IllegalArgumentException if there is no type mapping for the
77+
* provided type and the type is not concrete
78+
* @return a concrete implementation type for the given type
79+
*/
80+
public <T> Class<? extends T> getImplementationType(Class<T> aasInterfaceType) {
81+
if (aasInterfaceType == null) {
6482
throw new IllegalArgumentException("aasInterface must be non-null");
6583
}
66-
Class<?> classToInstantiate = aasInterface;
67-
if (typeMapping.containsKey(aasInterface)) {
68-
classToInstantiate = typeMapping.get(aasInterface);
84+
if (typeMapping.containsKey(aasInterfaceType)) {
85+
return (Class<? extends T>) typeMapping.get(aasInterfaceType);
6986
}
70-
if (classToInstantiate.isInterface()) {
71-
throw new IllegalArgumentException("could not resolve type for interface " + classToInstantiate.getName());
87+
if (aasInterfaceType.isInterface()) {
88+
throw new IllegalArgumentException("could not resolve type for interface " + aasInterfaceType.getName());
7289
}
73-
Constructor<?> constructor = classToInstantiate.getConstructor();
74-
constructor.setAccessible(true);
75-
return (T) constructor.newInstance();
90+
return aasInterfaceType;
7691
}
7792
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@
1515
*/
1616
package io.adminshell.aas.v3.dataformat.aml.deserialization;
1717

18-
import io.adminshell.aas.v3.dataformat.aml.deserialization.mappers.AssetAdministrationShellEnvironmentMapper;
18+
import io.adminshell.aas.v3.dataformat.aml.deserialization.mappers.*;
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;
2225
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2326
import io.adminshell.aas.v3.dataformat.mapping.MappingProvider;
2427
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
28+
import io.adminshell.aas.v3.model.Identifiable;
29+
import io.adminshell.aas.v3.model.MultiLanguageProperty;
30+
import io.adminshell.aas.v3.model.Referable;
31+
import io.adminshell.aas.v3.model.Qualifiable;
32+
33+
2534
import java.util.List;
2635

2736
/**
@@ -49,9 +58,36 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
4958
AmlParser parser = new AmlParser(aml);
5059
MappingProvider mappingProvider = new MappingProvider(Mapper.class, new DefaultMapper(), new DefaultMapper());
5160
mappingProvider.register(new AssetAdministrationShellEnvironmentMapper());
52-
MappingContext context = new MappingContext(mappingProvider, config.getTypeFactory());
61+
mappingProvider.register(new EnumMapper());
62+
mappingProvider.register(new ReferenceMapper());
63+
mappingProvider.register(new AssetAdministrationShellMapper());
64+
mappingProvider.register(new LangStringCollectionMapper());
65+
mappingProvider.register(new RelationshipElementMapper());
66+
mappingProvider.register(new ReferenceElementMapper());
67+
mappingProvider.register(new ReferableMapper<Referable>());
68+
mappingProvider.register(new IdentifiableMapper<Identifiable>());
69+
mappingProvider.register(new ConstraintCollectionMapper());
70+
mappingProvider.register(new QualifierMapper());
71+
mappingProvider.register(new OperationCollectionMapper());
72+
mappingProvider.register(new FileMapper());
73+
mappingProvider.register(new RangeMapper());
74+
mappingProvider.register(new ViewMapper());
75+
mappingProvider.register(new PropertyMapper());
76+
mappingProvider.register(new ConceptDescriptionMapper());
77+
mappingProvider.register(new EmbeddedDataSpecificationCollectionMapper());
78+
mappingProvider.register(new DataSpecificationIEC61360Mapper());
79+
mappingProvider.register(new EnumDataTypeIEC61360Mapper());
80+
AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();
81+
82+
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();
83+
propertyNamingStrategy.registerCustomNaming(Referable.class, "descriptions", "description");
84+
propertyNamingStrategy.registerCustomNaming(MultiLanguageProperty.class, "values", "value");
85+
//propertyNamingStrategy.registerCustomNaming(Qualifier.class, x -> "qualifier:" + x.getType() + "=" + x.getValue(), false);
86+
propertyNamingStrategy.registerCustomNaming(Qualifiable.class, "qualifiers", "qualifier","qualifier");
87+
MappingContext context = new MappingContext(mappingProvider, classNamingStrategy, propertyNamingStrategy, config.getTypeFactory());
5388
context.setDocumentInfo(AmlDocumentInfo.fromFile(aml));
54-
Object result = context.getMappingProvider().getMapper(AssetAdministrationShellEnvironment.class).map(parser, context);
89+
AssetAdministrationShellEnvironment result = context.map(AssetAdministrationShellEnvironment.class, parser);
90+
parser.resolveIdsToReferences(result);
5591
return (AssetAdministrationShellEnvironment) result;
5692
}
5793
}

0 commit comments

Comments
 (0)