]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Update vensim import, subscripts might finally work
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 11 Apr 2014 15:14:17 +0000 (15:14 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 11 Apr 2014 15:14:17 +0000 (15:14 +0000)
refs #2924

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/dev-jkauttio@29281 ac1ea38d-2e2b-0410-8846-a27921b304fc

27 files changed:
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java [deleted file]

index 62e8a6686a239ff81b1726ae6919baf0e59812fe..a26110b6a86cc8ae745085687fa19e93585efd5e 100644 (file)
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.HashMap;\r
 \r
 import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon;\r
+import org.simantics.sysdyn.modelImport.mdl.LookupVariable;\r
 import org.simantics.sysdyn.modelImport.mdl.MdlModel;\r
 import org.simantics.sysdyn.modelImport.mdl.Sketch;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchComment;\r
@@ -28,6 +29,9 @@ import org.simantics.sysdyn.modelImport.mdl.SketchElement;
 import org.simantics.sysdyn.modelImport.mdl.SketchObject;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchValve;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
+import org.simantics.sysdyn.modelImport.mdl.Subscript;\r
+import org.simantics.sysdyn.modelImport.mdl.SubscriptVariable;\r
+import org.simantics.sysdyn.modelImport.mdl.Variable;\r
 import org.simantics.sysdyn.modelImport.model.Model;\r
 import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
 import org.simantics.sysdyn.modelImport.model.element.Cloud;\r
@@ -41,7 +45,6 @@ import org.simantics.sysdyn.modelImport.model.element.Valve;
 import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
 import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;\r
 import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public class MdlParser {\r
 \r
@@ -55,15 +58,20 @@ public class MdlParser {
                        e.printStackTrace();\r
                        return null;\r
                }\r
-\r
+               \r
                // generate a sysdyn model from the mdl model\r
+               \r
                Model model = new Model(mdl.getName());\r
 \r
                double offset = 0;\r
                \r
                // add anumerations\r
-               for (Enumeration enumeration : mdl.getSubscripts()) {\r
-                       model.addEnumeration(enumeration);\r
+               for (Subscript subscript : mdl.getAllSubscripts()) {\r
+                       if (subscript.isSuperSet()) {\r
+                               Enumeration enumeration = new Enumeration(subscript.getName(), subscript.getValues());\r
+                               subscript.setEnumeration(enumeration);\r
+                               model.addEnumeration(enumeration);\r
+                       }\r
                }\r
                \r
                // add sketch labels and independent elements\r
@@ -71,27 +79,26 @@ public class MdlParser {
                        \r
                        sketch.setOffset(0, offset);\r
 \r
-                       model.addSymbol(new Comment(0, offset, -1, -1, sketch.getName()));\r
+                       //model.addSymbol(new Comment(0, offset, -1, -1, sketch.getName()));\r
                        \r
                        for (SketchElement element : sketch.getIndependentElements()) {\r
-                               Symbol symbol = element.getSymbol(sketch);\r
+                               Symbol symbol = element.getSymbol(mdl, sketch);\r
                                element.setModelObject(symbol);\r
                                model.addSymbol(symbol);\r
                        }\r
                        \r
                        offset += 200;\r
                }\r
-\r
+               \r
                // add dependent elements\r
                for (Sketch sketch : mdl.getSketches()) {\r
                        for (SketchVariable variable : sketch.getShadowVariables()) {\r
                                if (variable.getVariable() == null) {\r
-                                       System.err.println("null variable");\r
                                        continue;\r
                                }\r
                                ModelVariable original = model.getVariable(variable.getVariable().getName());\r
                                System.err.println("original variable "+original);\r
-                               Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(sketch);\r
+                               Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(mdl, sketch);\r
                                variable.setModelObject(symbol);\r
                                model.addSymbol(symbol);\r
                        }\r
@@ -117,7 +124,9 @@ public class MdlParser {
 \r
        private static MdlModel parseFile(File file) \r
                        throws Exception {\r
-               MdlModel mdl = new MdlModel(file.getName());\r
+               String name = file.getName().substring(0, file.getName().length()-4);\r
+               \r
+               MdlModel mdl = new MdlModel(name);\r
                \r
                // peek at the first line to see if we need to use UTF-8\r
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));\r
@@ -162,26 +171,25 @@ public class MdlParser {
                        \r
                        String cat;\r
                        Variable var;\r
-                       Enumeration sub;\r
+                       SubscriptVariable svar;\r
+                       LookupVariable lvar;\r
+                       Subscript subs;\r
 \r
                        // parse the (possible) variable declaration\r
                        if ((cat = MdlUtil.getPossibleCategory(str)) != null) {\r
                                category = cat;\r
                        }\r
-                       else if ((var = MdlUtil.getPossibleNormalVariable(str)) != null) {\r
+                       else if ((var = Variable.getPossibleVariable(str)) != null) {\r
                                mdl.addVariable(var, category);\r
                        }\r
-                       else if ((var = MdlUtil.getPossibleSubscriptVariable(str)) != null) {\r
-                               mdl.addVariable(var, category);\r
-                       }\r
-                       else if ((var = MdlUtil.getPossibleLookUpVariable(str)) != null) {\r
-                               mdl.addVariable(var, category);\r
+                       else if ((svar = SubscriptVariable.getPossibleSubscriptVariable(str)) != null) {\r
+                               mdl.addVariable(svar, category);\r
                        }\r
-                       else if ((var = MdlUtil.getPossibleNoExpressionVariable(str)) != null) {\r
-                               mdl.addVariable(var, category);\r
+                       else if ((lvar = LookupVariable.getPossibleLookUpVariable(str)) != null) {\r
+                               mdl.addVariable(lvar, category);\r
                        }\r
-                       else if ((sub = MdlUtil.getPossibleSubscript(str, mdl)) != null) {\r
-                               mdl.addSubscript(sub);\r
+                       else if ((subs = Subscript.getPossibleSubscript(str, mdl)) != null) {\r
+                               mdl.addSubscript(subs);\r
                        }\r
                        else {\r
                                // if we got this far, the variable could not be parsed\r
@@ -189,8 +197,6 @@ public class MdlParser {
                        }\r
 \r
                } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START));\r
-               \r
-               // add all variables to the model\r
 \r
                // END READING VARIABLE DATA\r
 \r
@@ -207,6 +213,8 @@ public class MdlParser {
                        // skip empty lines\r
                        if (line.isEmpty())\r
                                continue;\r
+                       \r
+                       ArrayList<SketchConnection> connections = new ArrayList<SketchConnection>();\r
 \r
                        SketchObject so;\r
 \r
@@ -223,7 +231,7 @@ public class MdlParser {
                        else if (line.startsWith("$")) {\r
                                // font declaration, nothing to do here\r
                        }\r
-                       else if ((so = MdlUtil.getPossibleSketchConnection(line)) != null) {\r
+                       else if ((so = MdlUtil.getPossibleSketchConnection(line, sketch)) != null) {\r
                                sketch.addConnection((SketchConnection)so);\r
                        }\r
                        else if ((so = MdlUtil.getPossibleSketchVariable(line, mdl)) != null) {\r
index d1787bbd02806248964b2be01a172742e2f751f9..7b547672f5248af6a8b91f13962aca56a1d42424 100644 (file)
@@ -1,15 +1,33 @@
 package org.simantics.sysdyn.modelImport;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.HashSet;\r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
+import org.simantics.sysdyn.modelImport.mdl.LookupVariable;\r
 import org.simantics.sysdyn.modelImport.mdl.MdlModel;\r
+import org.simantics.sysdyn.modelImport.mdl.Sketch;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchComment;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchConnection;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchElement;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchValve;\r
 import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
+import org.simantics.sysdyn.modelImport.mdl.Subscript;\r
+import org.simantics.sysdyn.modelImport.mdl.SubscriptVariable;\r
+import org.simantics.sysdyn.modelImport.mdl.Variable;\r
+import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
+import org.simantics.sysdyn.modelImport.model.element.Cloud;\r
+import org.simantics.sysdyn.modelImport.model.element.Comment;\r
+import org.simantics.sysdyn.modelImport.model.element.Connection;\r
+import org.simantics.sysdyn.modelImport.model.element.Dependency;\r
+import org.simantics.sysdyn.modelImport.model.element.Flow;\r
+import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.element.Stock;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
+import org.simantics.sysdyn.modelImport.model.element.Valve;\r
+import org.simantics.sysdyn.modelImport.model.element.Valve.Orientation;\r
 import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition;\r
 import org.simantics.sysdyn.modelImport.model.expression.DelayExpression;\r
 import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
@@ -19,7 +37,6 @@ import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;
 import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression;\r
 import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public class MdlUtil {\r
        \r
@@ -32,7 +49,7 @@ public class MdlUtil {
        }\r
        \r
        private static final String NUMBER = \r
-                       "-?\\d+(?:.\\d+)?";\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
@@ -62,12 +79,12 @@ public class MdlUtil {
        \r
        private static final String NORMAL_NAME_PATTERN =\r
                        VAR_NAME_NORMAL+"\\s*=\\s*";\r
+       public static final String NO_EXPRESSION_NAME_PATTERN = \r
+                       VAR_NAME_NORMAL+"\\s*~\\s*";\r
        private static final String SUBSCRIPT_NAME_PATTERN =\r
                        VAR_NAME_SUBSCRIPT+"\\s*=\\s*";\r
        private static final String LOOKUP_NAME_PATTERN =\r
                        LOOKUP_NAME+"\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)\\s*~\\s*";\r
-       public static final String NO_EXPRESSION_NAME_PATTERN = \r
-                       VAR_NAME_NORMAL+"\\s*~\\s*";\r
        \r
        private static final String EXPRESSION_PATTERN =\r
                        "([^~]*?(?:"+VAR_NAME_QUOTED+"[^~]*?)*)\\s*~\\s*";\r
@@ -79,19 +96,27 @@ public class MdlUtil {
        \r
        public static final String normalVariablePattern = \r
                        NORMAL_NAME_PATTERN   +EXPRESSION_PATTERN+UNIT_PATTERN+DESC_PATTERN;\r
+       public static final String noExpressionVariablePattern = \r
+                       NO_EXPRESSION_NAME_PATTERN               +UNIT_PATTERN+DESC_PATTERN;\r
        public static final String subscriptVariablePattern = \r
                        SUBSCRIPT_NAME_PATTERN+EXPRESSION_PATTERN+UNIT_PATTERN+DESC_PATTERN;\r
        public static final String lookupVariablePattern = \r
                        LOOKUP_NAME_PATTERN                      +UNIT_PATTERN+DESC_PATTERN;\r
-       public static final String noExpressionVariablePattern = \r
-                       NO_EXPRESSION_NAME_PATTERN               +UNIT_PATTERN+DESC_PATTERN;\r
        \r
        private static final int normalVariableName = 1;\r
        private static final int normalVariableExpression = 2;\r
+       private static final int normalVariableUnit = 3;\r
+       private static final int normalVariableDesc = 4;\r
+       \r
+       private static final int noExpressionVariableName = 1;\r
+       private static final int noExpressionVariableUnit = 2;\r
+       private static final int noExpressionVariableDesc = 3;\r
        \r
        private static final int subscriptVariableName = 1;\r
        private static final int subscriptVariableIndices = 2;\r
        private static final int subscriptVariableExpression = 3;\r
+       private static final int subscriptVariableUnit = 4;\r
+       private static final int subscriptVariableDesc = 5;\r
        \r
        private static final int lookupVariableName = 1;\r
        private static final int lookupVariableRangeXMin = 2;\r
@@ -99,86 +124,63 @@ public class MdlUtil {
        private static final int lookupVariableRangeXMax = 4;\r
        private static final int lookupVariableRangeYMax = 5;\r
        private static final int lookupVariablePoints = 6;\r
-       \r
-       private static final int noExpressionVariableName = 1;\r
+       private static final int lookupVariableUnit = 7;\r
+       private static final int lookupVariableDesc = 8;\r
        \r
        public static Variable getPossibleNormalVariable(String line) {\r
                Matcher matcher = Pattern.compile(normalVariablePattern).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
-               String name = normalize(matcher.group(normalVariableName));\r
-               Expression expression = parseExpression(matcher.group(normalVariableExpression));\r
+               String name = matcher.group(normalVariableName);\r
+               String expression = matcher.group(normalVariableExpression);\r
+               String unit = matcher.group(normalVariableUnit);\r
+               String desc = matcher.group(normalVariableDesc);\r
                \r
-               return getVariable(matcher, 2, name, expression);\r
+               return new Variable(name, expression, unit, desc);\r
        }\r
        \r
-       public static Variable getPossibleSubscriptVariable(String line) {\r
-               Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+       public static Variable getPossibleNoExpressionVariable(String line) {\r
+               Matcher matcher = Pattern.compile(noExpressionVariablePattern).matcher(line);\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
-               String name = normalize(matcher.group(subscriptVariableName));\r
-               String[] indices = normalize(matcher.group(subscriptVariableIndices)).split(",");\r
-               Expression expression = parseExpression(matcher.group(subscriptVariableExpression));\r
-               EnumerationExpression e = new EnumerationExpression(expression, indices);\r
+               String name = matcher.group(noExpressionVariableName);\r
+               String unit = matcher.group(noExpressionVariableUnit);\r
+               String desc = matcher.group(noExpressionVariableDesc);\r
                \r
-               return getVariable(matcher, 3, name, e);\r
+               return new Variable(name, null, unit, desc);\r
        }\r
        \r
-       public static Variable getPossibleLookUpVariable(String line) {\r
-               Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+       public static SubscriptVariable getPossibleSubscriptVariable(String line) {\r
+               Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line);\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
-               String name = normalize(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
-               double[] points = parseLookup(matcher.group(lookupVariablePoints));\r
-               LookupExpression expression = new LookupExpression(xMin, yMin, xMax, yMax, points);\r
+               String name = matcher.group(subscriptVariableName);\r
+               String[] subscript = matcher.group(subscriptVariableIndices).split(",");\r
+               String expression = matcher.group(subscriptVariableExpression);\r
+               String unit = matcher.group(subscriptVariableUnit);\r
+               String desc = matcher.group(subscriptVariableDesc);\r
                \r
-               return getVariable(matcher, 6, name, expression);\r
+               return new SubscriptVariable(name, expression, unit, desc, subscript);\r
        }\r
        \r
-       private static double[] parseLookup(String points) {\r
-               // a hack to remove the leading empty string otherwise produced by split()\r
-               points = points.replaceFirst("[(),]+", "");\r
-               String[] parts = points.split("[(),]+");\r
-               double[] result = new double[parts.length];\r
-               for (int i = 0; i < parts.length; i++) {\r
-                       result[i] = Double.parseDouble(parts[i]);\r
-               }\r
-               return result;\r
-       }\r
-       \r
-       public static Variable getPossibleNoExpressionVariable(String line) {\r
-               Matcher matcher = Pattern.compile(noExpressionVariablePattern).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+       public static LookupVariable getPossibleLookUpVariable(String line) {\r
+               Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line);\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
-               String name = normalize(matcher.group(noExpressionVariableName));\r
+               String name = matcher.group(lookupVariableName);\r
+               double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin));\r
+               double yMin = Double.parseDouble(matcher.group(lookupVariableRangeYMin));\r
+               double xMax = Double.parseDouble(matcher.group(lookupVariableRangeXMax));\r
+               double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax));\r
+               String expression = matcher.group(lookupVariablePoints);\r
+               String unit = matcher.group(lookupVariableUnit);\r
+               String desc = matcher.group(lookupVariableDesc);\r
                \r
-               return getVariable(matcher, 1, name, null);\r
-       }\r
-       \r
-       public static Variable getVariable(Matcher matcher, int groups, String name, Expression expression) {\r
-               String unit = matcher.group(groups + 1);\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(groups + 2);\r
-               return new Variable(name, expression, unit, range, description);\r
+               return new LookupVariable(name, expression, unit, desc, xMin, yMin, xMax, yMax);\r
        }\r
 \r
        private static final String SUBSCRIPT_PATTERN =\r
@@ -191,129 +193,32 @@ public class MdlUtil {
        private static final int subscriptType = 2;\r
        private static final int subscriptExpression = 3;\r
        \r
-       public static Enumeration getPossibleSubscript(String line, MdlModel mdl) {\r
-               Matcher matcher = Pattern.compile(subscriptPattern).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+       public static Subscript getPossibleSubscript(String line, MdlModel mdl) {\r
+               Matcher matcher = Pattern.compile(subscriptPattern).matcher(line);              \r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
-               String name = normalize(matcher.group(subscriptName));\r
-               boolean equivalence = matcher.group(subscriptType).equals("<->");\r
-               String expression = normalize(matcher.group(subscriptExpression));\r
+               String name = matcher.group(subscriptName);\r
+               boolean equivalent = matcher.group(subscriptType).equals("<->");\r
+               String expression = matcher.group(subscriptExpression);\r
                \r
-               if (equivalence) {\r
-                       return new Enumeration(name, mdl.getSubscript(expression));\r
-               }\r
-               else {\r
-                       return new Enumeration(name, expression.split(","));\r
-               }\r
-       }\r
-       \r
-       private static Expression parseExpression(String equation) {\r
-               Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(equation);\r
-\r
-               if (!matcher.matches()) {\r
-                       return new NormalExpression(normalize(equation));\r
-               }\r
-               \r
-               String function = matcher.group(1);\r
-               String[] parameters = splitParameters(matcher.group(2));\r
-               \r
-               if (function.startsWith("INTEG")) {\r
-                       if (parameters.length != 2) {\r
-                               System.err.println("malformed integral expression: "+equation);\r
+               if (equivalent) {\r
+                       Subscript parent = mdl.getSubscript(expression);\r
+                       if (parent == null) {\r
+                               System.err.println("equivalent subscript not found for "+name);\r
                                return null;\r
                        }\r
-                       String integral = normalize(parameters[0]);\r
-                       String initial = normalize(parameters[1]);\r
-                       return new IntegralExpression(integral, initial);\r
-               }\r
-               else if (function.startsWith("DELAY N")) {\r
-                       if (parameters.length != 4) {\r
-                               System.err.println("malformed delay expression: "+equation);\r
-                               return null;\r
-                       }\r
-                       String expression = normalize(parameters[0]);\r
-                       String time = normalize(parameters[1]);\r
-                       String initial = normalize(parameters[2]);\r
-                       int degree = Integer.parseInt(parameters[3]);\r
-                       return new DelayExpression(expression, time, initial, degree);\r
-               }\r
-               else if (function.startsWith("GAME")) {\r
-                       if (parameters.length != 1) {\r
-                               System.err.println("malformed game expression: "+equation);\r
-                               return null;\r
-                       }\r
-                       // game expressions are currently treated as normal expressions\r
-                       return new NormalExpression(normalize(parameters[0]));\r
+                       return new Subscript(name, parent.getValues());\r
                }\r
                else {\r
-                       // the function does not require special handling\r
-                       return new NormalExpression(normalize(equation));\r
-               }\r
-       }\r
-       \r
-       private static String[] splitParameters(String str) {\r
-               ArrayList<String> list = new ArrayList<String>();\r
-\r
-               int i;\r
-               int last = 0;\r
-               int level = 0;\r
-               boolean comment = false;\r
-               boolean brackets = false;\r
-               \r
-               for (i = 0; i < str.length(); i++) {\r
-                       char current = str.charAt(i);\r
-                       if (current == '"')\r
-                               // note that this does not currently support escaped quotation marks inside quoted variable names\r
-                               comment = !comment;\r
-                       else if (current == '[' && !comment)\r
-                               brackets = true;\r
-                       else if (current == ']' && !comment)\r
-                               brackets = false;\r
-                       else if (current == '(' && !comment && !brackets)\r
-                               level++;\r
-                       else if (current == ')' && !comment && !brackets)\r
-                               level--;\r
-                       else if (current == ',' && !comment && !brackets && level == 0) {\r
-                               list.add(str.substring(last, i).trim());\r
-                               last = i + 1;\r
-                       }\r
-               }\r
-               if (last < i) {\r
-                       list.add(str.substring(last, i).trim());\r
-               }\r
-               \r
-               return list.toArray(new String[list.size()]);\r
-       }\r
-       \r
-       private static Range parseRange(String unit) {\r
-               Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(("+NUMBER+"),("+NUMBER+")|\\?)\\]").matcher(unit);\r
-               if (matcher.find()) {\r
-                       Double start, end, step;\r
-                       start = Double.parseDouble(matcher.group(1));\r
-                       if (matcher.group(2).equals("?")) {\r
-                               end = null;\r
-                               step = null;\r
-                       }\r
-                       else {\r
-                               end = Double.parseDouble(matcher.group(3));\r
-                               step = Double.parseDouble(matcher.group(4));\r
-                       }\r
-                       return new Range(start, end, step, matcher.group());\r
-               }\r
-               else {\r
-                       return null;\r
+                       return new Subscript(name, new HashSet<String>(Arrays.asList(expression.split(","))));\r
                }\r
        }\r
        \r
        public static String getPossibleCategory(String line) {\r
-               Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+               Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line);           \r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
                return matcher.group(1);\r
        }\r
@@ -335,12 +240,22 @@ public class MdlUtil {
        private static final int connectionShape = 4;\r
        private static final int connectionPoints = 5;\r
        \r
-       public static SketchConnection getPossibleSketchConnection(String line) {\r
+       public static int getConnectionId(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(connectionId));\r
+       }\r
+       \r
+       public static int getConnectionFrom(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(connectionFrom));\r
+       }\r
+       \r
+       public static int getConnectionTo(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(connectionTo));\r
+       }\r
+       \r
+       public static SketchConnection getPossibleSketchConnection(String line, Sketch sketch) {\r
                Matcher matcher = Pattern.compile(sketchConnection).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
                int id = Integer.parseInt(matcher.group(connectionId));\r
                int from = Integer.parseInt(matcher.group(connectionFrom));\r
@@ -391,15 +306,13 @@ public class MdlUtil {
        \r
        public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) {\r
                Matcher matcher = Pattern.compile(sketchVariable).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
                Variable var = mdl.getVariable(normalize(matcher.group(elementName)));\r
                if (var == null) {\r
-                       System.err.println("could not find variable corresponding to "+normalize(matcher.group(elementName)));\r
+                       System.err.println("could not find variable corresponding to "+matcher.group(elementName));\r
                }\r
                \r
                SketchVariable variable = new SketchVariable(id, var);\r
@@ -411,10 +324,8 @@ public class MdlUtil {
        \r
        public static SketchValve getPossibleSketchValve(String line) {\r
                Matcher matcher = Pattern.compile(sketchValve).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
                TextPosition pos = getTextPos(matcher);\r
@@ -428,10 +339,8 @@ public class MdlUtil {
        \r
        public static SketchComment getPossibleSketchComment(String line) {\r
                Matcher matcher = Pattern.compile(sketchComment).matcher(line);\r
-               \r
-               if (!matcher.matches()) {\r
+               if (!matcher.matches())\r
                        return null;\r
-               }\r
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
                CommentIcon icon = getCommentIcon(matcher);\r
@@ -443,6 +352,22 @@ public class MdlUtil {
                return comment;\r
        }\r
        \r
+       public static int getElementX(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(elementX));\r
+       }\r
+       \r
+       public static int getElementY(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(elementY));\r
+       }\r
+       \r
+       public static int getElementWidth(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(elementWidth));\r
+       }\r
+       \r
+       public static int getElementHeight(Matcher matcher) {\r
+               return Integer.parseInt(matcher.group(elementHeight));\r
+       }\r
+       \r
        public static void initializeElement(SketchElement element, Matcher matcher) {\r
                int x = Integer.parseInt(matcher.group(elementX));\r
                int y = Integer.parseInt(matcher.group(elementY));\r
@@ -627,4 +552,131 @@ public class MdlUtil {
                return result.toString();\r
        }\r
        \r
+       \r
+       \r
+       public static void initializeVariable(MdlModel mdl, ModelVariable variable, Variable mdlVariable) {\r
+               variable.setName(mdlVariable.getName());\r
+               variable.setExpression(mdlVariable.getModelExpression(mdl));\r
+               \r
+               String unit = mdlVariable.getUnit();\r
+               Range range = parseRange(unit);\r
+               if (range != null)\r
+                       unit = unit.substring(0, unit.indexOf('[')).trim();\r
+               variable.setUnit(unit);\r
+               variable.setRange(range);\r
+               \r
+               variable.setDescription(mdlVariable.getDescription());\r
+       }\r
+       \r
+       private static Range parseRange(String unit) {\r
+               Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(\\?|("+NUMBER+")|("+NUMBER+"),("+NUMBER+"))\\]").matcher(unit);\r
+               if (matcher.find()) {\r
+                       Double start, end, step;\r
+                       start = Double.parseDouble(matcher.group(1));\r
+                       if (matcher.group(2).equals("?")) {\r
+                               end = null;\r
+                               step = null;\r
+                       }\r
+                       else if (matcher.group(2).contains(",")){\r
+                               end = Double.parseDouble(matcher.group(4));\r
+                               step = Double.parseDouble(matcher.group(5));\r
+                       }\r
+                       else {\r
+                               end = Double.parseDouble(matcher.group(3));\r
+                               step = null;\r
+                       }\r
+                       return new Range(start, end, step);\r
+               }\r
+               else {\r
+                       return null;\r
+               }\r
+       }\r
+       \r
+       public static double[] parseLookup(String lookup) {\r
+               // a hack to remove the leading empty string otherwise produced by split()\r
+               lookup = lookup.replaceFirst("[(),]+", "");\r
+               String[] parts = lookup.split("[(),]+");\r
+               double[] points = new double[parts.length];\r
+               for (int i = 0; i < parts.length; i++) {\r
+                       points[i] = Double.parseDouble(parts[i]);\r
+               }\r
+               return points;\r
+       }\r
+       \r
+       public static Expression parseExpression(String expression) {\r
+               Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(expression);\r
+               if (!matcher.matches())\r
+                       return new NormalExpression(normalize(expression));\r
+               \r
+               String function = matcher.group(1);\r
+               String[] parameters = splitParameters(matcher.group(2));\r
+               \r
+               if (function.startsWith("INTEG")) {\r
+                       if (parameters.length != 2) {\r
+                               System.err.println("malformed integral expression: "+expression);\r
+                               return null;\r
+                       }\r
+                       String integral = normalize(parameters[0]);\r
+                       String initial = normalize(parameters[1]);\r
+                       return new IntegralExpression(integral, initial);\r
+               }\r
+               else if (function.startsWith("DELAY N")) {\r
+                       if (parameters.length != 4) {\r
+                               System.err.println("malformed delay expression: "+expression);\r
+                               return null;\r
+                       }\r
+                       String expr = normalize(parameters[0]);\r
+                       String time = normalize(parameters[1]);\r
+                       String initial = normalize(parameters[2]);\r
+                       int degree = Integer.parseInt(parameters[3]);\r
+                       return new DelayExpression(expr, time, initial, degree);\r
+               }\r
+               else if (function.startsWith("GAME")) {\r
+                       if (parameters.length != 1) {\r
+                               System.err.println("malformed game expression: "+expression);\r
+                               return null;\r
+                       }\r
+                       // game expressions are currently treated as normal expressions\r
+                       return new NormalExpression(normalize(parameters[0]));\r
+               }\r
+               else {\r
+                       // the function does not require special handling\r
+                       return new NormalExpression(normalize(expression));\r
+               }\r
+       }\r
+       \r
+       public static String[] splitParameters(String str) {\r
+               ArrayList<String> list = new ArrayList<String>();\r
+\r
+               int i;\r
+               int last = 0;\r
+               int level = 0;\r
+               boolean comment = false;\r
+               boolean brackets = false;\r
+               \r
+               for (i = 0; i < str.length(); i++) {\r
+                       char current = str.charAt(i);\r
+                       if (current == '"')\r
+                               // note that this does not currently support escaped quotation marks inside quoted variable names\r
+                               comment = !comment;\r
+                       else if (current == '[' && !comment)\r
+                               brackets = true;\r
+                       else if (current == ']' && !comment)\r
+                               brackets = false;\r
+                       else if (current == '(' && !comment && !brackets)\r
+                               level++;\r
+                       else if (current == ')' && !comment && !brackets)\r
+                               level--;\r
+                       else if (current == ',' && !comment && !brackets && level == 0) {\r
+                               list.add(str.substring(last, i).trim());\r
+                               last = i + 1;\r
+                       }\r
+               }\r
+               if (last < i) {\r
+                       list.add(str.substring(last, i).trim());\r
+               }\r
+               \r
+               return list.toArray(new String[list.size()]);\r
+       }\r
+       \r
 }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java
new file mode 100644 (file)
index 0000000..441e360
--- /dev/null
@@ -0,0 +1,84 @@
+package org.simantics.sysdyn.modelImport.mdl;\r
+\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
+import org.simantics.sysdyn.modelImport.model.expression.LookupExpression;\r
+\r
+public class LookupVariable extends Variable {\r
+       \r
+       // a lookup variable contains a set of points\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
+       private static final String LOOKUP_VARIABLE =\r
+                       LOOKUP_NAME+"\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)";\r
+       \r
+       public static final String lookupVariablePattern = \r
+                       LOOKUP_VARIABLE+DELIM+UNIT+DELIM+DESCRIPTION+END;\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
+       private double xMin;\r
+       private double yMin;\r
+       private double xMax;\r
+       private double yMax;\r
+\r
+       public LookupVariable(String name, String expression, String unit, String description, \r
+                       double xMin, double yMin, double xMax, double yMax) {\r
+               super(name, expression, unit, description);\r
+               this.xMin = xMin;\r
+               this.yMin = yMin;\r
+               this.xMax = xMax;\r
+               this.yMax = yMax;\r
+       }\r
+       \r
+       public static LookupVariable getPossibleLookUpVariable(String line) {\r
+               Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line);\r
+               if (!matcher.matches())\r
+                       return null;\r
+               \r
+               String name = MdlUtil.normalize(matcher.group(lookupVariableName));\r
+               double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin));\r
+               double yMin = Double.parseDouble(matcher.group(lookupVariableRangeYMin));\r
+               double xMax = Double.parseDouble(matcher.group(lookupVariableRangeXMax));\r
+               double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax));\r
+               String expression = matcher.group(lookupVariablePoints);\r
+               String unit = matcher.group(lookupVariableUnit);\r
+               String desc = matcher.group(lookupVariableDesc);\r
+               \r
+               return new LookupVariable(name, expression, unit, desc, xMin, yMin, xMax, yMax);\r
+       }\r
+\r
+       public double getxMin() {\r
+               return xMin;\r
+       }\r
+\r
+       public double getyMin() {\r
+               return yMin;\r
+       }\r
+\r
+       public double getxMax() {\r
+               return xMax;\r
+       }\r
+\r
+       public double getyMax() {\r
+               return yMax;\r
+       }\r
+       \r
+       @Override\r
+       public LookupExpression getModelExpression(MdlModel mdl) {\r
+               return new LookupExpression(xMin, yMin, xMax, yMax, MdlUtil.parseLookup(getExpression()));\r
+       }\r
+       \r
+}\r
index 7bf61c52d464314f1b66069121724b9cc36abbae..b86b2a03554f5b72434686c65b38750744294ef0 100644 (file)
@@ -2,10 +2,9 @@ package org.simantics.sysdyn.modelImport.mdl;
 \r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
+import java.util.HashSet;\r
 import java.util.List;\r
-\r
-import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
+import java.util.Set;\r
 \r
 public class MdlModel {\r
        \r
@@ -13,7 +12,8 @@ public class MdlModel {
        \r
        private HashMap<String, Variable> variables;\r
        private HashMap<String, ArrayList<Variable>> groups;\r
-       private HashMap<String, Enumeration> subscripts;\r
+       private HashMap<String, Subscript> subscripts;\r
+       private HashMap<String, Subscript> subscriptValues;\r
        private ArrayList<Sketch> sketches;\r
        \r
        public MdlModel(String name) {\r
@@ -21,7 +21,8 @@ public class MdlModel {
                \r
                this.variables = new HashMap<String, Variable>();\r
                this.groups = new HashMap<String, ArrayList<Variable>>();\r
-               this.subscripts = new HashMap<String, Enumeration>();\r
+               this.subscripts = new HashMap<String, Subscript>();\r
+               this.subscriptValues = new HashMap<String, Subscript>();\r
                this.sketches = new ArrayList<Sketch>();\r
        }\r
        \r
@@ -29,10 +30,26 @@ public class MdlModel {
                return name;\r
        }\r
        \r
+       public void addVariable(SubscriptVariable variable, String group) {\r
+               SubscriptVariable original = (SubscriptVariable)variables.get(variable.getName());\r
+               if (original != null) {\r
+                       original.addLast(variable);\r
+               }\r
+               else {\r
+                       variables.put(variable.getName(), variable);\r
+                       \r
+                       if (group != null) {\r
+                               if (groups.get(group) == null) {\r
+                                       groups.put(group, new ArrayList<Variable>());\r
+                               }\r
+                               groups.get(group).add(variable);\r
+                       }\r
+               }\r
+       }\r
+       \r
        public void addVariable(Variable variable, String group) {\r
                if (variables.get(variable.getName()) != null) {\r
-                       System.err.println("warning, duplicate variable "+variable.getName());\r
-                       variables.get(variable.getName()).mergeWithVariable(variable);\r
+                       System.err.println("duplicate variable "+variable.getName());\r
                        return;\r
                }\r
                \r
@@ -50,20 +67,75 @@ public class MdlModel {
                return variables.get(name);\r
        }\r
        \r
-       public List<Variable> getVariables() {\r
-               return new ArrayList<Variable>(variables.values());\r
+       public Set<Variable> getAllVariables() {\r
+               return new HashSet<Variable>(variables.values());\r
+       }\r
+\r
+       public void addSubscript(Subscript subscript) {\r
+               Subscript duplicate = resolveSubscript(subscript.getValues());\r
+               if (duplicate != null) {\r
+                       subscripts.put(subscript.getName(), duplicate);\r
+                       System.err.println("dublicate subscript "+subscript.getName()+" added");\r
+               }\r
+               else {\r
+                       subscripts.put(subscript.getName(), subscript);\r
+                       System.err.println("subscript "+subscript.getName()+" added");\r
+               }\r
+       }\r
+       \r
+       public Subscript getSubscript(String key) {\r
+               return subscripts.get(key);\r
+       }\r
+       \r
+       public Set<Subscript> getAllSubscripts() {\r
+               return new HashSet<Subscript>(subscripts.values());\r
        }\r
        \r
-       public void addSubscript(Enumeration subscript) {\r
-               subscripts.put(subscript.getName(), subscript);\r
+       private Subscript getSuperSet(Subscript subscript) {\r
+               for (Subscript potential : getAllSubscripts()) {\r
+                       if (potential.isSuperSet() && \r
+                                       potential.getValues().containsAll(subscript.getValues())) {\r
+                               return potential;\r
+                       }\r
+               }\r
+               return null;\r
        }\r
        \r
-       public Enumeration getSubscript(String name) {\r
-               return subscripts.get(name);\r
+       private Set<Subscript> getSubSets(Subscript subscript) {\r
+               Set<Subscript> subsets = new HashSet<Subscript>();\r
+               for (Subscript potential : getAllSubscripts()) {\r
+                       if (subscript.getValues().size() > potential.getValues().size() &&\r
+                                       subscript.getValues().containsAll(potential.getValues())) {\r
+                               subsets.add(potential);\r
+                       }\r
+               }\r
+               return subsets;\r
        }\r
        \r
-       public List<Enumeration> getSubscripts() {\r
-               return new ArrayList<Enumeration>(subscripts.values());\r
+       // return a subscript that contains precisely the requested values\r
+       public Subscript resolveSubscript(Set<String> values) {\r
+               Set<String> allValues = new HashSet<String>();\r
+               for (String value : values) {\r
+                       if (subscripts.get(value) != null) {\r
+                               allValues.addAll(subscripts.get(value).getValues());\r
+                       }\r
+                       else {\r
+                               allValues.add(value);\r
+                       }\r
+               }\r
+               \r
+               for (Subscript subscript : getAllSubscripts()) {\r
+                       if (allValues.equals(subscript.getValues())) {\r
+                               return subscript;\r
+                       }\r
+               }\r
+               \r
+               System.err.print("ASDF subscript not found for value set:");\r
+               for (String s : allValues)\r
+                       System.err.print(" "+s);\r
+               System.err.println();\r
+               \r
+               return null;\r
        }\r
        \r
        public void addSketch(Sketch sketch) {\r
index 9aaac03c82092fdb7a9d9d65ab01dd83f512debe..6e7a8c8e4f73e0e5cbac285531ee36c7ee514710 100644 (file)
@@ -28,16 +28,13 @@ public class SketchComment extends SketchElement {
        }\r
        \r
        @Override\r
-       public Symbol getSymbol(Sketch sketch) {\r
-               double[] dimensions = getDimensions(sketch);\r
+       public Symbol getSymbol(MdlModel mdl, Sketch sketch) {\r
+               if (isInputOutput())\r
+                       return new Comment(getDimensions(), "I/O objects are not supported");\r
                \r
-               if (isInputOutput()) {\r
-                       return new Comment(dimensions, "input / output objects are not supported yet");\r
-               }\r
-               \r
-               switch (icon) {\r
-               case CLOUD: return new Cloud(dimensions);\r
-               case OTHER: return new Comment(dimensions, text);\r
+               switch(icon) {\r
+               case CLOUD: return new Cloud(getDimensions());\r
+               case OTHER: return new Comment(getDimensions(), text);\r
                default: return null;\r
                }\r
        }\r
index bfd3406651739b6b8c673c62578631ce47db6735..8d6e1c1cfa9511cf46a6ed812d46f99201e31a73 100644 (file)
@@ -1,5 +1,9 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
 import org.simantics.sysdyn.modelImport.MdlUtil.ConnectionType;\r
 import org.simantics.sysdyn.modelImport.model.element.Connection;\r
 import org.simantics.sysdyn.modelImport.model.element.Dependency;\r
index 3ab316ffd01353397cabc89662e56e8ad6172d2d..c1f3486c4b47ead88733144cd6b13ce3dfab59bf 100644 (file)
@@ -18,6 +18,21 @@ public abstract class SketchElement extends SketchObject {
                super(id);\r
        }\r
        \r
+       public SketchElement(int id, int x, int y, int width, int height, \r
+                       boolean attached, boolean allowsIn, boolean allowsOut, \r
+                       boolean inputOutput, boolean textLine) {\r
+               super(id);\r
+               this.x = x;\r
+               this.y = y;\r
+               this.width = width;\r
+               this.height = height;\r
+               this.attached = attached;\r
+               this.allowsIn = allowsIn;\r
+               this.allowsOut = allowsOut;\r
+               this.inputOutput = inputOutput;\r
+               this.textLine = textLine;\r
+       }\r
+       \r
        public void init(int x, int y, int width, int height, \r
                        boolean attached, boolean allowsIn, boolean allowsOut, \r
                        boolean inputOutput, boolean textLine) {\r
@@ -105,7 +120,7 @@ public abstract class SketchElement extends SketchObject {
                return textLine;\r
        }\r
 \r
-       public abstract Symbol getSymbol(Sketch sketch);\r
+       public abstract Symbol getSymbol(MdlModel mdl, Sketch sketch);\r
        \r
        private static final double SCALE_MULTIPLIER = 0.4;\r
        \r
index c393d1ba237ecedd59ce92c7cc7f864424675f0c..fb4c613534a36cf3a3bfbb40cb820f92339878b2 100644 (file)
@@ -1,8 +1,11 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
 import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
+import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
 import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 import org.simantics.sysdyn.modelImport.model.element.Valve;\r
+import org.simantics.sysdyn.modelImport.model.element.Valve.Orientation;\r
 import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition;\r
 \r
 public class SketchValve extends SketchElement {\r
@@ -28,15 +31,13 @@ public class SketchValve extends SketchElement {
        }\r
        \r
        @Override\r
-       public Symbol getSymbol(Sketch sketch) {\r
-               return new Valve(getDimensions(sketch), variable.getVariable(), textPosition);\r
-       }\r
-       \r
-       // TODO: do we really want to do this?\r
-       @Override\r
-       public void setModelObject(IWriteableObject modelObject) {\r
-               super.setModelObject(modelObject);\r
-               variable.setModelObject(modelObject);\r
+       public Valve getSymbol(MdlModel mdl, Sketch sketch) {\r
+               Valve valve = new Valve(Orientation.HORIZONTAL, textPosition);\r
+               \r
+               variable.getVariable().initializeModelVariable(valve, mdl);\r
+               valve.setDimensions(getDimensions(sketch));\r
+               \r
+               return valve;\r
        }\r
 \r
 }\r
index 9cef818499c8c06971c7bcf134b2060bc2f9f027..b73afe7618af12a44c1ce3db21c23e3476d6faae 100644 (file)
@@ -1,10 +1,11 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
 import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
+import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
 import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 import org.simantics.sysdyn.modelImport.model.element.Stock;\r
 import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public class SketchVariable extends SketchElement {\r
        \r
@@ -20,13 +21,18 @@ public class SketchVariable extends SketchElement {
        }\r
        \r
        @Override\r
-       public Symbol getSymbol(Sketch sketch) {\r
-               if (variable.getExpression() instanceof IntegralExpression) {\r
-                       return new Stock(getDimensions(sketch), variable);\r
-               }\r
-               else {\r
-                       return new Auxiliary(getDimensions(sketch), variable);\r
-               }\r
+       public Symbol getSymbol(MdlModel mdl, Sketch sketch) {\r
+               ModelVariable var;\r
+               \r
+               if (variable.getModelExpression(mdl) instanceof IntegralExpression)\r
+                       var = new Stock();\r
+               else\r
+                       var = new Auxiliary();\r
+               \r
+               variable.initializeModelVariable(var, mdl);\r
+               var.setDimensions(getDimensions(sketch));\r
+               \r
+               return var;\r
        }\r
        \r
 }
\ No newline at end of file
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java
new file mode 100644 (file)
index 0000000..587d942
--- /dev/null
@@ -0,0 +1,96 @@
+package org.simantics.sysdyn.modelImport.mdl;\r
+\r
+import java.util.Arrays;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
+import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
+\r
+public class Subscript  {\r
+       \r
+       // tODO: might need some rework\r
+       private static final String subscriptPattern = \r
+                       "([A-Za-z]\\w*)\\s*(:|<->)\\s*([^~]*?)\\s*~\\s*~\\s*\\|";\r
+       private static final int subscriptName = 1;\r
+       private static final int subscriptType = 2;\r
+       private static final int subscriptExpression = 3;\r
+       \r
+       private String name;\r
+       private Set<String> values;\r
+       \r
+       private Subscript superSet;\r
+       \r
+       private Enumeration enumeration;\r
+       \r
+       public Subscript(String name, Set<String>values) {\r
+               this.name = name;\r
+               this.values = values;\r
+       }\r
+       \r
+       public static Subscript getPossibleSubscript(String line, MdlModel mdl) {\r
+               Matcher matcher = Pattern.compile(subscriptPattern).matcher(line);              \r
+               if (!matcher.matches())\r
+                       return null;\r
+               \r
+               String name = MdlUtil.normalize(matcher.group(subscriptName));\r
+               boolean equivalent = matcher.group(subscriptType).equals("<->");\r
+               String expression = MdlUtil.normalize(matcher.group(subscriptExpression));\r
+               \r
+               if (equivalent) {\r
+                       Subscript parent = mdl.getSubscript(expression);\r
+                       if (parent == null) {\r
+                               System.err.println("equivalent subscript not found for "+name);\r
+                               return null;\r
+                       }\r
+                       return new Subscript(name, parent.getValues());\r
+               }\r
+               else {\r
+                       return new Subscript(name, new HashSet<String>(Arrays.asList(expression.split(","))));\r
+               }\r
+       }\r
+       \r
+       public String getName() {\r
+               return name;\r
+       }\r
+       \r
+       public Set<String> getValues() {\r
+               return values;\r
+       }\r
+       \r
+       public Subscript getSuperSet() {\r
+               return superSet;\r
+       }\r
+       \r
+       public void setSuperSet(Subscript superSet) {\r
+               this.superSet = superSet;\r
+       }\r
+       \r
+       public boolean isSuperSet() {\r
+               return superSet == null;\r
+       }\r
+       \r
+       public String getRepresentation() {\r
+               StringBuilder buffer = new StringBuilder();\r
+               buffer.append('{');\r
+               for (String value : values) {\r
+                       if (buffer.length() > 1) {\r
+                               buffer.append(',');\r
+                       }\r
+                       buffer.append(value);\r
+               }\r
+               buffer.append('}');\r
+               return buffer.toString();\r
+       }\r
+       \r
+       public void setEnumeration(Enumeration enumeration) {\r
+               this.enumeration = enumeration;\r
+       }\r
+       \r
+       public Enumeration getEnumeration() {\r
+               return enumeration != null ? enumeration : superSet.getEnumeration();\r
+       }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java
new file mode 100644 (file)
index 0000000..78b1867
--- /dev/null
@@ -0,0 +1,127 @@
+package org.simantics.sysdyn.modelImport.mdl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
+import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
+import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
+\r
+public class SubscriptVariable extends Variable {\r
+       \r
+       // a subscript variable is a variable that has a different equation\r
+       // depending on the value of the subscript it references\r
+       private static final String SUBSCRIPT_VARIABLE =\r
+                       "("+VARIABLE_SIMPLE+")\\[("+VARIABLE_SIMPLE+"(?:,"+VARIABLE_SIMPLE+")*)\\]";\r
+       \r
+       public static final String subscriptVariablePattern = \r
+                       SUBSCRIPT_VARIABLE+EXPRESSION+DELIM+UNIT+DELIM+DESCRIPTION+END;\r
+       private static final int subscriptVariableName = 1;\r
+       private static final int subscriptVariableIndices = 2;\r
+       private static final int subscriptVariableExpression = 3;\r
+       private static final int subscriptVariableUnit = 4;\r
+       private static final int subscriptVariableDesc = 5;\r
+\r
+       private String[] subscript;\r
+       private SubscriptVariable next;\r
+       \r
+       public SubscriptVariable(String name, String expression, String unit, String description, String[] subscript) {\r
+               super(name, expression, unit, description);\r
+               this.subscript = subscript;\r
+               this.next = null;\r
+       }\r
+       \r
+       public static SubscriptVariable getPossibleSubscriptVariable(String line) {\r
+               Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line);\r
+               if (!matcher.matches())\r
+                       return null;\r
+               \r
+               String name = MdlUtil.normalize(matcher.group(subscriptVariableName));\r
+               String[] subscript = MdlUtil.normalize(matcher.group(subscriptVariableIndices)).split(",");\r
+               String expression = MdlUtil.normalize(matcher.group(subscriptVariableExpression));\r
+               String unit = matcher.group(subscriptVariableUnit);\r
+               String desc = matcher.group(subscriptVariableDesc);\r
+               \r
+               return new SubscriptVariable(name, expression, unit, desc, subscript);\r
+       }\r
+       \r
+       public String[] getSubscript() {\r
+               return subscript;\r
+       }\r
+       \r
+       public SubscriptVariable getNext() {\r
+               return next;\r
+       }\r
+       \r
+       public void setNext(SubscriptVariable next) {\r
+               this.next = next;\r
+       }\r
+       \r
+       public void addLast(SubscriptVariable variable) {\r
+               // find the last variable in the list and add the new variable after it\r
+               SubscriptVariable last = this;\r
+               while (last.getNext() != null)\r
+                       last = last.getNext();\r
+               last.setNext(variable);\r
+       }\r
+       \r
+       @Override\r
+       public EnumerationExpression getModelExpression(MdlModel mdl) {\r
+               SubscriptVariable var;\r
+               // combine the list of subscript expressions into an enumeration \r
+               // expression\r
+               \r
+               List<Set<String>> indices = new ArrayList<Set<String>>();\r
+               for (int i = 0; i < subscript.length; i++) {\r
+                       indices.add(new HashSet<String>());\r
+               }\r
+               \r
+               var = this;\r
+               while (var != null) {\r
+                       for (int i = 0; i < var.getSubscript().length; i++) {\r
+                               indices.get(i).add(var.getSubscript()[i]);\r
+                       }\r
+                       \r
+                       var = var.getNext();\r
+               }\r
+               \r
+               List<Enumeration> enumerations = new ArrayList<Enumeration>();\r
+               for (Set<String> values : indices) {\r
+                       if (mdl.resolveSubscript(values) == null) {\r
+                               System.err.print("subscript not found for value set:");\r
+                               for (String s : values)\r
+                                       System.err.print(" "+s);\r
+                               System.err.println();\r
+                       }\r
+                       enumerations.add(mdl.resolveSubscript(values).getEnumeration());\r
+               }\r
+               \r
+               EnumerationExpression expr = new EnumerationExpression(enumerations);\r
+               \r
+               var = this;\r
+               while (var != null) {\r
+                       String[] array = new String[var.getSubscript().length];\r
+                       for (int i = 0; i < var.getSubscript().length; i++) {\r
+                               String value = var.getSubscript()[i];\r
+                               Subscript script = mdl.getSubscript(value);\r
+                               if (script == null || script.getValues().equals(enumerations.get(i).getValues())) {\r
+                                       array[i] = value;\r
+                               }\r
+                               else {\r
+                                       array[i] = script.getRepresentation();\r
+                               }\r
+                       }\r
+                       expr.addExpression(super.getModelExpression(mdl), array);\r
+                       \r
+                       var = var.getNext();\r
+               }\r
+               \r
+               return expr;\r
+       }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java
new file mode 100644 (file)
index 0000000..bbfc849
--- /dev/null
@@ -0,0 +1,177 @@
+package org.simantics.sysdyn.modelImport.mdl;\r
+\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
+import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.expression.DelayExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
+import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;\r
+import org.simantics.sysdyn.modelImport.model.support.Range;\r
+\r
+public class Variable {\r
+       \r
+       protected static final String NUMBER = "-?\\d+(?:\\.\\d+)?";\r
+       protected static final String DELIM = "\\s*~\\s*";\r
+       protected static final String END = "\\s*\\|";\r
+       \r
+       // a Vensim variable name is either a plain string that contains letters, \r
+       // numbers and whitespace, or a quoted string that can also contain any \r
+       // special characters (including other quotation marks that must be escaped)\r
+       protected static final String VARIABLE_QUOTED =\r
+                       "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"";\r
+       protected static final String VARIABLE_SIMPLE =\r
+                       "[A-Za-z](?![ \\w]*\\()(?: *\\w+)*";\r
+       \r
+       protected static final String NORMAL_VARIABLE =\r
+                       "("+VARIABLE_QUOTED+"|"+VARIABLE_SIMPLE+")";\r
+       // the expression does not necessarily exist\r
+       protected static final String EXPRESSION =\r
+                       "(?:\\s*=\\s*([^~]*?(?:"+VARIABLE_QUOTED+"[^~]*?)*))?";\r
+       protected static final String UNIT =\r
+                       "([^~]*?)";\r
+       protected static final String DESCRIPTION =\r
+                       "([^\\|]*?)";\r
+       \r
+       private static final String variablePattern = \r
+                       NORMAL_VARIABLE+EXPRESSION+DELIM+UNIT+DELIM+DESCRIPTION+END;\r
+       private static final int variableName = 1;\r
+       private static final int variableExpression = 2;\r
+       private static final int variableUnit = 3;\r
+       private static final int variableDesc = 4;\r
+       \r
+       private String name;\r
+       private String expression;\r
+       private String unit;\r
+       private String description;\r
+       \r
+       public Variable(String name, String expression, String unit, String description) {\r
+               this.name = name;\r
+               this.expression = expression;\r
+               this.unit = unit;\r
+               this.description = description;\r
+       }\r
+       \r
+       public static Variable getPossibleVariable(String line) {\r
+               Matcher matcher = Pattern.compile(variablePattern).matcher(line);\r
+               if (!matcher.matches())\r
+                       return null;\r
+               \r
+               String name = MdlUtil.normalize(matcher.group(variableName));\r
+               String expression = matcher.group(variableExpression);\r
+               if (expression != null) {\r
+                       expression = MdlUtil.normalize(expression);\r
+               }\r
+               String unit = matcher.group(variableUnit);\r
+               String desc = matcher.group(variableDesc);\r
+               \r
+               return new Variable(name, expression, unit, desc);\r
+       }\r
+       \r
+       public String getName() {\r
+               return name;\r
+       }\r
+       \r
+       public String getExpression() {\r
+               return expression;\r
+       }\r
+\r
+       public String getUnit() {\r
+               return unit;\r
+       }\r
+\r
+       public String getDescription() {\r
+               return description;\r
+       }\r
+       \r
+       public void initializeModelVariable(ModelVariable variable, MdlModel mdl) {\r
+               variable.setName(getName());\r
+               variable.setExpression(getModelExpression(mdl));\r
+               \r
+               String unit = getUnit();\r
+               Range range = parseRange(unit);\r
+               if (range != null)\r
+                       unit = unit.substring(0, unit.indexOf('[')).trim();\r
+               variable.setUnit(unit);\r
+               variable.setRange(range);\r
+               \r
+               variable.setDescription(getDescription());\r
+       }\r
+       \r
+       public Expression getModelExpression(MdlModel mdl) {\r
+               if (expression == null) {\r
+                       // if the expression has not been defined, return an empty expression\r
+                       return new NormalExpression("");\r
+               }\r
+\r
+               // the type of the returned expression object depends on the expression\r
+               Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(expression);\r
+               if (!matcher.matches()) {\r
+                       // the expression is not one of the special function calls\r
+                       return new NormalExpression(MdlUtil.normalize(expression));\r
+               }\r
+               \r
+               String function = matcher.group(1);\r
+               String[] parameters = MdlUtil.splitParameters(matcher.group(2));\r
+               \r
+               if (function.startsWith("INTEG")) {\r
+                       // an integral expression\r
+                       if (parameters.length != 2) {\r
+                               System.err.println("malformed integral expression: "+expression);\r
+                               return null;\r
+                       }\r
+                       String integral = MdlUtil.normalize(parameters[0]);\r
+                       String initial = MdlUtil.normalize(parameters[1]);\r
+                       return new IntegralExpression(integral, initial);\r
+               }\r
+               else if (function.startsWith("DELAY N")) {\r
+                       // a delay expression\r
+                       if (parameters.length != 4) {\r
+                               System.err.println("malformed delay expression: "+expression);\r
+                               return null;\r
+                       }\r
+                       String expr = MdlUtil.normalize(parameters[0]);\r
+                       String time = MdlUtil.normalize(parameters[1]);\r
+                       String initial = MdlUtil.normalize(parameters[2]);\r
+                       int degree = Integer.parseInt(parameters[3]);\r
+                       return new DelayExpression(expr, time, initial, degree);\r
+               }\r
+               else if (function.startsWith("GAME")) {\r
+                       // a game expression, currently treated as a normal expression\r
+                       if (parameters.length != 1) {\r
+                               System.err.println("malformed game expression: "+expression);\r
+                               return null;\r
+                       }\r
+                       return new NormalExpression(MdlUtil.normalize(parameters[0]));\r
+               }\r
+               else {\r
+                       return new NormalExpression(MdlUtil.normalize(expression));\r
+               }\r
+       }\r
+       \r
+       private static Range parseRange(String unit) {\r
+               Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(\\?|("+NUMBER+")|("+NUMBER+"),("+NUMBER+"))\\]").matcher(unit);\r
+               if (matcher.find()) {\r
+                       Double start, end, step;\r
+                       start = Double.parseDouble(matcher.group(1));\r
+                       if (matcher.group(2).equals("?")) {\r
+                               end = null;\r
+                               step = null;\r
+                       }\r
+                       else if (matcher.group(2).contains(",")){\r
+                               end = Double.parseDouble(matcher.group(4));\r
+                               step = Double.parseDouble(matcher.group(5));\r
+                       }\r
+                       else {\r
+                               end = Double.parseDouble(matcher.group(3));\r
+                               step = null;    \r
+                       }\r
+                       return new Range(start, end, step);\r
+               }\r
+               else {\r
+                       return null;\r
+               }\r
+       }\r
+}\r
index 11ddf9d3442c42dc4adf474f706060d51626bd8c..e30545b6f9db6f57afb5e16ee059424a167f2f0a 100644 (file)
@@ -5,21 +5,15 @@ import org.simantics.db.Resource;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public class Auxiliary extends ModelVariable {\r
        \r
-       public Auxiliary(double x, double y, double w, double h,\r
-                       String name, Expression expression, Range range, String unit, String description) {\r
-               super(x, y, w, h, name, expression, range, unit, description);\r
+       public Auxiliary() {\r
+               \r
        }\r
        \r
-       public Auxiliary(double[] dimensions, Variable variable) {\r
-               super(dimensions, variable);\r
-       }\r
-       \r
-       public Auxiliary(double x, double y, double w, double h, Variable variable) {\r
-               super(x, y, w, h, variable);\r
+       public Auxiliary(double[] dim, String name, Expression expression, Range range, String unit, String description) {\r
+               super(dim, name, expression, range, unit, description);\r
        }\r
        \r
        @Override\r
index 10050518ce8a88d58c89341bf9d35c1286e61ef5..9687c53314f5d09d6b03d9a4932573f61f575c26 100644 (file)
@@ -14,12 +14,12 @@ public class Cloud extends Symbol {
        \r
        private Resource cloud;\r
        \r
-       public Cloud(double x, double y, double w, double h) {\r
-               super(x, y, w, h);\r
+       public Cloud() {\r
+               \r
        }\r
        \r
-       public Cloud(double[] dimensions) {\r
-               super(dimensions);\r
+       public Cloud(double[] dim) {\r
+               super(dim);\r
        }\r
 \r
        @Override\r
index 2c72455398d362fc6028506662aa4e3ad4c004db..b632ca1df3e3e2a08918b3d6bdb8cee69af8b6cf 100644 (file)
@@ -13,13 +13,24 @@ public class Comment extends Symbol {
        \r
        private String text;\r
        \r
-       public Comment(double x, double y, double w, double h, String text) {\r
-               super(x, y, w, h);\r
+       public Comment() {\r
+               \r
+       }\r
+       \r
+       public Comment(String text) {\r
                this.text = text;\r
        }\r
        \r
-       public Comment(double[] dimensions, String text) {\r
-               super(dimensions);\r
+       public Comment(double[] dim, String text) {\r
+               super(dim);\r
+               this.text = text;\r
+       }\r
+\r
+       public String getText() {\r
+               return text;\r
+       }\r
+\r
+       public void setText(String text) {\r
                this.text = text;\r
        }\r
 \r
index 967bd04bbc6b9d146a23d491b398bc95d7c01c4c..dda8b59ad6cd639b84ba6398e08f2666d233e7bb 100644 (file)
@@ -16,17 +16,37 @@ import org.simantics.sysdyn.modelImport.model.WriteContext;
 \r
 public abstract class Connection implements IWriteableObject {\r
 \r
-       protected Symbol head;\r
-       protected Symbol tail;\r
+       private Symbol head;\r
+       private Symbol tail;\r
        \r
        private Resource connection;\r
        private Resource diagramConnection;\r
+       \r
+       public Connection() {\r
+               \r
+       }\r
 \r
        public Connection(Symbol tail, Symbol head) {\r
                this.tail = tail;\r
                this.head = head;\r
        }\r
        \r
+       public Symbol getHead() {\r
+               return head;\r
+       }\r
+\r
+       public void setHead(Symbol head) {\r
+               this.head = head;\r
+       }\r
+\r
+       public Symbol getTail() {\r
+               return tail;\r
+       }\r
+\r
+       public void setTail(Symbol tail) {\r
+               this.tail = tail;\r
+       }\r
+\r
        @Override\r
        public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
index 8da35e27c3a68bd3b37e7363c6fd50b21d510548..a2c486bbb9781668083bc0effd1b1832e30a1156 100644 (file)
@@ -15,6 +15,16 @@ public class Dependency extends Connection {
        private boolean showDelay;\r
        private double angle;\r
        \r
+       public Dependency() {\r
+               \r
+       }\r
+       \r
+       public Dependency(boolean showArrow, boolean showDelay, double angle) {\r
+               this.showArrow = showArrow;\r
+               this.showDelay = showDelay;\r
+               this.angle = angle;\r
+       }\r
+       \r
        public Dependency(Symbol tail, Symbol head, boolean showArrow, boolean showDelay, double angle) {\r
                super(tail, head);\r
                this.showArrow = showArrow;\r
@@ -22,9 +32,33 @@ public class Dependency extends Connection {
                this.angle = angle;\r
        }\r
 \r
+       public boolean isShowArrow() {\r
+               return showArrow;\r
+       }\r
+\r
+       public void setShowArrow(boolean showArrow) {\r
+               this.showArrow = showArrow;\r
+       }\r
+\r
+       public boolean isShowDelay() {\r
+               return showDelay;\r
+       }\r
+\r
+       public void setShowDelay(boolean showDelay) {\r
+               this.showDelay = showDelay;\r
+       }\r
+\r
+       public double getAngle() {\r
+               return angle;\r
+       }\r
+\r
+       public void setAngle(double angle) {\r
+               this.angle = angle;\r
+       }\r
+\r
        @Override\r
        public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               if (head == null || tail == null) {\r
+               if (getHead() == null || getTail() == null) {\r
                        System.err.println("dependency missing head or tail");\r
                        return null;\r
                }\r
index 747819da3d35403f1b7af5607e5fa9cbbd75a876..9e3b28d5493291f15007b2641a432db65794545e 100644 (file)
@@ -11,13 +11,17 @@ import org.simantics.sysdyn.modelImport.model.WriteContext;
 \r
 public class Flow extends Connection {\r
        \r
+       public Flow() {\r
+               \r
+       }\r
+       \r
        public Flow(Symbol tail, Symbol head) {\r
                super(tail, head);\r
        }\r
 \r
        @Override\r
        public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               if (head == null || tail == null) {\r
+               if (getHead() == null || getTail() == null) {\r
                        System.err.println("flow missing head or tail");\r
                        return null;\r
                }\r
index c297535f851167d883538efbb761166c81ef0936..d5e96f0094a96688d35989e6b262b82586e308b0 100644 (file)
@@ -12,7 +12,6 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public abstract class ModelVariable extends Symbol {\r
 \r
@@ -23,39 +22,68 @@ public abstract class ModelVariable extends Symbol {
        private String description;\r
        \r
        private Resource variable;\r
-\r
-       public ModelVariable(double x, double y, double w, double h,\r
-                       String name, Expression expression, Range range, String unit, String description) {\r
-               super(x, y, w, h);\r
+       \r
+       public ModelVariable() {\r
+               \r
+       }\r
+       \r
+       public ModelVariable(String name, Expression expression, Range range, String unit, String description) {\r
                this.name = name;\r
                this.expression = expression;\r
                this.range = range;\r
                this.unit = unit;\r
                this.description = description;\r
        }\r
-       \r
-       public ModelVariable(double[] dimensions, Variable variable) {\r
-               super(dimensions);\r
-               this.name = variable.getName();\r
-               this.expression = variable.getExpression();\r
-               this.range = variable.getRange();\r
-               this.unit = variable.getUnit();\r
-               this.description = variable.getDescription();\r
-       }\r
-       \r
-       public ModelVariable(double x, double y, double w, double h, Variable variable) {\r
-               super(x, y, w, h);\r
-               this.name = variable.getName();\r
-               this.expression = variable.getExpression();\r
-               this.range = variable.getRange();\r
-               this.unit = variable.getUnit();\r
-               this.description = variable.getDescription();\r
+\r
+       public ModelVariable(double[] dim, String name, Expression expression, Range range, String unit, String description) {\r
+               super(dim);\r
+               this.name = name;\r
+               this.expression = expression;\r
+               this.range = range;\r
+               this.unit = unit;\r
+               this.description = description;\r
        }\r
-       \r
+\r
        public String getName() {\r
                return name;\r
        }\r
 \r
+       public void setName(String name) {\r
+               this.name = name;\r
+       }\r
+\r
+       public Expression getExpression() {\r
+               return expression;\r
+       }\r
+\r
+       public void setExpression(Expression expression) {\r
+               this.expression = expression;\r
+       }\r
+\r
+       public Range getRange() {\r
+               return range;\r
+       }\r
+\r
+       public void setRange(Range range) {\r
+               this.range = range;\r
+       }\r
+\r
+       public String getUnit() {\r
+               return unit;\r
+       }\r
+\r
+       public void setUnit(String unit) {\r
+               this.unit = unit;\r
+       }\r
+\r
+       public String getDescription() {\r
+               return description;\r
+       }\r
+\r
+       public void setDescription(String description) {\r
+               this.description = description;\r
+       }\r
+\r
        @Override\r
        public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
index 0ad1822e3d80f7427494d02fe76588ae36a0ae69..716c2aa726b4c4321023d6149953a12d9e7dce32 100644 (file)
@@ -8,7 +8,6 @@ import org.simantics.layer0.Layer0;
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
 public class Shadow extends Symbol {\r
@@ -17,18 +16,24 @@ public class Shadow extends Symbol {
        \r
        private Resource shadow;\r
        \r
-       public Shadow(double x, double y, double w, double h, ModelVariable original) {\r
-               super(x, y, w, h);\r
-               this.original = original;\r
+       public Shadow() {\r
+               \r
        }\r
        \r
-       public Shadow(double[] dimensions, ModelVariable original) {\r
-               super(dimensions);\r
+       public Shadow(ModelVariable original) {\r
                this.original = original;\r
        }\r
        \r
-       public Shadow(SketchVariable variable, double hOffset, double vOffset, ModelVariable original) {\r
-               super(variable, hOffset, vOffset);\r
+       public Shadow(double[] dim, ModelVariable original) {\r
+               super(dim);\r
+               this.original = original;\r
+       }\r
+\r
+       public ModelVariable getOriginal() {\r
+               return original;\r
+       }\r
+\r
+       public void setOriginal(ModelVariable original) {\r
                this.original = original;\r
        }\r
 \r
index 0272567fe5d65497a06619196539527cc74dc1ed..9c4b11c26e5a324f194b18cdb8e181bd7447d2c8 100644 (file)
@@ -5,21 +5,15 @@ import org.simantics.db.Resource;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public class Stock extends ModelVariable {\r
        \r
-       public Stock(double x, double y, double w, double h,\r
-                       String name, IntegralExpression expression, Range range, String unit, String description) {\r
-               super(x, y, w, h, name, expression, range, unit, description);\r
+       public Stock() {\r
+               \r
        }\r
        \r
-       public Stock(double x, double y, double w, double h, Variable variable) {\r
-               super(x, y, w, h, variable);\r
-       }\r
-       \r
-       public Stock(double[] dimensions, Variable variable) {\r
-               super(dimensions, variable);\r
+       public Stock(double[] dim, String name, IntegralExpression expression, Range range, String unit, String description) {\r
+               super(dim, name, expression, range, unit, description);\r
        }\r
        \r
        @Override\r
index 70fc07092ef490929f85f9a5e85d3878bcc23256..e4f23be675269417a688016f24495c4080db10e2 100644 (file)
@@ -11,7 +11,6 @@ import org.simantics.diagram.stubs.G2DResource;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.modeling.ModelingResources;\r
-import org.simantics.sysdyn.modelImport.mdl.SketchElement;\r
 import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
@@ -23,44 +22,57 @@ public abstract class Symbol implements IWriteableObject {
        private double height;\r
 \r
        private Resource symbol;\r
-\r
-       public Symbol(double x, double y, double width, double height) {\r
-               this.x = x;\r
-               this.y = y;\r
-               this.width = width;\r
-               this.height = height;\r
-       }\r
        \r
-       public Symbol(double[] dimensions) {\r
-               this.x = dimensions[0];\r
-               this.y = dimensions[1];\r
-               this.width = dimensions[2];\r
-               this.height = dimensions[3];\r
+       public Symbol() {\r
+               \r
        }\r
-       \r
-       public Symbol(SketchElement element, double hOffset, double vOffset) {\r
-               this.x = element.getSysdyndX() + hOffset;\r
-               this.y = element.getSysdyndY() + vOffset;\r
-               this.width = element.getSysdynWidth();\r
-               this.height = element.getSysdynHeight();\r
+\r
+       public Symbol(double[] dim) {\r
+               x = dim[0];\r
+               y = dim[1];\r
+               width = dim[2];\r
+               height = dim[3];\r
        }\r
        \r
+       public void setDimensions(double[] dim) {\r
+               x = dim[0];\r
+               y = dim[1];\r
+               width = dim[2];\r
+               height = dim[3];\r
+       }\r
+\r
        public double getX() {\r
                return x;\r
        }\r
-       \r
+\r
+       public void setX(double x) {\r
+               this.x = x;\r
+       }\r
+\r
        public double getY() {\r
                return y;\r
        }\r
-       \r
+\r
+       public void setY(double y) {\r
+               this.y = y;\r
+       }\r
+\r
        public double getWidth() {\r
                return width;\r
        }\r
-       \r
+\r
+       public void setWidth(double width) {\r
+               this.width = width;\r
+       }\r
+\r
        public double getHeight() {\r
                return height;\r
        }\r
 \r
+       public void setHeight(double height) {\r
+               this.height = height;\r
+       }\r
+\r
        @Override\r
        public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
index 72cd912422f02a61efe3731b43efdbc54e12e0f3..f9451118ca1a8f8f3ad5fe6354fb35cea17222ee 100644 (file)
@@ -8,7 +8,6 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
-import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
 public class Valve extends ModelVariable {\r
        \r
@@ -20,21 +19,38 @@ public class Valve extends ModelVariable {
                INSIDE, BELOW, LEFT, ABOVE, RIGHT, UNSET\r
        }\r
        \r
+       private Orientation orientation;\r
        private TextPosition position;\r
        \r
-       public Valve(double x, double y, double w, double h,\r
-                       String name, Expression expression, Range range, String unit, String description, TextPosition position) {\r
-               super(x, y, w, h, name, expression, range, unit, description);\r
-               this.position = position;\r
+       public Valve() {\r
+               \r
        }\r
        \r
-       public Valve(double[] dimensions, Variable variable, TextPosition position) {\r
-               super(dimensions, variable);\r
+       public Valve(Orientation orientation, TextPosition position) {\r
+               this.orientation = orientation;\r
                this.position = position;\r
        }\r
        \r
-       public Valve(double x, double y, double w, double h, Variable variable, TextPosition position) {\r
-               super(x, y, w, h, variable);\r
+       public Valve(double[] dim, String name, Expression expression, Range range, String unit, String description, \r
+                       Orientation orientation, TextPosition position) {\r
+               super(dim, name, expression, range, unit, description);\r
+               this.orientation = orientation;\r
+               this.position = position;\r
+       }\r
+\r
+       public Orientation getOrientation() {\r
+               return orientation;\r
+       }\r
+\r
+       public void setOrientation(Orientation orientation) {\r
+               this.orientation = orientation;\r
+       }\r
+\r
+       public TextPosition getPosition() {\r
+               return position;\r
+       }\r
+\r
+       public void setPosition(TextPosition position) {\r
                this.position = position;\r
        }\r
 \r
@@ -44,22 +60,12 @@ public class Valve extends ModelVariable {
 \r
                Resource location = null;\r
                switch (position) {\r
-               case BELOW:\r
-                       location = sr.Bottom;\r
-                       break;\r
-               case LEFT:\r
-                       location = sr.Left;\r
-                       break;\r
-               case ABOVE:\r
-                       location = sr.Top;\r
-                       break;\r
-               case RIGHT:\r
-                       location = sr.Right;\r
-                       break;\r
-               default:\r
-                       System.err.println("unrecognized text location");\r
-                       location = sr.Bottom;\r
-                       break;\r
+               case BELOW: location = sr.Bottom; break;\r
+               case LEFT: location = sr.Left; break;\r
+               case ABOVE: location = sr.Top; break;\r
+               case RIGHT: location = sr.Right; break;\r
+               case UNSET:\r
+               default: System.err.println("valve text location not set"); location = sr.Bottom; break;\r
                }\r
                \r
                Resource valve = super.write(graph, parent, context);\r
index a39be3c39e666b4eef8d1acdf93464dc11c90e44..4b08a6712fbe8ea8e41755c584efa1f94a8bf04b 100644 (file)
@@ -13,24 +13,40 @@ import org.simantics.db.common.utils.ListUtils;
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
+import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
 \r
 public class EnumerationExpression extends Expression {\r
        \r
+       private List<Enumeration> enumerations;\r
        private List<SubscriptSubExpression> subExpressions;\r
        \r
-       public EnumerationExpression(Expression expression, String...indices) {\r
-               subExpressions = new ArrayList<SubscriptSubExpression>();\r
-               addExpression(expression, indices);\r
+       public EnumerationExpression(List<Enumeration> enumerations) {\r
+               this.enumerations = enumerations;\r
+               this.subExpressions = new ArrayList<SubscriptSubExpression>();\r
+               \r
+               System.err.print("create enumeration expression for ");\r
+               for (Enumeration e : enumerations) {\r
+                       System.err.print(e.getName()+" ");\r
+               }\r
+               System.err.println();\r
        }\r
        \r
        public void addExpression(Expression expression, String...indices) {\r
+               System.err.print("  add expression for indices ");\r
+               for (String s : indices) {\r
+                       System.err.print(s+" ");\r
+               }\r
+               System.err.println();\r
+               \r
+//             for (int i = 0; i < indices.length; i++) {\r
+//                     if (!enumerations.get(i).getValues().contains(indices)) {\r
+//                             System.err.println("tried adding an enumeration expression with incorrect indices");\r
+//                             return;\r
+//                     }\r
+//             }\r
                subExpressions.add(new SubscriptSubExpression(expression, indices));\r
        }\r
        \r
-       public void addExpressions(List<SubscriptSubExpression> expressions) {\r
-               subExpressions.addAll(expressions);\r
-       }\r
-       \r
        public List<SubscriptSubExpression> getExpressions() {\r
                return subExpressions;\r
        }\r
@@ -39,11 +55,6 @@ public class EnumerationExpression extends Expression {
        public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               ArrayList<Set<String>> enumerationIndices = new ArrayList<Set<String>>();\r
-               for (int i = 0; i < subExpressions.get(0).indices.length; i++) {\r
-                       enumerationIndices.add(new HashSet<String>());\r
-               }\r
-               \r
                for (SubscriptSubExpression subexpr : subExpressions) {\r
                        StringBuilder range = new StringBuilder();\r
                        range.append('[');\r
@@ -51,8 +62,6 @@ public class EnumerationExpression extends Expression {
                                if (i > 0)\r
                                        range.append(',');\r
                                range.append(subexpr.indices[i]);\r
-                               \r
-                               enumerationIndices.get(i).add(subexpr.indices[i]);\r
                        }\r
                        range.append(']');\r
                        \r
@@ -61,13 +70,13 @@ public class EnumerationExpression extends Expression {
                        graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING);\r
                }\r
                \r
-               ArrayList<Resource> enumerations = new ArrayList<Resource>();\r
-               for (Set<String> set : enumerationIndices) {\r
-                       enumerations.add(context.getEnumeration(set).getResource());\r
+               ArrayList<Resource> indexlist = new ArrayList<Resource>();\r
+               for (Enumeration enumeration : enumerations) {\r
+                       indexlist.add(enumeration.getResource());\r
                }\r
                \r
                // write array index list\r
-               graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, enumerations));\r
+               graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist));\r
                \r
                // set active expression?\r
                \r
index de793edbdb88d9ddc19f8fd2a18902772dfcacf2..8b571f815c99bc6dfe80a31d3e2caff1b61b240a 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.sysdyn.modelImport.model.support;\r
 \r
-import java.util.Arrays;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
@@ -18,22 +17,12 @@ public class Enumeration implements IWriteableObject {
        \r
        private String name;\r
        private Set<String> values;\r
-       private boolean copy;\r
        \r
        private Resource enumeration;\r
        \r
-       public Enumeration(String name, String...values) {\r
+       public Enumeration(String name, Set<String> values) {\r
                this.name = name;\r
-               this.values = new HashSet<String>(Arrays.asList(values));\r
-               \r
-               copy = false;\r
-       }\r
-       \r
-       public Enumeration(String name, Enumeration other) {\r
-               this.name = name;\r
-               this.values = other.getValues();\r
-               \r
-               copy = true;\r
+               this.values = values;\r
        }\r
        \r
        public String getName() {\r
@@ -62,9 +51,6 @@ public class Enumeration implements IWriteableObject {
                \r
                graph.claim(enumeration, sr.Enumeration_enumerationIndexList, ListUtils.create(graph, indices));\r
                \r
-               // register this enumeration with the context\r
-               context.registerEnumeration(this, copy);\r
-               \r
                return enumeration;\r
        }\r
        \r
index 03ecc6d544c7f30966e94cd6df3d97c8408fb5c1..e28c6b68471908d4c238017774e82d19949acbab 100644 (file)
@@ -11,15 +11,13 @@ public class Range implements IWriteableObject {
        private Double start;\r
        private Double end;\r
        private Double step;\r
-       private String original;\r
        \r
        private Resource resource;\r
        \r
-       public Range(Double start, Double end, Double step, String original) {\r
+       public Range(Double start, Double end, Double step) {\r
                this.start = start;\r
                this.end = end;\r
                this.step = step;\r
-               this.original = original;\r
        }\r
        \r
        public Double getStart() {\r
@@ -34,10 +32,6 @@ public class Range implements IWriteableObject {
                return step;\r
        }\r
 \r
-       public String originalString() {\r
-               return original;\r
-       }\r
-\r
        @Override\r
        public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
                return null;\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java
deleted file mode 100644 (file)
index aa08fb8..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-package org.simantics.sysdyn.modelImport.model.support;\r
-\r
-import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
-import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression;\r
-\r
-public class Variable {\r
-       \r
-       private String name;\r
-       private Expression expression;\r
-       private String unit;\r
-       private Range range;\r
-       private String description;\r
-       \r
-       public Variable(String name, Expression expression, String unit, Range range, String description) {\r
-               this.name = name;\r
-               this.expression = expression;\r
-               this.unit = unit;\r
-               this.range = range;\r
-               this.description = description;\r
-       }\r
-       \r
-       public String getName() {\r
-               return name;\r
-       }\r
-       \r
-       public Expression getExpression() {\r
-               return expression;\r
-       }\r
-       \r
-       public Range getRange() {\r
-               return range;\r
-       }\r
-\r
-       public String getUnit() {\r
-               return unit;\r
-       }\r
-\r
-       public String getDescription() {\r
-               return description;\r
-       }\r
-       \r
-       public void mergeWithVariable(Variable other) {\r
-               if (!name.equals(other.getName())) {\r
-                       System.err.println("attempted to merge variable "+name+" with variable "+other.getName());\r
-                       return;\r
-               }\r
-               if (expression instanceof EnumerationExpression && other.getExpression() instanceof EnumerationExpression) {\r
-                       ((EnumerationExpression)expression).addExpressions(((EnumerationExpression)other.getExpression()).getExpressions());\r
-               }\r
-               if (range == null) {\r
-                       range = other.getRange();\r
-               }\r
-               if (unit == null || unit.isEmpty()) {\r
-                       unit = other.getUnit();\r
-               }\r
-               if (description == null || description.isEmpty()) {\r
-                       description = other.getDescription();\r
-               }\r
-       }\r
-       \r
-}\r