]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Changed sysdyn result reader to buffered reader. Removed line counting of result...
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Oct 2012 07:23:14 +0000 (07:23 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Oct 2012 07:23:14 +0000 (07:23 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26007 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java

index a21a21554827d8c8a402b844108e3b59ea4e8c39..03325da6f77947a199582532798c2cb0882f7714 100644 (file)
@@ -11,6 +11,7 @@
  *******************************************************************************/\r
 package org.simantics.modelica.data;\r
 \r
+import java.io.BufferedInputStream;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.FileNotFoundException;\r
@@ -45,13 +46,14 @@ import org.xml.sax.SAXException;
  * \r
  * Will be replaced with a common OpenModelica plugin\r
  * @author Hannu Niemistö\r
+ * @author Tuomas Miettinen\r
  */\r
-public class SimulationResult {    \r
+public class SimulationResult {\r
 \r
     protected List<DataSet> variables = new ArrayList<DataSet>();\r
     protected List<DataSet> initials = new ArrayList<DataSet>();\r
     protected int numberOfLines = 0;\r
-    \r
+\r
     /**\r
      * Private class used in displaying errors\r
      */\r
@@ -83,7 +85,7 @@ public class SimulationResult {
                     stream = null;\r
                     return b.toString();\r
                 }\r
-                else if(c == '\n') \r
+                else if(c == '\n')\r
                     return b.toString();\r
                 else if(c == '\r')\r
                     ;\r
@@ -92,7 +94,7 @@ public class SimulationResult {
             }\r
         } catch (IOException e) {\r
             return null;\r
-        }           \r
+        }\r
 \r
     }\r
 \r
@@ -102,13 +104,13 @@ public class SimulationResult {
      * Supports both txt and xml type inits.\r
      * \r
      * @param file inits-file\r
-     * @throws FileNotFoundException \r
+     * @throws FileNotFoundException\r
      * @throws IOException\r
      */\r
     public void readInits(File file) throws FileNotFoundException, IOException {\r
 \r
         if(file.getName().endsWith("txt")) {\r
-            InputStream is = new FileInputStream(file);\r
+            InputStream is = openStream(file);\r
             readInitsTXT(is);\r
             is.close();\r
         } else if(file.getName().endsWith("xml")) {\r
@@ -134,24 +136,24 @@ public class SimulationResult {
             // Find start time\r
             node = (Node)xpath.evaluate\r
                     ("//fmiModelDescription/DefaultExperiment/@startTime",\r
-                            doc, \r
+                            doc,\r
                             XPathConstants.NODE);\r
             times[0] = Double.parseDouble(node.getNodeValue());\r
             // Find end time\r
             node = (Node)xpath.evaluate\r
                     ("//fmiModelDescription/DefaultExperiment/@stopTime",\r
-                            doc, \r
+                            doc,\r
                             XPathConstants.NODE);\r
             times[1] = Double.parseDouble(node.getNodeValue());\r
-            \r
+\r
             NodeList nodes;\r
-            \r
+\r
             // Find parameters and constants\r
             nodes = (NodeList)xpath.evaluate\r
                     ("//fmiModelDescription/ModelVariables/ScalarVariable[@variability='parameter']",\r
-                            doc, \r
+                            doc,\r
                             XPathConstants.NODESET);\r
-            \r
+\r
             // Add parameters and constants to initials\r
             for (int idx = 0; idx < nodes.getLength(); idx++) {\r
                 Node n = nodes.item(idx);\r
@@ -159,13 +161,13 @@ public class SimulationResult {
                 addToInitials(name, n, times);\r
 \r
             }\r
-            \r
+\r
             // Find all alias variables\r
             NodeList allAliasVariables = (NodeList)xpath.evaluate\r
                     ("//fmiModelDescription/ModelVariables/ScalarVariable[@alias='alias']",\r
-                            doc, \r
+                            doc,\r
                             XPathConstants.NODESET);\r
-            \r
+\r
             for(int i = 0; i < allAliasVariables.getLength(); i++) {\r
                 Node n = allAliasVariables.item(i);\r
                 String name = n.getAttributes().getNamedItem("name").getNodeValue();\r
@@ -175,13 +177,13 @@ public class SimulationResult {
                 String aliasVariableName = aliasVariableProperty.getNodeValue();\r
                 n = (Node)xpath.evaluate\r
                         ("//fmiModelDescription/ModelVariables/ScalarVariable[@name='" + aliasVariableName +"' and @variability='parameter']",\r
-                                doc, \r
+                                doc,\r
                                 XPathConstants.NODE);\r
                 // Add the value of the original value to initials with the name of the alias variable\r
                 if(n != null)\r
                     addToInitials(name, n, times);\r
             }\r
-            \r
+\r
 \r
         } catch (SAXException e) {\r
             e.printStackTrace();\r
@@ -195,10 +197,10 @@ public class SimulationResult {
             e.printStackTrace();\r
         }\r
     }\r
-    \r
+\r
     /**\r
-     * Adds a new dataset to initials using start name, value from \r
-     * node and times. \r
+     * Adds a new dataset to initials using start name, value from\r
+     * node and times.\r
      * \r
      * @param name Name for the dataset to be added to initials\r
      * @param node Node containing the start value attribute\r
@@ -215,8 +217,8 @@ public class SimulationResult {
                 Double d = Double.parseDouble(child.getAttributes().getNamedItem("start").getNodeValue());\r
                 values = new double[] {d,d};\r
                 initials.add(\r
-                        new DataSet(name, \r
-                                times , \r
+                        new DataSet(name,\r
+                                times ,\r
                                 values));\r
                 break;\r
             }\r
@@ -262,16 +264,16 @@ public class SimulationResult {
     }\r
 \r
 \r
-    \r
+\r
     /**\r
      * Read result file. Read all values in the result file.\r
-     *  \r
+     * \r
      * @param file result file\r
      * @throws FileNotFoundException\r
      * @throws IOException\r
      */\r
     public void read(File file) throws FileNotFoundException, IOException {\r
-       read(file, 1);\r
+        read(file, 1);\r
     }\r
 \r
 \r
@@ -284,24 +286,31 @@ public class SimulationResult {
      * @throws IOException\r
      */\r
     public void read(File file, int interval) throws FileNotFoundException, IOException {\r
-        // First check the number of time steps\r
-        FileReader fr = new FileReader(file);\r
-        LineNumberReader  lnr = new LineNumberReader(fr);\r
-        lnr.skip(Long.MAX_VALUE);\r
-        numberOfLines = lnr.getLineNumber() - 1; // minus the first row, which is for names\r
-        lnr.close();\r
-        fr.close();\r
-\r
-        InputStream is = new FileInputStream(file);\r
+        // First check the number of time steps.\r
+        // With *.mat result file this is not necessary\r
+        if (!(this instanceof MatSimulationResult)) {\r
+            FileReader fr = new FileReader(file);\r
+            LineNumberReader  lnr = new LineNumberReader(fr);\r
+            lnr.skip(Long.MAX_VALUE);\r
+            numberOfLines = lnr.getLineNumber() - 1; // minus the first row, which is for names\r
+            lnr.close();\r
+            fr.close();\r
+        }\r
+        InputStream is = openStream(file);\r
+        //InputStream is = new FileInputStream(file);\r
         read(is, interval);\r
         is.close();\r
     }\r
 \r
+    private InputStream openStream(File file) throws FileNotFoundException {\r
+        return new BufferedInputStream(new FileInputStream(file));\r
+    }\r
+\r
     final static Pattern p1 = Pattern.compile("DataSet: ([^ ]*)");\r
 \r
     /**\r
      * Read result file. The basic implementation supports\r
-     * plt-formatted results. Overridden to support other formats. \r
+     * plt-formatted results. Overridden to support other formats.\r
      * @param stream FileInputStream for the result file\r
      * @param interval the interval of results to be read. 1 reads all time steps, 2 reads every other time step, etc...\r
      */\r
@@ -315,12 +324,11 @@ public class SimulationResult {
         }\r
 \r
         // Data sets\r
-        while(true) {            \r
+        while(true) {\r
             Matcher matcher = p1.matcher(getLine(stream));\r
             if(!matcher.matches())\r
                 return;\r
             String name = matcher.group(1);\r
-\r
             double[] dtimes = new double[numberOfLines];\r
             double[] dvalues = new double[numberOfLines];\r
             int i = 0;\r
@@ -369,7 +377,7 @@ public class SimulationResult {
     }\r
 \r
     /**\r
-     * Gets DataSet for variable. Loops first the variables and then initials.  \r
+     * Gets DataSet for variable. Loops first the variables and then initials.\r
      * @param name the name of the variable\r
      * @return DataSet for the variable or null if DataSet not found\r
      */\r
@@ -377,10 +385,10 @@ public class SimulationResult {
         for(DataSet set : variables)\r
             if(set.name.equals(name))\r
                 return set;\r
-                for(DataSet set : initials)\r
-                    if(set.name.equals(name))\r
-                        return set;\r
-                        return null;\r
+        for(DataSet set : initials)\r
+            if(set.name.equals(name))\r
+                return set;\r
+        return null;\r
     }\r
 \r
 \r