| /******************************************************************************* |
| * Copyright (c) 2020 DFKI. |
| * |
| * This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * DFKI - Volkan Gezer <volkan.gezer@dfki.de> |
| * |
| *******************************************************************************/ |
| package org.eclipse.aas.basyx.codegen.util.submodel; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.LinkedHashMap; |
| import java.util.List; |
| |
| public class SubModel { |
| private String name; |
| private String filePath = ""; |
| private SubModelPropCollector mpc = new SubModelPropCollector(); |
| private SubModelOperationCollector moc = new SubModelOperationCollector(); |
| private List<String> secs = new ArrayList<String>(); |
| private HashMap<String, String> subsectosec = new HashMap<String, String>(); |
| private String fileSec = ""; |
| |
| SubModel(String modelName, SubModelPropCollector mpc, SubModelOperationCollector moc) { |
| this.name = modelName; |
| this.mpc = mpc; |
| this.moc = moc; |
| } |
| |
| /** |
| * |
| * Constructor. Creates a new sub model. |
| * |
| * @param modelName to create |
| */ |
| public SubModel(String modelName) { |
| this.name = modelName; |
| } |
| |
| public HashMap<String, String> getSubModelElementCollectionMap() { |
| return subsectosec; |
| } |
| |
| public String getFileSec() { |
| return fileSec; |
| } |
| |
| public List<String> getSubModelElementCollections() { |
| return secs; |
| } |
| |
| public SubModelPropCollector getMpc() { |
| return mpc; |
| } |
| public SubModelOperationCollector getMoc() { |
| return moc; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getSafeName() { |
| return name.toLowerCase().replace(" ", ""); |
| } |
| |
| /** |
| * Adds a property to the sub model with default value of "Undefined". |
| * @param propName to add |
| */ |
| public void addProperty(String propName) { |
| mpc.addProperty(propName); |
| } |
| |
| /** |
| * Adds a property to the sub model and sets its value to {@code propValue}. |
| * @param propName to add |
| * @param propValue to set the value |
| */ |
| public void addProperty(String propName, String propValue) { |
| mpc.addProperty(propName, propValue); |
| } |
| |
| /** |
| * Adds a property to the sub model, sets its value to {@code propValue}, and its type to {@code propType}. |
| * @param propName to add |
| * @param propValue to set the value |
| * @param propType One of {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addProperty(String propName, String propValue, String propType) { |
| mpc.addProperty(propName, propValue, propType); |
| |
| } |
| |
| /** |
| * Adds a property with name {@code propName} to an existing {@code subModelElementCollection}. |
| * @param propName to add into collection |
| * @param subModelElementCollection name to add into |
| */ |
| public void addPropertyToSubModelElementCollection(String propName, String subModelElementCollection) { |
| mpc.addPropertyToSubModelElementCollection(propName, subModelElementCollection); |
| } |
| |
| public void removePropertyFromSubModelElementCollection(String propName) { |
| mpc.removePropertyFromSubModelElementCollection(propName); |
| } |
| |
| public void removeProperty(String propName) { |
| mpc.removeProperty(propName); |
| } |
| |
| public void updateProperty(String propName, String propValue) { |
| mpc.updateProperty(propName, propValue); |
| } |
| |
| /** |
| * Adds a single input variable to an operation. See {@link #addInputVariables(String, HashMap)} to add multiple variables. |
| * @param opName to add inputs to |
| * @param inputName name of the variable |
| * @param paramType type of the variable. One of the following: |
| * {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addInputVariable(String opName, String inputName, String paramType) { |
| LinkedHashMap<String, String> paramAndType = new LinkedHashMap<String, String>(); |
| paramAndType.put(inputName, paramType); |
| moc.getInputVars().put(opName, paramAndType); |
| System.out.println("Adding single input variable to " + opName + ": Name: " + inputName + ", type: " + paramType); |
| } |
| |
| /** |
| * Adds a single output variable to an operation. See {@link #addOutputVariables(String, HashMap)} to add multiple variables. |
| * @param opName to add outputs to |
| * @param outputName name of the variable |
| * @param paramType type of the variable. One of the following: |
| * {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addOutputVariable(String opName, String outputName, String paramType) { |
| LinkedHashMap<String, String> paramAndType = new LinkedHashMap<String, String>(); |
| paramAndType.put(outputName, paramType); |
| moc.getInputVars().put(opName, paramAndType); |
| System.out.println("Adding single output variable to " + opName + ": Name: " + outputName + ", type: " + paramType); |
| } |
| |
| /** |
| * Adds a single inoutput variable to an operation. See {@link #addInOutputVariables(String, HashMap)} to add multiple variables. |
| * @param opName to add inoutputs to |
| * @param inoutputName name of the variable |
| * @param paramType type of the variable. One of the following: |
| * {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addInOutputVariable(String opName, String inoutputName, String paramType) { |
| LinkedHashMap<String, String> paramAndType = new LinkedHashMap<String, String>(); |
| paramAndType.put(inoutputName, paramType); |
| moc.getInputVars().put(opName, paramAndType); |
| System.out.println("Adding single inoutput variable to " + opName + ": Name: " + inoutputName + ", type: " + paramType); |
| } |
| |
| /** |
| * Adds input variables to an operation. See {@link #addInputVariable(String, String, String)} to add only one. |
| * @param opName to add inputs to |
| * @param inputVariables HashMap of variables with key being the input name and value being the type. |
| * Type can be one of the following: {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addInputVariables(String opName, LinkedHashMap<String, String> inputVariables) { |
| moc.addInputVariables(opName, inputVariables); |
| } |
| |
| /** |
| * Adds output variables to an operation. See {@link #addOutputVariable(String, String, String)} to add only one. |
| * @param opName to add outputs to |
| * @param outputVariables HashMap of variables with key being the output name and value being the type. |
| * Type can be one of the following: {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addOutputVariables(String opName, LinkedHashMap<String, String> outputVariables) { |
| moc.addOutputVariables(opName, outputVariables); |
| } |
| |
| /** |
| * Adds inout variables to an operation. See {@link #addInOutputVariable(String, String, String)} to add only one. |
| * @param opName to add inoutputs to |
| * @param inoutVariables HashMap of variables with key being the inoutput name and value being the type. |
| * Type can be one of the following: {@code Boolean, Collection, Container, Double, Float, Integer, Map, Null, String, Void} |
| */ |
| public void addInOutputVariables(String opName, LinkedHashMap<String, String> inoutputVariables) { |
| moc.addInOutputVariables(opName, inoutputVariables); |
| } |
| |
| /** |
| * Adds an operation to the sub model. |
| * @param opName to add |
| */ |
| public void addOperation(String opName) { |
| moc.addOperation(opName); |
| } |
| |
| /** |
| * Adds an operation with custom code body. Leaving code empty uses default code snippet. |
| * @param opName to add |
| * @param operationCode Java code directly written inside function call. |
| */ |
| public void addOperation(String opName, String operationCode) { |
| moc.addOperation(opName, operationCode); |
| } |
| |
| public void addOperation(String opName, String operationCode, String configKey, String secondValue) { |
| moc.addOperation(opName, operationCode, configKey, secondValue); |
| } |
| |
| /** |
| * Adds an operation with name {@code opName} to an existing {@code subModelElementCollection}. |
| * @param opName to add into collection |
| * @param subModelElementCollection name to add into |
| */ |
| public void addOperationToSubModelElementCollection(String opName, String subModelElementCollection) { |
| moc.addOperationToSubModelElementCollection(opName, subModelElementCollection); |
| } |
| |
| public void removeOperationFromSubModelElementCollection(String propName) { |
| moc.removeOperationFromSubModelElementCollection(propName); |
| } |
| |
| public void removeOperation(String opName) { |
| moc.removeOperation(opName); |
| } |
| |
| public void updateOperation(String opName, String operationCode, String configKey, String secondValue) { |
| moc.updateOperation(opName, operationCode, configKey, secondValue); |
| } |
| |
| /** |
| * Adds a SubModelElementCollection into a sub model. Required to group elements. |
| * @param name to define submodelelementcollection. Also used as shortId. |
| */ |
| public void addSubModelElementCollection(String name) { |
| secs.add(name); |
| System.out.println("Creating subModelElementCollection: " + name); |
| } |
| |
| /** |
| * Adds a File element to the sub model. |
| * @param filePath |
| */ |
| public void addFilePath(String filePath) { |
| addFilePath(filePath, ""); |
| } |
| |
| /** |
| * Adds a File element to the sub model's {@code subModelElementCollection}. If not exists, ignores the |
| * method. |
| * @param filePath |
| * @param subModelElementCollection |
| */ |
| public void addFilePath(String filePath, String subModelElementCollection) { |
| if(!secs.contains(subModelElementCollection)) { |
| System.err.println("SubModelElementCollection assigned to FilePath not found! Ignoring FilePath.\r\n" |
| + "Create it first using addSubModelElementCollection!"); |
| return; |
| } |
| this.filePath = filePath; |
| System.out.println("Adding file path: " + filePath); |
| this.fileSec = subModelElementCollection; |
| } |
| |
| public String getFilePath() { |
| return this.filePath; |
| } |
| |
| /** |
| * Adds {@code subSubModelElementCollection} into {@code parentSubModelElementCollection}. |
| * @param subSubModelElementCollection |
| * @param parentSubModelElementCollection |
| */ |
| public void addCollectionIntoSubModelElementCollection(String subSubModelElementCollection, String parentSubModelElementCollection) { |
| if(!secs.contains(parentSubModelElementCollection)) { |
| System.err.println("Parent subModelElementCollection " + parentSubModelElementCollection + " not found!\r\nCreate it to use as a parent."); |
| return; |
| } |
| subsectosec.put(subSubModelElementCollection, parentSubModelElementCollection); |
| System.out.println("Adding subModelElementCollection " + subSubModelElementCollection + " to " + parentSubModelElementCollection + "."); |
| } |
| |
| |
| } |