|
| 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; |
| 17 | + |
| 18 | +import io.adminshell.aas.v3.dataformat.aml.header.AutomationMLVersion; |
| 19 | +import io.adminshell.aas.v3.dataformat.aml.header.WriterHeader; |
| 20 | +import io.adminshell.aas.v3.dataformat.aml.mapper.AssetAdministrationShellEnvironmentMapper; |
| 21 | +import io.adminshell.aas.v3.dataformat.aml.mapper.ConstraintCollectionMapper; |
| 22 | +import io.adminshell.aas.v3.dataformat.aml.mapper.DataSpecificationContentMapper; |
| 23 | +import io.adminshell.aas.v3.dataformat.aml.mapper.DataSpecificationIEC61360Mapper; |
| 24 | +import io.adminshell.aas.v3.dataformat.aml.mapper.DefaultCollectionMapper; |
| 25 | +import io.adminshell.aas.v3.dataformat.aml.mapper.DefaultMapper; |
| 26 | +import io.adminshell.aas.v3.dataformat.aml.mapper.EmbeddedDataSpecificationCollectionMapper; |
| 27 | +import io.adminshell.aas.v3.dataformat.aml.mapper.FileMapper; |
| 28 | +import io.adminshell.aas.v3.dataformat.aml.mapper.LangStringCollectionMapper; |
| 29 | +import io.adminshell.aas.v3.dataformat.aml.mapper.MappingException; |
| 30 | +import io.adminshell.aas.v3.dataformat.aml.mapper.OperationVariableCollectionMapper; |
| 31 | +import io.adminshell.aas.v3.dataformat.aml.mapper.OperationVariableMapper; |
| 32 | +import io.adminshell.aas.v3.dataformat.aml.mapper.QualifierMapper; |
| 33 | +import io.adminshell.aas.v3.dataformat.aml.mapper.ReferenceCollectionMapper; |
| 34 | +import io.adminshell.aas.v3.dataformat.aml.mapper.ReferenceElementMapper; |
| 35 | +import io.adminshell.aas.v3.dataformat.aml.mapper.ReferenceMapper; |
| 36 | +import io.adminshell.aas.v3.dataformat.aml.mapper.RelationshipElementMapper; |
| 37 | +import io.adminshell.aas.v3.dataformat.aml.mapper.SubmodelCollectionMapper; |
| 38 | +import io.adminshell.aas.v3.dataformat.aml.mapper.SubmodelMapper; |
| 39 | +import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXFile; |
| 40 | +import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment; |
| 41 | +import io.adminshell.aas.v3.model.LangString; |
| 42 | +import io.adminshell.aas.v3.model.Referable; |
| 43 | +import java.util.List; |
| 44 | +import org.slf4j.LoggerFactory; |
| 45 | + |
| 46 | +public class AasToAmlMapper { |
| 47 | + |
| 48 | + public static final String DEFAULT_SCHEMA_VERSION = "2.15"; |
| 49 | + public static final String DEFAULT_AML_VERSION = "2.0"; |
| 50 | + public static final String DEFAULT_FILENAME = "AssetAdministrationShellEnvironment.aml"; |
| 51 | + private static final org.slf4j.Logger log = LoggerFactory.getLogger(AasToAmlMapper.class); |
| 52 | + private static final String DEFAULT_LANGUAGE = "EN"; |
| 53 | + private CAEXFile result; |
| 54 | + |
| 55 | + public CAEXFile map(AssetAdministrationShellEnvironment env) throws MappingException { |
| 56 | + return map(env, DEFAULT_SCHEMA_VERSION, DEFAULT_FILENAME); |
| 57 | + } |
| 58 | + |
| 59 | + public CAEXFile map(AssetAdministrationShellEnvironment env, String schemaVersion, String filename) throws MappingException { |
| 60 | + AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy(); |
| 61 | + classNamingStrategy.registerCustomNaming(LangString.class, x -> "aml-lang=" + x.getLanguage()); |
| 62 | + PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy(); |
| 63 | + propertyNamingStrategy.registerCustomNaming(Referable.class, "descriptions", "description"); |
| 64 | + MappingProvider mappingProvider = new MappingProvider( |
| 65 | + new DefaultMapper(), |
| 66 | + new DefaultCollectionMapper(), |
| 67 | + List.of( |
| 68 | + new AssetAdministrationShellEnvironmentMapper(), |
| 69 | + new SubmodelCollectionMapper(), |
| 70 | + new LangStringCollectionMapper(), |
| 71 | + new ReferenceMapper(), |
| 72 | + new OperationVariableMapper(), |
| 73 | + new OperationVariableCollectionMapper(), |
| 74 | + new ReferenceCollectionMapper(), |
| 75 | + new ConstraintCollectionMapper(), |
| 76 | + new QualifierMapper(), |
| 77 | + new FileMapper(), |
| 78 | + new SubmodelMapper(), |
| 79 | + new EmbeddedDataSpecificationCollectionMapper(), |
| 80 | + new DataSpecificationContentMapper(), |
| 81 | + new ReferenceElementMapper(), |
| 82 | + new RelationshipElementMapper(), |
| 83 | + new DataSpecificationIEC61360Mapper()), |
| 84 | + classNamingStrategy, |
| 85 | + propertyNamingStrategy |
| 86 | + ); |
| 87 | + // FIX |
| 88 | + // ReferenceElement and RelationshipElement reference other Referables. |
| 89 | + // Each such reference is represented as an InternalLink in AML. |
| 90 | + // InternalLinks require target to be an interface but AAS AML serialization |
| 91 | + // does not define creation of such interfaces. Therefore, we do a preprocessing |
| 92 | + // step to identify all Referable which are targeted by such a reference |
| 93 | + // to later add a generic interface to them upon serialization |
| 94 | + |
| 95 | + // NOTE |
| 96 | + // This has implications on ID generator and requires additional lookup table |
| 97 | + // for Referable --> AML ID of ExternalInterface |
| 98 | + MappingContext context = new MappingContext(mappingProvider, env); |
| 99 | + context.getFileBuilder().withSchemaVersion(schemaVersion); |
| 100 | + context.getFileBuilder().withFileName(filename); |
| 101 | + context.getFileBuilder().withAdditionalInformation(new AutomationMLVersion("2.0")); |
| 102 | + WriterHeader writerHeader = new WriterHeader(); |
| 103 | + writerHeader.setName("foo"); |
| 104 | + writerHeader.setId("bar"); |
| 105 | + context.getFileBuilder().withAdditionalInformation(writerHeader.wrap()); |
| 106 | + context.map(env); |
| 107 | + return context.getFileBuilder().build(); |
| 108 | + } |
| 109 | +} |
0 commit comments