Skip to content

Commit 4813ef8

Browse files
author
socadk
committed
JaamSim2MDSL example
1 parent f10f46e commit 4813ef8

28 files changed

+5742
-0
lines changed

dsl-core/io.mdsl/src/io/mdsl/generator/model/composition/views/jaamsim/JaamSimView.java

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ private void convertCommandToComponent(Command command) {
116116
if(command.emitsSingleSimpleEvent()) {
117117
MDSLLogger.reportDetailedInformation("Processing command " + command.getName() + ": case 1.");
118118
handleCase1AndCase4(command);
119+
// new March 7, 2022:
120+
return;
119121
}
120122

121123
// case 2a: single command emitting multiple composed events (DEP step AND)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Setup
2+
3+
This example requires JaamSim, which is does not seem to be available as a Maven artifact.
4+
5+
The following steps will create a local Maven repository for JaamSim:
6+
7+
1. Download the "Universal Jar" from the [JaamSim website](https://jaamsim.com/downloads.html).
8+
1. Run the following command in this directory, replacing `PATH_TO_JAAMSIM.jar` with the location of the downloaded jar: `mvn deploy:deploy-file -DgroupId=com.jaamsim -DartifactId=jaamsim -Dversion=2021-06 -Durl=file:./lib/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=PATH_TO_JAAMSIM.jar`
9+
1. Run `mvn clean verify` to make sure Maven can resolve the JaamSim dependency from the local repository.
10+
11+
# Running the JaamSim to MDSL generator
12+
13+
You can use Maven to convert JaamSim configuration files, typically ending with `.cfg` to MDSL (replace `PATH_TO_JAAMSIM_CONFIG` with a valid path): <!-- file URL or absolute path -->
14+
15+
`mvn compile exec:java -Dexec.arguments="PATH_TO_JAAMSIM_CONFIG"`
16+
17+
Alternatively, `JaamSimToMDSL` can also be run on the command line, it has a main method.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>io.mdsl</groupId>
5+
<artifactId>jaamsim2mdsl</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<properties>
8+
<maven.compiler.source>1.8</maven.compiler.source>
9+
<maven.compiler.target>1.8</maven.compiler.target>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
</properties>
12+
<dependencies>
13+
<dependency>
14+
<groupId>io.mdsl</groupId>
15+
<artifactId>mdsl-core</artifactId>
16+
<version>5.4.6-SNAPSHOT</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>com.jaamsim</groupId>
20+
<artifactId>jaamsim</artifactId>
21+
<version>2021-06</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.junit.jupiter</groupId>
25+
<artifactId>junit-jupiter</artifactId>
26+
<version>5.8.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.codehaus.mojo</groupId>
34+
<artifactId>exec-maven-plugin</artifactId>
35+
<version>3.0.0</version>
36+
<configuration>
37+
<mainClass>io.mdsl.generator.jaamsim.JaamSimToMDSL</mainClass>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
<repositories>
43+
<repository>
44+
<id>project.local</id>
45+
<name>project</name>
46+
<url>file:${project.basedir}/lib</url>
47+
</repository>
48+
</repositories>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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

Comments
 (0)