Skip to content

Commit

Permalink
implement methods to run automatically from gui
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Feb 10, 2024
1 parent 0a10fe3 commit 1561282
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public static String[] mergeArgs(String[] args, String... defaultArgs) {
*/
public static boolean isRunFromDesktop() {

// check if gui was explicitly enabled
String env = System.getenv().getOrDefault("RUN_GUI", "false");
if (env.equalsIgnoreCase("true") || env.equals("1") )
return true;

String property = System.getProperty("RUN_GUI", "false");
if (property.equalsIgnoreCase("true") || property.equals("1") )
return true;

String macIdentifier = System.getenv().getOrDefault("__CFBundleIdentifier", "none");

if (macIdentifier.equals("com.apple.java.JarLauncher") || macIdentifier.equals("com.apple.JavaLauncher"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public abstract class MATSimApplication implements Callable<Integer>, CommandLin

private static final Logger log = LogManager.getLogger(MATSimApplication.class);

private static final String ARGS_DELIMITER = "§$";

public static final String COLOR = "@|bold,fg(81) ";
static final String DEFAULT_NAME = "MATSimApplication";
static final String HEADER = COLOR +
Expand Down Expand Up @@ -202,9 +204,7 @@ public Integer call() throws Exception {
} catch (Exception e) {
log.error("Error running post-processing", e);
}

}

}


Expand Down Expand Up @@ -337,6 +337,12 @@ public static void run(Class<? extends MATSimApplication> clazz, String... args)
l.addAll(Arrays.asList(args));

args = l.toArray(new String[0]);

// Pass stored args to the instance as well
if (System.getenv().containsKey("MATSIM_GUI_ARGS")) {
String[] guiArgs = System.getenv("MATSIM_GUI_ARGS").split(ARGS_DELIMITER);
args = ApplicationUtils.mergeArgs(args, guiArgs);
}
}

prepareArgs(args);
Expand Down Expand Up @@ -369,12 +375,26 @@ public static void run(Class<? extends MATSimApplication> clazz, String... args)
public static void runWithDefaults(Class<? extends MATSimApplication> clazz, String[] args, String... defaultArgs) {

if (ApplicationUtils.isRunFromDesktop()) {
// TODO: implement
// runInGui(clazz, args, defaultArgs);

String value = String.join(ARGS_DELIMITER, defaultArgs);
System.getenv().put("MATSIM_GUI_ARGS", value);

run(clazz, "gui");

} else {
// run if no other command is present
if (args.length > 0) {
if (args[0].equals("run") || args[0].equals("prepare") || args[0].equals("analysis") || args[0].equals("gui") ){
// valid command is present
} else
// Automatically add run command
args = ApplicationUtils.mergeArgs(new String[]{"run"}, defaultArgs);
} else
// Automatically add run command
args = ApplicationUtils.mergeArgs(new String[]{"run"}, defaultArgs);

run(clazz, ApplicationUtils.mergeArgs(args, defaultArgs));
}

}

/**
Expand Down Expand Up @@ -670,7 +690,7 @@ public Integer call() throws Exception {
}

/**
* Option to switch post processing behavour
* Option to switch post-processing behaviour
*/
public enum PostProcessOption {

Expand Down
7 changes: 7 additions & 0 deletions matsim/src/main/java/org/matsim/run/gui/ExeRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public void killProcess() {
public void run() {
var processBuilder = new ProcessBuilder();
processBuilder.environment().put("MATSIM_GUI", "true"); // add "MATSIM_GUI" to the inherited vars

// Copy the MATSIM_GUI_ARGS environment variable to the process environment
// these arguments may be used internally by the matsim scenario
if (System.getenv().containsKey("MATSIM_GUI_ARGS")) {
processBuilder.environment().put("MATSIM_GUI_ARGS", System.getenv("MATSIM_GUI_ARGS"));
}

if (workingDirectory != null) {
processBuilder.directory(new File(workingDirectory));
}
Expand Down

0 comments on commit 1561282

Please sign in to comment.