| /******************************************************************************* |
| * 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.List; |
| |
| import org.eclipse.aas.basyx.codegen.util.Project; |
| /** |
| * Creates a monitor that keeps the list of sub models. |
| * @author DFKI |
| * |
| */ |
| public class SubModelCreator { |
| |
| List<SubModel> submodels = new ArrayList<SubModel>(); |
| |
| /** |
| * Adds a sub model into the list. Used only in UI version of the API. See {@link #addSubModel(SubModel)}. |
| * @param subModel |
| * @param mpc |
| * @param moc |
| */ |
| public void addSubModel(String subModel, SubModelPropCollector mpc, SubModelOperationCollector moc) { |
| SubModel m = new SubModel(subModel, mpc, moc); |
| submodels.add(m); |
| } |
| |
| /** |
| * Adds a sub model into the list. |
| * @param subModel to add |
| */ |
| public void addSubModel(SubModel subModel) { |
| submodels.add(subModel); |
| } |
| |
| public SubModel getSubModel(String subModelName) { |
| for (SubModel submodel : submodels) { |
| if(submodel.getName().equals(subModelName)) { |
| return submodel; |
| } |
| } |
| return null; |
| } |
| |
| public List<SubModel> getSubModels() { |
| return submodels; |
| } |
| |
| public List<String> getSubModelElementCollections(String subModelName) { |
| return getSubModel(subModelName).getSubModelElementCollections(); |
| } |
| |
| /** |
| * Alias for {@link #removeSubModel(String)}. |
| * @param subModelName |
| */ |
| public void deleteSubModel(String subModelName) { |
| removeSubModel(subModelName); |
| } |
| |
| /** |
| * Remove the submodel given with {@code modelName}. |
| * @param subModelName to remove. |
| */ |
| public void removeSubModel(String subModelName) { |
| for(SubModel model : submodels) { |
| if(model.getName().contentEquals(subModelName)) { |
| submodels.remove(model); |
| break; |
| } |
| } |
| } |
| |
| /** |
| * Clears all submodels. Use with care. |
| */ |
| public void removeAllSubModels() { |
| submodels.clear(); |
| } |
| |
| public String createSubModelFile(String subModelName, Project projectOperations) { |
| try { |
| SubModel submodel = getSubModel(subModelName); |
| SubModelOperationCollector moc = submodel.getMoc(); |
| SubModelPropCollector mpc = submodel.getMpc(); |
| String className = subModelName.replace(" ", ""); |
| String text = "package " + projectOperations.getNamespace() + ".module.submodel;\r\n" + |
| "\r\nimport java.util.function.Function;\r\n" + |
| "import java.util.ArrayList;\r\n" + |
| "import java.util.Collection;\r\n" + |
| "import " + projectOperations.getNamespace() + ".connection.ConnectedDevice;\r\n" + |
| "import " + projectOperations.getNamespace() + ".connection.DataCrawler;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.submodelelement.operation.Operation;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.submodelelement.SubmodelElementCollection;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.valuetypedef.PropertyValueTypeDef;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.File;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElement;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.SubModel;\r\n" + |
| "import org.eclipse.basyx.submodel.metamodel.map.submodelelement.operation.OperationVariable;\r\n" + |
| "\r\n" + |
| "/**\r\n" + |
| " * \r\n" + |
| " * @author DFKI\r\n" + |
| " * \r\n" + |
| " */\r\n" + |
| "\r\n" + |
| "public class " + className + " extends SubModel {\r\n" + |
| "\r\n" + |
| " protected ConnectedDevice connectedDevice;\r\n" + |
| "\r\n" + |
| " public " + className + "(ConnectedDevice connDev) {\r\n" + |
| "\r\n" + |
| " connectedDevice = connDev;\r\n" + |
| " setIdShort(\"" + subModelName + "\");\r\n" + |
| " //DataCrawler DataCrawler = new DataCrawler(connectedDevice);\r\n" + |
| " \r\n"; |
| text += createSubModelElementCollections(subModelName); |
| text += createFilePath(submodel, submodel.getFileSec()); |
| text += moc.prepareAllOperations(className, submodel); |
| text += mpc.prepareAllProperties(submodel); |
| |
| text += createSubModelElementCollectionLinks(subModelName); |
| text += createCollectionIntoSubModelCollection(subModelName); |
| text += createSubModelElementCollectionLinksParent(subModelName); |
| text += createSubModelElementCollectionSubModelElement(subModelName); |
| text += " }\r\n" + |
| " \r\n" + |
| "}\r\n" + |
| ""; |
| return text; |
| } |
| catch (Exception e) { |
| e.printStackTrace(); |
| return "Model cannot be found!"; |
| } |
| } |
| |
| private String createSubModelElementCollectionLinks(String subModelName) { |
| List<String> secs = getSubModel(subModelName).getSubModelElementCollections(); |
| String text = ""; |
| for (String sec : secs) { |
| if(getSubModel(subModelName).getSubModelElementCollectionMap().containsKey(sec)) { |
| // only add to submodel if not defined as subsub EC |
| text += " " + sec + ".setValue(" + sec + "value);\r\n" + |
| " " + sec + ".setIdShort(\"" + sec + "\");\r\n"; |
| } |
| |
| } |
| return text; |
| } |
| |
| private String createSubModelElementCollectionLinksParent(String subModelName) { |
| List<String> secs = getSubModel(subModelName).getSubModelElementCollections(); |
| String text = ""; |
| for (String sec : secs) { |
| if(!getSubModel(subModelName).getSubModelElementCollectionMap().containsKey(sec)) { |
| // only add to submodel if not defined as subsub EC |
| text += " " + sec + ".setValue(" + sec + "value);\r\n" + |
| " " + sec + ".setIdShort(\"" + sec + "\");\r\n"; |
| } |
| |
| } |
| return text; |
| } |
| |
| private String createSubModelElementCollectionSubModelElement(String subModelName) { |
| List<String> secs = getSubModel(subModelName).getSubModelElementCollections(); |
| String text = ""; |
| for (String sec : secs) { |
| if(!getSubModel(subModelName).getSubModelElementCollectionMap().containsKey(sec)) { |
| // only add to submodel if not defined as subsub EC |
| text += " addSubModelElement(" + sec + ");\r\n\r\n"; |
| } |
| |
| } |
| return text; |
| } |
| |
| private String createSubModelElementCollections(String subModelName) { |
| List<String> secs = getSubModel(subModelName).getSubModelElementCollections(); |
| String text = ""; |
| for (String sec : secs) { |
| text += " SubmodelElementCollection " + sec + " = new SubmodelElementCollection();\r\n" + |
| " Collection<ISubmodelElement> " + sec + "value = new ArrayList<ISubmodelElement>();\r\n"; |
| } |
| |
| return text; |
| } |
| |
| private String createCollectionIntoSubModelCollection(String subModelName) { |
| String text = ""; |
| HashMap<String, String> secollection = getSubModel(subModelName).getSubModelElementCollectionMap(); |
| // key = subsubelement collection, value = submodel element collection |
| for (String subsubelementcollection : secollection.keySet()) { |
| if(!getSubModel(subModelName).getSubModelElementCollections().contains(secollection.get(subsubelementcollection))) { |
| // subsubEC not defined in submodel |
| System.out.println(subModelName + " sub model does not have collection " + secollection.get(subsubelementcollection) + ". Creating."); |
| getSubModel(subModelName).addSubModelElementCollection(secollection.get(subsubelementcollection)); |
| } |
| text += " " + secollection.get(subsubelementcollection) + "value.add(" + subsubelementcollection + ");\r\n\r\n"; |
| |
| |
| } |
| |
| |
| return text; |
| } |
| |
| private String createFilePath(SubModel subModel, String subModelCollection) { |
| String text = ""; |
| if(subModel.getFilePath().length() > 0) { |
| text += " File file = new File();\r\n" + |
| " String filePath = \"" + subModel.getFilePath() + "\";\r\n" + |
| " file.setValue(filePath);\r\n"; |
| if(subModelCollection.length() > 0) { |
| text += " " + subModelCollection + "value.add(file);\r\n\r\n"; |
| } |
| else { |
| text += " addSubModelElement(file);\r\n\r\n"; |
| } |
| } |
| return text; |
| } |
| } |