]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Removed spreadsheet remover and improved sheet handling (refs #3473)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 28 Nov 2012 09:24:26 +0000 (09:24 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 28 Nov 2012 09:24:26 +0000 (09:24 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26473 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java

index b55a46c0209c3e03c08707dc293d6c879602e757..b62398e344c7b677a6e03eb55da0c3b9adeabd9a 100644 (file)
@@ -13,10 +13,10 @@ package org.simantics.modelica;
 \r
 import java.io.File;\r
 import java.io.FileNotFoundException;\r
+import java.io.FilenameFilter;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
 import java.io.PrintStream;\r
-import java.io.RandomAccessFile;\r
 import java.net.URL;\r
 import java.net.URLDecoder;\r
 import java.util.ArrayList;\r
@@ -471,19 +471,29 @@ public class ModelicaManager {
             Process p = runOMC(simulationLocation.fullModelDir, null, parameters);\r
             p.waitFor();\r
 \r
-            // Find the last line of the full description. This will be the name of the created simulation files\r
-            String lastLine = readLastLine(simulationLocation.fullModel.getAbsolutePath());\r
-            \r
-            if(lastLine != null && lastLine.startsWith("end ")) {\r
-                String lastClass = lastLine.substring(4, lastLine.length() -1); // e.g. "end xidz;" -> "xidz"\r
-                lastClass = lastClass + "_init.xml";\r
-                File initFile = new File(simulationLocation.fullModelDir, lastClass);\r
-                if(initFile != null && initFile.isFile()) {\r
-                    // Replace original init contents with the new contents\r
-                    replaceInitFileContents(simulationLocation.initFile.getAbsolutePath(), initFile.getAbsolutePath(), experimentParameters);\r
+            // Find the new init file\r
+            FilenameFilter initFilter = new FilenameFilter() {\r
+\r
+                @Override\r
+                public boolean accept(File dir, String name) {\r
+                    if(name.endsWith("_init.xml"))\r
+                        return true;\r
+                    else\r
+                        return false;\r
                 }\r
+            };\r
+\r
+            File initFile = null;\r
+            for(File f : simulationLocation.fullModelDir.listFiles(initFilter)) {\r
+                initFile = f;\r
+                break;\r
             }\r
-            \r
+\r
+            if(initFile != null && initFile.isFile()) {\r
+                // Replace original init contents with the new contents\r
+                replaceInitFileContents(simulationLocation.initFile.getAbsolutePath(), initFile.getAbsolutePath(), experimentParameters);\r
+            }\r
+\r
         } catch (ModelicaException e) {\r
             e.printStackTrace();\r
         } catch (InterruptedException e) {\r
@@ -568,48 +578,4 @@ public class ModelicaManager {
         }\r
     }\r
     \r
-    /**\r
-     * utility for reading the list line of a file\r
-     * @param fileName\r
-     * @return\r
-     */\r
-    private static String readLastLine( String fileName ) {\r
-        try {\r
-            File file = new File( fileName );\r
-            RandomAccessFile fileHandler = new RandomAccessFile( file, "r" );\r
-            long fileLength = file.length() - 1;\r
-            StringBuilder sb = new StringBuilder();\r
-     \r
-            for( long filePointer = fileLength; filePointer != -1; filePointer-- ) {\r
-                fileHandler.seek( filePointer );\r
-                int readByte = fileHandler.readByte();                \r
-     \r
-                if( readByte == 0xA ) {\r
-                    if( filePointer == fileLength ) {\r
-                        continue;\r
-                    } else {\r
-                        break;\r
-                    }\r
-                } else if( readByte == 0xD ) {\r
-                    if( filePointer == fileLength - 1 ) {\r
-                        continue;\r
-                    } else {\r
-                        break;\r
-                    }                    \r
-                }\r
-     \r
-                sb.append( ( char ) readByte );\r
-            }\r
-     \r
-            String lastLine = sb.reverse().toString();\r
-            \r
-            fileHandler.close();\r
-            return lastLine;\r
-        } catch( java.io.FileNotFoundException e ) {\r
-            e.printStackTrace();\r
-        } catch( java.io.IOException e ) {\r
-            e.printStackTrace();\r
-        }\r
-        return null;\r
-    }\r
 }\r
index 31e0c5bd70d015bdf997bb1d914517550f1d028c..1d5c8616772f91516f6361a79e7d06e58acc9f75 100644 (file)
@@ -125,7 +125,7 @@ public class ResourceSelectionProcessor implements SelectionProcessor<Object, Re
                 if(var != null)\r
                     r = var.getRepresents(backend);\r
             }\r
-\r
+            \r
             if(r == null) {\r
                 // SharedFunctionsFolder has properties but no adapted resource\r
                 SharedFunctionsFolder sff = AdaptionUtils.adaptToSingle(selection, SharedFunctionsFolder.class);\r
@@ -167,6 +167,14 @@ public class ResourceSelectionProcessor implements SelectionProcessor<Object, Re
                         r = connection;\r
                 }\r
             }\r
