]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Allow to use same enumeration name in different hierarchical levels. Show Module...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 17 May 2011 12:40:31 +0000 (12:40 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 17 May 2011 12:40:31 +0000 (12:40 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20985 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java

index 8d3212d828442d3d180efae6d5e3147c702e4e03..49b3f023d3602f176b72d79500f7a1123db3bf2e 100644 (file)
@@ -52,8 +52,7 @@ public class NewEnumerationNodeHandler extends AbstractHandler {
 \r
                Resource enumerationIndexes = OrderedSetUtils.create(g, sr.EnumerationIndexes);\r
                \r
-               String name = NameUtils.findFreshName(g, "Enumeration", configuration, l0.ConsistsOf, "%s%d");\r
-               if("Enumeration".equals(name)) name = "Enumeration1";\r
+               String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");\r
                \r
                GraphUtils.create2(g, \r
                                sr.Enumeration,\r
index 67732e3ac74ae7a857ace47e601c6b38834958af..6c289d6638c4145cbdc42088787a82cebf4f7ce6 100644 (file)
@@ -23,6 +23,7 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.exception.MissingVariableException;\r
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.db.layer0.variable.Variables;\r
 import org.simantics.diagram.stubs.DiagramResource;\r
@@ -71,8 +72,17 @@ public class ShowModuleHandler extends AbstractHandler {
                                        final Resource configuration = graph.getPossibleObject(componentType, st.IsDefinedBy);\r
                                        final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, configuration);\r
 \r
-                                       Variable variable = parent.browsePossible(graph, component);\r
-                                       final String finalRvi = Variables.getRVI(graph, variable);\r
+                                       Variable variable;\r
+                                       String rvi = "";\r
+                                       try {\r
+                                               variable = parent.browse(graph, component);\r
+                                               rvi = Variables.getRVI(graph, variable);\r
+                                       } catch (MissingVariableException e ) {\r
+                                               variable = Variables.getVariable(graph, graph.getURI(component));\r
+                                       }\r
+                                       final String finalRvi = rvi;\r
+                                       \r
+\r
                                        \r
                                        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
 \r
index d04bc24041924c55caf9f63f00f4d90c2f4f3e8b..941e0632a49d7d82d6843b4bd5107714842ba560 100644 (file)
@@ -51,28 +51,43 @@ public class Module implements IElement {
     \r
     /**\r
      * Returns the declaration of a module with possible redeclared enumerations\r
+     *     \r
+     * parameter Integer Enum__size = Enum.size;\r
+     * parameter Integer Enum__elements[:] = Enum.elements;\r
+     * ModuleType ModuleType1(Enum.size = Enum__size, Enum.elements = Enum__elements);\r
      * \r
-     * Module m(redeclared.size = enumeration.size, redeclared.elements = enumeration.elements);\r
+     * Temporary parameter variables are needed to avoid name conflicts when redeclaring an \r
+     * enumeration with the same name.\r
      * \r
      * @return Declaration of a module instance\r
      */\r
     public String getDeclaration() {\r
-       StringBuilder sb = new StringBuilder();\r
-       sb.append("    ");\r
-       sb.append(getType().getName());\r
-       sb.append(" ");\r
-       sb.append(getName());\r
+       \r
+       StringBuilder parameters = new StringBuilder();\r
+       StringBuilder redeclarations = new StringBuilder();\r
+       \r
+       \r
        if(!getRedeclarations().isEmpty()) {\r
-               sb.append("(");\r
+               redeclarations.append("(");\r
                Iterator<Redeclaration> i = getRedeclarations().iterator();\r
                while(i.hasNext()) {\r
                        Redeclaration rd = i.next();\r
-                       sb.append(rd.getRedeclaration());\r
+                       redeclarations.append(rd.getRedeclaration());\r
+                       parameters.append(rd.getParameters());\r
                        if(i.hasNext())\r
-                               sb.append(",");\r
+                               redeclarations.append(",");\r
                }\r
-               sb.append(")");\r
+               redeclarations.append(")");\r
        }\r
+       \r
+       \r
+       StringBuilder sb = new StringBuilder();\r
+       sb.append(parameters);\r
+       sb.append("    ");\r
+       sb.append(getType().getName());\r
+       sb.append(" ");\r
+       sb.append(getName());\r
+       sb.append(redeclarations.toString());\r
        sb.append(";\n");\r
        return sb.toString();\r
     }\r
index b048bbdbcb344c27189613f98eb0a6f853e6d6cd..cdf33978734847316c43177899117852b0aa29e5 100644 (file)
@@ -27,18 +27,60 @@ public class Redeclaration {
        if(replacedEnumeration == null || replacingEnumeration == null) {\r
                return "";\r
        }\r
+       String separator = getSeparator();\r
        StringBuilder sb = new StringBuilder();\r
        sb.append(replacedEnumeration.getName());\r
        sb.append(".size = ");\r
        sb.append(replacingEnumeration.getName());\r
-       sb.append(".size, ");\r
+       sb.append(separator + "size, ");\r
        sb.append(replacedEnumeration.getName());\r
        sb.append(".elements = ");\r
        sb.append(replacingEnumeration.getName());\r
-       sb.append(".elements");\r
+       sb.append(separator + "elements");\r
        \r
        return sb.toString();\r
     }\r
+    \r
+    /**\r
+     * Change the name of the re-declaration parameters to enable the use of \r
+     * enumerations with same names.\r
+     * \r
+     * @return\r
+     */\r
+    public String getParameters() {\r
+       String separator = getSeparator();\r
+       StringBuilder sb = new StringBuilder();\r
+       sb.append("    parameter Integer " + replacingEnumeration.getName() + separator + "size = " + replacingEnumeration.getName() + ".size;\n");\r
+       sb.append("    parameter Integer " + replacingEnumeration.getName() + separator + "elements[:] = " + replacingEnumeration.getName() + ".elements;\n");\r
+       return sb.toString();\r
+    }\r
+    \r
+    private String getSeparator() {\r
+       return getSeparator(this, "_");\r
+    }\r
+\r
+    /**\r
+     * Add enough separation marks that name conflicts would not exist.\r
+     * \r
+     * @param redeclaration\r
+     * @param separator\r
+     * @return\r
+     */\r
+    private String getSeparator(Redeclaration redeclaration, String separator) {\r
+       for(IElement e : redeclaration.replacedEnumeration.getParentConfiguration().getElements()) {\r
+               if(e instanceof Module) {\r
+                       for(Redeclaration rd : ((Module)e).getRedeclarations()) {\r
+                               if(rd.replacedEnumeration.name.equals(redeclaration.replacedEnumeration.name) &&\r
+                                               rd.replacingEnumeration.equals(redeclaration.replacedEnumeration)) {\r
+                                       String separatorTmp = getSeparator(rd, separator);\r
+                                       if(separatorTmp.length() > separator.length())\r
+                                               separator = separatorTmp;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       return separator + "_";\r
+    }\r
 \r
        public Enumeration getReplacedEnumeration() {\r
                return replacedEnumeration;\r