Skip to content

Commit

Permalink
Fixes #3302 by adding information to multiple imports
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Feb 19, 2022
1 parent 50d968d commit 4c489c5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 35 deletions.
Expand Up @@ -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;

Expand Down Expand Up @@ -95,6 +96,30 @@ private static Map<URI, String> 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.
*
Expand Down Expand Up @@ -242,4 +267,26 @@ public static Map<URI, String> 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<URI, URI> collectMultipleImportsOf(final GamlResource r) {
Map<URI, URI> result = Collections.EMPTY_MAP;
Set<URI> 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;
}

}
@@ -1,19 +1,20 @@
/*******************************************************************************************************
*
* 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;

import java.io.IOException;
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;
Expand Down Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -89,9 +88,7 @@ public boolean hasSemanticErrors() {
}

@Override
public String getEncoding() {
return "UTF-8";
}
public String getEncoding() { return "UTF-8"; }

@Override
public String toString() {
Expand All @@ -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));
Expand All @@ -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;
}

Expand All @@ -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<String, GamlResource> resources) {
Expand Down Expand Up @@ -172,8 +170,10 @@ private ModelDescription buildModelDescription(final LinkedHashMultimap<String,
/**
* Invalidate.
*
* @param r the r
* @param s the s
* @param r
* the r
* @param s
* the s
*/
public void invalidate(final GamlResource r, final String s) {
GamlCompilationError error = null;
Expand All @@ -192,7 +192,7 @@ public void invalidate(final GamlResource r, final String s) {
* @return the model description
*/
public ModelDescription buildCompleteDescription() {
if (MEMOIZE_DESCRIPTION && description != null) { return description; }
if (MEMOIZE_DESCRIPTION && description != null) return description;
final LinkedHashMultimap<String, GamlResource> imports = GamlResourceIndexer.validateImportsOf(this);
if (hasErrors() || hasSemanticErrors()) {
setDescription(null);
Expand All @@ -204,6 +204,13 @@ public ModelDescription buildCompleteDescription() {
if (model == null) {
invalidate(this, "Impossible to validate " + URI.decode(getURI().lastSegment()) + " (check the logs)");
}
Map<URI, URI> 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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down

0 comments on commit 4c489c5

Please sign in to comment.