|
| 1 | +package io.mdsl.generator.jaamsim; |
| 2 | + |
| 3 | +/* |
| 4 | +
|
| 5 | + * Copyright 2020 The MDSL Project Team |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.URISyntaxException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import com.jaamsim.basicsim.JaamSimModel; |
| 28 | +import com.jaamsim.basicsim.Entity; |
| 29 | +import com.jaamsim.input.Input; |
| 30 | + |
| 31 | +import io.mdsl.utils.MDSLLogger; |
| 32 | + |
| 33 | +public class JaamSimConfigurationWrapper { |
| 34 | + private static final String NEXT_COMPONENT_KEYWORD = "NextComponent"; |
| 35 | + |
| 36 | + private String configFileName = null; |
| 37 | + private JaamSimModel jsm = null; |
| 38 | + |
| 39 | + public JaamSimConfigurationWrapper(String pathToConfigFile) { |
| 40 | + super(); |
| 41 | + this.configFileName = pathToConfigFile; |
| 42 | + try { |
| 43 | + initializeJSM(pathToConfigFile); |
| 44 | + } catch (URISyntaxException e) { |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + // ** JaamSim config file access (file/string based) |
| 51 | + |
| 52 | + public String findModelNameInConfigFile() throws IOException { |
| 53 | + ArrayList<String> overlayTexts = this.findObjectDefinitionInConfigFile("OverlayText"); |
| 54 | + if(overlayTexts.size()!=1) { |
| 55 | + throw new IllegalArgumentException("There should be one and only one OverlayText entity"); |
| 56 | + } |
| 57 | + String formatInput = this.findInputString(overlayTexts.get(0), "Format"); |
| 58 | + return formatInput.substring(1, formatInput.length()-1); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public List<String> serversWithThisObjectAsNextComponent(String branchName) throws IOException { |
| 63 | + // no suited API operation, so direct, custom access to config file |
| 64 | + ArrayList<String> result = new ArrayList<String>(); |
| 65 | + |
| 66 | + List<String> jaamSimConfiguration = Files.readAllLines(Paths.get(this.configFileName)); |
| 67 | + for(String configFileEntry : jaamSimConfiguration) { |
| 68 | + if(configFileEntry.contains(branchName)&&configFileEntry.contains(NEXT_COMPONENT_KEYWORD)) { |
| 69 | + // is relevant and is a reference not the definition of branch name entity |
| 70 | + if(!configFileEntry.startsWith("Define")&&!configFileEntry.startsWith(branchName)) { |
| 71 | + result.add(configFileEntry.substring(0, configFileEntry.indexOf(" "))); |
| 72 | + // System.out.println("[I] Added: " + configFileEntry.substring(0, configFileEntry.indexOf(" "))); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + public String findFirstDefinitionInConfigFile(String entityTypeKeyword) throws IOException { |
| 81 | + String result = ""; |
| 82 | + List<String> jaamSimConfiguration = Files.readAllLines(Paths.get(this.configFileName)); |
| 83 | + for(String configFileEntry : jaamSimConfiguration) { |
| 84 | + if(configFileEntry.startsWith(entityTypeKeyword)) { |
| 85 | + int firstQuotePos = configFileEntry.indexOf("'"); |
| 86 | + int lastQuotePos = configFileEntry.lastIndexOf("'"); |
| 87 | + result = configFileEntry.substring(firstQuotePos+1, lastQuotePos); |
| 88 | + } |
| 89 | + } |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + public ArrayList<String> findObjectDefinitionInConfigFile(String modelElementKeyword) throws IOException { |
| 94 | + ArrayList<String> result = new ArrayList<String>(); |
| 95 | + modelElementKeyword = "Define " + modelElementKeyword; |
| 96 | + List<String> jaamSimConfiguration = Files.readAllLines(Paths.get(this.configFileName)); |
| 97 | + for(String configFileEntry : jaamSimConfiguration) { |
| 98 | + // assuming that definitions start in line 1 of config file: |
| 99 | + if(configFileEntry.startsWith(modelElementKeyword)) { |
| 100 | + int offset = modelElementKeyword.length() + 2; |
| 101 | + String servers = configFileEntry.substring(offset, configFileEntry.length()-1); // could also use lastIndexOf } |
| 102 | + String[] modelObjectList = servers.split(" "); |
| 103 | + for(int i=0; i<modelObjectList.length;i++) { |
| 104 | + if(modelObjectList[i]!=null&&!modelObjectList[i].equals("")&&!modelObjectList[i].equals(" ")) { |
| 105 | + if(!modelObjectList[i].equals("{") && !modelObjectList[i].equals("}")) { |
| 106 | + result.add(modelObjectList[i]); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + public List<String> findAttributeValueInEntity(String objectName, String attribute) { |
| 116 | + Entity entity = jsm.getEntity(objectName); |
| 117 | + |
| 118 | + if(entity==null) { |
| 119 | + throw new IllegalArgumentException("Object entity " + objectName + " not found in simulation configuration."); |
| 120 | + } |
| 121 | + |
| 122 | + Input<?> inputField = entity.getInput(attribute); |
| 123 | + |
| 124 | + if(inputField==null) { |
| 125 | + // System.out.println("[D] Object entity " + joinName + " does not have an attribute " + attribute); |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + return inputField.getValueTokens(); |
| 130 | + } |
| 131 | + |
| 132 | + public List<String> findWaitQueueListOfEntity(String objectName) { |
| 133 | + Entity entity = jsm.getEntity(objectName); |
| 134 | + |
| 135 | + if(entity==null) { |
| 136 | + throw new IllegalArgumentException("Object entity " + objectName + " not found in simulation configuration."); |
| 137 | + } |
| 138 | + |
| 139 | + Input<?> inputField = entity.getInput("WaitQueueList"); |
| 140 | + |
| 141 | + if(inputField==null) { |
| 142 | + throw new IllegalArgumentException("Object entity " + objectName + " does not have NextComponentList input."); |
| 143 | + } |
| 144 | + |
| 145 | + return inputField.getValueTokens(); |
| 146 | + } |
| 147 | + |
| 148 | + // ** JaamSim API access |
| 149 | + |
| 150 | + public void initializeJSM(String pathToConfigFile) throws URISyntaxException { |
| 151 | + if(jsm==null) { |
| 152 | + jsm = new JaamSimModel(); |
| 153 | + jsm.autoLoad(); |
| 154 | + File jaamSimModelConfiguration = new File (pathToConfigFile); // must be a URI or an absolute path |
| 155 | + jsm.configure(jaamSimModelConfiguration); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public String getSimulationObjectType(String configurationElement) { |
| 160 | + Entity entity = jsm.getEntity(configurationElement); |
| 161 | + if(entity==null) { |
| 162 | + System.err.println("[W] getSimulationObjectType: Entity not found in configuration: " + configurationElement); |
| 163 | + return null; |
| 164 | + } |
| 165 | + return entity.getObjectType().getName(); |
| 166 | + } |
| 167 | + |
| 168 | + public String findInputString(String elementName, String input) { |
| 169 | + Entity entity = jsm.getEntity(elementName); |
| 170 | + if(entity==null) { |
| 171 | + throw new IllegalArgumentException("Did not find an entity " + elementName); |
| 172 | + } |
| 173 | + Input<?> inputValue = entity.getInput(input); |
| 174 | + if(inputValue==null) { |
| 175 | + throw new IllegalArgumentException("Did not find an input value " + input + " in " + elementName); |
| 176 | + } |
| 177 | + return trimTitle(inputValue.getValueString()); |
| 178 | + } |
| 179 | + |
| 180 | + public List<String> findNextComponentsOfEntity(String simObject) { |
| 181 | + Entity entity = jsm.getEntity(simObject); |
| 182 | + |
| 183 | + if(entity==null) { |
| 184 | + throw new IllegalArgumentException("findNextComponentsOfEntity: entity " + simObject + " not found in simulation configuration."); |
| 185 | + } |
| 186 | + |
| 187 | + Input<?> inputField = entity.getInput("NextComponentList"); |
| 188 | + |
| 189 | + if(inputField==null) { |
| 190 | + throw new IllegalArgumentException("Object entity " + simObject + " does not have NextComponentList input."); |
| 191 | + } |
| 192 | + |
| 193 | + return inputField.getValueTokens(); |
| 194 | + } |
| 195 | + |
| 196 | + public List<String> findTargetComponentListOfEntity(String modelElement) { |
| 197 | + Entity entity = jsm.getEntity(modelElement); |
| 198 | + |
| 199 | + if(entity==null) { |
| 200 | + throw new IllegalArgumentException("Object entity " + modelElement + " not found in simulation configuration."); |
| 201 | + } |
| 202 | + |
| 203 | + Input<?> inputField = entity.getInput("TargetComponentList"); |
| 204 | + |
| 205 | + if(inputField==null) { |
| 206 | + throw new IllegalArgumentException("Object entity " + modelElement + " does not have TargetComponentList input."); |
| 207 | + } |
| 208 | + |
| 209 | + return inputField.getValueTokens(); |
| 210 | + } |
| 211 | + |
| 212 | + public String findNextComponentOfEntity(String modelElement) { |
| 213 | + Entity entity = jsm.getEntity(modelElement); |
| 214 | + |
| 215 | + if(entity==null) { |
| 216 | + throw new IllegalArgumentException("Object entity " + modelElement + " not found in simulation configuration."); |
| 217 | + } |
| 218 | + |
| 219 | + Input<?> inputField = entity.getInput(NEXT_COMPONENT_KEYWORD); |
| 220 | + |
| 221 | + if(inputField==null) { |
| 222 | + throw new IllegalArgumentException("Object entity " + modelElement + " does not have NextComponent input."); |
| 223 | + } |
| 224 | + |
| 225 | + if(inputField.getValueTokens().size()==0) { |
| 226 | + MDSLLogger.reportError("No value object found for " + modelElement); |
| 227 | + } |
| 228 | + else if (inputField.getValueTokens().size()>1) { |
| 229 | + MDSLLogger.reportWarning("More than one value object found for " + modelElement + ", picking first"); |
| 230 | + } |
| 231 | + |
| 232 | + return inputField.getValueTokens().get(0); |
| 233 | + } |
| 234 | + |
| 235 | + // TODO merge previous and next method (String vs. List<String>), previous one could call next one, or client changed |
| 236 | + |
| 237 | + public List<String> findNextComponentsFollowingEntity(String simObject) { |
| 238 | + Entity entity = jsm.getEntity(simObject); |
| 239 | + |
| 240 | + if(entity==null) { |
| 241 | + throw new IllegalArgumentException("Object entity " + simObject + " not found in simulation configuration."); |
| 242 | + } |
| 243 | + |
| 244 | + Input<?> inputField = entity.getInput(NEXT_COMPONENT_KEYWORD); |
| 245 | + |
| 246 | + if(inputField==null) { |
| 247 | + throw new IllegalArgumentException("Object entity " + simObject + " does not have NextComponent input."); |
| 248 | + } |
| 249 | + |
| 250 | + return inputField.getValueTokens(); |
| 251 | + } |
| 252 | + |
| 253 | + // TODO ADL helper |
| 254 | + |
| 255 | + // TODO COL support |
| 256 | + |
| 257 | + // ** misc helpers |
| 258 | + |
| 259 | + static public String trimTitle(String text) { |
| 260 | + return text.replace("Title", "").replace("-", "_").replace(" ", "_").trim(); |
| 261 | + } |
| 262 | + |
| 263 | + static public String normalizeName(String name) { |
| 264 | + String result = name.replaceAll("[&()]", "_"); |
| 265 | + return result.replace('?', '_').replace('+', '_').replace('-', '_'); |
| 266 | + } |
| 267 | +} |
0 commit comments