]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Update vensim import with support for most equation types, subscript support still...
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 2 Apr 2014 15:39:17 +0000 (15:39 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 2 Apr 2014 15:39:17 +0000 (15:39 +0000)
refs #2924

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

35 files changed:
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java [moved from dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java with 79% similarity]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Sketch.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java [moved from dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript2.java with 64% similarity]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableObject.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java [moved from dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Element.java with 68% similarity]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/DelayExpression.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java [new file with mode: 0644]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java [new file with mode: 0644]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IExpression.java [deleted file]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IntegralExpression.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/LookupExpression.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/NormalExpression.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/SubscriptExpression.java [deleted file]
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java
dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java

index e4b6e4ef44c724a55ecfbb840f0251cde60c035d..82e8e07e2e59d0f2a4a8aa069b1029c562c3e9c5 100644 (file)
@@ -19,7 +19,7 @@ import java.io.InputStreamReader;
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 \r
-import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon;\r
+import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon;\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,17 +28,19 @@ 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.Subscript2;\r
+import org.simantics.sysdyn.modelImport.mdl.Subscript;\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
 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.Element;\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.Shadow;\r
 import org.simantics.sysdyn.modelImport.model.element.Stock;\r
 import org.simantics.sysdyn.modelImport.model.element.Valve;\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.Variable;\r
 \r
 public class MdlParser {\r
@@ -68,19 +70,14 @@ public class MdlParser {
                }\r
 \r
                // generate a sysdyn model from the mdl model\r
-               Model model = new Model(file.getName());\r
+               Model model = new Model(mdl.getName());\r
 \r
                double offset = 0;\r
 \r
                // do this in several passes\r
 \r
-               HashMap<Variable, Element> variableToElement = new HashMap<Variable, Element>();\r
-               ArrayList<SketchVariable> shadows = new ArrayList<SketchVariable>();\r
-               \r
-               for (Subscript2 ss : mdl.getSubscripts()) {\r
-                       System.err.println("SUBSCRIPT "+ss.getName()+": "+ss.getValues());\r
-               }\r
-               \r
+               HashMap<Variable, Symbol> variableToElement = new HashMap<Variable, Symbol>();\r
+\r
                // add sketch labels and independent elements\r
                for (Sketch sketch : mdl.getSketches()) {\r
                        sketch.setEdges();\r
@@ -97,13 +94,13 @@ public class MdlParser {
                                        continue;\r
                                }\r
 \r
-                               Element modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
+                               Symbol modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
                                model.addElement(modelElement);\r
                                sketch.elements.put(comment.getId(), modelElement);\r
                        }\r
 \r
                        for (SketchValve valve : sketch.getValves()) {\r
-                               Element modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset);\r
+                               Symbol modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset);\r
                                model.addElement(modelElement);\r
                                sketch.elements.put(valve.getId(), modelElement);\r
                                sketch.elements.put(valve.getAttachedVariable().getId(), modelElement);\r
@@ -121,7 +118,7 @@ public class MdlParser {
                                        continue;\r
                                }\r
 \r
-                               Element modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
+                               Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
                                model.addElement(modelElement);\r
                                sketch.elements.put(variable.getId(), modelElement);\r
                                variableToElement.put(variable.getVariable(), modelElement);\r
@@ -135,25 +132,30 @@ public class MdlParser {
                        for (SketchVariable variable : sketch.getVariables()) {\r
                                if (!variable.allowsIn()) {\r
                                        // the variable is a shadow variable\r
-                                       Element original = variableToElement.get(variable.getVariable());\r
+                                       Symbol original = variableToElement.get(variable.getVariable());\r
                                        if (original == null) {\r
-                                               System.err.println("original not found");\r
-                                               continue;\r
+                                               System.err.println("original not found for "+variable.getVariable().getName());\r
+                                               Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
+                                               model.addElement(modelElement);\r
+                                               sketch.elements.put(variable.getId(), modelElement);\r
+                                               variableToElement.put(variable.getVariable(), modelElement);\r
+                                       } \r
+                                       else {\r
+                                               Shadow modelElement = new Shadow(\r
+                                                               variable.getSysdyndX() + sketch.hOffset, \r
+                                                               variable.getSysdyndY() + sketch.vOffset, \r
+                                                               variable.getSysdynWidth(),\r
+                                                               variable.getSysdynHeight(),\r
+                                                               (ModelVariable)variableToElement.get(variable.getVariable()));\r
+                                               model.addShadow(modelElement);\r
+                                               sketch.elements.put(variable.getId(), modelElement);\r
                                        }\r
-                                       Shadow modelElement = new Shadow(\r
-                                                       variable.getSysdyndX() + sketch.hOffset, \r
-                                                       variable.getSysdyndY() + sketch.vOffset, \r
-                                                       variable.getSysdynWidth(),\r
-                                                       variable.getSysdynHeight(),\r
-                                                       variableToElement.get(variable.getVariable()));\r
-                                       model.addShadow(modelElement);\r
-                                       sketch.elements.put(variable.getId(), modelElement);\r
                                }\r
                        }\r
 \r
                        for (SketchConnection connection : sketch.getConnections()) {\r
-                               Element head = sketch.elements.get(connection.getTo());\r
-                               Element tail = sketch.elements.get(connection.getFrom());\r
+                               Symbol head = sketch.elements.get(connection.getTo());\r
+                               Symbol tail = sketch.elements.get(connection.getFrom());\r
                                Connection c = connection.getWriteableConnection(head, tail, sketch.vOffset);\r
                                if (c != null) {\r
                                        model.addConnection(c);\r
@@ -168,6 +170,9 @@ public class MdlParser {
                        throws IOException {\r
                MdlModel mdl = new MdlModel(file.getName());\r
                \r
+               // TODO: do this somewhere else and more controlled\r
+               mdl.addVariable(new Variable("Time", new NormalExpression("time"), "", null, ""), null);\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
                String line = reader.readLine();\r
@@ -186,114 +191,81 @@ public class MdlParser {
                        line = reader.readLine();\r
                }\r
 \r
-               // read variable data\r
-               line = parseVariables(reader, line, mdl);\r
-\r
-               if (line == null) {\r
-                       System.err.println("unexpected end of file");\r
-                       reader.close();\r
-                       return null;\r
-               }\r
-\r
-               // read sketch data\r
-               line = parseSketches(reader, line, mdl);\r
-\r
-               if (line == null) {\r
-                       System.err.println("unexpected end of file");\r
-                       reader.close();\r
-                       return null;\r
-               }\r
-\r
-               // read other data\r
-               do {\r
-                       // TODO: is there anything relevant here?\r
-               } while ((line = reader.readLine()) != null);\r
+               // START READING VARIABLE DATA\r
 \r
-               reader.close();\r
-               return mdl;\r
-       }\r
-\r
-       private static String parseVariables(BufferedReader reader, String line, MdlModel mdl) \r
-                       throws IOException {\r
+               // keep track of the current category\r
                String category = null;\r
 \r
                do {\r
-                       if (line.isEmpty())\r
+                       // skip empty lines\r
+                       if (line.isEmpty()) \r
                                continue;\r
 \r
+                       // combine the (possible) variable declaration into one line    \r
                        StringBuilder buffer = new StringBuilder();\r
-                       \r
                        do {\r
-                               if (line.endsWith("\\"))\r
-                                       buffer.append(line.substring(0, line.length()-1));\r
-                               else\r
-                                       buffer.append(line);\r
-\r
+                               // strip trailing '\' if present\r
+                               buffer.append(line.endsWith("\\") ? line.substring(0, line.length()-1) : line);\r
+                               // each variable declaration ends with '|'\r
                                if (line.endsWith("|"))\r
                                        break;\r
                        } while ((line = reader.readLine()) != null);\r
-                       \r
                        String str = buffer.toString();\r
 \r
-                       String cat = MdlUtils.getPossibleCategory(str);\r
-                       if (cat != null) {\r
+                       String cat;\r
+                       Variable var;\r
+                       Subscript sub;\r
+\r
+                       // parse the (possible) variable declaration\r
+                       if ((cat = MdlUtil.getPossibleCategory(str)) != null) {\r
                                category = cat;\r
-                               continue;\r
                        }\r
-\r
-                       Variable var = MdlUtils.getPossibleVariable(str, category);\r
-                       if (var != null) {\r
+                       else if ((var = MdlUtil.getPossibleVariable(str, category)) != null) {\r
                                mdl.addVariable(var, category);\r
-                               continue;\r
                        }\r
-                       \r
-                       Variable ss = MdlUtils.getPossibleSubscriptVariable(str, category, mdl);\r
-                       if (ss != null) {\r
-                               Variable original = mdl.getVariable(ss.getName());\r
-                               if (original == null) {\r
-                                       mdl.addVariable(ss, category);\r
-                               }\r
-                               else {\r
-                                       original.mergeExpression(ss);\r
-                               }\r
-                               continue;\r
+                       else if ((var = MdlUtil.getPossibleSubscriptVariable(str, category, mdl)) != null) {\r
+                               Variable orig = mdl.getVariable(var.getName());\r
+                               if (orig != null)\r
+                                       orig.mergeWithVariable(var);\r
+                               else\r
+                                       mdl.addVariable(var, category);\r
                        }\r
-                       \r
-                       Variable lu = MdlUtils.getPossibleLookUpVariable(str, category);\r
-                       if (lu != null) {\r
-                               System.err.println("found lookup variable "+lu.getName());\r
-                               continue;\r
+                       else if ((var = MdlUtil.getPossibleLookUpVariable(str, category)) != null) {\r
+                               mdl.addVariable(var, category);\r
                        }\r
-                       \r
-                       Variable plain = MdlUtils.getPossibleNoExpressionVariable(str);\r
-                       if (plain != null) {\r
-                               System.err.println("found plain variable "+plain.getName());\r
-                               continue;\r
+                       else if ((var = MdlUtil.getPossibleNoExpressionVariable(str)) != null) {\r
+                               mdl.addVariable(var, category);\r
                        }\r
-                       \r
-                       Subscript2 subscript = MdlUtils.getPossibleSubscript(str, mdl);\r
-                       if (subscript != null) {\r
-                               mdl.addSubscript(subscript);\r
-                               continue;\r
+                       else if ((sub = MdlUtil.getPossibleSubscript(str, mdl)) != null) {\r
+                               mdl.addSubscript(sub);\r
+                       }\r
+                       else {\r
+                               // if we got this far, the variable could not be parsed\r
+                               System.err.println("unrecognized variable "+str);\r
                        }\r
 \r
-                       // if we got this far, the variable could not be parsed\r
-                       System.err.println("unrecognized variable "+str);\r
                } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START));\r
 \r
-               return line;\r
-       }\r
+               // END READING VARIABLE DATA\r
 \r
-       private static String parseSketches(BufferedReader reader, String line, MdlModel mdl) \r
-                       throws IOException {\r
+               if (line == null) {\r
+                       System.err.println("unexpected end of file");\r
+                       reader.close();\r
+                       return null;\r
+               }\r
+\r
+               // START READING SKETCH DATA\r
+\r
+               // keep track of the current sketch\r
                Sketch sketch = null;\r
 \r
                do {\r
+                       // skip empty lines\r
                        if (line.isEmpty())\r
                                continue;\r
-                       \r
+\r
                        SketchObject so;\r
-                       \r
+\r
                        if (line.startsWith(SKETCH_START)) {\r
                                sketch = new Sketch();\r
                                mdl.addSketch(sketch);\r
@@ -307,16 +279,17 @@ public class MdlParser {
                        else if (line.startsWith("$")) {\r
                                // font declaration, nothing to do here\r
                        }\r
-                       else if ((so = MdlUtils.getPossibleSketchConnection(line)) != null) {\r
+                       else if ((so = MdlUtil.getPossibleSketchConnection(line)) != null) {\r
                                sketch.addConnection((SketchConnection)so);\r
                        }\r
-                       else if ((so = MdlUtils.getPossibleSketchVariable(line, mdl)) != null) {\r
-                               sketch.addVariable((SketchVariable)so);\r
+                       else if ((so = MdlUtil.getPossibleSketchVariable(line, mdl)) != null) {\r
+                               SketchVariable variable = (SketchVariable)so;\r
+                               sketch.addVariable(variable);\r
                        }\r
-                       else if ((so = MdlUtils.getPossibleSketchValve(line)) != null) {\r
+                       else if ((so = MdlUtil.getPossibleSketchValve(line)) != null) {\r
                                SketchValve valve = (SketchValve)so;\r
                                // the next row after a valve should always the variable associated with the valve\r
-                               SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), mdl);\r
+                               SketchVariable attached = MdlUtil.getPossibleSketchVariable(reader.readLine(), mdl);\r
                                if (attached == null || !attached.isAttached()) {\r
                                        System.err.println("attached variable not found for valve");\r
                                }\r
@@ -324,7 +297,7 @@ public class MdlParser {
                                sketch.addValve((SketchValve)valve);\r
                                sketch.addVariable(attached);\r
                        }\r
-                       else if ((so = MdlUtils.getPossibleSketchComment(line)) != null) {\r
+                       else if ((so = MdlUtil.getPossibleSketchComment(line)) != null) {\r
                                SketchComment comment = (SketchComment)so;\r
                                if (comment.hasTextLine()) {\r
                                        comment.setText(reader.readLine());\r
@@ -335,10 +308,27 @@ public class MdlParser {
                                // if we got this far, the element could not be parsed\r
                                System.err.println("unrecognized element "+line);\r
                        }\r
-                       \r
+\r
                } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END));\r
 \r
-               return line;\r
+               // END READING SKETCH DATA\r
+\r
+               if (line == null) {\r
+                       System.err.println("unexpected end of file");\r
+                       reader.close();\r
+                       return null;\r
+               }\r
+\r
+               // START READING OTHER DATA\r
+\r
+               do {\r
+                       // TODO: is there anything relevant here?\r
+               } while ((line = reader.readLine()) != null);\r
+\r
+               // END READING OTHER DATA\r
+\r
+               reader.close();\r
+               return mdl;\r
        }\r
 \r
 }\r
similarity index 79%
rename from dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java
rename to dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java
index 6bce7af0ef5771f9761bbf44020e5e229941050f..51d104e3cbac94f944739fac94387672568e4fae 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.sysdyn.modelImport;\r
 \r
+import java.util.ArrayList;\r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
@@ -9,18 +10,18 @@ import org.simantics.sysdyn.modelImport.mdl.SketchConnection;
 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.Subscript2;\r
+import org.simantics.sysdyn.modelImport.mdl.Subscript;\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.IExpression;\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.LookupExpression;\r
 import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;\r
-import org.simantics.sysdyn.modelImport.model.expression.SubscriptExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
 import org.simantics.sysdyn.modelImport.model.support.Variable;\r
 \r
-public class MdlUtils {\r
+public class MdlUtil {\r
        \r
        public enum ConnectionType {\r
                ARROW, LINE_ARROW, LINE_SEGMENT, OTHER\r
@@ -50,6 +51,7 @@ public class MdlUtils {
                        VAR_NAME+"\\s*=\\s*";\r
        private static final String EQUATION_PATTERN =\r
                        "([^~]*?(?:"+VAR_NAME_QUOTED+"[^~]*?)*)\\s*~\\s*";\r
+       \r
        private static final String UNIT_PATTERN =\r
                        "([^~]*?)\\s*~\\s*";\r
        private static final String DESC_PATTERN =\r
@@ -71,7 +73,7 @@ public class MdlUtils {
                }\r
                \r
                String name = normalize(matcher.group(variableName));\r
-               IExpression expression = parseEquation(matcher.group(variableEquation));\r
+               Expression expression = parseExpression(matcher.group(variableEquation));\r
                \r
                String unit = matcher.group(variableUnit);\r
                Range range = parseRange(unit);\r
@@ -107,12 +109,12 @@ public class MdlUtils {
                        return null;\r
                }\r
                \r
-               String name = matcher.group(subscriptVariableName);\r
+               String name = normalize(matcher.group(subscriptVariableName));\r
                // TODO: find out if subscript indices can contain commas and update\r
                // this method accordingly\r
                String[] indices = matcher.group(subscriptVariableIndices).split(",");\r
-               IExpression expression = parseEquation(matcher.group(subscriptVariableEquation));\r
-               SubscriptExpression e = new SubscriptExpression(indices, expression);\r
+               Expression expression = parseExpression(matcher.group(subscriptVariableEquation));\r
+               EnumerationExpression e = new EnumerationExpression(indices, expression);\r
                \r
                String unit = matcher.group(subscriptVariableUnit);\r
                Range range = parseRange(unit);\r
@@ -153,12 +155,12 @@ public class MdlUtils {
                        return null;\r
                }\r
                \r
-               String name = matcher.group(lookupVariableName);\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
-               String points = matcher.group(lookupVariablePoints);\r
+               double[] points = parseLookup(matcher.group(lookupVariablePoints));\r
                \r
                LookupExpression e = new LookupExpression(xMin, yMin, xMax, yMax, points);\r
                \r
@@ -172,6 +174,17 @@ public class MdlUtils {
                return new Variable(name, e, unit, range, description);\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 final String NO_EXP_PATTERN = \r
                        VAR_NAME+"\\s*~\\s*";\r
        \r
@@ -211,58 +224,101 @@ public class MdlUtils {
        private static final int subscriptType = 2;\r
        private static final int subscriptEquation = 3;\r
        \r
-       public static Subscript2 getPossibleSubscript(String line, MdlModel mdl) {\r
+       public static Subscript getPossibleSubscript(String line, MdlModel mdl) {\r
                Matcher matcher = Pattern.compile(subscriptPattern).matcher(line);\r
                \r
                if (!matcher.matches()) {\r
                        return null;\r
                }\r
                \r
-               String name = matcher.group(subscriptName);\r
+               String name = normalize(matcher.group(subscriptName));\r
                boolean equivalence = matcher.group(subscriptType).equals("<->");\r
                String expression = matcher.group(subscriptEquation);\r
                \r
                if (equivalence) {\r
-                       return new Subscript2(name, mdl.getSubscript(expression));\r
+                       return new Subscript(name, mdl.getSubscript(normalize(expression)));\r
                }\r
                else {\r
-                       return new Subscript2(name, expression.split(","));\r
+                       return new Subscript(name, expression.split(","));\r
                }\r
        }\r
        \r
-       private static IExpression parseEquation(String equation) {\r
-               if (equation.startsWith("INTEG")) {\r
-                       return parseIntegralExpression(equation);\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
+                               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 (equation.startsWith("GAME")) {\r
-                       // game expressions are treated as normal parameters as we do not \r
-                       // have equivalent functionality in sysdyn\r
-                       return parseNormalExpression(equation);\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 not supported\r
+                       return new NormalExpression(normalize(parameters[0]));\r
                }\r
                else {\r
-                       return parseNormalExpression(equation);\r
+                       // the function does not require special handling\r
+                       return new NormalExpression(normalize(equation));\r
                }\r
        }\r
        \r
-       private static IntegralExpression parseIntegralExpression(String equation) {\r
-               Matcher matcher = Pattern.compile("INTEG \\((.*),(.*)\\)").matcher(equation);\r
-               if (!matcher.matches()) {\r
-                       System.err.println("could not parse integral expression: "+equation);\r
-                       return null;\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
-               String integral = normalize(matcher.group(1));\r
-               String initial = normalize(matcher.group(2));\r
-               \r
-               return new IntegralExpression(integral, initial);\r
-       }\r
-       \r
-       private static DelayExpression parseDelayExpression(String equation) {\r
-               return null;\r
-       }\r
-       \r
-       private static NormalExpression parseNormalExpression(String equation) {\r
-               return new NormalExpression(normalize(equation));\r
+               return list.toArray(new String[list.size()]);\r
        }\r
        \r
        private static Range parseRange(String unit) {\r
@@ -285,10 +341,8 @@ public class MdlUtils {
                }\r
        }\r
        \r
-       private static final String categoryPattern = "\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*";\r
-       \r
        public static String getPossibleCategory(String line) {\r
-               Matcher matcher = Pattern.compile(categoryPattern).matcher(line);\r
+               Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line);\r
                \r
                if (!matcher.matches()) {\r
                        return null;\r
@@ -314,29 +368,6 @@ public class MdlUtils {
        private static final int connectionShape = 4;\r
        private static final int connectionPoints = 5;\r
        \r
-       private static final String COMMON =\r
-               //  x,   y,   w,   h,   sh,  bits,hid, hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
-                       SAVE+SAVE+SAVE+SAVE+SAVE+SAVE+SKIP+SKIP+SAVE+".*";\r
-       \r
-       public static final String sketchVariable =\r
-               //   n,   id,  name,        x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
-                       "10,"+SAVE+VAR_NAME+","+COMMON;\r
-       public static final String sketchValve =\r
-                       "11,"+SAVE+SAVE+        COMMON;\r
-       public static final String sketchComment =\r
-                       "12,"+SAVE+SAVE+        COMMON;\r
-       \r
-       // group indices for later use\r
-       private static final int elementId = 1;\r
-       private static final int elementName = 2;\r
-       private static final int elementX = 3;\r
-       private static final int elementY = 4;\r
-       private static final int elementWidth = 5;\r
-       private static final int elementHeight = 6;\r
-       private static final int elementShape = 7;\r
-       private static final int elementBits = 8;\r
-       private static final int elementTextPos = 9;\r
-       \r
        public static SketchConnection getPossibleSketchConnection(String line) {\r
                Matcher matcher = Pattern.compile(sketchConnection).matcher(line);\r
                \r
@@ -368,6 +399,29 @@ public class MdlUtils {
                return array;\r
        }\r
        \r
+       private static final String COMMON =\r
+               //  x,   y,   w,   h,   sh,  bits,hid, hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
+                       SAVE+SAVE+SAVE+SAVE+SAVE+SAVE+SKIP+SKIP+SAVE+".*";\r
+       \r
+       public static final String sketchVariable =\r
+               //   n,   id,  name,        x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
+                       "10,"+SAVE+VAR_NAME+","+COMMON;\r
+       public static final String sketchValve =\r
+                       "11,"+SAVE+SAVE+        COMMON;\r
+       public static final String sketchComment =\r
+                       "12,"+SAVE+SAVE+        COMMON;\r
+       \r
+       // group indices for later use\r
+       private static final int elementId = 1;\r
+       private static final int elementName = 2;\r
+       private static final int elementX = 3;\r
+       private static final int elementY = 4;\r
+       private static final int elementWidth = 5;\r
+       private static final int elementHeight = 6;\r
+       private static final int elementShape = 7;\r
+       private static final int elementBits = 8;\r
+       private static final int elementTextPos = 9;\r
+       \r
        public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) {\r
                Matcher matcher = Pattern.compile(sketchVariable).matcher(line);\r
                \r
@@ -377,6 +431,9 @@ public class MdlUtils {
                \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
+               }\r
                \r
                SketchVariable variable = new SketchVariable(id, var);\r
                \r
@@ -487,7 +544,7 @@ public class MdlUtils {
                // method above)\r
                str = str.replaceAll("\t", "");\r
 \r
-               // remove inline :AND: and :OR: etc (not done currently)\r
+               // remove inline operations\r
                str = removeInlineOperation(str, "AND");\r
                str = removeInlineOperation(str, "OR");\r
                \r
@@ -507,6 +564,8 @@ public class MdlUtils {
                        // this should work in most cases, even with nested parentheses \r
                        // in the left and right hand side expression\r
                        \r
+                       // TODO: does not work in all cases\r
+                       \r
                        // the regular expression can be split into 5 parts:\r
                        // 1. delimiter character on the left (either start of string, ',' or '(':\r
                        //    (?:^|[(,])\r
index 43fc4ce90cfe40d3ba56e3955400293ca1a32847..89e68cbfa75e4e6ffd338a760d6833bf50408bc7 100644 (file)
@@ -1,7 +1,6 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
 import java.util.ArrayList;\r
-import java.util.Collections;\r
 import java.util.HashMap;\r
 import java.util.List;\r
 \r
@@ -13,19 +12,22 @@ public class MdlModel {
        \r
        private HashMap<String, Variable> variables;\r
        private HashMap<String, ArrayList<Variable>> groups;\r
-       \r
-       private HashMap<String, Subscript2> subscripts;\r
-       \r
+       private HashMap<String, Subscript> subscripts;\r
        private ArrayList<Sketch> sketches;\r
        \r
        public MdlModel(String name) {\r
                this.name = name;\r
+               \r
                this.variables = new HashMap<String, Variable>();\r
                this.groups = new HashMap<String, ArrayList<Variable>>();\r
-               this.subscripts = new HashMap<String, Subscript2>();\r
+               this.subscripts = new HashMap<String, Subscript>();\r
                this.sketches = new ArrayList<Sketch>();\r
        }\r
        \r
+       public String getName() {\r
+               return name;\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
@@ -49,16 +51,16 @@ public class MdlModel {
                return new ArrayList<Variable>(variables.values());\r
        }\r
        \r
-       public void addSubscript(Subscript2 subscript) {\r
+       public void addSubscript(Subscript subscript) {\r
                subscripts.put(subscript.getName(), subscript);\r
        }\r
        \r
-       public Subscript2 getSubscript(String name) {\r
+       public Subscript getSubscript(String name) {\r
                return subscripts.get(name);\r
        }\r
        \r
-       public List<Subscript2> getSubscripts() {\r
-               return new ArrayList<Subscript2>(subscripts.values());\r
+       public List<Subscript> getSubscripts() {\r
+               return new ArrayList<Subscript>(subscripts.values());\r
        }\r
        \r
        public void addSketch(Sketch sketch) {\r
index 471f5f843a8fec75a9f448969c0ed511300f4f21..619b1f0077ccb0d6b95ff250c0fb893eb5739702 100644 (file)
@@ -4,18 +4,26 @@ import java.util.ArrayList;
 import java.util.HashMap;\r
 import java.util.List;\r
 \r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 \r
 public class Sketch {\r
        \r
        private String name;\r
        \r
+       private boolean edgesOutOfDate;\r
+       public double topEdge = 0;\r
+       public double bottomEdge = 0;\r
+       public double leftEdge = 0;\r
+       public double rightEdge = 0;\r
+\r
        private ArrayList<SketchComment> comments;\r
        private ArrayList<SketchConnection> connections;\r
        private ArrayList<SketchValve> valves;\r
        private ArrayList<SketchVariable> variables;\r
        \r
        public Sketch() {\r
+               edgesOutOfDate = true;\r
+               \r
                comments = new ArrayList<SketchComment>();\r
                connections = new ArrayList<SketchConnection>();\r
                valves = new ArrayList<SketchValve>();\r
@@ -30,6 +38,54 @@ public class Sketch {
                this.name = name;\r
        }\r
        \r
+       private void updateEdges() {\r
+               if (edgesOutOfDate) {\r
+                       boolean first = true;\r
+                       for (SketchElement e : getAllElements()) {\r
+                               topEdge    = first ? e.getSysdynTopEdge()    : Math.min(topEdge, e.getSysdynTopEdge());\r
+                               bottomEdge = first ? e.getSysdynBottomEdge() : Math.max(bottomEdge, e.getSysdynBottomEdge());\r
+                               leftEdge   = first ? e.getSysdynLeftEdge()   : Math.min(leftEdge, e.getSysdynLeftEdge());\r
+                               rightEdge  = first ? e.getSysdynRightEdge()  : Math.max(rightEdge, e.getSysdynRightEdge());\r
+                               first = false;\r
+                       }\r
+                       edgesOutOfDate = false;\r
+               }\r
+       }\r
+       \r
+       public double getTopEdge() {\r
+               updateEdges();\r
+               return topEdge;\r
+       }\r
+       \r
+       public double getBottomEdge() {\r
+               updateEdges();\r
+               return bottomEdge;\r
+       }\r
+       \r
+       public double getLeftEdge() {\r
+               updateEdges();\r
+               return leftEdge;\r
+       }\r
+       \r
+       public double getRightEdge() {\r
+               updateEdges();\r
+               return rightEdge;\r
+       }\r
+       \r
+       public double getWidth() {\r
+               updateEdges();\r
+               return rightEdge - leftEdge;\r
+       }\r
+       \r
+       public double getHeight() {\r
+               updateEdges();\r
+               return bottomEdge - topEdge;\r
+       }\r
+       \r
+       public void addObject(SketchComment comment) {\r
+               \r
+       }\r
+       \r
        public void addComment(SketchComment comment) {\r
                comments.add(comment);\r
        }\r
@@ -76,12 +132,7 @@ public class Sketch {
        public double hOffset = 0;\r
        public double vOffset = 0;\r
        \r
-       public double topEdge = 0;\r
-       public double bottomEdge = 0;\r
-       public double leftEdge = 0;\r
-       public double rightEdge = 0;\r
-       \r
-       public HashMap<Integer, Element> elements = new HashMap<Integer, Element>();\r
+       public HashMap<Integer, Symbol> elements = new HashMap<Integer, Symbol>();\r
        \r
        public void setEdges() {\r
                boolean first = true;\r
index 20c5ff7d6377bd1b6a9964e8512a7495b9d5b016..53ef8f7e530bb8f88e4c1147798e39025326d2f2 100644 (file)
@@ -1,9 +1,9 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
-import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon;\r
+import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon;\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.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 \r
 public class SketchComment extends SketchElement {\r
        \r
@@ -27,7 +27,7 @@ public class SketchComment extends SketchElement {
                return icon;\r
        }\r
        \r
-       public Element getModelElement(double hOffset, double vOffset) {\r
+       public Symbol getModelElement(double hOffset, double vOffset) {\r
                if (icon.equals(CommentIcon.CLOUD)) {\r
                        return new Cloud(\r
                                        getSysdyndX() + hOffset, \r
index ed5d06915a49d8bd37531af9ba3a4ea31f1836f5..58f23af43778c58d68b5e154c6161763954e26dd 100644 (file)
@@ -1,9 +1,9 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
-import org.simantics.sysdyn.modelImport.MdlUtils.ConnectionType;\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
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 import org.simantics.sysdyn.modelImport.model.element.Flow;\r
 \r
 public class SketchConnection extends SketchObject {\r
@@ -33,7 +33,7 @@ public class SketchConnection extends SketchObject {
                return type;\r
        }\r
 \r
-       public Connection getWriteableConnection(Element head, Element tail, double offset) {\r
+       public Connection getWriteableConnection(Symbol head, Symbol tail, double offset) {\r
                if (type.equals(ConnectionType.ARROW)) {\r
                        return new Dependency(head, tail, false, false, getSysdynAngle(tail, head, offset));\r
                }\r
@@ -48,7 +48,7 @@ public class SketchConnection extends SketchObject {
        }\r
 \r
        // TODO: comment this?\r
-       public double getSysdynAngle(Element from, Element to, double voffset) {\r
+       public double getSysdynAngle(Symbol from, Symbol to, double voffset) {\r
                if (points == null || points.length == 0) {\r
                        return 0;\r
                }\r
@@ -75,7 +75,7 @@ public class SketchConnection extends SketchObject {
                // length of (p1-p0) x (p2-p0)\r
                double dd = dx0*dy - dy0*dx; \r
 \r
-               if (Math.abs(dd) < 1e-5) {\r
+               if (Math.abs(dd) < 0.01) {\r
                        // Points are (almost) collinear\r
                        return 0;\r
                }\r
index 1220b61ced98bc40415effa1c9cdbd702aa6d325..2af38c2a6675ca32c9ea230210b1ae360ae2cc44 100644 (file)
@@ -1,6 +1,6 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 \r
 public abstract class SketchElement extends SketchObject {\r
 \r
@@ -105,6 +105,6 @@ public abstract class SketchElement extends SketchObject {
                return textLine;\r
        }\r
 \r
-       public abstract Element getModelElement(double hOffset, double vOffset);\r
+       public abstract Symbol getModelElement(double hOffset, double vOffset);\r
        \r
 }\r
index ee5bd15ab3c39bce2bab6414b82935a738da3513..7685c0568ea9f718d3c33183f0fcf2d55502df81 100644 (file)
@@ -1,6 +1,6 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
-import org.simantics.sysdyn.modelImport.model.element.Element;\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.TextPosition;\r
 \r
@@ -23,7 +23,7 @@ public class SketchValve extends SketchElement {
        }\r
        \r
        @Override\r
-       public Element getModelElement(double hOffset, double vOffset) {\r
+       public Symbol getModelElement(double hOffset, double vOffset) {\r
                return new Valve(\r
                                getSysdyndX() + hOffset, \r
                                getSysdyndY() + vOffset, \r
index fe1a375fb6f92933afdf030eb387d89366d2b6f8..2770b5e1deb26de864b155ed653d43cdc62472f4 100644 (file)
@@ -1,7 +1,7 @@
 package org.simantics.sysdyn.modelImport.mdl;\r
 \r
 import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\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
@@ -20,7 +20,7 @@ public class SketchVariable extends SketchElement {
        }\r
        \r
        @Override\r
-       public Element getModelElement(double hOffset, double vOffset) {\r
+       public Symbol getModelElement(double hOffset, double vOffset) {\r
                if (variable.getExpression() instanceof IntegralExpression) {\r
                        return new Stock(\r
                                        getSysdyndX() + hOffset, \r
similarity index 64%
rename from dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript2.java
rename to dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java
index dddcc7b466a9e43e792f08915d94e79f5cace381..eb6e8852191eb71a4d9c2dc468b3103b14d3ac76 100644 (file)
@@ -3,17 +3,19 @@ package org.simantics.sysdyn.modelImport.mdl;
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
 \r
-public class Subscript2 {\r
+public class Subscript {\r
+       \r
+       // TODO: THIS IS PROBABLY NOT NEEDED AT ALL, COULD USE ENUMERATION DIRECTLY\r
        \r
        private String name;\r
        private ArrayList<String> values;\r
        \r
-       public Subscript2(String name, String...values) {\r
+       public Subscript(String name, String...values) {\r
                this.name = name;\r
                this.values = new ArrayList<String>(Arrays.asList(values));\r
        }\r
        \r
-       public Subscript2(String name, Subscript2 other) {\r
+       public Subscript(String name, Subscript other) {\r
                this.name = name;\r
                this.values = other.getValues();\r
        }\r
index c704b7c48ee9a6ec64a8e6146222452830b24505..0d327b0c7c17661491aaad87be1116cd3fb45fe6 100644 (file)
@@ -18,9 +18,20 @@ import org.simantics.db.exception.DatabaseException;
 public interface IWriteableObject {\r
        \r
        /**\r
-        * @param graph WriteGraph\r
-        * @param parent The resource where the object is located\r
+        * Write this object to the graph and return the corresponding resource.\r
+        * \r
+        * @param graph \r
+        * @param parent \r
+        * @param context\r
+        * @return Resource\r
         * @throws DatabaseException \r
         */\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException;\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException;\r
+       \r
+       /**\r
+        * Return the resource corresponding to this object or null if this object has not been written yet.\r
+        * \r
+        * @return Resource\r
+        */\r
+       public Resource getResource();\r
 }\r
index 733b16a219c0dab313267d23436a4f91aa852945..2cc8e282fadc17b06242edf9e6e89604f421e727 100644 (file)
@@ -25,7 +25,7 @@ import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.sysdyn.modelImport.model.element.Connection;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
 import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
 import org.simantics.sysdyn.modelImport.model.element.Shadow;\r
 import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
@@ -41,17 +41,19 @@ public class Model implements IWriteableObject {
        private HashMap<String, Enumeration> enumerations;\r
        private HashMap<String, ModelVariable> variables;\r
        \r
-       private ArrayList<Element> elements;\r
+       private ArrayList<Symbol> elements;\r
        private ArrayList<Shadow> shadows;\r
        private ArrayList<Connection> connections;\r
        \r
+       private Resource model;\r
+       \r
        public Model(String name) {\r
                this.name = name;\r
                \r
                enumerations = new HashMap<String, Enumeration>();\r
                variables = new HashMap<String, ModelVariable>();\r
                \r
-               elements = new ArrayList<Element>();\r
+               elements = new ArrayList<Symbol>();\r
                shadows = new ArrayList<Shadow>();\r
                connections = new ArrayList<Connection>();\r
        }\r
@@ -112,7 +114,7 @@ public class Model implements IWriteableObject {
                return variables.values();\r
        }\r
                \r
-       public void addElement(Element element) {\r
+       public void addElement(Symbol element) {\r
                elements.add(element);\r
        }\r
        \r
@@ -125,13 +127,13 @@ public class Model implements IWriteableObject {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                SimulationResource sim = SimulationResource.getInstance(graph);\r
                \r
-               Resource model = ModelUtils.createModel(graph);\r
+               model = ModelUtils.createModel(graph);\r
                graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING);\r
                \r
                // TODO: set simulation parameters\r
@@ -142,7 +144,7 @@ public class Model implements IWriteableObject {
                        e.write(graph, configuration, context);\r
                }\r
                \r
-               for (Element e : elements) {\r
+               for (Symbol e : elements) {\r
                        e.write(graph, configuration, context);\r
                }\r
                \r
@@ -156,6 +158,13 @@ public class Model implements IWriteableObject {
                \r
                Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
                graph.claimLiteral(diagram, dr.HasModCount, context.getObjectCount(), Bindings.LONG);\r
+               \r
+               return model;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getResource() {\r
+               return model;\r
        }\r
        \r
 }\r
index 80f3ba1e49f760911f674171faf7ce6e0169a913..39e978e441c8e72eded3df7635a58e2d1f513952 100644 (file)
@@ -1,5 +1,9 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
+\r
 public class WriteContext {\r
        \r
        private long objectCount;\r
@@ -9,8 +13,17 @@ public class WriteContext {
        private long dependencyCount;\r
        private long flowCount;\r
        \r
+       private HashMap<String, Enumeration> enumerations;\r
+       \r
        public WriteContext() {\r
-               this.objectCount = 0;\r
+               objectCount = 0;\r
+               \r
+               cloudCount = 0;\r
+               shadowCount = 0;\r
+               dependencyCount = 0;\r
+               flowCount = 0;\r
+               \r
+               enumerations = new HashMap<String, Enumeration>();\r
        }\r
        \r
        public String getNextObject() {\r
@@ -36,5 +49,19 @@ public class WriteContext {
        public String getNextFlow() {\r
                return "Flow" + flowCount++;\r
        }\r
+       \r
+       // register an enumeration, necessary for enumeration expressions\r
+       public void registerEnumeration(Enumeration enumeration) {\r
+               \r
+       }\r
+       \r
+       // attempt to obtain a reference to an enumeration based on either the \r
+       // name of the enumeration or the name of one of the elements of the \r
+       // enumeration\r
+       // (TODO: this must be updated to something more clever if there are\r
+       // collisions between names of enumerations and elements of enumerations)\r
+       public Enumeration resolveEnumeration(String lookup) {\r
+               return null;\r
+       }\r
 \r
 }\r
index 3e2654fa58691f2cb51dfc00f86969478ff3aaef..add03008160b8bb7ec5d3232672355b28f5f328a 100644 (file)
@@ -1,43 +1,31 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-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.expression.IExpression;\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, IExpression expression, Range range, String unit, String description) {\r
+                       String name, Expression expression, Range range, String unit, String description) {\r
                super(x, y, w, h, name, expression, range, unit, description);\r
        }\r
        \r
        public Auxiliary(double x, double y, double w, double h, Variable variable) {\r
                super(x, y, w, h, variable);\r
        }\r
-\r
-       @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
-               Resource variable = createVariable(graph, sr.Auxiliary, parent, context);\r
-               createSymbol(graph, sr.AuxiliarySymbol, variable, parent, context);\r
-               \r
-               setResource(variable);\r
-       }\r
-\r
+       \r
        @Override\r
-       public boolean supportsDependencies() {\r
-               return true;\r
+       public Resource getVariableType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).Auxiliary;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsFlows() {\r
-               return false;\r
+       public Resource getSymbolType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).AuxiliarySymbol;\r
        }\r
 \r
 }\r
index 1aba25843dec6928c529acde25e5c2380efb6b8f..bb1f2723ca738350d0fe4ff48049025a229fac31 100644 (file)
@@ -1,41 +1,47 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.layer0.Layer0;\r
 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.model.WriteContext;\r
 \r
-public class Cloud extends Element {\r
+public class Cloud extends Symbol {\r
+       \r
+       private Resource cloud;\r
        \r
        public Cloud(double x, double y, double w, double h) {\r
                super(x, y, w, h);\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
+               ModelingResources mr = ModelingResources.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource cloud = GraphUtils.create2(graph, sr.Cloud,\r
+               cloud = GraphUtils.create2(graph, sr.Cloud,\r
                                l0.HasName, context.getNextCloud(),\r
                                l0.PartOf, parent);\r
                \r
-               createSymbol(graph, sr.CloudSymbol, cloud, parent, context);\r
+               Resource symbol = super.write(graph, parent, context);\r
+               graph.claim(symbol, mr.ElementToComponent, cloud);\r
                \r
-               setResource(cloud);\r
+               return cloud;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsDependencies() {\r
-               return false;\r
+       public Resource getSymbolType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).CloudSymbol;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsFlows() {\r
-               return true;\r
+       public Resource getResource() {\r
+               return cloud;\r
        }\r
 \r
 }\r
index 264ec411db337b6fd1afc759cf3500e0cf3302c1..fb737ba763da04fbbf91f797a56e0416b1d8adad 100644 (file)
@@ -1,16 +1,15 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
 import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
-public class Comment extends Element {\r
+public class Comment extends Symbol {\r
        \r
        private String text;\r
        \r
@@ -20,25 +19,24 @@ public class Comment extends Element {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
-               SysdynResource sr = SysdynResource.getInstance(graph);          \r
                \r
-               Resource comment = createSymbol(graph, sr.AdditionalSymbols_MultilineText, null, parent, context);\r
+               Resource symbol = super.write(graph, parent, context);\r
                \r
-               graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING);\r
+               graph.claimLiteral(symbol, dr.HasText, text, Bindings.STRING);\r
                \r
-               setResource(comment);\r
+               return symbol;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsDependencies() {\r
-               return false;\r
+       public Resource getSymbolType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).AdditionalSymbols_MultilineText;\r
        }\r
 \r
        @Override\r
-       public boolean supportsFlows() {\r
-               return false;\r
+       public Resource getResource() {\r
+               // comments only have diagram resources\r
+               return getDiagramResource();\r
        }\r
-       \r
 }\r
index 00119d31315758467109bfe0dab6b99a6a87c205..d920ea7501cf3a02cbb58f260879b376ca7e34f1 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.utils.OrderedSetUtils;\r
@@ -15,49 +16,41 @@ import org.simantics.sysdyn.modelImport.model.WriteContext;
 \r
 public abstract class Connection implements IWriteableObject {\r
 \r
-       protected Element head;\r
-       protected Element tail;\r
+       protected Symbol head;\r
+       protected Symbol tail;\r
+       \r
+       private Resource connection;\r
+       private Resource diagramConnection;\r
 \r
-       public Connection(Element head, Element tail) {\r
+       public Connection(Symbol head, Symbol tail) {\r
                this.head = head;\r
                this.tail = tail;\r
        }\r
-\r
-       protected Resource createConnection(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
-                       throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph); \r
-               ModelingResources mr = ModelingResources.getInstance(graph);\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
-               Resource connection = GraphUtils.create2(graph, type,\r
-                               sr.Variable_HasHead, head.getResource(),\r
-                               sr.Variable_HasTail, tail.getResource(),\r
-                               l0.PartOf, parent);\r
-               graph.claim(connection, mr.Mapped, connection);\r
-               \r
-               return connection;\r
-       }\r
        \r
-       protected Resource createSymbol(WriteGraph graph, Resource type, Resource connection, Resource parent, WriteContext context) \r
-                       throws DatabaseException {\r
+       @Override\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
+               connection = GraphUtils.create2(graph, getConnectionType(graph),\r
+                               sr.Variable_HasHead, head.getResource(),\r
+                               sr.Variable_HasTail, tail.getResource(),\r
+                               l0.PartOf, parent);\r
+               graph.claim(connection, mr.Mapped, connection);\r
+               \r
                Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
                \r
-               Resource diagramTail = graph.getPossibleObject(tail.getResource(), mr.ComponentToElement);\r
                Resource tailConnector = GraphUtils.create2(graph, dr.Connector,\r
-                               sr.HasTailTerminal, diagramTail);\r
+                               sr.HasTailTerminal, tail.getDiagramResource());\r
 \r
-               Resource diagramHead = graph.getPossibleObject(head.getResource(), mr.ComponentToElement);\r
                Resource headConnector = GraphUtils.create2(graph, dr.Connector,\r
-                               sr.HasHeadTerminal, diagramHead,\r
+                               sr.HasHeadTerminal, head.getDiagramResource(),\r
                                dr.AreConnected, tailConnector);\r
 \r
-               Resource diagramConnection = GraphUtils.create2(graph, type,\r
+               diagramConnection = GraphUtils.create2(graph, getDiagramConnectionType(graph),\r
                                l0.HasName, context.getNextObject(),\r
                                l0.PartOf, diagram,\r
                                sr2.HasConnectionType, sr.SysdynConnectionType,\r
@@ -67,6 +60,18 @@ public abstract class Connection implements IWriteableObject {
 \r
                OrderedSetUtils.add(graph, diagram, diagramConnection);\r
                \r
+               return connection;\r
+       }\r
+       \r
+       public abstract Resource getConnectionType(ReadGraph graph);\r
+       public abstract Resource getDiagramConnectionType(ReadGraph graph);\r
+       \r
+       @Override\r
+       public Resource getResource() {\r
+               return connection;\r
+       }\r
+       \r
+       public Resource getDiagramResource() {\r
                return diagramConnection;\r
        }\r
 \r
index 3fab1e5c12a8e7c70758051620abecfd1abdcdab..72e34cb61bc4c0609aac261c97eb78d724f98a88 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
 import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
@@ -14,7 +15,7 @@ public class Dependency extends Connection {
        private boolean showDelay;\r
        private double angle;\r
        \r
-       public Dependency(Element head, Element tail, boolean showArrow, boolean showDelay, double angle) {\r
+       public Dependency(Symbol head, Symbol tail, boolean showArrow, boolean showDelay, double angle) {\r
                super(head, tail);\r
                this.showArrow = showArrow;\r
                this.showDelay = showDelay;\r
@@ -22,21 +23,32 @@ public class Dependency extends Connection {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                if (head == null || tail == null) {\r
                        System.err.println("dependency missing head or tail");\r
-                       return;\r
+                       return null;\r
                }\r
                \r
                Layer0 l0 = Layer0.getInstance(graph);\r
-               \r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource dependency = createConnection(graph, sr.Dependency, parent, context);\r
+               Resource dependency = super.write(graph, parent, context);\r
                graph.claimLiteral(dependency, l0.HasName, context.getNextDependency(), Bindings.STRING);\r
-               Resource symbol = createSymbol(graph, sr.DependencyConnection, dependency, parent, context);\r
                \r
-               graph.claimLiteral(symbol, sr.Dependency_angle, angle, Bindings.DOUBLE);\r
+               graph.claimLiteral(getDiagramResource(), sr.Dependency_angle, angle, Bindings.DOUBLE);\r
+               \r
+               return dependency;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getConnectionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).Dependency;\r
        }\r
+       \r
+       @Override\r
+       public Resource getDiagramConnectionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).DependencyConnection;\r
+       }\r
+       \r
 \r
 }\r
index 1b40f2fc4a891433f664b9b1759bbb6f8272ea18..da51170f2f269ba35ffccdd7359ce46ad6667904 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
 import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
@@ -10,24 +11,33 @@ import org.simantics.sysdyn.modelImport.model.WriteContext;
 \r
 public class Flow extends Connection {\r
        \r
-       public Flow(Element head, Element tail) {\r
+       public Flow(Symbol head, Symbol tail) {\r
                super(head, tail);\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                if (head == null || tail == null) {\r
                        System.err.println("flow missing head or tail");\r
-                       return;\r
+                       return null;\r
                }\r
                \r
                Layer0 l0 = Layer0.getInstance(graph);\r
                \r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-               \r
-               Resource flow = createConnection(graph, sr.Flow, parent, context);\r
+               Resource flow = super.write(graph, parent, context);\r
                graph.claimLiteral(flow, l0.HasName, context.getNextFlow(), Bindings.STRING);\r
-               Resource symbol = createSymbol(graph, sr.FlowConnection, flow, parent, context);\r
+               \r
+               return flow;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getConnectionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).Flow;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getDiagramConnectionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).FlowConnection;\r
        }\r
 \r
 }\r
index fd669dc6e55c4a7811d8e3f4ba7e066c579dc54f..cd2f452f28434d45f6da8ba87cbcc073636c5142 100644 (file)
@@ -1,8 +1,7 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
-import java.util.List;\r
-\r
 import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
@@ -11,22 +10,22 @@ import org.simantics.layer0.utils.direct.GraphUtils;
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
-import org.simantics.sysdyn.modelImport.model.support.Enumeration;\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 Element {\r
+public abstract class ModelVariable extends Symbol {\r
 \r
-       protected String name;\r
-       protected IExpression expression;\r
-       protected Range range;\r
-       protected String unit;\r
-       protected String description;\r
+       private String name;\r
+       private Expression expression;\r
+       private Range range;\r
+       private String unit;\r
+       private String description;\r
        \r
-       protected List<Enumeration> enumerations;\r
+       private Resource variable;\r
 \r
-       public ModelVariable(double x, double y, double w, double h, String name, IExpression expression, Range range, String unit, String description) {\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
                this.name = name;\r
                this.expression = expression;\r
@@ -48,21 +47,22 @@ public abstract class ModelVariable extends Element {
                return name;\r
        }\r
 \r
-       public Resource createVariable(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
-                       throws DatabaseException {\r
+       @Override\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
 \r
-               Resource variable = GraphUtils.create2(graph, type\r
+               variable = GraphUtils.create2(graph, getVariableType(graph)\r
                                l0.HasName, name,\r
                                l0.PartOf, parent);\r
                graph.claim(variable, mr.Mapped, variable);\r
                \r
-               expression.write(graph, variable, context);\r
-               \r
                // TODO: write references to enumerations here?\r
                \r
+               if (expression != null) {\r
+                       Resource expressionList = expression.write(graph, variable, context);\r
+               }\r
                if (range != null) {\r
                        range.write(graph, variable, context);\r
                }\r
@@ -72,8 +72,18 @@ public abstract class ModelVariable extends Element {
                if (description != null && !description.isEmpty()) {\r
                        graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING);\r
                }\r
+               \r
+               Resource symbol = super.write(graph, parent, context);\r
+               graph.claim(symbol, mr.ElementToComponent, variable);\r
 \r
                return variable;\r
        }\r
+       \r
+       public abstract Resource getVariableType(ReadGraph graph);\r
+       \r
+       @Override\r
+       public Resource getResource() {\r
+               return variable;\r
+       }\r
 \r
 }\r
index b699fa9e64f5b7f72e2fea1fcb49816230bfd13e..8f3e33eb0f2bc92090caa098e4f14873cb007fed 100644 (file)
@@ -1,52 +1,58 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.layer0.Layer0;\r
 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 Element {\r
+public class Shadow extends Symbol {\r
 \r
-       private Element original;\r
+       private ModelVariable original;\r
        \r
-       public Shadow(double x, double y, double w, double h, Element original) {\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
        }\r
        \r
-       public Shadow(SketchVariable variable, double hOffset, double vOffset, Element original) {\r
+       public Shadow(SketchVariable variable, double hOffset, double vOffset, ModelVariable original) {\r
                super(variable, hOffset, vOffset);\r
                this.original = original;\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
+               ModelingResources mr = ModelingResources.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource shadow = GraphUtils.create2(graph, sr.Shadow,\r
+               shadow = GraphUtils.create2(graph, sr.Shadow,\r
                                l0.HasName, context.getNextShadow(),\r
                                l0.PartOf, parent);\r
                \r
                graph.claim(shadow, sr.Shadow_original, original.getResource());\r
                \r
-               createSymbol(graph, sr.ShadowSymbol, shadow, parent, context);\r
+               Resource symbol = super.write(graph, parent, context);\r
+               graph.claim(symbol, mr.ElementToComponent, shadow);\r
                \r
-               setResource(shadow);\r
+               return shadow;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsDependencies() {\r
-               return original.supportsDependencies();\r
+       public Resource getSymbolType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).ShadowSymbol;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsFlows() {\r
-               return original.supportsFlows();\r
+       public Resource getResource() {\r
+               return shadow;\r
        }\r
 \r
 }\r
index 3c958ec1f08dc774b7127e69c30bad6c63bcfcaa..03351daa8512a3c6e0061603aaca537e91cb3859 100644 (file)
@@ -1,10 +1,8 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-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.expression.IntegralExpression;\r
 import org.simantics.sysdyn.modelImport.model.support.Range;\r
 import org.simantics.sysdyn.modelImport.model.support.Variable;\r
@@ -20,23 +18,14 @@ public class Stock extends ModelVariable {
                super(x, y, w, h, variable);\r
        }\r
        \r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
-               Resource stock = createVariable(graph, sr.Stock, parent, context);\r
-               createSymbol(graph, sr.StockSymbol, stock, parent, context);\r
-\r
-               setResource(stock);\r
-       }\r
-\r
        @Override\r
-       public boolean supportsDependencies() {\r
-               return true;\r
+       public Resource getVariableType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).Stock;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsFlows() {\r
-               return true;\r
+       public Resource getSymbolType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).StockSymbol;\r
        }\r
        \r
 }\r
similarity index 68%
rename from dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Element.java
rename to dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java
index 8baf0a2a368d8594663874e3476e61e156e7b558..58e9917470589cfbde1e8a8f0f6f38e90557b7eb 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
 import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.utils.OrderedSetUtils;\r
@@ -14,36 +15,28 @@ import org.simantics.sysdyn.modelImport.mdl.SketchElement;
 import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
-public abstract class Element implements IWriteableObject {\r
+public abstract class Symbol implements IWriteableObject {\r
        \r
        private double x;\r
        private double y;\r
        private double width;\r
        private double height;\r
 \r
-       private Resource resource;\r
+       private Resource symbol;\r
 \r
-       public Element(double x, double y, double width, double height) {\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 Element(SketchElement element, double hOffset, double vOffset) {\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
-\r
-       public Resource getResource() {\r
-               return resource;\r
-       }\r
-\r
-       protected void setResource(Resource resource) {\r
-               this.resource = resource;\r
-       }\r
        \r
        public double getX() {\r
                return x;\r
@@ -61,7 +54,8 @@ public abstract class Element implements IWriteableObject {
                return height;\r
        }\r
 \r
-       public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, Resource parent, WriteContext context) throws DatabaseException {\r
+       @Override\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
                G2DResource g2d = G2DResource.getInstance(graph);\r
                Layer0 l0 = Layer0.getInstance(graph);\r
@@ -69,23 +63,22 @@ public abstract class Element implements IWriteableObject {
 \r
                Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
                \r
-               Resource symbol = GraphUtils.create2(graph, type,\r
+               symbol = GraphUtils.create2(graph, getSymbolType(graph),\r
                                l0.HasName, context.getNextObject(),\r
                                l0.PartOf, diagram);\r
-               \r
-               if (variable != null) {\r
-                       graph.claim(symbol, mr.ElementToComponent, variable);\r
-               }\r
 \r
                double[] transform = { 1.0, 0.0, 0.0, 1.0, x, y };\r
                graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform, Bindings.DOUBLE_ARRAY);\r
 \r
                OrderedSetUtils.add(graph, diagram, symbol);\r
-\r
+               \r
                return symbol;\r
        }\r
        \r
-       public abstract boolean supportsDependencies();\r
-       public abstract boolean supportsFlows();\r
+       public abstract Resource getSymbolType(ReadGraph graph);\r
+       \r
+       public Resource getDiagramResource() {\r
+               return symbol;\r
+       }\r
 \r
 }\r
index 559eff52f30593d19224a4aa7b7e48ff7d2e7a10..521fc1e39d5d2eab540d01b3c2cc0250f40121f9 100644 (file)
@@ -1,11 +1,12 @@
 package org.simantics.sysdyn.modelImport.model.element;\r
 \r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 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.expression.IExpression;\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
@@ -22,7 +23,7 @@ public class Valve extends ModelVariable {
        private TextPosition position;\r
        \r
        public Valve(double x, double y, double w, double h,\r
-                       String name, IExpression expression, Range range, String unit, String description, TextPosition position) {\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
        }\r
@@ -33,12 +34,9 @@ public class Valve extends ModelVariable {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
 \r
-               Resource valve = createVariable(graph, sr.Valve, parent, context);\r
-               Resource symbol = createSymbol(graph, sr.ValveSymbol, valve, parent, context);\r
-               \r
                Resource location = null;\r
                switch (position) {\r
                case BELOW:\r
@@ -59,19 +57,21 @@ public class Valve extends ModelVariable {
                        break;\r
                }\r
                \r
-               graph.claim(symbol, sr.ValveSymbol_textLocation, location);\r
-\r
-               setResource(valve);\r
+               Resource valve = super.write(graph, parent, context);\r
+               \r
+               graph.claim(getDiagramResource(), sr.ValveSymbol_textLocation, location);\r
+               \r
+               return valve;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsDependencies() {\r
-               return true;\r
+       public Resource getVariableType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).Valve;\r
        }\r
-\r
+       \r
        @Override\r
-       public boolean supportsFlows() {\r
-               return true;\r
+       public Resource getSymbolType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).ValveSymbol;\r
        }\r
 \r
 }\r
index 07a5b976991710a2826841df4d79280176e2851e..f740d2012e75034c90fcb8d4216b484df466c2e7 100644 (file)
@@ -1,29 +1,43 @@
 package org.simantics.sysdyn.modelImport.model.expression;\r
 \r
-import java.util.Arrays;\r
-\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
-public class DelayExpression implements IExpression {\r
+public class DelayExpression extends Expression {\r
+       \r
+       private String expression;\r
+       private String time;\r
+       private String initial;\r
+       private int order;\r
+       \r
+       public DelayExpression(String expression, String time, String initial, int order) {\r
+               this.expression = expression;\r
+               this.time = time;\r
+               this.initial = initial;\r
+               this.order = order;\r
+       }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-//             Resource expression = GraphUtils.create2(graph, sr.DelayExpression,\r
-//                             sr.StockExpression_integralEquation, integral,\r
-//                             sr.StockExpression_initialEquation, initial,\r
-//                             l0.PartOf, parent);\r
-//             // TODO: why is the expression stored in two places?\r
-//             graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
+               Resource expr = super.write(graph, parent, context);\r
+               \r
+               graph.claimLiteral(expr, sr.DelayExpression_expression, expression, Bindings.STRING);\r
+               graph.claimLiteral(expr, sr.DelayExpression_delayTime, time, Bindings.STRING);\r
+               graph.claimLiteral(expr, sr.DelayExpression_initialValue, initial, Bindings.STRING);\r
+               graph.claimLiteral(expr, sr.DelayExpression_order, order, Bindings.INTEGER);\r
+               \r
+               return expr;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getExpressionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).DelayExpression;\r
        }\r
-\r
 }\r
diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java
new file mode 100644 (file)
index 0000000..bdb43e1
--- /dev/null
@@ -0,0 +1,97 @@
+package org.simantics.sysdyn.modelImport.model.expression;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.modelImport.model.WriteContext;\r
+\r
+public class EnumerationExpression extends Expression {\r
+       \r
+       private SubscriptSubExpression active;\r
+       private ArrayList<SubscriptSubExpression> subExpressions;\r
+       \r
+       public EnumerationExpression(String[] indices, Expression expression) {\r
+               active = new SubscriptSubExpression(indices, expression);\r
+               subExpressions = new ArrayList<SubscriptSubExpression>();\r
+               subExpressions.add(active);\r
+       }\r
+       \r
+       public void addExpression(String[] indices, Expression expression) {\r
+               subExpressions.add(new SubscriptSubExpression(indices, expression));\r
+       }\r
+       \r
+       public List<SubscriptSubExpression> getExpressions() {\r
+               return subExpressions;\r
+       }\r
+       \r
+       public void addExpressions(List<SubscriptSubExpression> expressions) {\r
+               subExpressions.addAll(expressions);\r
+       }\r
+       \r
+       @Override\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               for (SubscriptSubExpression subexpr : subExpressions) {\r
+                       subexpr.write(graph, parent, context);\r
+               }\r
+               \r
+               // write array index list\r
+               //graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, Arrays.asList()));\r
+               \r
+               // set active expression?\r
+               \r
+               return active.getResource();\r
+       }\r
+       \r
+       private class SubscriptSubExpression extends Expression {\r
+               private String[] indices;\r
+               private Expression expression;\r
+               \r
+               public SubscriptSubExpression(String[] indices, Expression expression) {\r
+                       this.indices = indices;\r
+                       this.expression = expression;\r
+               }\r
+               \r
+               @Override\r
+               public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                       \r
+                       StringBuilder range = new StringBuilder();\r
+                       range.append('[');\r
+                       for (String index : indices) {\r
+                               if (range.length() > 1)\r
+                                       range.append(',');\r
+                               range.append(index);\r
+                       }\r
+                       range.append(']');\r
+                       \r
+                       Resource expr = super.write(graph, parent, context);\r
+                       \r
+                       graph.claimLiteral(expr, sr.Expression_arrayRange, range.toString(), Bindings.STRING);\r
+                       \r
+                       return expr;\r
+               }\r
+\r
+               @Override\r
+               public Resource getExpressionType(ReadGraph graph) {\r
+                       return expression.getExpressionType(graph);\r
+               }\r
+               \r
+       }\r
+\r
+       @Override\r
+       public Resource getExpressionType(ReadGraph graph) {\r
+               return active.getExpressionType(graph);\r
+       }\r
+}\r
diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java
new file mode 100644 (file)
index 0000000..4bac0c8
--- /dev/null
@@ -0,0 +1,45 @@
+package org.simantics.sysdyn.modelImport.model.expression;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
+import org.simantics.sysdyn.modelImport.model.WriteContext;\r
+\r
+public abstract class Expression implements IWriteableObject {\r
+\r
+       private Resource expr;\r
+\r
+       @Override\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               expr = GraphUtils.create2(graph, getExpressionType(graph),\r
+                               l0.PartOf, parent);\r
+\r
+               // insert the expression into the expression list of the variable \r
+               // (necessary because of enumeration expressions)\r
+               Resource list = graph.getPossibleObject(parent, sr.Variable_expressionList);\r
+               if (list != null)\r
+                       ListUtils.insertBack(graph, list, Arrays.asList(expr));\r
+               else\r
+                       graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expr)));\r
+\r
+               return expr;\r
+       }\r
+       \r
+       public abstract Resource getExpressionType(ReadGraph graph);\r
+\r
+       @Override\r
+       public Resource getResource() {\r
+               return expr;\r
+       }\r
+}\r
diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IExpression.java
deleted file mode 100644 (file)
index 234a105..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-package org.simantics.sysdyn.modelImport.model.expression;\r
-\r
-import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
-\r
-public interface IExpression extends IWriteableObject {\r
-\r
-}\r
index 6c3ccfa882c8bb355a4cecf7a3a8f523ccc23794..3a1441173b360c04a96c4b3a441a7025b36812f0 100644 (file)
@@ -1,17 +1,14 @@
 package org.simantics.sysdyn.modelImport.model.expression;\r
 \r
-import java.util.Arrays;\r
-\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
-public class IntegralExpression implements IExpression {\r
+public class IntegralExpression extends Expression {\r
        \r
        private String integral;\r
        private String initial;\r
@@ -22,16 +19,19 @@ public class IntegralExpression implements IExpression {
        }\r
        \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource expression = GraphUtils.create2(graph, sr.StockExpression,\r
-                               sr.StockExpression_integralEquation, integral,\r
-                               sr.StockExpression_initialEquation, initial,\r
-                               l0.PartOf, parent);\r
-               // TODO: why is the expression stored in two places?\r
-               graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
+               Resource expr = super.write(graph, parent, context);\r
+               \r
+               graph.claimLiteral(expr, sr.StockExpression_integralEquation, integral, Bindings.STRING);\r
+               graph.claimLiteral(expr, sr.StockExpression_initialEquation, initial, Bindings.STRING);\r
+               \r
+               return expr;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getExpressionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).StockExpression;\r
        }\r
-\r
 }\r
index ccbfdb00ca7d2660087a8d48000758b9783504b6..c94b715552149083ce7ddf895789c70e6d5fddae 100644 (file)
@@ -1,19 +1,22 @@
 package org.simantics.sysdyn.modelImport.model.expression;\r
 \r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
-public class LookupExpression implements IExpression {\r
+public class LookupExpression extends Expression {\r
        \r
        private double xMin;\r
        private double yMin;\r
        private double xMax;\r
        private double yMax;\r
-       private String points;\r
+       private double[] points;\r
        \r
-       public LookupExpression(double xMin, double yMin, double xMax, double yMax, String points) {\r
+       public LookupExpression(double xMin, double yMin, double xMax, double yMax, double[] points) {\r
                this.xMin = xMin;\r
                this.yMin = yMin;\r
                this.xMax = xMax;\r
@@ -22,8 +25,34 @@ public class LookupExpression implements IExpression {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
-               // TODO: implementation\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               StringBuilder lookup = new StringBuilder();\r
+               lookup.append('{');\r
+               for (int i = 0; i < points.length / 2; i++) {\r
+                       if (i > 0)\r
+                               lookup.append(',');\r
+                       lookup.append('{').append(points[2*i]).append(',').append(points[2*i+1]).append('}');\r
+               }\r
+               lookup.append('}');\r
+               \r
+               Resource expr = super.write(graph, parent, context);\r
+               \r
+               // TODO: fix this\r
+               graph.claimLiteral(expr, sr.WithLookupExpression_expression, "", Bindings.STRING);\r
+               graph.claimLiteral(expr, sr.WithLookupExpression_lookup, lookup.toString(), Bindings.STRING);\r
+               graph.claimLiteral(expr, sr.WithLookupExpression_minX, xMin, Bindings.DOUBLE);\r
+               graph.claimLiteral(expr, sr.WithLookupExpression_minY, yMin, Bindings.DOUBLE);\r
+               graph.claimLiteral(expr, sr.WithLookupExpression_maxX, xMax, Bindings.DOUBLE);\r
+               graph.claimLiteral(expr, sr.WithLookupExpression_maxY, yMax, Bindings.DOUBLE);\r
+               \r
+               return expr;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getExpressionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).WithLookupExpression;\r
        }\r
 \r
 }\r
index 296fe5c55890bad07ef5b6429d33f77e037f0d82..a940a643456e94adc49479ca57b255fe4bc72bc2 100644 (file)
@@ -1,33 +1,34 @@
 package org.simantics.sysdyn.modelImport.model.expression;\r
 \r
-import java.util.Arrays;\r
-\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 \r
-public class NormalExpression implements IExpression {\r
+public class NormalExpression extends Expression {\r
 \r
-       private String equation;\r
+       private String expression;\r
        \r
-       public NormalExpression(String equation) {\r
-               this.equation = equation;\r
+       public NormalExpression(String expression) {\r
+               this.expression = expression;\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
-               Resource expression = GraphUtils.create2(graph, sr.NormalExpression,\r
-                               sr.Expression_equation, equation,\r
-                               l0.PartOf, variable);\r
-               // TODO: why is the expression stored in two places?\r
-               graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
+               \r
+               Resource expr = super.write(graph, parent, context);\r
+               \r
+               graph.claimLiteral(expr, sr.Expression_equation, expression, Bindings.STRING);\r
+               \r
+               return expr;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getExpressionType(ReadGraph graph) {\r
+               return SysdynResource.getInstance(graph).NormalExpression;\r
        }\r
 }\r
diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/SubscriptExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/SubscriptExpression.java
deleted file mode 100644 (file)
index e315e32..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-package org.simantics.sysdyn.modelImport.model.expression;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
-\r
-public class SubscriptExpression implements IExpression {\r
-       \r
-       private ArrayList<SubscriptExpressionElement> elements;\r
-       \r
-       public SubscriptExpression(String[] indices, IExpression expression) {\r
-               elements = new ArrayList<SubscriptExpressionElement>();\r
-               elements.add(new SubscriptExpressionElement(indices, expression));\r
-       }\r
-       \r
-       public List<SubscriptExpressionElement> getElements() {\r
-               return elements;\r
-       }\r
-       \r
-       public void merge(SubscriptExpression other) {\r
-               elements.addAll(other.getElements());\r
-       }\r
-       \r
-       @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context)\r
-                       throws DatabaseException {\r
-               System.err.println("write subscript expression");\r
-       }\r
-       \r
-       private class SubscriptExpressionElement {\r
-               \r
-               String[] indices;\r
-               IExpression expression;\r
-               \r
-               public SubscriptExpressionElement(String[] indices, IExpression expression) {\r
-                       this.indices = indices;\r
-                       this.expression = expression;\r
-               }\r
-       }\r
-\r
-}\r
index feb0f80153dbd9c37c31aa37ec9a6a94ee858d58..64d9df06950eea3d1340d24e6d22e72650008f83 100644 (file)
@@ -23,17 +23,17 @@ public class Enumeration implements IWriteableObject {
        public String getName() {\r
                return name;\r
        }\r
-       \r
-       public Resource getResource() {\r
-               return resource;\r
-       }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent, WriteContext context)\r
+       public Resource write(WriteGraph graph, Resource parent, WriteContext context)\r
                        throws DatabaseException {\r
                System.err.println("write enumeration");\r
-               \r
-               resource = null;\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getResource() {\r
+               return resource;\r
        }\r
 \r
 }\r
index fb51d0a056c3856314d1f2402e094776cf99c518..03ecc6d544c7f30966e94cd6df3d97c8408fb5c1 100644 (file)
@@ -13,6 +13,8 @@ public class Range implements IWriteableObject {
        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
                this.start = start;\r
                this.end = end;\r
@@ -37,8 +39,13 @@ public class Range implements IWriteableObject {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
-               // TODO: implementation\r
+       public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public Resource getResource() {\r
+               return resource;\r
        }\r
 \r
 }\r
index 63969c01efa701a605a9ce051b7985dccf4d4e5a..aa08fb8057b2d7f0c80e9e814eaa5b718cd1fd05 100644 (file)
@@ -1,17 +1,17 @@
 package org.simantics.sysdyn.modelImport.model.support;\r
 \r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
-import org.simantics.sysdyn.modelImport.model.expression.SubscriptExpression;\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 IExpression expression;\r
+       private Expression expression;\r
        private String unit;\r
        private Range range;\r
        private String description;\r
        \r
-       public Variable(String name, IExpression expression, String unit, Range range, String description) {\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
@@ -23,7 +23,7 @@ public class Variable {
                return name;\r
        }\r
        \r
-       public IExpression getExpression() {\r
+       public Expression getExpression() {\r
                return expression;\r
        }\r
        \r
@@ -39,9 +39,22 @@ public class Variable {
                return description;\r
        }\r
        \r
-       public void mergeExpression(Variable variable) {\r
-               if (expression instanceof SubscriptExpression) {\r
-                       ((SubscriptExpression)expression).merge((SubscriptExpression)variable.getExpression());\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