Skip to content

Commit

Permalink
Continue addressing #3096. Simmplification of some headless operations.
Browse files Browse the repository at this point in the history
Initialization of headless mode is made sooner.
  • Loading branch information
AlexisDrogoul committed May 5, 2021
1 parent 9924772 commit d07c657
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 112 deletions.
Expand Up @@ -13,6 +13,7 @@
import msi.gama.precompiler.GamlAnnotations.symbol;
import msi.gama.precompiler.IConcept;
import msi.gama.precompiler.ISymbolKind;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gaml.descriptions.IDescription;
Expand Down Expand Up @@ -53,8 +54,7 @@ public StartSimulation(final IDescription desc) {
}

private String retrieveModelFileAbsolutePath(final IScope scope, final String filename) {
if (filename.charAt(0) == '/')
return filename;
if (filename.charAt(0) == '/') return filename;
return new File(scope.getModel().getFilePath()).getParentFile().getAbsolutePath() + "/" + filename;
}

Expand All @@ -70,14 +70,13 @@ protected Object privateExecuteIn(final IScope scope) throws GamaRuntimeExceptio
modelPath = scope.getModel().getFilePath();
}

if (this.hasFacet(IKeywords.WITHSEED))
seed = Cast.asInt(scope, getFacetValue(scope, IKeywords.WITHSEED));
if (this.hasFacet(IKeywords.WITHSEED)) { seed = Cast.asInt(scope, getFacetValue(scope, IKeywords.WITHSEED)); }

final long lseed = seed;

