From: lempinen Date: Tue, 17 May 2011 12:40:31 +0000 (+0000) Subject: Allow to use same enumeration name in different hierarchical levels. Show Module... X-Git-Tag: simantics-1.4RC1~8 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a50a0f50e0c778f195bc9907b1251ce6e34377e9;p=simantics%2Fsysdyn.git Allow to use same enumeration name in different hierarchical levels. Show Module -handler to work also with module types, not just instantiated modules. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20985 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java index 8d3212d8..49b3f023 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java @@ -52,8 +52,7 @@ public class NewEnumerationNodeHandler extends AbstractHandler { Resource enumerationIndexes = OrderedSetUtils.create(g, sr.EnumerationIndexes); - String name = NameUtils.findFreshName(g, "Enumeration", configuration, l0.ConsistsOf, "%s%d"); - if("Enumeration".equals(name)) name = "Enumeration1"; + String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d"); GraphUtils.create2(g, sr.Enumeration, diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java index 67732e3a..6c289d66 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java @@ -23,6 +23,7 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.exception.MissingVariableException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.diagram.stubs.DiagramResource; @@ -71,8 +72,17 @@ public class ShowModuleHandler extends AbstractHandler { final Resource configuration = graph.getPossibleObject(componentType, st.IsDefinedBy); final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, configuration); - Variable variable = parent.browsePossible(graph, component); - final String finalRvi = Variables.getRVI(graph, variable); + Variable variable; + String rvi = ""; + try { + variable = parent.browse(graph, component); + rvi = Variables.getRVI(graph, variable); + } catch (MissingVariableException e ) { + variable = Variables.getVariable(graph, graph.getURI(component)); + } + final String finalRvi = rvi; + + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java index d04bc240..941e0632 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -51,28 +51,43 @@ public class Module implements IElement { /** * Returns the declaration of a module with possible redeclared enumerations + * + * parameter Integer Enum__size = Enum.size; + * parameter Integer Enum__elements[:] = Enum.elements; + * ModuleType ModuleType1(Enum.size = Enum__size, Enum.elements = Enum__elements); * - * Module m(redeclared.size = enumeration.size, redeclared.elements = enumeration.elements); + * Temporary parameter variables are needed to avoid name conflicts when redeclaring an + * enumeration with the same name. * * @return Declaration of a module instance */ public String getDeclaration() { - StringBuilder sb = new StringBuilder(); - sb.append(" "); - sb.append(getType().getName()); - sb.append(" "); - sb.append(getName()); + + StringBuilder parameters = new StringBuilder(); + StringBuilder redeclarations = new StringBuilder(); + + if(!getRedeclarations().isEmpty()) { - sb.append("("); + redeclarations.append("("); Iterator i = getRedeclarations().iterator(); while(i.hasNext()) { Redeclaration rd = i.next(); - sb.append(rd.getRedeclaration()); + redeclarations.append(rd.getRedeclaration()); + parameters.append(rd.getParameters()); if(i.hasNext()) - sb.append(","); + redeclarations.append(","); } - sb.append(")"); + redeclarations.append(")"); } + + + StringBuilder sb = new StringBuilder(); + sb.append(parameters); + sb.append(" "); + sb.append(getType().getName()); + sb.append(" "); + sb.append(getName()); + sb.append(redeclarations.toString()); sb.append(";\n"); return sb.toString(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java index b048bbdb..cdf33978 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java @@ -27,18 +27,60 @@ public class Redeclaration { if(replacedEnumeration == null || replacingEnumeration == null) { return ""; } + String separator = getSeparator(); StringBuilder sb = new StringBuilder(); sb.append(replacedEnumeration.getName()); sb.append(".size = "); sb.append(replacingEnumeration.getName()); - sb.append(".size, "); + sb.append(separator + "size, "); sb.append(replacedEnumeration.getName()); sb.append(".elements = "); sb.append(replacingEnumeration.getName()); - sb.append(".elements"); + sb.append(separator + "elements"); return sb.toString(); } + + /** + * Change the name of the re-declaration parameters to enable the use of + * enumerations with same names. + * + * @return + */ + public String getParameters() { + String separator = getSeparator(); + StringBuilder sb = new StringBuilder(); + sb.append(" parameter Integer " + replacingEnumeration.getName() + separator + "size = " + replacingEnumeration.getName() + ".size;\n"); + sb.append(" parameter Integer " + replacingEnumeration.getName() + separator + "elements[:] = " + replacingEnumeration.getName() + ".elements;\n"); + return sb.toString(); + } + + private String getSeparator() { + return getSeparator(this, "_"); + } + + /** + * Add enough separation marks that name conflicts would not exist. + * + * @param redeclaration + * @param separator + * @return + */ + private String getSeparator(Redeclaration redeclaration, String separator) { + for(IElement e : redeclaration.replacedEnumeration.getParentConfiguration().getElements()) { + if(e instanceof Module) { + for(Redeclaration rd : ((Module)e).getRedeclarations()) { + if(rd.replacedEnumeration.name.equals(redeclaration.replacedEnumeration.name) && + rd.replacingEnumeration.equals(redeclaration.replacedEnumeration)) { + String separatorTmp = getSeparator(rd, separator); + if(separatorTmp.length() > separator.length()) + separator = separatorTmp; + } + } + } + } + return separator + "_"; + } public Enumeration getReplacedEnumeration() { return replacedEnumeration;