import org.simantics.sysdyn.mdlImport.mdlElements.Connection;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Dependency;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Element;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.EquivalenceSubscript;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Expression;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Flow;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
input.reset();\r
}\r
\r
+ \r
+ boolean isControl = false;\r
+ \r
while (( line = input.readLine()) != null){\r
// Build an element (combine the lines to one string)\r
StringBuilder elementBuilder = new StringBuilder();\r
- while(line != null) {\r
+ while(line != null && !line.contains("\\\\\\---///")) {\r
// Add a new line for the element\r
elementBuilder.append(line);\r
if(line.endsWith("|") && !line.endsWith("~~|")) {\r
line = input.readLine();\r
}\r
\r
- String variable = elementBuilder.toString();\r
- if(variable.contains(".Control")) {\r
- // Start of control variables\r
+ if(line.contains("\\\\\\---///"))\r
break;\r
- } else {\r
- // Add element string to model\r
- mdlFile.addVariable(variable);\r
- }\r
- }\r
-\r
- while (( line = input.readLine()) != null && !line.contains("\\\\\\---///")){\r
- if(line.trim().isEmpty())\r
- continue;\r
\r
- // Build a control element (combine the lines to one string)\r
- StringBuilder elementBuilder = new StringBuilder();\r
- while(line != null) {\r
- // Add a new line for the element\r
- elementBuilder.append(line);\r
- if(line.endsWith("|") && !line.endsWith("~~|")) {\r
- //Element definition has ended\r
- break;\r
+ String variable = elementBuilder.toString();\r
+\r
+ if(variable.trim().matches("[\\*]{46}.+[\\*]{46}.+")) {\r
+ if(variable.contains(".Control")) {\r
+ isControl = true;\r
+ } else {\r
+ isControl = false;\r
}\r
- line = input.readLine();\r
+ continue;\r
+ }\r
+\r
+ // Add element string to model\r
+ if(isControl) {\r
+ mdlFile.addControl(variable);\r
+ } else {\r
+ mdlFile.addVariable(variable);\r
}\r
- String control = elementBuilder.toString();\r
- mdlFile.addControl(control);\r
}\r
\r
- \r
while (( line = input.readLine()) != null && !line.contains("///---\\\\\\")){\r
mdlFile.addSketchData(line);\r
}\r
}\r
\r
private static void getVariableData(Model model, ArrayList<String> elements) {\r
- for(String elementString : elements) {\r
- createVariable(model, elementString);\r
+ ArrayList<EquivalenceSubscript> equivalenceSubscripts = new ArrayList<EquivalenceSubscript>();\r
+ for(String elementString : elements) {\r
+ Variable v = createVariable(model, elementString);\r
+ if(v instanceof EquivalenceSubscript){\r
+ equivalenceSubscripts.add((EquivalenceSubscript) v);\r
+ }\r
+ }\r
+ \r
+ // All variables are ready, determine subscript equivalences\r
+ for(EquivalenceSubscript es : equivalenceSubscripts) {\r
+ Element e = model.getElement(es.getEquivalentToName());\r
+ if(e != null && e instanceof Subscript) {\r
+ es.setEquivalentTo((Subscript)e);\r
}\r
+ }\r
}\r
\r
\r
\r
}\r
\r
+ private static Variable getVariable(Model model, String name) {\r
+ Element e = model.getElement(name);\r
+ Variable variable = null;\r
+ if(e != null && e instanceof Variable)\r
+ variable = (Variable)e;\r
+ return variable;\r
+ }\r
+ \r
+ private static String[] getNormalVariableNameDataAndRange(String element) {\r
+ String[] nameAndData = element.split("=", 2);\r
+ String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
+ if(nameAndData.length == 2)\r
+ return new String[] {nameAndRange[0], nameAndData[1], nameAndRange.length == 2 ? nameAndRange[1] : null};\r
+ return null;\r
+ }\r
+ \r
+ private static String[] getSubscriptNameAndData(String element) {\r
+ String[] nameAndData = element.split(":");\r
+ if(nameAndData.length == 2)\r
+ return nameAndData;\r
+ return null;\r
+ } \r
+ \r
+ private static String[] getEquivalenceSubscriptNameAndData(String element) {\r
+ String[] nameAndData = element.split("\\<\\-\\>");\r
+ if(nameAndData.length == 2)\r
+ return nameAndData;\r
+ return null;\r
+ }\r
+ \r
+ private static String[] getTableNameDataAndRange(String element) {\r
+ String[] parts = element.split("\\~"); \r
+ if(!parts[0].contains("(") || !parts[0].contains(")"))\r
+ return null;\r
+ String name = element.substring(0, element.indexOf("("));\r
+ String theRest = element.substring(element.indexOf("("));\r
+ String[] nameAndData = {name, theRest};\r
+ String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
+ if(nameAndData.length == 2)\r
+ return new String[] {nameAndRange[0], nameAndData[1], nameAndRange.length == 2 ? nameAndRange[1] : null};\r
+ return nameAndData;\r
+ }\r
+ \r
+ private static String[] getDataVariableNameAndData(String element) {\r
+ String[] nameAndData = {\r
+ element.substring(0, element.indexOf("~")),\r
+ " " + element.substring(element.indexOf("~"))};\r
+ return nameAndData;\r
+ }\r
\r
private static Variable createVariable(Model model, String elementString) {\r
\r
continue;\r
\r
Expression expression = new Expression();\r
- String[] nameAndData = s.split("=", 2); \r
- if(nameAndData.length == 2) {\r
- // A normal variable of some sort\r
- String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
+\r
+ String[] nameAndData = null;\r
+ String name;\r
+ \r
+ // Create the expression based on the expression string\r
+ if((nameAndData = getNormalVariableNameDataAndRange(s)) != null) {\r
\r
- // Get element in the first iteration\r
+ name = nameAndData[0].replace("\"", "");\r
+ variable = getVariable(model, name);\r
if(variable == null) {\r
- Element e = model.getElement(nameAndRange[0]);\r
- if(e != null && e instanceof Variable)\r
- variable = (Variable)e;\r
- if(variable == null) {\r
- variable = new Auxiliary();\r
- variable.setName(nameAndRange[0].replace("\"", ""));\r
- model.addElement(variable);\r
- }\r
+ variable = new Auxiliary();\r
+ variable.setName(name);\r
+ model.addElement(variable);\r
}\r
\r
- \r
- if(nameAndRange.length == 2)\r
- expression.setRange(nameAndRange[1]);\r
- \r
if(!nameAndData[1].trim().endsWith("|")) {\r
// Multiple expressions\r
expression.setExpression(nameAndData[1].trim());\r
} else {\r
String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
expression.setExpression(expressionUnitsAndComments[0].trim());\r
- \r
- expressionUnitsAndComments[1] = expressionUnitsAndComments[1].trim();\r
- \r
- if(expressionUnitsAndComments[1].contains("[") &&\r
- expressionUnitsAndComments[1].contains("]") &&\r
- expressionUnitsAndComments[1].lastIndexOf("]") == expressionUnitsAndComments[1].length() - 1) {\r
- // Range definitions are at the end\r
- String range = expressionUnitsAndComments[1].substring(\r
- expressionUnitsAndComments[1].lastIndexOf("[") + 1, \r
- expressionUnitsAndComments[1].length() - 1);\r
- String[] rangeParts = range.split(",");\r
- \r
- try {\r
- variable.setRangeStart(Double.parseDouble(rangeParts[0]));\r
- if(rangeParts.length >= 2)\r
- variable.setRangeEnd(Double.parseDouble(rangeParts[1]));\r
- if(rangeParts.length >= 3)\r
- variable.setRangeStep(Double.parseDouble(rangeParts[2]));\r
- } catch (NumberFormatException e) {\r
- // Not a double\r
- }\r
- expressionUnitsAndComments[1] = expressionUnitsAndComments[1].substring(0, expressionUnitsAndComments[1].lastIndexOf("["));\r
- }\r
- variable.setUnits(expressionUnitsAndComments[1].trim());\r
- \r
- \r
- variable.setComments(expressionUnitsAndComments[2].trim());\r
}\r
- } else {\r
- // SUBSCRIPT\r
- String[] subscript = s.split(":"); \r
- if(subscript.length == 2) {\r
- // Subscript\r
+ \r
+ } else if((nameAndData = getSubscriptNameAndData(s)) != null) {\r
+ \r
+ name = nameAndData[0].replace("\"", "");\r
+ variable = getVariable(model, name);\r
+ if(variable == null) {\r
variable = new Subscript();\r
+ variable.setName(name);\r
model.addElement(variable);\r
- variable.setName(subscript[0].replace("\"", ""));\r
- String[] expressionUnitsAndComments = subscript[1].split("[\\~|\\|]");\r
- expression.setExpression(expressionUnitsAndComments[0].trim());\r
- variable.setUnits(expressionUnitsAndComments[1].trim());\r
- variable.setComments(expressionUnitsAndComments[2].trim());\r
- } else if(s.contains("(")){\r
- // TABLE: These are actually functions in vensim. When function library is created, use these to create interpolation functions\r
+ }\r
+ \r
+ // No support for multidimensional variables. Don't know what that would mean\r
+ String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+ expression.setExpression(expressionUnitsAndComments[0].trim());\r
+ variable.setUnits(expressionUnitsAndComments[1].trim());\r
+ variable.setComments(expressionUnitsAndComments[2].trim());\r
+ \r
+ } else if((nameAndData = getEquivalenceSubscriptNameAndData(s)) != null) {\r
+ \r
+ name = nameAndData[0].replace("\"", "");\r
+ variable = getVariable(model, name);\r
+ if(variable == null) {\r
+ variable = new EquivalenceSubscript();\r
+ variable.setName(name);\r
+ model.addElement(variable);\r
+ }\r
+ \r
+ // No support for multidimensional variables. Don't know what that would mean\r
+ String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+ expression.setExpression(expressionUnitsAndComments[0].trim());\r
+ variable.setUnits(expressionUnitsAndComments[1].trim());\r
+ variable.setComments(expressionUnitsAndComments[2].trim());\r
+ \r
+ } else if((nameAndData = getTableNameDataAndRange(s)) != null) {\r
+ \r
+ name =(nameAndData[0].replace("\"", ""));\r
+ variable = getVariable(model, name);\r
+ if(variable == null) {\r
variable = new Auxiliary();\r
- variable.setName(s.substring(0, s.indexOf("(")).replace("\"", ""));\r
+ variable.setName(name);\r
model.addElement(variable);\r
- String theRest = s.substring(s.indexOf("(") + 1);\r
- String[] expressionUnitsAndComments = theRest.split("[\\~|\\|]");\r
- variable.setUnits(expressionUnitsAndComments[1].trim());\r
- variable.setComments(expressionUnitsAndComments[2].trim());\r
- // [(0,0)-(2,5)],(0,5),(0.5,3),(1,0.5),(2,0.5) => ,(0,5),(0.5,3),(1,0.5),(2,0.5)\r
- String table = expressionUnitsAndComments[0].trim().split("[\\[|\\]]")[2];\r
- // ,(0,5),(0.5,3),(1,0.5),(2,0.5) => (0,5),(0.5,3),(1,0.5),(2,0.5)\r
- table = table.substring(table.indexOf(",") + 1, table.lastIndexOf(")"));\r
- table = "{" + table + "}";\r
- table = table.replace("(", "{");\r
- table = table.replace(")", "}");\r
- expression.setExpression(table); \r
- } else {\r
- // Just an empty variable, Data-driven variable\r
+ }\r
+ \r
+ String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+ // ([(0,0)-(2,5)],(0,5),(0.5,3),(1,0.5),(2,0.5) => ( ; (0,0)-(2,5) ; ,(0,5),(0.5,3),(1,0.5),(2,0.5)\r
+ String table = expressionUnitsAndComments[0].trim().split("[\\[|\\]]")[2];\r
+ // ,(0,5),(0.5,3),(1,0.5),(2,0.5) => (0,5),(0.5,3),(1,0.5),(2,0.5)\r
+ table = table.substring(table.indexOf(",") + 1, table.lastIndexOf(")"));\r
+ table = "{" + table + "}";\r
+ table = table.replace("(", "{");\r
+ table = table.replace(")", "}");\r
+ expression.setExpression(table); \r
+ \r
+ \r
+ } else if((nameAndData = getDataVariableNameAndData(s)) != null) {\r
+ \r
+ name = nameAndData[0].replace("\"", "");\r
+ variable = getVariable(model, name);\r
+ if(variable == null) {\r
variable = new Auxiliary();\r
- String[] nameUnitsAndComments = s.split("[\\~|\\|]");\r
- variable.setName(nameUnitsAndComments[0].trim().replace("\"", ""));\r
+ variable.setName(name);\r
model.addElement(variable);\r
- expression.setExpression("");\r
- variable.setUnits(nameUnitsAndComments[1].trim());\r
- variable.setComments(nameUnitsAndComments[2].trim());\r
}\r
- }\r
+ \r
+ expression.setExpression("");\r
+ } \r
+ \r
+ if(nameAndData == null || variable == null)\r
+ continue;\r
+ \r
+ // Set possible range for the expression\r
+ if(nameAndData.length == 3)\r
+ expression.setRange(nameAndData[2]);\r
\r
- if(variable != null) {\r
- // Finally add the expression to element\r
- variable.getExpressions().add(expression);\r
+ // Set units and comments for the variable \r
+ if(nameAndData[1].trim().endsWith("|")) {\r
+ String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+ String units = expressionUnitsAndComments[1].trim();\r
+ if(units.contains("[") &&\r
+ units.contains("]") &&\r
+ units.lastIndexOf("]") == units.length() - 1) {\r
+ // Range definitions are at the end\r
+ String range = units.substring(\r
+ units.lastIndexOf("[") + 1, \r
+ units.length() - 1);\r
+ String[] rangeParts = range.split(",");\r
+ \r
+ try {\r
+ variable.setRangeStart(Double.parseDouble(rangeParts[0]));\r
+ if(rangeParts.length >= 2)\r
+ variable.setRangeEnd(Double.parseDouble(rangeParts[1]));\r
+ if(rangeParts.length >= 3)\r
+ variable.setRangeStep(Double.parseDouble(rangeParts[2]));\r
+ } catch (NumberFormatException e) {\r
+ // Not a double\r
+ }\r
+ expressionUnitsAndComments[1] = units.substring(0, units.lastIndexOf("["));\r
+ }\r
+ variable.setUnits(expressionUnitsAndComments[1].trim());\r
+ variable.setComments(expressionUnitsAndComments[2].trim());\r
}\r
+ \r
+ // Finally add the expression to element\r
+ variable.getExpressions().add(expression);\r
}\r
return variable;\r
}\r
String[] data = line.split(",");\r
if(data[0].equals("1")) {\r
// Connections are handled after all elements\r
- String[] connectionData = line.split("[,\\|\\(\\)]");\r
+ String[] connectionData = line.split(",");\r
connections.add(connectionData);\r
\r
} else if(data[0].equals("11")){\r
model.addElement(view, e);\r
}\r
}\r
+\r
e.setX(Integer.parseInt(data[3]) / SCALE); \r
e.setY(Integer.parseInt(data[4]) / SCALE); \r
elementNumbers.put(data[1], e);\r
// Dependency\r
Point2D startPoint = new Point2D.Double(start.getX(), start.getY());\r
Point2D endPoint = new Point2D.Double(end.getX(), end.getY());\r
- Double controlX = Double.parseDouble(connectionData[15]) / SCALE;\r
- Double controlY = Double.parseDouble(connectionData[16]) / SCALE;\r
- Point2D controlPoint = new Point2D.Double(controlX, controlY);\r
+ String controlX = connectionData[13].substring(connectionData[13].indexOf("(") + 1);\r
+ String controlY = connectionData[14].substring(0, connectionData[14].indexOf(")"));\r
+ Point2D controlPoint = new Point2D.Double(Double.parseDouble(controlX) / SCALE, Double.parseDouble(controlY) / SCALE);\r
\r
if(ghostNumbers.contains(connectionData[2]) ||\r
ghostNumbers.contains(connectionData[3])) {\r