import java.util.ArrayList;\r
import java.util.HashMap;\r
\r
-import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon;\r
+import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon;\r
import org.simantics.sysdyn.modelImport.mdl.MdlModel;\r
import org.simantics.sysdyn.modelImport.mdl.Sketch;\r
import org.simantics.sysdyn.modelImport.mdl.SketchComment;\r
import org.simantics.sysdyn.modelImport.mdl.SketchObject;\r
import org.simantics.sysdyn.modelImport.mdl.SketchValve;\r
import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
-import org.simantics.sysdyn.modelImport.mdl.Subscript2;\r
+import org.simantics.sysdyn.modelImport.mdl.Subscript;\r
import org.simantics.sysdyn.modelImport.model.Model;\r
import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
import org.simantics.sysdyn.modelImport.model.element.Cloud;\r
import org.simantics.sysdyn.modelImport.model.element.Comment;\r
import org.simantics.sysdyn.modelImport.model.element.Connection;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
import org.simantics.sysdyn.modelImport.model.element.Shadow;\r
import org.simantics.sysdyn.modelImport.model.element.Stock;\r
import org.simantics.sysdyn.modelImport.model.element.Valve;\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
\r
public class MdlParser {\r
}\r
\r
// generate a sysdyn model from the mdl model\r
- Model model = new Model(file.getName());\r
+ Model model = new Model(mdl.getName());\r
\r
double offset = 0;\r
\r
// do this in several passes\r
\r
- HashMap<Variable, Element> variableToElement = new HashMap<Variable, Element>();\r
- ArrayList<SketchVariable> shadows = new ArrayList<SketchVariable>();\r
- \r
- for (Subscript2 ss : mdl.getSubscripts()) {\r
- System.err.println("SUBSCRIPT "+ss.getName()+": "+ss.getValues());\r
- }\r
- \r
+ HashMap<Variable, Symbol> variableToElement = new HashMap<Variable, Symbol>();\r
+\r
// add sketch labels and independent elements\r
for (Sketch sketch : mdl.getSketches()) {\r
sketch.setEdges();\r
continue;\r
}\r
\r
- Element modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
+ Symbol modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset);\r
model.addElement(modelElement);\r
sketch.elements.put(comment.getId(), modelElement);\r
}\r
\r
for (SketchValve valve : sketch.getValves()) {\r
- Element modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset);\r
+ Symbol 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
continue;\r
}\r
\r
- Element modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
+ Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
model.addElement(modelElement);\r
sketch.elements.put(variable.getId(), modelElement);\r
variableToElement.put(variable.getVariable(), modelElement);\r
for (SketchVariable variable : sketch.getVariables()) {\r
if (!variable.allowsIn()) {\r
// the variable is a shadow variable\r
- Element original = variableToElement.get(variable.getVariable());\r
+ Symbol original = variableToElement.get(variable.getVariable());\r
if (original == null) {\r
- System.err.println("original not found");\r
- continue;\r
+ System.err.println("original not found for "+variable.getVariable().getName());\r
+ Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset);\r
+ model.addElement(modelElement);\r
+ sketch.elements.put(variable.getId(), modelElement);\r
+ variableToElement.put(variable.getVariable(), modelElement);\r
+ } \r
+ else {\r
+ Shadow modelElement = new Shadow(\r
+ variable.getSysdyndX() + sketch.hOffset, \r
+ variable.getSysdyndY() + sketch.vOffset, \r
+ variable.getSysdynWidth(),\r
+ variable.getSysdynHeight(),\r
+ (ModelVariable)variableToElement.get(variable.getVariable()));\r
+ model.addShadow(modelElement);\r
+ sketch.elements.put(variable.getId(), modelElement);\r
}\r
- Shadow modelElement = new Shadow(\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
for (SketchConnection connection : sketch.getConnections()) {\r
- Element head = sketch.elements.get(connection.getTo());\r
- Element tail = sketch.elements.get(connection.getFrom());\r
+ Symbol head = sketch.elements.get(connection.getTo());\r
+ Symbol tail = sketch.elements.get(connection.getFrom());\r
Connection c = connection.getWriteableConnection(head, tail, sketch.vOffset);\r
if (c != null) {\r
model.addConnection(c);\r
throws IOException {\r
MdlModel mdl = new MdlModel(file.getName());\r
\r
+ // TODO: do this somewhere else and more controlled\r
+ mdl.addVariable(new Variable("Time", new NormalExpression("time"), "", null, ""), null);\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
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
+ // START READING VARIABLE DATA\r
\r
- reader.close();\r
- return mdl;\r
- }\r
-\r
- private static String parseVariables(BufferedReader reader, String line, MdlModel mdl) \r
- throws IOException {\r
+ // keep track of the current category\r
String category = null;\r
\r
do {\r
- if (line.isEmpty())\r
+ // skip empty lines\r
+ if (line.isEmpty()) \r
continue;\r
\r
+ // combine the (possible) variable declaration into one line \r
StringBuilder buffer = new StringBuilder();\r
- \r
do {\r
- if (line.endsWith("\\"))\r
- buffer.append(line.substring(0, line.length()-1));\r
- else\r
- buffer.append(line);\r
-\r
+ // strip trailing '\' if present\r
+ buffer.append(line.endsWith("\\") ? line.substring(0, line.length()-1) : line);\r
+ // each variable declaration ends with '|'\r
if (line.endsWith("|"))\r
break;\r
} while ((line = reader.readLine()) != null);\r
- \r
String str = buffer.toString();\r
\r
- String cat = MdlUtils.getPossibleCategory(str);\r
- if (cat != null) {\r
+ String cat;\r
+ Variable var;\r
+ Subscript sub;\r
+\r
+ // parse the (possible) variable declaration\r
+ if ((cat = MdlUtil.getPossibleCategory(str)) != null) {\r
category = cat;\r
- continue;\r
}\r
-\r
- Variable var = MdlUtils.getPossibleVariable(str, category);\r
- if (var != null) {\r
+ else if ((var = MdlUtil.getPossibleVariable(str, category)) != null) {\r
mdl.addVariable(var, category);\r
- continue;\r
}\r
- \r
- Variable ss = MdlUtils.getPossibleSubscriptVariable(str, category, mdl);\r
- if (ss != null) {\r
- Variable original = mdl.getVariable(ss.getName());\r
- if (original == null) {\r
- mdl.addVariable(ss, category);\r
- }\r
- else {\r
- original.mergeExpression(ss);\r
- }\r
- continue;\r
+ else if ((var = MdlUtil.getPossibleSubscriptVariable(str, category, mdl)) != null) {\r
+ Variable orig = mdl.getVariable(var.getName());\r
+ if (orig != null)\r
+ orig.mergeWithVariable(var);\r
+ else\r
+ mdl.addVariable(var, category);\r
}\r
- \r
- Variable lu = MdlUtils.getPossibleLookUpVariable(str, category);\r
- if (lu != null) {\r
- System.err.println("found lookup variable "+lu.getName());\r
- continue;\r
+ else if ((var = MdlUtil.getPossibleLookUpVariable(str, category)) != null) {\r
+ mdl.addVariable(var, category);\r
}\r
- \r
- Variable plain = MdlUtils.getPossibleNoExpressionVariable(str);\r
- if (plain != null) {\r
- System.err.println("found plain variable "+plain.getName());\r
- continue;\r
+ else if ((var = MdlUtil.getPossibleNoExpressionVariable(str)) != null) {\r
+ mdl.addVariable(var, category);\r
}\r
- \r
- Subscript2 subscript = MdlUtils.getPossibleSubscript(str, mdl);\r
- if (subscript != null) {\r
- mdl.addSubscript(subscript);\r
- continue;\r
+ else if ((sub = MdlUtil.getPossibleSubscript(str, mdl)) != null) {\r
+ mdl.addSubscript(sub);\r
+ }\r
+ else {\r
+ // if we got this far, the variable could not be parsed\r
+ System.err.println("unrecognized variable "+str);\r
}\r
\r
- // if we got this far, the variable could not be parsed\r
- System.err.println("unrecognized variable "+str);\r
} while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START));\r
\r
- return line;\r
- }\r
+ // END READING VARIABLE DATA\r
\r
- private static String parseSketches(BufferedReader reader, String line, MdlModel mdl) \r
- throws IOException {\r
+ if (line == null) {\r
+ System.err.println("unexpected end of file");\r
+ reader.close();\r
+ return null;\r
+ }\r
+\r
+ // START READING SKETCH DATA\r
+\r
+ // keep track of the current sketch\r
Sketch sketch = null;\r
\r
do {\r
+ // skip empty lines\r
if (line.isEmpty())\r
continue;\r
- \r
+\r
SketchObject so;\r
- \r
+\r
if (line.startsWith(SKETCH_START)) {\r
sketch = new Sketch();\r
mdl.addSketch(sketch);\r
else if (line.startsWith("$")) {\r
// font declaration, nothing to do here\r
}\r
- else if ((so = MdlUtils.getPossibleSketchConnection(line)) != null) {\r
+ else if ((so = MdlUtil.getPossibleSketchConnection(line)) != null) {\r
sketch.addConnection((SketchConnection)so);\r
}\r
- else if ((so = MdlUtils.getPossibleSketchVariable(line, mdl)) != null) {\r
- sketch.addVariable((SketchVariable)so);\r
+ else if ((so = MdlUtil.getPossibleSketchVariable(line, mdl)) != null) {\r
+ SketchVariable variable = (SketchVariable)so;\r
+ sketch.addVariable(variable);\r
}\r
- else if ((so = MdlUtils.getPossibleSketchValve(line)) != null) {\r
+ else if ((so = MdlUtil.getPossibleSketchValve(line)) != null) {\r
SketchValve valve = (SketchValve)so;\r
// the next row after a valve should always the variable associated with the valve\r
- SketchVariable attached = MdlUtils.getPossibleSketchVariable(reader.readLine(), mdl);\r
+ SketchVariable attached = MdlUtil.getPossibleSketchVariable(reader.readLine(), mdl);\r
if (attached == null || !attached.isAttached()) {\r
System.err.println("attached variable not found for valve");\r
}\r
sketch.addValve((SketchValve)valve);\r
sketch.addVariable(attached);\r
}\r
- else if ((so = MdlUtils.getPossibleSketchComment(line)) != null) {\r
+ else if ((so = MdlUtil.getPossibleSketchComment(line)) != null) {\r
SketchComment comment = (SketchComment)so;\r
if (comment.hasTextLine()) {\r
comment.setText(reader.readLine());\r
// if we got this far, the element could not be parsed\r
System.err.println("unrecognized element "+line);\r
}\r
- \r
+\r
} while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END));\r
\r
- return line;\r
+ // END READING SKETCH DATA\r
+\r
+ if (line == null) {\r
+ System.err.println("unexpected end of file");\r
+ reader.close();\r
+ return null;\r
+ }\r
+\r
+ // START READING OTHER DATA\r
+\r
+ do {\r
+ // TODO: is there anything relevant here?\r
+ } while ((line = reader.readLine()) != null);\r
+\r
+ // END READING OTHER DATA\r
+\r
+ reader.close();\r
+ return mdl;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport;\r
\r
+import java.util.ArrayList;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
import org.simantics.sysdyn.modelImport.mdl.SketchElement;\r
import org.simantics.sysdyn.modelImport.mdl.SketchValve;\r
import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
-import org.simantics.sysdyn.modelImport.mdl.Subscript2;\r
+import org.simantics.sysdyn.modelImport.mdl.Subscript;\r
import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition;\r
import org.simantics.sysdyn.modelImport.model.expression.DelayExpression;\r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
import org.simantics.sysdyn.modelImport.model.expression.LookupExpression;\r
import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;\r
-import org.simantics.sysdyn.modelImport.model.expression.SubscriptExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
\r
-public class MdlUtils {\r
+public class MdlUtil {\r
\r
public enum ConnectionType {\r
ARROW, LINE_ARROW, LINE_SEGMENT, OTHER\r
VAR_NAME+"\\s*=\\s*";\r
private static final String EQUATION_PATTERN =\r
"([^~]*?(?:"+VAR_NAME_QUOTED+"[^~]*?)*)\\s*~\\s*";\r
+ \r
private static final String UNIT_PATTERN =\r
"([^~]*?)\\s*~\\s*";\r
private static final String DESC_PATTERN =\r
}\r
\r
String name = normalize(matcher.group(variableName));\r
- IExpression expression = parseEquation(matcher.group(variableEquation));\r
+ Expression expression = parseExpression(matcher.group(variableEquation));\r
\r
String unit = matcher.group(variableUnit);\r
Range range = parseRange(unit);\r
return null;\r
}\r
\r
- String name = matcher.group(subscriptVariableName);\r
+ String name = normalize(matcher.group(subscriptVariableName));\r
// TODO: find out if subscript indices can contain commas and update\r
// this method accordingly\r
String[] indices = matcher.group(subscriptVariableIndices).split(",");\r
- IExpression expression = parseEquation(matcher.group(subscriptVariableEquation));\r
- SubscriptExpression e = new SubscriptExpression(indices, expression);\r
+ Expression expression = parseExpression(matcher.group(subscriptVariableEquation));\r
+ EnumerationExpression e = new EnumerationExpression(indices, expression);\r
\r
String unit = matcher.group(subscriptVariableUnit);\r
Range range = parseRange(unit);\r
return null;\r
}\r
\r
- String name = matcher.group(lookupVariableName);\r
+ String name = normalize(matcher.group(lookupVariableName));\r
double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin));\r
double xMax = Double.parseDouble(matcher.group(lookupVariableRangeYMin));\r
double yMin = Double.parseDouble(matcher.group(lookupVariableRangeXMax));\r
double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax));\r
- String points = matcher.group(lookupVariablePoints);\r
+ double[] points = parseLookup(matcher.group(lookupVariablePoints));\r
\r
LookupExpression e = new LookupExpression(xMin, yMin, xMax, yMax, points);\r
\r
return new Variable(name, e, unit, range, description);\r
}\r
\r
+ private static double[] parseLookup(String points) {\r
+ // a hack to remove the leading empty string otherwise produced by split()\r
+ points = points.replaceFirst("[(),]+", "");\r
+ String[] parts = points.split("[(),]+");\r
+ double[] result = new double[parts.length];\r
+ for (int i = 0; i < parts.length; i++) {\r
+ result[i] = Double.parseDouble(parts[i]);\r
+ }\r
+ return result;\r
+ }\r
+ \r
public static final String NO_EXP_PATTERN = \r
VAR_NAME+"\\s*~\\s*";\r
\r
private static final int subscriptType = 2;\r
private static final int subscriptEquation = 3;\r
\r
- public static Subscript2 getPossibleSubscript(String line, MdlModel mdl) {\r
+ public static Subscript getPossibleSubscript(String line, MdlModel mdl) {\r
Matcher matcher = Pattern.compile(subscriptPattern).matcher(line);\r
\r
if (!matcher.matches()) {\r
return null;\r
}\r
\r
- String name = matcher.group(subscriptName);\r
+ String name = normalize(matcher.group(subscriptName));\r
boolean equivalence = matcher.group(subscriptType).equals("<->");\r
String expression = matcher.group(subscriptEquation);\r
\r
if (equivalence) {\r
- return new Subscript2(name, mdl.getSubscript(expression));\r
+ return new Subscript(name, mdl.getSubscript(normalize(expression)));\r
}\r
else {\r
- return new Subscript2(name, expression.split(","));\r
+ return new Subscript(name, expression.split(","));\r
}\r
}\r
\r
- private static IExpression parseEquation(String equation) {\r
- if (equation.startsWith("INTEG")) {\r
- return parseIntegralExpression(equation);\r
+ private static Expression parseExpression(String equation) {\r
+ Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(equation);\r
+\r
+ if (!matcher.matches()) {\r
+ return new NormalExpression(normalize(equation));\r
+ }\r
+ \r
+ String function = matcher.group(1);\r
+ String[] parameters = splitParameters(matcher.group(2));\r
+ \r
+ if (function.startsWith("INTEG")) {\r
+ if (parameters.length != 2) {\r
+ System.err.println("malformed integral expression: "+equation);\r
+ return null;\r
+ }\r
+ String integral = normalize(parameters[0]);\r
+ String initial = normalize(parameters[1]);\r
+ return new IntegralExpression(integral, initial);\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
+ else if (function.startsWith("DELAY N")) {\r
+ if (parameters.length != 4) {\r
+ System.err.println("malformed delay expression: "+equation);\r
+ return null;\r
+ }\r
+ String expression = normalize(parameters[0]);\r
+ String time = normalize(parameters[1]);\r
+ String initial = normalize(parameters[2]);\r
+ int degree = Integer.parseInt(parameters[3]);\r
+ return new DelayExpression(expression, time, initial, degree);\r
+ }\r
+ else if (function.startsWith("GAME")) {\r
+ if (parameters.length != 1) {\r
+ System.err.println("malformed game expression: "+equation);\r
+ return null;\r
+ }\r
+ // game expressions are currently not supported\r
+ return new NormalExpression(normalize(parameters[0]));\r
}\r
else {\r
- return parseNormalExpression(equation);\r
+ // the function does not require special handling\r
+ return new NormalExpression(normalize(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
+ private static String[] splitParameters(String str) {\r
+ ArrayList<String> list = new ArrayList<String>();\r
+\r
+ int i;\r
+ int last = 0;\r
+ int level = 0;\r
+ boolean comment = false;\r
+ boolean brackets = false;\r
+ \r
+ for (i = 0; i < str.length(); i++) {\r
+ char current = str.charAt(i);\r
+ if (current == '"')\r
+ // note that this does not currently support escaped quotation marks inside quoted variable names\r
+ comment = !comment;\r
+ else if (current == '[' && !comment)\r
+ brackets = true;\r
+ else if (current == ']' && !comment)\r
+ brackets = false;\r
+ else if (current == '(' && !comment && !brackets)\r
+ level++;\r
+ else if (current == ')' && !comment && !brackets)\r
+ level--;\r
+ else if (current == ',' && !comment && !brackets && level == 0) {\r
+ list.add(str.substring(last, i).trim());\r
+ last = i + 1;\r
+ }\r
+ }\r
+ if (last < i) {\r
+ list.add(str.substring(last, i).trim());\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
+ return list.toArray(new String[list.size()]);\r
}\r
\r
private static Range parseRange(String unit) {\r
}\r
}\r
\r
- private static final String categoryPattern = "\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*";\r
- \r
public static String getPossibleCategory(String line) {\r
- Matcher matcher = Pattern.compile(categoryPattern).matcher(line);\r
+ Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line);\r
\r
if (!matcher.matches()) {\r
return null;\r
private static final int connectionShape = 4;\r
private static final int connectionPoints = 5;\r
\r
- private static final String COMMON =\r
- // x, y, w, h, sh, bits,hid, hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
- SAVE+SAVE+SAVE+SAVE+SAVE+SAVE+SKIP+SKIP+SAVE+".*";\r
- \r
- public static final String sketchVariable =\r
- // n, id, name, x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
- "10,"+SAVE+VAR_NAME+","+COMMON;\r
- public static final String sketchValve =\r
- "11,"+SAVE+SAVE+ COMMON;\r
- public static final String sketchComment =\r
- "12,"+SAVE+SAVE+ COMMON;\r
- \r
- // group indices for later use\r
- private static final int elementId = 1;\r
- private static final int elementName = 2;\r
- private static final int elementX = 3;\r
- private static final int elementY = 4;\r
- private static final int elementWidth = 5;\r
- private static final int elementHeight = 6;\r
- private static final int elementShape = 7;\r
- private static final int elementBits = 8;\r
- private static final int elementTextPos = 9;\r
- \r
public static SketchConnection getPossibleSketchConnection(String line) {\r
Matcher matcher = Pattern.compile(sketchConnection).matcher(line);\r
\r
return array;\r
}\r
\r
+ private static final String COMMON =\r
+ // x, y, w, h, sh, bits,hid, hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
+ SAVE+SAVE+SAVE+SAVE+SAVE+SAVE+SKIP+SKIP+SAVE+".*";\r
+ \r
+ public static final String sketchVariable =\r
+ // n, id, name, x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
+ "10,"+SAVE+VAR_NAME+","+COMMON;\r
+ public static final String sketchValve =\r
+ "11,"+SAVE+SAVE+ COMMON;\r
+ public static final String sketchComment =\r
+ "12,"+SAVE+SAVE+ COMMON;\r
+ \r
+ // group indices for later use\r
+ private static final int elementId = 1;\r
+ private static final int elementName = 2;\r
+ private static final int elementX = 3;\r
+ private static final int elementY = 4;\r
+ private static final int elementWidth = 5;\r
+ private static final int elementHeight = 6;\r
+ private static final int elementShape = 7;\r
+ private static final int elementBits = 8;\r
+ private static final int elementTextPos = 9;\r
+ \r
public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) {\r
Matcher matcher = Pattern.compile(sketchVariable).matcher(line);\r
\r
\r
int id = Integer.parseInt(matcher.group(elementId));\r
Variable var = mdl.getVariable((normalize(matcher.group(elementName))));\r
+ if (var == null) {\r
+ System.err.println("could not find variable corresponding to "+normalize(matcher.group(elementName)));\r
+ }\r
\r
SketchVariable variable = new SketchVariable(id, var);\r
\r
// method above)\r
str = str.replaceAll("\t", "");\r
\r
- // remove inline :AND: and :OR: etc (not done currently)\r
+ // remove inline operations\r
str = removeInlineOperation(str, "AND");\r
str = removeInlineOperation(str, "OR");\r
\r
// this should work in most cases, even with nested parentheses \r
// in the left and right hand side expression\r
\r
+ // TODO: does not work in all cases\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
package org.simantics.sysdyn.modelImport.mdl;\r
\r
import java.util.ArrayList;\r
-import java.util.Collections;\r
import java.util.HashMap;\r
import java.util.List;\r
\r
\r
private HashMap<String, Variable> variables;\r
private HashMap<String, ArrayList<Variable>> groups;\r
- \r
- private HashMap<String, Subscript2> subscripts;\r
- \r
+ private HashMap<String, Subscript> subscripts;\r
private ArrayList<Sketch> sketches;\r
\r
public MdlModel(String name) {\r
this.name = name;\r
+ \r
this.variables = new HashMap<String, Variable>();\r
this.groups = new HashMap<String, ArrayList<Variable>>();\r
- this.subscripts = new HashMap<String, Subscript2>();\r
+ this.subscripts = new HashMap<String, Subscript>();\r
this.sketches = new ArrayList<Sketch>();\r
}\r
\r
+ public String getName() {\r
+ return name;\r
+ }\r
+ \r
public void addVariable(Variable variable, String group) {\r
if (variables.get(variable.getName()) != null) {\r
System.err.println("warning, duplicate variable "+variable.getName());\r
return new ArrayList<Variable>(variables.values());\r
}\r
\r
- public void addSubscript(Subscript2 subscript) {\r
+ public void addSubscript(Subscript subscript) {\r
subscripts.put(subscript.getName(), subscript);\r
}\r
\r
- public Subscript2 getSubscript(String name) {\r
+ public Subscript getSubscript(String name) {\r
return subscripts.get(name);\r
}\r
\r
- public List<Subscript2> getSubscripts() {\r
- return new ArrayList<Subscript2>(subscripts.values());\r
+ public List<Subscript> getSubscripts() {\r
+ return new ArrayList<Subscript>(subscripts.values());\r
}\r
\r
public void addSketch(Sketch sketch) {\r
import java.util.HashMap;\r
import java.util.List;\r
\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
\r
public class Sketch {\r
\r
private String name;\r
\r
+ private boolean edgesOutOfDate;\r
+ public double topEdge = 0;\r
+ public double bottomEdge = 0;\r
+ public double leftEdge = 0;\r
+ public double rightEdge = 0;\r
+\r
private ArrayList<SketchComment> comments;\r
private ArrayList<SketchConnection> connections;\r
private ArrayList<SketchValve> valves;\r
private ArrayList<SketchVariable> variables;\r
\r
public Sketch() {\r
+ edgesOutOfDate = true;\r
+ \r
comments = new ArrayList<SketchComment>();\r
connections = new ArrayList<SketchConnection>();\r
valves = new ArrayList<SketchValve>();\r
this.name = name;\r
}\r
\r
+ private void updateEdges() {\r
+ if (edgesOutOfDate) {\r
+ boolean first = true;\r
+ for (SketchElement e : getAllElements()) {\r
+ topEdge = first ? e.getSysdynTopEdge() : Math.min(topEdge, e.getSysdynTopEdge());\r
+ bottomEdge = first ? e.getSysdynBottomEdge() : Math.max(bottomEdge, e.getSysdynBottomEdge());\r
+ leftEdge = first ? e.getSysdynLeftEdge() : Math.min(leftEdge, e.getSysdynLeftEdge());\r
+ rightEdge = first ? e.getSysdynRightEdge() : Math.max(rightEdge, e.getSysdynRightEdge());\r
+ first = false;\r
+ }\r
+ edgesOutOfDate = false;\r
+ }\r
+ }\r
+ \r
+ public double getTopEdge() {\r
+ updateEdges();\r
+ return topEdge;\r
+ }\r
+ \r
+ public double getBottomEdge() {\r
+ updateEdges();\r
+ return bottomEdge;\r
+ }\r
+ \r
+ public double getLeftEdge() {\r
+ updateEdges();\r
+ return leftEdge;\r
+ }\r
+ \r
+ public double getRightEdge() {\r
+ updateEdges();\r
+ return rightEdge;\r
+ }\r
+ \r
+ public double getWidth() {\r
+ updateEdges();\r
+ return rightEdge - leftEdge;\r
+ }\r
+ \r
+ public double getHeight() {\r
+ updateEdges();\r
+ return bottomEdge - topEdge;\r
+ }\r
+ \r
+ public void addObject(SketchComment comment) {\r
+ \r
+ }\r
+ \r
public void addComment(SketchComment comment) {\r
comments.add(comment);\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, Element> elements = new HashMap<Integer, Element>();\r
+ public HashMap<Integer, Symbol> elements = new HashMap<Integer, Symbol>();\r
\r
public void setEdges() {\r
boolean first = true;\r
package org.simantics.sysdyn.modelImport.mdl;\r
\r
-import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon;\r
+import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon;\r
import org.simantics.sysdyn.modelImport.model.element.Cloud;\r
import org.simantics.sysdyn.modelImport.model.element.Comment;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
\r
public class SketchComment extends SketchElement {\r
\r
return icon;\r
}\r
\r
- public Element getModelElement(double hOffset, double vOffset) {\r
+ public Symbol getModelElement(double hOffset, double vOffset) {\r
if (icon.equals(CommentIcon.CLOUD)) {\r
return new Cloud(\r
getSysdyndX() + hOffset, \r
package org.simantics.sysdyn.modelImport.mdl;\r
\r
-import org.simantics.sysdyn.modelImport.MdlUtils.ConnectionType;\r
+import org.simantics.sysdyn.modelImport.MdlUtil.ConnectionType;\r
import org.simantics.sysdyn.modelImport.model.element.Connection;\r
import org.simantics.sysdyn.modelImport.model.element.Dependency;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
import org.simantics.sysdyn.modelImport.model.element.Flow;\r
\r
public class SketchConnection extends SketchObject {\r
return type;\r
}\r
\r
- public Connection getWriteableConnection(Element head, Element tail, double offset) {\r
+ public Connection getWriteableConnection(Symbol head, Symbol tail, double offset) {\r
if (type.equals(ConnectionType.ARROW)) {\r
return new Dependency(head, tail, false, false, getSysdynAngle(tail, head, offset));\r
}\r
}\r
\r
// TODO: comment this?\r
- public double getSysdynAngle(Element from, Element to, double voffset) {\r
+ public double getSysdynAngle(Symbol from, Symbol to, double voffset) {\r
if (points == null || points.length == 0) {\r
return 0;\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
+ if (Math.abs(dd) < 0.01) {\r
// Points are (almost) collinear\r
return 0;\r
}\r
package org.simantics.sysdyn.modelImport.mdl;\r
\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
\r
public abstract class SketchElement extends SketchObject {\r
\r
return textLine;\r
}\r
\r
- public abstract Element getModelElement(double hOffset, double vOffset);\r
+ public abstract Symbol getModelElement(double hOffset, double vOffset);\r
\r
}\r
package org.simantics.sysdyn.modelImport.mdl;\r
\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
import org.simantics.sysdyn.modelImport.model.element.Valve;\r
import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition;\r
\r
}\r
\r
@Override\r
- public Element getModelElement(double hOffset, double vOffset) {\r
+ public Symbol getModelElement(double hOffset, double vOffset) {\r
return new Valve(\r
getSysdyndX() + hOffset, \r
getSysdyndY() + vOffset, \r
package org.simantics.sysdyn.modelImport.mdl;\r
\r
import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
import org.simantics.sysdyn.modelImport.model.element.Stock;\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
}\r
\r
@Override\r
- public Element getModelElement(double hOffset, double vOffset) {\r
+ public Symbol getModelElement(double hOffset, double vOffset) {\r
if (variable.getExpression() instanceof IntegralExpression) {\r
return new Stock(\r
getSysdyndX() + hOffset, \r
import java.util.ArrayList;\r
import java.util.Arrays;\r
\r
-public class Subscript2 {\r
+public class Subscript {\r
+ \r
+ // TODO: THIS IS PROBABLY NOT NEEDED AT ALL, COULD USE ENUMERATION DIRECTLY\r
\r
private String name;\r
private ArrayList<String> values;\r
\r
- public Subscript2(String name, String...values) {\r
+ public Subscript(String name, String...values) {\r
this.name = name;\r
this.values = new ArrayList<String>(Arrays.asList(values));\r
}\r
\r
- public Subscript2(String name, Subscript2 other) {\r
+ public Subscript(String name, Subscript other) {\r
this.name = name;\r
this.values = other.getValues();\r
}\r
public interface IWriteableObject {\r
\r
/**\r
- * @param graph WriteGraph\r
- * @param parent The resource where the object is located\r
+ * Write this object to the graph and return the corresponding resource.\r
+ * \r
+ * @param graph \r
+ * @param parent \r
+ * @param context\r
+ * @return Resource\r
* @throws DatabaseException \r
*/\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException;\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException;\r
+ \r
+ /**\r
+ * Return the resource corresponding to this object or null if this object has not been written yet.\r
+ * \r
+ * @return Resource\r
+ */\r
+ public Resource getResource();\r
}\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.sysdyn.modelImport.model.element.Connection;\r
-import org.simantics.sysdyn.modelImport.model.element.Element;\r
+import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
import org.simantics.sysdyn.modelImport.model.element.Shadow;\r
import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
private HashMap<String, Enumeration> enumerations;\r
private HashMap<String, ModelVariable> variables;\r
\r
- private ArrayList<Element> elements;\r
+ private ArrayList<Symbol> elements;\r
private ArrayList<Shadow> shadows;\r
private ArrayList<Connection> connections;\r
\r
+ private Resource model;\r
+ \r
public Model(String name) {\r
this.name = name;\r
\r
enumerations = new HashMap<String, Enumeration>();\r
variables = new HashMap<String, ModelVariable>();\r
\r
- elements = new ArrayList<Element>();\r
+ elements = new ArrayList<Symbol>();\r
shadows = new ArrayList<Shadow>();\r
connections = new ArrayList<Connection>();\r
}\r
return variables.values();\r
}\r
\r
- public void addElement(Element element) {\r
+ public void addElement(Symbol element) {\r
elements.add(element);\r
}\r
\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ public Resource 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
+ model = ModelUtils.createModel(graph);\r
graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING);\r
\r
// TODO: set simulation parameters\r
e.write(graph, configuration, context);\r
}\r
\r
- for (Element e : elements) {\r
+ for (Symbol e : elements) {\r
e.write(graph, configuration, context);\r
}\r
\r
\r
Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
graph.claimLiteral(diagram, dr.HasModCount, context.getObjectCount(), Bindings.LONG);\r
+ \r
+ return model;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getResource() {\r
+ return model;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model;\r
\r
+import java.util.HashMap;\r
+\r
+import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
+\r
public class WriteContext {\r
\r
private long objectCount;\r
private long dependencyCount;\r
private long flowCount;\r
\r
+ private HashMap<String, Enumeration> enumerations;\r
+ \r
public WriteContext() {\r
- this.objectCount = 0;\r
+ objectCount = 0;\r
+ \r
+ cloudCount = 0;\r
+ shadowCount = 0;\r
+ dependencyCount = 0;\r
+ flowCount = 0;\r
+ \r
+ enumerations = new HashMap<String, Enumeration>();\r
}\r
\r
public String getNextObject() {\r
public String getNextFlow() {\r
return "Flow" + flowCount++;\r
}\r
+ \r
+ // register an enumeration, necessary for enumeration expressions\r
+ public void registerEnumeration(Enumeration enumeration) {\r
+ \r
+ }\r
+ \r
+ // attempt to obtain a reference to an enumeration based on either the \r
+ // name of the enumeration or the name of one of the elements of the \r
+ // enumeration\r
+ // (TODO: this must be updated to something more clever if there are\r
+ // collisions between names of enumerations and elements of enumerations)\r
+ public Enumeration resolveEnumeration(String lookup) {\r
+ return null;\r
+ }\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
\r
public class Auxiliary extends ModelVariable {\r
\r
public Auxiliary(double x, double y, double w, double h,\r
- String name, IExpression expression, Range range, String unit, String description) {\r
+ String name, Expression expression, Range range, String unit, String description) {\r
super(x, y, w, h, name, expression, range, unit, description);\r
}\r
\r
public Auxiliary(double x, double y, double w, double h, Variable variable) {\r
super(x, y, w, h, variable);\r
}\r
-\r
- @Override\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, context);\r
- createSymbol(graph, sr.AuxiliarySymbol, variable, parent, context);\r
- \r
- setResource(variable);\r
- }\r
-\r
+ \r
@Override\r
- public boolean supportsDependencies() {\r
- return true;\r
+ public Resource getVariableType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).Auxiliary;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsFlows() {\r
- return false;\r
+ public Resource getSymbolType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).AuxiliarySymbol;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.modeling.ModelingResources;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class Cloud extends Element {\r
+public class Cloud extends Symbol {\r
+ \r
+ private Resource cloud;\r
\r
public Cloud(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, WriteContext context) throws DatabaseException {\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource cloud = GraphUtils.create2(graph, sr.Cloud,\r
+ cloud = GraphUtils.create2(graph, sr.Cloud,\r
l0.HasName, context.getNextCloud(),\r
l0.PartOf, parent);\r
\r
- createSymbol(graph, sr.CloudSymbol, cloud, parent, context);\r
+ Resource symbol = super.write(graph, parent, context);\r
+ graph.claim(symbol, mr.ElementToComponent, cloud);\r
\r
- setResource(cloud);\r
+ return cloud;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsDependencies() {\r
- return false;\r
+ public Resource getSymbolType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).CloudSymbol;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsFlows() {\r
- return true;\r
+ public Resource getResource() {\r
+ return cloud;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class Comment extends Element {\r
+public class Comment extends Symbol {\r
\r
private String text;\r
\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ public Resource 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, context);\r
+ Resource symbol = super.write(graph, parent, context);\r
\r
- graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING);\r
+ graph.claimLiteral(symbol, dr.HasText, text, Bindings.STRING);\r
\r
- setResource(comment);\r
+ return symbol;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsDependencies() {\r
- return false;\r
+ public Resource getSymbolType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).AdditionalSymbols_MultilineText;\r
}\r
\r
@Override\r
- public boolean supportsFlows() {\r
- return false;\r
+ public Resource getResource() {\r
+ // comments only have diagram resources\r
+ return getDiagramResource();\r
}\r
- \r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.common.utils.OrderedSetUtils;\r
\r
public abstract class Connection implements IWriteableObject {\r
\r
- protected Element head;\r
- protected Element tail;\r
+ protected Symbol head;\r
+ protected Symbol tail;\r
+ \r
+ private Resource connection;\r
+ private Resource diagramConnection;\r
\r
- public Connection(Element head, Element tail) {\r
+ public Connection(Symbol head, Symbol tail) {\r
this.head = head;\r
this.tail = tail;\r
}\r
-\r
- protected Resource createConnection(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
- throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph); \r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
- Resource connection = GraphUtils.create2(graph, type,\r
- sr.Variable_HasHead, head.getResource(),\r
- sr.Variable_HasTail, tail.getResource(),\r
- l0.PartOf, parent);\r
- graph.claim(connection, mr.Mapped, connection);\r
- \r
- return connection;\r
- }\r
\r
- protected Resource createSymbol(WriteGraph graph, Resource type, Resource connection, Resource parent, WriteContext context) \r
- throws DatabaseException {\r
+ @Override\r
+ public Resource 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
StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
+ connection = GraphUtils.create2(graph, getConnectionType(graph),\r
+ sr.Variable_HasHead, head.getResource(),\r
+ sr.Variable_HasTail, tail.getResource(),\r
+ l0.PartOf, parent);\r
+ graph.claim(connection, mr.Mapped, connection);\r
+ \r
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.HasTailTerminal, diagramTail);\r
+ sr.HasTailTerminal, tail.getDiagramResource());\r
\r
- Resource diagramHead = graph.getPossibleObject(head.getResource(), mr.ComponentToElement);\r
Resource headConnector = GraphUtils.create2(graph, dr.Connector,\r
- sr.HasHeadTerminal, diagramHead,\r
+ sr.HasHeadTerminal, head.getDiagramResource(),\r
dr.AreConnected, tailConnector);\r
\r
- Resource diagramConnection = GraphUtils.create2(graph, type,\r
+ diagramConnection = GraphUtils.create2(graph, getDiagramConnectionType(graph),\r
l0.HasName, context.getNextObject(),\r
l0.PartOf, diagram,\r
sr2.HasConnectionType, sr.SysdynConnectionType,\r
\r
OrderedSetUtils.add(graph, diagram, diagramConnection);\r
\r
+ return connection;\r
+ }\r
+ \r
+ public abstract Resource getConnectionType(ReadGraph graph);\r
+ public abstract Resource getDiagramConnectionType(ReadGraph graph);\r
+ \r
+ @Override\r
+ public Resource getResource() {\r
+ return connection;\r
+ }\r
+ \r
+ public Resource getDiagramResource() {\r
return diagramConnection;\r
}\r
\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
private boolean showDelay;\r
private double angle;\r
\r
- public Dependency(Element head, Element tail, boolean showArrow, boolean showDelay, double angle) {\r
+ public Dependency(Symbol head, Symbol tail, boolean showArrow, boolean showDelay, double angle) {\r
super(head, tail);\r
this.showArrow = showArrow;\r
this.showDelay = showDelay;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ public Resource 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
+ return null;\r
}\r
\r
Layer0 l0 = Layer0.getInstance(graph);\r
- \r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource dependency = createConnection(graph, sr.Dependency, parent, context);\r
+ Resource dependency = super.write(graph, 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
+ graph.claimLiteral(getDiagramResource(), sr.Dependency_angle, angle, Bindings.DOUBLE);\r
+ \r
+ return dependency;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getConnectionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).Dependency;\r
}\r
+ \r
+ @Override\r
+ public Resource getDiagramConnectionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).DependencyConnection;\r
+ }\r
+ \r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
\r
public class Flow extends Connection {\r
\r
- public Flow(Element head, Element tail) {\r
+ public Flow(Symbol head, Symbol tail) {\r
super(head, tail);\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ public Resource 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
+ return null;\r
}\r
\r
Layer0 l0 = Layer0.getInstance(graph);\r
\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- \r
- Resource flow = createConnection(graph, sr.Flow, parent, context);\r
+ Resource flow = super.write(graph, parent, context);\r
graph.claimLiteral(flow, l0.HasName, context.getNextFlow(), Bindings.STRING);\r
- Resource symbol = createSymbol(graph, sr.FlowConnection, flow, parent, context);\r
+ \r
+ return flow;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getConnectionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).Flow;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getDiagramConnectionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).FlowConnection;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
-import java.util.List;\r
-\r
import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
-import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
\r
-public abstract class ModelVariable extends Element {\r
+public abstract class ModelVariable extends Symbol {\r
\r
- protected String name;\r
- protected IExpression expression;\r
- protected Range range;\r
- protected String unit;\r
- protected String description;\r
+ private String name;\r
+ private Expression expression;\r
+ private Range range;\r
+ private String unit;\r
+ private String description;\r
\r
- protected List<Enumeration> enumerations;\r
+ private Resource variable;\r
\r
- public ModelVariable(double x, double y, double w, double h, String name, IExpression expression, Range range, String unit, String description) {\r
+ public ModelVariable(double x, double y, double w, double h,\r
+ String name, Expression expression, Range range, String unit, String description) {\r
super(x, y, w, h);\r
this.name = name;\r
this.expression = expression;\r
return name;\r
}\r
\r
- public Resource createVariable(WriteGraph graph, Resource type, Resource parent, WriteContext context) \r
- throws DatabaseException {\r
+ @Override\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
ModelingResources mr = ModelingResources.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource variable = GraphUtils.create2(graph, type, \r
+ variable = GraphUtils.create2(graph, getVariableType(graph), \r
l0.HasName, name,\r
l0.PartOf, parent);\r
graph.claim(variable, mr.Mapped, variable);\r
\r
- expression.write(graph, variable, context);\r
- \r
// TODO: write references to enumerations here?\r
\r
+ if (expression != null) {\r
+ Resource expressionList = expression.write(graph, variable, context);\r
+ }\r
if (range != null) {\r
range.write(graph, variable, context);\r
}\r
if (description != null && !description.isEmpty()) {\r
graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING);\r
}\r
+ \r
+ Resource symbol = super.write(graph, parent, context);\r
+ graph.claim(symbol, mr.ElementToComponent, variable);\r
\r
return variable;\r
}\r
+ \r
+ public abstract Resource getVariableType(ReadGraph graph);\r
+ \r
+ @Override\r
+ public Resource getResource() {\r
+ return variable;\r
+ }\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.modeling.ModelingResources;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class Shadow extends Element {\r
+public class Shadow extends Symbol {\r
\r
- private Element original;\r
+ private ModelVariable original;\r
\r
- public Shadow(double x, double y, double w, double h, Element original) {\r
+ private Resource shadow;\r
+ \r
+ public Shadow(double x, double y, double w, double h, ModelVariable original) {\r
super(x, y, w, h);\r
this.original = original;\r
}\r
\r
- public Shadow(SketchVariable variable, double hOffset, double vOffset, Element original) {\r
+ public Shadow(SketchVariable variable, double hOffset, double vOffset, ModelVariable original) {\r
super(variable, hOffset, vOffset);\r
this.original = original;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource shadow = GraphUtils.create2(graph, sr.Shadow,\r
+ 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, context);\r
+ Resource symbol = super.write(graph, parent, context);\r
+ graph.claim(symbol, mr.ElementToComponent, shadow);\r
\r
- setResource(shadow);\r
+ return shadow;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsDependencies() {\r
- return original.supportsDependencies();\r
+ public Resource getSymbolType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).ShadowSymbol;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsFlows() {\r
- return original.supportsFlows();\r
+ public Resource getResource() {\r
+ return shadow;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
super(x, y, w, h, variable);\r
}\r
\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, context);\r
- createSymbol(graph, sr.StockSymbol, stock, parent, context);\r
-\r
- setResource(stock);\r
- }\r
-\r
@Override\r
- public boolean supportsDependencies() {\r
- return true;\r
+ public Resource getVariableType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).Stock;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsFlows() {\r
- return true;\r
+ public Resource getSymbolType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).StockSymbol;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.common.utils.OrderedSetUtils;\r
import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public abstract class Element implements IWriteableObject {\r
+public abstract class Symbol implements IWriteableObject {\r
\r
private double x;\r
private double y;\r
private double width;\r
private double height;\r
\r
- private Resource resource;\r
+ private Resource symbol;\r
\r
- public Element(double x, double y, double width, double height) {\r
+ public Symbol(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 Element(SketchElement element, double hOffset, double vOffset) {\r
+ public Symbol(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
- return resource;\r
- }\r
-\r
- protected void setResource(Resource resource) {\r
- this.resource = resource;\r
- }\r
\r
public double getX() {\r
return x;\r
return height;\r
}\r
\r
- public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, Resource parent, WriteContext context) throws DatabaseException {\r
+ @Override\r
+ public Resource write(WriteGraph graph, 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
\r
Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
\r
- Resource symbol = GraphUtils.create2(graph, type,\r
+ symbol = GraphUtils.create2(graph, getSymbolType(graph),\r
l0.HasName, context.getNextObject(),\r
l0.PartOf, diagram);\r
- \r
- if (variable != null) {\r
- graph.claim(symbol, mr.ElementToComponent, variable);\r
- }\r
\r
double[] transform = { 1.0, 0.0, 0.0, 1.0, x, y };\r
graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform, Bindings.DOUBLE_ARRAY);\r
\r
OrderedSetUtils.add(graph, diagram, symbol);\r
-\r
+ \r
return symbol;\r
}\r
\r
- public abstract boolean supportsDependencies();\r
- public abstract boolean supportsFlows();\r
+ public abstract Resource getSymbolType(ReadGraph graph);\r
+ \r
+ public Resource getDiagramResource() {\r
+ return symbol;\r
+ }\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.element;\r
\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
import org.simantics.sysdyn.modelImport.model.support.Variable;\r
\r
private TextPosition position;\r
\r
public Valve(double x, double y, double w, double h,\r
- String name, IExpression expression, Range range, String unit, String description, TextPosition position) {\r
+ String name, Expression 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
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\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
break;\r
}\r
\r
- graph.claim(symbol, sr.ValveSymbol_textLocation, location);\r
-\r
- setResource(valve);\r
+ Resource valve = super.write(graph, parent, context);\r
+ \r
+ graph.claim(getDiagramResource(), sr.ValveSymbol_textLocation, location);\r
+ \r
+ return valve;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsDependencies() {\r
- return true;\r
+ public Resource getVariableType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).Valve;\r
}\r
-\r
+ \r
@Override\r
- public boolean supportsFlows() {\r
- return true;\r
+ public Resource getSymbolType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).ValveSymbol;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.expression;\r
\r
-import java.util.Arrays;\r
-\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class DelayExpression implements IExpression {\r
+public class DelayExpression extends Expression {\r
+ \r
+ private String expression;\r
+ private String time;\r
+ private String initial;\r
+ private int order;\r
+ \r
+ public DelayExpression(String expression, String time, String initial, int order) {\r
+ this.expression = expression;\r
+ this.time = time;\r
+ this.initial = initial;\r
+ this.order = order;\r
+ }\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\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
+ Resource expr = super.write(graph, parent, context);\r
+ \r
+ graph.claimLiteral(expr, sr.DelayExpression_expression, expression, Bindings.STRING);\r
+ graph.claimLiteral(expr, sr.DelayExpression_delayTime, time, Bindings.STRING);\r
+ graph.claimLiteral(expr, sr.DelayExpression_initialValue, initial, Bindings.STRING);\r
+ graph.claimLiteral(expr, sr.DelayExpression_order, order, Bindings.INTEGER);\r
+ \r
+ return expr;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).DelayExpression;\r
}\r
-\r
}\r
--- /dev/null
+package org.simantics.sysdyn.modelImport.model.expression;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.modelImport.model.WriteContext;\r
+\r
+public class EnumerationExpression extends Expression {\r
+ \r
+ private SubscriptSubExpression active;\r
+ private ArrayList<SubscriptSubExpression> subExpressions;\r
+ \r
+ public EnumerationExpression(String[] indices, Expression expression) {\r
+ active = new SubscriptSubExpression(indices, expression);\r
+ subExpressions = new ArrayList<SubscriptSubExpression>();\r
+ subExpressions.add(active);\r
+ }\r
+ \r
+ public void addExpression(String[] indices, Expression expression) {\r
+ subExpressions.add(new SubscriptSubExpression(indices, expression));\r
+ }\r
+ \r
+ public List<SubscriptSubExpression> getExpressions() {\r
+ return subExpressions;\r
+ }\r
+ \r
+ public void addExpressions(List<SubscriptSubExpression> expressions) {\r
+ subExpressions.addAll(expressions);\r
+ }\r
+ \r
+ @Override\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ for (SubscriptSubExpression subexpr : subExpressions) {\r
+ subexpr.write(graph, parent, context);\r
+ }\r
+ \r
+ // write array index list\r
+ //graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, Arrays.asList()));\r
+ \r
+ // set active expression?\r
+ \r
+ return active.getResource();\r
+ }\r
+ \r
+ private class SubscriptSubExpression extends Expression {\r
+ private String[] indices;\r
+ private Expression expression;\r
+ \r
+ public SubscriptSubExpression(String[] indices, Expression expression) {\r
+ this.indices = indices;\r
+ this.expression = expression;\r
+ }\r
+ \r
+ @Override\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ StringBuilder range = new StringBuilder();\r
+ range.append('[');\r
+ for (String index : indices) {\r
+ if (range.length() > 1)\r
+ range.append(',');\r
+ range.append(index);\r
+ }\r
+ range.append(']');\r
+ \r
+ Resource expr = super.write(graph, parent, context);\r
+ \r
+ graph.claimLiteral(expr, sr.Expression_arrayRange, range.toString(), Bindings.STRING);\r
+ \r
+ return expr;\r
+ }\r
+\r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return expression.getExpressionType(graph);\r
+ }\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return active.getExpressionType(graph);\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.modelImport.model.expression;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
+import org.simantics.sysdyn.modelImport.model.WriteContext;\r
+\r
+public abstract class Expression implements IWriteableObject {\r
+\r
+ private Resource expr;\r
+\r
+ @Override\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ expr = GraphUtils.create2(graph, getExpressionType(graph),\r
+ l0.PartOf, parent);\r
+\r
+ // insert the expression into the expression list of the variable \r
+ // (necessary because of enumeration expressions)\r
+ Resource list = graph.getPossibleObject(parent, sr.Variable_expressionList);\r
+ if (list != null)\r
+ ListUtils.insertBack(graph, list, Arrays.asList(expr));\r
+ else\r
+ graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expr)));\r
+\r
+ return expr;\r
+ }\r
+ \r
+ public abstract Resource getExpressionType(ReadGraph graph);\r
+\r
+ @Override\r
+ public Resource getResource() {\r
+ return expr;\r
+ }\r
+}\r
+++ /dev/null
-package org.simantics.sysdyn.modelImport.model.expression;\r
-\r
-import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
-\r
-public interface IExpression extends IWriteableObject {\r
-\r
-}\r
package org.simantics.sysdyn.modelImport.model.expression;\r
\r
-import java.util.Arrays;\r
-\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class IntegralExpression implements IExpression {\r
+public class IntegralExpression extends Expression {\r
\r
private String integral;\r
private String initial;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
- Resource expression = GraphUtils.create2(graph, sr.StockExpression,\r
- sr.StockExpression_integralEquation, integral,\r
- sr.StockExpression_initialEquation, initial,\r
- l0.PartOf, parent);\r
- // TODO: why is the expression stored in two places?\r
- graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
+ Resource expr = super.write(graph, parent, context);\r
+ \r
+ graph.claimLiteral(expr, sr.StockExpression_integralEquation, integral, Bindings.STRING);\r
+ graph.claimLiteral(expr, sr.StockExpression_initialEquation, initial, Bindings.STRING);\r
+ \r
+ return expr;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).StockExpression;\r
}\r
-\r
}\r
package org.simantics.sysdyn.modelImport.model.expression;\r
\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class LookupExpression implements IExpression {\r
+public class LookupExpression extends Expression {\r
\r
private double xMin;\r
private double yMin;\r
private double xMax;\r
private double yMax;\r
- private String points;\r
+ private double[] points;\r
\r
- public LookupExpression(double xMin, double yMin, double xMax, double yMax, String points) {\r
+ public LookupExpression(double xMin, double yMin, double xMax, double yMax, double[] points) {\r
this.xMin = xMin;\r
this.yMin = yMin;\r
this.xMax = xMax;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
- // TODO: implementation\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ StringBuilder lookup = new StringBuilder();\r
+ lookup.append('{');\r
+ for (int i = 0; i < points.length / 2; i++) {\r
+ if (i > 0)\r
+ lookup.append(',');\r
+ lookup.append('{').append(points[2*i]).append(',').append(points[2*i+1]).append('}');\r
+ }\r
+ lookup.append('}');\r
+ \r
+ Resource expr = super.write(graph, parent, context);\r
+ \r
+ // TODO: fix this\r
+ graph.claimLiteral(expr, sr.WithLookupExpression_expression, "", Bindings.STRING);\r
+ graph.claimLiteral(expr, sr.WithLookupExpression_lookup, lookup.toString(), Bindings.STRING);\r
+ graph.claimLiteral(expr, sr.WithLookupExpression_minX, xMin, Bindings.DOUBLE);\r
+ graph.claimLiteral(expr, sr.WithLookupExpression_minY, yMin, Bindings.DOUBLE);\r
+ graph.claimLiteral(expr, sr.WithLookupExpression_maxX, xMax, Bindings.DOUBLE);\r
+ graph.claimLiteral(expr, sr.WithLookupExpression_maxY, yMax, Bindings.DOUBLE);\r
+ \r
+ return expr;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).WithLookupExpression;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.expression;\r
\r
-import java.util.Arrays;\r
-\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
-public class NormalExpression implements IExpression {\r
+public class NormalExpression extends Expression {\r
\r
- private String equation;\r
+ private String expression;\r
\r
- public NormalExpression(String equation) {\r
- this.equation = equation;\r
+ public NormalExpression(String expression) {\r
+ this.expression = expression;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
- Resource expression = GraphUtils.create2(graph, sr.NormalExpression,\r
- sr.Expression_equation, equation,\r
- l0.PartOf, variable);\r
- // TODO: why is the expression stored in two places?\r
- graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression)));\r
+ \r
+ Resource expr = super.write(graph, parent, context);\r
+ \r
+ graph.claimLiteral(expr, sr.Expression_equation, expression, Bindings.STRING);\r
+ \r
+ return expr;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return SysdynResource.getInstance(graph).NormalExpression;\r
}\r
}\r
+++ /dev/null
-package org.simantics.sysdyn.modelImport.model.expression;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
-\r
-public class SubscriptExpression implements IExpression {\r
- \r
- private ArrayList<SubscriptExpressionElement> elements;\r
- \r
- public SubscriptExpression(String[] indices, IExpression expression) {\r
- elements = new ArrayList<SubscriptExpressionElement>();\r
- elements.add(new SubscriptExpressionElement(indices, expression));\r
- }\r
- \r
- public List<SubscriptExpressionElement> getElements() {\r
- return elements;\r
- }\r
- \r
- public void merge(SubscriptExpression other) {\r
- elements.addAll(other.getElements());\r
- }\r
- \r
- @Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context)\r
- throws DatabaseException {\r
- System.err.println("write subscript expression");\r
- }\r
- \r
- private class SubscriptExpressionElement {\r
- \r
- String[] indices;\r
- IExpression expression;\r
- \r
- public SubscriptExpressionElement(String[] indices, IExpression expression) {\r
- this.indices = indices;\r
- this.expression = expression;\r
- }\r
- }\r
-\r
-}\r
public String getName() {\r
return name;\r
}\r
- \r
- public Resource getResource() {\r
- return resource;\r
- }\r
\r
@Override\r
- public void write(WriteGraph graph, Resource parent, WriteContext context)\r
+ public Resource write(WriteGraph graph, Resource parent, WriteContext context)\r
throws DatabaseException {\r
System.err.println("write enumeration");\r
- \r
- resource = null;\r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getResource() {\r
+ return resource;\r
}\r
\r
}\r
private Double step;\r
private String original;\r
\r
+ private Resource resource;\r
+ \r
public Range(Double start, Double end, Double step, String original) {\r
this.start = start;\r
this.end = end;\r
}\r
\r
@Override\r
- public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
- // TODO: implementation\r
+ public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getResource() {\r
+ return resource;\r
}\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.support;\r
\r
-import org.simantics.sysdyn.modelImport.model.expression.IExpression;\r
-import org.simantics.sysdyn.modelImport.model.expression.SubscriptExpression;\r
+import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
+import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression;\r
\r
public class Variable {\r
\r
private String name;\r
- private IExpression expression;\r
+ private Expression expression;\r
private String unit;\r
private Range range;\r
private String description;\r
\r
- public Variable(String name, IExpression expression, String unit, Range range, String description) {\r
+ public Variable(String name, Expression expression, String unit, Range range, String description) {\r
this.name = name;\r
this.expression = expression;\r
this.unit = unit;\r
return name;\r
}\r
\r
- public IExpression getExpression() {\r
+ public Expression getExpression() {\r
return expression;\r
}\r
\r
return description;\r
}\r
\r
- public void mergeExpression(Variable variable) {\r
- if (expression instanceof SubscriptExpression) {\r
- ((SubscriptExpression)expression).merge((SubscriptExpression)variable.getExpression());\r
+ public void mergeWithVariable(Variable other) {\r
+ if (!name.equals(other.getName())) {\r
+ System.err.println("attempted to merge variable "+name+" with variable "+other.getName());\r
+ return;\r
+ }\r
+ if (expression instanceof EnumerationExpression && other.getExpression() instanceof EnumerationExpression) {\r
+ ((EnumerationExpression)expression).addExpressions(((EnumerationExpression)other.getExpression()).getExpressions());\r
+ }\r
+ if (range == null) {\r
+ range = other.getRange();\r
+ }\r
+ if (unit == null || unit.isEmpty()) {\r
+ unit = other.getUnit();\r
+ }\r
+ if (description == null || description.isEmpty()) {\r
+ description = other.getDescription();\r
}\r
}\r
\r