+            \r
+            \r
+            // Check that var has found the correct variable\r
+            if(r != null && var != null) {\r
+                if(!r.equals(var.getRepresents(backend)))\r
+                    // Var found the wrong variable. This may happen with ModuleType editors\r
+                    var = null;\r
+            }\r
 \r
             // Independent variable\r
             if (backend.isInstanceOf(r, sr.IndependentVariable)) {\r
index 7d97c24bd8c5a00362479845d9fdeccad4badc4d..3533d6a981f1f059183a865ccf5c5ad050216ec4 100644 (file)
@@ -307,7 +307,7 @@ public class SysdynResult {
                            ma = accessor.getComponent( ref );\r
                     } catch (AccessorConstructionException e2) {\r
                         // Unable to read data from file\r
-                        e2.printStackTrace();\r
+                        System.err.println("Variable " + variable + " not found");\r
                         return null;\r
                     }\r
                                } \r
index d65c07b1afa01ac5a024001dc5feb6a76cb0fad7..2fe8ba6cb93d6b7adc82faf81db7d8b3a38d8ac0 100644 (file)
@@ -64,6 +64,9 @@ public class ModelicaWriter {
         b.append("    parameter Integer elements[:];\n");\r
         b.append("end Enumeration_class;\n\n");\r
         \r
+        // Spreadsheets are built when variables use values from them, so we cannot insert them yet\r
+        int spreadsheetlocation = b.length();\r
+        \r
         HashSet<String> sheetNames = new HashSet<String>();\r
         for(Sheet sheet : getSpreadSheets(configurations))\r
             sheetNames.add(sheet.getName());\r
@@ -81,9 +84,10 @@ public class ModelicaWriter {
         \r
         b.append("end " + modelName + ";\n\n");\r
         \r
-        b.append(getGlobalSpreadSheets(configurations));\r
-        \r
+        // Insert spreadsheets at this point. \r
+        b.insert(spreadsheetlocation, getGlobalSpreadSheets(configurations));\r
 \r
+        \r
         return b.toString();\r
     }\r
     \r
index daefd69c16b8ff8236b8e1fb6534471807c3eb62..a2deef14ffda0e5cc4c8df3e68024f0f351be3ca 100644 (file)
@@ -52,22 +52,24 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable {
     \r
     @UpdateMethod\r
     public boolean updateCells(ReadGraph g, Resource r) throws DatabaseException {\r
-        this.resource = r;\r
-        \r
-        g.getObjects(r, Layer0.getInstance(g).ConsistsOf);\r
-        \r
-        Variable v = g.adapt(r, Variable.class);\r
         cells.clear();\r
         usedRanges.clear();\r
-        for(Variable child : v.getChildren(g)) {\r
-            String name = child.getName(g);\r
-            try {\r
-                SpreadsheetUtils.decodeCellAbsolute(name);\r
-                Variant value = child.getPropertyValue(g, SheetVariables.CONTENT, Bindings.VARIANT);\r
-                cells.put(name, value.getValue());\r
-            } catch (CellParseException e) {\r
-            } catch (MissingVariableException e) {\r
-                System.out.println("missing content for: " + name);\r
+        this.resource = null;\r
+\r
+        if(g.hasStatement(r)) {\r
+            this.resource = r;\r
+            g.getObjects(r, Layer0.getInstance(g).ConsistsOf);\r
+            Variable v = g.adapt(r, Variable.class);\r
+            for(Variable child : v.getChildren(g)) {\r
+                String name = child.getName(g);\r
+                try {\r
+                    SpreadsheetUtils.decodeCellAbsolute(name);\r
+                    Variant value = child.getPropertyValue(g, SheetVariables.CONTENT, Bindings.VARIANT);\r
+                    cells.put(name, value.getValue());\r
+                } catch (CellParseException e) {\r
+                } catch (MissingVariableException e) {\r
+                    System.out.println("missing content for: " + name);\r
+                }\r
             }\r
         }\r
         return true;\r