From ffcb1b8e8694ba1ce5f768721389b1cbd702465f Mon Sep 17 00:00:00 2001 From: AlexisDrogoul Date: Wed, 5 May 2021 10:41:32 +0700 Subject: [PATCH] Addresses #3096. Needs to be tested thoroughly in headless mode ! --- .../batch/test/ModelLibraryTester.java | 10 ++--- .../batch/validation/ModelLibraryRunner.java | 8 ++-- .../validation/ModelLibraryValidator.java | 6 +-- .../core/HeadlessSimulationLoader.java | 25 +++++++++-- .../openmole/MoleSimulationLoader.java | 15 +++---- .../msi/gama/headless/openmole/TestMole.java | 26 ----------- .../gaml/resource/GamlResourceServices.java | 43 ++++++++++--------- ummisco.gama.ui.modeling/META-INF/MANIFEST.MF | 1 + .../ui/GamlExecutableExtensionFactory.java | 12 ++++-- 9 files changed, 67 insertions(+), 79 deletions(-) delete mode 100644 msi.gama.headless/src/msi/gama/headless/openmole/TestMole.java diff --git a/msi.gama.headless/src/msi/gama/headless/batch/test/ModelLibraryTester.java b/msi.gama.headless/src/msi/gama/headless/batch/test/ModelLibraryTester.java index 4ef0b78641..7bd2949ecb 100644 --- a/msi.gama.headless/src/msi/gama/headless/batch/test/ModelLibraryTester.java +++ b/msi.gama.headless/src/msi/gama/headless/batch/test/ModelLibraryTester.java @@ -43,7 +43,7 @@ private ModelLibraryTester() {} @Override public int start(final List args) throws IOException { DEBUG.ON(); - final Injector injector = HeadlessSimulationLoader.preloadGAMA(); + final Injector injector = HeadlessSimulationLoader.getInjector(); final GamlModelBuilder builder = createBuilder(injector); original = System.out; @@ -89,11 +89,11 @@ public void test(final GamlModelBuilder builder, final int[] count, final int[] final List 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 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) { @@ -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; } } diff --git a/msi.gama.headless/src/msi/gama/headless/batch/validation/ModelLibraryRunner.java b/msi.gama.headless/src/msi/gama/headless/batch/validation/ModelLibraryRunner.java index 4e03f33cea..4654b0dc83 100644 --- a/msi.gama.headless/src/msi/gama/headless/batch/validation/ModelLibraryRunner.java +++ b/msi.gama.headless/src/msi/gama/headless/batch/validation/ModelLibraryRunner.java @@ -35,7 +35,7 @@ private ModelLibraryRunner() { @Override public int start(final List 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 }; @@ -115,7 +115,7 @@ public int start(final List args) throws IOException { private void validateAndRun(final GamlModelBuilder builder, final Map 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 errors = new ArrayList<>(); @@ -156,9 +156,7 @@ private void validateAndRun(final GamlModelBuilder builder, final Map 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 }; @@ -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; } } diff --git a/msi.gama.headless/src/msi/gama/headless/core/HeadlessSimulationLoader.java b/msi.gama.headless/src/msi/gama/headless/core/HeadlessSimulationLoader.java index fe411a0a39..1661cdb6f5 100644 --- a/msi.gama.headless/src/msi/gama/headless/core/HeadlessSimulationLoader.java +++ b/msi.gama.headless/src/msi/gama/headless/core/HeadlessSimulationLoader.java @@ -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(); @@ -108,9 +124,10 @@ public static synchronized IModel loadModel(final File myFile, final List 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); diff --git a/msi.gama.headless/src/msi/gama/headless/openmole/MoleSimulationLoader.java b/msi.gama.headless/src/msi/gama/headless/openmole/MoleSimulationLoader.java index 6fc3e0ba07..e29d5d9e79 100644 --- a/msi.gama.headless/src/msi/gama/headless/openmole/MoleSimulationLoader.java +++ b/msi.gama.headless/src/msi/gama/headless/openmole/MoleSimulationLoader.java @@ -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; @@ -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 @@ -48,7 +44,6 @@ public static IModel loadModel(final File modelPath, final List errors, final GamlProperties metadata) throws IOException, GamaHeadlessException { - HeadlessSimulationLoader.preloadGAMA(); return HeadlessSimulationLoader.loadModel(modelPath, errors, metadata); } diff --git a/msi.gama.headless/src/msi/gama/headless/openmole/TestMole.java b/msi.gama.headless/src/msi/gama/headless/openmole/TestMole.java deleted file mode 100644 index 9971d57797..0000000000 --- a/msi.gama.headless/src/msi/gama/headless/openmole/TestMole.java +++ /dev/null @@ -1,26 +0,0 @@ -/********************************************************************************************* - * - * - * 'TestMole.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; - -public class TestMole { - - /** - * @param args - */ - public static void main(String[] args) { - MoleSimulationLoader.loadGAMA(); - - } - -} diff --git a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResourceServices.java b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResourceServices.java index 3dc952de73..9d04d26f66 100644 --- a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResourceServices.java +++ b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResourceServices.java @@ -62,12 +62,7 @@ public class GamlResourceServices { private static GamlSyntacticConverter converter = new GamlSyntacticConverter(); private static final Map resourceListeners = GamaMapFactory.createUnordered(); private static final Map resourceErrors = GamaMapFactory.createUnordered(); - private static final XtextResourceSet poolSet = new XtextResourceSet() { - { - setClasspathURIContext(GamlResourceServices.class); - } - - }; + private static XtextResourceSet poolSet; private static final LoadingCache> documentationCache = CacheBuilder.newBuilder().build(new CacheLoader>() { @@ -91,11 +86,11 @@ public static IMap 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()); @@ -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 @@ -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(); @@ -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()); @@ -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); @@ -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; + } + } diff --git a/ummisco.gama.ui.modeling/META-INF/MANIFEST.MF b/ummisco.gama.ui.modeling/META-INF/MANIFEST.MF index eee31ee08e..ecd0df0087 100644 --- a/ummisco.gama.ui.modeling/META-INF/MANIFEST.MF +++ b/ummisco.gama.ui.modeling/META-INF/MANIFEST.MF @@ -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 diff --git a/ummisco.gama.ui.modeling/src-gen/msi/gama/lang/gaml/ui/GamlExecutableExtensionFactory.java b/ummisco.gama.ui.modeling/src-gen/msi/gama/lang/gaml/ui/GamlExecutableExtensionFactory.java index 393a0c3933..6def9899f5 100644 --- a/ummisco.gama.ui.modeling/src-gen/msi/gama/lang/gaml/ui/GamlExecutableExtensionFactory.java +++ b/ummisco.gama.ui.modeling/src-gen/msi/gama/lang/gaml/ui/GamlExecutableExtensionFactory.java @@ -3,15 +3,18 @@ */ 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 { @@ -19,9 +22,10 @@ public class GamlExecutableExtensionFactory extends AbstractGuiceAwareExecutable 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; }