</command>\r
<command\r
commandId="org.simantics.sysdyn.ui.importMdl"\r
- label="vensim import test"\r
+ label="Vensim Model (.mdl)"\r
style="push">\r
</command>\r
</menu>\r
</property>\r
<property\r
name="windowImages"\r
- value="icons/simantics_sysdyn128.png">\r
+ value="icons/simantics_sysdyn16.png,icons/simantics_sysdyn32.png,icons/simantics_sysdyn48.png,icons/simantics_sysdyn64.png,icons/simantics_sysdyn128.png">\r
</property>\r
<property\r
name="aboutText"\r
value="%about.text">\r
</property>\r
+ <property\r
+ name="aboutImage"\r
+ value="icons/simantics_sysdyn128.png">\r
+ </property>\r
</product>\r
</extension>\r
<extension\r
import org.simantics.sysdyn.modelImport.mdl.SketchVariable;\r
import org.simantics.sysdyn.modelImport.mdl.Subscript;\r
import org.simantics.sysdyn.modelImport.mdl.SubscriptVariable;\r
-import org.simantics.sysdyn.modelImport.mdl.Variable;\r
+import org.simantics.sysdyn.modelImport.mdl.MdlVariable;\r
+import org.simantics.sysdyn.modelImport.model.Comment;\r
import org.simantics.sysdyn.modelImport.model.Connection;\r
import org.simantics.sysdyn.modelImport.model.Model;\r
-import org.simantics.sysdyn.modelImport.model.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.Variable;\r
import org.simantics.sysdyn.modelImport.model.Shadow;\r
import org.simantics.sysdyn.modelImport.model.Symbol;\r
import org.simantics.sysdyn.modelImport.model.Valve;\r
// generate a sysdyn model from the mdl model\r
Model model = new Model(mdl.getName());\r
\r
- double offset = 0;\r
-\r
// add (necessary) enumerations\r
for (Subscript subscript : mdl.getAllSubscripts()) {\r
if (!subscript.isEquivalent())\r
- model.addEnumeration(subscript.getEnumeration(mdl));\r
+ model.addEnumeration(subscript.getEnumeration());\r
}\r
\r
// add lookup functions\r
model.addFunction(lookup.getFunction());\r
}\r
\r
+ int offset = 0;\r
+ \r
// add sketch labels and independent elements\r
for (Sketch sketch : mdl.getSketches()) {\r
\r
- sketch.setOffset(0, offset);\r
-\r
- //model.addSymbol(new Comment(0, offset, -1, -1, sketch.getName()));\r
+ // add the sketch label to the diagram for the sake of clarity\r
+ model.addSymbol(new Comment(\r
+ MdlUtil.getSysdynDimensions(offset, 0, -1, -1), \r
+ "========== VENSIM SKETCH: "+sketch.getName()+" =========="));\r
+ sketch.setLocation(offset, 20);\r
\r
for (SketchComment comment : sketch.getComments()) {\r
- Symbol symbol = comment.getSymbol(mdl, sketch);\r
+ Symbol symbol = comment.getSymbol();\r
if (symbol != null) {\r
comment.setModelObject(symbol);\r
model.addSymbol(symbol);\r
}\r
\r
for (SketchValve valve : sketch.getValves()) {\r
- Valve symbol = valve.getSymbol(mdl, sketch);\r
+ Valve symbol = valve.getSymbol();\r
if (symbol != null) {\r
valve.setModelObject(symbol);\r
model.addSymbol(symbol);\r
}\r
\r
for (SketchVariable variable : sketch.getNonShadowVariables()) {\r
- ModelVariable symbol = variable.getSymbol(mdl, sketch);\r
+ Variable symbol = variable.getSymbol();\r
if (symbol != null) {\r
variable.setModelObject(symbol);\r
model.addSymbol(symbol);\r
}\r
}\r
\r
- offset += 200;\r
+ offset += sketch.getWidth();\r
}\r
\r
// add dependent elements\r
for (Sketch sketch : mdl.getSketches()) {\r
for (SketchVariable variable : sketch.getShadowVariables()) {\r
- ModelVariable original = model.getVariable(variable.getName());\r
- Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(mdl, sketch);\r
+ Variable original = model.getVariable(variable.getName());\r
+ Symbol symbol = original != null ? new Shadow(variable.getDimensions(), original) : variable.getSymbol();\r
if (symbol != null) {\r
variable.setModelObject(symbol);\r
model.addSymbol(symbol);\r
}\r
\r
for (SketchConnection connection : sketch.getConnections()) {\r
- Connection conn = connection.getConnection(sketch);\r
+ Connection conn = connection.getConnection();\r
if (conn != null) {\r
connection.setModelObject(conn);\r
model.addConnection(conn);\r
}\r
\r
// Set simulation parameters\r
- Variable start = mdl.getVariable(PARAMETER_START);\r
+ MdlVariable start = mdl.getVariable(PARAMETER_START);\r
if (start != null && Pattern.matches(MdlUtil.DBL, start.getExpressionString()))\r
model.setStart(Double.parseDouble(start.getExpressionString()));\r
\r
- Variable stop = mdl.getVariable(PARAMETER_STOP);\r
+ MdlVariable stop = mdl.getVariable(PARAMETER_STOP);\r
if (stop != null && Pattern.matches(MdlUtil.DBL, stop.getExpressionString()))\r
model.setStop(Double.parseDouble(stop.getExpressionString()));\r
\r
- Variable step = mdl.getVariable(PARAMETER_STEP);\r
- if (step != null && Pattern.matches(MdlUtil.DBL, step.getExpressionString()))\r
+ MdlVariable step = mdl.getVariable(PARAMETER_STEP);\r
+ if (step != null && Pattern.matches(MdlUtil.DBL, step.getExpressionString())) {\r
model.setStep(Double.parseDouble(step.getExpressionString()));\r
+ // TODO: for some reason sysdyn only accepts certain time units\r
+ // so this most definitely does not work in all cases\r
+ String unit = step.getUnit();\r
+ if (unit.contains("[")) {\r
+ unit = unit.substring(0, unit.indexOf('[')).trim();\r
+ }\r
+ model.setUnit(unit.toLowerCase());\r
+ }\r
+ \r
\r
// TODO: unit and result step thingamajig\r
\r
if ((cat = Declaration.parseCategory(str)) != null) {\r
category = cat;\r
}\r
- else if ((decl = Variable.getPossible(str)) != null) {\r
- mdl.addVariable((Variable)decl, category);\r
+ else if ((decl = MdlVariable.getPossible(str, mdl)) != null) {\r
+ mdl.addVariable((MdlVariable)decl, category);\r
}\r
- else if ((decl = SubscriptVariable.getPossible(str)) != null) {\r
+ else if ((decl = SubscriptVariable.getPossible(str, mdl)) != null) {\r
mdl.addSubscriptVariable((SubscriptVariable)decl, category);\r
}\r
- else if ((decl = Lookup.getPossible(str)) != null) {\r
+ else if ((decl = Lookup.getPossible(str, mdl)) != null) {\r
mdl.addLookup((Lookup)decl);\r
}\r
- else if ((decl = Subscript.getPossible(str)) != null) {\r
+ else if ((decl = Subscript.getPossible(str, mdl)) != null) {\r
mdl.addSubscript((Subscript)decl);\r
}\r
else {\r
SketchObject so;\r
\r
if (line.startsWith(SKETCH_START)) {\r
- sketch = new Sketch();\r
+ sketch = new Sketch(mdl);\r
mdl.addSketch(sketch);\r
}\r
else if (line.startsWith(SKETCH_VERSION)) {\r
else if (line.startsWith("$")) {\r
// font declaration, nothing to do here\r
}\r
- else if ((so = SketchConnection.getPossible(line)) != null) {\r
+ else if ((so = SketchConnection.getPossible(line, sketch)) != null) {\r
sketch.addConnection((SketchConnection)so);\r
}\r
- else if ((so = SketchVariable.getPossible(line)) != null) {\r
+ else if ((so = SketchVariable.getPossible(line, sketch)) != null) {\r
sketch.addVariable((SketchVariable)so);\r
}\r
- else if ((so = SketchValve.getPossible(line)) != null) {\r
+ else if ((so = SketchValve.getPossible(line, sketch)) != null) {\r
SketchValve valve = (SketchValve)so;\r
// the next row after a valve should always the variable associated with the valve\r
- SketchVariable attached = SketchVariable.getPossible(reader.readLine());\r
+ SketchVariable attached = SketchVariable.getPossible(reader.readLine(), sketch);\r
if (attached == null || !attached.isAttached()) {\r
throw new Exception("attached variable not found for valve");\r
}\r
valve.setAttached(attached);\r
sketch.addValve(valve);\r
}\r
- else if ((so = SketchComment.getPossible(line)) != null) {\r
+ else if ((so = SketchComment.getPossible(line, sketch)) != null) {\r
SketchComment comment = (SketchComment)so;\r
if (comment.hasTextLine()) {\r
comment.setText(reader.readLine());\r
}\r
- sketch.addComment(comment);\r
+ // do not add i/o objects to the sketch currently\r
+ if (!comment.isInputOutput())\r
+ sketch.addComment(comment);\r
}\r
else {\r
// if we got this far, the element could not be parsed\r
import org.simantics.sysdyn.modelImport.mdl.MdlModel;\r
\r
public class MdlUtil {\r
+ \r
+ // multiplier for dimension conversion between vensim and sysdyn\r
+ private static final double SCALE_MULTIPLIER = 0.45;\r
\r
// most of this is based on the documentation of the .mdl file format \r
// available in http://www.vensim.com/documentation/24305.htm\r
\r
if (indices[i].endsWith("!")) {\r
String subscript = indices[i].substring(0, indices[i].length() - 1);\r
- List<String> values = mdl.getSubscript(subscript).getValues(mdl);\r
+ List<String> values = mdl.getSubscript(subscript).getValues();\r
result.append('{');\r
for (int j = 0; j < values.size(); j++) {\r
if (j > 0)\r
\r
return list.toArray(new String[list.size()]);\r
}\r
+ \r
+ public static double[] getSysdynDimensions(int x, int y, int width, int height) {\r
+ return new double[] {\r
+ x * SCALE_MULTIPLIER,\r
+ y * SCALE_MULTIPLIER,\r
+ width * SCALE_MULTIPLIER,\r
+ height * SCALE_MULTIPLIER\r
+ };\r
+ }\r
}\r
protected static final int declarationDescription = 2;\r
\r
private String name;\r
+ private MdlModel mdl;\r
+ \r
private String unit;\r
private String description;\r
\r
- protected Declaration(String name) {\r
+ protected Declaration(String name, MdlModel mdl) {\r
this.name = name;\r
+ this.mdl = mdl;\r
}\r
\r
protected void parseSuffix(String suffix) \r
return name;\r
}\r
\r
+ public MdlModel getMdl() {\r
+ return mdl;\r
+ }\r
+ \r
public String getUnit() {\r
return unit;\r
}\r
\r
private Function function;\r
\r
- protected Lookup(String name, double xMin, double yMin, double xMax, double yMax, double[] points) {\r
- super(name);\r
+ protected Lookup(String name, MdlModel mdl, \r
+ double xMin, double yMin, double xMax, double yMax, \r
+ double[] points) {\r
+ super(name, mdl);\r
this.xMin = xMin;\r
this.yMin = yMin;\r
this.xMax = xMax;\r
this.points = points;\r
}\r
\r
- public static Lookup getPossible(String line) \r
+ public static Lookup getPossible(String line, MdlModel mdl) \r
throws Exception {\r
Matcher matcher = Pattern.compile(LOOKUP_DECL).matcher(line);\r
if (!matcher.matches()) {\r
double yMax = Double.parseDouble(matcher.group(lookupRangeYMax));\r
double[] points = parsePoints(matcher.group(lookupPoints));\r
\r
- Lookup lookup = new Lookup(name, xMin, yMin, xMax, yMax, points);\r
+ Lookup lookup = new Lookup(name, mdl, xMin, yMin, xMax, yMax, points);\r
lookup.parseSuffix(matcher.group(lookupSuffix));\r
return lookup;\r
}\r
\r
private String name;\r
\r
- private Map<String, Variable> variables;\r
- private Map<String, ArrayList<Variable>> groups;\r
+ private Map<String, MdlVariable> variables;\r
+ private Map<String, ArrayList<MdlVariable>> groups;\r
private Map<String, Lookup> lookups;\r
private Map<String, Subscript> subscripts;\r
private List<Sketch> sketches;\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.variables = new HashMap<String, MdlVariable>();\r
+ this.groups = new HashMap<String, ArrayList<MdlVariable>>();\r
this.lookups = new HashMap<String, Lookup>();\r
this.subscripts = new HashMap<String, Subscript>();\r
this.sketches = new ArrayList<Sketch>();\r
return name;\r
}\r
\r
- public void addVariable(Variable variable, String group) \r
+ public void addVariable(MdlVariable variable, String group) \r
throws Exception {\r
if (variables.get(variable.getName()) != null) {\r
throw new Exception("duplicate variable "+variable.getName());\r
}\r
\r
variables.put(variable.getName(), variable);\r
- addGroup(variable, group);\r
+ \r
+ if (group != null) {\r
+ if (groups.get(group) == null) {\r
+ groups.put(group, new ArrayList<MdlVariable>());\r
+ }\r
+ groups.get(group).add(variable);\r
+ }\r
}\r
\r
public void addSubscriptVariable(SubscriptVariable variable, String group) \r
throws Exception {\r
- Variable original = variables.get(variable.getName());\r
+ MdlVariable original = variables.get(variable.getName());\r
if (original != null) {\r
if (!(original instanceof SubscriptVariable)) {\r
throw new Exception("incompatible type for variable "+variable.getName());\r
((SubscriptVariable)original).addSubscriptVariable(variable);\r
}\r
else {\r
- variables.put(variable.getName(), variable);\r
- addGroup(variable, group);\r
- }\r
- }\r
- \r
- private void addGroup(Variable variable, String group) {\r
- if (group != null) {\r
- if (groups.get(group) == null) {\r
- groups.put(group, new ArrayList<Variable>());\r
- }\r
- groups.get(group).add(variable);\r
+ addVariable(variable, group);\r
}\r
}\r
\r
- public Variable getVariable(String name) {\r
+ public MdlVariable getVariable(String name) {\r
return variables.get(name);\r
}\r
\r
- public Set<Variable> getAllVariables() {\r
- return new HashSet<Variable>(variables.values());\r
+ public Set<MdlVariable> getAllVariables() {\r
+ return new HashSet<MdlVariable>(variables.values());\r
}\r
\r
public void addLookup(Lookup lookup) \r
throw new Exception("duplicate subscript "+subscript.getName());\r
}\r
\r
- // TODO: check if a subscript with the same values already exists\r
- \r
subscripts.put(subscript.getName(), subscript);\r
}\r
\r
- public Subscript getSubscript(String key) {\r
- return subscripts.get(key);\r
+ public Subscript getSubscript(String name) {\r
+ return subscripts.get(name);\r
}\r
\r
public Set<Subscript> getAllSubscripts() {\r
return new HashSet<Subscript>(subscripts.values());\r
}\r
\r
- public Subscript resolveSubscript(List<String> values) {\r
- return resolveSubscript(new HashSet<String>(values));\r
- }\r
- \r
public Subscript resolveSubscript(Set<String> values) {\r
- // if one value is actually a subscript, expand it to its values\r
+ // if a value is actually a subscript, expand it to its values\r
Set<String> all = new HashSet<String>();\r
for (String value : values) {\r
Subscript potential = subscripts.get(value);\r
if (potential != null) {\r
- all.addAll(potential.getValues(this));\r
+ all.addAll(potential.getValues());\r
}\r
else {\r
all.add(value);\r
// find the subscript that contains exactly the given set of values.\r
// only consider original subscripts (ignore equivalent subscripts)\r
for (Subscript subscript : getAllSubscripts()) {\r
- if (!subscript.isEquivalent() && subscript.getValues(this).containsAll(all) && all.containsAll(subscript.getValues(this))) {\r
+ if (!subscript.isEquivalent() && \r
+ subscript.getValues().containsAll(all) && \r
+ all.containsAll(subscript.getValues())) {\r
return subscript;\r
}\r
}\r
package org.simantics.sysdyn.modelImport.mdl;\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.MdlUtil;\r
-import org.simantics.sysdyn.modelImport.model.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.Variable;\r
import org.simantics.sysdyn.modelImport.model.expression.DelayExpression;\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.NormalExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
-public class Variable extends Declaration {\r
+public class MdlVariable extends Declaration {\r
\r
protected static final String EXPRESSION =\r
"(?:\\s*=\\s*([^~]*?(?:"+MdlUtil.SPECIAL_NAME+"[^~]*?)*))?";\r
\r
private String expression;\r
\r
- protected Variable(String name, String expression) {\r
- super(name);\r
+ protected MdlVariable(String name, MdlModel mdl, String expression) {\r
+ super(name, mdl);\r
this.expression = expression;\r
}\r
\r
- public static Variable getPossible(String line) \r
+ public static MdlVariable getPossible(String line, MdlModel mdl) \r
throws Exception {\r
Matcher matcher = Pattern.compile(VARIABLE_DECL).matcher(line);\r
if (!matcher.matches()) {\r
if (expression != null)\r
expression = MdlUtil.normalize(expression);\r
\r
- Variable var = new Variable(name, expression);\r
+ MdlVariable var = new MdlVariable(name, mdl, expression);\r
var.parseSuffix(matcher.group(variableSuffix));\r
return var;\r
}\r
return expression;\r
}\r
\r
- public <T extends ModelVariable> T setUpModelVariable(T variable, \r
- double[] dimensions, MdlModel mdl) {\r
+ public <T extends Variable> T initVariable(T variable) {\r
variable.setName(getName());\r
if (expression != null)\r
- variable.setExpression(getExpression(mdl));\r
+ variable.setExpression(getExpression());\r
\r
String unit = getUnit();\r
Range range = parseRange(unit);\r
variable.setRange(range);\r
variable.setDescription(getDescription());\r
\r
- variable.setDimensions(dimensions);\r
- \r
return variable;\r
}\r
\r
- public Expression getExpression(MdlModel mdl) {\r
- return parseExpression(MdlUtil.finalize(expression, mdl));\r
+ public Expression getExpression() {\r
+ return parseExpression(MdlUtil.finalize(expression, getMdl()));\r
}\r
\r
protected static Expression parseExpression(String expression) {\r
\r
import java.util.ArrayList;\r
import java.util.HashMap;\r
-import java.util.Iterator;\r
import java.util.List;\r
\r
-import org.simantics.sysdyn.modelImport.model.Symbol;\r
-\r
public class Sketch {\r
\r
+ private MdlModel mdl;\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
+ private int xOffset = 0;\r
+ private int yOffset = 0;\r
+ \r
+ private boolean updateEdges;\r
+ private int topEdge = 0;\r
+ private int bottomEdge = 0;\r
+ private int leftEdge = 0;\r
+ private int rightEdge = 0;\r
\r
private List<SketchComment> comments;\r
private List<SketchConnection> connections;\r
\r
private HashMap<Integer, SketchObject> objects;\r
\r
- public Sketch() {\r
- edgesOutOfDate = true;\r
+ public Sketch(MdlModel mdl) {\r
+ this.mdl = mdl;\r
+ \r
+ updateEdges = true;\r
\r
comments = new ArrayList<SketchComment>();\r
connections = new ArrayList<SketchConnection>();\r
objects = new HashMap<Integer, SketchObject>();\r
}\r
\r
+ public MdlModel getMdl() {\r
+ return mdl;\r
+ }\r
+ \r
public String getName() {\r
return name;\r
}\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
+ if (updateEdges) {\r
+ boolean first = true;\r
+ for (SketchElement e : getAllElements()) {\r
+ topEdge = first ? e.getTop() : Math.min(topEdge, e.getTop() );\r
+ bottomEdge = first ? e.getBottom() : Math.max(bottomEdge, e.getBottom());\r
+ leftEdge = first ? e.getLeft() : Math.min(leftEdge, e.getLeft());\r
+ rightEdge = first ? e.getRight() : Math.max(rightEdge, e.getRight());\r
+ first = false;\r
+ }\r
+ updateEdges = false;\r
+ }\r
}\r
\r
- public double getWidth() {\r
+ public int getWidth() {\r
updateEdges();\r
return rightEdge - leftEdge;\r
}\r
\r
- public double getHeight() {\r
+ public int getHeight() {\r
updateEdges();\r
return bottomEdge - topEdge;\r
}\r
\r
- public void setOffset(double horizontal, double vertical) {\r
- hOffset = horizontal;\r
- vOffset = vertical;\r
+ // sets the relative location of this sketch in the diagram \r
+ public void setLocation(int x, int y) {\r
+ xOffset = x;\r
+ yOffset = y;\r
}\r
\r
- private double hOffset, vOffset;\r
- \r
- public double getHorizontalOffset() {\r
- return hOffset;\r
+ public int getElementXOffset() {\r
+ updateEdges();\r
+ return -leftEdge + xOffset;\r
}\r
\r
- public double getVerticalOffset() {\r
- return vOffset;\r
+ public int getElementYOffset() {\r
+ updateEdges();\r
+ return -topEdge + yOffset;\r
}\r
\r
public void addComment(SketchComment comment) {\r
comments.add(comment);\r
objects.put(comment.getId(), comment);\r
+ updateEdges = true;\r
}\r
\r
public List<SketchComment> getComments() {\r
// replace the attached variable with the valve in order to redirect \r
// possible connections to the variable to the valve\r
objects.put(valve.getAttached().getId(), valve);\r
+ updateEdges = true;\r
}\r
\r
public List<SketchValve> getValves() {\r
public void addVariable(SketchVariable variable) {\r
variables.add(variable);\r
objects.put(variable.getId(), variable);\r
+ updateEdges = true;\r
}\r
\r
public List<SketchVariable> getVariables() {\r
private String text;\r
private CommentIcon icon;\r
\r
- protected SketchComment(int id, CommentIcon icon) {\r
- super(id);\r
+ protected SketchComment(int id, Sketch sketch, CommentIcon icon) {\r
+ super(id, sketch);\r
this.icon = icon;\r
}\r
\r
- public static SketchComment getPossible(String line) \r
+ public static SketchComment getPossible(String line, Sketch sketch) \r
throws Exception {\r
Matcher matcher = Pattern.compile(SKETCH_COMMENT).matcher(line);\r
if (!matcher.matches()) {\r
default: icon = CommentIcon.OTHER; break;\r
}\r
\r
- SketchComment element = new SketchComment(id, icon);\r
+ SketchComment element = new SketchComment(id, sketch, icon);\r
element.parseSuffix(matcher.group(commentSuffix));\r
return element;\r
}\r
}\r
\r
@Override\r
- public Symbol getSymbol(MdlModel mdl, Sketch sketch) {\r
+ public Symbol getSymbol() {\r
if (isInputOutput())\r
return new Comment(getDimensions(), "I/O objects are not supported");\r
\r
switch(icon) {\r
- case CLOUD: return new Cloud(getDimensions(sketch));\r
- case OTHER: return new Comment(getDimensions(sketch), text);\r
+ case CLOUD: return new Cloud(getDimensions());\r
+ case OTHER: return new Comment(getDimensions(), text);\r
default: return null;\r
}\r
}\r
private ConnectionType type;\r
int[] points;\r
\r
- protected SketchConnection(int id, int from, int to, ConnectionType type, int[] points) {\r
- super(id);\r
+ protected SketchConnection(int id, Sketch sketch, int from, int to, ConnectionType type, int[] points) {\r
+ super(id, sketch);\r
this.from = from;\r
this.to = to;\r
this.type = type;\r
this.points = points;\r
}\r
\r
- public static SketchConnection getPossible(String line) \r
+ public static SketchConnection getPossible(String line, Sketch sketch) \r
throws Exception {\r
Matcher matcher = Pattern.compile(SKETCH_CONNECTION).matcher(line);\r
if (!matcher.matches()) {\r
\r
int[] points = parsePoints(matcher.group(connectionPoints));\r
\r
- return new SketchConnection(id, from, to, type, points);\r
+ return new SketchConnection(id, sketch, from, to, type, points);\r
}\r
\r
private static int[] parsePoints(String str) {\r
return points;\r
}\r
\r
- public Connection getConnection(Sketch sketch) {\r
- Symbol tail = (Symbol)sketch.getObject(from).getModelObject();\r
+ public Connection getConnection() {\r
+ Symbol tail = (Symbol)getSketch().getObject(from).getModelObject();\r
if (tail == null) {\r
return null;\r
}\r
- Symbol head = (Symbol)sketch.getObject(to).getModelObject();\r
+ Symbol head = (Symbol)getSketch().getObject(to).getModelObject();\r
if (head == null) {\r
return null;\r
}\r
\r
switch (type) {\r
- case ARROW: return new Dependency(tail, head, true, false, getAngle(sketch));\r
+ case ARROW: return new Dependency(tail, head, true, false, getAngle(getSketch()));\r
case LINE_ARROW: return new Flow(tail, head);\r
case LINE_SEGMENT: return new Flow(head, tail);\r
default: return null;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
import org.simantics.sysdyn.modelImport.model.Symbol;\r
import org.simantics.sysdyn.modelImport.model.Valve.TextPosition;\r
\r
public abstract class SketchElement extends SketchObject {\r
\r
- // multiplier for dimension conversion between vensim and sysdyn\r
- private static final double SCALE_MULTIPLIER = 0.4;\r
- \r
// each sketch element is declared with a string:\r
// n,id,name,x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
\r
private boolean textLine;\r
private TextPosition textPosition;\r
\r
- protected SketchElement(int id) {\r
- super(id);\r
+ protected SketchElement(int id, Sketch sketch) {\r
+ super(id, sketch);\r
}\r
\r
protected void parseSuffix(String suffix) \r
return height;\r
}\r
\r
+ public int getTop() {\r
+ return getY() - getHeight();\r
+ }\r
+ \r
+ public int getBottom() {\r
+ return getY() + getHeight();\r
+ }\r
+ \r
+ public int getLeft() {\r
+ return getX() - getWidth();\r
+ }\r
+ \r
+ public int getRight() {\r
+ return getX() + getWidth();\r
+ }\r
+ \r
public boolean isAttached() {\r
return attached;\r
}\r
return textPosition;\r
}\r
\r
- public abstract Symbol getSymbol(MdlModel mdl, Sketch sketch);\r
- \r
- public double[] getDimensions(Sketch sketch) {\r
- double[] dimensions = getDimensions();\r
- dimensions[0] = dimensions[0] + sketch.getHorizontalOffset();\r
- dimensions[1] = dimensions[1] + sketch.getVerticalOffset();\r
- return dimensions;\r
- }\r
+ public abstract Symbol getSymbol();\r
\r
- // get element dimensions [x, y, width, height]\r
+ // get sysdyn dimensions for element [x, y, width, height]\r
public double[] getDimensions() {\r
- double[] dimensions = new double[4];\r
- dimensions[0] = (getX() - getWidth()) * SCALE_MULTIPLIER;\r
- dimensions[1] = (getY() - getHeight()) * SCALE_MULTIPLIER;\r
- dimensions[2] = getWidth() * 2 * SCALE_MULTIPLIER;\r
- dimensions[3] = getHeight() * 2 * SCALE_MULTIPLIER;\r
- return dimensions;\r
+ return MdlUtil.getSysdynDimensions(\r
+ getLeft() + getSketch().getElementXOffset(),\r
+ getTop() + getSketch().getElementYOffset(),\r
+ getWidth() * 2,\r
+ getHeight() * 2\r
+ );\r
}\r
\r
}\r
protected static final String SKIP_ANY = "[^,]*,";\r
\r
private int id;\r
+ private Sketch sketch;\r
+ \r
private IWriteableObject modelObject;\r
\r
- protected SketchObject(int id) {\r
+ protected SketchObject(int id, Sketch sketch) {\r
this.id = id;\r
+ this.sketch = sketch;\r
}\r
\r
public int getId() {\r
return id;\r
}\r
\r
+ public Sketch getSketch() {\r
+ return sketch;\r
+ }\r
+ \r
public void setModelObject(IWriteableObject modelObject) {\r
this.modelObject = modelObject;\r
}\r
\r
private SketchVariable attached;\r
\r
- protected SketchValve(int id) {\r
- super(id);\r
+ protected SketchValve(int id, Sketch sketch) {\r
+ super(id, sketch);\r
}\r
\r
- public static SketchValve getPossible(String line) \r
+ public static SketchValve getPossible(String line, Sketch sketch) \r
throws Exception {\r
Matcher matcher = Pattern.compile(SKETCH_VALVE).matcher(line);\r
if (!matcher.matches()) {\r
\r
int id = Integer.parseInt(matcher.group(valveId));\r
\r
- SketchValve element = new SketchValve(id);\r
+ SketchValve element = new SketchValve(id, sketch);\r
element.parseSuffix(matcher.group(valveSuffix));\r
return element;\r
}\r
}\r
\r
@Override\r
- public Valve getSymbol(MdlModel mdl, Sketch sketch) {\r
- Variable variable = mdl.getVariable(attached.getName());\r
+ public Valve getSymbol() {\r
+ MdlVariable variable = getSketch().getMdl().getVariable(attached.getName());\r
if (variable == null) {\r
return null;\r
}\r
\r
Valve valve = new Valve(Orientation.HORIZONTAL, getTextPosition());\r
+ valve.setDimensions(getDimensions());\r
\r
- return variable.setUpModelVariable(valve, getDimensions(sketch), mdl);\r
+ return variable.initVariable(valve);\r
}\r
\r
}\r
\r
import org.simantics.sysdyn.modelImport.MdlUtil;\r
import org.simantics.sysdyn.modelImport.model.Auxiliary;\r
-import org.simantics.sysdyn.modelImport.model.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.Variable;\r
import org.simantics.sysdyn.modelImport.model.Stock;\r
-import org.simantics.sysdyn.modelImport.model.Symbol;\r
-import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
\r
public class SketchVariable extends SketchElement {\r
\r
\r
private String name;\r
\r
- protected SketchVariable(int id, String name) {\r
- super(id);\r
+ protected SketchVariable(int id, Sketch sketch, String name) {\r
+ super(id, sketch);\r
this.name = name;\r
}\r
\r
- public static SketchVariable getPossible(String line) \r
+ public static SketchVariable getPossible(String line, Sketch sketch) \r
throws Exception {\r
Matcher matcher = Pattern.compile(SKETCH_VARIABLE).matcher(line);\r
if (!matcher.matches()) {\r
int id = Integer.parseInt(matcher.group(variableId));\r
String name = MdlUtil.normalize(matcher.group(variableName));\r
\r
- SketchVariable element = new SketchVariable(id, name);\r
+ SketchVariable element = new SketchVariable(id, sketch, name);\r
element.parseSuffix(matcher.group(variableSuffix));\r
return element;\r
}\r
}\r
\r
@Override\r
- public ModelVariable getSymbol(MdlModel mdl, Sketch sketch) {\r
- Variable variable = mdl.getVariable(name);\r
+ public Variable getSymbol() {\r
+ MdlVariable variable = getSketch().getMdl().getVariable(name);\r
if (variable == null) {\r
return null;\r
}\r
\r
- ModelVariable var;\r
- \r
+ Variable var;\r
if (variable.getExpressionString() != null && variable.getExpressionString().startsWith("INTEG"))\r
var = new Stock();\r
else\r
var = new Auxiliary();\r
+ var.setDimensions(getDimensions());\r
\r
- return variable.setUpModelVariable(var, getDimensions(sketch), mdl);\r
+ return variable.initVariable(var);\r
}\r
\r
}
\ No newline at end of file
package org.simantics.sysdyn.modelImport.mdl;\r
\r
import java.util.Arrays;\r
-import java.util.HashSet;\r
import java.util.List;\r
-import java.util.Set;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
\r
private Enumeration enumeration;\r
\r
- protected Subscript(String name, List<String> values) {\r
- super(name);\r
+ protected Subscript(String name, MdlModel mdl, List<String> values) {\r
+ super(name, mdl);\r
this.original = null;\r
this.values = values;\r
}\r
\r
- protected Subscript(String name, String original) {\r
- super(name);\r
+ protected Subscript(String name, MdlModel mdl, String original) {\r
+ super(name, mdl);\r
this.original = original;\r
this.values = null;\r
}\r
\r
- public static Subscript getPossible(String line) \r
+ public static Subscript getPossible(String line, MdlModel mdl) \r
throws Exception {\r
Matcher matcher = Pattern.compile(SUBSCRIPT_DECL).matcher(line); \r
if (!matcher.matches())\r
\r
Subscript subscript;\r
if (equivalent)\r
- subscript = new Subscript(name, expression);\r
+ subscript = new Subscript(name, mdl, expression);\r
else\r
- subscript = new Subscript(name, Arrays.asList(expression.split(",")));\r
+ subscript = new Subscript(name, mdl, Arrays.asList(expression.split(",")));\r
subscript.parseSuffix(matcher.group(subscriptSuffix));\r
return subscript;\r
}\r
return original;\r
}\r
\r
- public List<String> getValues(MdlModel mdl) {\r
+ public List<String> getValues() {\r
if (values != null)\r
return values;\r
else\r
- return mdl.getSubscript(original).getValues(mdl);\r
+ return getMdl().getSubscript(original).getValues();\r
}\r
\r
public boolean isEquivalent() {\r
return original != null;\r
}\r
\r
- public Enumeration getEnumeration(MdlModel mdl) {\r
+ public Enumeration getEnumeration() {\r
if (enumeration == null) {\r
- enumeration = new Enumeration(getName(), getValues(mdl));\r
+ enumeration = new Enumeration(getName(), getValues());\r
}\r
\r
return enumeration;\r
import org.simantics.sysdyn.modelImport.model.expression.NormalExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
\r
-public class SubscriptVariable extends Variable {\r
+public class SubscriptVariable extends MdlVariable {\r
\r
// a subscript variable is a variable that has a different equation\r
// depending on the value of the subscript it references\r
private String[] indices;\r
private SubscriptVariable next;\r
\r
- protected SubscriptVariable(String name, String expression, String[] indices) {\r
- super(name, expression);\r
+ protected SubscriptVariable(String name, MdlModel mdl, String expression, String[] indices) {\r
+ super(name, mdl, expression);\r
this.indices = indices;\r
this.next = null;\r
}\r
\r
- public static SubscriptVariable getPossible(String line) \r
+ public static SubscriptVariable getPossible(String line, MdlModel mdl) \r
throws Exception {\r
Matcher matcher = Pattern.compile(SUBSCRIPT_VARIABLE_DECL).matcher(line);\r
if (!matcher.matches()) {\r
if (expression != null)\r
expression = MdlUtil.normalize(expression);\r
\r
- SubscriptVariable subVar = new SubscriptVariable(name, expression, indices);\r
+ SubscriptVariable subVar = new SubscriptVariable(name, mdl, expression, indices);\r
subVar.parseSuffix(matcher.group(subscriptVariableSuffix));\r
return subVar;\r
}\r
}\r
\r
@Override\r
- public EnumerationExpression getExpression(MdlModel mdl) {\r
+ public EnumerationExpression getExpression() {\r
SubscriptVariable var;\r
\r
// find out what subscripts the combined expression uses by collecting\r
\r
List<Enumeration> enumerations = new ArrayList<Enumeration>();\r
for (Set<String> values : subscripts) {\r
- Subscript potential = mdl.resolveSubscript(values);\r
+ Subscript potential = getMdl().resolveSubscript(values);\r
if (potential == null) {\r
System.err.println("subscript indices could not be resolved ");\r
for (String value : values) {\r
}\r
System.err.println();\r
}\r
- enumerations.add(potential.getEnumeration(mdl));\r
+ enumerations.add(potential.getEnumeration());\r
}\r
\r
EnumerationExpression expr = new EnumerationExpression(enumerations);\r
\r
- // populate the created expression (TODO: comment)\r
+ // populate the created expression\r
\r
- // TODO: is this check correct, also does not work correctly yet\r
+ // TODO: is this check correct?\r
if (next == null && enumerations.size() == 2) {\r
- // option a:\r
-// StringBuilder buffer = new StringBuilder();\r
-// buffer.append('{');\r
-// for (int i = 0; i < values.length; i++) {\r
-// if (i > 0) {\r
-// buffer.append(',');\r
-// }\r
-// buffer.append('{');\r
-// for (int j = 0; j < values[i].length; j++) {\r
-// if (j > 0) {\r
-// buffer.append(',');\r
-// }\r
-// buffer.append(values[i][j]);\r
-// }\r
-// buffer.append('}');\r
-// }\r
-// buffer.append('}');\r
-//\r
-// String[] exprindices = new String[enumerations.size()];\r
-// for (int i = 0; i < exprindices.length; i++) {\r
-// exprindices[i] = enumerations.get(i).getName();\r
-// }\r
-//\r
-// expr.addExpression(new NormalExpression(buffer.toString()), exprindices);\r
-// return expr;\r
- \r
- // option b: (probably more sensible)\r
double[][] values = getPossibleValueArray(getExpressionString());\r
if (values != null) {\r
for (int i = 0; i < values.length; i++) {\r
int limit = workqueue.size();\r
for (int j = 0; j < limit; j++) {\r
WorkExpression current = workqueue.pollFirst();\r
- Subscript potential = mdl.getSubscript(current.indices[i]);\r
+ Subscript potential = getMdl().getSubscript(current.indices[i]);\r
if (potential != null) {\r
- for (String value : potential.getValues(mdl)) {\r
+ for (String value : potential.getValues()) {\r
String[] newindices = Arrays.copyOf(current.indices, current.indices.length);\r
newindices[i] = value;\r
String newexpression = current.expression;\r
}\r
\r
for (WorkExpression we : workqueue) {\r
- // TODO: is this check correct\r
- String expression = MdlUtil.finalize(we.expression, mdl);\r
+ String expression = MdlUtil.finalize(we.expression, getMdl());\r
+ // TODO: is this check correct?\r
if (next == null) {\r
expression = removeComparisons(expression, indices, we.indices);\r
}\r
import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
-public class Auxiliary extends ModelVariable {\r
+public class Auxiliary extends Variable {\r
\r
public Auxiliary() {\r
\r
import java.util.ArrayList;\r
import java.util.Collection;\r
import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.Resource;\r
\r
private String name;\r
// necessary simulation parameters\r
- private double start, stop, step;\r
+ private Double start, stop, step;\r
private String unit;\r
\r
// the structure of the model\r
- private HashMap<String, ModelVariable> variables;\r
- private HashMap<String, Enumeration> enumerations;\r
- private HashMap<String, Function> functions;\r
+ private Map<String, Variable> variables;\r
+ private Map<String, Enumeration> enumerations;\r
+ private Map<String, Function> functions;\r
\r
- private ArrayList<Symbol> symbols;\r
- private ArrayList<Shadow> shadows;\r
- private ArrayList<Connection> connections;\r
+ private List<Symbol> symbols;\r
+ private List<Shadow> shadows;\r
+ private List<Connection> connections;\r
\r
private Resource model;\r
\r
public Model(String name) {\r
this.name = name;\r
\r
- variables = new HashMap<String, ModelVariable>();\r
+ variables = new HashMap<String, Variable>();\r
enumerations = new HashMap<String, Enumeration>();\r
functions = new HashMap<String, Function>();\r
\r
symbols = new ArrayList<Symbol>();\r
shadows = new ArrayList<Shadow>();\r
connections = new ArrayList<Connection>();\r
- \r
- this.start = 0;\r
- this.stop = 100;\r
- this.step = 1;\r
- this.unit = null;\r
- }\r
- \r
- public void setParameters(double start, double stop, double step, String unit) {\r
- \r
}\r
\r
public double getStart() {\r
this.step = step;\r
}\r
\r
- public void addSymbol(ModelVariable variable) {\r
+ public String getUnit() {\r
+ return unit;\r
+ }\r
+ \r
+ public void setUnit(String unit) {\r
+ this.unit = unit;\r
+ }\r
+ \r
+ public void addSymbol(Variable variable) {\r
if (variables.get(variable.getName()) != null) {\r
System.err.println("variable "+variable.getName()+" already defined");\r
}\r
symbols.add(variable);\r
}\r
\r
- public ModelVariable getVariable(String name) {\r
+ public Variable getVariable(String name) {\r
return variables.get(name);\r
}\r
\r
- public Collection<ModelVariable> getVariables() {\r
+ public Collection<Variable> getVariables() {\r
return variables.values();\r
}\r
\r
\r
// TODO: this must be updated if/when simulation parameters are moved \r
// from model to experiment\r
- //graph.claimLiteral(model, sr.SysdynModel_startTime, start, Bindings.DOUBLE);\r
- //graph.claimLiteral(model, sr.SysdynModel_stopTime, stop, Bindings.DOUBLE);\r
- //graph.claimLiteral(model, sr.SysdynModel_simulationStepLength, step, Bindings.DOUBLE);\r
- //graph.claimLiteral(model, sr.SysdynModel_timeUnit, unit, Bindings.STRING);\r
+ if (start != null)\r
+ graph.claimLiteral(model, sr.SysdynModel_startTime, start, Bindings.DOUBLE);\r
+ if (stop != null)\r
+ graph.claimLiteral(model, sr.SysdynModel_stopTime, stop, Bindings.DOUBLE);\r
+ if (step != null)\r
+ graph.claimLiteral(model, sr.SysdynModel_simulationStepLength, step, Bindings.DOUBLE);\r
+ if (unit != null)\r
+ graph.claimLiteral(model, sr.SysdynModel_timeUnit, unit, Bindings.STRING);\r
\r
Resource configuration = graph.getSingleObject(model, sim.HasConfiguration);\r
\r
\r
public class Shadow extends Symbol {\r
\r
- private ModelVariable original;\r
+ private Variable original;\r
\r
private Resource shadow;\r
\r
\r
}\r
\r
- public Shadow(ModelVariable original) {\r
+ public Shadow(Variable original) {\r
this.original = original;\r
}\r
\r
- public Shadow(double[] dim, ModelVariable original) {\r
+ public Shadow(double[] dim, Variable original) {\r
super(dim);\r
this.original = original;\r
}\r
\r
- public ModelVariable getOriginal() {\r
+ public Variable getOriginal() {\r
return original;\r
}\r
\r
- public void setOriginal(ModelVariable original) {\r
+ public void setOriginal(Variable original) {\r
this.original = original;\r
}\r
\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
-public class Stock extends ModelVariable {\r
+public class Stock extends Variable {\r
\r
public Stock() {\r
\r
import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
-public class Valve extends ModelVariable {\r
+public class Valve extends Variable {\r
\r
public enum Orientation {\r
HORIZONTAL, VERTICAL\r
import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
-public abstract class ModelVariable extends Symbol {\r
+public abstract class Variable extends Symbol {\r
\r
private String name;\r
private Expression expression;\r
\r
private Resource variable;\r
\r
- public ModelVariable() {\r
+ public Variable() {\r
\r
}\r
\r
- public ModelVariable(String name, Expression expression, Range range, String unit, String description) {\r
+ public Variable(String name, Expression expression, Range range, String unit, String description) {\r
this.name = name;\r
this.expression = expression;\r
this.range = range;\r
this.description = description;\r
}\r
\r
- public ModelVariable(double[] dim, String name, Expression expression, Range range, String unit, String description) {\r
+ public Variable(double[] dim, String name, Expression expression, Range range, String unit, String description) {\r
super(dim);\r
this.name = name;\r
this.expression = expression;\r
// write array index list\r
graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist));\r
\r
- // set active expression?\r
+ // TODO: set active expression?\r
\r
return getResource();\r
}\r
package org.simantics.sysdyn.modelImport.model.support;\r
\r
import java.util.ArrayList;\r
-import java.util.HashSet;\r
import java.util.List;\r
-import java.util.Set;\r
\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
package org.simantics.sysdyn.modelImport.model.support;\r
\r
-import java.util.List;\r
-\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
graph.claimLiteral(function, l0.HasDescription, body, Bindings.STRING);\r
}\r
\r
+ // TODO: should alco handle inputs and outputs somehow\r
+ \r
return function;\r
}\r
\r
\r
@Override\r
public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException {\r
+ // TODO: implementation\r
return null;\r
}\r
\r