]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Improved spreadsheet handling for 1.6: Model sheets can be used in module instances...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 29 May 2012 09:18:27 +0000 (09:18 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 29 May 2012 09:18:27 +0000 (09:18 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25066 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/VariableRVIUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/SheetFormatUtils.java

index 6773376db00cc59cd52e50efdc29128113c8ca65..fa3d62ab16084b4861e0a10aaddc0f16035bbe93 100644 (file)
@@ -43,6 +43,7 @@ import org.simantics.sysdyn.representation.Configuration;
 import org.simantics.sysdyn.representation.Enumeration;\r
 import org.simantics.sysdyn.representation.EnumerationIndex;\r
 import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Model;\r
 import org.simantics.sysdyn.representation.Module;\r
 import org.simantics.sysdyn.representation.Sheet;\r
 import org.simantics.sysdyn.representation.Variable;\r
@@ -184,6 +185,14 @@ public class ExpressionUtils {
                     for(int i = 0; i < parts.length && current != null; i++) {\r
                         current = getElement(current, parts[i]);\r
                     }\r
+                    \r
+                    if(current == null) {\r
+                       // Sheets are currently located in the model root. Try to find the sheet.\r
+                       current = conf.getModuleType().getParent(); // Get module type parent (should be a model)\r
+                       if(current instanceof Model)\r
+                               current = getElement(((Model)current).getModelConfiguration(), parts[0]); // Try to get the sheet\r
+                    }\r
+                    \r
                     if(current != null && current instanceof Sheet) {\r
                         Sheet sheet = (Sheet) current;\r
                         String e = ef.getExpression();\r
index 62118077067090bc99b6a4a8207ca2b7649cbd7c..63143b0e6000d123a4626142df7e0c82c41c49a9 100644 (file)
@@ -59,7 +59,7 @@ public class VariableRVIUtils {
      * @throws DatabaseException\r
      */\r
     private static void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes) throws DatabaseException {\r
-        traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(0), "", "");\r
+        traverseIndexes(g, rvi, rvis, arrayIndexes, 0, "", "");\r
     }\r
 \r
     /**\r
@@ -76,10 +76,10 @@ public class VariableRVIUtils {
      * @param indexNamesSoFar String representation of the indexes so far in name format\r
      * @throws DatabaseException\r
      */\r
-    private static void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes, Resource currentEnumeration, String indexesSoFar, String indexNamesSoFar) throws DatabaseException {\r
+    private static void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes, int currentIndex, String indexesSoFar, String indexNamesSoFar) throws DatabaseException {\r
         SysdynResource sr = SysdynResource.getInstance(g);\r
         // Enumeration indexes of the current enumeration (e.g. the first EnumIndexes in Var[EnumIndexes, EnumIndexes, EnumIndexes])\r
-        Resource enumerationIndexes = g.getPossibleObject(currentEnumeration, sr.Enumeration_enumerationIndexes);\r
+        Resource enumerationIndexes = g.getPossibleObject(arrayIndexes.get(currentIndex), sr.Enumeration_enumerationIndexes);\r
         if(enumerationIndexes == null)\r
             return;\r
         List<Resource> indexes = OrderedSetUtils.toList(g, enumerationIndexes);\r
@@ -87,12 +87,11 @@ public class VariableRVIUtils {
             Boolean b = g.getPossibleRelatedValue(indexes.get(i), sr.EnumerationIndex_showEnumerationIndexInCharts, Bindings.BOOLEAN);\r
             // If this index is not wanted to be shown in charts, the recursion does not go any further and rvis.put() is not called for this enumeration\r
             if(Boolean.TRUE.equals(b)) {\r
-                int arrayIndex = arrayIndexes.indexOf(currentEnumeration);\r
                 // Get the name of the index\r
                 String name = g.getRelatedValue(indexes.get(i), Layer0.getInstance(g).HasName);\r
-                if(arrayIndex < arrayIndexes.size() - 1)\r
+                if(currentIndex < arrayIndexes.size() - 1)\r
                     // If there are still more EnumIndexes, recursively call the function and add current index to indexesSoFar and indexNamesSoFar\r
-                    traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(arrayIndex + 1)\r
+                    traverseIndexes(g, rvi, rvis, arrayIndexes, currentIndex + 1\r
                             indexesSoFar + (i + 1) +",", indexNamesSoFar + (name) +",");\r
                 else {\r
                     // The last enumeration. Add [rvi[1, 1, 1] = rvi[index1, index1, index1]}and so on to the rvis map\r
@@ -129,7 +128,7 @@ public class VariableRVIUtils {
                     // Find all redeclarations\r
                     for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.Module_redeclaration, sr.Redeclaration))) {\r
                         Resource replaced = graph.getSingleObject(redeclaration, sr.Redeclaration_replacedEnumeration);\r
-                        if(enumerations.contains(replaced)) {\r
+                        while(enumerations.contains(replaced)) {\r
                             // Replace the redelcared enumeration in enumerations -list with the replacing enumeration\r
                             enumerations.add(enumerations.indexOf(replaced), graph.getSingleObject(redeclaration, sr.Redeclaration_replacingEnumeration));\r
                             enumerations.remove(replaced);\r
index ab41ec5f629ac89cd9f7b0528df5aede8fa75a5c..090af57a0ec0607fce41b24622ca95f63b19693e 100644 (file)
@@ -50,12 +50,32 @@ public class ModelicaWriter {
         b.append("    parameter Integer size;\n");\r
         b.append("    parameter Integer elements[:];\n");\r
         b.append("end Enumeration_class;\n\n");\r
-\r
+        \r
         for(Configuration conf : configurations)\r
-            writeConfiguration(conf, b);\r
+            writeConfiguration(conf, "SpreadSheetBook", b);\r
+        \r
+        b.append(getGlobalSpreadSheets(configurations));\r
 \r
         return b.toString();\r
     }\r
+    \r
+    /**\r
+     * \r
+     */\r
+    private static String getGlobalSpreadSheets(Collection<Configuration> configurations) {\r
+       StringBuilder sheets = new StringBuilder();\r
+       for(Configuration conf : configurations) {\r
+               if(conf.getModel() != null) {\r
+                       for(IElement e : conf.getElements()) {\r
+                               if(e instanceof Book) {\r
+                                       return ((Book)e).getBook();\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       \r
+       return sheets.toString();\r
+    }\r
 \r
     /**\r
      * Write a single configuration to a given string builder\r
@@ -63,7 +83,7 @@ public class ModelicaWriter {
      * @param configuration Model or module configuration\r
      * @param b String builder\r
      */\r
-    private static void writeConfiguration(Configuration configuration, StringBuilder b) {\r
+    private static void writeConfiguration(Configuration configuration, String spreadSheetClass, StringBuilder b) {\r
         String app;\r
         \r
         boolean game = RepresentationUtils.isGameExperimentActive();\r
@@ -77,7 +97,6 @@ public class ModelicaWriter {
         ArrayList<Dependency> inputDependencies = new ArrayList<Dependency>();\r
         ArrayList<Dependency> outputDependencies = new ArrayList<Dependency>();\r
         HashMap<String, ArrayList<Input>> moduleInputs = new HashMap<String, ArrayList<Input>>();\r
-        Book book = null;\r
 \r
         // Initialize lists\r
         for(IElement element : configuration.getElements()) {\r
@@ -112,9 +131,6 @@ public class ModelicaWriter {
                     // References from child modules\r
                     inputDependencies.add(dependency);\r
                 }\r
-            } else if (element instanceof Book) {\r
-                // Spreadsheet book\r
-                book = (Book)element;\r
             }\r
         }\r
         \r
@@ -126,7 +142,14 @@ public class ModelicaWriter {
         // If the configuration is model configuration, use model name. Otherwise, use configuration name.\r
         ModuleType mt = configuration.getModuleType();\r
         String className = mt != null ? (mt.getName().replace(" ", "")) : (configuration.getName().replace(" ", ""));\r
-        b.append("class ").append(className).append('\n');\r
+        \r
+        b.append("class ").append(className);\r
+        \r
+        // Extend spreadsheetclass to get spreadsheet cell info\r
+        if(spreadSheetClass != null && !spreadSheetClass.isEmpty())\r
+               b.append("\n    extends " + spreadSheetClass + ";");\r
+        \r
+        b.append("\n");\r
 \r
         b.append("// Variable definitions\n");\r
         for(IndependentVariable variable : variables) {\r
@@ -164,11 +187,6 @@ public class ModelicaWriter {
             }\r
         }\r
 \r
-        if(book != null) {\r
-            b.append("// Spreadsheet definition\n");\r
-            b.append(book.markBook());\r
-        }\r
-\r
         boolean initialEquations = false;\r
         for(Stock stock : stocks) {\r
             app = stock.getInitialEquation();\r
@@ -221,12 +239,6 @@ public class ModelicaWriter {
 \r
         b.append("end ").append(className).append(";\n\n");\r
 \r
-\r
-        // Update sheet definitions to contain the elements that were used.\r
-        if(book != null) {\r
-            int s = b.indexOf(book.markBook());\r
-            b.replace(s, s + book.markBook().length(), book.getBook());\r
-        }\r
     }\r
     \r
     /**\r
index c4621a7e788f80ea970f2f4972a31a7c6d8443c9..031d4f43c97154f7960e330be6811c1b62e1972e 100644 (file)
@@ -31,9 +31,16 @@ public class Book extends Variable {
     \r
     public String getBook() {\r
         StringBuilder book = new StringBuilder();\r
+        \r
+        book.append("partial class SpreadSheetBook\n");\r
+        \r
         for(Sheet sheet : sheets)\r
             book.append(sheet.getStringRepresentation());\r
+        \r
+        book.append("end SpreadSheetBook;");\r
+        \r
         return book.toString();\r
+        \r
     }\r
 \r
 }\r
index dbda4ea31a56f047dd72efb939c55f1e61cf4817..3cb5be55770eeea45a3f971c43425f07d46c0f9f 100644 (file)
@@ -16,8 +16,10 @@ import java.util.HashMap;
 \r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedElement;\r
 import org.simantics.objmap.annotations.RelatedElements;\r
 import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.representation.annotations.BuiltinFunctions;\r
 \r
@@ -46,6 +48,9 @@ public class Model {
     \r
     @RelatedValue(SysdynResource.URIs.SysdynModel_variableFilter)\r
     private String variableFilter;\r
+    \r
+    @RelatedElement(SimulationResource.URIs.HasConfiguration)\r
+    private Configuration configuration;\r
 \r
     @RelatedElements(\r
             value = Layer0.URIs.ConsistsOf,\r
@@ -176,4 +181,13 @@ public class Model {
     public FunctionLibrary getBuiltins() {\r
         return builtins;\r
     }\r
+    \r
+    \r
+    /**\r
+     * \r
+     * @return Configuration of this model\r
+     */\r
+    public Configuration getModelConfiguration() {\r
+       return configuration;\r
+    }\r
 }\r
index 241060971fc480b88e57694cbf19fd7ed6729f6b..85331ac2a1c5d2bb660d02665e72a1bf9bcd3384 100644 (file)
@@ -163,7 +163,7 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable {
                 Variant cell = matrix.get(i, j);\r
                 if(cell.getBinding().equals(Bindings.DOUBLE)) {\r
                     array[i][j] = (Double)cell.getValue();\r
-                } else if (cell.getBinding().equals(Bindings.MUTABLE_STRING)) {\r
+                } else if (cell.getBinding().equals(Bindings.MUTABLE_STRING) || (cell.getBinding().equals(Bindings.STRING))) {\r
                     try {\r
                         array[i][j] = Double.parseDouble(cell.getValue().toString());\r
                     } catch (NumberFormatException e) {\r
index 7a5f165bd69f9eecca3a98b6ba16f228d388a7db..e12969f815c37b81fc72c4385aa3de21ed8a366f 100644 (file)
@@ -71,7 +71,6 @@ public enum Variability {
     private static boolean isParameter(IndependentVariable variable, Configuration configuration, String reference, boolean allowVariables) {\r
         // Check if references are references to sheets or enumerations. \r
         // Sheet and Enumeration references are allowed, since sheets contain only constants / parameters\r
-\r
         String r = reference.split("\\.")[0]; \r
         for(IElement element : configuration.getElements()) {\r
             if(element instanceof Module) {\r
@@ -124,6 +123,27 @@ public enum Variability {
                 }\r
             }\r
         }\r
+        \r
+        // Try to find sheet in another way: this might be a module type configuration. Find the model configuration and its sheet\r
+        if(configuration.getModuleType() != null) {\r
+               Object parent = configuration.getModuleType().getParent();\r
+               if(parent != null && parent instanceof Model) {\r
+                       configuration = ((Model)parent).getModelConfiguration();\r
+                for(IElement element : configuration.getElements()) {\r
+                       if(element instanceof Book) {\r
+                        for(Sheet sheet : ((Book)element).getSheets()) {\r
+                            if(r.equals(sheet.getName())) {\r
+                                return true;\r
+                            }\r
+                        }\r
+                        break;\r
+                       }\r
+                }\r
+               }\r
+        }\r
+        \r
+        \r
+        \r
         // If there was no sheet for this reference name, or there was some other problem, return false\r
         return false;\r
     }\r
index 918eccbbed34f679cb0c1f48febde7b8b1285ff3..0eb9f750011ea3b0b59a9f62474be9d0fc2feaf9 100644 (file)
@@ -22,6 +22,8 @@ import org.simantics.sysdyn.expressionParser.ParseException;
 import org.simantics.sysdyn.expressionParser.Token;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Model;\r
+import org.simantics.sysdyn.representation.ModuleType;\r
 import org.simantics.sysdyn.representation.Sheet;\r
 import org.simantics.sysdyn.representation.Variable;\r
 \r
@@ -38,6 +40,15 @@ public class SheetFormatUtils {
             for(String key : functionCalls.keySet()) {\r
                 String[] parts = key.split("\\.");\r
                 Object current = v.getParentConfiguration();\r
+                \r
+                // Hack. Sheets can currently exist only in models, not in module types.\r
+                ModuleType moduleType= ((Configuration)current).getModuleType();\r
+                if(moduleType != null) {\r
+                       if(moduleType.getParent() != null && moduleType.getParent() instanceof Model)\r
+                               current = ((Model)moduleType.getParent()).getModelConfiguration();\r
+                }\r
+                // end Hack.\r
+                \r
                 Object found = null;\r
                 for(int i = 0; i < parts.length && current != null; i++) {\r
                     found = null;\r