*******************************************************************************/\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
* \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
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
}\r
} catch (IOException e) {\r
return null;\r
- } \r
+ }\r
\r
}\r
\r
* 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
// 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
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
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
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
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
}\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
* @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
}\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
}\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
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