]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Update vensim import, sketches are imported successfully but equation handling is...
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 13 Mar 2014 15:40:31 +0000 (15:40 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 13 Mar 2014 15:40:31 +0000 (15:40 +0000)
refs #2924

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

29 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java
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/Cloud.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/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/IExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model2.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Range.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java [new file with mode: 0644]
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/Variable2.java

index b9855c8fb408d9f1dc01fbfa371b2ba341f40435..fe212805a8eabfb099008808d605874b45617e58 100644 (file)
@@ -28,6 +28,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.sysdyn.modelImport.MdlParser;\r
 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.ui.Activator;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
@@ -67,13 +68,13 @@ public class ImportMdlHandler extends AbstractHandler {
                \r
                // Convert Vensim model to Simantics SysDyn format using MdlParser\r
                MdlParser2 parser = new MdlParser2();\r
-               parser.parse(file);\r
+               final Model2 model = parser.parse(file);\r
                \r
                SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
                        \r
                        @Override\r
                        public void perform(WriteGraph graph) throws DatabaseException {\r
-                               //model.write(graph, project);\r
+                               model.write(graph, project);\r
                        }\r
                });\r
                \r
index a4c3f805f44c22570ce11ae37add662f16f1473f..f5e8b642345a4bc6e13635be802cb7301b26bb01 100644 (file)
@@ -19,16 +19,23 @@ 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.model.Auxiliary2;\r
+import org.simantics.sysdyn.modelImport.model.Cloud2;\r
+import org.simantics.sysdyn.modelImport.model.Comment2;\r
 import org.simantics.sysdyn.modelImport.model.Connection2;\r
 import org.simantics.sysdyn.modelImport.model.Element2;\r
-import org.simantics.sysdyn.modelImport.model.Model;\r
+import org.simantics.sysdyn.modelImport.model.IntegralExpression;\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
 import org.simantics.sysdyn.modelImport.model.SketchComment;\r
 import org.simantics.sysdyn.modelImport.model.SketchConnection;\r
 import org.simantics.sysdyn.modelImport.model.SketchElement;\r
 import org.simantics.sysdyn.modelImport.model.SketchValve;\r
 import org.simantics.sysdyn.modelImport.model.SketchVariable;\r