IModel mdl = null;
try {
mdl = HeadlessSimulationLoader.loadModel(new File(modelPath));
mdl = HeadlessSimulationLoader.loadModel(new File(modelPath), null, null, GAMA.isInHeadLessMode());
} catch (final IOException e) {
throw GamaRuntimeException.error("Sub model file not found!", scope);
} catch (final GamaHeadlessException e) {
Expand Down
Expand Up @@ -103,7 +103,7 @@ public static synchronized IModel loadModel(final File myFile) throws IOExceptio
*/
public static synchronized IModel loadModel(final File myFile, final List<GamlCompilationError> errors)
throws IOException, GamaHeadlessException {
return loadModel(myFile, errors, null);
return loadModel(myFile, errors, null, true);
}

/**
Expand All @@ -123,8 +123,10 @@ public static synchronized IModel loadModel(final File myFile, final List<GamlCo
* in case the compilation ends in error
*/
public static synchronized IModel loadModel(final File myFile, final List<GamlCompilationError> errors,
final GamlProperties metaProperties) throws IOException, GamaHeadlessException {
preloadGAMA(); // make sure the injector is created
final GamlProperties metaProperties, final boolean initHeadless) throws IOException, GamaHeadlessException {
if (initHeadless) {
preloadGAMA(); // make sure the injector is created.
}
if (myFile == null) throw new IOException("Model file is null");
final String fileName = myFile.getAbsolutePath();
if (!myFile.exists()) throw new IOException("Model file does not exist: " + fileName);
Expand Down
Expand Up @@ -44,7 +44,7 @@ public static IModel loadModel(final File modelPath, final List<GamlCompilationE

public static IModel loadModel(final File modelPath, final List<GamlCompilationError> errors,
final GamlProperties metadata) throws IOException, GamaHeadlessException {
return HeadlessSimulationLoader.loadModel(modelPath, errors, metadata);
return HeadlessSimulationLoader.loadModel(modelPath, errors, metadata, true);
}

public static IMoleExperiment newExperiment(final IModel model) {
Expand Down
41 changes: 14 additions & 27 deletions msi.gama.headless/src/msi/gama/headless/runtime/Application.java
Expand Up @@ -126,12 +126,8 @@ private boolean checkParameters(final List<String> args) {
size = size - 1;
mustContainOutFile = false;
}
if (args.contains(THREAD_PARAMETER)) {
size = size - 2;
}
if (args.contains(VERBOSE_PARAMETER)) {
size = size - 1;
}
if (args.contains(THREAD_PARAMETER)) { size = size - 2; }
if (args.contains(VERBOSE_PARAMETER)) { size = size - 1; }
if (mustContainInFile && mustContainOutFile && size < 2) {
showError(HeadLessErrors.INPUT_NOT_DEFINED, null);
return false;
Expand All @@ -146,13 +142,9 @@ private boolean checkParameters(final List<String> args) {
Globals.OUTPUT_PATH = args.get(outIndex);
Globals.IMAGES_PATH = Globals.OUTPUT_PATH + "/snapshot";
final File output = new File(Globals.OUTPUT_PATH);
if (!output.exists()) {
output.mkdir();
}
if (!output.exists()) { output.mkdir(); }
final File images = new File(Globals.IMAGES_PATH);
if (!images.exists()) {
images.mkdir();
}
if (!images.exists()) { images.mkdir(); }
}

if (mustContainInFile) {
Expand All @@ -177,6 +169,7 @@ private static boolean showError(final int errorCode, final String path) {
@Override
public Object start(final IApplicationContext context) throws Exception {

HeadlessSimulationLoader.preloadGAMA();
DEBUG.OFF();

final Map<String, String[]> mm = context.getArguments();
Expand All @@ -188,13 +181,13 @@ public Object start(final IApplicationContext context) throws Exception {
DEBUG.LOG(showHelp());
DEBUG.OFF();

} else if (args.contains(RUN_LIBRARY_PARAMETER)) {
} else if (args.contains(RUN_LIBRARY_PARAMETER))
return ModelLibraryRunner.getInstance().start(args);
} else if (args.contains(VALIDATE_LIBRARY_PARAMETER)) {
else if (args.contains(VALIDATE_LIBRARY_PARAMETER))
return ModelLibraryValidator.getInstance().start(args);
} else if (args.contains(TEST_LIBRARY_PARAMETER)) {
else if (args.contains(TEST_LIBRARY_PARAMETER))
return ModelLibraryTester.getInstance().start(args);
} else if (args.contains(CHECK_MODEL_PARAMETER)) {
else if (args.contains(CHECK_MODEL_PARAMETER)) {
ModelLibraryGenerator.start(this, args);
} else if (args.contains(BUILD_XML_PARAMETER)) {
buildXML(args);
Expand All @@ -205,9 +198,9 @@ public Object start(final IApplicationContext context) throws Exception {
}

public String after(final List<String> args, final String arg) {
if (args == null || args.size() < 2) { return null; }
if (args == null || args.size() < 2) return null;
for (int i = 0; i < args.size() - 1; i++) {
if (args.get(i).equals(arg)) { return args.get(i + 1); }
if (args.get(i).equals(arg)) return args.get(i + 1);
}
return null;
}
Expand All @@ -226,7 +219,7 @@ public void buildXML(final List<String> arg)
DEBUG.ERR(showHelp());
return;
}
HeadlessSimulationLoader.preloadGAMA();

final List<IExperimentJob> jb = ExperimentationPlanFactory.buildExperiment(arg.get(arg.size() - 2));
final ArrayList<IExperimentJob> selectedJob = new ArrayList<>();
for (final IExperimentJob j : jb) {
Expand All @@ -250,7 +243,6 @@ public void buildXML(final List<String> arg)
public void buildXMLForModelLibrary(final ArrayList<File> modelPaths, final String outputPath)
throws ParserConfigurationException, TransformerException, IOException, GamaHeadlessException {
// "arg[]" are the paths to the different models
HeadlessSimulationLoader.preloadGAMA();
final ArrayList<IExperimentJob> selectedJob = new ArrayList<>();
for (final File modelFile : modelPaths) {
final List<IExperimentJob> jb = ExperimentationPlanFactory.buildExperiment(modelFile.getAbsolutePath());
Expand Down Expand Up @@ -289,16 +281,13 @@ public void runXMLForModelLibrary(final String xmlPath) throws FileNotFoundExcep
}

public void runSimulation(final List<String> args) throws FileNotFoundException, InterruptedException {
if (!checkParameters(args)) {
System.exit(-1);
}
if (!checkParameters(args)) { System.exit(-1); }

verbose = args.contains(VERBOSE_PARAMETER);
if (verbose) {
DEBUG.ON();

}
HeadlessSimulationLoader.preloadGAMA();
this.tunnelingMode = args.contains(TUNNELING_PARAMETER);
this.consoleMode = args.contains(CONSOLE_PARAMETER);
if (args.contains(SOCKET_PARAMETER)) {
Expand All @@ -315,9 +304,7 @@ public void runSimulation(final List<String> args) throws FileNotFoundException,
processorQueue = new LocalSimulationRuntime(this.numberOfThread);

Reader in = null;
if (this.verbose && !this.tunnelingMode) {
DEBUG.ON();
}
if (this.verbose && !this.tunnelingMode) { DEBUG.ON(); }

if (this.consoleMode) {
in = new Reader(ConsoleReader.readOnConsole());
Expand Down

0 comments on commit d07c657

Please sign in to comment.