\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
*******************************************************************************/\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
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
// 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
}\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
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
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
\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
@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
@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
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
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
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
}\r
\r
public String getName() {\r
+ if(name == null)\r
+ return "Name";\r
return name;\r
}\r
\r
}\r
\r
public String getExpression() {\r
+ if(expression == null)\r
+ return "";\r
return expression;\r
}\r
\r
\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
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
}\r
\r
public String getName() {\r
+ if(name == null)\r
+ return "ModelName";\r
return name;\r
}\r
\r
*/\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
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
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
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
@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
@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
\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
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
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
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
\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
OrderedSetUtils.add(graph, diagram, symbol);\r
\r
resource = variable;\r
- System.out.println("===================================================");\r
}\r
\r
\r