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

Commit 8ca3008

Browse files
committed
generic solution approach for parsing logic
1 parent 5bad8a0 commit 8ca3008

8 files changed

Lines changed: 196 additions & 23 deletions

File tree

dataformat-uanodeset/src/main/java/io/adminshell/aas/v3/dataformat/i4aas/I4AASDeserializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.adminshell.aas.v3.dataformat.Deserializer;
2727
import io.adminshell.aas.v3.dataformat.i4aas.parsers.EnvironmentParser;
2828
import io.adminshell.aas.v3.dataformat.i4aas.parsers.ParserContext;
29+
import io.adminshell.aas.v3.dataformat.i4aas.parsers.UANodeWrapper;
2930
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
3031

3132
public class I4AASDeserializer implements Deserializer {
@@ -34,8 +35,9 @@ public class I4AASDeserializer implements Deserializer {
3435
public AssetAdministrationShellEnvironment read(String value) throws DeserializationException {
3536
try {
3637
UANodeSet uaNodeSet = new UANodeSetUnmarshaller().unmarshall(value);
37-
UANode aasEnv = findAASEnvironment(uaNodeSet);
38-
return new EnvironmentParser(aasEnv, new ParserContext(uaNodeSet)).parse();
38+
ParserContext parserContext = new ParserContext(uaNodeSet);
39+
UANodeWrapper environment = parserContext.getPreparsedEnvironment();
40+
return new EnvironmentParser(environment, new ParserContext(uaNodeSet)).parse();
3941
} catch (JAXBException e) {
4042
throw new DeserializationException("Deserialization failed due to a JAXBException.", e);
4143
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.i4aas.parsers;
17+
18+
import io.adminshell.aas.v3.dataformat.i4aas.mappers.utils.I4AASIdentifier;
19+
import io.adminshell.aas.v3.model.AssetAdministrationShell;
20+
21+
public class AssetAdministrationShellParser extends I4AASParser<AssetAdministrationShell> {
22+
23+
public AssetAdministrationShellParser(UANodeWrapper src, ParserContext ctx) {
24+
super(src, ctx, I4AASIdentifier.AASAssetAdministrationShellType);
25+
}
26+
27+
@Override
28+
protected void parseAndAttachChildren() {
29+
// TODO Auto-generated method stub
30+
31+
}
32+
33+
@Override
34+
protected AssetAdministrationShell createTargetObject() {
35+
// TODO Auto-generated method stub
36+
return null;
37+
}
38+
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.i4aas.parsers;
17+
18+
import java.util.function.Consumer;
19+
import java.util.function.Predicate;
20+
21+
public class ConditionParserAction<TARGET> {
22+
23+
public final Predicate<UANodeWrapper> condition;
24+
public final I4AASParser<TARGET> parser;
25+
public final Consumer<TARGET> action;
26+
27+
private ConditionParserAction(Predicate<UANodeWrapper> condition, I4AASParser<TARGET> parser, Consumer<TARGET> action) {
28+
this.condition = condition;
29+
this.parser = parser;
30+
this.action = action;
31+
}
32+
33+
public static <TARGET> ConditionParserAction<TARGET> of(Predicate<UANodeWrapper> condition, I4AASParser<TARGET> parser, Consumer<TARGET> action) {
34+
return new ConditionParserAction<TARGET>(condition, parser, action);
35+
}
36+
37+
}

dataformat-uanodeset/src/main/java/io/adminshell/aas/v3/dataformat/i4aas/parsers/EnvironmentParser.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,31 @@
1515
*/
1616
package io.adminshell.aas.v3.dataformat.i4aas.parsers;
1717

18-
import org.opcfoundation.ua._2011._03.uanodeset.UANode;
18+
import static io.adminshell.aas.v3.dataformat.i4aas.mappers.utils.I4AASIdentifier.AASAssetAdministrationShellType;
19+
import static io.adminshell.aas.v3.dataformat.i4aas.mappers.utils.I4AASIdentifier.AASSubmodelType;
1920

21+
import java.util.function.Predicate;
22+
23+
import io.adminshell.aas.v3.dataformat.i4aas.mappers.utils.I4AASIdentifier;
24+
import io.adminshell.aas.v3.model.AssetAdministrationShell;
2025
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
26+
import io.adminshell.aas.v3.model.Submodel;
2127
import io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment;
2228

23-
public class EnvironmentParser extends I4AASParser<UANode, AssetAdministrationShellEnvironment> {
29+
public class EnvironmentParser extends I4AASParser<AssetAdministrationShellEnvironment> {
30+
31+
private Predicate<UANodeWrapper> pred;
2432

25-
public EnvironmentParser(UANode src, ParserContext ctx) {
26-
super(src, ctx);
33+
public EnvironmentParser(UANodeWrapper src, ParserContext ctx) {
34+
super(src, ctx, I4AASIdentifier.AASEnvironmentType);
35+
this.<Submodel>addChildMapping(ofType(AASSubmodelType), new SubmodelParser(src, ctx), target.getSubmodels()::add);
36+
this.<AssetAdministrationShell>addChildMapping(ofType(AASAssetAdministrationShellType), new AssetAdministrationShellParser(src, ctx), target.getAssetAdministrationShells()::add);
2737
}
28-
38+
2939
@Override
3040
protected AssetAdministrationShellEnvironment createTargetObject() {
3141
target = new DefaultAssetAdministrationShellEnvironment();
3242
return target;
3343
}
34-
35-
@Override
36-
protected void parseAndAttachChildren() {
37-
// TODO Auto-generated method stub
38-
}
39-
44+
4045
}

dataformat-uanodeset/src/main/java/io/adminshell/aas/v3/dataformat/i4aas/parsers/I4AASParser.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,53 @@
1515
*/
1616
package io.adminshell.aas.v3.dataformat.i4aas.parsers;
1717

18-
import org.opcfoundation.ua._2011._03.uanodeset.UANode;
18+
import java.util.List;
19+
import java.util.function.Consumer;
20+
import java.util.function.Predicate;
1921

20-
public abstract class I4AASParser<SOURCE extends UANode, TARGET> {
22+
import io.adminshell.aas.v3.dataformat.i4aas.mappers.utils.BasicIdentifier;
23+
24+
public abstract class I4AASParser<TARGET> {
2125

2226
protected ParserContext ctx;
23-
protected SOURCE source;
27+
protected UANodeWrapper source;
2428
protected TARGET target;
29+
protected BasicIdentifier type;
30+
protected List<ConditionParserAction<Object>> genericChildParserLogic;
2531

26-
public I4AASParser(SOURCE src, ParserContext ctx) {
32+
public I4AASParser(UANodeWrapper src, ParserContext ctx, BasicIdentifier forUaType) {
2733
this.source = src;
2834
this.ctx = ctx;
35+
this.type = forUaType;
2936
}
3037

38+
protected abstract TARGET createTargetObject();
39+
3140
public final TARGET parse() {
3241
target = createTargetObject();
3342
parseAndAttachChildren();
3443
return target;
3544
}
3645

37-
protected abstract void parseAndAttachChildren();
46+
protected final <CHILDTARGET> void addChildMapping(Predicate<UANodeWrapper> condition, I4AASParser<CHILDTARGET> parser,
47+
Consumer<CHILDTARGET> action) {
48+
genericChildParserLogic.add((ConditionParserAction<Object>) ConditionParserAction.<CHILDTARGET>of(condition, parser, action));
49+
}
50+
51+
protected Predicate<UANodeWrapper> ofType(BasicIdentifier identifier) {
52+
return node -> node.getType() == identifier;
53+
}
54+
55+
protected void parseAndAttachChildren() {
56+
for (UANodeWrapper uaNodeWrapper : source.getPropertiesAndComponents()) {
57+
for (ConditionParserAction<Object> logic : genericChildParserLogic) {
58+
if (logic.condition.test(uaNodeWrapper)) {
59+
Object parse = logic.parser.parse();
60+
logic.action.accept(parse);
61+
}
62+
}
63+
}
64+
}
65+
3866

39-
protected abstract TARGET createTargetObject();
4067
}

dataformat-uanodeset/src/main/java/io/adminshell/aas/v3/dataformat/i4aas/parsers/ParserContext.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Optional;
2121
import java.util.function.Function;
2222
import java.util.function.Predicate;
23-
import java.util.stream.Collectors;
2423
import java.util.stream.Stream;
2524

2625
import org.opcfoundation.ua._2011._03.uanodeset.ListOfReferences;
@@ -39,7 +38,7 @@ public class ParserContext {
3938
private int i4aasNsIdx;
4039

4140
protected NodeIdResolver nodeId2NodeMap;
42-
private UANodeWrapper uaNodeWrapper;
41+
private UANodeWrapper uaEnvironment;
4342

4443
public ParserContext(UANodeSet uaNodeset) {
4544
this.uaNodeset = uaNodeset;
@@ -50,7 +49,7 @@ public ParserContext(UANodeSet uaNodeset) {
5049

5150
Optional<UANode> environmentNode = findNodeOfAASEnvironmentType();
5251
if (environmentNode.isPresent()) {
53-
uaNodeWrapper = new UANodeWrapper(environmentNode.get(), nodeId2NodeMap, this);
52+
uaEnvironment = new UANodeWrapper(environmentNode.get(), nodeId2NodeMap, this);
5453
} else {
5554
//log no environment found
5655
}
@@ -131,7 +130,7 @@ protected Predicate<? super Reference> typeOf(UaIdentifier id) {
131130
}
132131

133132
public UANodeWrapper getPreparsedEnvironment() {
134-
return uaNodeWrapper;
133+
return uaEnvironment;
135134
}
136135

137136
public int getI4aasNsIdx() {
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.i4aas.parsers;
17+
18+
import java.util.function.Consumer;
19+
import java.util.function.Predicate;
20+
21+
import io.adminshell.aas.v3.dataformat.i4aas.mappers.utils.I4AASIdentifier;
22+
import io.adminshell.aas.v3.model.AdministrativeInformation;
23+
import io.adminshell.aas.v3.model.Submodel;
24+
import io.adminshell.aas.v3.model.impl.DefaultSubmodel;
25+
26+
public class SubmodelParser extends I4AASParser<Submodel> {
27+
28+
private I4AASParser<AdministrativeInformation> pars = new I4AASParser<AdministrativeInformation>(source, ctx, type) {
29+
30+
@Override
31+
protected AdministrativeInformation createTargetObject() {
32+
// TODO Auto-generated method stub
33+
return null;
34+
}
35+
};
36+
37+
public SubmodelParser(UANodeWrapper src, ParserContext ctx) {
38+
super(src, ctx, I4AASIdentifier.AASSubmodelElementType);
39+
//Predicate<UANodeWrapper> pred;
40+
//Consumer<AdministrativeInformation> act;
41+
//this.<AdministrativeInformation>addChildMapping(pred, pars , act);
42+
}
43+
44+
@Override
45+
protected Submodel createTargetObject() {
46+
DefaultSubmodel defaultSubmodel = new DefaultSubmodel();
47+
return defaultSubmodel;
48+
}
49+
50+
@Override
51+
protected void parseAndAttachChildren() {
52+
// TODO Auto-generated method stub
53+
}
54+
55+
56+
}

dataformat-uanodeset/src/main/java/io/adminshell/aas/v3/dataformat/i4aas/parsers/UANodeWrapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.adminshell.aas.v3.dataformat.i4aas.parsers;
1717

18+
import java.util.ArrayList;
1819
import java.util.List;
1920
import java.util.Objects;
2021
import java.util.stream.Collectors;
@@ -80,6 +81,13 @@ public List<UANodeWrapper> getComponents() {
8081
public List<UANodeWrapper> getProperties() {
8182
return properties;
8283
}
84+
85+
public List<UANodeWrapper> getPropertiesAndComponents() {
86+
List<UANodeWrapper> propertiesAndComponents = new ArrayList<>();
87+
propertiesAndComponents.addAll(properties);
88+
propertiesAndComponents.addAll(components);
89+
return propertiesAndComponents;
90+
}
8391

8492
public BasicIdentifier getType() {
8593
return type;

0 commit comments

Comments
 (0)