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

Commit

Permalink
Merge pull request #53 from admin-shell-io/bugfix/serialization
Browse files Browse the repository at this point in the history
Bugfix/serialization
  • Loading branch information
FrankSchnicke authored Jan 26, 2022
2 parents e31c54b + d14bc2e commit 9879b22
Show file tree
Hide file tree
Showing 13 changed files with 2,325 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
import io.adminshell.aas.v3.dataformat.mapping.MappingProvider;
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
import io.adminshell.aas.v3.model.AssetInformation;
import io.adminshell.aas.v3.model.Identifiable;
import io.adminshell.aas.v3.model.MultiLanguageProperty;
import io.adminshell.aas.v3.model.Referable;
import io.adminshell.aas.v3.model.Qualifiable;


import java.util.List;

/**
Expand Down Expand Up @@ -64,8 +64,8 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
mappingProvider.register(new LangStringCollectionMapper());
mappingProvider.register(new RelationshipElementMapper());
mappingProvider.register(new ReferenceElementMapper());
mappingProvider.register(new ReferableMapper<Referable>());
mappingProvider.register(new IdentifiableMapper<Identifiable>());
mappingProvider.register(new ReferableMapper<>());
mappingProvider.register(new IdentifiableMapper<>());
mappingProvider.register(new ConstraintCollectionMapper());
mappingProvider.register(new QualifierMapper());
mappingProvider.register(new OperationCollectionMapper());
Expand All @@ -77,12 +77,15 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
mappingProvider.register(new EmbeddedDataSpecificationCollectionMapper());
mappingProvider.register(new DataSpecificationIEC61360Mapper());
mappingProvider.register(new EnumDataTypeIEC61360Mapper());
mappingProvider.register(new IdentifierKeyValuePairCollectionMapper());

AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();

PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();
propertyNamingStrategy.registerCustomNaming(Referable.class, "descriptions", "description");
propertyNamingStrategy.registerCustomNaming(MultiLanguageProperty.class, "values", "value");
propertyNamingStrategy.registerCustomNaming(Qualifiable.class, "qualifiers", "qualifier","qualifier");
propertyNamingStrategy.registerCustomNaming(Qualifiable.class, "qualifiers", "qualifier", "qualifier");
propertyNamingStrategy.registerCustomNaming(AssetInformation.class, "specificAssetIds", "specificAssetId");
MappingContext context = new MappingContext(mappingProvider, classNamingStrategy, propertyNamingStrategy, config.getTypeFactory());
context.setDocumentInfo(AmlDocumentInfo.fromFile(aml));
AssetAdministrationShellEnvironment result = context.map(AssetAdministrationShellEnvironment.class, parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public AssetAdministrationShellEnvironment map(AmlParser parser, MappingContext
CAEXFile.SystemUnitClassLib systemUnitClassLib = parser.getContent().getSystemUnitClassLib().stream()
.filter(x -> x.getName().equalsIgnoreCase(ASSET_ADMINISTRATION_SHELL_SYSTEM_UNIT_CLASSES))
.findFirst()
.orElse(null);
.orElse(CAEXFile.SystemUnitClassLib.builder().build());

List<SystemUnitFamilyType> systemUnitFamilyTypeShells = systemUnitClassLib.getSystemUnitClass().stream()
.filter(x ->x.getSupportedRoleClass().get(0).getRefRoleClassPath().equalsIgnoreCase(ROLE_CLASS_LIB_ASSET_ADMINISTRATION_SHELL))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.adminshell.aas.v3.dataformat.aml.deserialization.mappers;

import io.adminshell.aas.v3.dataformat.aml.deserialization.AmlParser;
import io.adminshell.aas.v3.dataformat.aml.deserialization.DefaultMapper;
import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext;
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType;
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXObject;
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
import io.adminshell.aas.v3.model.IdentifierKeyValuePair;
import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> {

@Override
protected Collection mapCollectionValueProperty(AmlParser parser, MappingContext context) throws MappingException {
Collection result = new ArrayList<>();
CAEXObject current = parser.getCurrent();
AttributeType parent = findAttribute(current, context.getProperty(), context);
if (parent == null) {
return result;
}
List<AttributeType> attributes = findAttributes(parent, x -> x.getName().startsWith("specificAssetId"));
for (AttributeType attribute : attributes) {
parser.setCurrent(attribute);
try {
IdentifierKeyValuePair element = context.getTypeFactory().newInstance(IdentifierKeyValuePair.class);
for (PropertyDescriptor property : AasUtils.getAasProperties(IdentifierKeyValuePair.class)) {
Object propertyValue = context
.with(element)
.with(property)
.withoutType()
.map(property.getReadMethod().getGenericReturnType(), parser);
if (propertyValue != null) {
try {
property.getWriteMethod().invoke(element, propertyValue);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new MappingException(String.format("error setting property value for property %s", property.getName()), ex);
}
}
}
parser.setCurrent(attribute);
result.add(element);
} catch (NoSuchMethodException ex) {
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(IdentifierKeyValuePairCollectionMapper.class.getName()).log(Level.SEVERE, null, ex);
}
}
parser.setCurrent(parent);
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.ViewMapper;
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXFile;
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.ConceptDescriptionMapper;
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.IdentifierKeyValuePairCollectionMapper;
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.PropertyMapper;
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.RangeMapper;
import io.adminshell.aas.v3.dataformat.aml.serialization.mappers.ReferenceMapper;
Expand Down Expand Up @@ -99,6 +100,7 @@ public CAEXFile map(AssetAdministrationShellEnvironment env, AmlSerializationCon
mappingProvider.register(new PropertyMapper());
mappingProvider.register(new RangeMapper());
mappingProvider.register(new ConceptDescriptionMapper());
mappingProvider.register(new IdentifierKeyValuePairCollectionMapper());
MappingContext context = new MappingContext(
mappingProvider,
classNamingStrategy,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.adminshell.aas.v3.dataformat.aml.serialization.mappers;

import io.adminshell.aas.v3.dataformat.aml.serialization.DefaultMapper;
import io.adminshell.aas.v3.dataformat.aml.serialization.AmlGenerator;
import io.adminshell.aas.v3.dataformat.aml.serialization.MappingContext;
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType;
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
import io.adminshell.aas.v3.model.AssetInformation;
import io.adminshell.aas.v3.model.IdentifierKeyValuePair;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class IdentifierKeyValuePairCollectionMapper extends DefaultMapper<Collection<IdentifierKeyValuePair>> {

private static final String ATTRIBUTE_NAME = "specificAssetId";

@Override
public void map(Collection<IdentifierKeyValuePair> value, AmlGenerator generator, MappingContext context) throws MappingException {
if (value == null || context == null || value.isEmpty()) {
return;
}
AttributeType.Builder wrapperBuilder = AttributeType.builder()
.withName(ATTRIBUTE_NAME)
.withRefSemantic(generator.refSemantic(AssetInformation.class, ATTRIBUTE_NAME));
List<AttributeType> attributes = new ArrayList<>();
for (IdentifierKeyValuePair element : value) {
AttributeType.Builder builder = AttributeType.builder()
.withName(ATTRIBUTE_NAME + (value.size() > 1 ? "_" + (attributes.size() + 1) : ""));
for (PropertyDescriptor property : AasUtils.getAasProperties(element.getClass())) {
if (!skipProperty(property)) {
context.with(property)
.map(property.getReadMethod().getGenericReturnType(),
getElemenetPropertyValue(element, property, context),
generator.with(builder));
}
}
attributes.add(builder.build());
}
attributes.forEach(x -> wrapperBuilder.addAttribute(x));
generator.add(wrapperBuilder.build());
}

protected Object getElemenetPropertyValue(IdentifierKeyValuePair elemenet, PropertyDescriptor property, MappingContext context) throws MappingException {
try {
return property.getReadMethod().invoke(elemenet);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new MappingException("failed to get property value for property " + property.getName(), ex);
}
}
}
Loading

0 comments on commit 9879b22

Please sign in to comment.