From 4c489c5589dd7882d0193c814d4b4d18309289e5 Mon Sep 17 00:00:00 2001 From: AlexisDrogoul Date: Sat, 19 Feb 2022 21:24:13 +0700 Subject: [PATCH] Fixes #3302 by adding information to multiple imports --- .../gaml/indexer/GamlResourceIndexer.java | 47 ++++++++++++ .../gama/lang/gaml/resource/GamlResource.java | 72 ++++++++++--------- 2 files changed, 84 insertions(+), 35 deletions(-) diff --git a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/indexer/GamlResourceIndexer.java b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/indexer/GamlResourceIndexer.java index 70e1fef091..be5dd68dfa 100644 --- a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/indexer/GamlResourceIndexer.java +++ b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/indexer/GamlResourceIndexer.java @@ -11,6 +11,7 @@ package msi.gama.lang.gaml.indexer; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -95,6 +96,30 @@ private static Map getImportsAsAbsoluteURIS(final URI baseURI, fina return result; } + /** + * Gets the import object. + * + * @param m + * the m + * @param modelURI + * the model URI + * @param importUri + * the import uri + * @return the import object + */ + public static Import getImportObject(final EObject m, final URI modelURI, final URI importUri) { + for (final Import e : ((Model) m).getImports()) { + final String u = e.getImportURI(); + if (u != null) { + URI uri = URI.createURI(u, true); + uri = GamlResourceServices.properlyEncodedURI(uri.resolve(modelURI)); + if (uri.equals(importUri)) return e; + } + } + return null; + + } + /** * Adds the import. * @@ -242,4 +267,26 @@ public static Map allImportsOf(final URI uri) { return index.sortedDepthFirstSearchWithLabels(GamlResourceServices.properlyEncodedURI(uri)); } + /** + * Mark multiple imports. Returns the map of imports that are imported several times. Keys are the double imports, + * values the direct import that imports it + * + * @param uri + * the uri + * @return the list + */ + public static Map collectMultipleImportsOf(final GamlResource r) { + Map result = Collections.EMPTY_MAP; + Set uris = directImportsOf(r.getURI()); + for (URI uri : uris) { + for (URI imported : allImportsOf(uri).keySet()) { + if (uris.contains(imported)) { + if (result == Collections.EMPTY_MAP) { result = new HashMap<>(); } + result.put(imported, uri); + } + } + } + return result; + } + } diff --git a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResource.java b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResource.java index 90431f3177..21b577b553 100644 --- a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResource.java +++ b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/resource/GamlResource.java @@ -1,12 +1,12 @@ /******************************************************************************************************* * - * GamlResource.java, in msi.gama.lang.gaml, is part of the source code of the - * GAMA modeling and simulation platform (v.1.8.2). + * GamlResource.java, in msi.gama.lang.gaml, is part of the source code of the GAMA modeling and simulation platform + * (v.1.8.2). * * (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. - * + * ********************************************************************************************************/ package msi.gama.lang.gaml.resource; @@ -14,6 +14,7 @@ import java.io.InputStream; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Set; import org.eclipse.emf.common.util.URI; @@ -63,10 +64,10 @@ public class GamlResource extends LazyLinkingResource { /** The memoize description. */ private static boolean MEMOIZE_DESCRIPTION = false; - + /** The description. */ ModelDescription description; - + /** The element. */ ISyntacticElement element; @@ -75,9 +76,7 @@ public class GamlResource extends LazyLinkingResource { * * @return the validation context */ - public ValidationContext getValidationContext() { - return GamlResourceServices.getValidationContext(this); - } + public ValidationContext getValidationContext() { return GamlResourceServices.getValidationContext(this); } /** * Checks for semantic errors. @@ -89,9 +88,7 @@ public boolean hasSemanticErrors() { } @Override - public String getEncoding() { - return "UTF-8"; - } + public String getEncoding() { return "UTF-8"; } @Override public String toString() { @@ -101,8 +98,10 @@ public String toString() { /** * Update with. * - * @param model the model - * @param newState the new state + * @param model + * the model + * @param newState + * the new state */ public void updateWith(final ModelDescription model, final boolean newState) { GamlResourceServices.updateState(getURI(), model, newState, GamlResourceServices.getValidationContext(this)); @@ -114,9 +113,7 @@ public void updateWith(final ModelDescription model, final boolean newState) { * @return the syntactic contents */ public ISyntacticElement getSyntacticContents() { - if (element == null) { - setElement(GamlResourceServices.buildSyntacticContents(this)); - } + if (element == null) { setElement(GamlResourceServices.buildSyntacticContents(this)); } return element; } @@ -129,7 +126,8 @@ public ISyntacticElement getSyntacticContents() { /** * Builds the model description. * - * @param resources the resources + * @param resources + * the resources * @return the model description */ private ModelDescription buildModelDescription(final LinkedHashMultimap resources) { @@ -172,8 +170,10 @@ private ModelDescription buildModelDescription(final LinkedHashMultimap imports = GamlResourceIndexer.validateImportsOf(this); if (hasErrors() || hasSemanticErrors()) { setDescription(null); @@ -204,6 +204,13 @@ public ModelDescription buildCompleteDescription() { if (model == null) { invalidate(this, "Impossible to validate " + URI.decode(getURI().lastSegment()) + " (check the logs)"); } + Map doubleImports = GamlResourceIndexer.collectMultipleImportsOf(this); + doubleImports.forEach((imported, importer) -> { + String s = imported.lastSegment() + " is already imported by " + importer.lastSegment(); + EObject o = GamlResourceIndexer.getImportObject(getContents().get(0), getURI(), imported); + GamlCompilationError error = new GamlCompilationError(s, IGamlIssue.GENERAL, o, false, true); + getValidationContext().add(error); + }); setDescription(model); return model; } @@ -267,27 +274,24 @@ protected void doUnload() { /** * Sets the description. * - * @param model the new description + * @param model + * the new description */ private void setDescription(final ModelDescription model) { - if (!MEMOIZE_DESCRIPTION) { return; } - if (model == description) { return; } - if (description != null) { - description.dispose(); - } + if (!MEMOIZE_DESCRIPTION || model == description) return; + if (description != null) { description.dispose(); } description = model; } /** * Sets the element. * - * @param model the new element + * @param model + * the new element */ private void setElement(final ISyntacticElement model) { - if (model == element) { return; } - if (element != null) { - element.dispose(); - } + if (model == element) return; + if (element != null) { element.dispose(); } element = model; } @@ -313,16 +317,14 @@ public void process(final GamlResource state) throws Exception { } @Override - public OnChangeEvictingCache getCache() { - return (OnChangeEvictingCache) super.getCache(); - } + public OnChangeEvictingCache getCache() { return (OnChangeEvictingCache) super.getCache(); } @Override protected void doLinking() { // If the imports are not correctly updated, we cannot proceed final EObject faulty = GamlResourceIndexer.updateImports(this); if (faulty != null) { - System.out.println(getURI() + " cannot import " + faulty); + // DEBUG.OUT(getURI() + " cannot import " + faulty); final EAttribute attribute = getContents().get(0) instanceof Model ? GamlPackage.Literals.IMPORT__IMPORT_URI : GamlPackage.Literals.HEADLESS_EXPERIMENT__IMPORT_URI; getErrors().add(new EObjectDiagnosticImpl(Severity.ERROR, IGamlIssue.IMPORT_ERROR,