From: jkauttio Date: Fri, 30 May 2014 14:41:27 +0000 (+0000) Subject: Finish cleanup of vensim import for release. X-Git-Tag: 1.8.1~41 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=503b4789151701f4713fc0afe510278a342822aa;p=simantics%2Fsysdyn.git Finish cleanup of vensim import for release. refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29562 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 58e73242..e6c36627 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -1298,7 +1298,7 @@ @@ -2583,12 +2583,16 @@ + value="icons/simantics_sysdyn16.png,icons/simantics_sysdyn32.png,icons/simantics_sysdyn48.png,icons/simantics_sysdyn64.png,icons/simantics_sysdyn128.png"> + + values = mdl.getSubscript(subscript).getValues(mdl); + List values = mdl.getSubscript(subscript).getValues(); result.append('{'); for (int j = 0; j < values.size(); j++) { if (j > 0) @@ -284,4 +287,13 @@ public class MdlUtil { return list.toArray(new String[list.size()]); } + + public static double[] getSysdynDimensions(int x, int y, int width, int height) { + return new double[] { + x * SCALE_MULTIPLIER, + y * SCALE_MULTIPLIER, + width * SCALE_MULTIPLIER, + height * SCALE_MULTIPLIER + }; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java index 45d1bb41..4282a6dd 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java @@ -18,11 +18,14 @@ public abstract class Declaration { protected static final int declarationDescription = 2; private String name; + private MdlModel mdl; + private String unit; private String description; - protected Declaration(String name) { + protected Declaration(String name, MdlModel mdl) { this.name = name; + this.mdl = mdl; } protected void parseSuffix(String suffix) @@ -42,6 +45,10 @@ public abstract class Declaration { return name; } + public MdlModel getMdl() { + return mdl; + } + public String getUnit() { return unit; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java index d6528be1..cf0ff894 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java @@ -33,8 +33,10 @@ public class Lookup extends Declaration { private Function function; - protected Lookup(String name, double xMin, double yMin, double xMax, double yMax, double[] points) { - super(name); + protected Lookup(String name, MdlModel mdl, + double xMin, double yMin, double xMax, double yMax, + double[] points) { + super(name, mdl); this.xMin = xMin; this.yMin = yMin; this.xMax = xMax; @@ -42,7 +44,7 @@ public class Lookup extends Declaration { this.points = points; } - public static Lookup getPossible(String line) + public static Lookup getPossible(String line, MdlModel mdl) throws Exception { Matcher matcher = Pattern.compile(LOOKUP_DECL).matcher(line); if (!matcher.matches()) { @@ -56,7 +58,7 @@ public class Lookup extends Declaration { double yMax = Double.parseDouble(matcher.group(lookupRangeYMax)); double[] points = parsePoints(matcher.group(lookupPoints)); - Lookup lookup = new Lookup(name, xMin, yMin, xMax, yMax, points); + Lookup lookup = new Lookup(name, mdl, xMin, yMin, xMax, yMax, points); lookup.parseSuffix(matcher.group(lookupSuffix)); return lookup; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java index 202dcad8..fba97c5c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java @@ -11,8 +11,8 @@ public class MdlModel { private String name; - private Map variables; - private Map> groups; + private Map variables; + private Map> groups; private Map lookups; private Map subscripts; private List sketches; @@ -20,8 +20,8 @@ public class MdlModel { public MdlModel(String name) { this.name = name; - this.variables = new HashMap(); - this.groups = new HashMap>(); + this.variables = new HashMap(); + this.groups = new HashMap>(); this.lookups = new HashMap(); this.subscripts = new HashMap(); this.sketches = new ArrayList(); @@ -31,19 +31,25 @@ public class MdlModel { return name; } - public void addVariable(Variable variable, String group) + public void addVariable(MdlVariable variable, String group) throws Exception { if (variables.get(variable.getName()) != null) { throw new Exception("duplicate variable "+variable.getName()); } variables.put(variable.getName(), variable); - addGroup(variable, group); + + if (group != null) { + if (groups.get(group) == null) { + groups.put(group, new ArrayList()); + } + groups.get(group).add(variable); + } } public void addSubscriptVariable(SubscriptVariable variable, String group) throws Exception { - Variable original = variables.get(variable.getName()); + MdlVariable original = variables.get(variable.getName()); if (original != null) { if (!(original instanceof SubscriptVariable)) { throw new Exception("incompatible type for variable "+variable.getName()); @@ -51,26 +57,16 @@ public class MdlModel { ((SubscriptVariable)original).addSubscriptVariable(variable); } else { - variables.put(variable.getName(), variable); - addGroup(variable, group); - } - } - - private void addGroup(Variable variable, String group) { - if (group != null) { - if (groups.get(group) == null) { - groups.put(group, new ArrayList()); - } - groups.get(group).add(variable); + addVariable(variable, group); } } - public Variable getVariable(String name) { + public MdlVariable getVariable(String name) { return variables.get(name); } - public Set getAllVariables() { - return new HashSet(variables.values()); + public Set getAllVariables() { + return new HashSet(variables.values()); } public void addLookup(Lookup lookup) @@ -96,30 +92,24 @@ public class MdlModel { throw new Exception("duplicate subscript "+subscript.getName()); } - // TODO: check if a subscript with the same values already exists - subscripts.put(subscript.getName(), subscript); } - public Subscript getSubscript(String key) { - return subscripts.get(key); + public Subscript getSubscript(String name) { + return subscripts.get(name); } public Set getAllSubscripts() { return new HashSet(subscripts.values()); } - public Subscript resolveSubscript(List values) { - return resolveSubscript(new HashSet(values)); - } - public Subscript resolveSubscript(Set values) { - // if one value is actually a subscript, expand it to its values + // if a value is actually a subscript, expand it to its values Set all = new HashSet(); for (String value : values) { Subscript potential = subscripts.get(value); if (potential != null) { - all.addAll(potential.getValues(this)); + all.addAll(potential.getValues()); } else { all.add(value); @@ -129,7 +119,9 @@ public class MdlModel { // find the subscript that contains exactly the given set of values. // only consider original subscripts (ignore equivalent subscripts) for (Subscript subscript : getAllSubscripts()) { - if (!subscript.isEquivalent() && subscript.getValues(this).containsAll(all) && all.containsAll(subscript.getValues(this))) { + if (!subscript.isEquivalent() && + subscript.getValues().containsAll(all) && + all.containsAll(subscript.getValues())) { return subscript; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java similarity index 84% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java index 25f21fa2..6e45c926 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlVariable.java @@ -1,18 +1,17 @@ package org.simantics.sysdyn.modelImport.mdl; -import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.simantics.sysdyn.modelImport.MdlUtil; -import org.simantics.sysdyn.modelImport.model.ModelVariable; +import org.simantics.sysdyn.modelImport.model.Variable; import org.simantics.sysdyn.modelImport.model.expression.DelayExpression; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; import org.simantics.sysdyn.modelImport.model.expression.NormalExpression; import org.simantics.sysdyn.modelImport.model.support.Range; -public class Variable extends Declaration { +public class MdlVariable extends Declaration { protected static final String EXPRESSION = "(?:\\s*=\\s*([^~]*?(?:"+MdlUtil.SPECIAL_NAME+"[^~]*?)*))?"; @@ -25,12 +24,12 @@ public class Variable extends Declaration { private String expression; - protected Variable(String name, String expression) { - super(name); + protected MdlVariable(String name, MdlModel mdl, String expression) { + super(name, mdl); this.expression = expression; } - public static Variable getPossible(String line) + public static MdlVariable getPossible(String line, MdlModel mdl) throws Exception { Matcher matcher = Pattern.compile(VARIABLE_DECL).matcher(line); if (!matcher.matches()) { @@ -42,7 +41,7 @@ public class Variable extends Declaration { if (expression != null) expression = MdlUtil.normalize(expression); - Variable var = new Variable(name, expression); + MdlVariable var = new MdlVariable(name, mdl, expression); var.parseSuffix(matcher.group(variableSuffix)); return var; } @@ -51,11 +50,10 @@ public class Variable extends Declaration { return expression; } - public T setUpModelVariable(T variable, - double[] dimensions, MdlModel mdl) { + public T initVariable(T variable) { variable.setName(getName()); if (expression != null) - variable.setExpression(getExpression(mdl)); + variable.setExpression(getExpression()); String unit = getUnit(); Range range = parseRange(unit); @@ -67,13 +65,11 @@ public class Variable extends Declaration { variable.setRange(range); variable.setDescription(getDescription()); - variable.setDimensions(dimensions); - return variable; } - public Expression getExpression(MdlModel mdl) { - return parseExpression(MdlUtil.finalize(expression, mdl)); + public Expression getExpression() { + return parseExpression(MdlUtil.finalize(expression, getMdl())); } protected static Expression parseExpression(String expression) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Sketch.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Sketch.java index cba3e0c9..89f40f87 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Sketch.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Sketch.java @@ -2,20 +2,21 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import org.simantics.sysdyn.modelImport.model.Symbol; - public class Sketch { + private MdlModel mdl; private String name; - private boolean edgesOutOfDate; - public double topEdge = 0; - public double bottomEdge = 0; - public double leftEdge = 0; - public double rightEdge = 0; + private int xOffset = 0; + private int yOffset = 0; + + private boolean updateEdges; + private int topEdge = 0; + private int bottomEdge = 0; + private int leftEdge = 0; + private int rightEdge = 0; private List comments; private List connections; @@ -24,8 +25,10 @@ public class Sketch { private HashMap objects; - public Sketch() { - edgesOutOfDate = true; + public Sketch(MdlModel mdl) { + this.mdl = mdl; + + updateEdges = true; comments = new ArrayList(); connections = new ArrayList(); @@ -35,6 +38,10 @@ public class Sketch { objects = new HashMap(); } + public MdlModel getMdl() { + return mdl; + } + public String getName() { return name; } @@ -44,67 +51,49 @@ public class Sketch { } private void updateEdges() { -// if (edgesOutOfDate) { -// boolean first = true; -// for (SketchElement e : getAllElements()) { -// topEdge = first ? e.getSysdynTopEdge() : Math.min(topEdge, e.getSysdynTopEdge()); -// bottomEdge = first ? e.getSysdynBottomEdge() : Math.max(bottomEdge, e.getSysdynBottomEdge()); -// leftEdge = first ? e.getSysdynLeftEdge() : Math.min(leftEdge, e.getSysdynLeftEdge()); -// rightEdge = first ? e.getSysdynRightEdge() : Math.max(rightEdge, e.getSysdynRightEdge()); -// first = false; -// } -// edgesOutOfDate = false; -// } - } - - public double getTopEdge() { - updateEdges(); - return topEdge; - } - - public double getBottomEdge() { - updateEdges(); - return bottomEdge; - } - - public double getLeftEdge() { - updateEdges(); - return leftEdge; - } - - public double getRightEdge() { - updateEdges(); - return rightEdge; + if (updateEdges) { + boolean first = true; + for (SketchElement e : getAllElements()) { + topEdge = first ? e.getTop() : Math.min(topEdge, e.getTop() ); + bottomEdge = first ? e.getBottom() : Math.max(bottomEdge, e.getBottom()); + leftEdge = first ? e.getLeft() : Math.min(leftEdge, e.getLeft()); + rightEdge = first ? e.getRight() : Math.max(rightEdge, e.getRight()); + first = false; + } + updateEdges = false; + } } - public double getWidth() { + public int getWidth() { updateEdges(); return rightEdge - leftEdge; } - public double getHeight() { + public int getHeight() { updateEdges(); return bottomEdge - topEdge; } - public void setOffset(double horizontal, double vertical) { - hOffset = horizontal; - vOffset = vertical; + // sets the relative location of this sketch in the diagram + public void setLocation(int x, int y) { + xOffset = x; + yOffset = y; } - private double hOffset, vOffset; - - public double getHorizontalOffset() { - return hOffset; + public int getElementXOffset() { + updateEdges(); + return -leftEdge + xOffset; } - public double getVerticalOffset() { - return vOffset; + public int getElementYOffset() { + updateEdges(); + return -topEdge + yOffset; } public void addComment(SketchComment comment) { comments.add(comment); objects.put(comment.getId(), comment); + updateEdges = true; } public List getComments() { @@ -126,6 +115,7 @@ public class Sketch { // replace the attached variable with the valve in order to redirect // possible connections to the variable to the valve objects.put(valve.getAttached().getId(), valve); + updateEdges = true; } public List getValves() { @@ -135,6 +125,7 @@ public class Sketch { public void addVariable(SketchVariable variable) { variables.add(variable); objects.put(variable.getId(), variable); + updateEdges = true; } public List getVariables() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java index e0d0eb13..3c4b2384 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java @@ -22,12 +22,12 @@ public class SketchComment extends SketchElement { private String text; private CommentIcon icon; - protected SketchComment(int id, CommentIcon icon) { - super(id); + protected SketchComment(int id, Sketch sketch, CommentIcon icon) { + super(id, sketch); this.icon = icon; } - public static SketchComment getPossible(String line) + public static SketchComment getPossible(String line, Sketch sketch) throws Exception { Matcher matcher = Pattern.compile(SKETCH_COMMENT).matcher(line); if (!matcher.matches()) { @@ -42,7 +42,7 @@ public class SketchComment extends SketchElement { default: icon = CommentIcon.OTHER; break; } - SketchComment element = new SketchComment(id, icon); + SketchComment element = new SketchComment(id, sketch, icon); element.parseSuffix(matcher.group(commentSuffix)); return element; } @@ -60,13 +60,13 @@ public class SketchComment extends SketchElement { } @Override - public Symbol getSymbol(MdlModel mdl, Sketch sketch) { + public Symbol getSymbol() { if (isInputOutput()) return new Comment(getDimensions(), "I/O objects are not supported"); switch(icon) { - case CLOUD: return new Cloud(getDimensions(sketch)); - case OTHER: return new Comment(getDimensions(sketch), text); + case CLOUD: return new Cloud(getDimensions()); + case OTHER: return new Comment(getDimensions(), text); default: return null; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java index cd20a7b0..e1fe384a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java @@ -42,15 +42,15 @@ public class SketchConnection extends SketchObject { private ConnectionType type; int[] points; - protected SketchConnection(int id, int from, int to, ConnectionType type, int[] points) { - super(id); + protected SketchConnection(int id, Sketch sketch, int from, int to, ConnectionType type, int[] points) { + super(id, sketch); this.from = from; this.to = to; this.type = type; this.points = points; } - public static SketchConnection getPossible(String line) + public static SketchConnection getPossible(String line, Sketch sketch) throws Exception { Matcher matcher = Pattern.compile(SKETCH_CONNECTION).matcher(line); if (!matcher.matches()) { @@ -75,7 +75,7 @@ public class SketchConnection extends SketchObject { int[] points = parsePoints(matcher.group(connectionPoints)); - return new SketchConnection(id, from, to, type, points); + return new SketchConnection(id, sketch, from, to, type, points); } private static int[] parsePoints(String str) { @@ -108,18 +108,18 @@ public class SketchConnection extends SketchObject { return points; } - public Connection getConnection(Sketch sketch) { - Symbol tail = (Symbol)sketch.getObject(from).getModelObject(); + public Connection getConnection() { + Symbol tail = (Symbol)getSketch().getObject(from).getModelObject(); if (tail == null) { return null; } - Symbol head = (Symbol)sketch.getObject(to).getModelObject(); + Symbol head = (Symbol)getSketch().getObject(to).getModelObject(); if (head == null) { return null; } switch (type) { - case ARROW: return new Dependency(tail, head, true, false, getAngle(sketch)); + case ARROW: return new Dependency(tail, head, true, false, getAngle(getSketch())); case LINE_ARROW: return new Flow(tail, head); case LINE_SEGMENT: return new Flow(head, tail); default: return null; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java index 3507db92..ab4eae9e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java @@ -3,14 +3,12 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.simantics.sysdyn.modelImport.MdlUtil; import org.simantics.sysdyn.modelImport.model.Symbol; import org.simantics.sysdyn.modelImport.model.Valve.TextPosition; public abstract class SketchElement extends SketchObject { - // multiplier for dimension conversion between vensim and sysdyn - private static final double SCALE_MULTIPLIER = 0.4; - // each sketch element is declared with a string: // n,id,name,x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font) @@ -41,8 +39,8 @@ public abstract class SketchElement extends SketchObject { private boolean textLine; private TextPosition textPosition; - protected SketchElement(int id) { - super(id); + protected SketchElement(int id, Sketch sketch) { + super(id, sketch); } protected void parseSuffix(String suffix) @@ -91,6 +89,22 @@ public abstract class SketchElement extends SketchObject { return height; } + public int getTop() { + return getY() - getHeight(); + } + + public int getBottom() { + return getY() + getHeight(); + } + + public int getLeft() { + return getX() - getWidth(); + } + + public int getRight() { + return getX() + getWidth(); + } + public boolean isAttached() { return attached; } @@ -115,23 +129,16 @@ public abstract class SketchElement extends SketchObject { return textPosition; } - public abstract Symbol getSymbol(MdlModel mdl, Sketch sketch); - - public double[] getDimensions(Sketch sketch) { - double[] dimensions = getDimensions(); - dimensions[0] = dimensions[0] + sketch.getHorizontalOffset(); - dimensions[1] = dimensions[1] + sketch.getVerticalOffset(); - return dimensions; - } + public abstract Symbol getSymbol(); - // get element dimensions [x, y, width, height] + // get sysdyn dimensions for element [x, y, width, height] public double[] getDimensions() { - double[] dimensions = new double[4]; - dimensions[0] = (getX() - getWidth()) * SCALE_MULTIPLIER; - dimensions[1] = (getY() - getHeight()) * SCALE_MULTIPLIER; - dimensions[2] = getWidth() * 2 * SCALE_MULTIPLIER; - dimensions[3] = getHeight() * 2 * SCALE_MULTIPLIER; - return dimensions; + return MdlUtil.getSysdynDimensions( + getLeft() + getSketch().getElementXOffset(), + getTop() + getSketch().getElementYOffset(), + getWidth() * 2, + getHeight() * 2 + ); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java index 2ddb1e4c..a4c2ac47 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java @@ -11,16 +11,23 @@ public abstract class SketchObject { protected static final String SKIP_ANY = "[^,]*,"; private int id; + private Sketch sketch; + private IWriteableObject modelObject; - protected SketchObject(int id) { + protected SketchObject(int id, Sketch sketch) { this.id = id; + this.sketch = sketch; } public int getId() { return id; } + public Sketch getSketch() { + return sketch; + } + public void setModelObject(IWriteableObject modelObject) { this.modelObject = modelObject; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java index f9d5bd38..7f385314 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java @@ -15,11 +15,11 @@ public class SketchValve extends SketchElement { private SketchVariable attached; - protected SketchValve(int id) { - super(id); + protected SketchValve(int id, Sketch sketch) { + super(id, sketch); } - public static SketchValve getPossible(String line) + public static SketchValve getPossible(String line, Sketch sketch) throws Exception { Matcher matcher = Pattern.compile(SKETCH_VALVE).matcher(line); if (!matcher.matches()) { @@ -28,7 +28,7 @@ public class SketchValve extends SketchElement { int id = Integer.parseInt(matcher.group(valveId)); - SketchValve element = new SketchValve(id); + SketchValve element = new SketchValve(id, sketch); element.parseSuffix(matcher.group(valveSuffix)); return element; } @@ -42,15 +42,16 @@ public class SketchValve extends SketchElement { } @Override - public Valve getSymbol(MdlModel mdl, Sketch sketch) { - Variable variable = mdl.getVariable(attached.getName()); + public Valve getSymbol() { + MdlVariable variable = getSketch().getMdl().getVariable(attached.getName()); if (variable == null) { return null; } Valve valve = new Valve(Orientation.HORIZONTAL, getTextPosition()); + valve.setDimensions(getDimensions()); - return variable.setUpModelVariable(valve, getDimensions(sketch), mdl); + return variable.initVariable(valve); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java index f6f3a1f0..9bcbeba8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java @@ -5,10 +5,8 @@ import java.util.regex.Pattern; import org.simantics.sysdyn.modelImport.MdlUtil; import org.simantics.sysdyn.modelImport.model.Auxiliary; -import org.simantics.sysdyn.modelImport.model.ModelVariable; +import org.simantics.sysdyn.modelImport.model.Variable; import org.simantics.sysdyn.modelImport.model.Stock; -import org.simantics.sysdyn.modelImport.model.Symbol; -import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; public class SketchVariable extends SketchElement { @@ -20,12 +18,12 @@ public class SketchVariable extends SketchElement { private String name; - protected SketchVariable(int id, String name) { - super(id); + protected SketchVariable(int id, Sketch sketch, String name) { + super(id, sketch); this.name = name; } - public static SketchVariable getPossible(String line) + public static SketchVariable getPossible(String line, Sketch sketch) throws Exception { Matcher matcher = Pattern.compile(SKETCH_VARIABLE).matcher(line); if (!matcher.matches()) { @@ -35,7 +33,7 @@ public class SketchVariable extends SketchElement { int id = Integer.parseInt(matcher.group(variableId)); String name = MdlUtil.normalize(matcher.group(variableName)); - SketchVariable element = new SketchVariable(id, name); + SketchVariable element = new SketchVariable(id, sketch, name); element.parseSuffix(matcher.group(variableSuffix)); return element; } @@ -45,20 +43,20 @@ public class SketchVariable extends SketchElement { } @Override - public ModelVariable getSymbol(MdlModel mdl, Sketch sketch) { - Variable variable = mdl.getVariable(name); + public Variable getSymbol() { + MdlVariable variable = getSketch().getMdl().getVariable(name); if (variable == null) { return null; } - ModelVariable var; - + Variable var; if (variable.getExpressionString() != null && variable.getExpressionString().startsWith("INTEG")) var = new Stock(); else var = new Auxiliary(); + var.setDimensions(getDimensions()); - return variable.setUpModelVariable(var, getDimensions(sketch), mdl); + return variable.initVariable(var); } } \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java index 820a608f..c6b4a940 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java @@ -1,9 +1,7 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,19 +22,19 @@ public class Subscript extends Declaration { private Enumeration enumeration; - protected Subscript(String name, List values) { - super(name); + protected Subscript(String name, MdlModel mdl, List values) { + super(name, mdl); this.original = null; this.values = values; } - protected Subscript(String name, String original) { - super(name); + protected Subscript(String name, MdlModel mdl, String original) { + super(name, mdl); this.original = original; this.values = null; } - public static Subscript getPossible(String line) + public static Subscript getPossible(String line, MdlModel mdl) throws Exception { Matcher matcher = Pattern.compile(SUBSCRIPT_DECL).matcher(line); if (!matcher.matches()) @@ -48,9 +46,9 @@ public class Subscript extends Declaration { Subscript subscript; if (equivalent) - subscript = new Subscript(name, expression); + subscript = new Subscript(name, mdl, expression); else - subscript = new Subscript(name, Arrays.asList(expression.split(","))); + subscript = new Subscript(name, mdl, Arrays.asList(expression.split(","))); subscript.parseSuffix(matcher.group(subscriptSuffix)); return subscript; } @@ -59,20 +57,20 @@ public class Subscript extends Declaration { return original; } - public List getValues(MdlModel mdl) { + public List getValues() { if (values != null) return values; else - return mdl.getSubscript(original).getValues(mdl); + return getMdl().getSubscript(original).getValues(); } public boolean isEquivalent() { return original != null; } - public Enumeration getEnumeration(MdlModel mdl) { + public Enumeration getEnumeration() { if (enumeration == null) { - enumeration = new Enumeration(getName(), getValues(mdl)); + enumeration = new Enumeration(getName(), getValues()); } return enumeration; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java index 23bf744e..2bbe092b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java @@ -15,7 +15,7 @@ import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression; import org.simantics.sysdyn.modelImport.model.expression.NormalExpression; import org.simantics.sysdyn.modelImport.model.support.Enumeration; -public class SubscriptVariable extends Variable { +public class SubscriptVariable extends MdlVariable { // a subscript variable is a variable that has a different equation // depending on the value of the subscript it references @@ -29,13 +29,13 @@ public class SubscriptVariable extends Variable { private String[] indices; private SubscriptVariable next; - protected SubscriptVariable(String name, String expression, String[] indices) { - super(name, expression); + protected SubscriptVariable(String name, MdlModel mdl, String expression, String[] indices) { + super(name, mdl, expression); this.indices = indices; this.next = null; } - public static SubscriptVariable getPossible(String line) + public static SubscriptVariable getPossible(String line, MdlModel mdl) throws Exception { Matcher matcher = Pattern.compile(SUBSCRIPT_VARIABLE_DECL).matcher(line); if (!matcher.matches()) { @@ -48,7 +48,7 @@ public class SubscriptVariable extends Variable { if (expression != null) expression = MdlUtil.normalize(expression); - SubscriptVariable subVar = new SubscriptVariable(name, expression, indices); + SubscriptVariable subVar = new SubscriptVariable(name, mdl, expression, indices); subVar.parseSuffix(matcher.group(subscriptVariableSuffix)); return subVar; } @@ -74,7 +74,7 @@ public class SubscriptVariable extends Variable { } @Override - public EnumerationExpression getExpression(MdlModel mdl) { + public EnumerationExpression getExpression() { SubscriptVariable var; // find out what subscripts the combined expression uses by collecting @@ -101,7 +101,7 @@ public class SubscriptVariable extends Variable { List enumerations = new ArrayList(); for (Set values : subscripts) { - Subscript potential = mdl.resolveSubscript(values); + Subscript potential = getMdl().resolveSubscript(values); if (potential == null) { System.err.println("subscript indices could not be resolved "); for (String value : values) { @@ -109,42 +109,15 @@ public class SubscriptVariable extends Variable { } System.err.println(); } - enumerations.add(potential.getEnumeration(mdl)); + enumerations.add(potential.getEnumeration()); } EnumerationExpression expr = new EnumerationExpression(enumerations); - // populate the created expression (TODO: comment) + // populate the created expression - // TODO: is this check correct, also does not work correctly yet + // TODO: is this check correct? if (next == null && enumerations.size() == 2) { - // option a: -// StringBuilder buffer = new StringBuilder(); -// buffer.append('{'); -// for (int i = 0; i < values.length; i++) { -// if (i > 0) { -// buffer.append(','); -// } -// buffer.append('{'); -// for (int j = 0; j < values[i].length; j++) { -// if (j > 0) { -// buffer.append(','); -// } -// buffer.append(values[i][j]); -// } -// buffer.append('}'); -// } -// buffer.append('}'); -// -// String[] exprindices = new String[enumerations.size()]; -// for (int i = 0; i < exprindices.length; i++) { -// exprindices[i] = enumerations.get(i).getName(); -// } -// -// expr.addExpression(new NormalExpression(buffer.toString()), exprindices); -// return expr; - - // option b: (probably more sensible) double[][] values = getPossibleValueArray(getExpressionString()); if (values != null) { for (int i = 0; i < values.length; i++) { @@ -169,9 +142,9 @@ public class SubscriptVariable extends Variable { int limit = workqueue.size(); for (int j = 0; j < limit; j++) { WorkExpression current = workqueue.pollFirst(); - Subscript potential = mdl.getSubscript(current.indices[i]); + Subscript potential = getMdl().getSubscript(current.indices[i]); if (potential != null) { - for (String value : potential.getValues(mdl)) { + for (String value : potential.getValues()) { String[] newindices = Arrays.copyOf(current.indices, current.indices.length); newindices[i] = value; String newexpression = current.expression; @@ -186,8 +159,8 @@ public class SubscriptVariable extends Variable { } for (WorkExpression we : workqueue) { - // TODO: is this check correct - String expression = MdlUtil.finalize(we.expression, mdl); + String expression = MdlUtil.finalize(we.expression, getMdl()); + // TODO: is this check correct? if (next == null) { expression = removeComparisons(expression, indices, we.indices); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java index 871d2106..73d2e280 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java @@ -6,7 +6,7 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; -public class Auxiliary extends ModelVariable { +public class Auxiliary extends Variable { public Auxiliary() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java index 65cc051d..7f1326a8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java @@ -14,6 +14,8 @@ package org.simantics.sysdyn.modelImport.model; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.simantics.databoard.Bindings; import org.simantics.db.Resource; @@ -32,39 +34,30 @@ public class Model implements IWriteableObject { private String name; // necessary simulation parameters - private double start, stop, step; + private Double start, stop, step; private String unit; // the structure of the model - private HashMap variables; - private HashMap enumerations; - private HashMap functions; + private Map variables; + private Map enumerations; + private Map functions; - private ArrayList symbols; - private ArrayList shadows; - private ArrayList connections; + private List symbols; + private List shadows; + private List connections; private Resource model; public Model(String name) { this.name = name; - variables = new HashMap(); + variables = new HashMap(); enumerations = new HashMap(); functions = new HashMap(); symbols = new ArrayList(); shadows = new ArrayList(); connections = new ArrayList(); - - this.start = 0; - this.stop = 100; - this.step = 1; - this.unit = null; - } - - public void setParameters(double start, double stop, double step, String unit) { - } public double getStart() { @@ -91,7 +84,15 @@ public class Model implements IWriteableObject { this.step = step; } - public void addSymbol(ModelVariable variable) { + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public void addSymbol(Variable variable) { if (variables.get(variable.getName()) != null) { System.err.println("variable "+variable.getName()+" already defined"); } @@ -102,11 +103,11 @@ public class Model implements IWriteableObject { symbols.add(variable); } - public ModelVariable getVariable(String name) { + public Variable getVariable(String name) { return variables.get(name); } - public Collection getVariables() { + public Collection getVariables() { return variables.values(); } @@ -166,10 +167,14 @@ public class Model implements IWriteableObject { // TODO: this must be updated if/when simulation parameters are moved // from model to experiment - //graph.claimLiteral(model, sr.SysdynModel_startTime, start, Bindings.DOUBLE); - //graph.claimLiteral(model, sr.SysdynModel_stopTime, stop, Bindings.DOUBLE); - //graph.claimLiteral(model, sr.SysdynModel_simulationStepLength, step, Bindings.DOUBLE); - //graph.claimLiteral(model, sr.SysdynModel_timeUnit, unit, Bindings.STRING); + if (start != null) + graph.claimLiteral(model, sr.SysdynModel_startTime, start, Bindings.DOUBLE); + if (stop != null) + graph.claimLiteral(model, sr.SysdynModel_stopTime, stop, Bindings.DOUBLE); + if (step != null) + graph.claimLiteral(model, sr.SysdynModel_simulationStepLength, step, Bindings.DOUBLE); + if (unit != null) + graph.claimLiteral(model, sr.SysdynModel_timeUnit, unit, Bindings.STRING); Resource configuration = graph.getSingleObject(model, sim.HasConfiguration); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow.java index 2e7d2689..228b5099 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow.java @@ -11,7 +11,7 @@ import org.simantics.sysdyn.SysdynResource; public class Shadow extends Symbol { - private ModelVariable original; + private Variable original; private Resource shadow; @@ -19,20 +19,20 @@ public class Shadow extends Symbol { } - public Shadow(ModelVariable original) { + public Shadow(Variable original) { this.original = original; } - public Shadow(double[] dim, ModelVariable original) { + public Shadow(double[] dim, Variable original) { super(dim); this.original = original; } - public ModelVariable getOriginal() { + public Variable getOriginal() { return original; } - public void setOriginal(ModelVariable original) { + public void setOriginal(Variable original) { this.original = original; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java index 76842a2d..1e658ed8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java @@ -6,7 +6,7 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; import org.simantics.sysdyn.modelImport.model.support.Range; -public class Stock extends ModelVariable { +public class Stock extends Variable { public Stock() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java index 9a84e094..011b3c15 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java @@ -8,7 +8,7 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; -public class Valve extends ModelVariable { +public class Valve extends Variable { public enum Orientation { HORIZONTAL, VERTICAL diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable.java similarity index 86% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable.java index 5e64585d..c29b46bc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable.java @@ -12,7 +12,7 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; -public abstract class ModelVariable extends Symbol { +public abstract class Variable extends Symbol { private String name; private Expression expression; @@ -22,11 +22,11 @@ public abstract class ModelVariable extends Symbol { private Resource variable; - public ModelVariable() { + public Variable() { } - public ModelVariable(String name, Expression expression, Range range, String unit, String description) { + public Variable(String name, Expression expression, Range range, String unit, String description) { this.name = name; this.expression = expression; this.range = range; @@ -34,7 +34,7 @@ public abstract class ModelVariable extends Symbol { this.description = description; } - public ModelVariable(double[] dim, String name, Expression expression, Range range, String unit, String description) { + public Variable(double[] dim, String name, Expression expression, Range range, String unit, String description) { super(dim); this.name = name; this.expression = expression; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java index c3246adc..bfccc31f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java @@ -54,7 +54,7 @@ public class EnumerationExpression extends Expression { // write array index list graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist)); - // set active expression? + // TODO: set active expression? return getResource(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java index 5ea53010..2a6e8e92 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java @@ -1,9 +1,7 @@ package org.simantics.sysdyn.modelImport.model.support; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java index 8aa761f1..9f17087e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java @@ -1,7 +1,5 @@ package org.simantics.sysdyn.modelImport.model.support; -import java.util.List; - import org.simantics.databoard.Bindings; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -54,6 +52,8 @@ public class Function implements IWriteableObject { graph.claimLiteral(function, l0.HasDescription, body, Bindings.STRING); } + // TODO: should alco handle inputs and outputs somehow + return function; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java index e28c6b68..bfcf236f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java @@ -34,6 +34,7 @@ public class Range implements IWriteableObject { @Override public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException { + // TODO: implementation return null; }