]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Update vensim import, partial equation handling present but subscript support missing
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 31 Mar 2014 07:45:08 +0000 (07:45 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 31 Mar 2014 07:45:08 +0000 (07:45 +0000)
refs #2924

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

32 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java
org.simantics.sysdyn.ui/sysdyn.product
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/DelayExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableObject.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/LookupExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelElementFactory.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Range.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Sketch2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchComment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchConnection.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchElement.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchObject.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchValve.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java [new file with mode: 0644]

index fe212805a8eabfb099008808d605874b45617e58..7bd39e6fb8245d1fbed2699dca21d6646a3bf7b9 100644 (file)
@@ -29,6 +29,7 @@ import org.simantics.sysdyn.modelImport.MdlParser;
 import org.simantics.sysdyn.modelImport.MdlParser2;\r
 import org.simantics.sysdyn.modelImport.model.Model;\r
 import org.simantics.sysdyn.modelImport.model.Model2;\r
+import org.simantics.sysdyn.modelImport.model.WriteContext;\r
 import org.simantics.sysdyn.ui.Activator;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
@@ -74,7 +75,7 @@ public class ImportMdlHandler extends AbstractHandler {
                        \r
                        @Override\r
                        public void perform(WriteGraph graph) throws DatabaseException {\r
-                               model.write(graph, project);\r
+                               model.write(graph, project, new WriteContext());\r
                        }\r
                });\r
                \r
index 5c3c71bbffe3439f62702319fccf3a95504d6826..01a866ef68db93342239d9034aa1782ace40d741 100644 (file)
@@ -13,9 +13,8 @@
    </configIni>\r
 \r
    <launcherArgs>\r
-      <programArgs>-fixerrors
---launcher.XXMaxPermSize 192m
--data @noDefault</programArgs>\r
+      <programArgs>-fixerrors\r
+--launcher.XXMaxPermSize 192m</programArgs>\r
       <vmArgs>-ea  -Xmx768M -XX:MaxPermSize=192m -Xshare:off -Dorg.simantics.undo.enabled=false</vmArgs>\r
       <vmArgsMac>-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts</vmArgsMac>\r
       <vmArgsWin>-Dorg.osgi.framework.os.name=win32</vmArgsWin>\r
@@ -32,7 +31,6 @@
       </win>\r
    </launcher>\r
 \r
-\r
    <vm>\r
       <windows include="true">org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6</windows>\r
    </vm>\r
index f5e8b642345a4bc6e13635be802cb7301b26bb01..aa667ca1da8b6c364193a671cd5610652965e37a 100644 (file)
@@ -26,6 +26,7 @@ import org.simantics.sysdyn.modelImport.model.Comment2;
 import org.simantics.sysdyn.modelImport.model.Connection2;\r
 import org.simantics.sysdyn.modelImport.model.Element2;\r
 import org.simantics.sysdyn.modelImport.model.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.MdlModel;\r
 import org.simantics.sysdyn.modelImport.model.Model2;\r
 import org.simantics.sysdyn.modelImport.model.Shadow2;\r
 import org.simantics.sysdyn.modelImport.model.Sketch2;\r
