| package org.eclipse.basyx.aas.factory.json; |
| |
| import java.util.List; |
| import java.util.Map; |
| import java.util.stream.Collectors; |
| |
| import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell; |
| import org.eclipse.basyx.aas.metamodel.map.parts.Asset; |
| import org.eclipse.basyx.submodel.metamodel.api.reference.IKey; |
| import org.eclipse.basyx.submodel.metamodel.api.reference.IReference; |
| import org.eclipse.basyx.submodel.metamodel.map.SubModel; |
| import org.eclipse.basyx.submodel.metamodel.map.parts.ConceptDescription; |
| import org.eclipse.basyx.submodel.metamodel.map.reference.Reference; |
| import org.eclipse.basyx.vab.coder.json.serialization.DefaultTypeFactory; |
| import org.eclipse.basyx.vab.coder.json.serialization.GSONTools; |
| |
| /** |
| * This class can be used to parse JSON to Metamodel Objects |
| * |
| * @author conradi |
| * |
| */ |
| public class JSONToMetamodelConverter { |
| |
| private Map<String, Object> root; |
| |
| // Buffer used for parsed assets to prevent deserializing them multiple times |
| private List<Asset> assetBuf; |
| |
| @SuppressWarnings("unchecked") |
| public JSONToMetamodelConverter(String jsonContent) { |
| root = (Map<String, Object>) new GSONTools(new DefaultTypeFactory()).deserialize(jsonContent); |
| } |
| |
| /** |
| * Parses the AASs from the JSON |
| * |
| * @return the AASs parsed from the JSON |
| */ |
| @SuppressWarnings("unchecked") |
| public List<AssetAdministrationShell> parseAAS() { |
| assetBuf = parseAssets(); |
| return ((List<Object>) root.get(MetamodelToJSONConverter.ASSET_ADMINISTRATION_SHELLS)).stream() |
| .map(this::parseAssetAdministrationShell).collect(Collectors.toList()); |
| } |
| |
| @SuppressWarnings("unchecked") |
| private AssetAdministrationShell parseAssetAdministrationShell(Object mapObject) { |
| AssetAdministrationShell parsedAAS = AssetAdministrationShell.createAsFacade((Map<String, Object>) mapObject); |
| // Fix Asset - Asset in json-Serialization is just a reference |
| Map<String, Object> assetRefMap = (Map<String, Object>) parsedAAS.get(AssetAdministrationShell.ASSET); |
| if (assetRefMap.get(Reference.KEY) == null && assetRefMap.get(Asset.KIND) != null) { |
| // => Is already an asset, => does not need to be fixed |
| return parsedAAS; |
| } |
| parsedAAS.put(AssetAdministrationShell.ASSETREF, assetRefMap); |
| |
| // Now try to find the Asset and add it to the AssetAdministrationShell |
| IReference assetRef = parsedAAS.getAssetReference(); |
| IKey lastKey = assetRef.getKeys().get(assetRef.getKeys().size() - 1); |
| String idValue = lastKey.getValue(); |
| for (Asset asset : assetBuf) { |
| if (asset.getIdentification().getId().equals(idValue)) { |
| parsedAAS.put(AssetAdministrationShell.ASSET, asset); |
| break; |
| } |
| } |
| |
| return parsedAAS; |
| } |
| |
| /** |
| * Parses the Submodels from the JSON |
| * |
| * @return the Submodels parsed from the JSON |
| */ |
| @SuppressWarnings("unchecked") |
| public List<SubModel> parseSubmodels() { |
| return ((List<Object>) root.get(MetamodelToJSONConverter.SUBMODELS)).stream() |
| .map(i -> SubModel.createAsFacade((Map<String, Object>) i)).collect(Collectors.toList()); |
| } |
| |
| /** |
| * Parses the Assets from the JSON |
| * |
| * @return the Assets parsed from the JSON |
| */ |
| @SuppressWarnings("unchecked") |
| public List<Asset> parseAssets() { |
| return ((List<Object>) root.get(MetamodelToJSONConverter.ASSETS)).stream() |
| .map(i -> Asset.createAsFacade((Map<String, Object>) i)).collect(Collectors.toList()); |
| } |
| |
| /** |
| * Parses the ConceptDescriptions from the JSON |
| * |
| * @return the ConceptDescriptions parsed from the JSON |
| */ |
| @SuppressWarnings("unchecked") |
| public List<ConceptDescription> parseConceptDescriptions() { |
| return ((List<Object>) root.get(MetamodelToJSONConverter.CONCEPT_DESCRIPTIONS)).stream() |
| .map(i -> ConceptDescription.createAsFacade((Map<String, Object>) i)).collect(Collectors.toList()); |
| } |
| } |