Skip to content

Commit

Permalink
Addresses #3096. Needs to be tested thoroughly in headless mode !
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed May 5, 2021
1 parent a27f720 commit ffcb1b8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 79 deletions.
Expand Up @@ -43,7 +43,7 @@ private ModelLibraryTester() {}
@Override
public int start(final List<String> args) throws IOException {
DEBUG.ON();
final Injector injector = HeadlessSimulationLoader.preloadGAMA();
final Injector injector = HeadlessSimulationLoader.getInjector();
final GamlModelBuilder builder = createBuilder(injector);

original = System.out;
Expand Down Expand Up @@ -89,11 +89,11 @@ public void test(final GamlModelBuilder builder, final int[] count, final int[]
final List<GamlCompilationError> errors = new ArrayList<>();
try {
final IModel model = builder.compile(p, errors);
if (model == null || model.getDescription() == null) { return; }
if (model == null || model.getDescription() == null) return;
final List<String> testExpNames = ((ModelDescription) model.getDescription()).getExperimentNames().stream()
.filter(e -> model.getExperiment(e).isTest()).collect(Collectors.toList());

if (testExpNames.isEmpty()) { return; }
if (testExpNames.isEmpty()) return;
for (final String expName : testExpNames) {
final IExperimentPlan exp = GAMA.addHeadlessExperiment(model, expName, new ParametersSet(), null);
if (exp != null) {
Expand Down Expand Up @@ -121,9 +121,7 @@ public void test(final GamlModelBuilder builder, final int[] count, final int[]
}

public static ModelLibraryTester getInstance() {
if (instance == null) {
instance = new ModelLibraryTester();
}
if (instance == null) { instance = new ModelLibraryTester(); }
return instance;
}
}
Expand Up @@ -35,7 +35,7 @@ private ModelLibraryRunner() {

@Override
public int start(final List<String> args) throws IOException {
final Injector injector = HeadlessSimulationLoader.preloadGAMA();
final Injector injector = HeadlessSimulationLoader.getInjector();
final GamlModelBuilder builder = createBuilder(injector);
final int[] count = { 0 };
final int[] code = { 0, 0 };
Expand Down Expand Up @@ -115,7 +115,7 @@ public int start(final List<String> args) throws IOException {
private void validateAndRun(final GamlModelBuilder builder, final Map<String, Exception> executionErrors,
final int[] countOfModelsValidated, final int[] returnCode, final URL pathToModel, final boolean expGUIOnly,
final int nbCycles) {
if (pathToModel.toString().contains("Database")) { return; }
if (pathToModel.toString().contains("Database")) return;
DEBUG.PAD("", 80, '=');

final List<GamlCompilationError> errors = new ArrayList<>();
Expand Down Expand Up @@ -156,9 +156,7 @@ private void validateAndRun(final GamlModelBuilder builder, final Map<String, Ex
}

public static ModelLibraryRunner getInstance() {
if (instance == null) {
instance = new ModelLibraryRunner();
}
if (instance == null) { instance = new ModelLibraryRunner(); }
return instance;
}
}
Expand Up @@ -29,7 +29,7 @@ private ModelLibraryValidator() {

@Override
public int start(final List<String> args) throws IOException {
final Injector injector = HeadlessSimulationLoader.preloadGAMA();
final Injector injector = HeadlessSimulationLoader.getInjector();
final GamlModelBuilder builder = createBuilder(injector);
final int[] count = { 0 };
final int[] code = { 0, 0 };
Expand Down Expand Up @@ -98,9 +98,7 @@ private void validate(final GamlModelBuilder builder, final int[] countOfModelsV
}

public static ModelLibraryValidator getInstance() {
if (instance == null) {
instance = new ModelLibraryValidator();
}
if (instance == null) { instance = new ModelLibraryValidator(); }
return instance;
}
}
Expand Up @@ -37,11 +37,27 @@ public class HeadlessSimulationLoader {
DEBUG.ON();
}

public static Injector preloadGAMA() {
// 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();
}

private 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();
Injector injector;
try {
// We initialize XText and Gaml.
injector = GamlStandaloneSetup.doSetup();
Expand Down Expand Up @@ -108,9 +124,10 @@ public static synchronized IModel loadModel(final File myFile, final List<GamlCo
*/
public static synchronized IModel loadModel(final File myFile, final List<GamlCompilationError> errors,
final GamlProperties metaProperties) throws IOException, GamaHeadlessException {
if (myFile == null) { throw new IOException("Model file is null"); }
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); }
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);
Expand Down
@@ -1,14 +1,14 @@
/*********************************************************************************************
*
*
*
* 'MoleSimulationLoader.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.openmole;

Expand All @@ -24,12 +24,8 @@

public abstract class MoleSimulationLoader {

public static void loadGAMA() {
// Now done in the loader
}

/**
*
*
* @param modelPath
* @return a compiled model
* @throws IOException
Expand All @@ -48,7 +44,6 @@ 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 {
HeadlessSimulationLoader.preloadGAMA();
return HeadlessSimulationLoader.loadModel(modelPath, errors, metadata);
}

Expand Down
26 changes: 0 additions & 26 deletions msi.gama.headless/src/msi/gama/headless/openmole/TestMole.java

This file was deleted.

Expand Up @@ -62,12 +62,7 @@ public class GamlResourceServices {
private static GamlSyntacticConverter converter = new GamlSyntacticConverter();
private static final Map<URI, IGamlBuilderListener> resourceListeners = GamaMapFactory.createUnordered();
private static final Map<URI, ValidationContext> resourceErrors = GamaMapFactory.createUnordered();
private static final XtextResourceSet poolSet = new XtextResourceSet() {
{
setClasspathURIContext(GamlResourceServices.class);
}

};
private static XtextResourceSet poolSet;
private static final LoadingCache<URI, IMap<EObject, IGamlDescription>> documentationCache =
CacheBuilder.newBuilder().build(new CacheLoader<URI, IMap<EObject, IGamlDescription>>() {

Expand All @@ -91,11 +86,11 @@ public static IMap<EObject, IGamlDescription> getDocumentationCache(final Resour
* encoded version of the parameter.
*/
public static URI properlyEncodedURI(final URI uri) {
if (uri == null) { return null; }
if (uri == null) return null;
URI pre_properlyEncodedURI = uri;
if (GAMA.isInHeadLessMode() && !uri.isPlatformResource()) {
final String filePath = uri.toFileString();
if (filePath == null) { return null; }
if (filePath == null) return null;
final File file = new File(filePath);
try {
pre_properlyEncodedURI = URI.createFileURI(file.getCanonicalPath());
Expand Down Expand Up @@ -125,7 +120,7 @@ public static void updateState(final URI uri, final ModelDescription model, fina
final URI newURI = properlyEncodedURI(uri);

final IGamlBuilderListener listener = resourceListeners.get(newURI);
if (listener == null) { return; }
if (listener == null) return;
// DEBUG.LOG("Finishing updating the state of editor for " +
// uri.lastSegment());
final Iterable exps = model == null ? newState ? Collections.EMPTY_SET : null
Expand Down Expand Up @@ -200,9 +195,9 @@ public static IPath getPathOf(final Resource r) {

public static String getModelPathOf(final Resource r) {
// Likely in a headless scenario (w/o workspace)
if (r.getURI().isFile()) {
if (r.getURI().isFile())
return new Path(r.getURI().toFileString()).toOSString();
} else {
else {
final IPath path = getPathOf(r);
final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
final IPath fullPath = file.getLocation();
Expand All @@ -214,16 +209,16 @@ private static boolean isProject(final File f) {
final String[] files = f.list();
if (files != null) {
for (final String s : files) {
if (".project".equals(s)) { return true; }
if (".project".equals(s)) return true;
}
}
return false;
}

public static String getProjectPathOf(final Resource r) {
if (r == null) { return ""; }
if (r == null) return "";
final URI uri = r.getURI();
if (uri == null) { return ""; }
if (uri == null) return "";
// Cf. #2983 -- we are likely in a headless scenario
if (uri.isFile()) {
File project = new File(uri.toFileString());
Expand All @@ -248,15 +243,11 @@ public synchronized static GamlResource getTemporaryResource(final IDescription
final EObject e = desc.getUnderlyingElement();
if (e != null) {
r = (GamlResource) e.eResource();
if (r != null) {
rs = r.getResourceSet();
}
if (r != null) { rs = r.getResourceSet(); }
}
}
}
if (rs == null) {
rs = poolSet;
}
if (rs == null) { rs = getPoolSet(); }
final URI uri = URI.createURI(IKeyword.SYNTHETIC_RESOURCES_PREFIX + resourceCount++ + ".gaml", false);
// TODO Modifier le cache de la resource ici ?
final GamlResource result = (GamlResource) rs.createResource(uri);
Expand Down Expand Up @@ -287,4 +278,16 @@ public static ISyntacticElement buildSyntacticContents(final GamlResource r) {
return converter.buildSyntacticContents(r.getParseResult().getRootASTElement(), null);
}

private static XtextResourceSet getPoolSet() {
if (poolSet == null) {
poolSet = new XtextResourceSet() {
{
setClasspathURIContext(GamlResourceServices.class);
}

};
}
return poolSet;
}

}
1 change: 1 addition & 0 deletions ummisco.gama.ui.modeling/META-INF/MANIFEST.MF
Expand Up @@ -53,3 +53,4 @@ Export-Package: msi.gama.lang.gaml.ide,
Bundle-Activator: ummisco.gama.ui.modeling.internal.ModelingActivator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Automatic-Module-Name: ummisco.gama.ui.modeling
Import-Package: msi.gama.headless.core
Expand Up @@ -3,25 +3,29 @@
*/
package msi.gama.lang.gaml.ui;

import com.google.inject.Injector;
import org.eclipse.core.runtime.Platform;
import org.eclipse.xtext.ui.guice.AbstractGuiceAwareExecutableExtensionFactory;
import org.osgi.framework.Bundle;

import com.google.inject.Injector;

import msi.gama.headless.core.HeadlessSimulationLoader;
import msi.gama.runtime.GAMA;
import ummisco.gama.ui.modeling.internal.ModelingActivator;

/**
* This class was generated. Customizations should only happen in a newly
* introduced subclass.
* This class was generated. Customizations should only happen in a newly introduced subclass.
*/
public class GamlExecutableExtensionFactory extends AbstractGuiceAwareExecutableExtensionFactory {

@Override
protected Bundle getBundle() {
return Platform.getBundle(ModelingActivator.PLUGIN_ID);
}

@Override
protected Injector getInjector() {
if (GAMA.isInHeadLessMode()) return HeadlessSimulationLoader.getInjector();
ModelingActivator activator = ModelingActivator.getInstance();
return activator != null ? activator.getInjector(ModelingActivator.MSI_GAMA_LANG_GAML_GAML) : null;
}
Expand Down

0 comments on commit ffcb1b8

Please sign in to comment.