diff --git a/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java b/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java index 4f0f6fd35a2..07824aee849 100644 --- a/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java +++ b/contribs/application/src/main/java/org/matsim/application/ApplicationUtils.java @@ -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")) diff --git a/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java b/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java index 97722ab2e93..6a3b0ef9e04 100644 --- a/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java +++ b/contribs/application/src/main/java/org/matsim/application/MATSimApplication.java @@ -70,6 +70,8 @@ public abstract class MATSimApplication implements Callable, 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 + @@ -202,9 +204,7 @@ public Integer call() throws Exception { } catch (Exception e) { log.error("Error running post-processing", e); } - } - } @@ -337,6 +337,12 @@ public static void run(Class 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); @@ -369,12 +375,26 @@ public static void run(Class clazz, String... args) public static void runWithDefaults(Class 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)); } - } /** @@ -670,7 +690,7 @@ public Integer call() throws Exception { } /** - * Option to switch post processing behavour + * Option to switch post-processing behaviour */ public enum PostProcessOption { diff --git a/matsim/src/main/java/org/matsim/run/gui/ExeRunner.java b/matsim/src/main/java/org/matsim/run/gui/ExeRunner.java index 8e2f94c3109..7c1b75eac56 100644 --- a/matsim/src/main/java/org/matsim/run/gui/ExeRunner.java +++ b/matsim/src/main/java/org/matsim/run/gui/ExeRunner.java @@ -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)); }