From 01b9c7dc0dfcdef2a1e0bb86f563bc15d6442e23 Mon Sep 17 00:00:00 2001 From: Baptiste Lesquoy Date: Thu, 24 Oct 2024 14:37:41 +0700 Subject: [PATCH 1/4] Addresses #352 For now I'm just handling the case where the dataType variable is not defined. But if we run headless legacy on Model 12.gaml from model library, we define the displays as 2d explicitly, so shouldn't dataType be 2d display ? And if so, what is the expected dataType in case of 3d display ? --- gama.headless/src/gama/headless/xml/XMLWriter.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gama.headless/src/gama/headless/xml/XMLWriter.java b/gama.headless/src/gama/headless/xml/XMLWriter.java index 3aab8eb409..1ddbb5b374 100644 --- a/gama.headless/src/gama/headless/xml/XMLWriter.java +++ b/gama.headless/src/gama/headless/xml/XMLWriter.java @@ -12,6 +12,7 @@ import java.io.*; +import gama.headless.common.DataType; import gama.headless.core.*; import gama.headless.job.ExperimentJob; import gama.headless.job.ListenedVariable; @@ -66,7 +67,9 @@ public void close() { public void writeResultStep(final long step, final ListenedVariable[] vars) { StringBuilder sb = new StringBuilder().append("\t\n"); for ( int i = 0; i < vars.length; i++ ) { - sb.append("\t\t").append(vars[i].getValue()) + sb.append("\t\t").append(vars[i].getValue()) .append("\n"); } sb.append("\t\n"); From 64f72469d279a203479b42ccf77a5efd0e5867e5 Mon Sep 17 00:00:00 2001 From: Baptiste Lesquoy Date: Wed, 30 Oct 2024 15:33:12 +0700 Subject: [PATCH 2/4] Fixes wrong error message and arrayoutofbound when displaying some headless errors --- .../src/gama/headless/common/HeadLessErrors.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gama.headless/src/gama/headless/common/HeadLessErrors.java b/gama.headless/src/gama/headless/common/HeadLessErrors.java index aec2d249d2..aa17ad179f 100644 --- a/gama.headless/src/gama/headless/common/HeadLessErrors.java +++ b/gama.headless/src/gama/headless/common/HeadLessErrors.java @@ -29,15 +29,15 @@ public abstract class HeadLessErrors { /** The Constant NOT_EXIST_FILE_ERROR. */ public static final int NOT_EXIST_FILE_ERROR = 4; - - /** The Constant HPC_PARAMETER_ERROR. */ - public static final int HPC_PARAMETER_ERROR = 5; +// +// /** The Constant HPC_PARAMETER_ERROR. */ +// public static final int HPC_PARAMETER_ERROR = 5; /** The Constant INPUT_NOT_DEFINED. */ - public static final int INPUT_NOT_DEFINED = 6; + public static final int INPUT_NOT_DEFINED = 5 /* 6 */; /** The Constant OUTPUT_NOT_DEFINED. */ - public static final int OUTPUT_NOT_DEFINED = 7; + public static final int OUTPUT_NOT_DEFINED = 6 /* 7 */; /** The Constant ERRORS. */ private final static String[] ERRORS = @@ -46,7 +46,7 @@ public abstract class HeadLessErrors { "Launching error... try again", "Unable to create directory at #. Check your file permission!", "Unable to create directory at #. Directory already exist!", - "Unable to read input file #. File not exist!", + "Unable to read input file #. File does not exist!", "Input file is not defined", "Output directory is not defined"}; From 4047c13727930e6104f5c72acdded2962b4618db Mon Sep 17 00:00:00 2001 From: Baptiste Lesquoy Date: Wed, 30 Oct 2024 15:34:26 +0700 Subject: [PATCH 3/4] Restores -gaml headless mode and improves error handling in case the output directories cannot be created --- .../headless/runtime/HeadlessApplication.java | 23 +++++++++++++------ .../src/gama/headless/xml/XMLWriter.java | 6 +++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/gama.headless/src/gama/headless/runtime/HeadlessApplication.java b/gama.headless/src/gama/headless/runtime/HeadlessApplication.java index a63e0d308b..fd8b2e580c 100644 --- a/gama.headless/src/gama/headless/runtime/HeadlessApplication.java +++ b/gama.headless/src/gama/headless/runtime/HeadlessApplication.java @@ -305,6 +305,10 @@ private boolean checkParameters(final List args) { size = size - 4; mustContainInFile = mustContainOutFolder = false; } + + if (args.contains(GAML_PARAMETER)) { + size = size - 2; + } // Runner verification // ======================== @@ -319,11 +323,15 @@ private boolean checkParameters(final List args) { // Check and create output folder Globals.OUTPUT_PATH = args.get(args.size() - 1); final File output = new File(Globals.OUTPUT_PATH); - if (!output.exists()) { output.mkdir(); } + if (!output.exists() && !output.mkdir()) { + return showError(HeadLessErrors.PERMISSION_ERROR, Globals.OUTPUT_PATH); + } // Check and create output image folder Globals.IMAGES_PATH = Globals.OUTPUT_PATH + "/snapshot"; final File images = new File(Globals.IMAGES_PATH); - if (!images.exists()) { images.mkdir(); } + if (!images.exists() && !images.mkdir()) { + return showError(HeadLessErrors.PERMISSION_ERROR, Globals.IMAGES_PATH); + } } if (mustContainInFile) { final int inIndex = args.size() - (mustContainOutFolder ? 2 : 1); @@ -658,10 +666,11 @@ public void runBatchSimulation(final String experimentName, final String pathToM */ public void runGamlSimulation(final List args) throws IOException, GamaCompilationFailedException, InterruptedException { - final String pathToModel = args.get(args.size() - 1); - assertIsAModelFile(pathToModel); - final String argExperimentName = args.get(args.size() - 2); - final String argGamlFile = args.get(args.size() - 1); + + final String argExperimentName = args.get(args.size() - 3); + final String argGamlFile = args.get(args.size() - 2); + final String argOutDir = args.get(args.size() - 1); + assertIsAModelFile(argGamlFile); Integer numberOfCores = args.contains(THREAD_PARAMETER) ? Integer.parseInt(after(args, THREAD_PARAMETER)) : null; @@ -674,7 +683,7 @@ public void runGamlSimulation(final List args) } } if (selectedJob == null) return; - Globals.OUTPUT_PATH = args.get(args.size() - 3); + Globals.OUTPUT_PATH = argOutDir; selectedJob.setBufferedWriter(new XMLWriter(Globals.OUTPUT_PATH + "/" + Globals.OUTPUT_FILENAME + ".xml")); processorQueue.setNumberOfThreads(numberOfCores != null ? numberOfCores : SimulationRuntime.DEFAULT_NB_THREADS); diff --git a/gama.headless/src/gama/headless/xml/XMLWriter.java b/gama.headless/src/gama/headless/xml/XMLWriter.java index 1ddbb5b374..eedd44c2c6 100644 --- a/gama.headless/src/gama/headless/xml/XMLWriter.java +++ b/gama.headless/src/gama/headless/xml/XMLWriter.java @@ -10,10 +10,12 @@ ********************************************************************************************************/ package gama.headless.xml; -import java.io.*; +import java.io.BufferedWriter; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; import gama.headless.common.DataType; -import gama.headless.core.*; import gama.headless.job.ExperimentJob; import gama.headless.job.ListenedVariable; From 3eff4736550a0c51a1b1b17cbfa951478c500754 Mon Sep 17 00:00:00 2001 From: Baptiste Lesquoy Date: Wed, 30 Oct 2024 15:59:41 +0700 Subject: [PATCH 4/4] Adds a steps argument for gaml headless mode --- .../gama/headless/runtime/HeadlessApplication.java | 12 +++++++++--- .../headless/script/ExperimentationPlanFactory.java | 8 ++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/gama.headless/src/gama/headless/runtime/HeadlessApplication.java b/gama.headless/src/gama/headless/runtime/HeadlessApplication.java index fd8b2e580c..5c437ea5a2 100644 --- a/gama.headless/src/gama/headless/runtime/HeadlessApplication.java +++ b/gama.headless/src/gama/headless/runtime/HeadlessApplication.java @@ -121,6 +121,9 @@ private static Injector configureInjector() { /** The Constant THREAD_PARAMETER. */ final public static String THREAD_PARAMETER = "-hpc"; + /** The Constant THREAD_PARAMETER. */ + final public static String STEPS_PARAMETER = "-steps"; + /** The Constant PING_INTERVAL. */ final public static String PING_INTERVAL = "-ping_interval"; @@ -667,14 +670,17 @@ public void runBatchSimulation(final String experimentName, final String pathToM public void runGamlSimulation(final List args) throws IOException, GamaCompilationFailedException, InterruptedException { + final String argExperimentName = args.get(args.size() - 3); final String argGamlFile = args.get(args.size() - 2); final String argOutDir = args.get(args.size() - 1); + final Integer numberOfSteps = args.contains(STEPS_PARAMETER) ? Integer.parseInt(after(args, STEPS_PARAMETER)) : null; + final Integer numberOfCores = args.contains(THREAD_PARAMETER) ? Integer.parseInt(after(args, THREAD_PARAMETER)) : null; + assertIsAModelFile(argGamlFile); - Integer numberOfCores = - args.contains(THREAD_PARAMETER) ? Integer.parseInt(after(args, THREAD_PARAMETER)) : null; - final List jb = ExperimentationPlanFactory.buildExperiment(argGamlFile, numberOfCores); + final List jb = ExperimentationPlanFactory.buildExperiment(argGamlFile,numberOfSteps, numberOfCores); + ExperimentJob selectedJob = null; for (final IExperimentJob j : jb) { if (j.getExperimentName().equals(argExperimentName)) { diff --git a/gama.headless/src/gama/headless/script/ExperimentationPlanFactory.java b/gama.headless/src/gama/headless/script/ExperimentationPlanFactory.java index 1f5249a585..5855a3aeed 100644 --- a/gama.headless/src/gama/headless/script/ExperimentationPlanFactory.java +++ b/gama.headless/src/gama/headless/script/ExperimentationPlanFactory.java @@ -158,17 +158,17 @@ public class ExperimentationPlanFactory { * @param modelFileName * the model file name * @param numberOfCores - * the number of cores passed throuhg the '-hpc' parameter. If null, means that '-hpc' is not set + * the number of cores passed through the '-hpc' parameter. If null, means that '-hpc' is not set * @return the list * @throws IOException * Signals that an I/O exception has occurred. * @throws GamaHeadlessException * the gama headless exception */ - public static List buildExperiment(final String modelFileName, final Integer numberOfCores) + public static List buildExperiment(final String modelFileName, final Integer numberOfSteps, final Integer numberOfCores) throws IOException, GamaCompilationFailedException { final long[] seeds = { DEFAULT_SEED }; - return JobListFactory.constructAllJobs(modelFileName, seeds, DEFAULT_FINAL_STEP, numberOfCores); + return JobListFactory.constructAllJobs(modelFileName, seeds, numberOfSteps == null ? DEFAULT_FINAL_STEP : numberOfSteps, numberOfCores); } /** @@ -184,7 +184,7 @@ public static List buildExperiment(final String modelFileName, f */ public static List buildExperiment(final String modelFileName) throws IOException, GamaCompilationFailedException { - return buildExperiment(modelFileName, null); + return buildExperiment(modelFileName,null, null); } /**