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

Commit 2f80fe3

Browse files
Working on deserialization, add deserialization of Operations
1 parent 6e406c5 commit 2f80fe3

3 files changed

Lines changed: 81 additions & 0 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
@@ -68,6 +68,7 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
6868
mappingProvider.register(new IdentifiableMapper<Identifiable>());
6969
mappingProvider.register(new ConstraintCollectionMapper());
7070
mappingProvider.register(new QualifierMapper());
71+
mappingProvider.register(new OperationCollectionMapper());
7172
AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();
7273

7374
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ protected List<InternalElementType> findInternalElements(InternalElementType par
413413
});
414414
}
415415

416+
protected List<InternalElementType> findInternalElements(CAEXObject parent, Predicate<InternalElementType> filter) {
417+
if (parent == null || !InternalElementType.class.isAssignableFrom(parent.getClass())) {
418+
return List.of();
419+
}
420+
return findInternalElements((InternalElementType) parent, filter);
421+
}
422+
416423
protected List<InternalElementType> findInternalElements(InternalElementType parent, Predicate<InternalElementType> filter) {
417424
if (parent == null || filter == null) {
418425
return List.of();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.CAEXObject;
22+
import io.adminshell.aas.v3.dataformat.aml.model.caex.InternalElementType;
23+
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
24+
import io.adminshell.aas.v3.model.OperationVariable;
25+
import io.adminshell.aas.v3.model.SubmodelElement;
26+
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 OperationCollectionMapper extends DefaultMapper<Collection<OperationVariable>> {
33+
34+
35+
@Override
36+
protected Collection mapCollectionValueProperty(AmlParser parser, MappingContext context) throws MappingException {
37+
38+
if (parser == null || context == null || context.getProperty() == null) {
39+
return null;
40+
}
41+
42+
List<InternalElementType> variables = findInternalElements(parser.getCurrent(), x -> x.getName().equalsIgnoreCase(context.getProperty().getName()));
43+
if (variables == null || variables.size() == 0) return null;
44+
if (variables.size() > 1)
45+
throw new MappingException(String.format("multiple %s elements are found in %s %s", context.getProperty().getName(), parser.getCurrent().getID(), parser.getCurrent().getName()));
46+
47+
List<InternalElementType> internalElements = findInternalElements(variables.get(0), SubmodelElement.class, true, context);
48+
49+
CAEXObject current = parser.getCurrent();
50+
51+
if (!internalElements.isEmpty()) {
52+
Collection result = new ArrayList<>();
53+
for (InternalElementType internalElement : internalElements) {
54+
try {
55+
parser.setCurrent(internalElement);
56+
OperationVariable operationVariable = context.getTypeFactory().newInstance(OperationVariable.class);
57+
58+
SubmodelElement submodelElement = (SubmodelElement) context.withoutProperty().map(typeFromInternalElement(internalElement), parser);
59+
operationVariable.setValue(submodelElement);
60+
result.add(operationVariable);
61+
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
62+
throw new MappingException("error mapping Collection<OperationVariable> - could not create new instance for interface OperationVariable");
63+
}
64+
}
65+
parser.setCurrent(current);
66+
return result;
67+
}
68+
return null;
69+
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)