@@ -51,229 +52,220 @@ public class MdlParser2 {
 \r
        private static final String UTF_8 = "{UTF-8}";\r
        private static final String SKETCH_VERSION = "V300";\r
-       \r
+\r
        private static final String CATEGORY_CONTROL = "Control";\r
-       \r
+\r
        // each .mdl is divided into three sections, these are the the delimiter\r
        // strings used to identify where each section starts\r
        private static final String SKETCH_START = "\\\\\\---///";\r
        private static final String SKETCH_END = "///---\\\\\\";\r
-       \r
-       private static final double H_SPACE = 10;\r
-       private static final double V_SPACE = 0;\r
-\r
-       private HashMap<String, Variable2> variables;\r
-       private HashMap<String, Variable2> controls;\r
-       private ArrayList<Sketch2> sketches;\r
-\r
-       public MdlParser2() {\r
-               variables = new HashMap<String, Variable2>();\r
-               controls = new HashMap<String, Variable2>();\r
-               sketches = new ArrayList<Sketch2>();\r
-       }\r
 \r
-       public Model2 parse(File file) {\r
-               try {\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
-\r
-                       if (line == null) {\r
-                               // file is empty, nothing to do here\r
-                               reader.close();\r
-                               return null;\r
-                       }\r
+       private static final double H_SPACE = 0;\r
+       private static final double V_SPACE = 10;\r
 \r
-                       if (line.startsWith(UTF_8)) {\r
-                               reader.close();\r
-                               reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));\r
-                               // skip the first line\r
-                               reader.readLine();\r
-                               line = reader.readLine();\r
-                       }\r
-                       \r
-                       // read variable data\r
-                       line = readVariables(reader, line);\r
-                       \r
-                       if (line == null) {\r
-                               // unexpected end of file\r
-                               System.err.println("unexpected end of file");\r
-                               reader.close();\r
-                               return null;\r
-                       }\r
-                       \r
-                       // read sketch data\r
-                       line = readSketches(reader, line);\r
-                       \r
-                       if (line == null) {\r
-                               // unexpected end of file\r
-                               System.err.println("unexpected end of file");\r
-                               reader.close();\r
-                               return null;\r
-                       }\r
-                       \r
-                       // read other data\r
-                       \r
-                       do {\r
-                               // ignore other data for now\r
-                       } while ((line = reader.readLine()) != null);\r
-\r
-                       reader.close();\r
+       public static Model2 parse(File file) {\r
+               // generate a mdl model based on the contents of the file\r
+               MdlModel mdl;\r
+               try {\r
+                       mdl = parseFile(file);\r
                }\r
                catch (IOException e) {\r
                        e.printStackTrace();\r
                        return null;\r
                }\r
-               \r
-               // create new model\r
 \r
+               // generate a sysdyn model from the mdl model\r
                Model2 model = new Model2(file.getName());\r
-               \r
-               double hOffset = 0;\r
-               double vOffset = V_SPACE;\r
-               \r
+\r
+               double offset = 0;\r
+\r
                // do this in several passes\r
-               \r
-               HashMap<Variable2, Element2> allVariables = new HashMap<Variable2, Element2>();\r
+\r
+               HashMap<Variable2, Element2> variableToElement = new HashMap<Variable2, Element2>();\r
+               ArrayList<SketchVariable> shadows = new ArrayList<SketchVariable>();\r
                \r
                // add sketch labels and independent elements\r
-               for (int i = 0; i < sketches.size(); i++) {\r
-                       Sketch2 sketch = sketches.get(i);\r
-                       \r
-                       sketch.hOffset = hOffset;\r
-                       sketch.vOffset = vOffset;\r
-                       \r
-                       model.addElement(new Comment2(sketch.getWidth() / 2 + sketch.hOffset, 0, sketch.getName()));\r
-                       \r
+               for (Sketch2 sketch : mdl.getSketches()) {\r
+                       sketch.setEdges();\r
+\r
+                       sketch.hOffset = 0;\r
+                       sketch.vOffset = 0 - sketch.topEdge + 10 + offset;\r
+\r
+                       model.addElement(new Comment2(0, offset, -1, -1, sketch.getName()));\r
+\r
                        for (SketchComment comment : sketch.getComments()) {\r
                                if (comment.isInputOutput()) {\r
                                        // input / output objects are not supported yet\r
                                        System.err.println("input / output objects are not supported yet");\r
                                        continue;\r
                                }\r
-                               \r
+\r
                                Element2 modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
                                model.addElement(modelElement);\r
                                sketch.elements.put(comment.getId(), modelElement);\r
                        }\r
-                       \r
+\r
                        for (SketchValve valve : sketch.getValves()) {\r
                                Element2 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
-                               allVariables.put(valve.getAttachedVariable().getVariable(), modelElement);\r
+                               variableToElement.put(valve.getAttachedVariable().getVariable(), modelElement);\r
                        }\r
-                       \r
+\r
                        for (SketchVariable variable : sketch.getVariables()) {\r
                                if (!variable.allowsIn()) {\r
                                        // the variable is a shadow variable, skip these for now\r
                                        continue;\r
                                }\r
-                               \r
+\r
                                if (variable.isAttached()) {\r
                                        // the variable is attached to a valve, already handled\r
                                        continue;\r
                                }\r
-                               \r
+\r
                                Element2 modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
                                model.addElement(modelElement);\r
                                sketch.elements.put(variable.getId(), modelElement);\r
-                               allVariables.put(variable.getVariable(), modelElement);\r
+                               variableToElement.put(variable.getVariable(), modelElement);\r
                        }\r
-                       \r
-                       hOffset += sketch.getWidth() + H_SPACE;\r
+\r
+                       offset += (sketch.bottomEdge - sketch.topEdge + V_SPACE);\r
                }\r
-               \r
+\r
                // add dependent elements\r
-               for (int i = 0; i < sketches.size(); i++) {\r
-                       Sketch2 sketch = sketches.get(i);\r
-                       \r
+               for (Sketch2 sketch : mdl.getSketches()) {\r
                        for (SketchVariable variable : sketch.getVariables()) {\r
                                if (!variable.allowsIn()) {\r
                                        // the variable is a shadow variable\r
-                                       Element2 original = allVariables.get(variable.getVariable());\r
+                                       Element2 original = variableToElement.get(variable.getVariable());\r
                                        if (original == null) {\r
                                                System.err.println("original not found");\r
                                                continue;\r
                                        }\r
-                                       Shadow2 modelElement = new Shadow2(variable.getX(sketch.hOffset), variable.getY(sketch.vOffset), allVariables.get(variable.getVariable()));\r
+                                       Shadow2 modelElement = new Shadow2(\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
+\r
                        for (SketchConnection connection : sketch.getConnections()) {\r
                                Element2 head = sketch.elements.get(connection.getTo());\r
                                Element2 tail = sketch.elements.get(connection.getFrom());\r
-                               Connection2 c = connection.getWriteableConnection(head, tail);\r
+                               Connection2 c = connection.getWriteableConnection(head, tail, sketch.vOffset);\r
                                if (c != null) {\r
                                        model.addConnection(c);\r
                                }\r
                        }\r
                }\r
-               \r
+\r
                return model;\r
        }\r
-       \r
-       private String readVariables(BufferedReader reader, String line) \r
+\r
+       private static MdlModel parseFile(File file) \r
                        throws IOException {\r
+               MdlModel mdl = new MdlModel(file.getName());\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
+\r
+               if (line == null) {\r
+                       // file is empty, nothing to do here\r
+                       reader.close();\r
+                       return null;\r
+               }\r
+\r
+               if (line.startsWith(UTF_8)) {\r
+                       reader.close();\r
+                       reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));\r
+                       // skip the first line\r
+                       reader.readLine();\r
+                       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
+\r
+               reader.close();\r
+               return mdl;\r
+       }\r
+\r
+       private static String parseVariables(BufferedReader reader, String line, MdlModel mdl) \r
+                       throws IOException {\r
                String category = null;\r
-               \r
+\r
                do {\r
-                       if (line.isEmpty()) {\r
+                       if (line.isEmpty())\r
                                continue;\r
-                       }\r
-                       \r
-                       StringBuilder buffer = new StringBuilder(line);\r
-                       \r
+\r
+                       StringBuilder buffer = new StringBuilder();\r
                        do {\r
                                if (line.endsWith("\\"))\r
                                        buffer.append(line.substring(0, line.length()-1));\r
                                else\r
                                        buffer.append(line);\r
-                               \r
+\r
                                if (line.endsWith("|"))\r
                                        break;\r
                        } while ((line = reader.readLine()) != null);\r
-                       \r
                        String str = buffer.toString();\r
 \r
-                       // handle category declarations\r
                        String cat = MdlUtils.getPossibleCategory(str);\r
                        if (cat != null) {\r
                                category = cat;\r
                                continue;\r
                        }\r
-                       \r
+\r
                        Variable2 var = MdlUtils.getPossibleVariable(str, category);\r
                        if (var != null) {\r
-                               if (CATEGORY_CONTROL.equals(category)) {\r
-                                       controls.put(var.getName(), var);\r
-                               }\r
-                               else {\r
-                                       variables.put(var.getName(), var);\r
-                               }\r
+                               mdl.addVariable(var, category);\r
                                continue;\r
                        }\r
-                       \r
+\r
+                       // if we got this far, the variable could not be parsed\r
                        System.err.println("unrecognized variable "+str);\r
-                       \r
                } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START));\r
-               \r
+\r
                return line;\r
        }\r
-       \r
-       private String readSketches(BufferedReader reader, String line\r
+\r
+       private static String parseSketches(BufferedReader reader, String line, MdlModel mdl\r
                        throws IOException {\r
-               \r
                Sketch2 sketch = null;\r
-               \r
+\r
                do {\r
+                       if (line.isEmpty())\r
+                               continue;\r
+                       \r
                        if (line.startsWith(SKETCH_START)) {\r
                                sketch = new Sketch2();\r
-                               sketches.add(sketch);\r
+                               mdl.addSketch(sketch);\r
                                continue;\r
                        }\r
                        else if (line.startsWith(SKETCH_VERSION)) {\r
@@ -288,25 +280,24 @@ public class MdlParser2 {
                                // font declaration, nothing to do here\r
                                continue;\r
                        }\r
-                       \r
+\r
                        SketchConnection connection = MdlUtils.getPossibleSketchConnection(line);\r
                        if (connection != null) {\r
                                sketch.addConnection(connection);\r
                                continue;\r
                        }\r
-                       \r
-                       SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, variables);\r
+\r
+                       SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, mdl);\r
                        if (variable != null) {\r
                                sketch.addVariable(variable);\r
                                continue;\r
                        }\r
-                       \r
+\r
                        SketchValve valve = MdlUtils.getPossibleSketchValve(line);\r
                        if (valve != null) {\r
                                // the next row after a valve should always the variable associated with the valve\r
-                               SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), variables);\r
+                               SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), mdl);\r
                                if (attached == null || !attached.isAttached()) {\r
-                                       // TODO: should also verify that the variable is in fact attached\r
                                        System.err.println("attached variable not found for valve");\r
                                        continue;\r
                                }\r
@@ -315,7 +306,7 @@ public class MdlParser2 {
                                sketch.addVariable(attached);\r
                                continue;\r
                        }\r
-                       \r
+\r
                        SketchComment comment = MdlUtils.getPossibleSketchComment(line);\r
                        if (comment != null) {\r
                                if (comment.hasTextLine()) {\r
@@ -324,12 +315,11 @@ public class MdlParser2 {
                                sketch.addComment(comment);\r
                                continue;\r
                        }\r
-                       \r
-                       // if we got this far, the element was not recognized\r
+\r
+                       // if we got this far, the element could not be parsed\r
                        System.err.println("unrecognized element "+line);\r
-                       \r
                } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END));\r
-               \r
+\r
                return line;\r
        }\r
 \r
index 7a814889dc4995bd2fc8a9a0e1cf94b8900cbb3a..eda44c42866e6d7796fd7944d88ddb5bdf114ff1 100644 (file)
@@ -1,11 +1,12 @@
 package org.simantics.sysdyn.modelImport;\r
 \r
-import java.util.HashMap;\r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
+import org.simantics.sysdyn.modelImport.model.DelayExpression;\r
 import org.simantics.sysdyn.modelImport.model.IExpression;\r
 import org.simantics.sysdyn.modelImport.model.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.MdlModel;\r
 import org.simantics.sysdyn.modelImport.model.NormalExpression;\r
 import org.simantics.sysdyn.modelImport.model.Range;\r
 import org.simantics.sysdyn.modelImport.model.SketchComment;\r
@@ -79,6 +80,9 @@ public class MdlUtils {
        private static final int variableUnit = 3;\r
        private static final int variableDesc = 4;\r
        \r
+       private static final String NUMBER = \r
+                       "-?\\d+(?:.\\d+)?";\r
+       \r
        public static Variable2 getPossibleVariable(String line, String category) {\r
                Matcher matcher = Pattern.compile(variablePattern).matcher(line);\r
                \r
@@ -86,7 +90,7 @@ public class MdlUtils {
                        return null;\r
                }\r
                \r
-               String name = matcher.group(variableName);\r
+               String name = normalize(matcher.group(variableName));\r
                IExpression expression = parseEquation(matcher.group(variableEquation));\r
                \r
                String unit = matcher.group(variableUnit);\r
@@ -100,23 +104,48 @@ public class MdlUtils {
        }\r
        \r
        private static IExpression parseEquation(String equation) {\r
-               Matcher matcher = Pattern.compile("INTEG\\s*\\((.*),(.*)\\)").matcher(equation);\r
-               if (matcher.matches()) {\r
-                       return new IntegralExpression(matcher.group(1), matcher.group(2));\r
+               if (equation.startsWith("INTEG")) {\r
+                       return parseIntegralExpression(equation);\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
                }\r
                else {\r
-                       return new NormalExpression("");\r
+                       return parseNormalExpression(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
+               }\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
+       }\r
+       \r
        private static Range parseRange(String unit) {\r
-               Matcher matcher = Pattern.compile("\\[(-?\\d+|\\?),(-?\\d+|\\?)\\]").matcher(unit);\r
+               Matcher matcher = Pattern.compile("\\[("+NUMBER+"),("+NUMBER+"|\\?)\\]").matcher(unit);\r
                if (matcher.find()) {\r
                        String start = matcher.group(1);\r
                        String end = matcher.group(2);\r
                        // TODO: and step is?\r
                        return new Range(\r
-                                       start.equals("?") ? null : Double.parseDouble(start), \r
+                                       Double.parseDouble(start), \r
                                        end.equals("?") ? null : Double.parseDouble(end), \r
                                        null, \r
                                        matcher.group());\r
@@ -189,11 +218,27 @@ public class MdlUtils {
                int from = Integer.parseInt(matcher.group(connectionFrom));\r
                int to = Integer.parseInt(matcher.group(connectionTo));\r
                ConnectionType type = getConnectionType(matcher);\r
+               int[] points = parsePoints(matcher.group(connectionPoints));\r
                \r
-               return new SketchConnection(id, from, to, type);\r
+               return new SketchConnection(id, from, to, type, points);\r
        }\r
        \r
-       public static SketchVariable getPossibleSketchVariable(String line, HashMap<String, Variable2> variables) {\r
+       private static int[] parsePoints(String points) {\r
+               // points should be a well formed points string\r
+               int sep = points.indexOf('|');\r
+               int count = Integer.parseInt(points.substring(0, sep));\r
+               int[] array = new int[count*2];\r
+               for (int i = 0; i < count; i++) {\r
+                       int start = sep + 1;\r
+                       sep = points.indexOf('|', start);\r
+                       String[] pair = points.substring(start+1, sep-1).split(",");\r
+                       array[i*2] = Integer.parseInt(pair[0]);\r
+                       array[i*2+1] = Integer.parseInt(pair[1]);\r
+               }\r
+               return array;\r
+       }\r
+       \r
+       public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) {\r
                Matcher matcher = Pattern.compile(sketchVariable).matcher(line);\r
                \r
                if (!matcher.matches()) {\r
@@ -201,7 +246,7 @@ public class MdlUtils {
                }\r
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
-               Variable2 var = variables.get(matcher.group(elementName));\r
+               Variable2 var = mdl.getVariable((normalize(matcher.group(elementName))));\r
                \r
                SketchVariable variable = new SketchVariable(id, var);\r
                \r
@@ -218,8 +263,9 @@ public class MdlUtils {
                }\r
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
+               TextPosition pos = getTextPos(matcher);\r
                \r
-               SketchValve valve = new SketchValve(id);\r
+               SketchValve valve = new SketchValve(id, pos);\r
                \r
                initializeElement(valve, matcher);\r
                \r
@@ -312,9 +358,75 @@ public class MdlUtils {
                str = str.replaceAll("\t", "");\r
 \r
                // remove inline :AND: and :OR: etc (not done currently)\r
+               str = removeInlineOperation(str, "AND");\r
+               str = removeInlineOperation(str, "OR");\r
+               \r
+               // rename vensim method calls to sysdyn equivalents\r
+               str = str.replaceAll("IF THEN ELSE", "IFTHENELSE");\r
+               \r
+               // TODO: replace array slice operations\r
 \r
-               // transform all variable names to (single-)quoted modelica syntax\r
+               // normalize variable names\r
+               str = processVariables(str);\r
+\r
+               return str;\r
+       }\r
+       \r
+       private static String removeInlineOperation(String str, String op) {\r
+               while (str.contains(":"+op+":")) {\r
+                       // this should work in most cases, even with nested parentheses \r
+                       // in the left and right hand side expression\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
+                       // 2. left hand side expression, anything except ',' unless it is \r
+                       //    enclosed in parentheses which is allowed:\r
+                       //    ([^(,]*(?:\\(.*\\)[^(,])*)\r
+                       // 3. the inlined operation:\r
+                       //    :OPERATION:\r
+                       // 4. right hand side expression, similar to left hand side expression\r
+                       //    but with ')' instead of '(':\r
+                       //    ([^),]*(?:\\(.*\\)[^),])*)\r
+                       // 5. delimiter character on the right (either end of string, ',' or ')'):\r
+                       //    (?:$|[),])\r
+                       \r
+                       // note that the expression also makes heavy use of both non-capturing and\r
+                       // capturing groups ("(?:)" and "()")\r
+                       Matcher matcher = Pattern.compile(\r
+                                       "(?:^|[(,])([^(,]*(?:\\(.*\\)[^(,])*):" + op + ":([^),]*(?:\\(.*\\)[^),])*)(?:$|[),])").matcher(str);\r
+                       \r
+                       if (!matcher.find()) {\r
+                               System.err.println("inlined operation not found");\r
+                               return str;\r
+                       }\r
+                       \r
+                       StringBuilder buf = new StringBuilder();\r
+                       \r
+                       // part before left hand side\r
+                       buf.append(str.substring(0, matcher.start(1)));\r
+                       // operation open\r
+                       buf.append(op);\r
+                       buf.append('(');\r
+                       // left hand side\r
+                       buf.append(matcher.group(1).trim());\r
+                       // delimiter\r
+                       buf.append(',');\r
+                       // right hand side\r
+                       buf.append(matcher.group(2).trim());\r
+                       // operation close\r
+                       buf.append(')');\r
+                       // part after right hand side\r
+                       buf.append(str.substring(matcher.end(2)));\r
+                       \r
+                       str = buf.toString();\r
+               }\r
+               return str;\r
+       }\r
+       \r
+       private static String processVariables(String str) {\r
                StringBuilder result = new StringBuilder();\r
+               \r
                int offset = 0;\r
 \r
                Matcher matcher = Pattern.compile(VAR_NAME).matcher(str);\r
@@ -322,24 +434,37 @@ public class MdlUtils {
                        result.append(str.substring(offset, matcher.start()));\r
 \r
                        String replacement = matcher.group();\r
-\r
+                       \r
                        if (replacement.startsWith("\"")) {\r
-                               replacement = replacement.substring(1, replacement.length() - 1);\r
+                               // if the variable name is quoted, change the quotes to modelica syntax\r
+                               replacement = "'" + replacement.substring(1, replacement.length() - 1) + "'";\r
+                       }\r
+                       else if (Pattern.compile("[-+*/]").matcher(replacement).find()) {\r
+                               // if the variable name contains "bad" characters, quote it\r
+                               replacement = "'" + replacement + "'";\r
+                       }\r
+                       else {\r
+                               // otherwise simply capitalize the variable name properly\r
+                               String[] parts = replacement.split("\\s+");\r
+                               StringBuilder buf = new StringBuilder();\r
+                               for (String part : parts) {\r
+                                       part = part.substring(0, 1).toUpperCase() + part.substring(1);\r
+                                       if (buf.length() > 0)\r
+                                               buf.append(' ');\r
+                                       buf.append(part);\r
+                               }\r
+                               replacement = buf.toString();\r
                        }\r
 \r
-                       result.append('\'');\r
                        result.append(replacement);\r
-                       result.append('\'');\r
 \r
                        offset = matcher.end();\r
                }\r
                if (offset < str.length()) {\r
                        result.append(str.substring(offset));\r
                }\r
-\r
-               str = result.toString();\r
-\r
-               return str;\r
+               \r
+               return result.toString();\r
        }\r
        \r
 }\r
index 16facadad951043adfff4d9647e6c0266cbdff31..cbca5c98f3aa5f9e580e868a4e6503bfcc67643e 100644 (file)
@@ -7,21 +7,21 @@ import org.simantics.sysdyn.SysdynResource;
 \r
 public class Auxiliary2 extends ModelVariable {\r
        \r
-       public Auxiliary2(double x, double y, \r
+       public Auxiliary2(double x, double y, double w, double h,\r
                        String name, IExpression expression, Range range, String unit, String description) {\r
-               super(x, y, name, expression, range, unit, description);\r
+               super(x, y, w, h, name, expression, range, unit, description);\r
        }\r
        \r
-       public Auxiliary2(double x, double y, Variable2 variable) {\r
-               super(x, y, variable);\r
+       public Auxiliary2(double x, double y, double w, double h, Variable2 variable) {\r
+               super(x, y, w, h, variable);\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\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);\r
-               createSymbol(graph, sr.AuxiliarySymbol, variable, parent);\r
+               Resource variable = createVariable(graph, sr.Auxiliary, parent, context);\r
+               createSymbol(graph, sr.AuxiliarySymbol, variable, parent, context);\r
                \r
                setResource(variable);\r
        }\r
index 68301a27d95ed76723e388b66a4a0d1c15bb26df..14d39b631a6873b0e9b5aac92e5fa939c33192e6 100644 (file)
@@ -9,19 +9,20 @@ import org.simantics.sysdyn.SysdynResource;
 \r
 public class Cloud2 extends Element2 {\r
        \r
-       public Cloud2(double x, double y) {\r
-               super(x, y);\r
+       public Cloud2(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) throws DatabaseException {\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource cloud = GraphUtils.create2(graph, sr.Cloud, \r
+               Resource cloud = GraphUtils.create2(graph, sr.Cloud,\r
+                               l0.HasName, context.getNextCloud(),\r
                                l0.PartOf, parent);\r
                \r
-               createSymbol(graph, sr.CloudSymbol, cloud, parent);\r
+               createSymbol(graph, sr.CloudSymbol, cloud, parent, context);\r
                \r
                setResource(cloud);\r
        }\r
index fc5264ded7548ccaea6ec054b598b2eaaa73a4e4..985fc73a87ab2409593672d2aa85dd7edfea932e 100644 (file)
@@ -13,17 +13,17 @@ public class Comment2 extends Element2 {
        \r
        private String text;\r
        \r
-       public Comment2(double x, double y, String text) {\r
-               super(x, y);\r
+       public Comment2(double x, double y, double w, double h, String text) {\r
+               super(x, y, w, h);\r
                this.text = text;\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+       public void 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);\r
+               Resource comment = createSymbol(graph, sr.AdditionalSymbols_MultilineText, null, parent, context);\r
                \r
                graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING);\r
                \r
index 455f0be6963ee326f87c2dbefdc245b8b3e6c8a2..c034f6383151da9cfb7f232a429d397976efdf49 100644 (file)
@@ -21,14 +21,8 @@ public abstract class Connection2 implements IWriteableObject {
                this.tail = tail;\r
        }\r
 \r
-       protected Resource createConnection(WriteGraph graph, Resource type, Resource symbol, Resource parent) \r
+       protected Resource createConnection(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
                        throws DatabaseException {\r
-               // TODO: remove this\r
-               if (head == null || tail == null) {\r
-                       return null;\r
-               }\r
-               \r
-               // create connection\r
                Layer0 l0 = Layer0.getInstance(graph); \r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
@@ -38,10 +32,19 @@ public abstract class Connection2 implements IWriteableObject {
                                sr.Variable_HasTail, tail.getResource(),\r
                                l0.PartOf, parent);\r
                graph.claim(connection, mr.Mapped, connection);\r
-\r
-               // create symbol\r
+               \r
+               return connection;\r
+       }\r
+       \r
+       protected Resource createSymbol(WriteGraph graph, Resource type, Resource connection, Resource parent, WriteContext context) \r
+                       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
+               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
@@ -52,16 +55,17 @@ public abstract class Connection2 implements IWriteableObject {
                                sr.HasHeadTerminal, diagramHead,\r
                                dr.AreConnected, tailConnector);\r
 \r
-               Resource diagramConnection = GraphUtils.create2(graph, symbol,\r
+               Resource diagramConnection = GraphUtils.create2(graph, type,\r
+                               l0.HasName, context.getNextObject(),\r
+                               l0.PartOf, diagram,\r
                                sr2.HasConnectionType, sr.SysdynConnectionType,\r
                                mr.DiagramConnectionToConnection, connection,\r
                                dr.HasArrowConnector, headConnector,\r
                                dr.HasPlainConnector, tailConnector);\r
 \r
-               Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
                OrderedSetUtils.add(graph, diagram, diagramConnection);\r
-\r
-               return connection;\r
+               \r
+               return diagramConnection;\r
        }\r
 \r
 }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/DelayExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/DelayExpression.java
new file mode 100644 (file)
index 0000000..ccb552b
--- /dev/null
@@ -0,0 +1,28 @@
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+import java.util.Arrays;\r
+\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
+\r
+public class DelayExpression implements IExpression {\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\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
+       }\r
+\r
+}\r
index d4fa8e405bd1862bc24d0b0733715225bf35c956..973d1ebf3ff7ae4d0b67ddd0dbdd425429e62ba6 100644 (file)
@@ -1,27 +1,41 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
+import org.simantics.databoard.Bindings;\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.sysdyn.SysdynResource;\r
 \r
 public class Dependency2 extends Connection2 {\r
        \r
        private boolean showArrow;\r
        private boolean showDelay;\r
+       private double angle;\r
        \r
-       public Dependency2(Element2 head, Element2 tail, boolean showArrow, boolean showDelay) {\r
+       public Dependency2(Element2 head, Element2 tail, boolean showArrow, boolean showDelay, double angle) {\r
                super(head, tail);\r
                this.showArrow = showArrow;\r
                this.showDelay = showDelay;\r
+               this.angle = angle;\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent)\r
-                       throws DatabaseException {\r
+       public void 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
+               }\r
+               \r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               \r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource dependency = createConnection(graph, sr.Dependency, sr.DependencyConnection, parent);\r
+               Resource dependency = createConnection(graph, sr.Dependency, 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
        }\r
 \r
 }\r
index 43914ccc05a7192bd7a6c0b44a668e1fa82e793c..005ca18c15d8e65b495b9d6107cb5fe872c63395 100644 (file)
@@ -7,21 +7,31 @@ import org.simantics.db.common.utils.OrderedSetUtils;
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.diagram.stubs.DiagramResource;\r
 import org.simantics.diagram.stubs.G2DResource;\r
+import org.simantics.layer0.Layer0;\r
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.modeling.ModelingResources;\r
 \r
 public abstract class Element2 implements IWriteableObject {\r
        \r
-       private static final double SCALE = 1.0 / 3.0;\r
-\r
        private double x;\r
        private double y;\r
+       private double width;\r
+       private double height;\r
 \r
        private Resource resource;\r
 \r
-       public Element2(double x, double y) {\r
-               this.x = x * SCALE;\r
-               this.y = y * SCALE;\r
+       public Element2(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 Element2(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
@@ -31,13 +41,34 @@ public abstract class Element2 implements IWriteableObject {
        public void setResource(Resource resource) {\r
                this.resource = resource;\r
        }\r
+       \r
+       public double getX() {\r
+               return x;\r
+       }\r
+       \r
+       public double getY() {\r
+               return y;\r
+       }\r
+       \r
+       public double getWidth() {\r
+               return width;\r
+       }\r
+       \r
+       public double getHeight() {\r
+               return height;\r
+       }\r
 \r
-       public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, Resource parent) throws DatabaseException {\r
+       public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, 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
                ModelingResources mr = ModelingResources.getInstance(graph);\r
 \r
-               Resource symbol = GraphUtils.create2(graph, type);\r
+               Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
+               \r
+               Resource symbol = GraphUtils.create2(graph, type,\r
+                               l0.HasName, context.getNextObject(),\r
+                               l0.PartOf, diagram);\r
                \r
                if (variable != null) {\r
                        graph.claim(symbol, mr.ElementToComponent, variable);\r
@@ -46,7 +77,6 @@ public abstract class Element2 implements IWriteableObject {
                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
-               Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
                OrderedSetUtils.add(graph, diagram, symbol);\r
 \r
                return symbol;\r
index d631439ff45c752343f375b39e0c1be974ca7aa3..fc3b5979584bda0b169307d67c652396501ac3a2 100644 (file)
@@ -1,8 +1,10 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
+import org.simantics.databoard.Bindings;\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.sysdyn.SysdynResource;\r
 \r
 public class Flow2 extends Connection2 {\r
@@ -12,10 +14,19 @@ public class Flow2 extends Connection2 {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+       public void 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
+               }\r
+               \r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               \r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource flow = createConnection(graph, sr.Flow, sr.FlowConnection, parent);\r
+               Resource flow = createConnection(graph, sr.Flow, parent, context);\r
+               graph.claimLiteral(flow, l0.HasName, context.getNextFlow(), Bindings.STRING);\r
+               Resource symbol = createSymbol(graph, sr.FlowConnection, flow, parent, context);\r
        }\r
 \r
 }\r
index 9929c80c7b3ca1e136fdb50b28bad418729516f6..c704b7c48ee9a6ec64a8e6146222452830b24505 100644 (file)
@@ -22,5 +22,5 @@ public interface IWriteableObject {
         * @param parent The resource where the object is located\r
         * @throws DatabaseException \r
         */\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException;\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException;\r
 }\r
index 40d096b40e3432eac3edea8f6afe2686b60869e2..783b181dc61bbf6b706617f517c509e186ebd9da 100644 (file)
@@ -21,7 +21,7 @@ public class IntegralExpression implements IExpression {
        }\r
        \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/LookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/LookupExpression.java
new file mode 100644 (file)
index 0000000..1ee5831
--- /dev/null
@@ -0,0 +1,14 @@
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+public class LookupExpression implements IExpression {\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+               // TODO: implementation\r
+       }\r
+\r
+}\r
index c1dbceb42e136fd4d299a4567729b8b3fe7022c8..1a5519e89970da56f409b92b5f5e7f6c9e3d26b5 100644 (file)
@@ -1,5 +1,50 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
-public class MdlModel {\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
 \r
+public class MdlModel {\r
+       \r
+       private String name;\r
+       \r
+       private HashMap<String, Variable2> variables;\r
+       private HashMap<String, ArrayList<Variable2>> groups;\r
+       \r
+       private ArrayList<Sketch2> sketches;\r
+       \r
+       public MdlModel(String name) {\r
+               this.name = name;\r
+               this.variables = new HashMap<String, Variable2>();\r
+               this.groups = new HashMap<String, ArrayList<Variable2>>();\r
+               this.sketches = new ArrayList<Sketch2>();\r
+       }\r
+       \r
+       public void addVariable(Variable2 variable, String group) {\r
+               if (variables.get(variable.getName()) != null) {\r
+                       System.err.println("warning, duplicate variable "+variable.getName());\r
+               }\r
+               \r
+               variables.put(variable.getName(), variable);\r
+               \r
+               if (group != null) {\r
+                       if (groups.get(group) == null) {\r
+                               groups.put(group, new ArrayList<Variable2>());\r
+                       }\r
+                       groups.get(group).add(variable);\r
+               }\r
+       }\r
+       \r
+       public Variable2 getVariable(String name) {\r
+               return variables.get(name);\r
+       }\r
+       \r
+       public void addSketch(Sketch2 sketch) {\r
+               sketches.add(sketch);\r
+       }\r
+       \r
+       public List<Sketch2> getSketches() {\r
+               return sketches;\r
+       }\r
+       \r
 }\r
index c6bce0cba4fcb8f12e7157e9e03c6c2b01259fd9..4189cd1911c044eaa42f97e42b3e0ad6a37b9c6e 100644 (file)
@@ -17,7 +17,9 @@ import org.simantics.databoard.Bindings;
 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.modeling.ModelingResources;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.sysdyn.utils.ModelUtils;\r
 \r
@@ -77,8 +79,10 @@ public class Model2 implements IWriteableObject {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {         \r
+       public void 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
@@ -89,16 +93,19 @@ public class Model2 implements IWriteableObject {
                Resource configuration = graph.getSingleObject(model, sim.HasConfiguration);\r
                \r
                for (Element2 e : elements) {\r
-                       e.write(graph, configuration);\r
+                       e.write(graph, configuration, context);\r
                }\r
                \r
                for (Shadow2 s : shadows) {\r
-                       s.write(graph, configuration);\r
+                       s.write(graph, configuration, context);\r
                }\r
                \r
                for (Connection2 c : connections) {\r
-                       c.write(graph, configuration);\r
+                       c.write(graph, configuration, context);\r
                }\r
+               \r
+               Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
+               graph.claimLiteral(diagram, dr.HasModCount, context.getObjectCount(), Bindings.LONG);\r
        }\r
        \r
 }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelElementFactory.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelElementFactory.java
new file mode 100644 (file)
index 0000000..01b3a12
--- /dev/null
@@ -0,0 +1,68 @@
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon;\r
+\r
+public class ModelElementFactory {\r
+       \r
+       public static void getConnection() {\r
+               \r
+       }\r
+       \r
+       public static void getDependency() {\r
+               \r
+       }\r
+       \r
+       public static void getFlow() {\r
+               \r
+       }\r
+       \r
+       public static Element2 getElement(SketchVariable variable) {\r
+               if (variable.getVariable().getExpression() instanceof IntegralExpression) {\r
+                       return getStock(variable);\r
+               }\r
+               else {\r
+                       return getAuxiliary(variable);\r
+               }\r
+       }\r
+       \r
+       public static Element2 getElement(SketchValve valve) {\r
+               return getValve(valve);\r
+       }\r
+       \r
+       public static Element2 getElement(SketchComment comment) {\r
+               if (comment.getIcon().equals(CommentIcon.CLOUD)) {\r
+                       return getCloud(comment);\r
+               }\r
+               else {\r
+                       return getComment(comment);\r
+               }\r
+       }\r
+       \r
+       public static Auxiliary2 getAuxiliary(SketchVariable variable) {\r
+               return null;\r
+       }\r
+       \r
+       public static Stock2 getStock(SketchVariable variable) {\r
+               assert variable.getVariable().getExpression() instanceof IntegralExpression;\r
+               \r
+               return null;\r
+       }\r
+       \r
+       public static Valve2 getValve(SketchValve valve) {\r
+               return null;\r
+       }\r
+       \r
+       public static Cloud2 getCloud(SketchComment comment) {\r
+               assert comment.getIcon().equals(CommentIcon.CLOUD);\r
+               return null;\r
+       }\r
+       \r
+       public static Comment2 getComment(SketchComment comment) {\r
+               return null;\r
+       }\r
+       \r
+       public static Shadow2 getShadow(SketchVariable variable) {\r
+               return null;\r
+       }\r
+\r
+}\r
index 81b66a147f4d77be82d208c81d79108d3e0aab0a..1a5c496d5da5cd334fba2c63bd73a5940e576909 100644 (file)
@@ -17,8 +17,8 @@ public abstract class ModelVariable extends Element2 {
        protected String unit;\r
        protected String description;\r
 \r
-       public ModelVariable(double x, double y, String name, IExpression expression, Range range, String unit, String description) {\r
-               super(x, y);\r
+       public ModelVariable(double x, double y, double w, double h, String name, IExpression expression, Range range, String unit, String description) {\r
+               super(x, y, w, h);\r
                this.name = name;\r
                this.expression = expression;\r
                this.range = range;\r
@@ -26,8 +26,8 @@ public abstract class ModelVariable extends Element2 {
                this.description = description;\r
        }\r
        \r
-       public ModelVariable(double x, double y, Variable2 variable) {\r
-               super(x, y);\r
+       public ModelVariable(double x, double y, double w, double h, Variable2 variable) {\r
+               super(x, y, w, h);\r
                this.name = variable.getName();\r
                this.expression = variable.getExpression();\r
                this.range = variable.getRange();\r
@@ -35,7 +35,7 @@ public abstract class ModelVariable extends Element2 {
                this.description = variable.getDescription();\r
        }\r
 \r
-       public Resource createVariable(WriteGraph graph, Resource type, Resource parent) \r
+       public Resource createVariable(WriteGraph graph, Resource type, Resource parent, WriteContext context\r
                        throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
@@ -46,10 +46,10 @@ public abstract class ModelVariable extends Element2 {
                                l0.PartOf, parent);\r
                graph.claim(variable, mr.Mapped, variable);\r
                \r
-               expression.write(graph, variable);\r
+               expression.write(graph, variable, context);\r
                \r
                if (range != null) {\r
-                       range.write(graph, variable);\r
+                       range.write(graph, variable, context);\r
                }\r
                if (unit != null && !unit.isEmpty()) {\r
                        graph.claimLiteral(variable, sr.Variable_unit, unit, Bindings.STRING);\r
index e7b0fb526b9ab50c90b2796b7bc5d68d2c9be132..f84bcc0564a88085cbc6f6723ac75f1c71e0e6ea 100644 (file)
@@ -19,7 +19,7 @@ public class NormalExpression implements IExpression {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource variable) throws DatabaseException {\r
+       public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
 \r
index 638ef7ba82cd664feb56640a0308a6c34d3c9d2f..9a7ca8a04d4323c9dff3fe10e03bfa81367b8ffe 100644 (file)
@@ -35,7 +35,7 @@ public class Range implements IWriteableObject {
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource variable) throws DatabaseException {\r
+       public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
                // TODO: implementation\r
        }\r
 \r
index 4b51e424b45ee0fc31c6a63100d9a5d347f0638e..f386fbccf38eb5a8237cdfd3ebad3d9b56f95696 100644 (file)
@@ -11,22 +11,28 @@ public class Shadow2 extends Element2 {
 \r
        private Element2 original;\r
        \r
-       public Shadow2(double x, double y, Element2 original) {\r
-               super(x, y);\r
+       public Shadow2(double x, double y, double w, double h, Element2 original) {\r
+               super(x, y, w, h);\r
+               this.original = original;\r
+       }\r
+       \r
+       public Shadow2(SketchVariable variable, double hOffset, double vOffset, Element2 original) {\r
+               super(variable, hOffset, vOffset);\r
                this.original = original;\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                \r
-               Resource shadow = GraphUtils.create2(graph, sr.Shadow, \r
+               Resource 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);\r
+               createSymbol(graph, sr.ShadowSymbol, shadow, parent, context);\r
                \r
                setResource(shadow);\r
        }\r
index 5512c8d66480668425d1d6ef5fcbbfb62e7b3def..b2f86ae8f71c3ba4431368362a4203f4aaa0ed72 100644 (file)
@@ -68,28 +68,36 @@ public class Sketch2 {
                return list;\r
        }\r
        \r
-       public int getWidth() {\r
-               int width = 0;\r
-               for (SketchElement e : getAllElements()) {\r
-                       width = Math.max(width, e.getX()+e.getWidth());\r
-               }\r
-               return width;\r
-       }\r
-       \r
-       public int getHeight() {\r
-               int height = 0;\r
-               for (SketchElement e : getAllElements()) {\r
-                       height = Math.max(height, e.getY()+e.getHeight());\r
-               }\r
-               return height;\r
-       }\r
-       \r
        // relevant for sysdyn model creation, this is not the cleanest place to\r
        // store this information but it works\r
        \r
        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, Element2> elements = new HashMap<Integer, Element2>();\r
+       \r
+       public void setEdges() {\r
+               boolean first = true;\r
+               for (SketchElement e : getAllElements()) {\r
+                       if (first) {\r
+                               topEdge = e.getSysdynTopEdge();\r
+                               bottomEdge = e.getSysdynBottomEdge();\r
+                               leftEdge = e.getSysdynLeftEdge();\r
+                               rightEdge = e.getSysdynRightEdge();\r
+                               first = false;\r
+                       }\r
+                       else {\r
+                               topEdge = Math.min(topEdge, e.getSysdynTopEdge());\r
+                               bottomEdge = Math.max(bottomEdge, e.getSysdynBottomEdge());\r
+                               leftEdge = Math.min(leftEdge, e.getSysdynLeftEdge());\r
+                               rightEdge = Math.max(rightEdge, e.getSysdynRightEdge());\r
+                       }\r
+               }\r
+       }\r
 \r
 }\r
index f077379f56b6084f0c9cb344410a853096692077..d053a5c0b1dde6c924386bfea9a54361b14d3b87 100644 (file)
@@ -24,12 +24,21 @@ public class SketchComment extends SketchElement {
                return icon;\r
        }\r
        \r
-       public Element2 getModelElement(double xOffset, double yOffset) {\r
+       public Element2 getModelElement(double hOffset, double vOffset) {\r
                if (icon.equals(CommentIcon.CLOUD)) {\r
-                       return new Cloud2(getX(xOffset), getY(yOffset));\r
+                       return new Cloud2(\r
+                                       getSysdyndX() + hOffset, \r
+                                       getSysdyndY() + vOffset, \r
+                                       getSysdynWidth(),\r
+                                       getSysdynHeight());\r
                }\r
                else {\r
-                       return new Comment2(getX(xOffset), getY(yOffset), text);\r
+                       return new Comment2(\r
+                                       getSysdyndX() + hOffset, \r
+                                       getSysdyndY() + vOffset, \r
+                                       getSysdynWidth(),\r
+                                       getSysdynHeight(),\r
+                                       text);\r
                }\r
        }\r
        \r
index 6207d692dff65395bd1ae335ac2a7b860a56e0d3..5968eb8e6fb5a5492ce302a36153be0b3b0eb3c2 100644 (file)
@@ -7,29 +7,31 @@ public class SketchConnection extends SketchObject {
        private int from;\r
        private int to;\r
        private ConnectionType type;\r
-       \r
-       public SketchConnection(int id, int from, int to, ConnectionType type) {\r
+       int[] points;\r
+\r
+       public SketchConnection(int id, int from, int to, ConnectionType type, int[] points) {\r
                super(id);\r
                this.from = from;\r
                this.to = to;\r
                this.type = type;\r
+               this.points = points;\r
        }\r
-       \r
+\r
        public int getFrom() {\r
                return from;\r
        }\r
-       \r
+\r
        public int getTo() {\r
                return to;\r
        }\r
-       \r
+\r
        public ConnectionType getType() {\r
                return type;\r
        }\r
-       \r
-       public Connection2 getWriteableConnection(Element2 head, Element2 tail) {\r
+\r
+       public Connection2 getWriteableConnection(Element2 head, Element2 tail, double offset) {\r
                if (type.equals(ConnectionType.ARROW)) {\r
-                       return new Dependency2(head, tail, false, false);\r
+                       return new Dependency2(head, tail, false, false, getSysdynAngle(tail, head, offset));\r
                }\r
                else if (type.equals(ConnectionType.LINE_ARROW)) {\r
                        return new Flow2(head, tail);\r
@@ -40,5 +42,47 @@ public class SketchConnection extends SketchObject {
                }\r
                return null;\r
        }\r
-       \r
+\r
+       // TODO: comment this?\r
+       public double getSysdynAngle(Element2 from, Element2 to, double voffset) {\r
+               if (points == null || points.length == 0) {\r
+                       return 0;\r
+               }\r
+               \r
+               // 'from' element is in (x0, y0) and 'to' element is in (x2, y2)\r
+               double x0 = from.getX() + (from.getWidth() / 2);\r
+               double y0 = from.getY() + (from.getHeight() / 2);\r
+               double x2 = to.getX() + (to.getWidth() / 2);\r
+               double y2 = to.getY() + (to.getHeight() / 2);\r
+               \r
+               // treat the first points in points as the control point (x1, y1)\r
+               double x1 = (double)points[0] * SCALE_MULTIPLIER;\r
+               double y1 = (double)points[1] * SCALE_MULTIPLIER + voffset;\r
+               \r
+               //System.err.println("("+x0+","+y0+") -> ("+x1+","+y1+") -> ("+x2+","+y2+")");\r
+\r
+               double dx0 = x1 - x0;\r
+               double dy0 = y1 - y0;\r
+               double dx1 = x1 - x2;\r
+               double dy1 = y1 - y2;\r
+               double dx  = x2 - x0;\r
+               double dy  = y2 - y0;\r
+\r
+               // length of (p1-p0) x (p2-p0)\r
+               double dd = dx0*dy - dy0*dx; \r
+\r
+               if (Math.abs(dd) < 1e-5) {\r
+                       // Points are (almost) collinear\r
+                       return 0;\r
+               }\r
+               else {\r
+                       // (p1-p0) * (p1-p2) / dd\r
+                       double offset = (dx0*dx1 + dy0*dy1) / dd;\r
+                       double angle = Math.PI*0.5 - Math.atan(offset);\r
+                       if (dd > 0.0)\r
+                               angle = angle - Math.PI;\r
+                       return angle;\r
+               }\r
+       }\r
+\r
 }\r
index ef54a0174fd1b9920b9339fcb3d6e612fef0ecb9..34ce854d98f0e5b9b0f6fe39f613160ce2bf5f21 100644 (file)
@@ -34,18 +34,10 @@ public abstract class SketchElement extends SketchObject {
                return x;\r
        }\r
        \r
-       public double getX(double offset) {\r
-               return x + offset;\r
-       }\r
-\r
        public int getY() {\r
                return y;\r
        }\r
        \r
-       public double getY(double offset) {\r
-               return y + offset;\r
-       }\r
-\r
        public int getWidth() {\r
                return width;\r
        }\r
@@ -54,6 +46,43 @@ public abstract class SketchElement extends SketchObject {
                return height;\r
        }\r
        \r
+       // in vensim the coordinates refer to the center of the element with \r
+       // width and height being the distances between the center and the edges,\r
+       // whereas in sysdyn the coordinates refer to the top-left corner of the\r
+       // element\r
+       \r
+       public double getSysdyndX() {\r
+               return getSysdynLeftEdge();\r
+       }\r
+       \r
+       public double getSysdyndY() {\r
+               return getSysdynTopEdge();\r
+       }\r
+       \r
+       public double getSysdynWidth() {\r
+               return ((double)width) * 2 * SCALE_MULTIPLIER;\r
+       }\r
+       \r
+       public double getSysdynHeight() {\r
+               return ((double)height) * 2 * SCALE_MULTIPLIER;\r
+       }\r
+       \r
+       public double getSysdynTopEdge() {\r
+               return (double)(y - height) * SCALE_MULTIPLIER;\r
+       }\r
+       \r
+       public double getSysdynBottomEdge() {\r
+               return (double)(y + height) * SCALE_MULTIPLIER;\r
+       }\r
+\r
+       public double getSysdynLeftEdge() {\r
+               return (double)(x - width) * SCALE_MULTIPLIER;\r
+       }\r
+\r
+       public double getSysdynRightEdge() {\r
+               return (double)(x + width) * SCALE_MULTIPLIER;\r
+       }\r
+       \r
        public boolean isAttached() {\r
                return attached;\r
        }\r
@@ -74,6 +103,6 @@ public abstract class SketchElement extends SketchObject {
                return textLine;\r
        }\r
 \r
-       public abstract Element2 getModelElement(double xOffset, double yOffset);\r
+       public abstract Element2 getModelElement(double hOffset, double vOffset);\r
        \r
 }\r
index 89b1933c2c9b3100e007081aa35c2ecbe4c9cfda..003741c7881e16b75b5ae0c109dcc902da204e42 100644 (file)
@@ -1,10 +1,12 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
 public abstract class SketchObject {\r
+       \r
+       protected static final double SCALE_MULTIPLIER = 0.4;\r
 \r
        private int id;\r
        \r
-       SketchObject(int id) {\r
+       public SketchObject(int id) {\r
                this.id = id;\r
        }\r
        \r
index 6b8161f4173b5fabe721166842bc0e423be3009a..670e23cc6b5af6ede62b580528af0ab4108378ea 100644 (file)
@@ -1,11 +1,15 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
+import org.simantics.sysdyn.modelImport.model.Valve2.TextPosition;\r
+\r
 public class SketchValve extends SketchElement {\r
        \r
        private SketchVariable variable;\r
+       private TextPosition textPosition;\r
        \r
-       public SketchValve(int id) {\r
+       public SketchValve(int id, TextPosition textPosition) {\r
                super(id);\r
+               this.textPosition = textPosition;\r
        }\r
 \r
        public SketchVariable getAttachedVariable() {\r
@@ -17,8 +21,14 @@ public class SketchValve extends SketchElement {
        }\r
        \r
        @Override\r
-       public Element2 getModelElement(double xOffset, double yOffset) {\r
-               return new Valve2(getX(xOffset), getY(yOffset), variable.getVariable());\r
+       public Element2 getModelElement(double hOffset, double vOffset) {\r
+               return new Valve2(\r
+                               getSysdyndX() + hOffset, \r
+                               getSysdyndY() + vOffset, \r
+                               getSysdynWidth(),\r
+                               getSysdynHeight(), \r
+                               variable.getVariable(),\r
+                               textPosition);\r
        }\r
 \r
 }\r
index 1d86295f67463d4a47bf8d28a3bbff02d0933c1d..6a0fabb790d9a81db9bbe758ffe2e46e4f5367be 100644 (file)
@@ -14,12 +14,22 @@ public class SketchVariable extends SketchElement {
        }\r
        \r
        @Override\r
-       public Element2 getModelElement(double xOffset, double yOffset) {\r
+       public Element2 getModelElement(double hOffset, double vOffset) {\r
                if (variable.getExpression() instanceof IntegralExpression) {\r
-                       return new Stock2(getX(xOffset), getY(yOffset), variable);\r
+                       return new Stock2(\r
+                                       getSysdyndX() + hOffset, \r
+                                       getSysdyndY() + vOffset, \r
+                                       getSysdynWidth(),\r
+                                       getSysdynHeight(),\r
+                                       variable);\r
                }\r
                else {\r
-                       return new Auxiliary2(getX(xOffset), getY(yOffset), variable);\r
+                       return new Auxiliary2(\r
+                                       getSysdyndX() + hOffset, \r
+                                       getSysdyndY() + vOffset,  \r
+                                       getSysdynWidth(),\r
+                                       getSysdynHeight(),\r
+                                       variable);\r
                }\r
        }\r
        \r
index 41d424c41f9f4f40d7f467a0df5158bc56d7307b..412cf35d7f7ba9069be3eaee7735e90d6e348898 100644 (file)
@@ -7,20 +7,20 @@ import org.simantics.sysdyn.SysdynResource;
 \r
 public class Stock2 extends ModelVariable {\r
        \r
-       public Stock2(double x, double y, \r
+       public Stock2(double x, double y, double w, double h,\r
                        String name, IntegralExpression expression, Range range, String unit, String description) {\r
-               super(x, y, name, expression, range, unit, description);\r
+               super(x, y, w, h, name, expression, range, unit, description);\r
        }\r
        \r
-       public Stock2(double x, double y, Variable2 variable) {\r
-               super(x, y, variable);\r
+       public Stock2(double x, double y, double w, double h, Variable2 variable) {\r
+               super(x, y, w, h, variable);\r
        }\r
        \r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\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);\r
-               createSymbol(graph, sr.StockSymbol, stock, parent);\r
+               Resource stock = createVariable(graph, sr.Stock, parent, context);\r
+               createSymbol(graph, sr.StockSymbol, stock, parent, context);\r
 \r
                setResource(stock);\r
        }\r
index 5ea5a99b2e975267a28fa934c03eaddb9649abe5..0abd36daa14b2141bf398c502cf13b8e9d8bd622 100644 (file)
@@ -15,21 +15,47 @@ public class Valve2 extends ModelVariable {
                INSIDE, BELOW, LEFT, ABOVE, RIGHT, UNSET\r
        }\r
        \r
-       public Valve2(double x, double y, \r
-                       String name, IExpression expression, Range range, String unit, String description) {\r
-               super(x, y, name, expression, range, unit, description);\r
+       private TextPosition position;\r
+       \r
+       public Valve2(double x, double y, double w, double h,\r
+                       String name, IExpression 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
        \r
-       public Valve2(double x, double y, Variable2 variable) {\r
-               super(x, y, variable);\r
+       public Valve2(double x, double y, double w, double h, Variable2 variable, TextPosition position) {\r
+               super(x, y, w, h, variable);\r
+               this.position = position;\r
        }\r
 \r
        @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+       public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
 \r
-               Resource valve = createVariable(graph, sr.Valve, parent);\r
-               createSymbol(graph, sr.ValveSymbol, valve, parent);\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
+                       location = sr.Bottom;\r
+                       break;\r
+               case LEFT:\r
+                       location = sr.Left;\r
+                       break;\r
+               case ABOVE:\r
+                       location = sr.Top;\r
+                       break;\r
+               case RIGHT:\r
+                       location = sr.Right;\r
+                       break;\r
+               default:\r
+                       System.err.println("unrecognized text location");\r
+                       location = sr.Bottom;\r
+                       break;\r
+               }\r
+               \r
+               graph.claim(symbol, sr.ValveSymbol_textLocation, location);\r
 \r
                setResource(valve);\r
        }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java
new file mode 100644 (file)
index 0000000..80f3ba1
--- /dev/null
@@ -0,0 +1,40 @@
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+public class WriteContext {\r
+       \r
+       private long objectCount;\r
+       \r
+       private long cloudCount;\r
+       private long shadowCount;\r
+       private long dependencyCount;\r
+       private long flowCount;\r
+       \r
+       public WriteContext() {\r
+               this.objectCount = 0;\r
+       }\r
+       \r
+       public String getNextObject() {\r
+               return Long.toString(objectCount++);\r
+       }\r
+       \r
+       public long getObjectCount() {\r
+               return objectCount;\r
+       }\r
+       \r
+       public String getNextCloud() {\r
+               return "Cloud" + cloudCount++;\r
+       }\r
+       \r
+       public String getNextShadow() {\r
+               return "Shadow" + shadowCount++;\r
+       }\r
+       \r
+       public String getNextDependency() {\r
+               return "Dependency" + dependencyCount++;\r
+       }\r
+       \r
+       public String getNextFlow() {\r
+               return "Flow" + flowCount++;\r
+       }\r
+\r
+}\r