+import org.simantics.sysdyn.modelImport.model.Stock2;\r
+import org.simantics.sysdyn.modelImport.model.Valve2;\r
 import org.simantics.sysdyn.modelImport.model.Variable2;\r
 \r
 /*\r
@@ -43,13 +50,17 @@ import org.simantics.sysdyn.modelImport.model.Variable2;
 public class MdlParser2 {\r
 \r
        private static final String UTF_8 = "{UTF-8}";\r
-       private static final String CONTROL_STR = ".Control";\r
        private static final String SKETCH_VERSION = "V300";\r
        \r
+       private static final String CATEGORY_CONTROL = "Control";\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
@@ -61,7 +72,7 @@ public class MdlParser2 {
                sketches = new ArrayList<Sketch2>();\r
        }\r
 \r
-       public Model parse(File file) {\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
@@ -118,29 +129,91 @@ public class MdlParser2 {
 \r
                Model2 model = new Model2(file.getName());\r
                \r
-               int offset = 0;\r
+               double hOffset = 0;\r
+               double vOffset = V_SPACE;\r
+               \r
+               // do this in several passes\r
                \r
-               for (Sketch2 sketch : sketches) {\r
-                       // must keep track of which elements in the sketch correspond to \r
-                       // which elements in the model so connections can be constructed \r
-                       // accurately\r
-                       HashMap<SketchElement, Element2> elementMap = new HashMap<SketchElement, Element2>();\r
+               HashMap<Variable2, Element2> allVariables = new HashMap<Variable2, Element2>();\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
-                       for (SketchElement element : sketch.getElements()) {\r
-                               Element2 e = element.getWriteableElement(0, 0);\r
-                               //model.addElement(e);\r
-                               //elementMap.put(element, e);\r
+                       model.addElement(new Comment2(sketch.getWidth() / 2 + sketch.hOffset, 0, 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
+                               Element2 modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
+                               model.addElement(modelElement);\r
+                               sketch.elements.put(comment.getId(), modelElement);\r
                        }\r
                        \r
-                       for (SketchConnection connection : sketch.getConnections()) {\r
-                               Connection2 c = connection.getWriteableConnection();\r
-                               //model.addConnection(c);\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
+                       }\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
+                               if (variable.isAttached()) {\r
+                                       // the variable is attached to a valve, already handled\r
+                                       continue;\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
+                       }\r
+                       \r
+                       hOffset += sketch.getWidth() + H_SPACE;\r
+               }\r
+               \r
+               // add dependent elements\r
+               for (int i = 0; i < sketches.size(); i++) {\r
+                       Sketch2 sketch = sketches.get(i);\r
+                       \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
+                                       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
+                                       model.addShadow(modelElement);\r
+                                       sketch.elements.put(variable.getId(), modelElement);\r
+                               }\r
                        }\r
                        \r
-                       offset += sketch.getWidth() + 100;\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
+                               if (c != null) {\r
+                                       model.addConnection(c);\r
+                               }\r
+                       }\r
                }\r
                \r
-               return null;\r
+               return model;\r
        }\r
        \r
        private String readVariables(BufferedReader reader, String line) \r
@@ -155,7 +228,7 @@ public class MdlParser2 {
                        \r
                        StringBuilder buffer = new StringBuilder(line);\r
                        \r
-                       while ((line = reader.readLine()) != null) {\r
+                       do {\r
                                if (line.endsWith("\\"))\r
                                        buffer.append(line.substring(0, line.length()-1));\r
                                else\r
@@ -163,15 +236,25 @@ public class MdlParser2 {
                                \r
                                if (line.endsWith("|"))\r
                                        break;\r
-                       }\r
+                       } while ((line = reader.readLine()) != null);\r
                        \r
                        String str = buffer.toString();\r
-                       \r
-                       // TODO: must handle categories other than .Control\r
+\r
+                       // handle category declarations\r
+                       String cat = MdlUtils.getPossibleCategory(str);\r
+                       if (cat != null) {\r
+                               category = cat;\r
+                               continue;\r
+                       }\r
                        \r
                        Variable2 var = MdlUtils.getPossibleVariable(str, category);\r
                        if (var != null) {\r
-                               variables.put(var.getName(), var);\r
+                               if (CATEGORY_CONTROL.equals(category)) {\r
+                                       controls.put(var.getName(), var);\r
+                               }\r
+                               else {\r
+                                       variables.put(var.getName(), var);\r
+                               }\r
                                continue;\r
                        }\r
                        \r
@@ -208,13 +291,13 @@ public class MdlParser2 {
                        \r
                        SketchConnection connection = MdlUtils.getPossibleSketchConnection(line);\r
                        if (connection != null) {\r
-                               sketch.addSketchObject(connection);\r
+                               sketch.addConnection(connection);\r
                                continue;\r
                        }\r
                        \r
                        SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, variables);\r
                        if (variable != null) {\r
-                               sketch.addSketchObject(variable);\r
+                               sketch.addVariable(variable);\r
                                continue;\r
                        }\r
                        \r
@@ -228,23 +311,17 @@ public class MdlParser2 {
                                        continue;\r
                                }\r
                                valve.setAttachedVariable(attached);\r
-                               sketch.addSketchObject(valve);\r
-                               sketch.addSketchObject(attached);\r
+                               sketch.addValve(valve);\r
+                               sketch.addVariable(attached);\r
                                continue;\r
                        }\r
                        \r
                        SketchComment comment = MdlUtils.getPossibleSketchComment(line);\r
                        if (comment != null) {\r
-                               if (comment.hasTextNextLine()) {\r
+                               if (comment.hasTextLine()) {\r
                                        comment.setText(reader.readLine());\r
                                }\r
-                               \r
-                               if (comment.isIOElement()) {\r
-                                       System.err.println("IO elements are not currently supported");\r
-                                       continue;\r
-                               }\r
-                               \r
-                               sketch.addSketchObject(comment);\r
+                               sketch.addComment(comment);\r
                                continue;\r
                        }\r
                        \r
index 252260a483f856f4939d6bb04c5816794ae21040..7a814889dc4995bd2fc8a9a0e1cf94b8900cbb3a 100644 (file)
@@ -4,6 +4,10 @@ import java.util.HashMap;
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
+import org.simantics.sysdyn.modelImport.model.IExpression;\r
+import org.simantics.sysdyn.modelImport.model.IntegralExpression;\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
 import org.simantics.sysdyn.modelImport.model.SketchConnection;\r
 import org.simantics.sysdyn.modelImport.model.SketchElement;\r
@@ -11,7 +15,6 @@ import org.simantics.sysdyn.modelImport.model.SketchValve;
 import org.simantics.sysdyn.modelImport.model.SketchVariable;\r
 import org.simantics.sysdyn.modelImport.model.Variable2;\r
 import org.simantics.sysdyn.modelImport.model.Valve2.TextPosition;\r
-import org.simantics.utils.datastructures.Pair;\r
 \r
 public class MdlUtils {\r
        \r
@@ -84,21 +87,55 @@ public class MdlUtils {
                }\r
                \r
                String name = matcher.group(variableName);\r
-               String equation = matcher.group(variableEquation);\r
+               IExpression expression = parseEquation(matcher.group(variableEquation));\r
+               \r
                String unit = matcher.group(variableUnit);\r
+               Range range = parseRange(unit);\r
+               if (range != null) {\r
+                       unit = unit.substring(0, unit.indexOf(range.originalString())).trim();\r
+               }\r
                String description = matcher.group(variableDesc);\r
                \r
-               return new Variable2(name, equation, unit, description);\r
+               return new Variable2(name, expression, unit, range, description);\r
        }\r
        \r
-       public static Pair<String, String> getPossibleIntegral(String equation) {\r
-               \r
+       private static IExpression parseEquation(String equation) {\r
                Matcher matcher = Pattern.compile("INTEG\\s*\\((.*),(.*)\\)").matcher(equation);\r
                if (matcher.matches()) {\r
-                       return new Pair<String, String>(matcher.group(1), matcher.group(2));\r
+                       return new IntegralExpression(matcher.group(1), matcher.group(2));\r
+               }\r
+               else {\r
+                       return new NormalExpression("");\r
+               }\r
+       }\r
+       \r
+       private static Range parseRange(String unit) {\r
+               Matcher matcher = Pattern.compile("\\[(-?\\d+|\\?),(-?\\d+|\\?)\\]").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
+                                       end.equals("?") ? null : Double.parseDouble(end), \r
+                                       null, \r
+                                       matcher.group());\r
+               }\r
+               else {\r
+                       return null;\r
+               }\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
+               \r
+               if (!matcher.matches()) {\r
+                       return null;\r
                }\r
                \r
-               return null;\r
+               return matcher.group(1);\r
        }\r
        \r
        // sketch object data is defined similarly\r
@@ -165,11 +202,8 @@ public class MdlUtils {
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
                Variable2 var = variables.get(matcher.group(elementName));\r
-               boolean attached = elementIsAttached(matcher);\r
-               boolean in = elementAllowsInBound(matcher);\r
-               boolean out = elementAllowsOutBound(matcher);\r
                \r
-               SketchVariable variable = new SketchVariable(id, var, attached, in, out);\r
+               SketchVariable variable = new SketchVariable(id, var);\r
                \r
                initializeElement(variable, matcher);\r
                \r
@@ -201,10 +235,8 @@ public class MdlUtils {
                \r
                int id = Integer.parseInt(matcher.group(elementId));\r
                CommentIcon icon = getCommentIcon(matcher);\r
-               boolean nextLine = elementHasCommentLine(matcher);\r
-               boolean isIO = elementIsIO(matcher);\r
                \r
-               SketchComment comment = new SketchComment(id, icon, nextLine, isIO);\r
+               SketchComment comment = new SketchComment(id, icon);\r
                \r
                initializeElement(comment, matcher);\r
                \r
@@ -216,16 +248,22 @@ public class MdlUtils {
                int y = Integer.parseInt(matcher.group(elementY));\r
                int w = Integer.parseInt(matcher.group(elementWidth));\r
                int h = Integer.parseInt(matcher.group(elementHeight));\r
+               boolean attached = elementIsAttached(matcher);\r
+               boolean in = elementAllowsInBound(matcher);\r
+               boolean out = elementAllowsOutBound(matcher);\r
+               boolean inputOutput = elementIsInputOutput(matcher);\r
+               boolean textLine = elementhasTextLine(matcher);\r
                \r
-               element.setLocationAndSize(x, y, w, h);\r
+               element.init(x, y, w, h, attached, in, out, inputOutput, textLine);\r
        }\r
        \r
        public static ConnectionType getConnectionType(Matcher matcher) {\r
                switch(Integer.parseInt(matcher.group(connectionShape))) {\r
-               case 0: return ConnectionType.ARROW;\r
+               case 0: \r
+               case 1: return ConnectionType.ARROW;\r
                case 4: return ConnectionType.LINE_ARROW;\r
                case 100: return ConnectionType.LINE_SEGMENT;\r
-               default: return ConnectionType.OTHER;\r
+               default: System.err.println("connection type "+Integer.parseInt(matcher.group(connectionShape))); return ConnectionType.OTHER;\r
                }\r
        }\r
        \r
@@ -259,11 +297,11 @@ public class MdlUtils {
                return (Integer.parseInt(matcher.group(elementBits)) & 1<<1) != 0;\r
        }\r
        \r
-       public static boolean elementHasCommentLine(Matcher matcher) {\r
+       public static boolean elementhasTextLine(Matcher matcher) {\r
                return (Integer.parseInt(matcher.group(elementBits)) & 1<<2) != 0;\r
        }\r
        \r
-       public static boolean elementIsIO(Matcher matcher) {\r
+       public static boolean elementIsInputOutput(Matcher matcher) {\r
                return (Integer.parseInt(matcher.group(elementBits)) & 1<<3) != 0;\r
        }\r
        \r
index 17047bb93d8d944632672088fd285d12f81d03ca..16facadad951043adfff4d9647e6c0266cbdff31 100644 (file)
@@ -1,45 +1,39 @@
 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 Auxiliary2 extends ModelVariable {\r
        \r
-       protected String equation;\r
-\r
        public Auxiliary2(double x, double y, \r
-                       String name, String unit, Range range, String description, \r
-                       String equation) {\r
-               this.equation = equation;\r
+                       String name, IExpression expression, Range range, String unit, String description) {\r
+               super(x, y, name, expression, range, unit, description);\r
        }\r
        \r
-       public Auxiliary2(double x, double y, Variable2 variable, String equation) {\r
-               this.equation = equation;\r
+       public Auxiliary2(double x, double y, Variable2 variable) {\r
+               super(x, y, variable);\r
        }\r
 \r
        @Override\r
        public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
 \r
                Resource variable = createVariable(graph, sr.Auxiliary, parent);\r
-\r
-               Resource expression = GraphUtils.create2(graph, sr.NormalExpression,\r
-                               sr.Expression_equation, equation,\r
-                               l0.PartOf, variable);\r
-               // TODO: why is the expressoin stored in two places?\r
-               graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
-\r
                createSymbol(graph, sr.AuxiliarySymbol, variable, parent);\r
-\r
+               \r
                setResource(variable);\r
        }\r
 \r
+       @Override\r
+       public boolean supportsDependencies() {\r
+               return true;\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsFlows() {\r
+               return false;\r
+       }\r
+\r
 }\r
index 7653072b48ec997a24eb9f7c9d340ced918c9ffc..3567125809abc9c9658f05e355cf01eee9ba38fd 100644 (file)
@@ -49,7 +49,6 @@ public class Cloud extends Element {
                                        sr.Cloud);\r
 \r
                        graph.claim(parent, l0.ConsistsOf, cloud);\r
-\r
                        \r
 \r
                        Resource symbol = GraphUtils.create2(graph, \r
index fec8991762a1e71588c9df0f6143f142b0bd367d..68301a27d95ed76723e388b66a4a0d1c15bb26df 100644 (file)
@@ -1,9 +1,39 @@
 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
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
 public class Cloud2 extends Element2 {\r
        \r
-       public Cloud2() {\r
+       public Cloud2(double x, double y) {\r
+               super(x, y);\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               Resource cloud = GraphUtils.create2(graph, sr.Cloud, \r
+                               l0.PartOf, parent);\r
+               \r
+               createSymbol(graph, sr.CloudSymbol, cloud, parent);\r
                \r
+               setResource(cloud);\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsDependencies() {\r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsFlows() {\r
+               return true;\r
        }\r
 \r
 }\r
index 53334c7bce2872454842e1a47e23545867ce754f..fc5264ded7548ccaea6ec054b598b2eaaa73a4e4 100644 (file)
@@ -1,5 +1,43 @@
 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.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
 public class Comment2 extends Element2 {\r
+       \r
+       private String text;\r
+       \r
+       public Comment2(double x, double y, String text) {\r
+               super(x, y);\r
+               this.text = text;\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent) 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
+               \r
+               graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING);\r
+               \r
+               setResource(comment);\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsDependencies() {\r
+               return false;\r
+       }\r
 \r
+       @Override\r
+       public boolean supportsFlows() {\r
+               return false;\r
+       }\r
+       \r
 }\r
index 7d0485ce23ed62ae59a88fb5044353424a7833cd..455f0be6963ee326f87c2dbefdc245b8b3e6c8a2 100644 (file)
@@ -2,28 +2,66 @@ package org.simantics.sysdyn.modelImport.model;
 \r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\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.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
 \r
 public abstract class Connection2 implements IWriteableObject {\r
-       \r
+\r
        protected Element2 head;\r
        protected Element2 tail;\r
 \r
-       @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
-               System.err.println("Writing connection");\r
-       }\r
-       \r
-       protected void writeConnection() {\r
-               \r
-       }\r
-       \r
-       public void setHead(Element2 head) {\r
+       public Connection2(Element2 head, Element2 tail) {\r
                this.head = head;\r
-       }\r
-       \r
-       public void setTail(Element2 tail) {\r
                this.tail = tail;\r
        }\r
-       \r
+\r
+       protected Resource createConnection(WriteGraph graph, Resource type, Resource symbol, Resource parent) \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
+\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
+               // create symbol\r
+               DiagramResource dr = DiagramResource.getInstance(graph);\r
+               StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+               \r
+               Resource diagramTail = graph.getPossibleObject(tail.getResource(), mr.ComponentToElement);\r
+               Resource tailConnector = GraphUtils.create2(graph, dr.Connector,\r
+                               sr.HasTailTerminal, diagramTail);\r
+\r
+               Resource diagramHead = graph.getPossibleObject(head.getResource(), mr.ComponentToElement);\r
+               Resource headConnector = GraphUtils.create2(graph, dr.Connector,\r
+                               sr.HasHeadTerminal, diagramHead,\r
+                               dr.AreConnected, tailConnector);\r
+\r
+               Resource diagramConnection = GraphUtils.create2(graph, symbol,\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
+\r
 }\r
index d390e7e98a1525f5a6dad59f30e3e6c00dfa5e58..d4fa8e405bd1862bc24d0b0733715225bf35c956 100644 (file)
@@ -1,25 +1,27 @@
 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
+import org.simantics.sysdyn.SysdynResource;\r
+\r
 public class Dependency2 extends Connection2 {\r
        \r
        private boolean showArrow;\r
        private boolean showDelay;\r
        \r
-       public Dependency2(boolean showArrow, boolean showDelay) {\r
+       public Dependency2(Element2 head, Element2 tail, boolean showArrow, boolean showDelay) {\r
+               super(head, tail);\r
                this.showArrow = showArrow;\r
                this.showDelay = showDelay;\r
        }\r
 \r
        @Override\r
-       public void setHead(Element2 head) {\r
-               // TODO: make sure head is of the right type\r
-               super.setHead(head);\r
-       }\r
-\r
-       @Override\r
-       public void setTail(Element2 Tail) {\r
-               // TODO: make sure tail is of the right type\r
-               super.setTail(tail);\r
+       public void write(WriteGraph graph, Resource parent)\r
+                       throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               Resource dependency = createConnection(graph, sr.Dependency, sr.DependencyConnection, parent);\r
        }\r
 \r
 }\r
index 22a91f0f0c5d20c66f7bc45c640d8b89f37eb473..43914ccc05a7192bd7a6c0b44a668e1fa82e793c 100644 (file)
@@ -10,33 +10,20 @@ import org.simantics.diagram.stubs.G2DResource;
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.modeling.ModelingResources;\r
 \r
-public class Element2 implements IWriteableObject {\r
+public abstract class Element2 implements IWriteableObject {\r
+       \r
+       private static final double SCALE = 1.0 / 3.0;\r
 \r
-       private int x;\r
-       private int y;\r
+       private double x;\r
+       private double y;\r
 \r
        private Resource resource;\r
 \r
-       public Element2() {\r
-\r
+       public Element2(double x, double y) {\r
+               this.x = x * SCALE;\r
+               this.y = y * SCALE;\r
        }\r
 \r
-       //      public Element2(int x, int y, int width, int height) {\r
-       //              this.x = x;\r
-       //              this.y = y;\r
-       //              this.width = width;\r
-       //              this.height = height;\r
-       //              \r
-       //              this.resource = null;\r
-       //      }\r
-\r
-       @Override\r
-       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
-               System.err.println("Writing element");\r
-       }\r
-\r
-       // TODO: is this the right place for this?\r
-\r
        public Resource getResource() {\r
                return resource;\r
        }\r
@@ -50,8 +37,11 @@ public class Element2 implements IWriteableObject {
                G2DResource g2d = G2DResource.getInstance(graph);\r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
 \r
-               Resource symbol = GraphUtils.create2(graph, type, \r
-                               mr.ElementToComponent, variable);\r
+               Resource symbol = GraphUtils.create2(graph, type);\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
@@ -61,5 +51,8 @@ public class Element2 implements IWriteableObject {
 \r
                return symbol;\r
        }\r
+       \r
+       public abstract boolean supportsDependencies();\r
+       public abstract boolean supportsFlows();\r
 \r
 }\r
index db78ba79b403df9e8a5c50aeefe89e714496a26d..d631439ff45c752343f375b39e0c1be974ca7aa3 100644 (file)
@@ -1,21 +1,21 @@
 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
+import org.simantics.sysdyn.SysdynResource;\r
+\r
 public class Flow2 extends Connection2 {\r
        \r
-       public Flow2() {\r
-               \r
-       }\r
-\r
-       @Override\r
-       public void setHead(Element2 head) {\r
-               // TODO: make sure head is of the right type\r
-               super.setHead(head);\r
+       public Flow2(Element2 head, Element2 tail) {\r
+               super(head, tail);\r
        }\r
 \r
        @Override\r
-       public void setTail(Element2 tail) {\r
-               // TODO: make sure tail is of the right type\r
-               super.setTail(tail);\r
+       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               Resource flow = createConnection(graph, sr.Flow, sr.FlowConnection, parent);\r
        }\r
 \r
 }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IExpression.java
new file mode 100644 (file)
index 0000000..d4ab16f
--- /dev/null
@@ -0,0 +1,5 @@
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+public interface IExpression extends IWriteableObject {\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java
new file mode 100644 (file)
index 0000000..40d096b
--- /dev/null
@@ -0,0 +1,36 @@
+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 IntegralExpression implements IExpression {\r
+       \r
+       private String integral;\r
+       private String initial;\r
+       \r
+       public IntegralExpression(String integral, String initial) {\r
+               this.integral = integral;\r
+               this.initial = initial;\r
+       }\r
+       \r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\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
+       }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java
new file mode 100644 (file)
index 0000000..c1dbceb
--- /dev/null
@@ -0,0 +1,5 @@
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+public class MdlModel {\r
+\r
+}\r
index 447efad4d26e4a9a163758381ccbccb2f59f871c..c6bce0cba4fcb8f12e7157e9e03c6c2b01259fd9 100644 (file)
@@ -29,12 +29,14 @@ public class Model2 implements IWriteableObject {
        \r
        // the structure of the model\r
        private ArrayList<Element2> elements;\r
+       private ArrayList<Shadow2> shadows;\r
        private ArrayList<Connection2> connections;\r
        \r
        public Model2(String name) {\r
                this.name = name;\r
                \r
                elements = new ArrayList<Element2>();\r
+               shadows = new ArrayList<Shadow2>();\r
                connections = new ArrayList<Connection2>();\r
        }\r
 \r
@@ -66,6 +68,10 @@ public class Model2 implements IWriteableObject {
                elements.add(element);\r
        }\r
        \r
+       public void addShadow(Shadow2 shadow) {\r
+               shadows.add(shadow);\r
+       }\r
+       \r
        public void addConnection(Connection2 connection) {\r
                connections.add(connection);\r
        }\r
@@ -78,7 +84,7 @@ public class Model2 implements IWriteableObject {
                Resource model = ModelUtils.createModel(graph);\r
                graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING);\r
                \r
-               // TODO: set simulatin parameters\r
+               // TODO: set simulation parameters\r
                \r
                Resource configuration = graph.getSingleObject(model, sim.HasConfiguration);\r
                \r
@@ -86,6 +92,10 @@ public class Model2 implements IWriteableObject {
                        e.write(graph, configuration);\r
                }\r
                \r
+               for (Shadow2 s : shadows) {\r
+                       s.write(graph, configuration);\r
+               }\r
+               \r
                for (Connection2 c : connections) {\r
                        c.write(graph, configuration);\r
                }\r
index 1a1df905fbf70b9cfc30a0ffb0c5ad23e9c40367..81b66a147f4d77be82d208c81d79108d3e0aab0a 100644 (file)
@@ -9,18 +9,34 @@ import org.simantics.layer0.utils.direct.GraphUtils;
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.sysdyn.SysdynResource;\r
 \r
-public class ModelVariable extends Element2 {\r
+public abstract class ModelVariable extends Element2 {\r
 \r
        protected String name;\r
+       protected IExpression expression;\r
        protected Range range;\r
        protected String unit;\r
        protected String description;\r
 \r
-       public ModelVariable() {\r
-\r
+       public ModelVariable(double x, double y, String name, IExpression expression, Range range, String unit, String description) {\r
+               super(x, y);\r
+               this.name = name;\r
+               this.expression = expression;\r
+               this.range = range;\r
+               this.unit = unit;\r
+               this.description = description;\r
+       }\r
+       \r
+       public ModelVariable(double x, double y, Variable2 variable) {\r
+               super(x, y);\r
+               this.name = variable.getName();\r
+               this.expression = variable.getExpression();\r
+               this.range = variable.getRange();\r
+               this.unit = variable.getUnit();\r
+               this.description = variable.getDescription();\r
        }\r
 \r
-       public Resource createVariable(WriteGraph graph, Resource type, Resource parent) throws DatabaseException {\r
+       public Resource createVariable(WriteGraph graph, Resource type, Resource parent) \r
+                       throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(graph);\r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
@@ -30,17 +46,17 @@ public class ModelVariable extends Element2 {
                                l0.PartOf, parent);\r
                graph.claim(variable, mr.Mapped, variable);\r
                \r
+               expression.write(graph, variable);\r
+               \r
+               if (range != null) {\r
+                       range.write(graph, variable);\r
+               }\r
                if (unit != null && !unit.isEmpty()) {\r
                        graph.claimLiteral(variable, sr.Variable_unit, unit, Bindings.STRING);\r
                }\r
                if (description != null && !description.isEmpty()) {\r
                        graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING);\r
                }\r
-               if (range != null) {\r
-                       //graph.claimLiteral(variable, sr.HasRangeStart, rangeStart);\r
-                       //graph.claimLiteral(variable, sr.HasRangeEnd, rangeEnd);\r
-                       //graph.claimLiteral(variable, sr.HasRangeStep, rangeStep);\r
-               }\r
 \r
                return variable;\r
        }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java
new file mode 100644 (file)
index 0000000..e7b0fb5
--- /dev/null
@@ -0,0 +1,32 @@
+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 NormalExpression implements IExpression {\r
+\r
+       private String equation;\r
+       \r
+       public NormalExpression(String equation) {\r
+               this.equation = equation;\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource variable) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\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
+}\r
index 30bec9aadcf386f554528c6d319f7f3b9d46589f..638ef7ba82cd664feb56640a0308a6c34d3c9d2f 100644 (file)
@@ -1,15 +1,42 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
-public class Range {\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+public class Range implements IWriteableObject {\r
        \r
-       private double start;\r
-       private double end;\r
-       private double step;\r
+       private Double start;\r
+       private Double end;\r
+       private Double step;\r
+       private String original;\r
        \r
-       public Range(double start, double end, double step) {\r
+       public Range(Double start, Double end, Double step, String original) {\r
                this.start = start;\r
                this.end = end;\r
                this.step = step;\r
+               this.original = original;\r
+       }\r
+       \r
+       public Double getStart() {\r
+               return start;\r
+       }\r
+\r
+       public Double getEnd() {\r
+               return end;\r
+       }\r
+\r
+       public Double getStep() {\r
+               return step;\r
+       }\r
+\r
+       public String originalString() {\r
+               return original;\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource variable) throws DatabaseException {\r
+               // TODO: implementation\r
        }\r
 \r
 }\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java
new file mode 100644 (file)
index 0000000..4b51e42
--- /dev/null
@@ -0,0 +1,44 @@
+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
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
+public class Shadow2 extends Element2 {\r
+\r
+       private Element2 original;\r
+       \r
+       public Shadow2(double x, double y, Element2 original) {\r
+               super(x, y);\r
+               this.original = original;\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               \r
+               Resource shadow = GraphUtils.create2(graph, sr.Shadow, \r
+                               l0.PartOf, parent);\r
+               \r
+               graph.claim(shadow, sr.Shadow_original, original.getResource());\r
+               \r
+               createSymbol(graph, sr.ShadowSymbol, shadow, parent);\r
+               \r
+               setResource(shadow);\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsDependencies() {\r
+               return original.supportsDependencies();\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsFlows() {\r
+               return original.supportsFlows();\r
+       }\r
+\r
+}\r
index 04b88d7efcf946df037ea6278c56d2a01a3083a8..5512c8d66480668425d1d6ef5fcbbfb62e7b3def 100644 (file)
@@ -2,19 +2,22 @@ package org.simantics.sysdyn.modelImport.model;
 \r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
-import java.util.Iterator;\r
 import java.util.List;\r
 \r
 public class Sketch2 {\r
        \r
        private String name;\r
        \r
-       private HashMap<Integer, SketchObject> objectMap;\r
-       private ArrayList<SketchConnection> unfinished;\r
+       private ArrayList<SketchComment> comments;\r
+       private ArrayList<SketchConnection> connections;\r
+       private ArrayList<SketchValve> valves;\r
+       private ArrayList<SketchVariable> variables;\r
        \r
        public Sketch2() {\r
-               objectMap = new HashMap<Integer, SketchObject>();\r
-               unfinished = new ArrayList<SketchConnection>();\r
+               comments = new ArrayList<SketchComment>();\r
+               connections = new ArrayList<SketchConnection>();\r
+               valves = new ArrayList<SketchValve>();\r
+               variables = new ArrayList<SketchVariable>();\r
        }\r
        \r
        public String getName() {\r
@@ -25,73 +28,49 @@ public class Sketch2 {
                this.name = name;\r
        }\r
        \r
-       public SketchObject getSketchObject(int index) {\r
-               return objectMap.get(index);\r
-       }\r
-\r
-       public void addSketchObject(SketchConnection object) {\r
-               unfinished.add(object);\r
-               \r
-               addSketchObject((SketchObject)object);\r
+       public void addComment(SketchComment comment) {\r
+               comments.add(comment);\r
        }\r
        \r
-       public void addSketchObject(SketchObject object) {\r
-               if (objectMap.get(object.getId()) != null) {\r
-                       System.err.println("duplicate object with id "+object.getId());\r
-                       return;\r
-               }\r
-               objectMap.put(object.getId(), object);\r
-               \r
-               // NOTE: could just do another pass to update connections at the end \r
-               // or just use a priority queue or something but the number of \r
-               // unfinished connections at any given time should be relatively \r
-               // small in practice so it does not matter that this is inefficient\r
-               Iterator<SketchConnection> i = unfinished.iterator();\r
-               while (i.hasNext()) {\r
-                       SketchConnection connection = i.next();\r
-                       \r
-                       SketchObject from = objectMap.get(connection.getFromId());\r
-                       SketchObject to   = objectMap.get(connection.getToId());\r
-                       \r
-                       if (from != null && to != null) {\r
-                               // connections can only connect elements so if this is not the \r
-                               // case something has gone wrong with the indexing\r
-                               assert from instanceof SketchElement;\r
-                               assert to instanceof SketchElement;\r
-                               \r
-                               connection.setFrom((SketchElement)from);\r
-                               connection.setTo((SketchElement)to);\r
-                               \r
-                               i.remove();\r
-                       }\r
-               }\r
+       public List<SketchComment> getComments() {\r
+               return comments;\r
        }\r
-\r
-       // TODO: ugly and inefficient\r
        \r
-       public List<SketchElement> getElements() {\r
-               ArrayList<SketchElement> list = new ArrayList<SketchElement>();\r
-               for (SketchObject o : objectMap.values()) {\r
-                       if (o instanceof SketchElement) {\r
-                               list.add((SketchElement)o);\r
-                       }\r
-               }\r
-               return list;\r
+       public void addConnection(SketchConnection connection) {\r
+               connections.add(connection);\r
        }\r
        \r
        public List<SketchConnection> getConnections() {\r
-               ArrayList<SketchConnection> list = new ArrayList<SketchConnection>();\r
-               for (SketchObject o : objectMap.values()) {\r
-                       if (o instanceof SketchConnection) {\r
-                               list.add((SketchConnection)o);\r
-                       }\r
-               }\r
+               return connections;\r
+       }\r
+       \r
+       public void addValve(SketchValve valve) {\r
+               valves.add(valve);\r
+       }\r
+       \r
+       public List<SketchValve> getValves() {\r
+               return valves;\r
+       }\r
+       \r
+       public void addVariable(SketchVariable variable) {\r
+               variables.add(variable);\r
+       }\r
+       \r
+       public List<SketchVariable> getVariables() {\r
+               return variables;\r
+       }\r
+       \r
+       public List<SketchElement> getAllElements() {\r
+               ArrayList<SketchElement> list = new ArrayList<SketchElement>();\r
+               list.addAll(getComments());\r
+               list.addAll(getValves());\r
+               list.addAll(getVariables());\r
                return list;\r
        }\r
        \r
        public int getWidth() {\r
                int width = 0;\r
-               for (SketchElement e : getElements()) {\r
+               for (SketchElement e : getAllElements()) {\r
                        width = Math.max(width, e.getX()+e.getWidth());\r
                }\r
                return width;\r
@@ -99,10 +78,18 @@ public class Sketch2 {
        \r
        public int getHeight() {\r
                int height = 0;\r
-               for (SketchElement e : getElements()) {\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 HashMap<Integer, Element2> elements = new HashMap<Integer, Element2>();\r
 \r
 }\r
index c30e51b20f196623a51617599a55a674df3d9cfd..f077379f56b6084f0c9cb344410a853096692077 100644 (file)
@@ -6,14 +6,10 @@ public class SketchComment extends SketchElement {
        \r
        private String text;\r
        private CommentIcon icon;\r
-       private boolean nextLine;\r
-       private boolean isIO;\r
 \r
-       public SketchComment(int id, CommentIcon icon, boolean nextLine, boolean isIO) {\r
+       public SketchComment(int id, CommentIcon icon) {\r
                super(id);\r
                this.icon = icon;\r
-               this.nextLine = nextLine;\r
-               this.isIO = isIO;\r
        }\r
 \r
        public String getText() {\r
@@ -25,28 +21,16 @@ public class SketchComment extends SketchElement {
        }\r
        \r
        public CommentIcon getIcon() {\r
-               return this.icon;\r
+               return icon;\r
        }\r
        \r
-       public boolean isCloud() {\r
-               return this.icon.equals(CommentIcon.CLOUD);\r
-       }\r
-       \r
-       public boolean hasTextNextLine() {\r
-               return nextLine;\r
-       }\r
-       \r
-       public boolean isIOElement() {\r
-               return this.isIO;\r
-       }\r
-       \r
-       public Comment2 getWriteableElement(int xOffset, int yOffset) {\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public String toString() {\r
-               return text != null ? "\""+text+"\"" : "(empty comment)";\r
+       public Element2 getModelElement(double xOffset, double yOffset) {\r
+               if (icon.equals(CommentIcon.CLOUD)) {\r
+                       return new Cloud2(getX(xOffset), getY(yOffset));\r
+               }\r
+               else {\r
+                       return new Comment2(getX(xOffset), getY(yOffset), text);\r
+               }\r
        }\r
        \r
 }\r
index acb4bd3766155286a10276948ab9e255324e8c4b..6207d692dff65395bd1ae335ac2a7b860a56e0d3 100644 (file)
@@ -4,61 +4,41 @@ import org.simantics.sysdyn.modelImport.MdlUtils.ConnectionType;
 \r
 public class SketchConnection extends SketchObject {\r
 \r
-       private int fromId;\r
-       private SketchElement from;\r
-       private int toId;\r
-       private SketchElement to;\r
+       private int from;\r
+       private int to;\r
        private ConnectionType type;\r
        \r
-       public SketchConnection(int id, int fromId, int toId, ConnectionType type) {\r
+       public SketchConnection(int id, int from, int to, ConnectionType type) {\r
                super(id);\r
-               this.fromId = fromId;\r
-               this.toId = toId;\r
+               this.from = from;\r
+               this.to = to;\r
                this.type = type;\r
        }\r
        \r
-       public int getFromId() {\r
-               return fromId;\r
-       }\r
-       \r
-       public int getToId() {\r
-               return toId;\r
-       }\r
-\r
-       public SketchElement getFrom() {\r
-               if (from == null) {\r
-                       System.err.println("endpoint "+fromId+" for connection "+getId()+" is unset");\r
-               }\r
+       public int getFrom() {\r
                return from;\r
        }\r
-\r
-       public void setFrom(SketchElement from) {\r
-               this.from = from;\r
-       }\r
-\r
-       public SketchElement getTo() {\r
-               if (to == null) {\r
-                       System.err.println("endpoint "+toId+" for connection "+getId()+" is unset");\r
-               }\r
+       \r
+       public int getTo() {\r
                return to;\r
        }\r
-\r
-       public void setTo(SketchElement to) {\r
-               this.to = to;\r
-       }\r
        \r
        public ConnectionType getType() {\r
                return type;\r
        }\r
        \r
-       public Connection2 getWriteableConnection() {\r
-               // return dependency or flow\r
+       public Connection2 getWriteableConnection(Element2 head, Element2 tail) {\r
+               if (type.equals(ConnectionType.ARROW)) {\r
+                       return new Dependency2(head, tail, false, false);\r
+               }\r
+               else if (type.equals(ConnectionType.LINE_ARROW)) {\r
+                       return new Flow2(head, tail);\r
+               }\r
+               else if (type.equals(ConnectionType.LINE_SEGMENT)) {\r
+                       // TODO: what is this I don't even...\r
+                       return new Flow2(tail, head);\r
+               }\r
                return null;\r
        }\r
        \r
-       @Override\r
-       public String toString() {\r
-               return from.toString() + " --> " + to.toString();\r
-       }\r
-       \r
 }\r
index 157b7d98266c2f4d56dfaf4910886826ceb981d0..ef54a0174fd1b9920b9339fcb3d6e612fef0ecb9 100644 (file)
@@ -6,25 +6,45 @@ public abstract class SketchElement extends SketchObject {
        private int y;\r
        private int width;\r
        private int height;\r
+       private boolean attached;\r
+       private boolean allowsIn;\r
+       private boolean allowsOut;\r
+       private boolean inputOutput;\r
+       private boolean textLine;\r
        \r
        public SketchElement(int id) {\r
                super(id);\r
        }\r
        \r
-       public void setLocationAndSize(int x, int y, int width, int height) {\r
+       public void init(int x, int y, int width, int height, \r
+                       boolean attached, boolean allowsIn, boolean allowsOut, \r
+                       boolean inputOutput, boolean textLine) {\r
                this.x = x;\r
                this.y = y;\r
                this.width = width;\r
                this.height = height;\r
+               this.attached = attached;\r
+               this.allowsIn = allowsIn;\r
+               this.allowsOut = allowsOut;\r
+               this.inputOutput = inputOutput;\r
+               this.textLine = textLine;\r
        }\r
 \r
        public int getX() {\r
                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
@@ -34,6 +54,26 @@ public abstract class SketchElement extends SketchObject {
                return height;\r
        }\r
        \r
-       public abstract Element2 getWriteableElement(int xOffset, int yOffset);\r
+       public boolean isAttached() {\r
+               return attached;\r
+       }\r
+\r
+       public boolean allowsIn() {\r
+               return allowsIn;\r
+       }\r
+\r
+       public boolean allowsOut() {\r
+               return allowsOut;\r
+       }\r
+\r
+       public boolean isInputOutput() {\r
+               return inputOutput;\r
+       }\r
+\r
+       public boolean hasTextLine() {\r
+               return textLine;\r
+       }\r
+\r
+       public abstract Element2 getModelElement(double xOffset, double yOffset);\r
        \r
 }\r
index b28c6f0c818342046859112ab72af07d1489a478..89b1933c2c9b3100e007081aa35c2ecbe4c9cfda 100644 (file)
@@ -1,9 +1,7 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
 public abstract class SketchObject {\r
-       \r
-       // TODO: some type stuff here maybe?\r
-       \r
+\r
        private int id;\r
        \r
        SketchObject(int id) {\r
index 0a774d3d8ac05c3125b9a7e1c002eb0214759dc1..6b8161f4173b5fabe721166842bc0e423be3009a 100644 (file)
@@ -17,13 +17,8 @@ public class SketchValve extends SketchElement {
        }\r
        \r
        @Override\r
-       public Valve2 getWriteableElement(int xOffset, int yOffset) {\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public String toString() {\r
-               return "X("+variable.toString()+")";\r
+       public Element2 getModelElement(double xOffset, double yOffset) {\r
+               return new Valve2(getX(xOffset), getY(yOffset), variable.getVariable());\r
        }\r
 \r
 }\r
index fad806ac36a5921f4e95a10effa8fe8bc528477f..1d86295f67463d4a47bf8d28a3bbff02d0933c1d 100644 (file)
@@ -1,53 +1,26 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
-import org.simantics.sysdyn.modelImport.MdlUtils;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
 public class SketchVariable extends SketchElement {\r
        \r
        private Variable2 variable;\r
-       boolean isAttached;\r
-       boolean allowsInBound;\r
-       boolean allowsOutBound;\r
 \r
-       public SketchVariable(int id, Variable2 variable, boolean isAttached, boolean in, boolean out) {\r
+       public SketchVariable(int id, Variable2 variable) {\r
                super(id);\r
                this.variable = variable;\r
-               this.isAttached = isAttached;\r
-               this.allowsInBound = in;\r
-               this.allowsOutBound = out;\r
        }\r
        \r
        public Variable2 getVariable() {\r
-               return this.variable;\r
-       }\r
-       \r
-       public boolean isAttached() {\r
-               return isAttached;\r
-       }\r
-       \r
-       public boolean allowsInBound() {\r
-               return allowsInBound;\r
-       }\r
-       \r
-       public boolean allowsOutBound() {\r
-               return allowsOutBound;\r
+               return variable;\r
        }\r
        \r
        @Override\r
-       public Element2 getWriteableElement(int xOffset, int yOffset) {\r
-               Pair<String, String> integral = MdlUtils.getPossibleIntegral(variable.getEquation());\r
-               if (integral != null) {\r
-                       return new Stock2(getX(), getY(), variable, integral);\r
+       public Element2 getModelElement(double xOffset, double yOffset) {\r
+               if (variable.getExpression() instanceof IntegralExpression) {\r
+                       return new Stock2(getX(xOffset), getY(yOffset), variable);\r
                }\r
                else {\r
-                       return new Auxiliary2(getX(), getY(), variable, variable.getEquation());\r
+                       return new Auxiliary2(getX(xOffset), getY(yOffset), variable);\r
                }\r
        }\r
        \r
-       @Override\r
-       public String toString() {\r
-               return (allowsInBound?"":"<")+variable.getName()+(allowsInBound?"":">");\r
-       }\r
-       \r
 }\r
index ae37c2c3e6ff663a0c60da7711e2b3cb6ae0fcff..41d424c41f9f4f40d7f467a0df5158bc56d7307b 100644 (file)
@@ -1,49 +1,38 @@
 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
-import org.simantics.utils.datastructures.Pair;\r
 \r
 public class Stock2 extends ModelVariable {\r
        \r
-       protected String integral;\r
-       protected String initial;\r
-\r
        public Stock2(double x, double y, \r
-                       String name, String unit, Range range, String description, \r
-                       String integral, String initial) {\r
-               this.integral = integral;\r
-               this.initial = initial;\r
+                       String name, IntegralExpression expression, Range range, String unit, String description) {\r
+               super(x, y, name, expression, range, unit, description);\r
        }\r
        \r
-       public Stock2(double x, double y, Variable2 variable, Pair<String, String> equation) {\r
-               this.integral = equation.first;\r
-               this.initial = equation.second;\r
+       public Stock2(double x, double y, Variable2 variable) {\r
+               super(x, y, variable);\r
        }\r
        \r
        public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
 \r
                Resource stock = createVariable(graph, sr.Stock, parent);\r
-\r
-               Resource expression = GraphUtils.create2(graph, sr.StockExpression,\r
-                               sr.StockExpression_integralEquation, integral,\r
-                               sr.StockExpression_initialEquation, initial,\r
-                               l0.PartOf, stock);\r
-               // TODO: why is the expressoin stored in two places?\r
-               graph.claim(stock, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
-\r
                createSymbol(graph, sr.StockSymbol, stock, parent);\r
 \r
                setResource(stock);\r
        }\r
+\r
+       @Override\r
+       public boolean supportsDependencies() {\r
+               return true;\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsFlows() {\r
+               return true;\r
+       }\r
        \r
 }\r
index d222372b825b95a86aa2f23144d4637a61c5707b..5ea5a99b2e975267a28fa934c03eaddb9649abe5 100644 (file)
@@ -1,6 +1,11 @@
 package org.simantics.sysdyn.modelImport.model;\r
 \r
-public class Valve2 extends Element2 {\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
+\r
+public class Valve2 extends ModelVariable {\r
        \r
        public enum Orientation {\r
                HORIZONTAL, VERTICAL\r
@@ -10,8 +15,33 @@ public class Valve2 extends Element2 {
                INSIDE, BELOW, LEFT, ABOVE, RIGHT, UNSET\r
        }\r
        \r
-       public Valve2(String equation, Orientation orientation, TextPosition position, String unit, Range range, String description) {\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
+       }\r
+       \r
+       public Valve2(double x, double y, Variable2 variable) {\r
+               super(x, y, variable);\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent) 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
+\r
+               setResource(valve);\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsDependencies() {\r
+               return true;\r
+       }\r
+\r
+       @Override\r
+       public boolean supportsFlows() {\r
+               return true;\r
        }\r
 \r
 }\r
index 8c646cb4508356db33271fc8699d1c9f8cdd667e..f1d365c65247127b232c6cf0b70cec86e99832fd 100644 (file)
@@ -3,14 +3,16 @@ package org.simantics.sysdyn.modelImport.model;
 public class Variable2 {\r
        \r
        private String name;\r
-       private String equation;\r
+       private IExpression expression;\r
        private String unit;\r
+       private Range range;\r
        private String description;\r
        \r
-       public Variable2(String name, String equation, String unit, String description) {\r
+       public Variable2(String name, IExpression expression, String unit, Range range, String description) {\r
                this.name = name;\r
-               this.equation = equation;\r
+               this.expression = expression;\r
                this.unit = unit;\r
+               this.range = range;\r
                this.description = description;\r
        }\r
        \r
@@ -18,31 +20,20 @@ public class Variable2 {
                return name;\r
        }\r
        \r
-       public void setName(String name) {\r
-               this.name = name;\r
+       public IExpression getExpression() {\r
+               return expression;\r
        }\r
        \r
-       public String getEquation() {\r
-               return equation;\r
-       }\r
-\r
-       public void setEquation(String equation) {\r
-               this.equation = equation;\r
+       public Range getRange() {\r
+               return range;\r
        }\r
 \r
        public String getUnit() {\r
                return unit;\r
        }\r
 \r
-       public void setUnit(String unit) {\r
-               this.unit = unit;\r
-       }\r
-\r
        public String getDescription() {\r
                return description;\r
        }\r
-\r
-       public void setDescription(String description) {\r
-               this.description = description;\r
-       }\r
+       \r
 }\r