Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous fixes for headless legacy #362

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions gama.headless/src/gama/headless/common/HeadLessErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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"};

Expand Down
35 changes: 25 additions & 10 deletions gama.headless/src/gama/headless/runtime/HeadlessApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private static Injector configureInjector() {
/** The Constant THREAD_PARAMETER. */
final public static String THREAD_PARAMETER = "-hpc";

/** The Constant THREAD_PARAMETER. */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ Getting worse: Overall Code Complexity
The mean cyclomatic complexity increases from 4.79 to 5.00, threshold = 4

final public static String STEPS_PARAMETER = "-steps";

/** The Constant PING_INTERVAL. */
final public static String PING_INTERVAL = "-ping_interval";

Expand Down Expand Up @@ -305,6 +308,10 @@ private boolean checkParameters(final List<String> args) {
size = size - 4;
mustContainInFile = mustContainOutFolder = false;
}

if (args.contains(GAML_PARAMETER)) {
size = size - 2;
}
lesquoyb marked this conversation as resolved.
Show resolved Hide resolved

// Runner verification
// ========================
Expand All @@ -319,11 +326,15 @@ private boolean checkParameters(final List<String> 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);
Expand Down Expand Up @@ -658,14 +669,18 @@ public void runBatchSimulation(final String experimentName, final String pathToM
*/
public void runGamlSimulation(final List<String> 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);

Integer numberOfCores =
args.contains(THREAD_PARAMETER) ? Integer.parseInt(after(args, THREAD_PARAMETER)) : null;
final List<IExperimentJob> jb = ExperimentationPlanFactory.buildExperiment(argGamlFile, numberOfCores);

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);

final List<IExperimentJob> jb = ExperimentationPlanFactory.buildExperiment(argGamlFile,numberOfSteps, numberOfCores);

ExperimentJob selectedJob = null;
for (final IExperimentJob j : jb) {
if (j.getExperimentName().equals(argExperimentName)) {
Expand All @@ -674,7 +689,7 @@ public void runGamlSimulation(final List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IExperimentJob> buildExperiment(final String modelFileName, final Integer numberOfCores)
public static List<IExperimentJob> 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);
}

/**
Expand All @@ -184,7 +184,7 @@ public static List<IExperimentJob> buildExperiment(final String modelFileName, f
*/
public static List<IExperimentJob> buildExperiment(final String modelFileName)
throws IOException, GamaCompilationFailedException {
return buildExperiment(modelFileName, null);
return buildExperiment(modelFileName,null, null);
}

/**
Expand Down
11 changes: 8 additions & 3 deletions gama.headless/src/gama/headless/xml/XMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +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.core.*;
import gama.headless.common.DataType;
import gama.headless.job.ExperimentJob;
import gama.headless.job.ListenedVariable;

Expand Down Expand Up @@ -66,7 +69,9 @@ public void close() {
public void writeResultStep(final long step, final ListenedVariable[] vars) {
StringBuilder sb = new StringBuilder().append("\t<Step id='").append(step).append("' >\n");
for ( int i = 0; i < vars.length; i++ ) {
sb.append("\t\t<Variable name='").append(vars[i].getName()).append("' type='").append(vars[i].getDataType().name()).append("'>").append(vars[i].getValue())
sb.append("\t\t<Variable name='").append(vars[i].getName())
.append("' type='").append((vars[i].getDataType() == null ? DataType.UNDEFINED : vars[i].getDataType()).name())
.append("'>").append(vars[i].getValue())
.append("</Variable>\n");
}
sb.append("\t</Step>\n");
Expand Down