import org.simantics.sysdyn.modelImport.model.SketchComment;\r
import org.simantics.sysdyn.modelImport.model.SketchConnection;\r
import org.simantics.sysdyn.modelImport.model.SketchElement;\r
+import org.simantics.sysdyn.modelImport.model.SketchObject;\r
import org.simantics.sysdyn.modelImport.model.SketchValve;\r
import org.simantics.sysdyn.modelImport.model.SketchVariable;\r
import org.simantics.sysdyn.modelImport.model.Stock2;\r
continue;\r
\r
StringBuilder buffer = new StringBuilder();\r
+ \r
do {\r
if (line.endsWith("\\"))\r
buffer.append(line.substring(0, line.length()-1));\r
continue;\r
}\r
\r
+ Variable2 ss = MdlUtils.getPossibleSubscriptVariable(str, category, mdl);\r
+ if (ss != null) {\r
+ Variable2 original = mdl.getVariable(ss.getName());\r
+ if (original == null) {\r
+ mdl.addVariable(ss, category);\r
+ }\r
+ else {\r
+ original.mergeExpression(ss);\r
+ }\r
+ continue;\r
+ }\r
+ \r
+ Variable2 lu = MdlUtils.getPossibleLookUpVariable(str, category);\r
+ if (lu != null) {\r
+ System.err.println("found lookup variable "+lu.getName());\r
+ continue;\r
+ }\r
+ \r
+ Variable2 plain = MdlUtils.getPossibleNoExpressionVariable(str);\r
+ if (plain != null) {\r
+ System.err.println("found plain variable "+plain.getName());\r
+ continue;\r
+ }\r
+ \r
Subscript2 subscript = MdlUtils.getPossibleSubscript(str, mdl);\r
if (subscript != null) {\r
mdl.addSubscript(subscript);\r
if (line.isEmpty())\r
continue;\r
\r
+ SketchObject so;\r
+ \r
if (line.startsWith(SKETCH_START)) {\r
sketch = new Sketch2();\r
mdl.addSketch(sketch);\r
- continue;\r
}\r
else if (line.startsWith(SKETCH_VERSION)) {\r
// version declaration, nothing to do here\r
- continue;\r
}\r
else if (line.startsWith("*")) {\r
sketch.setName(line.substring(1));\r
- continue;\r
}\r
else if (line.startsWith("$")) {\r
// font declaration, nothing to do here\r
- continue;\r
}\r
-\r
- SketchConnection connection = MdlUtils.getPossibleSketchConnection(line);\r
- if (connection != null) {\r
- sketch.addConnection(connection);\r
- continue;\r
+ else if ((so = MdlUtils.getPossibleSketchConnection(line)) != null) {\r
+ sketch.addConnection((SketchConnection)so);\r
}\r
-\r
- SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, mdl);\r
- if (variable != null) {\r
- sketch.addVariable(variable);\r
- continue;\r
+ else if ((so = MdlUtils.getPossibleSketchVariable(line, mdl)) != null) {\r
+ sketch.addVariable((SketchVariable)so);\r
}\r
-\r
- SketchValve valve = MdlUtils.getPossibleSketchValve(line);\r
- if (valve != null) {\r
+ else if ((so = MdlUtils.getPossibleSketchValve(line)) != null) {\r
+ SketchValve valve = (SketchValve)so;\r
// the next row after a valve should always the variable associated with the valve\r
SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), mdl);\r
if (attached == null || !attached.isAttached()) {\r
System.err.println("attached variable not found for valve");\r
- continue;\r
}\r
valve.setAttachedVariable(attached);\r
- sketch.addValve(valve);\r
+ sketch.addValve((SketchValve)valve);\r
sketch.addVariable(attached);\r
- continue;\r
}\r
-\r
- SketchComment comment = MdlUtils.getPossibleSketchComment(line);\r
- if (comment != null) {\r
+ else if ((so = MdlUtils.getPossibleSketchComment(line)) != null) {\r
+ SketchComment comment = (SketchComment)so;\r
if (comment.hasTextLine()) {\r
comment.setText(reader.readLine());\r
}\r
sketch.addComment(comment);\r
- continue;\r
}\r
-\r
- // if we got this far, the element could not be parsed\r
- System.err.println("unrecognized element "+line);\r
+ else {\r
+ // if we got this far, the element could not be parsed\r
+ System.err.println("unrecognized element "+line);\r
+ }\r
+ \r
} while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END));\r
\r
return line;\r
import org.simantics.sysdyn.modelImport.model.DelayExpression;\r
import org.simantics.sysdyn.modelImport.model.IExpression;\r
import org.simantics.sysdyn.modelImport.model.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.LookupExpression;\r
import org.simantics.sysdyn.modelImport.model.MdlModel;\r
import org.simantics.sysdyn.modelImport.model.NormalExpression;\r
import org.simantics.sysdyn.modelImport.model.Range;\r
import org.simantics.sysdyn.modelImport.model.SketchValve;\r
import org.simantics.sysdyn.modelImport.model.SketchVariable;\r
import org.simantics.sysdyn.modelImport.model.Subscript2;\r
+import org.simantics.sysdyn.modelImport.model.SubscriptExpression;\r
import org.simantics.sysdyn.modelImport.model.Variable2;\r
import org.simantics.sysdyn.modelImport.model.Valve2.TextPosition;\r
\r
public enum CommentIcon {\r
CLOUD, OTHER\r
}\r
+ \r
+ private static final String NUMBER = \r
+ "-?\\d+(?:.\\d+)?";\r
\r
// most of this data is from the documentation of the .mdl file format \r
// available in http://www.vensim.com/documentation/24305.htm\r
private static final int subscriptVariableUnit = 4;\r
private static final int subscriptVariableDesc = 5;\r
\r
- public static Variable2 getPossibleSubscriptVariable(String line, String category) {\r
- return null;\r
+ public static Variable2 getPossibleSubscriptVariable(String line, String category, MdlModel mdl) {\r
+ Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line);\r
+ \r
+ if (!matcher.matches()) {\r
+ return null;\r
+ }\r
+ \r
+ String name = matcher.group(subscriptVariableName);\r
+ // TODO: find out if subscript indices can contain commas and update\r
+ // this method accordingly\r
+ String[] indices = matcher.group(subscriptVariableIndices).split(",");\r
+ IExpression expression = parseEquation(matcher.group(subscriptVariableEquation));\r
+ SubscriptExpression e = new SubscriptExpression(indices, expression);\r
+ \r
+ String unit = matcher.group(subscriptVariableUnit);\r
+ Range range = parseRange(unit);\r
+ if (range != null) {\r
+ unit = unit.substring(0, unit.indexOf(range.originalString())).trim();\r
+ }\r
+ String description = matcher.group(subscriptVariableDesc);\r
+ \r
+ return new Variable2(name, e, unit, range, description);\r
+ }\r
+ \r
+ private static final String LOOKUP_NAME =\r
+ "([A-Za-z](?: *\\w+)*)";\r
+ private static final String LOOKUP_RANGE =\r
+ "\\[\\(("+NUMBER+"),("+NUMBER+")\\)-\\(("+NUMBER+"),("+NUMBER+")\\)\\]";\r
+ private static final String LOOKUP_POINTS =\r
+ "(\\("+NUMBER+","+NUMBER+"\\)(?:,\\("+NUMBER+","+NUMBER+"\\))*)";\r
+ \r
+ private static final String LOOKUP_PATTERN =\r
+ LOOKUP_NAME+"\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)\\s*~\\s*";\r
+\r
+ public static final String lookupVariablePattern = \r
+ LOOKUP_PATTERN+UNIT_PATTERN+DESC_PATTERN;\r
+ \r
+ private static final int lookupVariableName = 1;\r
+ private static final int lookupVariableRangeXMin = 2;\r
+ private static final int lookupVariableRangeYMin = 3;\r
+ private static final int lookupVariableRangeXMax = 4;\r
+ private static final int lookupVariableRangeYMax = 5;\r
+ private static final int lookupVariablePoints = 6;\r
+ private static final int lookupVariableUnit = 7;\r
+ private static final int lookupVariableDesc = 8;\r
+ \r
+ public static Variable2 getPossibleLookUpVariable(String line, String category) {\r
+ Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line);\r
+ \r
+ if (!matcher.matches()) {\r
+ return null;\r
+ }\r
+ \r
+ String name = matcher.group(lookupVariableName);\r
+ double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin));\r
+ double xMax = Double.parseDouble(matcher.group(lookupVariableRangeYMin));\r
+ double yMin = Double.parseDouble(matcher.group(lookupVariableRangeXMax));\r
+ double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax));\r
+ String points = matcher.group(lookupVariablePoints);\r
+ \r
+ LookupExpression e = new LookupExpression(xMin, yMin, xMax, yMax, points);\r
+ \r
+ String unit = matcher.group(lookupVariableUnit);\r
+ Range range = parseRange(unit);\r
+ if (range != null) {\r
+ unit = unit.substring(0, unit.indexOf(range.originalString())).trim();\r
+ }\r
+ String description = matcher.group(lookupVariableDesc);\r
+ \r
+ return new Variable2(name, e, unit, range, description);\r
+ }\r
+ \r
+ public static final String NO_EXP_PATTERN = \r
+ VAR_NAME+"\\s*~\\s*";\r
+ \r
+ public static final String noExpressionVariablePattern = \r
+ NO_EXP_PATTERN+UNIT_PATTERN+DESC_PATTERN;\r
+ \r
+ private static final int noExpVariableName = 1;\r
+ private static final int noExpVariableUnit = 2;\r
+ private static final int noExpVariableDesc = 3;\r
+ \r
+ public static Variable2 getPossibleNoExpressionVariable(String line) {\r
+ Matcher matcher = Pattern.compile(noExpressionVariablePattern).matcher(line);\r
+ \r
+ if (!matcher.matches()) {\r
+ return null;\r
+ }\r
+ \r
+ String name = normalize(matcher.group(noExpVariableName));\r
+ \r
+ String unit = matcher.group(noExpVariableUnit);\r
+ Range range = parseRange(unit);\r
+ if (range != null) {\r
+ unit = unit.substring(0, unit.indexOf(range.originalString())).trim();\r
+ }\r
+ String description = matcher.group(noExpVariableDesc);\r
+ \r
+ return new Variable2(name, null, unit, range, description);\r
}\r
\r
private static final String SUBSCRIPT_PATTERN =\r
return new NormalExpression(normalize(equation));\r
}\r
\r
- private static final String NUMBER = \r
- "-?\\d+(?:.\\d+)?";\r
- \r
private static Range parseRange(String unit) {\r
Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(("+NUMBER+"),("+NUMBER+")|\\?)\\]").matcher(unit);\r
if (matcher.find()) {\r
}\r
}\r
\r
- private static final String categoryPattern = "\\*{56}\\s+\\.(\\S+)\\s+\\*{56}.*";\r
+ private static final String categoryPattern = "\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*";\r
\r
public static String getPossibleCategory(String line) {\r
Matcher matcher = Pattern.compile(categoryPattern).matcher(line);\r