From b9f667b31a514775ea3f38270e4bb344d24866e7 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 22 Feb 2011 10:21:28 +0000 Subject: [PATCH] Added error tolerance. Angle calculations for dependency connections based on the control points in Vensim git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19814 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/mdlImport/ImportUtils.java | 5 +- .../simantics/sysdyn/mdlImport/MdlParser.java | 46 ++++++++--- .../mdlImport/mdlElements/Auxiliary.java | 4 + .../sysdyn/mdlImport/mdlElements/Cloud.java | 17 ++-- .../mdlImport/mdlElements/Connection.java | 17 +++- .../mdlImport/mdlElements/Dependency.java | 77 +++++++++++++++---- .../sysdyn/mdlImport/mdlElements/Element.java | 2 + .../mdlImport/mdlElements/Expression.java | 2 + .../sysdyn/mdlImport/mdlElements/Flow.java | 2 + .../sysdyn/mdlImport/mdlElements/Model.java | 20 +++-- .../sysdyn/mdlImport/mdlElements/Stock.java | 3 + .../mdlImport/mdlElements/Subscript.java | 5 +- .../mdlImport/mdlElements/Variable.java | 18 ++--- 13 files changed, 165 insertions(+), 53 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java index d04e9dfe..cba41737 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java @@ -30,16 +30,17 @@ public class ImportUtils { if(string.contains("\"")) { boolean startedQuote = false; - StringBuilder var = new StringBuilder();; + StringBuilder var = new StringBuilder(); for(char c : string.toCharArray()) { if(c == '"') { if(!startedQuote) { startedQuote = true; var = new StringBuilder(); - break; + continue; } else { startedQuote = false; sb.append(var.toString().replaceAll("[^a-zA-Z 0-9]+", "")); + continue; } } if(startedQuote) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java index 2118f6fb..3dcb3560 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java @@ -11,10 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.mdlImport; +import java.awt.geom.Point2D; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.util.ArrayList; import java.util.HashMap; @@ -66,13 +70,16 @@ public class MdlParser { try { String line = null; //not declared within while loop - // Skip document definitions like {UTF-8} from the beginning + // See if the document is encoded with UTF-8 input.mark(30); - while (( line = input.readLine()) != null && - line.trim().startsWith("{") && line.trim().endsWith("}")){ - input.mark(30); + if (( line = input.readLine()) != null && + line.contains("{UTF-8}")){ + Reader in = new InputStreamReader(new FileInputStream(aFile), "UTF-8"); + input = new BufferedReader(in); + line = input.readLine(); + } else { + input.reset(); } - input.reset(); while (( line = input.readLine()) != null){ // Build an element (combine the lines to one string) @@ -296,6 +303,7 @@ public class MdlParser { // STARTED A NEW VIEW HashMap elementNumbers = new HashMap(); + ArrayList ghostNumbers = new ArrayList(); ArrayList connections = new ArrayList(); HashMap emptyValves = new HashMap(); // map for valves that don't have an element @@ -312,10 +320,10 @@ public class MdlParser { } String[] data = line.split(","); - if(data[0].equals("1")) { // Connections are handled after all elements - connections.add(data); + String[] connectionData = line.split("[,\\|\\(\\)]"); + connections.add(connectionData); } else if(data[0].equals("11")){ // Valve @@ -374,11 +382,12 @@ public class MdlParser { model.relocateElement(view, e); } else if(data[0].equals("10") && data.length > 15){ // TODO: Ghost - // for now, direct back to the original element Element originalElement = model.getElement(data[2].replace("\"", "")); - if(originalElement != null) + if(originalElement != null) { elementNumbers.put(data[1], originalElement); + ghostNumbers.add(data[1]); + } } i++; @@ -422,7 +431,24 @@ public class MdlParser { Connection connection; if(connectionData[9].equals("64")) { // Dependency - connection = new Dependency(); + Point2D startPoint = new Point2D.Double(start.getX(), start.getY()); + Point2D endPoint = new Point2D.Double(end.getX(), end.getY()); + Double controlX = Double.parseDouble(connectionData[15]) / SCALE; + Double controlY = Double.parseDouble(connectionData[16]) / SCALE; + Point2D controlPoint = new Point2D.Double(controlX, controlY); + + if(ghostNumbers.contains(connectionData[2]) || + ghostNumbers.contains(connectionData[3])) { + connection = new Dependency(); + } else { + double angle = Dependency.angleOfArc( + startPoint.getX(), startPoint.getY(), + controlPoint.getX(), controlPoint.getY(), + endPoint.getX(), endPoint.getY()); + + connection = new Dependency(angle); + } + } else { // Flow connection = new Flow(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java index bcdf6900..375878d2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java @@ -22,6 +22,7 @@ public class Auxiliary extends Variable { @Override public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); Resource e = GraphUtils.create2(graph, sr.NormalExpression, @@ -32,6 +33,9 @@ public class Auxiliary extends Variable { @Override public void write(WriteGraph graph, Resource parent, double xOffset, double yOffset) { + if(parent == null || graph == null) + return; + try { SysdynResource sr = SysdynResource.getInstance(graph); if(!graph.isInstanceOf(parent, sr.Configuration)) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java index a024ed05..95a83811 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java @@ -27,23 +27,30 @@ public class Cloud extends Element { @Override public void write(WriteGraph graph, Resource parent, double xOffset, double yOffset) { + if(parent == null || graph == null) + return; + try { SysdynResource sr = SysdynResource.getInstance(graph); - - if(!graph.isInstanceOf(parent, sr.Configuration)) - return; - Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); DiagramResource dr = DiagramResource.getInstance(graph); G2DResource g2d = G2DResource.getInstance(graph); + + if(!graph.isInstanceOf(parent, sr.Configuration)) + return; + + Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram); + if(diagram == null) + return; + Resource cloud = GraphUtils.create2(graph, sr.Cloud); graph.claim(parent, l0.ConsistsOf, cloud); - Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram); + Resource symbol = GraphUtils.create2(graph, sr.CloudSymbol, diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java index 33992d87..06bde8e5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java @@ -26,12 +26,27 @@ public abstract class Connection implements IWriteableMDLObject { protected Element start, end; public Resource writeConnection(WriteGraph graph, Resource configuration, Resource connectionType, Resource connectionSymbol) throws DatabaseException { + if(configuration == null || graph == null + || start.getResource() == null || end.getResource() == null) { + return null; + } SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); DiagramResource dr = DiagramResource.getInstance(graph); StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); + + if(diagram == null) + return null; + + if(connectionType == null) + connectionType = sr.Dependency; + + if(connectionSymbol == null) + connectionSymbol = sr.DependencyConnection; + // Build the connection to configuration Resource connection = GraphUtils.create2(graph, connectionType, @@ -60,8 +75,6 @@ public abstract class Connection implements IWriteableMDLObject { dr.HasArrowConnector, headConnector, dr.HasPlainConnector, tailConnector); - Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); - OrderedSetUtils.add(graph, diagram, diagramConnection); return connection; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Dependency.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Dependency.java index c92a20c3..65b4c60a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Dependency.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Dependency.java @@ -18,27 +18,76 @@ import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; public class Dependency extends Connection { + + private double angle; + + public Dependency() { + this(-0.1); + } + + public Dependency(double angle) { + this.angle = angle; + } @Override public void write(WriteGraph graph, Resource parent) { + if(parent == null || graph == null || start == null || end == null) + return; try { - System.out.println("DEPENDENCY:"); - System.out.println(start.getName() + " => " + end.getName()); - System.out.println("=============================================="); - if(start.getName().equals("Kotihoidon hoitopaikat") && end.getName().equals("Kotihoidon vuosittaiset kustannukset")) { - System.out.println("MORO"); - } - SysdynResource sr = SysdynResource.getInstance(graph); - Resource connection = writeConnection(graph, parent, sr.Dependency, sr.DependencyConnection); - - ModelingResources mr = ModelingResources.getInstance(graph); - Resource diagramConnection = graph.getSingleObject(connection, mr.ConnectionToDiagramConnection); - graph.claimLiteral(diagramConnection, sr.angle, -0.1); - + SysdynResource sr = SysdynResource.getInstance(graph); + Resource connection = writeConnection(graph, parent, sr.Dependency, sr.DependencyConnection); + + if(connection != null) { + ModelingResources mr = ModelingResources.getInstance(graph); + Resource diagramConnection = graph.getSingleObject(connection, mr.ConnectionToDiagramConnection); + graph.claimLiteral(diagramConnection, sr.angle, angle); + } + } catch (DatabaseException e) { e.printStackTrace(); } - + + } + + public double getAngle() { + return angle; + } + + public void setAngle(double angle) { + this.angle = angle; } + + + + /** + * Returns an angle in radians between straight line from (x0,y0) to (x2,y2) + * and an arc from (x0,y0) to (x2,y2) thru (x1,y1). The angle + * is measured at (x0,y0) and is between -PI and PI. + */ + public static double angleOfArc( + double x0, double y0, + double x1, double y1, + double x2, double y2) { + double dx0 = x1-x0; + double dy0 = y1-y0; + double dx1 = x1-x2; + double dy1 = y1-y2; + double dx = x2-x0; + double dy = y2-y0; + // Length of cross product (p1-p0)x(p2-p0) + double dd = dx0*dy - dy0*dx; + + if(Math.abs(dd) < 1e-6) // Points are (almost) collinear + return 0.0; + else { + // (p1-p0)*(p1-p2) / dd + double offset = (dx0*dx1 + dy0*dy1) / dd; + double angle = Math.PI*0.5 - Math.atan(offset); + if(dd > 0.0) + angle = angle-Math.PI; + return angle; + + } + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java index 34fa0c6a..fb233346 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java @@ -46,6 +46,8 @@ public abstract class Element implements IWriteableMDLElement { } public String getName() { + if(name == null) + return "Name"; return name; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java index b2b20f6b..e36ca60a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java @@ -29,6 +29,8 @@ public class Expression { } public String getExpression() { + if(expression == null) + return ""; return expression; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Flow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Flow.java index 9b23b1a4..b8e8e50f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Flow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Flow.java @@ -20,6 +20,8 @@ public class Flow extends Connection { @Override public void write(WriteGraph graph, Resource parent) { + if(parent == null || graph == null) + return; try { SysdynResource sr = SysdynResource.getInstance(graph); writeConnection(graph, parent, sr.Flow, sr.FlowConnection); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java index bf18d369..011cb30b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java @@ -32,7 +32,7 @@ import org.simantics.modeling.ModelingResources; public class Model implements IWriteableMDLObject { private String name, timeUnit, saveper; - private double startTime, endTime, timeStep; + private double startTime = 0, endTime = 10, timeStep = 1; private HashMap elementMap = new HashMap(); private ArrayList subscripts = new ArrayList(); @@ -126,6 +126,8 @@ public class Model implements IWriteableMDLObject { } public String getName() { + if(name == null) + return "ModelName"; return name; } @@ -204,6 +206,9 @@ public class Model implements IWriteableMDLObject { */ @Override public void write(WriteGraph graph, Resource parent) { + if(parent == null || graph == null) + return; + try { SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); @@ -217,8 +222,8 @@ public class Model implements IWriteableMDLObject { graph, sr.SysdynModel, l0.PartOf, parent, - l0.HasName, name, - l0.HasLabel, name, + l0.HasName, getName(), + l0.HasLabel, getName(), sr.HasStartTime, startTime, sr.HasStopTime, endTime ); @@ -231,7 +236,7 @@ public class Model implements IWriteableMDLObject { Resource conf = GraphUtils.create2(graph, sr.Configuration, l0.PartOf, model, - l0.HasName, name + l0.HasName, getName() ); graph.claim(conf, mr.CompositeToDiagram, diagram); @@ -265,11 +270,12 @@ public class Model implements IWriteableMDLObject { if(v.getHeight() > height) height = v.getHeight(); } - // FIXME: INDEXING + for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { - if(i * (int)n + j < views.size()) { - View v = views.get(i * (int)n + j); + int index = i * (int)n + j; + if(index < views.size()) { + View v = views.get(index); v.setxOffset(width * j); v.setyOffset(height * i); v.write(graph, conf); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java index 19cf3e4b..fdb90541 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java @@ -61,6 +61,9 @@ public class Stock extends Variable { @Override public void write(WriteGraph graph, Resource parent, double xOffset, double yOffset) { + if(parent == null || graph == null) + return; + try { SysdynResource sr = SysdynResource.getInstance(graph); if(!graph.isInstanceOf(parent, sr.Configuration)) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java index 7f7f25fd..40ca4761 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java @@ -30,6 +30,9 @@ public class Subscript extends Variable { @Override public void write(WriteGraph graph, Resource parent, double xOffset, double yOffset) { + if(parent == null || graph == null) + return; + try { SysdynResource sr = SysdynResource.getInstance(graph); if(!graph.isInstanceOf(parent, sr.Configuration)) @@ -49,7 +52,7 @@ public class Subscript extends Variable { Resource enumeration = GraphUtils.create2(graph, sr.Enumeration, - l0.HasName, name, + l0.HasName, this.getName(), sr.HasEnumerationIndexes, enumerationIndexes); graph.claim(parent, l0.ConsistsOf, enumeration); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java index 562e48fe..40528043 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java @@ -34,17 +34,16 @@ public abstract class Variable extends Element { private Double rangeStart, rangeEnd, rangeStep; protected void createVariable(WriteGraph graph, Resource configuration, Resource variableType, Resource symbolType, double xOffset, double yOffset) throws DatabaseException { - System.out.println("ELEMENT: " + name); - if("Tyƶvoiman poisto Rate".equals(getName())) { - System.out.println("STOP"); - } SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); DiagramResource dr = DiagramResource.getInstance(graph); G2DResource g2d = G2DResource.getInstance(graph); - System.out.println("Expressions"); + Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); + if(diagram == null) + return; + Resource expressionList = OrderedSetUtils.create(graph, sr.Expressions); // Make sure at least one expression exist @@ -68,7 +67,6 @@ public abstract class Variable extends Element { Resource arrayIndexList = OrderedSetUtils.create(graph, sr.ArrayIndexes); - System.out.println("Variable"); Resource variable = GraphUtils.create2(graph, variableType, l0.HasName, ImportUtils.escapeName(name), @@ -76,10 +74,10 @@ public abstract class Variable extends Element { graph.claim(variable, mr.Mapped, variable); - System.out.println("Subscripts"); if(subscripts != null) { for(Subscript sub : subscripts) { - OrderedSetUtils.add(graph, arrayIndexList, sub.getResource()); + if(sub.getResource() != null) + OrderedSetUtils.add(graph, arrayIndexList, sub.getResource()); } graph.claim(variable, sr.HasArrayIndexes, arrayIndexList); } @@ -97,9 +95,6 @@ public abstract class Variable extends Element { graph.claim(configuration, l0.ConsistsOf, variable); - System.out.println("Diagram"); - - Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); Resource symbol = GraphUtils.create2(graph, symbolType, @@ -111,7 +106,6 @@ public abstract class Variable extends Element { OrderedSetUtils.add(graph, diagram, symbol); resource = variable; - System.out.println("==================================================="); } -- 2.47.1