]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Added error tolerance. Angle calculations for dependency connections based on the...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 22 Feb 2011 10:21:28 +0000 (10:21 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 22 Feb 2011 10:21:28 +0000 (10:21 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19814 ac1ea38d-2e2b-0410-8846-a27921b304fc

13 files changed:
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Dependency.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Flow.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java

index d04e9dfef1a1968dd4ff75426247f336b76e012d..cba41737db93e95b258b1616af3ef3e81da1256c 100644 (file)
@@ -30,16 +30,17 @@ public class ImportUtils {
                \r
                if(string.contains("\"")) {\r
                        boolean startedQuote = false;\r
-                       StringBuilder var = new StringBuilder();;\r
+                       StringBuilder var = new StringBuilder();\r
                        for(char c : string.toCharArray()) {\r
                                if(c == '"') {\r
                                        if(!startedQuote) {\r
                                                startedQuote = true;\r
                                                var = new StringBuilder();\r
-                                               break;\r
+                                               continue;\r
                                        } else {\r
                                                startedQuote = false;\r
                                                sb.append(var.toString().replaceAll("[^a-zA-Z 0-9]+", ""));\r
+                                               continue;\r
                                        }\r
                                }\r
                                if(startedQuote) {\r
index 2118f6fb400e60f5e9d871807099b9e6b45f062a..3dcb3560b225f7b707d11ec4bb260701f5d1f53a 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.mdlImport;\r
 \r
+import java.awt.geom.Point2D;\r
 import java.io.BufferedReader;\r
 import java.io.File;\r
+import java.io.FileInputStream;\r
 import java.io.FileReader;\r
 import java.io.IOException;\r
+import java.io.InputStreamReader;\r
+import java.io.Reader;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 \r
@@ -66,13 +70,16 @@ public class MdlParser {
                        try {\r
                                String line = null; //not declared within while loop\r
                                \r
-                               // Skip document definitions like {UTF-8} from the beginning\r
+                               // See if the document is encoded with UTF-8\r
                                input.mark(30);\r
-                               while (( line = input.readLine()) != null &&\r
-                                               line.trim().startsWith("{") && line.trim().endsWith("}")){\r
-                                       input.mark(30);\r
+                               if (( line = input.readLine()) != null &&\r
+                                               line.contains("{UTF-8}")){\r
+                                       Reader in = new InputStreamReader(new FileInputStream(aFile), "UTF-8");\r
+                                       input =  new BufferedReader(in);\r
+                                       line = input.readLine();\r
+                               } else {\r
+                                       input.reset();\r
                                }\r
-                               input.reset();\r
                                \r
                                while (( line = input.readLine()) != null){\r
                                        // Build an element (combine the lines to one string)\r
@@ -296,6 +303,7 @@ public class MdlParser {
                                // STARTED A NEW VIEW\r
 \r
                                HashMap<String, Element> elementNumbers = new HashMap<String, Element>();\r
+                               ArrayList<String> ghostNumbers = new ArrayList<String>();\r
                                ArrayList<String[]> connections = new ArrayList<String[]>();\r
                                HashMap<String, String[]> emptyValves = new HashMap<String, String[]>(); // map for valves that don't have an element \r
                                \r
@@ -312,10 +320,10 @@ public class MdlParser {
                                        }\r
                                                \r
                                        String[] data = line.split(",");\r
-\r
                                        if(data[0].equals("1")) {\r
                                                // Connections are handled after all elements\r
-                                               connections.add(data);\r
+                                               String[] connectionData = line.split("[,\\|\\(\\)]");\r
+                                               connections.add(connectionData);\r
                                                \r
                                        } else if(data[0].equals("11")){\r
                                                // Valve\r
@@ -374,11 +382,12 @@ public class MdlParser {
                                                model.relocateElement(view, e);\r
                                        } else if(data[0].equals("10") && data.length > 15){\r
                                                // TODO: Ghost\r
-                                               \r
                                                // for now, direct back to the original element\r
                                                Element originalElement = model.getElement(data[2].replace("\"", ""));\r
-                                               if(originalElement != null)\r
+                                               if(originalElement != null) {\r
                                                        elementNumbers.put(data[1], originalElement);\r
+                                                       ghostNumbers.add(data[1]);\r
+                                               }\r
                                        }\r
                                        \r
                                        i++;\r
@@ -422,7 +431,24 @@ public class MdlParser {
                                        Connection connection;          \r
                                        if(connectionData[9].equals("64")) {\r
                                                // Dependency\r
-                                               connection = new Dependency();\r
+                                               Point2D startPoint = new Point2D.Double(start.getX(), start.getY());\r
+                                               Point2D endPoint = new Point2D.Double(end.getX(), end.getY());\r
+                                               Double controlX = Double.parseDouble(connectionData[15]) / SCALE;\r
+                                               Double controlY = Double.parseDouble(connectionData[16]) / SCALE;\r
+                                               Point2D controlPoint = new Point2D.Double(controlX, controlY);\r
+                                               \r
+                                               if(ghostNumbers.contains(connectionData[2]) ||\r
+                                                               ghostNumbers.contains(connectionData[3])) {\r
+                                                       connection = new Dependency();\r
+                                               } else {\r
+                                                       double angle = Dependency.angleOfArc(\r
+                                                                       startPoint.getX(), startPoint.getY(),\r
+                                                                       controlPoint.getX(), controlPoint.getY(),\r
+                                                                       endPoint.getX(), endPoint.getY());\r
+\r
+                                                       connection = new Dependency(angle);\r
+                                               }\r
+                                               \r
                                        } else {\r
                                                // Flow\r
                                                connection = new Flow();\r
index bcdf6900a0d53532d2404d9b36c826381d810cb9..375878d23c67704b8266dd50661989666a79e091 100644 (file)
@@ -22,6 +22,7 @@ public class Auxiliary extends Variable {
 \r
        @Override\r
        public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException {\r
+               \r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                Resource e = GraphUtils.create2(graph, \r
                                sr.NormalExpression,\r
@@ -32,6 +33,9 @@ public class Auxiliary extends Variable {
        @Override\r
        public void write(WriteGraph graph, Resource parent, double xOffset,\r
                        double yOffset) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
+               \r
                try {\r
                        SysdynResource sr = SysdynResource.getInstance(graph);\r
                        if(!graph.isInstanceOf(parent, sr.Configuration))\r
index a024ed05ad57a7725fe4f01045a35cc7d74d04c0..95a8381151e9758363f0be68e19765483a73a55f 100644 (file)
@@ -27,23 +27,30 @@ public class Cloud extends Element {
        @Override\r
        public void write(WriteGraph graph, Resource parent, double xOffset,\r
                        double yOffset) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
+               \r
                try {\r
                        SysdynResource sr = SysdynResource.getInstance(graph);\r
-                       \r
-                       if(!graph.isInstanceOf(parent, sr.Configuration))\r
-                               return;\r
-                       \r
                        Layer0 l0 = Layer0.getInstance(graph); \r
                        ModelingResources mr = ModelingResources.getInstance(graph);\r
                        DiagramResource dr = DiagramResource.getInstance(graph);\r
                        G2DResource g2d = G2DResource.getInstance(graph);\r
+                       \r
+                       if(!graph.isInstanceOf(parent, sr.Configuration))\r
+                               return;\r
+                       \r
+                       Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
 \r
+                       if(diagram == null)\r
+                               return;\r
+                       \r
                        Resource cloud = GraphUtils.create2(graph, \r
                                        sr.Cloud);\r
 \r
                        graph.claim(parent, l0.ConsistsOf, cloud);\r
 \r
-                       Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
+                       \r
 \r
                        Resource symbol = GraphUtils.create2(graph, \r
                                        sr.CloudSymbol,\r
index 33992d87164cf422e414a2b92d0947661de447d8..06bde8e5b859422602765ca6d0d96bc6aa78b831 100644 (file)
@@ -26,12 +26,27 @@ public abstract class Connection implements IWriteableMDLObject {
        protected Element start, end;\r
        \r
        public Resource writeConnection(WriteGraph graph, Resource configuration, Resource connectionType, Resource connectionSymbol) throws DatabaseException {\r
+               if(configuration == null || graph == null \r
+                               || start.getResource() == null || end.getResource() == null) {\r
+                       return null;\r
+               }\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                Layer0 l0 = Layer0.getInstance(graph); \r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
                StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
                \r
+               Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
+               \r
+               if(diagram == null)\r
+                       return null;\r
+               \r
+               if(connectionType == null)\r
+                       connectionType = sr.Dependency;\r
+               \r
+               if(connectionSymbol == null)\r
+                       connectionSymbol = sr.DependencyConnection;\r
+               \r
                // Build the connection to configuration\r
                Resource connection = GraphUtils.create2(graph, \r
                                connectionType,\r
@@ -60,8 +75,6 @@ public abstract class Connection implements IWriteableMDLObject {
                                dr.HasArrowConnector, headConnector,\r
                                dr.HasPlainConnector, tailConnector);\r
                \r
-               Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
-               \r
                OrderedSetUtils.add(graph, diagram, diagramConnection);\r
                \r
                return connection;\r
index c92a20c3a08dcf2447041f220ae3229e3f9c939a..65b4c60ac8ecd6c3e4a2f2c3d47dac089050072b 100644 (file)
@@ -18,27 +18,76 @@ import org.simantics.modeling.ModelingResources;
 import org.simantics.sysdyn.SysdynResource;\r
 \r
 public class Dependency extends Connection {\r
+       \r
+       private double angle;\r
+       \r
+       public Dependency() {\r
+               this(-0.1);\r
+       }\r
+       \r
+       public Dependency(double angle) {\r
+               this.angle = angle; \r
+       }\r
 \r
        @Override\r
        public void write(WriteGraph graph, Resource parent) {\r
+               if(parent == null || graph == null || start == null || end == null)\r
+                       return;\r
                try {\r
-                       System.out.println("DEPENDENCY:");\r
-               System.out.println(start.getName() + " => " + end.getName());\r
-               System.out.println("==============================================");\r
-               if(start.getName().equals("Kotihoidon hoitopaikat") && end.getName().equals("Kotihoidon vuosittaiset kustannukset")) {\r
-                       System.out.println("MORO");\r
-               }\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-               Resource connection = writeConnection(graph, parent, sr.Dependency, sr.DependencyConnection);\r
-               \r
-               ModelingResources mr = ModelingResources.getInstance(graph);\r
-               Resource diagramConnection = graph.getSingleObject(connection, mr.ConnectionToDiagramConnection);\r
-               graph.claimLiteral(diagramConnection, sr.angle, -0.1);\r
-               \r
+                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                       Resource connection = writeConnection(graph, parent, sr.Dependency, sr.DependencyConnection);\r
+\r
+                       if(connection != null) {\r
+                               ModelingResources mr = ModelingResources.getInstance(graph);\r
+                               Resource diagramConnection = graph.getSingleObject(connection, mr.ConnectionToDiagramConnection);\r
+                               graph.claimLiteral(diagramConnection, sr.angle, angle);\r
+                       }\r
+\r
                } catch (DatabaseException e) {\r
                        e.printStackTrace();\r
                }\r
-               \r
+\r
+       }\r
+\r
+       public double getAngle() {\r
+               return angle;\r
+       }\r
+\r
+       public void setAngle(double angle) {\r
+               this.angle = angle;\r
        }\r
+       \r
+       \r
+       \r
+    /**\r
+     * Returns an angle in radians between straight line from (x0,y0) to (x2,y2)\r
+     * and an arc from (x0,y0) to (x2,y2) thru (x1,y1). The angle\r
+     * is measured at (x0,y0) and is between -PI and PI.\r
+     */\r
+    public static double angleOfArc(\r
+        double x0, double y0, \r
+        double x1, double y1,\r
+        double x2, double y2) {\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
+        // Length of cross product (p1-p0)x(p2-p0)\r
+        double dd = dx0*dy - dy0*dx; \r
+        \r
+        if(Math.abs(dd) < 1e-6) // Points are (almost) collinear\r
+            return 0.0;\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
 }\r
index 34fa0c6a871915234afcb7664e24c64eb19d3286..fb233346a4ce86fd6f7dbe54c1e134fd834d6f8d 100644 (file)
@@ -46,6 +46,8 @@ public abstract class Element implements IWriteableMDLElement {
        }\r
 \r
        public String getName() {\r
+               if(name == null)\r
+                       return "Name";\r
                return name;\r
        }\r
 \r
index b2b20f6b80aebde1778e56a7612d69a9845743c9..e36ca60adac64562e772571bbb384b26942a6c27 100644 (file)
@@ -29,6 +29,8 @@ public class Expression {
        }\r
 \r
        public String getExpression() {\r
+               if(expression == null)\r
+                       return "";\r
                return expression;\r
        }\r
 \r
index 9b23b1a4fe8b9001a0af6263bb733bb3ddce7c5f..b8e8e50f9ac57524d7311644fbf4eae1c55ce1cd 100644 (file)
@@ -20,6 +20,8 @@ public class Flow extends Connection {
 \r
        @Override\r
        public void write(WriteGraph graph, Resource parent) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
                try {\r
                        SysdynResource sr = SysdynResource.getInstance(graph);\r
                        writeConnection(graph, parent, sr.Flow, sr.FlowConnection);\r
index bf18d3695733fa347cf3f8a14e1a9795e66eaa1d..011cb30bf6c59c5502e4dd76eb2b8d1d9337a784 100644 (file)
@@ -32,7 +32,7 @@ import org.simantics.modeling.ModelingResources;
 public class Model implements IWriteableMDLObject {\r
 \r
        private String name, timeUnit, saveper;\r
-       private double startTime, endTime, timeStep;\r
+       private double startTime = 0, endTime = 10, timeStep = 1;\r
        \r
        private HashMap<String, Element> elementMap = new HashMap<String, Element>();\r
        private ArrayList<Subscript> subscripts = new ArrayList<Subscript>();\r
@@ -126,6 +126,8 @@ public class Model implements IWriteableMDLObject {
        }\r
 \r
        public String getName() {\r
+               if(name == null)\r
+                       return "ModelName";\r
                return name;\r
        }\r
 \r
@@ -204,6 +206,9 @@ public class Model implements IWriteableMDLObject {
         */\r
        @Override\r
        public void write(WriteGraph graph, Resource parent) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
+               \r
                try {\r
                        SysdynResource sr = SysdynResource.getInstance(graph);\r
                        Layer0 l0 = Layer0.getInstance(graph);\r
@@ -217,8 +222,8 @@ public class Model implements IWriteableMDLObject {
                                        graph,\r
                                        sr.SysdynModel,\r
                                        l0.PartOf, parent,\r
-                                       l0.HasName, name,\r
-                                       l0.HasLabel, name,\r
+                                       l0.HasName, getName(),\r
+                                       l0.HasLabel, getName(),\r
                                        sr.HasStartTime, startTime,\r
                                        sr.HasStopTime, endTime                 \r
                        );\r
@@ -231,7 +236,7 @@ public class Model implements IWriteableMDLObject {
                        Resource conf = GraphUtils.create2(graph,\r
                                        sr.Configuration,\r
                                        l0.PartOf, model,\r
-                                       l0.HasName, name\r
+                                       l0.HasName, getName()\r
                        );\r
 \r
                        graph.claim(conf, mr.CompositeToDiagram, diagram);\r
@@ -265,11 +270,12 @@ public class Model implements IWriteableMDLObject {
                                if(v.getHeight() > height)\r
                                        height = v.getHeight();\r
                        }\r
-                       // FIXME: INDEXING\r
+\r
                        for(int i = 0; i < n; i++) {\r
                                for(int j = 0; j < n; j++) {\r
-                                       if(i * (int)n + j < views.size()) {\r
-                                               View v = views.get(i * (int)n + j);\r
+                                       int index = i * (int)n + j;\r
+                                       if(index < views.size()) {\r
+                                               View v = views.get(index);\r
                                                v.setxOffset(width * j);\r
                                                v.setyOffset(height * i);\r
                                                v.write(graph, conf);\r
index 19cf3e4bb64c362d69887965f7176dd602688f5b..fdb9054190d8d38caddea61ad654cc4ec6ed46ed 100644 (file)
@@ -61,6 +61,9 @@ public class Stock extends Variable {
        @Override\r
        public void write(WriteGraph graph, Resource parent, double xOffset,\r
                        double yOffset) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
+               \r
                try {\r
                        SysdynResource sr = SysdynResource.getInstance(graph);\r
                        if(!graph.isInstanceOf(parent, sr.Configuration))\r
index 7f7f25fd86a8c133c80defaafa450503526b99ae..40ca47616beec3d316a08bc670c6f02ce2a2ae12 100644 (file)
@@ -30,6 +30,9 @@ public class Subscript extends Variable {
        @Override\r
        public void write(WriteGraph graph, Resource parent, double xOffset,\r
                        double yOffset) {\r
+               if(parent == null || graph == null)\r
+                       return;\r
+               \r
                try {\r
                        SysdynResource sr = SysdynResource.getInstance(graph);\r
                        if(!graph.isInstanceOf(parent, sr.Configuration))\r
@@ -49,7 +52,7 @@ public class Subscript extends Variable {
 \r
                        Resource enumeration = GraphUtils.create2(graph, \r
                                        sr.Enumeration,\r
-                                       l0.HasName, name,\r
+                                       l0.HasName, this.getName(),\r
                                        sr.HasEnumerationIndexes, enumerationIndexes);\r
 \r
                        graph.claim(parent, l0.ConsistsOf, enumeration);\r
index 562e48fe61bb9db233331d17f5824f3a383b1113..405280436865f2a2306efb804be0cfce47b8f011 100644 (file)
@@ -34,17 +34,16 @@ public abstract class Variable extends Element {
        private Double rangeStart, rangeEnd, rangeStep;\r
        \r
        protected void createVariable(WriteGraph graph, Resource configuration, Resource variableType, Resource symbolType, double xOffset, double yOffset) throws DatabaseException {\r
-               System.out.println("ELEMENT: " + name);\r
-               if("Tyƶvoiman poisto Rate".equals(getName())) {\r
-                       System.out.println("STOP");\r
-               }\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                Layer0 l0 = Layer0.getInstance(graph); \r
                ModelingResources mr = ModelingResources.getInstance(graph);\r
                DiagramResource dr = DiagramResource.getInstance(graph);\r
                G2DResource g2d = G2DResource.getInstance(graph);\r
                \r
-               System.out.println("Expressions");\r
+               Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
+               if(diagram == null)\r
+                       return;\r
+               \r
                Resource expressionList = OrderedSetUtils.create(graph, sr.Expressions);\r
                \r
                // Make sure at least one expression exist\r
@@ -68,7 +67,6 @@ public abstract class Variable extends Element {
                Resource arrayIndexList = OrderedSetUtils.create(graph, sr.ArrayIndexes);\r
                \r
 \r
-               System.out.println("Variable");\r
                Resource variable = GraphUtils.create2(graph, \r
                                variableType,\r
                                l0.HasName, ImportUtils.escapeName(name),\r
@@ -76,10 +74,10 @@ public abstract class Variable extends Element {
                graph.claim(variable, mr.Mapped, variable);\r
                \r
                \r
-               System.out.println("Subscripts");\r
                if(subscripts != null) {\r
                        for(Subscript sub : subscripts) {\r
-                               OrderedSetUtils.add(graph, arrayIndexList, sub.getResource());\r
+                               if(sub.getResource() != null)\r
+                                       OrderedSetUtils.add(graph, arrayIndexList, sub.getResource());\r
                        }\r
                        graph.claim(variable, sr.HasArrayIndexes, arrayIndexList);\r
                }\r
@@ -97,9 +95,6 @@ public abstract class Variable extends Element {
        \r
                graph.claim(configuration, l0.ConsistsOf, variable);\r
                \r
-               System.out.println("Diagram");\r
-\r
-               Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
                \r
                Resource symbol = GraphUtils.create2(graph, \r
                                symbolType,\r
@@ -111,7 +106,6 @@ public abstract class Variable extends Element {
                OrderedSetUtils.add(graph, diagram, symbol);\r
                                \r
                resource = variable;\r
-               System.out.println("===================================================");\r
        }\r
        \r
        \r