From 2ee59eace7e39eb6285bf9593009758eaacaff0c Mon Sep 17 00:00:00 2001 From: jkauttio Date: Thu, 13 Mar 2014 15:40:31 +0000 Subject: [PATCH] Update vensim import, sketches are imported successfully but equation handling is still missing refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches@29119 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/handlers/imports/ImportMdlHandler.java | 5 +- .../sysdyn/modelImport/MdlParser2.java | 147 +++++++++++++----- .../sysdyn/modelImport/MdlUtils.java | 76 ++++++--- .../sysdyn/modelImport/model/Auxiliary2.java | 36 ++--- .../sysdyn/modelImport/model/Cloud.java | 1 - .../sysdyn/modelImport/model/Cloud2.java | 32 +++- .../sysdyn/modelImport/model/Comment2.java | 38 +++++ .../sysdyn/modelImport/model/Connection2.java | 68 ++++++-- .../sysdyn/modelImport/model/Dependency2.java | 22 +-- .../sysdyn/modelImport/model/Element2.java | 39 ++--- .../sysdyn/modelImport/model/Flow2.java | 22 +-- .../sysdyn/modelImport/model/IExpression.java | 5 + .../modelImport/model/IntegralExpression.java | 36 +++++ .../sysdyn/modelImport/model/MdlModel.java | 5 + .../sysdyn/modelImport/model/Model2.java | 12 +- .../modelImport/model/ModelVariable.java | 34 ++-- .../modelImport/model/NormalExpression.java | 32 ++++ .../sysdyn/modelImport/model/Range.java | 37 ++++- .../sysdyn/modelImport/model/Shadow2.java | 44 ++++++ .../sysdyn/modelImport/model/Sketch2.java | 109 ++++++------- .../modelImport/model/SketchComment.java | 34 ++-- .../modelImport/model/SketchConnection.java | 58 +++---- .../modelImport/model/SketchElement.java | 44 +++++- .../modelImport/model/SketchObject.java | 4 +- .../sysdyn/modelImport/model/SketchValve.java | 9 +- .../modelImport/model/SketchVariable.java | 39 +---- .../sysdyn/modelImport/model/Stock2.java | 39 ++--- .../sysdyn/modelImport/model/Valve2.java | 36 ++++- .../sysdyn/modelImport/model/Variable2.java | 29 ++-- 29 files changed, 722 insertions(+), 370 deletions(-) create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IExpression.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java diff --git a/dev-jkauttio/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java b/dev-jkauttio/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java index b9855c8f..fe212805 100644 --- a/dev-jkauttio/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java +++ b/dev-jkauttio/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java @@ -28,6 +28,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.modelImport.MdlParser; import org.simantics.sysdyn.modelImport.MdlParser2; import org.simantics.sysdyn.modelImport.model.Model; +import org.simantics.sysdyn.modelImport.model.Model2; import org.simantics.sysdyn.ui.Activator; import org.simantics.ui.SimanticsUI; @@ -67,13 +68,13 @@ public class ImportMdlHandler extends AbstractHandler { // Convert Vensim model to Simantics SysDyn format using MdlParser MdlParser2 parser = new MdlParser2(); - parser.parse(file); + final Model2 model = parser.parse(file); SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { - //model.write(graph, project); + model.write(graph, project); } }); diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser2.java index a4c3f805..f5e8b642 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser2.java @@ -19,16 +19,23 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; +import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon; +import org.simantics.sysdyn.modelImport.model.Auxiliary2; +import org.simantics.sysdyn.modelImport.model.Cloud2; +import org.simantics.sysdyn.modelImport.model.Comment2; import org.simantics.sysdyn.modelImport.model.Connection2; import org.simantics.sysdyn.modelImport.model.Element2; -import org.simantics.sysdyn.modelImport.model.Model; +import org.simantics.sysdyn.modelImport.model.IntegralExpression; import org.simantics.sysdyn.modelImport.model.Model2; +import org.simantics.sysdyn.modelImport.model.Shadow2; import org.simantics.sysdyn.modelImport.model.Sketch2; import org.simantics.sysdyn.modelImport.model.SketchComment; import org.simantics.sysdyn.modelImport.model.SketchConnection; import org.simantics.sysdyn.modelImport.model.SketchElement; import org.simantics.sysdyn.modelImport.model.SketchValve; import org.simantics.sysdyn.modelImport.model.SketchVariable; +import org.simantics.sysdyn.modelImport.model.Stock2; +import org.simantics.sysdyn.modelImport.model.Valve2; import org.simantics.sysdyn.modelImport.model.Variable2; /* @@ -43,13 +50,17 @@ import org.simantics.sysdyn.modelImport.model.Variable2; public class MdlParser2 { private static final String UTF_8 = "{UTF-8}"; - private static final String CONTROL_STR = ".Control"; private static final String SKETCH_VERSION = "V300"; + private static final String CATEGORY_CONTROL = "Control"; + // each .mdl is divided into three sections, these are the the delimiter // strings used to identify where each section starts private static final String SKETCH_START = "\\\\\\---///"; private static final String SKETCH_END = "///---\\\\\\"; + + private static final double H_SPACE = 10; + private static final double V_SPACE = 0; private HashMap variables; private HashMap controls; @@ -61,7 +72,7 @@ public class MdlParser2 { sketches = new ArrayList(); } - public Model parse(File file) { + public Model2 parse(File file) { try { // peek at the first line to see if we need to use UTF-8 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); @@ -118,29 +129,91 @@ public class MdlParser2 { Model2 model = new Model2(file.getName()); - int offset = 0; + double hOffset = 0; + double vOffset = V_SPACE; + + // do this in several passes - for (Sketch2 sketch : sketches) { - // must keep track of which elements in the sketch correspond to - // which elements in the model so connections can be constructed - // accurately - HashMap elementMap = new HashMap(); + HashMap allVariables = new HashMap(); + + // add sketch labels and independent elements + for (int i = 0; i < sketches.size(); i++) { + Sketch2 sketch = sketches.get(i); + + sketch.hOffset = hOffset; + sketch.vOffset = vOffset; - for (SketchElement element : sketch.getElements()) { - Element2 e = element.getWriteableElement(0, 0); - //model.addElement(e); - //elementMap.put(element, e); + model.addElement(new Comment2(sketch.getWidth() / 2 + sketch.hOffset, 0, sketch.getName())); + + for (SketchComment comment : sketch.getComments()) { + if (comment.isInputOutput()) { + // input / output objects are not supported yet + System.err.println("input / output objects are not supported yet"); + continue; + } + + Element2 modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset); + model.addElement(modelElement); + sketch.elements.put(comment.getId(), modelElement); } - for (SketchConnection connection : sketch.getConnections()) { - Connection2 c = connection.getWriteableConnection(); - //model.addConnection(c); + for (SketchValve valve : sketch.getValves()) { + Element2 modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset); + model.addElement(modelElement); + sketch.elements.put(valve.getId(), modelElement); + sketch.elements.put(valve.getAttachedVariable().getId(), modelElement); + allVariables.put(valve.getAttachedVariable().getVariable(), modelElement); + } + + for (SketchVariable variable : sketch.getVariables()) { + if (!variable.allowsIn()) { + // the variable is a shadow variable, skip these for now + continue; + } + + if (variable.isAttached()) { + // the variable is attached to a valve, already handled + continue; + } + + Element2 modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset); + model.addElement(modelElement); + sketch.elements.put(variable.getId(), modelElement); + allVariables.put(variable.getVariable(), modelElement); + } + + hOffset += sketch.getWidth() + H_SPACE; + } + + // add dependent elements + for (int i = 0; i < sketches.size(); i++) { + Sketch2 sketch = sketches.get(i); + + for (SketchVariable variable : sketch.getVariables()) { + if (!variable.allowsIn()) { + // the variable is a shadow variable + Element2 original = allVariables.get(variable.getVariable()); + if (original == null) { + System.err.println("original not found"); + continue; + } + Shadow2 modelElement = new Shadow2(variable.getX(sketch.hOffset), variable.getY(sketch.vOffset), allVariables.get(variable.getVariable())); + model.addShadow(modelElement); + sketch.elements.put(variable.getId(), modelElement); + } } - offset += sketch.getWidth() + 100; + for (SketchConnection connection : sketch.getConnections()) { + Element2 head = sketch.elements.get(connection.getTo()); + Element2 tail = sketch.elements.get(connection.getFrom()); + Connection2 c = connection.getWriteableConnection(head, tail); + if (c != null) { + model.addConnection(c); + } + } } - return null; + return model; } private String readVariables(BufferedReader reader, String line) @@ -155,7 +228,7 @@ public class MdlParser2 { StringBuilder buffer = new StringBuilder(line); - while ((line = reader.readLine()) != null) { + do { if (line.endsWith("\\")) buffer.append(line.substring(0, line.length()-1)); else @@ -163,15 +236,25 @@ public class MdlParser2 { if (line.endsWith("|")) break; - } + } while ((line = reader.readLine()) != null); String str = buffer.toString(); - - // TODO: must handle categories other than .Control + + // handle category declarations + String cat = MdlUtils.getPossibleCategory(str); + if (cat != null) { + category = cat; + continue; + } Variable2 var = MdlUtils.getPossibleVariable(str, category); if (var != null) { - variables.put(var.getName(), var); + if (CATEGORY_CONTROL.equals(category)) { + controls.put(var.getName(), var); + } + else { + variables.put(var.getName(), var); + } continue; } @@ -208,13 +291,13 @@ public class MdlParser2 { SketchConnection connection = MdlUtils.getPossibleSketchConnection(line); if (connection != null) { - sketch.addSketchObject(connection); + sketch.addConnection(connection); continue; } SketchVariable variable = MdlUtils.getPossibleSketchVariable(line, variables); if (variable != null) { - sketch.addSketchObject(variable); + sketch.addVariable(variable); continue; } @@ -228,23 +311,17 @@ public class MdlParser2 { continue; } valve.setAttachedVariable(attached); - sketch.addSketchObject(valve); - sketch.addSketchObject(attached); + sketch.addValve(valve); + sketch.addVariable(attached); continue; } SketchComment comment = MdlUtils.getPossibleSketchComment(line); if (comment != null) { - if (comment.hasTextNextLine()) { + if (comment.hasTextLine()) { comment.setText(reader.readLine()); } - - if (comment.isIOElement()) { - System.err.println("IO elements are not currently supported"); - continue; - } - - sketch.addSketchObject(comment); + sketch.addComment(comment); continue; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java index 252260a4..7a814889 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java @@ -4,6 +4,10 @@ import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.simantics.sysdyn.modelImport.model.IExpression; +import org.simantics.sysdyn.modelImport.model.IntegralExpression; +import org.simantics.sysdyn.modelImport.model.NormalExpression; +import org.simantics.sysdyn.modelImport.model.Range; import org.simantics.sysdyn.modelImport.model.SketchComment; import org.simantics.sysdyn.modelImport.model.SketchConnection; import org.simantics.sysdyn.modelImport.model.SketchElement; @@ -11,7 +15,6 @@ import org.simantics.sysdyn.modelImport.model.SketchValve; import org.simantics.sysdyn.modelImport.model.SketchVariable; import org.simantics.sysdyn.modelImport.model.Variable2; import org.simantics.sysdyn.modelImport.model.Valve2.TextPosition; -import org.simantics.utils.datastructures.Pair; public class MdlUtils { @@ -84,21 +87,55 @@ public class MdlUtils { } String name = matcher.group(variableName); - String equation = matcher.group(variableEquation); + IExpression expression = parseEquation(matcher.group(variableEquation)); + String unit = matcher.group(variableUnit); + Range range = parseRange(unit); + if (range != null) { + unit = unit.substring(0, unit.indexOf(range.originalString())).trim(); + } String description = matcher.group(variableDesc); - return new Variable2(name, equation, unit, description); + return new Variable2(name, expression, unit, range, description); } - public static Pair getPossibleIntegral(String equation) { - + private static IExpression parseEquation(String equation) { Matcher matcher = Pattern.compile("INTEG\\s*\\((.*),(.*)\\)").matcher(equation); if (matcher.matches()) { - return new Pair(matcher.group(1), matcher.group(2)); + return new IntegralExpression(matcher.group(1), matcher.group(2)); + } + else { + return new NormalExpression(""); + } + } + + private static Range parseRange(String unit) { + Matcher matcher = Pattern.compile("\\[(-?\\d+|\\?),(-?\\d+|\\?)\\]").matcher(unit); + if (matcher.find()) { + String start = matcher.group(1); + String end = matcher.group(2); + // TODO: and step is? + return new Range( + start.equals("?") ? null : Double.parseDouble(start), + end.equals("?") ? null : Double.parseDouble(end), + null, + matcher.group()); + } + else { + return null; + } + } + + private static final String categoryPattern = "\\*{56}\\s+\\.(\\S+)\\s+\\*{56}.*"; + + public static String getPossibleCategory(String line) { + Matcher matcher = Pattern.compile(categoryPattern).matcher(line); + + if (!matcher.matches()) { + return null; } - return null; + return matcher.group(1); } // sketch object data is defined similarly @@ -165,11 +202,8 @@ public class MdlUtils { int id = Integer.parseInt(matcher.group(elementId)); Variable2 var = variables.get(matcher.group(elementName)); - boolean attached = elementIsAttached(matcher); - boolean in = elementAllowsInBound(matcher); - boolean out = elementAllowsOutBound(matcher); - SketchVariable variable = new SketchVariable(id, var, attached, in, out); + SketchVariable variable = new SketchVariable(id, var); initializeElement(variable, matcher); @@ -201,10 +235,8 @@ public class MdlUtils { int id = Integer.parseInt(matcher.group(elementId)); CommentIcon icon = getCommentIcon(matcher); - boolean nextLine = elementHasCommentLine(matcher); - boolean isIO = elementIsIO(matcher); - SketchComment comment = new SketchComment(id, icon, nextLine, isIO); + SketchComment comment = new SketchComment(id, icon); initializeElement(comment, matcher); @@ -216,16 +248,22 @@ public class MdlUtils { int y = Integer.parseInt(matcher.group(elementY)); int w = Integer.parseInt(matcher.group(elementWidth)); int h = Integer.parseInt(matcher.group(elementHeight)); + boolean attached = elementIsAttached(matcher); + boolean in = elementAllowsInBound(matcher); + boolean out = elementAllowsOutBound(matcher); + boolean inputOutput = elementIsInputOutput(matcher); + boolean textLine = elementhasTextLine(matcher); - element.setLocationAndSize(x, y, w, h); + element.init(x, y, w, h, attached, in, out, inputOutput, textLine); } public static ConnectionType getConnectionType(Matcher matcher) { switch(Integer.parseInt(matcher.group(connectionShape))) { - case 0: return ConnectionType.ARROW; + case 0: + case 1: return ConnectionType.ARROW; case 4: return ConnectionType.LINE_ARROW; case 100: return ConnectionType.LINE_SEGMENT; - default: return ConnectionType.OTHER; + default: System.err.println("connection type "+Integer.parseInt(matcher.group(connectionShape))); return ConnectionType.OTHER; } } @@ -259,11 +297,11 @@ public class MdlUtils { return (Integer.parseInt(matcher.group(elementBits)) & 1<<1) != 0; } - public static boolean elementHasCommentLine(Matcher matcher) { + public static boolean elementhasTextLine(Matcher matcher) { return (Integer.parseInt(matcher.group(elementBits)) & 1<<2) != 0; } - public static boolean elementIsIO(Matcher matcher) { + public static boolean elementIsInputOutput(Matcher matcher) { return (Integer.parseInt(matcher.group(elementBits)) & 1<<3) != 0; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary2.java index 17047bb9..16facada 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary2.java @@ -1,45 +1,39 @@ package org.simantics.sysdyn.modelImport.model; -import java.util.Arrays; - import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; public class Auxiliary2 extends ModelVariable { - protected String equation; - public Auxiliary2(double x, double y, - String name, String unit, Range range, String description, - String equation) { - this.equation = equation; + String name, IExpression expression, Range range, String unit, String description) { + super(x, y, name, expression, range, unit, description); } - public Auxiliary2(double x, double y, Variable2 variable, String equation) { - this.equation = equation; + public Auxiliary2(double x, double y, Variable2 variable) { + super(x, y, variable); } @Override public void write(WriteGraph graph, Resource parent) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); Resource variable = createVariable(graph, sr.Auxiliary, parent); - - Resource expression = GraphUtils.create2(graph, sr.NormalExpression, - sr.Expression_equation, equation, - l0.PartOf, variable); - // TODO: why is the expressoin stored in two places? - graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); - createSymbol(graph, sr.AuxiliarySymbol, variable, parent); - + setResource(variable); } + @Override + public boolean supportsDependencies() { + return true; + } + + @Override + public boolean supportsFlows() { + return false; + } + } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java index 7653072b..35671258 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java @@ -49,7 +49,6 @@ public class Cloud extends Element { sr.Cloud); graph.claim(parent, l0.ConsistsOf, cloud); - Resource symbol = GraphUtils.create2(graph, diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud2.java index fec89917..68301a27 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud2.java @@ -1,9 +1,39 @@ package org.simantics.sysdyn.modelImport.model; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; + public class Cloud2 extends Element2 { - public Cloud2() { + public Cloud2(double x, double y) { + super(x, y); + } + + @Override + public void write(WriteGraph graph, Resource parent) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource cloud = GraphUtils.create2(graph, sr.Cloud, + l0.PartOf, parent); + + createSymbol(graph, sr.CloudSymbol, cloud, parent); + setResource(cloud); + } + + @Override + public boolean supportsDependencies() { + return false; + } + + @Override + public boolean supportsFlows() { + return true; } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment2.java index 53334c7b..fc5264de 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment2.java @@ -1,5 +1,43 @@ package org.simantics.sysdyn.modelImport.model; +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; + public class Comment2 extends Element2 { + + private String text; + + public Comment2(double x, double y, String text) { + super(x, y); + this.text = text; + } + + @Override + public void write(WriteGraph graph, Resource parent) throws DatabaseException { + DiagramResource dr = DiagramResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource comment = createSymbol(graph, sr.AdditionalSymbols_MultilineText, null, parent); + + graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING); + + setResource(comment); + } + + @Override + public boolean supportsDependencies() { + return false; + } + @Override + public boolean supportsFlows() { + return false; + } + } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection2.java index 7d0485ce..455f0be6 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection2.java @@ -2,28 +2,66 @@ package org.simantics.sysdyn.modelImport.model; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; public abstract class Connection2 implements IWriteableObject { - + protected Element2 head; protected Element2 tail; - @Override - public void write(WriteGraph graph, Resource parent) throws DatabaseException { - System.err.println("Writing connection"); - } - - protected void writeConnection() { - - } - - public void setHead(Element2 head) { + public Connection2(Element2 head, Element2 tail) { this.head = head; - } - - public void setTail(Element2 tail) { this.tail = tail; } - + + protected Resource createConnection(WriteGraph graph, Resource type, Resource symbol, Resource parent) + throws DatabaseException { + // TODO: remove this + if (head == null || tail == null) { + return null; + } + + // create connection + Layer0 l0 = Layer0.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource connection = GraphUtils.create2(graph, type, + sr.Variable_HasHead, head.getResource(), + sr.Variable_HasTail, tail.getResource(), + l0.PartOf, parent); + graph.claim(connection, mr.Mapped, connection); + + // create symbol + DiagramResource dr = DiagramResource.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + + Resource diagramTail = graph.getPossibleObject(tail.getResource(), mr.ComponentToElement); + Resource tailConnector = GraphUtils.create2(graph, dr.Connector, + sr.HasTailTerminal, diagramTail); + + Resource diagramHead = graph.getPossibleObject(head.getResource(), mr.ComponentToElement); + Resource headConnector = GraphUtils.create2(graph, dr.Connector, + sr.HasHeadTerminal, diagramHead, + dr.AreConnected, tailConnector); + + Resource diagramConnection = GraphUtils.create2(graph, symbol, + sr2.HasConnectionType, sr.SysdynConnectionType, + mr.DiagramConnectionToConnection, connection, + dr.HasArrowConnector, headConnector, + dr.HasPlainConnector, tailConnector); + + Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram); + OrderedSetUtils.add(graph, diagram, diagramConnection); + + return connection; + } + } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency2.java index d390e7e9..d4fa8e40 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency2.java @@ -1,25 +1,27 @@ package org.simantics.sysdyn.modelImport.model; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; + public class Dependency2 extends Connection2 { private boolean showArrow; private boolean showDelay; - public Dependency2(boolean showArrow, boolean showDelay) { + public Dependency2(Element2 head, Element2 tail, boolean showArrow, boolean showDelay) { + super(head, tail); this.showArrow = showArrow; this.showDelay = showDelay; } @Override - public void setHead(Element2 head) { - // TODO: make sure head is of the right type - super.setHead(head); - } - - @Override - public void setTail(Element2 Tail) { - // TODO: make sure tail is of the right type - super.setTail(tail); + public void write(WriteGraph graph, Resource parent) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource dependency = createConnection(graph, sr.Dependency, sr.DependencyConnection, parent); } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element2.java index 22a91f0f..43914ccc 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element2.java @@ -10,33 +10,20 @@ import org.simantics.diagram.stubs.G2DResource; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; -public class Element2 implements IWriteableObject { +public abstract class Element2 implements IWriteableObject { + + private static final double SCALE = 1.0 / 3.0; - private int x; - private int y; + private double x; + private double y; private Resource resource; - public Element2() { - + public Element2(double x, double y) { + this.x = x * SCALE; + this.y = y * SCALE; } - // public Element2(int x, int y, int width, int height) { - // this.x = x; - // this.y = y; - // this.width = width; - // this.height = height; - // - // this.resource = null; - // } - - @Override - public void write(WriteGraph graph, Resource parent) throws DatabaseException { - System.err.println("Writing element"); - } - - // TODO: is this the right place for this? - public Resource getResource() { return resource; } @@ -50,8 +37,11 @@ public class Element2 implements IWriteableObject { G2DResource g2d = G2DResource.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); - Resource symbol = GraphUtils.create2(graph, type, - mr.ElementToComponent, variable); + Resource symbol = GraphUtils.create2(graph, type); + + if (variable != null) { + graph.claim(symbol, mr.ElementToComponent, variable); + } double[] transform = { 1.0, 0.0, 0.0, 1.0, x, y }; graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform, Bindings.DOUBLE_ARRAY); @@ -61,5 +51,8 @@ public class Element2 implements IWriteableObject { return symbol; } + + public abstract boolean supportsDependencies(); + public abstract boolean supportsFlows(); } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow2.java index db78ba79..d631439f 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow2.java @@ -1,21 +1,21 @@ package org.simantics.sysdyn.modelImport.model; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; + public class Flow2 extends Connection2 { - public Flow2() { - - } - - @Override - public void setHead(Element2 head) { - // TODO: make sure head is of the right type - super.setHead(head); + public Flow2(Element2 head, Element2 tail) { + super(head, tail); } @Override - public void setTail(Element2 tail) { - // TODO: make sure tail is of the right type - super.setTail(tail); + public void write(WriteGraph graph, Resource parent) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource flow = createConnection(graph, sr.Flow, sr.FlowConnection, parent); } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IExpression.java new file mode 100644 index 00000000..d4ab16f0 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IExpression.java @@ -0,0 +1,5 @@ +package org.simantics.sysdyn.modelImport.model; + +public interface IExpression extends IWriteableObject { + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java new file mode 100644 index 00000000..40d096b4 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IntegralExpression.java @@ -0,0 +1,36 @@ +package org.simantics.sysdyn.modelImport.model; + +import java.util.Arrays; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; + +public class IntegralExpression implements IExpression { + + private String integral; + private String initial; + + public IntegralExpression(String integral, String initial) { + this.integral = integral; + this.initial = initial; + } + + @Override + public void write(WriteGraph graph, Resource parent) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource expression = GraphUtils.create2(graph, sr.StockExpression, + sr.StockExpression_integralEquation, integral, + sr.StockExpression_initialEquation, initial, + l0.PartOf, parent); + // TODO: why is the expression stored in two places? + graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); + } + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java new file mode 100644 index 00000000..c1dbceb4 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/MdlModel.java @@ -0,0 +1,5 @@ +package org.simantics.sysdyn.modelImport.model; + +public class MdlModel { + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model2.java index 447efad4..c6bce0cb 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model2.java @@ -29,12 +29,14 @@ public class Model2 implements IWriteableObject { // the structure of the model private ArrayList elements; + private ArrayList shadows; private ArrayList connections; public Model2(String name) { this.name = name; elements = new ArrayList(); + shadows = new ArrayList(); connections = new ArrayList(); } @@ -66,6 +68,10 @@ public class Model2 implements IWriteableObject { elements.add(element); } + public void addShadow(Shadow2 shadow) { + shadows.add(shadow); + } + public void addConnection(Connection2 connection) { connections.add(connection); } @@ -78,7 +84,7 @@ public class Model2 implements IWriteableObject { Resource model = ModelUtils.createModel(graph); graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING); - // TODO: set simulatin parameters + // TODO: set simulation parameters Resource configuration = graph.getSingleObject(model, sim.HasConfiguration); @@ -86,6 +92,10 @@ public class Model2 implements IWriteableObject { e.write(graph, configuration); } + for (Shadow2 s : shadows) { + s.write(graph, configuration); + } + for (Connection2 c : connections) { c.write(graph, configuration); } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java index 1a1df905..81b66a14 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java @@ -9,18 +9,34 @@ import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; -public class ModelVariable extends Element2 { +public abstract class ModelVariable extends Element2 { protected String name; + protected IExpression expression; protected Range range; protected String unit; protected String description; - public ModelVariable() { - + public ModelVariable(double x, double y, String name, IExpression expression, Range range, String unit, String description) { + super(x, y); + this.name = name; + this.expression = expression; + this.range = range; + this.unit = unit; + this.description = description; + } + + public ModelVariable(double x, double y, Variable2 variable) { + super(x, y); + this.name = variable.getName(); + this.expression = variable.getExpression(); + this.range = variable.getRange(); + this.unit = variable.getUnit(); + this.description = variable.getDescription(); } - public Resource createVariable(WriteGraph graph, Resource type, Resource parent) throws DatabaseException { + public Resource createVariable(WriteGraph graph, Resource type, Resource parent) + throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); @@ -30,17 +46,17 @@ public class ModelVariable extends Element2 { l0.PartOf, parent); graph.claim(variable, mr.Mapped, variable); + expression.write(graph, variable); + + if (range != null) { + range.write(graph, variable); + } if (unit != null && !unit.isEmpty()) { graph.claimLiteral(variable, sr.Variable_unit, unit, Bindings.STRING); } if (description != null && !description.isEmpty()) { graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING); } - if (range != null) { - //graph.claimLiteral(variable, sr.HasRangeStart, rangeStart); - //graph.claimLiteral(variable, sr.HasRangeEnd, rangeEnd); - //graph.claimLiteral(variable, sr.HasRangeStep, rangeStep); - } return variable; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java new file mode 100644 index 00000000..e7b0fb52 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/NormalExpression.java @@ -0,0 +1,32 @@ +package org.simantics.sysdyn.modelImport.model; + +import java.util.Arrays; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; + +public class NormalExpression implements IExpression { + + private String equation; + + public NormalExpression(String equation) { + this.equation = equation; + } + + @Override + public void write(WriteGraph graph, Resource variable) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource expression = GraphUtils.create2(graph, sr.NormalExpression, + sr.Expression_equation, equation, + l0.PartOf, variable); + // TODO: why is the expression stored in two places? + graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); + } +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Range.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Range.java index 30bec9aa..638ef7ba 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Range.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Range.java @@ -1,15 +1,42 @@ package org.simantics.sysdyn.modelImport.model; -public class Range { +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; + +public class Range implements IWriteableObject { - private double start; - private double end; - private double step; + private Double start; + private Double end; + private Double step; + private String original; - public Range(double start, double end, double step) { + public Range(Double start, Double end, Double step, String original) { this.start = start; this.end = end; this.step = step; + this.original = original; + } + + public Double getStart() { + return start; + } + + public Double getEnd() { + return end; + } + + public Double getStep() { + return step; + } + + public String originalString() { + return original; + } + + @Override + public void write(WriteGraph graph, Resource variable) throws DatabaseException { + // TODO: implementation } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java new file mode 100644 index 00000000..4b51e424 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow2.java @@ -0,0 +1,44 @@ +package org.simantics.sysdyn.modelImport.model; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; + +public class Shadow2 extends Element2 { + + private Element2 original; + + public Shadow2(double x, double y, Element2 original) { + super(x, y); + this.original = original; + } + + @Override + public void write(WriteGraph graph, Resource parent) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource shadow = GraphUtils.create2(graph, sr.Shadow, + l0.PartOf, parent); + + graph.claim(shadow, sr.Shadow_original, original.getResource()); + + createSymbol(graph, sr.ShadowSymbol, shadow, parent); + + setResource(shadow); + } + + @Override + public boolean supportsDependencies() { + return original.supportsDependencies(); + } + + @Override + public boolean supportsFlows() { + return original.supportsFlows(); + } + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Sketch2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Sketch2.java index 04b88d7e..5512c8d6 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Sketch2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Sketch2.java @@ -2,19 +2,22 @@ package org.simantics.sysdyn.modelImport.model; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; public class Sketch2 { private String name; - private HashMap objectMap; - private ArrayList unfinished; + private ArrayList comments; + private ArrayList connections; + private ArrayList valves; + private ArrayList variables; public Sketch2() { - objectMap = new HashMap(); - unfinished = new ArrayList(); + comments = new ArrayList(); + connections = new ArrayList(); + valves = new ArrayList(); + variables = new ArrayList(); } public String getName() { @@ -25,73 +28,49 @@ public class Sketch2 { this.name = name; } - public SketchObject getSketchObject(int index) { - return objectMap.get(index); - } - - public void addSketchObject(SketchConnection object) { - unfinished.add(object); - - addSketchObject((SketchObject)object); + public void addComment(SketchComment comment) { + comments.add(comment); } - public void addSketchObject(SketchObject object) { - if (objectMap.get(object.getId()) != null) { - System.err.println("duplicate object with id "+object.getId()); - return; - } - objectMap.put(object.getId(), object); - - // NOTE: could just do another pass to update connections at the end - // or just use a priority queue or something but the number of - // unfinished connections at any given time should be relatively - // small in practice so it does not matter that this is inefficient - Iterator i = unfinished.iterator(); - while (i.hasNext()) { - SketchConnection connection = i.next(); - - SketchObject from = objectMap.get(connection.getFromId()); - SketchObject to = objectMap.get(connection.getToId()); - - if (from != null && to != null) { - // connections can only connect elements so if this is not the - // case something has gone wrong with the indexing - assert from instanceof SketchElement; - assert to instanceof SketchElement; - - connection.setFrom((SketchElement)from); - connection.setTo((SketchElement)to); - - i.remove(); - } - } + public List getComments() { + return comments; } - - // TODO: ugly and inefficient - public List getElements() { - ArrayList list = new ArrayList(); - for (SketchObject o : objectMap.values()) { - if (o instanceof SketchElement) { - list.add((SketchElement)o); - } - } - return list; + public void addConnection(SketchConnection connection) { + connections.add(connection); } public List getConnections() { - ArrayList list = new ArrayList(); - for (SketchObject o : objectMap.values()) { - if (o instanceof SketchConnection) { - list.add((SketchConnection)o); - } - } + return connections; + } + + public void addValve(SketchValve valve) { + valves.add(valve); + } + + public List getValves() { + return valves; + } + + public void addVariable(SketchVariable variable) { + variables.add(variable); + } + + public List getVariables() { + return variables; + } + + public List getAllElements() { + ArrayList list = new ArrayList(); + list.addAll(getComments()); + list.addAll(getValves()); + list.addAll(getVariables()); return list; } public int getWidth() { int width = 0; - for (SketchElement e : getElements()) { + for (SketchElement e : getAllElements()) { width = Math.max(width, e.getX()+e.getWidth()); } return width; @@ -99,10 +78,18 @@ public class Sketch2 { public int getHeight() { int height = 0; - for (SketchElement e : getElements()) { + for (SketchElement e : getAllElements()) { height = Math.max(height, e.getY()+e.getHeight()); } return height; } + + // relevant for sysdyn model creation, this is not the cleanest place to + // store this information but it works + + public double hOffset = 0; + public double vOffset = 0; + + public HashMap elements = new HashMap(); } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchComment.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchComment.java index c30e51b2..f077379f 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchComment.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchComment.java @@ -6,14 +6,10 @@ public class SketchComment extends SketchElement { private String text; private CommentIcon icon; - private boolean nextLine; - private boolean isIO; - public SketchComment(int id, CommentIcon icon, boolean nextLine, boolean isIO) { + public SketchComment(int id, CommentIcon icon) { super(id); this.icon = icon; - this.nextLine = nextLine; - this.isIO = isIO; } public String getText() { @@ -25,28 +21,16 @@ public class SketchComment extends SketchElement { } public CommentIcon getIcon() { - return this.icon; + return icon; } - public boolean isCloud() { - return this.icon.equals(CommentIcon.CLOUD); - } - - public boolean hasTextNextLine() { - return nextLine; - } - - public boolean isIOElement() { - return this.isIO; - } - - public Comment2 getWriteableElement(int xOffset, int yOffset) { - return null; - } - - @Override - public String toString() { - return text != null ? "\""+text+"\"" : "(empty comment)"; + public Element2 getModelElement(double xOffset, double yOffset) { + if (icon.equals(CommentIcon.CLOUD)) { + return new Cloud2(getX(xOffset), getY(yOffset)); + } + else { + return new Comment2(getX(xOffset), getY(yOffset), text); + } } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchConnection.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchConnection.java index acb4bd37..6207d692 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchConnection.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchConnection.java @@ -4,61 +4,41 @@ import org.simantics.sysdyn.modelImport.MdlUtils.ConnectionType; public class SketchConnection extends SketchObject { - private int fromId; - private SketchElement from; - private int toId; - private SketchElement to; + private int from; + private int to; private ConnectionType type; - public SketchConnection(int id, int fromId, int toId, ConnectionType type) { + public SketchConnection(int id, int from, int to, ConnectionType type) { super(id); - this.fromId = fromId; - this.toId = toId; + this.from = from; + this.to = to; this.type = type; } - public int getFromId() { - return fromId; - } - - public int getToId() { - return toId; - } - - public SketchElement getFrom() { - if (from == null) { - System.err.println("endpoint "+fromId+" for connection "+getId()+" is unset"); - } + public int getFrom() { return from; } - - public void setFrom(SketchElement from) { - this.from = from; - } - - public SketchElement getTo() { - if (to == null) { - System.err.println("endpoint "+toId+" for connection "+getId()+" is unset"); - } + + public int getTo() { return to; } - - public void setTo(SketchElement to) { - this.to = to; - } public ConnectionType getType() { return type; } - public Connection2 getWriteableConnection() { - // return dependency or flow + public Connection2 getWriteableConnection(Element2 head, Element2 tail) { + if (type.equals(ConnectionType.ARROW)) { + return new Dependency2(head, tail, false, false); + } + else if (type.equals(ConnectionType.LINE_ARROW)) { + return new Flow2(head, tail); + } + else if (type.equals(ConnectionType.LINE_SEGMENT)) { + // TODO: what is this I don't even... + return new Flow2(tail, head); + } return null; } - @Override - public String toString() { - return from.toString() + " --> " + to.toString(); - } - } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchElement.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchElement.java index 157b7d98..ef54a017 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchElement.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchElement.java @@ -6,25 +6,45 @@ public abstract class SketchElement extends SketchObject { private int y; private int width; private int height; + private boolean attached; + private boolean allowsIn; + private boolean allowsOut; + private boolean inputOutput; + private boolean textLine; public SketchElement(int id) { super(id); } - public void setLocationAndSize(int x, int y, int width, int height) { + public void init(int x, int y, int width, int height, + boolean attached, boolean allowsIn, boolean allowsOut, + boolean inputOutput, boolean textLine) { this.x = x; this.y = y; this.width = width; this.height = height; + this.attached = attached; + this.allowsIn = allowsIn; + this.allowsOut = allowsOut; + this.inputOutput = inputOutput; + this.textLine = textLine; } public int getX() { return x; } + + public double getX(double offset) { + return x + offset; + } public int getY() { return y; } + + public double getY(double offset) { + return y + offset; + } public int getWidth() { return width; @@ -34,6 +54,26 @@ public abstract class SketchElement extends SketchObject { return height; } - public abstract Element2 getWriteableElement(int xOffset, int yOffset); + public boolean isAttached() { + return attached; + } + + public boolean allowsIn() { + return allowsIn; + } + + public boolean allowsOut() { + return allowsOut; + } + + public boolean isInputOutput() { + return inputOutput; + } + + public boolean hasTextLine() { + return textLine; + } + + public abstract Element2 getModelElement(double xOffset, double yOffset); } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchObject.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchObject.java index b28c6f0c..89b1933c 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchObject.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchObject.java @@ -1,9 +1,7 @@ package org.simantics.sysdyn.modelImport.model; public abstract class SketchObject { - - // TODO: some type stuff here maybe? - + private int id; SketchObject(int id) { diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchValve.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchValve.java index 0a774d3d..6b8161f4 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchValve.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchValve.java @@ -17,13 +17,8 @@ public class SketchValve extends SketchElement { } @Override - public Valve2 getWriteableElement(int xOffset, int yOffset) { - return null; - } - - @Override - public String toString() { - return "X("+variable.toString()+")"; + public Element2 getModelElement(double xOffset, double yOffset) { + return new Valve2(getX(xOffset), getY(yOffset), variable.getVariable()); } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchVariable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchVariable.java index fad806ac..1d86295f 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchVariable.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/SketchVariable.java @@ -1,53 +1,26 @@ package org.simantics.sysdyn.modelImport.model; -import org.simantics.sysdyn.modelImport.MdlUtils; -import org.simantics.utils.datastructures.Pair; - public class SketchVariable extends SketchElement { private Variable2 variable; - boolean isAttached; - boolean allowsInBound; - boolean allowsOutBound; - public SketchVariable(int id, Variable2 variable, boolean isAttached, boolean in, boolean out) { + public SketchVariable(int id, Variable2 variable) { super(id); this.variable = variable; - this.isAttached = isAttached; - this.allowsInBound = in; - this.allowsOutBound = out; } public Variable2 getVariable() { - return this.variable; - } - - public boolean isAttached() { - return isAttached; - } - - public boolean allowsInBound() { - return allowsInBound; - } - - public boolean allowsOutBound() { - return allowsOutBound; + return variable; } @Override - public Element2 getWriteableElement(int xOffset, int yOffset) { - Pair integral = MdlUtils.getPossibleIntegral(variable.getEquation()); - if (integral != null) { - return new Stock2(getX(), getY(), variable, integral); + public Element2 getModelElement(double xOffset, double yOffset) { + if (variable.getExpression() instanceof IntegralExpression) { + return new Stock2(getX(xOffset), getY(yOffset), variable); } else { - return new Auxiliary2(getX(), getY(), variable, variable.getEquation()); + return new Auxiliary2(getX(xOffset), getY(yOffset), variable); } } - @Override - public String toString() { - return (allowsInBound?"":"<")+variable.getName()+(allowsInBound?"":">"); - } - } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock2.java index ae37c2c3..41d424c4 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock2.java @@ -1,49 +1,38 @@ package org.simantics.sysdyn.modelImport.model; -import java.util.Arrays; - import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; -import org.simantics.utils.datastructures.Pair; public class Stock2 extends ModelVariable { - protected String integral; - protected String initial; - public Stock2(double x, double y, - String name, String unit, Range range, String description, - String integral, String initial) { - this.integral = integral; - this.initial = initial; + String name, IntegralExpression expression, Range range, String unit, String description) { + super(x, y, name, expression, range, unit, description); } - public Stock2(double x, double y, Variable2 variable, Pair equation) { - this.integral = equation.first; - this.initial = equation.second; + public Stock2(double x, double y, Variable2 variable) { + super(x, y, variable); } public void write(WriteGraph graph, Resource parent) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); Resource stock = createVariable(graph, sr.Stock, parent); - - Resource expression = GraphUtils.create2(graph, sr.StockExpression, - sr.StockExpression_integralEquation, integral, - sr.StockExpression_initialEquation, initial, - l0.PartOf, stock); - // TODO: why is the expressoin stored in two places? - graph.claim(stock, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); - createSymbol(graph, sr.StockSymbol, stock, parent); setResource(stock); } + + @Override + public boolean supportsDependencies() { + return true; + } + + @Override + public boolean supportsFlows() { + return true; + } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve2.java index d222372b..5ea5a99b 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve2.java @@ -1,6 +1,11 @@ package org.simantics.sysdyn.modelImport.model; -public class Valve2 extends Element2 { +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; + +public class Valve2 extends ModelVariable { public enum Orientation { HORIZONTAL, VERTICAL @@ -10,8 +15,33 @@ public class Valve2 extends Element2 { INSIDE, BELOW, LEFT, ABOVE, RIGHT, UNSET } - public Valve2(String equation, Orientation orientation, TextPosition position, String unit, Range range, String description) { - + public Valve2(double x, double y, + String name, IExpression expression, Range range, String unit, String description) { + super(x, y, name, expression, range, unit, description); + } + + public Valve2(double x, double y, Variable2 variable) { + super(x, y, variable); + } + + @Override + public void write(WriteGraph graph, Resource parent) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource valve = createVariable(graph, sr.Valve, parent); + createSymbol(graph, sr.ValveSymbol, valve, parent); + + setResource(valve); + } + + @Override + public boolean supportsDependencies() { + return true; + } + + @Override + public boolean supportsFlows() { + return true; } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable2.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable2.java index 8c646cb4..f1d365c6 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable2.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable2.java @@ -3,14 +3,16 @@ package org.simantics.sysdyn.modelImport.model; public class Variable2 { private String name; - private String equation; + private IExpression expression; private String unit; + private Range range; private String description; - public Variable2(String name, String equation, String unit, String description) { + public Variable2(String name, IExpression expression, String unit, Range range, String description) { this.name = name; - this.equation = equation; + this.expression = expression; this.unit = unit; + this.range = range; this.description = description; } @@ -18,31 +20,20 @@ public class Variable2 { return name; } - public void setName(String name) { - this.name = name; + public IExpression getExpression() { + return expression; } - public String getEquation() { - return equation; - } - - public void setEquation(String equation) { - this.equation = equation; + public Range getRange() { + return range; } public String getUnit() { return unit; } - public void setUnit(String unit) { - this.unit = unit; - } - public String getDescription() { return description; } - - public void setDescription(String description) { - this.description = description; - } + } -- 2.47.1