/
HeadlessSimulationLoader.java
147 lines (130 loc) · 4.97 KB
/
HeadlessSimulationLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*********************************************************************************************
*
*
* 'HeadlessSimulationLoader.java', in plugin 'msi.gama.headless', is part of the source code of the GAMA modeling and
* simulation platform. (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
*
*
**********************************************************************************************/
package msi.gama.headless.core;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import com.google.inject.Injector;
import msi.gama.common.preferences.GamaPreferences;
import msi.gama.kernel.model.IModel;
import msi.gama.lang.gaml.GamlStandaloneSetup;
import msi.gama.lang.gaml.validation.GamlModelBuilder;
import msi.gama.precompiler.GamlProperties;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gaml.compilation.GamlCompilationError;
import one.util.streamex.StreamEx;
import ummisco.gama.dev.utils.DEBUG;
public class HeadlessSimulationLoader {
static {
DEBUG.ON();
}
// The injector to use in headless mode
Injector injector;
private static HeadlessSimulationLoader INSTANCE = new HeadlessSimulationLoader();
// Singleton
private HeadlessSimulationLoader() {}
public static Injector getInjector() {
return INSTANCE.configureInjector();
}
public static void preloadGAMA() {
INSTANCE.configureInjector();
}
private Injector configureInjector() {
if (injector != null) return injector;
DEBUG.LOG("GAMA configuring and loading...");
System.setProperty("java.awt.headless", "true");
GAMA.setHeadLessMode();
try {
// We initialize XText and Gaml.
injector = GamlStandaloneSetup.doSetup();
} catch (final Exception e1) {
throw GamaRuntimeException.create(e1, GAMA.getRuntimeScope());
}
// SEED HACK // WARNING AD : Why ?
GamaPreferences.External.CORE_SEED_DEFINED.set(true);
GamaPreferences.External.CORE_SEED.set(1.0);
// SEED HACK
return injector;
}
/**
* Compiles a file to a GAMA model ready to be experimented
*
* @param myFile
* the main model file
* @return a compiled model
* @throws IOException
* in case the file is null or not found
* @throws GamaHeadlessException
* in case the compilation ends in error
* @deprecated use loadModel(File, List<GamlCompilationError>) instead
*/
@Deprecated
public static synchronized IModel loadModel(final File myFile) throws IOException, GamaHeadlessException {
return loadModel(myFile, null);
}
/**
* Compiles a file to a GAMA model ready to be experimented
*
* @param myFile
* the main model file
* @param errors
* a list that will be filled with compilation errors / warnings (can be null)
* @return a compiled model
* @throws IOException
* in case the file is null or not found
* @throws GamaHeadlessException
* in case the compilation ends in error
*/
public static synchronized IModel loadModel(final File myFile, final List<GamlCompilationError> errors)
throws IOException, GamaHeadlessException {
return loadModel(myFile, errors, null, true);
}
/**
* Compiles a file to a GAMA model ready to be experimented
*
* @param myFile
* the main model file
* @param errors
* a list that will be filled with compilation errors / warnings (can be null)
* @param metaProperties
* an instance of GamlProperties that will be filled with the sylmbolic names of bundles required to run
* the model (can be null) and other informations (skills, operators, statements, ...).
* @return a compiled model
* @throws IOException
* in case the file is null or not found
* @throws GamaHeadlessException
* in case the compilation ends in error
*/
public static synchronized IModel loadModel(final File myFile, final List<GamlCompilationError> errors,
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);
DEBUG.LOG(fileName + " model is being compiled...");
final IModel model = GamlModelBuilder.getDefaultInstance().compile(URI.createFileURI(fileName), errors);
if (model == null) {
DEBUG.LOG("Model compiled with following indications: \n"
+ (errors == null ? "" : StreamEx.of(errors).joining("\n")));
throw new GamaHeadlessException(
"Model cannot be compiled. See list of attached errors \n" + StreamEx.of(errors).joining("\n"));
}
// if (metaProperties != null)
// model.getDescription().collectMetaInformation(metaProperties);
return model;
}
}