import org.simantics.sysdyn.modelImport.MdlParser2;\r
import org.simantics.sysdyn.modelImport.model.Model;\r
import org.simantics.sysdyn.modelImport.model.Model2;\r
+import org.simantics.sysdyn.modelImport.model.WriteContext;\r
import org.simantics.sysdyn.ui.Activator;\r
import org.simantics.ui.SimanticsUI;\r
\r
\r
@Override\r
public void perform(WriteGraph graph) throws DatabaseException {\r
- model.write(graph, project);\r
+ model.write(graph, project, new WriteContext());\r
}\r
});\r
\r
</configIni>\r
\r
<launcherArgs>\r
- <programArgs>-fixerrors
---launcher.XXMaxPermSize 192m
--data @noDefault</programArgs>\r
+ <programArgs>-fixerrors\r
+--launcher.XXMaxPermSize 192m</programArgs>\r
<vmArgs>-ea -Xmx768M -XX:MaxPermSize=192m -Xshare:off -Dorg.simantics.undo.enabled=false</vmArgs>\r
<vmArgsMac>-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts</vmArgsMac>\r
<vmArgsWin>-Dorg.osgi.framework.os.name=win32</vmArgsWin>\r
</win>\r
</launcher>\r
\r
-\r
<vm>\r
<windows include="true">org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6</windows>\r
</vm>\r
import org.simantics.sysdyn.modelImport.model.Connection2;\r
import org.simantics.sysdyn.modelImport.model.Element2;\r
import org.simantics.sysdyn.modelImport.model.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.MdlModel;\r
import org.simantics.sysdyn.modelImport.model.Model2;\r
import org.simantics.sysdyn.modelImport.model.Shadow2;\r
import org.simantics.sysdyn.modelImport.model.Sketch2;\r
\r
private static final String UTF_8 = "{UTF-8}";\r
private static final String SKETCH_VERSION = "V300";\r
- \r
+\r
private static final String CATEGORY_CONTROL = "Control";\r
- \r
+\r
// each .mdl is divided into three sections, these are the the delimiter\r
// strings used to identify where each section starts\r
private static final String SKETCH_START = "\\\\\\---///";\r
private static final String SKETCH_END = "///---\\\\\\";\r
- \r
- private static final double H_SPACE = 10;\r
- private static final double V_SPACE = 0;\r
-\r
- private HashMap<String, Variable2> variables;\r
- private HashMap<String, Variable2> controls;\r
- private ArrayList<Sketch2> sketches;\r
-\r
- public MdlParser2() {\r
- variables = new HashMap<String, Variable2>();\r
- controls = new HashMap<String, Variable2>();\r
- sketches = new ArrayList<Sketch2>();\r
- }\r
\r
- public Model2 parse(File file) {\r
- try {\r
- // peek at the first line to see if we need to use UTF-8\r
- BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));\r
- String line = reader.readLine();\r
-\r
- if (line == null) {\r
- // file is empty, nothing to do here\r
- reader.close();\r
- return null;\r
- }\r
+ private static final double H_SPACE = 0;\r
+ private static final double V_SPACE = 10;\r
\r
- if (line.startsWith(UTF_8)) {\r
- reader.close();\r
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));\r
- // skip the first line\r
- reader.readLine();\r
- line = reader.readLine();\r
- }\r
- \r
- // read variable data\r
- line = readVariables(reader, line);\r
- \r
- if (line == null) {\r
- // unexpected end of file\r
- System.err.println("unexpected end of file");\r
- reader.close();\r
- return null;\r
- }\r
- \r
- // read sketch data\r
- line = readSketches(reader, line);\r
- \r
- if (line == null) {\r
- // unexpected end of file\r
- System.err.println("unexpected end of file");\r
- reader.close();\r
- return null;\r
- }\r
- \r
- // read other data\r
- \r
- do {\r
- // ignore other data for now\r
- } while ((line = reader.readLine()) != null);\r
-\r
- reader.close();\r
+ public static Model2 parse(File file) {\r
+ // generate a mdl model based on the contents of the file\r
+ MdlModel mdl;\r
+ try {\r
+ mdl = parseFile(file);\r
}\r
catch (IOException e) {\r
e.printStackTrace();\r
return null;\r
}\r
- \r
- // create new model\r
\r
+ // generate a sysdyn model from the mdl model\r
Model2 model = new Model2(file.getName());\r
- \r
- double hOffset = 0;\r
- double vOffset = V_SPACE;\r
- \r
+\r
+ double offset = 0;\r
+\r
// do this in several passes\r
- \r
- HashMap<Variable2, Element2> allVariables = new HashMap<Variable2, Element2>();\r
+\r
+ HashMap<Variable2, Element2> variableToElement = new HashMap<Variable2, Element2>();\r
+ ArrayList<SketchVariable> shadows = new ArrayList<SketchVariable>();\r
\r
// add sketch labels and independent elements\r
- for (int i = 0; i < sketches.size(); i++) {\r
- Sketch2 sketch = sketches.get(i);\r
- \r
- sketch.hOffset = hOffset;\r
- sketch.vOffset = vOffset;\r
- \r
- model.addElement(new Comment2(sketch.getWidth() / 2 + sketch.hOffset, 0, sketch.getName()));\r
- \r
+ for (Sketch2 sketch : mdl.getSketches()) {\r
+ sketch.setEdges();\r
+\r
+ sketch.hOffset = 0;\r
+ sketch.vOffset = 0 - sketch.topEdge + 10 + offset;\r
+\r
+ model.addElement(new Comment2(0, offset, -1, -1, sketch.getName()));\r
+\r
for (SketchComment comment : sketch.getComments()) {\r
if (comment.isInputOutput()) {\r
// input / output objects are not supported yet\r
System.err.println("input / output objects are not supported yet");\r
continue;\r
}\r
- \r
+\r
Element2 modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
model.addElement(modelElement);\r
sketch.elements.put(comment.getId(), modelElement);\r
}\r
- \r
+\r
for (SketchValve valve : sketch.getValves()) {\r
Element2 modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset);\r
model.addElement(modelElement);\r
sketch.elements.put(valve.getId(), modelElement);\r
sketch.elements.put(valve.getAttachedVariable().getId(), modelElement);\r
- allVariables.put(valve.getAttachedVariable().getVariable(), modelElement);\r
+ variableToElement.put(valve.getAttachedVariable().getVariable(), modelElement);\r
}\r
- \r
+\r
for (SketchVariable variable : sketch.getVariables()) {\r
if (!variable.allowsIn()) {\r
// the variable is a shadow variable, skip these for now\r
continue;\r
}\r
- \r
+\r
if (variable.isAttached()) {\r
// the variable is attached to a valve, already handled\r
continue;\r
}\r
- \r
+\r
Element2 modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
model.addElement(modelElement);\r
sketch.elements.put(variable.getId(), modelElement);\r
- allVariables.put(variable.getVariable(), modelElement);\r
+ variableToElement.put(variable.getVariable(), modelElement);\r
}\r
- \r
- hOffset += sketch.getWidth() + H_SPACE;\r
+\r
+ offset += (sketch.bottomEdge - sketch.topEdge + V_SPACE);\r
}\r
- \r
+\r
// add dependent elements\r
- for (int i = 0; i < sketches.size(); i++) {\r
- Sketch2 sketch = sketches.get(i);\r
- \r
+ for (Sketch2 sketch : mdl.getSketches()) {\r
for (SketchVariable variable : sketch.getVariables()) {\r
if (!variable.allowsIn()) {\r
// the variable is a shadow variable\r
- Element2 original = allVariables.get(variable.getVariable());\r
+ Element2 original = variableToElement.get(variable.getVariable());\r
if (original == null) {\r
System.err.println("original not found");\r
continue;\r
}\r
- Shadow2 modelElement = new Shadow2(variable.getX(sketch.hOffset), variable.getY(sketch.vOffset), allVariables.get(variable.getVariable()));\r
+ Shadow2 modelElement = new Shadow2(\r
+ variable.getSysdyndX() + sketch.hOffset, \r
+ variable.getSysdyndY() + sketch.vOffset, \r
+ variable.getSysdynWidth(),\r
+ variable.getSysdynHeight(),\r
+ variableToElement.get(variable.getVariable()));\r
model.addShadow(modelElement);\r
sketch.elements.put(variable.getId(), modelElement);\r
}\r
}\r
- \r
+\r
for (SketchConnection connection : sketch.getConnections()) {\r
Element2 head = sketch.elements.get(connection.getTo());\r
Element2 tail = sketch.elements.get(connection.getFrom());\r
- Connection2 c = connection.getWriteableConnection(head, tail);\r
+ Connection2 c = connection.getWriteableConnection(head, tail, sketch.vOffset);\r
if (c != null) {\r
model.addConnection(c);\r
}\r
}\r
}\r
- \r
+\r
return model;\r
}\r
- \r
- private String readVariables(BufferedReader reader, String line) \r
+\r
+ private static MdlModel parseFile(File file) \r
throws IOException {\r
+ MdlModel mdl = new MdlModel(file.getName());\r
\r
+ // peek at the first line to see if we need to use UTF-8\r
+ BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));\r
+ String line = reader.readLine();\r
+\r
+ if (line == null) {\r
+ // file is empty, nothing to do here\r
+ reader.close();\r
+ return null;\r
+ }\r
+\r
+ if (line.startsWith(UTF_8)) {\r
+ reader.close();\r
+ reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));\r
+ // skip the first line\r
+ reader.readLine();\r
+ line = reader.readLine();\r
+ }\r
+\r
+ // read variable data\r
+ line = parseVariables(reader, line, mdl);\r
+\r
+ if (line == null) {\r
+ System.err.println("unexpected end of file");\r
+ reader.close();\r
+ return null;\r
+ }\r
+\r
+ // read sketch data\r
+ line = parseSketches(reader, line, mdl);\r
+\r
+ if (line == null) {\r
+ System.err.println("unexpected end of file");\r
+ reader.close();\r
+ return null;\r
+ }\r
+\r
+ // read other data\r
+ do {\r
+ // TODO: is there anything relevant here?\r
+ } while ((line = reader.readLine()) != null);\r
+\r
+ reader.close();\r
+ return mdl;\r
+ }\r
+\r
+ private static String parseVariables(BufferedReader reader, String line, MdlModel mdl) \r
+ throws IOException {\r
String category = null;\r
- \r
+\r
do {\r
- if (line.isEmpty()) {\r
+ if (line.isEmpty())\r
continue;\r
- }\r
- \r
- StringBuilder buffer = new StringBuilder(line);\r
- \r
+\r
+ StringBuilder buffer = new StringBuilder();\r
do {\r
if (line.endsWith("\\"))\r
buffer.append(line.substring(0, line.length()-1));\r
else\r
buffer.append(line);\r
- \r
+\r
if (line.endsWith("|"))\r
break;\r
} while ((line = reader.readLine()) != null);\r
- \r
String str = buffer.toString();\r
\r
- // handle category declarations\r
String cat = MdlUtils.getPossibleCategory(str);\r
if (cat != null) {\r
category = cat;\r
continue;\r
}\r
- \r
+\r
Variable2 var = MdlUtils.getPossibleVariable(str, category);\r
if (var != null) {\r
- if (CATEGORY_CONTROL.equals(category)) {\r
- controls.put(var.getName(), var);\r
- }\r
- else {\r
- variables.put(var.getName(), var);\r
- }\r
+ mdl.addVariable(var, category);\r
continue;\r
}\r
- \r
+\r
+ // if we got this far, the variable could not be parsed\r
System.err.println("unrecognized variable "+str);\r
- \r
} while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START));\r
- \r
+\r
return line;\r
}\r
- \r
- private String readSketches(BufferedReader reader, String line) \r
+\r
+ private static String parseSketches(BufferedReader reader, String line, MdlModel mdl) \r
throws IOException {\r
- \r
Sketch2 sketch = null;\r
- \r
+\r
do {\r
+ if (line.isEmpty())\r
+ continue;\r
+ \r
if (line.startsWith(SKETCH_START)) {\r
sketch = new Sketch2();\r
- sketches.add(sketch);\r
+ mdl.addSketch(sketch);\r
continue;\r
}\r
else if (line.startsWith(SKETCH_VERSION)) {\r
// font declaration, nothing to do here\r
continue;\r
}\r
- \r
+\r
SketchConnection connection = MdlUtils.getPossibleSketchConnection(line);\r
if (connection != null) {\r
sketch.addConnection(connection);\r
continue;\r
}\r
- \r
- SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, variables);\r
+\r
+ SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, mdl);\r
if (variable != null) {\r
sketch.addVariable(variable);\r
continue;\r
}\r
- \r
+\r
SketchValve valve = MdlUtils.getPossibleSketchValve(line);\r
if (valve != null) {\r
// the next row after a valve should always the variable associated with the valve\r
- SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), variables);\r
+ SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), mdl);\r
if (attached == null || !attached.isAttached()) {\r
- // TODO: should also verify that the variable is in fact attached\r
System.err.println("attached variable not found for valve");\r
continue;\r
}\r
sketch.addVariable(attached);\r
continue;\r
}\r
- \r
+\r
SketchComment comment = MdlUtils.getPossibleSketchComment(line);\r
if (comment != null) {\r
if (comment.hasTextLine()) {\r
sketch.addComment(comment);\r
continue;\r
}\r
- \r
- // if we got this far, the element was not recognized\r
+\r
+ // if we got this far, the element could not be parsed\r
System.err.println("unrecognized element "+line);\r
- \r
} while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END));\r
- \r
+\r
return line;\r
}\r
\r
package org.simantics.sysdyn.modelImport;\r
\r
-import java.util.HashMap;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
+import org.simantics.sysdyn.modelImport.model.DelayExpression;\r
import org.simantics.sysdyn.modelImport.model.IExpression;\r
import org.simantics.sysdyn.modelImport.model.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.MdlModel;\r
import org.simantics.sysdyn.modelImport.model.NormalExpression;\r
import org.simantics.sysdyn.modelImport.model.Range;\r
import org.simantics.sysdyn.modelImport.model.SketchComment;\r
private static final int variableUnit = 3;\r
private static final int variableDesc = 4;\r
\r
+ private static final String NUMBER = \r
+ "-?\\d+(?:.\\d+)?";\r
+ \r
public static Variable2 getPossibleVariable(String line, String category) {\r
Matcher matcher = Pattern.compile(variablePattern).matcher(line);\r
\r
return null;\r
}\r
\r
- String name = matcher.group(variableName);\r
+ String name = normalize(matcher.group(variableName));\r
IExpression expression = parseEquation(matcher.group(variableEquation));\r
\r
String unit = matcher.group(variableUnit);\r
}\r
\r
private static IExpression parseEquation(String equation) {\r
- Matcher matcher = Pattern.compile("INTEG\\s*\\((.*),(.*)\\)").matcher(equation);\r
- if (matcher.matches()) {\r
- return new IntegralExpression(matcher.group(1), matcher.group(2));\r
+ if (equation.startsWith("INTEG")) {\r
+ return parseIntegralExpression(equation);\r
+ }\r
+ else if (equation.startsWith("GAME")) {\r
+ // game expressions are treated as normal parameters as we do not \r
+ // have equivalent functionality in sysdyn\r
+ return parseNormalExpression(equation);\r
}\r
else {\r
- return new NormalExpression("");\r
+ return parseNormalExpression(equation);\r
}\r
}\r
\r
+ private static IntegralExpression parseIntegralExpression(String equation) {\r
+ Matcher matcher = Pattern.compile("INTEG \\((.*),(.*)\\)").matcher(equation);\r
+ if (!matcher.matches()) {\r
+ System.err.println("could not parse integral expression: "+equation);\r
+ return null;\r
+ }\r
+ \r
+ String integral = normalize(matcher.group(1));\r
+ String initial = normalize(matcher.group(2));\r
+ \r
+ return new IntegralExpression(integral, initial);\r
+ }\r
+ \r
+ private static DelayExpression parseDelayExpression(String equation) {\r
+ return null;\r
+ }\r
+ \r
+ private static NormalExpression parseNormalExpression(String equation) {\r
+ return new NormalExpression(normalize(equation));\r
+ }\r
+ \r
private static Range parseRange(String unit) {\r
- Matcher matcher = Pattern.compile("\\[(-?\\d+|\\?),(-?\\d+|\\?)\\]").matcher(unit);\r
+ Matcher matcher = Pattern.compile("\\[("+NUMBER+"),("+NUMBER+"|\\?)\\]").matcher(unit);\r
if (matcher.find()) {\r
String start = matcher.group(1);\r
String end = matcher.group(2);\r
// TODO: and step is?\r
return new Range(\r
- start.equals("?") ? null : Double.parseDouble(start), \r
+ Double.parseDouble(start), \r
end.equals("?") ? null : Double.parseDouble(end), \r
null, \r
matcher.group());\r
int from = Integer.parseInt(matcher.group(connectionFrom));\r
int to = Integer.parseInt(matcher.group(connectionTo));\r
ConnectionType type = getConnectionType(matcher);\r
+ int[] points = parsePoints(matcher.group(connectionPoints));\r
\r
- return new SketchConnection(id, from, to, type);\r
+ return new SketchConnection(id, from, to, type, points);\r
}\r
\r
- public static SketchVariable getPossibleSketchVariable(String line, HashMap<String, Variable2> variables) {\r
+ private static int[] parsePoints(String points) {\r
+ // points should be a well formed points string\r
+ int sep = points.indexOf('|');\r
+ int count = Integer.parseInt(points.substring(0, sep));\r
+ int[] array = new int[count*2];\r
+ for (int i = 0; i < count; i++) {\r
+ int start = sep + 1;\r
+ sep = points.indexOf('|', start);\r
+ String[] pair = points.substring(start+1, sep-1).split(",");\r
+ array[i*2] = Integer.parseInt(pair[0]);\r
+ array[i*2+1] = Integer.parseInt(pair[1]);\r
+ }\r
+ return array;\r
+ }\r
+ \r
+ public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) {\r
Matcher matcher = Pattern.compile(sketchVariable).matcher(line);\r
\r
if (!matcher.matches()) {\r
}\r
\r
int id = Integer.parseInt(matcher.group(elementId));\r
- Variable2 var = variables.get(matcher.group(elementName));\r
+ Variable2 var = mdl.getVariable((normalize(matcher.group(elementName))));\r
\r
SketchVariable variable = new SketchVariable(id, var);\r
\r
}\r
\r
int id = Integer.parseInt(matcher.group(elementId));\r
+ TextPosition pos = getTextPos(matcher);\r
\r
- SketchValve valve = new SketchValve(id);\r
+ SketchValve valve = new SketchValve(id, pos);\r
\r
initializeElement(valve, matcher);\r
\r
str = str.replaceAll("\t", "");\r
\r
// remove inline :AND: and :OR: etc (not done currently)\r
+ str = removeInlineOperation(str, "AND");\r
+ str = removeInlineOperation(str, "OR");\r
+ \r
+ // rename vensim method calls to sysdyn equivalents\r
+ str = str.replaceAll("IF THEN ELSE", "IFTHENELSE");\r
+ \r
+ // TODO: replace array slice operations\r
\r
- // transform all variable names to (single-)quoted modelica syntax\r
+ // normalize variable names\r
+ str = processVariables(str);\r
+\r
+ return str;\r
+ }\r
+ \r
+ private static String removeInlineOperation(String str, String op) {\r
+ while (str.contains(":"+op+":")) {\r
+ // this should work in most cases, even with nested parentheses \r
+ // in the left and right hand side expression\r
+ \r
+ // the regular expression can be split into 5 parts:\r
+ // 1. delimiter character on the left (either start of string, ',' or '(':\r
+ // (?:^|[(,])\r
+ // 2. left hand side expression, anything except ',' unless it is \r
+ // enclosed in parentheses which is allowed:\r
+ // ([^(,]*(?:\\(.*\\)[^(,])*)\r
+ // 3. the inlined operation:\r
+ // :OPERATION:\r
+ // 4. right hand side expression, similar to left hand side expression\r
+ // but with ')' instead of '(':\r
+ // ([^),]*(?:\\(.*\\)[^),])*)\r
+ // 5. delimiter character on the right (either end of string, ',' or ')'):\r
+ // (?:$|[),])\r
+ \r
+ // note that the expression also makes heavy use of both non-capturing and\r
+ // capturing groups ("(?:)" and "()")\r
+ Matcher matcher = Pattern.compile(\r
+ "(?:^|[(,])([^(,]*(?:\\(.*\\)[^(,])*):" + op + ":([^),]*(?:\\(.*\\)[^),])*)(?:$|[),])").matcher(str);\r
+ \r
+ if (!matcher.find()) {\r
+ System.err.println("inlined operation not found");\r
+ return str;\r
+ }\r
+ \r
+ StringBuilder buf = new StringBuilder();\r
+ \r
+ // part before left hand side\r
+ buf.append(str.substring(0, matcher.start(1)));\r
+ // operation open\r
+ buf.append(op);\r
+ buf.append('(');\r
+ // left hand side\r
+ buf.append(matcher.group(1).trim());\r
+ // delimiter\r
+ buf.append(',');\r
+ // right hand side\r
+ buf.append(matcher.group(2).trim());\r
+ // operation close\r
+ buf.append(')');\r
+ // part after right hand side\r
+ buf.append(str.substring(matcher.end(2)));\r
+ \r
+ str = buf.toString();\r
+ }\r
+ return str;\r
+ }\r
+ \r
+ private static String processVariables(String str) {\r
StringBuilder result = new StringBuilder();\r
+ \r
int offset = 0;\r
\r
Matcher matcher = Pattern.compile(VAR_NAME).matcher(str);\r
result.append(str.substring(offset, matcher.start()));\r
\r
String replacement = matcher.group();\r
-\r
+ \r
if (replacement.startsWith("\"")) {\r
- replacement = replacement.substring(1, replacement.length() - 1);\r
+ // if the variable name is quoted, change the quotes to modelica syntax\r
+ replacement = "'" + replacement.substring(1, replacement.length() - 1) + "'";\r
+ }\r
+ else if (Pattern.compile("[-+*/]").matcher(replacement).find()) {\r
+ // if the variable name contains "bad" characters, quote it\r
+ replacement = "'" + replacement + "'";\r
+ }\r
+ else {\r
+ // otherwise simply capitalize the variable name properly\r
+ String[] parts = replacement.split("\\s+");\r
+ StringBuilder buf = new StringBuilder();\r
+ for (String part : parts) {\r
+ part = part.substring(0, 1).toUpperCase() + part.substring(1);\r
+ if (buf.length() > 0)\r
+ buf.append(' ');\r
+ buf.append(part);\r
+ }\r
+ replacement = buf.toString();\r
}\r
\r
- result.append('\'');\r
result.append(replacement);\r
- result.append('\'');\r
\r
offset = matcher.end();\r
}\r
if (offset < str.length()) {\r
result.append(str.substring(offset));\r
}\r
-\r
- str = result.toString();\r
-\r
- return str;\r
+ \r
+ return result.toString();\r
}\r
\r
}\r
\r
public class Auxiliary2 extends ModelVariable {\r
\r
- public Auxiliary2(double x, double y, \r
+ public Auxiliary2(double x, double y, double w, double h,\r
String name, IExpression expression, Range range, String unit, String description) {\r
- super(x, y, name, expression, range, unit, description);\r
+ super(x, y, w, h, name, expression, range, unit, description);\r
}\r
\r
- public Auxiliary2(double x, double y, Variable2 variable) {\r
- super(x, y, variable);\r
+ public Auxiliary2(double x, double y, double w, double h, Variable2 variable) {\r
+ super(x, y, w, h, variable);\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource variable = createVariable(graph, sr.Auxiliary, parent);\r
- createSymbol(graph, sr.AuxiliarySymbol, variable, parent);\r
+ Resource variable = createVariable(graph, sr.Auxiliary, parent, context);\r
+ createSymbol(graph, sr.AuxiliarySymbol, variable, parent, context);\r
\r
setResource(variable);\r
}\r
\r
public class Cloud2 extends Element2 {\r
\r
- public Cloud2(double x, double y) {\r
- super(x, y);\r
+ public Cloud2(double x, double y, double w, double h) {\r
+ super(x, y, w, h);\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource cloud = GraphUtils.create2(graph, sr.Cloud, \r
+ Resource cloud = GraphUtils.create2(graph, sr.Cloud,\r
+ l0.HasName, context.getNextCloud(),\r
l0.PartOf, parent);\r
\r
- createSymbol(graph, sr.CloudSymbol, cloud, parent);\r
+ createSymbol(graph, sr.CloudSymbol, cloud, parent, context);\r
\r
setResource(cloud);\r
}\r
\r
private String text;\r
\r
- public Comment2(double x, double y, String text) {\r
- super(x, y);\r
+ public Comment2(double x, double y, double w, double h, String text) {\r
+ super(x, y, w, h);\r
this.text = text;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
DiagramResource dr = DiagramResource.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph); \r
\r
- Resource comment = createSymbol(graph, sr.AdditionalSymbols_MultilineText, null, parent);\r
+ Resource comment = createSymbol(graph, sr.AdditionalSymbols_MultilineText, null, parent, context);\r
\r
graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING);\r
\r
this.tail = tail;\r
}\r
\r
- protected Resource createConnection(WriteGraph graph, Resource type, Resource symbol, Resource parent) \r
+ protected Resource createConnection(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
throws DatabaseException {\r
- // TODO: remove this\r
- if (head == null || tail == null) {\r
- return null;\r
- }\r
- \r
- // create connection\r
Layer0 l0 = Layer0.getInstance(graph); \r
ModelingResources mr = ModelingResources.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
sr.Variable_HasTail, tail.getResource(),\r
l0.PartOf, parent);\r
graph.claim(connection, mr.Mapped, connection);\r
-\r
- // create symbol\r
+ \r
+ return connection;\r
+ }\r
+ \r
+ protected Resource createSymbol(WriteGraph graph, Resource type, Resource connection, Resource parent, WriteContext context) \r
+ throws DatabaseException {\r
DiagramResource dr = DiagramResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
\r
Resource diagramTail = graph.getPossibleObject(tail.getResource(), mr.ComponentToElement);\r
Resource tailConnector = GraphUtils.create2(graph, dr.Connector,\r
sr.HasHeadTerminal, diagramHead,\r
dr.AreConnected, tailConnector);\r
\r
- Resource diagramConnection = GraphUtils.create2(graph, symbol,\r
+ Resource diagramConnection = GraphUtils.create2(graph, type,\r
+ l0.HasName, context.getNextObject(),\r
+ l0.PartOf, diagram,\r
sr2.HasConnectionType, sr.SysdynConnectionType,\r
mr.DiagramConnectionToConnection, connection,\r
dr.HasArrowConnector, headConnector,\r
dr.HasPlainConnector, tailConnector);\r
\r
- Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
OrderedSetUtils.add(graph, diagram, diagramConnection);\r
-\r
- return connection;\r
+ \r
+ return diagramConnection;\r
}\r
\r
}\r
--- /dev/null
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
+public class DelayExpression implements IExpression {\r
+\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+// Resource expression = GraphUtils.create2(graph, sr.DelayExpression,\r
+// sr.StockExpression_integralEquation, integral,\r
+// sr.StockExpression_initialEquation, initial,\r
+// l0.PartOf, parent);\r
+// // TODO: why is the expression stored in two places?\r
+// graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
+ }\r
+\r
+}\r
package org.simantics.sysdyn.modelImport.model;\r
\r
+import org.simantics.databoard.Bindings;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
\r
public class Dependency2 extends Connection2 {\r
\r
private boolean showArrow;\r
private boolean showDelay;\r
+ private double angle;\r
\r
- public Dependency2(Element2 head, Element2 tail, boolean showArrow, boolean showDelay) {\r
+ public Dependency2(Element2 head, Element2 tail, boolean showArrow, boolean showDelay, double angle) {\r
super(head, tail);\r
this.showArrow = showArrow;\r
this.showDelay = showDelay;\r
+ this.angle = angle;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent)\r
- throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ if (head == null || tail == null) {\r
+ System.err.println("dependency missing head or tail");\r
+ return;\r
+ }\r
+ \r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ \r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource dependency = createConnection(graph, sr.Dependency, sr.DependencyConnection, parent);\r
+ Resource dependency = createConnection(graph, sr.Dependency, parent, context);\r
+ graph.claimLiteral(dependency, l0.HasName, context.getNextDependency(), Bindings.STRING);\r
+ Resource symbol = createSymbol(graph, sr.DependencyConnection, dependency, parent, context);\r
+ \r
+ graph.claimLiteral(symbol, sr.Dependency_angle, angle, Bindings.DOUBLE);\r
}\r
\r
}\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.diagram.stubs.DiagramResource;\r
import org.simantics.diagram.stubs.G2DResource;\r
+import org.simantics.layer0.Layer0;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.modeling.ModelingResources;\r
\r
public abstract class Element2 implements IWriteableObject {\r
\r
- private static final double SCALE = 1.0 / 3.0;\r
-\r
private double x;\r
private double y;\r
+ private double width;\r
+ private double height;\r
\r
private Resource resource;\r
\r
- public Element2(double x, double y) {\r
- this.x = x * SCALE;\r
- this.y = y * SCALE;\r
+ public Element2(double x, double y, double width, double height) {\r
+ this.x = x;\r
+ this.y = y;\r
+ this.width = width;\r
+ this.height = height;\r
+ }\r
+ \r
+ public Element2(SketchElement element, double hOffset, double vOffset) {\r
+ this.x = element.getSysdyndX() + hOffset;\r
+ this.y = element.getSysdyndY() + vOffset;\r
+ this.width = element.getSysdynWidth();\r
+ this.height = element.getSysdynHeight();\r
}\r
\r
public Resource getResource() {\r
public void setResource(Resource resource) {\r
this.resource = resource;\r
}\r
+ \r
+ public double getX() {\r
+ return x;\r
+ }\r
+ \r
+ public double getY() {\r
+ return y;\r
+ }\r
+ \r
+ public double getWidth() {\r
+ return width;\r
+ }\r
+ \r
+ public double getHeight() {\r
+ return height;\r
+ }\r
\r
- public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, Resource parent) throws DatabaseException {\r
+ public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, Resource parent, WriteContext context) throws DatabaseException {\r
DiagramResource dr = DiagramResource.getInstance(graph);\r
G2DResource g2d = G2DResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
ModelingResources mr = ModelingResources.getInstance(graph);\r
\r
- Resource symbol = GraphUtils.create2(graph, type);\r
+ Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
+ \r
+ Resource symbol = GraphUtils.create2(graph, type,\r
+ l0.HasName, context.getNextObject(),\r
+ l0.PartOf, diagram);\r
\r
if (variable != null) {\r
graph.claim(symbol, mr.ElementToComponent, variable);\r
double[] transform = { 1.0, 0.0, 0.0, 1.0, x, y };\r
graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform, Bindings.DOUBLE_ARRAY);\r
\r
- Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
OrderedSetUtils.add(graph, diagram, symbol);\r
\r
return symbol;\r
package org.simantics.sysdyn.modelImport.model;\r
\r
+import org.simantics.databoard.Bindings;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
\r
public class Flow2 extends Connection2 {\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ if (head == null || tail == null) {\r
+ System.err.println("flow missing head or tail");\r
+ return;\r
+ }\r
+ \r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ \r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource flow = createConnection(graph, sr.Flow, sr.FlowConnection, parent);\r
+ Resource flow = createConnection(graph, sr.Flow, parent, context);\r
+ graph.claimLiteral(flow, l0.HasName, context.getNextFlow(), Bindings.STRING);\r
+ Resource symbol = createSymbol(graph, sr.FlowConnection, flow, parent, context);\r
}\r
\r
}\r
* @param parent The resource where the object is located\r
* @throws DatabaseException \r
*/\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException;\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException;\r
}\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
--- /dev/null
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+public class LookupExpression implements IExpression {\r
+\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ // TODO: implementation\r
+ }\r
+\r
+}\r
package org.simantics.sysdyn.modelImport.model;\r
\r
-public class MdlModel {\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
\r
+public class MdlModel {\r
+ \r
+ private String name;\r
+ \r
+ private HashMap<String, Variable2> variables;\r
+ private HashMap<String, ArrayList<Variable2>> groups;\r
+ \r
+ private ArrayList<Sketch2> sketches;\r
+ \r
+ public MdlModel(String name) {\r
+ this.name = name;\r
+ this.variables = new HashMap<String, Variable2>();\r
+ this.groups = new HashMap<String, ArrayList<Variable2>>();\r
+ this.sketches = new ArrayList<Sketch2>();\r
+ }\r
+ \r
+ public void addVariable(Variable2 variable, String group) {\r
+ if (variables.get(variable.getName()) != null) {\r
+ System.err.println("warning, duplicate variable "+variable.getName());\r
+ }\r
+ \r
+ variables.put(variable.getName(), variable);\r
+ \r
+ if (group != null) {\r
+ if (groups.get(group) == null) {\r
+ groups.put(group, new ArrayList<Variable2>());\r
+ }\r
+ groups.get(group).add(variable);\r
+ }\r
+ }\r
+ \r
+ public Variable2 getVariable(String name) {\r
+ return variables.get(name);\r
+ }\r
+ \r
+ public void addSketch(Sketch2 sketch) {\r
+ sketches.add(sketch);\r
+ }\r
+ \r
+ public List<Sketch2> getSketches() {\r
+ return sketches;\r
+ }\r
+ \r
}\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.sysdyn.utils.ModelUtils;\r
\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException { \r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
SimulationResource sim = SimulationResource.getInstance(graph);\r
\r
Resource model = ModelUtils.createModel(graph);\r
Resource configuration = graph.getSingleObject(model, sim.HasConfiguration);\r
\r
for (Element2 e : elements) {\r
- e.write(graph, configuration);\r
+ e.write(graph, configuration, context);\r
}\r
\r
for (Shadow2 s : shadows) {\r
- s.write(graph, configuration);\r
+ s.write(graph, configuration, context);\r
}\r
\r
for (Connection2 c : connections) {\r
- c.write(graph, configuration);\r
+ c.write(graph, configuration, context);\r
}\r
+ \r
+ Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
+ graph.claimLiteral(diagram, dr.HasModCount, context.getObjectCount(), Bindings.LONG);\r
}\r
\r
}\r
--- /dev/null
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon;\r
+\r
+public class ModelElementFactory {\r
+ \r
+ public static void getConnection() {\r
+ \r
+ }\r
+ \r
+ public static void getDependency() {\r
+ \r
+ }\r
+ \r
+ public static void getFlow() {\r
+ \r
+ }\r
+ \r
+ public static Element2 getElement(SketchVariable variable) {\r
+ if (variable.getVariable().getExpression() instanceof IntegralExpression) {\r
+ return getStock(variable);\r
+ }\r
+ else {\r
+ return getAuxiliary(variable);\r
+ }\r
+ }\r
+ \r
+ public static Element2 getElement(SketchValve valve) {\r
+ return getValve(valve);\r
+ }\r
+ \r
+ public static Element2 getElement(SketchComment comment) {\r
+ if (comment.getIcon().equals(CommentIcon.CLOUD)) {\r
+ return getCloud(comment);\r
+ }\r
+ else {\r
+ return getComment(comment);\r
+ }\r
+ }\r
+ \r
+ public static Auxiliary2 getAuxiliary(SketchVariable variable) {\r
+ return null;\r
+ }\r
+ \r
+ public static Stock2 getStock(SketchVariable variable) {\r
+ assert variable.getVariable().getExpression() instanceof IntegralExpression;\r
+ \r
+ return null;\r
+ }\r
+ \r
+ public static Valve2 getValve(SketchValve valve) {\r
+ return null;\r
+ }\r
+ \r
+ public static Cloud2 getCloud(SketchComment comment) {\r
+ assert comment.getIcon().equals(CommentIcon.CLOUD);\r
+ return null;\r
+ }\r
+ \r
+ public static Comment2 getComment(SketchComment comment) {\r
+ return null;\r
+ }\r
+ \r
+ public static Shadow2 getShadow(SketchVariable variable) {\r
+ return null;\r
+ }\r
+\r
+}\r
protected String unit;\r
protected String description;\r
\r
- public ModelVariable(double x, double y, String name, IExpression expression, Range range, String unit, String description) {\r
- super(x, y);\r
+ public ModelVariable(double x, double y, double w, double h, String name, IExpression expression, Range range, String unit, String description) {\r
+ super(x, y, w, h);\r
this.name = name;\r
this.expression = expression;\r
this.range = range;\r
this.description = description;\r
}\r
\r
- public ModelVariable(double x, double y, Variable2 variable) {\r
- super(x, y);\r
+ public ModelVariable(double x, double y, double w, double h, Variable2 variable) {\r
+ super(x, y, w, h);\r
this.name = variable.getName();\r
this.expression = variable.getExpression();\r
this.range = variable.getRange();\r
this.description = variable.getDescription();\r
}\r
\r
- public Resource createVariable(WriteGraph graph, Resource type, Resource parent) \r
+ public Resource createVariable(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
ModelingResources mr = ModelingResources.getInstance(graph);\r
l0.PartOf, parent);\r
graph.claim(variable, mr.Mapped, variable);\r
\r
- expression.write(graph, variable);\r
+ expression.write(graph, variable, context);\r
\r
if (range != null) {\r
- range.write(graph, variable);\r
+ range.write(graph, variable, context);\r
}\r
if (unit != null && !unit.isEmpty()) {\r
graph.claimLiteral(variable, sr.Variable_unit, unit, Bindings.STRING);\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource variable) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource variable) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
// TODO: implementation\r
}\r
\r
\r
private Element2 original;\r
\r
- public Shadow2(double x, double y, Element2 original) {\r
- super(x, y);\r
+ public Shadow2(double x, double y, double w, double h, Element2 original) {\r
+ super(x, y, w, h);\r
+ this.original = original;\r
+ }\r
+ \r
+ public Shadow2(SketchVariable variable, double hOffset, double vOffset, Element2 original) {\r
+ super(variable, hOffset, vOffset);\r
this.original = original;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource shadow = GraphUtils.create2(graph, sr.Shadow, \r
+ Resource shadow = GraphUtils.create2(graph, sr.Shadow,\r
+ l0.HasName, context.getNextShadow(),\r
l0.PartOf, parent);\r
\r
graph.claim(shadow, sr.Shadow_original, original.getResource());\r
\r
- createSymbol(graph, sr.ShadowSymbol, shadow, parent);\r
+ createSymbol(graph, sr.ShadowSymbol, shadow, parent, context);\r
\r
setResource(shadow);\r
}\r
return list;\r
}\r
\r
- public int getWidth() {\r
- int width = 0;\r
- for (SketchElement e : getAllElements()) {\r
- width = Math.max(width, e.getX()+e.getWidth());\r
- }\r
- return width;\r
- }\r
- \r
- public int getHeight() {\r
- int height = 0;\r
- for (SketchElement e : getAllElements()) {\r
- height = Math.max(height, e.getY()+e.getHeight());\r
- }\r
- return height;\r
- }\r
- \r
// relevant for sysdyn model creation, this is not the cleanest place to\r
// store this information but it works\r
\r
public double hOffset = 0;\r
public double vOffset = 0;\r
\r
+ public double topEdge = 0;\r
+ public double bottomEdge = 0;\r
+ public double leftEdge = 0;\r
+ public double rightEdge = 0;\r
+ \r
public HashMap<Integer, Element2> elements = new HashMap<Integer, Element2>();\r
+ \r
+ public void setEdges() {\r
+ boolean first = true;\r
+ for (SketchElement e : getAllElements()) {\r
+ if (first) {\r
+ topEdge = e.getSysdynTopEdge();\r
+ bottomEdge = e.getSysdynBottomEdge();\r
+ leftEdge = e.getSysdynLeftEdge();\r
+ rightEdge = e.getSysdynRightEdge();\r
+ first = false;\r
+ }\r
+ else {\r
+ topEdge = Math.min(topEdge, e.getSysdynTopEdge());\r
+ bottomEdge = Math.max(bottomEdge, e.getSysdynBottomEdge());\r
+ leftEdge = Math.min(leftEdge, e.getSysdynLeftEdge());\r
+ rightEdge = Math.max(rightEdge, e.getSysdynRightEdge());\r
+ }\r
+ }\r
+ }\r
\r
}\r
return icon;\r
}\r
\r
- public Element2 getModelElement(double xOffset, double yOffset) {\r
+ public Element2 getModelElement(double hOffset, double vOffset) {\r
if (icon.equals(CommentIcon.CLOUD)) {\r
- return new Cloud2(getX(xOffset), getY(yOffset));\r
+ return new Cloud2(\r
+ getSysdyndX() + hOffset, \r
+ getSysdyndY() + vOffset, \r
+ getSysdynWidth(),\r
+ getSysdynHeight());\r
}\r
else {\r
- return new Comment2(getX(xOffset), getY(yOffset), text);\r
+ return new Comment2(\r
+ getSysdyndX() + hOffset, \r
+ getSysdyndY() + vOffset, \r
+ getSysdynWidth(),\r
+ getSysdynHeight(),\r
+ text);\r
}\r
}\r
\r
private int from;\r
private int to;\r
private ConnectionType type;\r
- \r
- public SketchConnection(int id, int from, int to, ConnectionType type) {\r
+ int[] points;\r
+\r
+ public SketchConnection(int id, int from, int to, ConnectionType type, int[] points) {\r
super(id);\r
this.from = from;\r
this.to = to;\r
this.type = type;\r
+ this.points = points;\r
}\r
- \r
+\r
public int getFrom() {\r
return from;\r
}\r
- \r
+\r
public int getTo() {\r
return to;\r
}\r
- \r
+\r
public ConnectionType getType() {\r
return type;\r
}\r
- \r
- public Connection2 getWriteableConnection(Element2 head, Element2 tail) {\r
+\r
+ public Connection2 getWriteableConnection(Element2 head, Element2 tail, double offset) {\r
if (type.equals(ConnectionType.ARROW)) {\r
- return new Dependency2(head, tail, false, false);\r
+ return new Dependency2(head, tail, false, false, getSysdynAngle(tail, head, offset));\r
}\r
else if (type.equals(ConnectionType.LINE_ARROW)) {\r
return new Flow2(head, tail);\r
}\r
return null;\r
}\r
- \r
+\r
+ // TODO: comment this?\r
+ public double getSysdynAngle(Element2 from, Element2 to, double voffset) {\r
+ if (points == null || points.length == 0) {\r
+ return 0;\r
+ }\r
+ \r
+ // 'from' element is in (x0, y0) and 'to' element is in (x2, y2)\r
+ double x0 = from.getX() + (from.getWidth() / 2);\r
+ double y0 = from.getY() + (from.getHeight() / 2);\r
+ double x2 = to.getX() + (to.getWidth() / 2);\r
+ double y2 = to.getY() + (to.getHeight() / 2);\r
+ \r
+ // treat the first points in points as the control point (x1, y1)\r
+ double x1 = (double)points[0] * SCALE_MULTIPLIER;\r
+ double y1 = (double)points[1] * SCALE_MULTIPLIER + voffset;\r
+ \r
+ //System.err.println("("+x0+","+y0+") -> ("+x1+","+y1+") -> ("+x2+","+y2+")");\r
+\r
+ double dx0 = x1 - x0;\r
+ double dy0 = y1 - y0;\r
+ double dx1 = x1 - x2;\r
+ double dy1 = y1 - y2;\r
+ double dx = x2 - x0;\r
+ double dy = y2 - y0;\r
+\r
+ // length of (p1-p0) x (p2-p0)\r
+ double dd = dx0*dy - dy0*dx; \r
+\r
+ if (Math.abs(dd) < 1e-5) {\r
+ // Points are (almost) collinear\r
+ return 0;\r
+ }\r
+ else {\r
+ // (p1-p0) * (p1-p2) / dd\r
+ double offset = (dx0*dx1 + dy0*dy1) / dd;\r
+ double angle = Math.PI*0.5 - Math.atan(offset);\r
+ if (dd > 0.0)\r
+ angle = angle - Math.PI;\r
+ return angle;\r
+ }\r
+ }\r
+\r
}\r
return x;\r
}\r
\r
- public double getX(double offset) {\r
- return x + offset;\r
- }\r
-\r
public int getY() {\r
return y;\r
}\r
\r
- public double getY(double offset) {\r
- return y + offset;\r
- }\r
-\r
public int getWidth() {\r
return width;\r
}\r
return height;\r
}\r
\r
+ // in vensim the coordinates refer to the center of the element with \r
+ // width and height being the distances between the center and the edges,\r
+ // whereas in sysdyn the coordinates refer to the top-left corner of the\r
+ // element\r
+ \r
+ public double getSysdyndX() {\r
+ return getSysdynLeftEdge();\r
+ }\r
+ \r
+ public double getSysdyndY() {\r
+ return getSysdynTopEdge();\r
+ }\r
+ \r
+ public double getSysdynWidth() {\r
+ return ((double)width) * 2 * SCALE_MULTIPLIER;\r
+ }\r
+ \r
+ public double getSysdynHeight() {\r
+ return ((double)height) * 2 * SCALE_MULTIPLIER;\r
+ }\r
+ \r
+ public double getSysdynTopEdge() {\r
+ return (double)(y - height) * SCALE_MULTIPLIER;\r
+ }\r
+ \r
+ public double getSysdynBottomEdge() {\r
+ return (double)(y + height) * SCALE_MULTIPLIER;\r
+ }\r
+\r
+ public double getSysdynLeftEdge() {\r
+ return (double)(x - width) * SCALE_MULTIPLIER;\r
+ }\r
+\r
+ public double getSysdynRightEdge() {\r
+ return (double)(x + width) * SCALE_MULTIPLIER;\r
+ }\r
+ \r
public boolean isAttached() {\r
return attached;\r
}\r
return textLine;\r
}\r
\r
- public abstract Element2 getModelElement(double xOffset, double yOffset);\r
+ public abstract Element2 getModelElement(double hOffset, double vOffset);\r
\r
}\r
package org.simantics.sysdyn.modelImport.model;\r
\r
public abstract class SketchObject {\r
+ \r
+ protected static final double SCALE_MULTIPLIER = 0.4;\r
\r
private int id;\r
\r
- SketchObject(int id) {\r
+ public SketchObject(int id) {\r
this.id = id;\r
}\r
\r
package org.simantics.sysdyn.modelImport.model;\r
\r
+import org.simantics.sysdyn.modelImport.model.Valve2.TextPosition;\r
+\r
public class SketchValve extends SketchElement {\r
\r
private SketchVariable variable;\r
+ private TextPosition textPosition;\r
\r
- public SketchValve(int id) {\r
+ public SketchValve(int id, TextPosition textPosition) {\r
super(id);\r
+ this.textPosition = textPosition;\r
}\r
\r
public SketchVariable getAttachedVariable() {\r
}\r
\r
@Override\r
- public Element2 getModelElement(double xOffset, double yOffset) {\r
- return new Valve2(getX(xOffset), getY(yOffset), variable.getVariable());\r
+ public Element2 getModelElement(double hOffset, double vOffset) {\r
+ return new Valve2(\r
+ getSysdyndX() + hOffset, \r
+ getSysdyndY() + vOffset, \r
+ getSysdynWidth(),\r
+ getSysdynHeight(), \r
+ variable.getVariable(),\r
+ textPosition);\r
}\r
\r
}\r
}\r
\r
@Override\r
- public Element2 getModelElement(double xOffset, double yOffset) {\r
+ public Element2 getModelElement(double hOffset, double vOffset) {\r
if (variable.getExpression() instanceof IntegralExpression) {\r
- return new Stock2(getX(xOffset), getY(yOffset), variable);\r
+ return new Stock2(\r
+ getSysdyndX() + hOffset, \r
+ getSysdyndY() + vOffset, \r
+ getSysdynWidth(),\r
+ getSysdynHeight(),\r
+ variable);\r
}\r
else {\r
- return new Auxiliary2(getX(xOffset), getY(yOffset), variable);\r
+ return new Auxiliary2(\r
+ getSysdyndX() + hOffset, \r
+ getSysdyndY() + vOffset, \r
+ getSysdynWidth(),\r
+ getSysdynHeight(),\r
+ variable);\r
}\r
}\r
\r
\r
public class Stock2 extends ModelVariable {\r
\r
- public Stock2(double x, double y, \r
+ public Stock2(double x, double y, double w, double h,\r
String name, IntegralExpression expression, Range range, String unit, String description) {\r
- super(x, y, name, expression, range, unit, description);\r
+ super(x, y, w, h, name, expression, range, unit, description);\r
}\r
\r
- public Stock2(double x, double y, Variable2 variable) {\r
- super(x, y, variable);\r
+ public Stock2(double x, double y, double w, double h, Variable2 variable) {\r
+ super(x, y, w, h, variable);\r
}\r
\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource stock = createVariable(graph, sr.Stock, parent);\r
- createSymbol(graph, sr.StockSymbol, stock, parent);\r
+ Resource stock = createVariable(graph, sr.Stock, parent, context);\r
+ createSymbol(graph, sr.StockSymbol, stock, parent, context);\r
\r
setResource(stock);\r
}\r
INSIDE, BELOW, LEFT, ABOVE, RIGHT, UNSET\r
}\r
\r
- public Valve2(double x, double y, \r
- String name, IExpression expression, Range range, String unit, String description) {\r
- super(x, y, name, expression, range, unit, description);\r
+ private TextPosition position;\r
+ \r
+ public Valve2(double x, double y, double w, double h,\r
+ String name, IExpression expression, Range range, String unit, String description, TextPosition position) {\r
+ super(x, y, w, h, name, expression, range, unit, description);\r
+ this.position = position;\r
}\r
\r
- public Valve2(double x, double y, Variable2 variable) {\r
- super(x, y, variable);\r
+ public Valve2(double x, double y, double w, double h, Variable2 variable, TextPosition position) {\r
+ super(x, y, w, h, variable);\r
+ this.position = position;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource valve = createVariable(graph, sr.Valve, parent);\r
- createSymbol(graph, sr.ValveSymbol, valve, parent);\r
+ Resource valve = createVariable(graph, sr.Valve, parent, context);\r
+ Resource symbol = createSymbol(graph, sr.ValveSymbol, valve, parent, context);\r
+ \r
+ Resource location = null;\r
+ switch (position) {\r
+ case BELOW:\r
+ location = sr.Bottom;\r
+ break;\r
+ case LEFT:\r
+ location = sr.Left;\r
+ break;\r
+ case ABOVE:\r
+ location = sr.Top;\r
+ break;\r
+ case RIGHT:\r
+ location = sr.Right;\r
+ break;\r
+ default:\r
+ System.err.println("unrecognized text location");\r
+ location = sr.Bottom;\r
+ break;\r
+ }\r
+ \r
+ graph.claim(symbol, sr.ValveSymbol_textLocation, location);\r
\r
setResource(valve);\r
}\r
--- /dev/null
+package org.simantics.sysdyn.modelImport.model;\r
+\r
+public class WriteContext {\r
+ \r
+ private long objectCount;\r
+ \r
+ private long cloudCount;\r
+ private long shadowCount;\r
+ private long dependencyCount;\r
+ private long flowCount;\r
+ \r
+ public WriteContext() {\r
+ this.objectCount = 0;\r
+ }\r
+ \r
+ public String getNextObject() {\r
+ return Long.toString(objectCount++);\r
+ }\r
+ \r
+ public long getObjectCount() {\r
+ return objectCount;\r
+ }\r
+ \r
+ public String getNextCloud() {\r
+ return "Cloud" + cloudCount++;\r
+ }\r
+ \r
+ public String getNextShadow() {\r
+ return "Shadow" + shadowCount++;\r
+ }\r
+ \r
+ public String getNextDependency() {\r
+ return "Dependency" + dependencyCount++;\r
+ }\r
+ \r
+ public String getNextFlow() {\r
+ return "Flow" + flowCount++;\r
+ }\r
+\r
+}\r