From 4a693ca5f30c7f47662eafc8e45b19e66f195d63 Mon Sep 17 00:00:00 2001 From: jkauttio Date: Wed, 9 Apr 2014 12:38:31 +0000 Subject: [PATCH] Update vensim import with more subscript support refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/dev-jkauttio@29264 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/modelImport/MdlParser.java | 172 +++++---------- .../simantics/sysdyn/modelImport/MdlUtil.java | 197 ++++++++---------- .../sysdyn/modelImport/mdl/MdlModel.java | 15 +- .../sysdyn/modelImport/mdl/Sketch.java | 46 +++- .../sysdyn/modelImport/mdl/SketchComment.java | 25 +-- .../modelImport/mdl/SketchConnection.java | 52 +++-- .../sysdyn/modelImport/mdl/SketchElement.java | 21 +- .../sysdyn/modelImport/mdl/SketchObject.java | 13 +- .../sysdyn/modelImport/mdl/SketchValve.java | 22 +- .../modelImport/mdl/SketchVariable.java | 18 +- .../sysdyn/modelImport/mdl/Subscript.java | 31 --- .../sysdyn/modelImport/model/Model.java | 51 ++--- .../modelImport/model/WriteContext.java | 60 +++++- .../modelImport/model/element/Auxiliary.java | 4 + .../modelImport/model/element/Cloud.java | 4 + .../modelImport/model/element/Comment.java | 5 + .../modelImport/model/element/Connection.java | 4 +- .../modelImport/model/element/Dependency.java | 4 +- .../modelImport/model/element/Flow.java | 4 +- .../model/element/ModelVariable.java | 11 +- .../modelImport/model/element/Shadow.java | 5 + .../modelImport/model/element/Stock.java | 4 + .../modelImport/model/element/Symbol.java | 7 + .../modelImport/model/element/Valve.java | 5 + .../expression/EnumerationExpression.java | 98 ++++----- .../model/expression/Expression.java | 8 +- .../model/expression/IntegralExpression.java | 1 + .../model/support/Enumeration.java | 53 ++++- 28 files changed, 511 insertions(+), 429 deletions(-) delete mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java index 82e8e07e..62e8a668 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java @@ -28,7 +28,6 @@ import org.simantics.sysdyn.modelImport.mdl.SketchElement; import org.simantics.sysdyn.modelImport.mdl.SketchObject; import org.simantics.sysdyn.modelImport.mdl.SketchValve; import org.simantics.sysdyn.modelImport.mdl.SketchVariable; -import org.simantics.sysdyn.modelImport.mdl.Subscript; import org.simantics.sysdyn.modelImport.model.Model; import org.simantics.sysdyn.modelImport.model.element.Auxiliary; import org.simantics.sysdyn.modelImport.model.element.Cloud; @@ -41,30 +40,18 @@ import org.simantics.sysdyn.modelImport.model.element.Stock; import org.simantics.sysdyn.modelImport.model.element.Valve; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; import org.simantics.sysdyn.modelImport.model.expression.NormalExpression; +import org.simantics.sysdyn.modelImport.model.support.Enumeration; import org.simantics.sysdyn.modelImport.model.support.Variable; public class MdlParser { - private static final String UTF_8 = "{UTF-8}"; - 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 = 0; - private static final double V_SPACE = 10; - public static Model parse(File file) { // generate a mdl model based on the contents of the file MdlModel mdl; try { mdl = parseFile(file); } - catch (IOException e) { + catch (Exception e) { e.printStackTrace(); return null; } @@ -73,106 +60,65 @@ public class MdlParser { Model model = new Model(mdl.getName()); double offset = 0; - - // do this in several passes - - HashMap variableToElement = new HashMap(); - + + // add anumerations + for (Enumeration enumeration : mdl.getSubscripts()) { + model.addEnumeration(enumeration); + } + // add sketch labels and independent elements for (Sketch sketch : mdl.getSketches()) { - sketch.setEdges(); - - sketch.hOffset = 0; - sketch.vOffset = 0 - sketch.topEdge + 10 + offset; - - model.addElement(new Comment(0, offset, -1, -1, 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; - } - - Symbol modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset); - model.addElement(modelElement); - sketch.elements.put(comment.getId(), modelElement); - } - - for (SketchValve valve : sketch.getValves()) { - Symbol modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset); - model.addElement(modelElement); - sketch.elements.put(valve.getId(), modelElement); - sketch.elements.put(valve.getAttachedVariable().getId(), modelElement); - variableToElement.put(valve.getAttachedVariable().getVariable(), modelElement); + + sketch.setOffset(0, offset); + + model.addSymbol(new Comment(0, offset, -1, -1, sketch.getName())); + + for (SketchElement element : sketch.getIndependentElements()) { + Symbol symbol = element.getSymbol(sketch); + element.setModelObject(symbol); + model.addSymbol(symbol); } - - 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; - } - - Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset); - model.addElement(modelElement); - sketch.elements.put(variable.getId(), modelElement); - variableToElement.put(variable.getVariable(), modelElement); - } - - offset += (sketch.bottomEdge - sketch.topEdge + V_SPACE); + + offset += 200; } // add dependent elements for (Sketch sketch : mdl.getSketches()) { - for (SketchVariable variable : sketch.getVariables()) { - if (!variable.allowsIn()) { - // the variable is a shadow variable - Symbol original = variableToElement.get(variable.getVariable()); - if (original == null) { - System.err.println("original not found for "+variable.getVariable().getName()); - Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset); - model.addElement(modelElement); - sketch.elements.put(variable.getId(), modelElement); - variableToElement.put(variable.getVariable(), modelElement); - } - else { - Shadow modelElement = new Shadow( - variable.getSysdyndX() + sketch.hOffset, - variable.getSysdyndY() + sketch.vOffset, - variable.getSysdynWidth(), - variable.getSysdynHeight(), - (ModelVariable)variableToElement.get(variable.getVariable())); - model.addShadow(modelElement); - sketch.elements.put(variable.getId(), modelElement); - } + for (SketchVariable variable : sketch.getShadowVariables()) { + if (variable.getVariable() == null) { + System.err.println("null variable"); + continue; } + ModelVariable original = model.getVariable(variable.getVariable().getName()); + System.err.println("original variable "+original); + Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(sketch); + variable.setModelObject(symbol); + model.addSymbol(symbol); } for (SketchConnection connection : sketch.getConnections()) { - Symbol head = sketch.elements.get(connection.getTo()); - Symbol tail = sketch.elements.get(connection.getFrom()); - Connection c = connection.getWriteableConnection(head, tail, sketch.vOffset); - if (c != null) { - model.addConnection(c); + Connection conn = connection.getConnection(sketch); + if (conn != null) { + connection.setModelObject(conn); + model.addConnection(conn); } } } + + // Set simulation parameters return model; } + + private static final String UTF_8 = "{UTF-8}"; + private static final String SKETCH_VERSION = "V300"; + private static final String SKETCH_START = "\\\\\\---///"; + private static final String SKETCH_END = "///---\\\\\\"; private static MdlModel parseFile(File file) - throws IOException { + throws Exception { MdlModel mdl = new MdlModel(file.getName()); - // TODO: do this somewhere else and more controlled - mdl.addVariable(new Variable("Time", new NormalExpression("time"), "", null, ""), null); - // peek at the first line to see if we need to use UTF-8 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = reader.readLine(); @@ -212,25 +158,23 @@ public class MdlParser { } while ((line = reader.readLine()) != null); String str = buffer.toString(); + ArrayList variables = new ArrayList(); + String cat; Variable var; - Subscript sub; + Enumeration sub; // parse the (possible) variable declaration if ((cat = MdlUtil.getPossibleCategory(str)) != null) { category = cat; } - else if ((var = MdlUtil.getPossibleVariable(str, category)) != null) { + else if ((var = MdlUtil.getPossibleNormalVariable(str)) != null) { mdl.addVariable(var, category); } - else if ((var = MdlUtil.getPossibleSubscriptVariable(str, category, mdl)) != null) { - Variable orig = mdl.getVariable(var.getName()); - if (orig != null) - orig.mergeWithVariable(var); - else - mdl.addVariable(var, category); + else if ((var = MdlUtil.getPossibleSubscriptVariable(str)) != null) { + mdl.addVariable(var, category); } - else if ((var = MdlUtil.getPossibleLookUpVariable(str, category)) != null) { + else if ((var = MdlUtil.getPossibleLookUpVariable(str)) != null) { mdl.addVariable(var, category); } else if ((var = MdlUtil.getPossibleNoExpressionVariable(str)) != null) { @@ -241,17 +185,17 @@ public class MdlParser { } else { // if we got this far, the variable could not be parsed - System.err.println("unrecognized variable "+str); + throw new Exception("unrecognized variable "+str); } } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START)); + + // add all variables to the model // END READING VARIABLE DATA if (line == null) { - System.err.println("unexpected end of file"); - reader.close(); - return null; + throw new Exception("unexpected end of file"); } // START READING SKETCH DATA @@ -283,19 +227,17 @@ public class MdlParser { sketch.addConnection((SketchConnection)so); } else if ((so = MdlUtil.getPossibleSketchVariable(line, mdl)) != null) { - SketchVariable variable = (SketchVariable)so; - sketch.addVariable(variable); + sketch.addVariable((SketchVariable)so); } else if ((so = MdlUtil.getPossibleSketchValve(line)) != null) { SketchValve valve = (SketchValve)so; // the next row after a valve should always the variable associated with the valve SketchVariable attached = MdlUtil.getPossibleSketchVariable(reader.readLine(), mdl); if (attached == null || !attached.isAttached()) { - System.err.println("attached variable not found for valve"); + throw new Exception("attached variable not found for valve"); } valve.setAttachedVariable(attached); - sketch.addValve((SketchValve)valve); - sketch.addVariable(attached); + sketch.addValve(valve); } else if ((so = MdlUtil.getPossibleSketchComment(line)) != null) { SketchComment comment = (SketchComment)so; @@ -306,7 +248,7 @@ public class MdlParser { } else { // if we got this far, the element could not be parsed - System.err.println("unrecognized element "+line); + throw new Exception("unrecognized element "+line); } } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END)); @@ -314,9 +256,7 @@ public class MdlParser { // END READING SKETCH DATA if (line == null) { - System.err.println("unexpected end of file"); - reader.close(); - return null; + throw new Exception("unexpected end of file"); } // START READING OTHER DATA diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java index 51d104e3..d1787bbd 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java @@ -10,7 +10,6 @@ import org.simantics.sysdyn.modelImport.mdl.SketchConnection; import org.simantics.sysdyn.modelImport.mdl.SketchElement; import org.simantics.sysdyn.modelImport.mdl.SketchValve; import org.simantics.sysdyn.modelImport.mdl.SketchVariable; -import org.simantics.sysdyn.modelImport.mdl.Subscript; import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition; import org.simantics.sysdyn.modelImport.model.expression.DelayExpression; import org.simantics.sysdyn.modelImport.model.expression.Expression; @@ -18,6 +17,7 @@ import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; import org.simantics.sysdyn.modelImport.model.expression.LookupExpression; import org.simantics.sysdyn.modelImport.model.expression.NormalExpression; import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression; +import org.simantics.sysdyn.modelImport.model.support.Enumeration; import org.simantics.sysdyn.modelImport.model.support.Range; import org.simantics.sysdyn.modelImport.model.support.Variable; @@ -44,12 +44,32 @@ public class MdlUtil { "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\""; private static final String VAR_NAME_SIMPLE = "[A-Za-z](?![ \\w]*\\()(?: *\\w+)*"; - private static final String VAR_NAME = + private static final String VAR_NAME_NORMAL = "("+VAR_NAME_QUOTED+"|"+VAR_NAME_SIMPLE+")"; - private static final String VARIABLE_PATTERN = - VAR_NAME+"\\s*=\\s*"; - private static final String EQUATION_PATTERN = + // a subscript variable is a variable that has a different equation + // depending on the value of the subscript it references + private static final String VAR_NAME_SUBSCRIPT = + "("+VAR_NAME_SIMPLE+")\\[("+VAR_NAME_SIMPLE+"(?:,"+VAR_NAME_SIMPLE+")*)\\]"; + + // a lookup variable contains a set of points + private static final String LOOKUP_NAME = + "([A-Za-z](?: *\\w+)*)"; + private static final String LOOKUP_RANGE = + "\\[\\(("+NUMBER+"),("+NUMBER+")\\)-\\(("+NUMBER+"),("+NUMBER+")\\)\\]"; + private static final String LOOKUP_POINTS = + "(\\("+NUMBER+","+NUMBER+"\\)(?:,\\("+NUMBER+","+NUMBER+"\\))*)"; + + private static final String NORMAL_NAME_PATTERN = + VAR_NAME_NORMAL+"\\s*=\\s*"; + private static final String SUBSCRIPT_NAME_PATTERN = + VAR_NAME_SUBSCRIPT+"\\s*=\\s*"; + private static final String LOOKUP_NAME_PATTERN = + LOOKUP_NAME+"\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)\\s*~\\s*"; + public static final String NO_EXPRESSION_NAME_PATTERN = + VAR_NAME_NORMAL+"\\s*~\\s*"; + + private static final String EXPRESSION_PATTERN = "([^~]*?(?:"+VAR_NAME_QUOTED+"[^~]*?)*)\\s*~\\s*"; private static final String UNIT_PATTERN = @@ -57,52 +77,45 @@ public class MdlUtil { private static final String DESC_PATTERN = "([^\\|]*?)\\s*\\|"; - public static final String variablePattern = - VARIABLE_PATTERN+EQUATION_PATTERN+UNIT_PATTERN+DESC_PATTERN; + public static final String normalVariablePattern = + NORMAL_NAME_PATTERN +EXPRESSION_PATTERN+UNIT_PATTERN+DESC_PATTERN; + public static final String subscriptVariablePattern = + SUBSCRIPT_NAME_PATTERN+EXPRESSION_PATTERN+UNIT_PATTERN+DESC_PATTERN; + public static final String lookupVariablePattern = + LOOKUP_NAME_PATTERN +UNIT_PATTERN+DESC_PATTERN; + public static final String noExpressionVariablePattern = + NO_EXPRESSION_NAME_PATTERN +UNIT_PATTERN+DESC_PATTERN; - private static final int variableName = 1; - private static final int variableEquation = 2; - private static final int variableUnit = 3; - private static final int variableDesc = 4; + private static final int normalVariableName = 1; + private static final int normalVariableExpression = 2; - public static Variable getPossibleVariable(String line, String category) { - Matcher matcher = Pattern.compile(variablePattern).matcher(line); + private static final int subscriptVariableName = 1; + private static final int subscriptVariableIndices = 2; + private static final int subscriptVariableExpression = 3; + + private static final int lookupVariableName = 1; + private static final int lookupVariableRangeXMin = 2; + private static final int lookupVariableRangeYMin = 3; + private static final int lookupVariableRangeXMax = 4; + private static final int lookupVariableRangeYMax = 5; + private static final int lookupVariablePoints = 6; + + private static final int noExpressionVariableName = 1; + + public static Variable getPossibleNormalVariable(String line) { + Matcher matcher = Pattern.compile(normalVariablePattern).matcher(line); if (!matcher.matches()) { return null; } - String name = normalize(matcher.group(variableName)); - Expression expression = parseExpression(matcher.group(variableEquation)); + String name = normalize(matcher.group(normalVariableName)); + Expression expression = parseExpression(matcher.group(normalVariableExpression)); - 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 Variable(name, expression, unit, range, description); + return getVariable(matcher, 2, name, expression); } - // a subscript variable is a variable that has a different equation - // depending on the value of the subscript it references - private static final String VAR_NAME_SUBSCRIPT = - "("+VAR_NAME_SIMPLE+")\\[("+VAR_NAME_SIMPLE+"(?:,"+VAR_NAME_SIMPLE+")*)\\]"; - - private static final String SUBSCRIPT_VARIABLE_PATTERN = - VAR_NAME_SUBSCRIPT+"\\s*=\\s*"; - - public static final String subscriptVariablePattern = - SUBSCRIPT_VARIABLE_PATTERN+EQUATION_PATTERN+UNIT_PATTERN+DESC_PATTERN; - - private static final int subscriptVariableName = 1; - private static final int subscriptVariableIndices = 2; - private static final int subscriptVariableEquation = 3; - private static final int subscriptVariableUnit = 4; - private static final int subscriptVariableDesc = 5; - - public static Variable getPossibleSubscriptVariable(String line, String category, MdlModel mdl) { + public static Variable getPossibleSubscriptVariable(String line) { Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line); if (!matcher.matches()) { @@ -110,45 +123,14 @@ public class MdlUtil { } String name = normalize(matcher.group(subscriptVariableName)); - // TODO: find out if subscript indices can contain commas and update - // this method accordingly - String[] indices = matcher.group(subscriptVariableIndices).split(","); - Expression expression = parseExpression(matcher.group(subscriptVariableEquation)); - EnumerationExpression e = new EnumerationExpression(indices, expression); + String[] indices = normalize(matcher.group(subscriptVariableIndices)).split(","); + Expression expression = parseExpression(matcher.group(subscriptVariableExpression)); + EnumerationExpression e = new EnumerationExpression(expression, indices); - String unit = matcher.group(subscriptVariableUnit); - Range range = parseRange(unit); - if (range != null) { - unit = unit.substring(0, unit.indexOf(range.originalString())).trim(); - } - String description = matcher.group(subscriptVariableDesc); - - return new Variable(name, e, unit, range, description); + return getVariable(matcher, 3, name, e); } - private static final String LOOKUP_NAME = - "([A-Za-z](?: *\\w+)*)"; - private static final String LOOKUP_RANGE = - "\\[\\(("+NUMBER+"),("+NUMBER+")\\)-\\(("+NUMBER+"),("+NUMBER+")\\)\\]"; - private static final String LOOKUP_POINTS = - "(\\("+NUMBER+","+NUMBER+"\\)(?:,\\("+NUMBER+","+NUMBER+"\\))*)"; - - private static final String LOOKUP_PATTERN = - LOOKUP_NAME+"\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)\\s*~\\s*"; - - public static final String lookupVariablePattern = - LOOKUP_PATTERN+UNIT_PATTERN+DESC_PATTERN; - - private static final int lookupVariableName = 1; - private static final int lookupVariableRangeXMin = 2; - private static final int lookupVariableRangeYMin = 3; - private static final int lookupVariableRangeXMax = 4; - private static final int lookupVariableRangeYMax = 5; - private static final int lookupVariablePoints = 6; - private static final int lookupVariableUnit = 7; - private static final int lookupVariableDesc = 8; - - public static Variable getPossibleLookUpVariable(String line, String category) { + public static Variable getPossibleLookUpVariable(String line) { Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line); if (!matcher.matches()) { @@ -161,17 +143,9 @@ public class MdlUtil { double yMin = Double.parseDouble(matcher.group(lookupVariableRangeXMax)); double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax)); double[] points = parseLookup(matcher.group(lookupVariablePoints)); + LookupExpression expression = new LookupExpression(xMin, yMin, xMax, yMax, points); - LookupExpression e = new LookupExpression(xMin, yMin, xMax, yMax, points); - - String unit = matcher.group(lookupVariableUnit); - Range range = parseRange(unit); - if (range != null) { - unit = unit.substring(0, unit.indexOf(range.originalString())).trim(); - } - String description = matcher.group(lookupVariableDesc); - - return new Variable(name, e, unit, range, description); + return getVariable(matcher, 6, name, expression); } private static double[] parseLookup(String points) { @@ -185,16 +159,6 @@ public class MdlUtil { return result; } - public static final String NO_EXP_PATTERN = - VAR_NAME+"\\s*~\\s*"; - - public static final String noExpressionVariablePattern = - NO_EXP_PATTERN+UNIT_PATTERN+DESC_PATTERN; - - private static final int noExpVariableName = 1; - private static final int noExpVariableUnit = 2; - private static final int noExpVariableDesc = 3; - public static Variable getPossibleNoExpressionVariable(String line) { Matcher matcher = Pattern.compile(noExpressionVariablePattern).matcher(line); @@ -202,29 +166,32 @@ public class MdlUtil { return null; } - String name = normalize(matcher.group(noExpVariableName)); + String name = normalize(matcher.group(noExpressionVariableName)); - String unit = matcher.group(noExpVariableUnit); + return getVariable(matcher, 1, name, null); + } + + public static Variable getVariable(Matcher matcher, int groups, String name, Expression expression) { + String unit = matcher.group(groups + 1); Range range = parseRange(unit); if (range != null) { unit = unit.substring(0, unit.indexOf(range.originalString())).trim(); } - String description = matcher.group(noExpVariableDesc); - - return new Variable(name, null, unit, range, description); + String description = matcher.group(groups + 2); + return new Variable(name, expression, unit, range, description); } private static final String SUBSCRIPT_PATTERN = "("+VAR_NAME_SIMPLE+")\\s*(:|<->)\\s*"; public static final String subscriptPattern = - SUBSCRIPT_PATTERN+EQUATION_PATTERN+UNIT_PATTERN+DESC_PATTERN; + SUBSCRIPT_PATTERN+EXPRESSION_PATTERN+UNIT_PATTERN+DESC_PATTERN; private static final int subscriptName = 1; private static final int subscriptType = 2; - private static final int subscriptEquation = 3; + private static final int subscriptExpression = 3; - public static Subscript getPossibleSubscript(String line, MdlModel mdl) { + public static Enumeration getPossibleSubscript(String line, MdlModel mdl) { Matcher matcher = Pattern.compile(subscriptPattern).matcher(line); if (!matcher.matches()) { @@ -233,13 +200,13 @@ public class MdlUtil { String name = normalize(matcher.group(subscriptName)); boolean equivalence = matcher.group(subscriptType).equals("<->"); - String expression = matcher.group(subscriptEquation); + String expression = normalize(matcher.group(subscriptExpression)); if (equivalence) { - return new Subscript(name, mdl.getSubscript(normalize(expression))); + return new Enumeration(name, mdl.getSubscript(expression)); } else { - return new Subscript(name, expression.split(",")); + return new Enumeration(name, expression.split(",")); } } @@ -278,7 +245,7 @@ public class MdlUtil { System.err.println("malformed game expression: "+equation); return null; } - // game expressions are currently not supported + // game expressions are currently treated as normal expressions return new NormalExpression(normalize(parameters[0])); } else { @@ -405,7 +372,7 @@ public class MdlUtil { public static final String sketchVariable = // n, id, name, x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font) - "10,"+SAVE+VAR_NAME+","+COMMON; + "10,"+SAVE+VAR_NAME_NORMAL+","+COMMON; public static final String sketchValve = "11,"+SAVE+SAVE+ COMMON; public static final String sketchComment = @@ -430,7 +397,7 @@ public class MdlUtil { } int id = Integer.parseInt(matcher.group(elementId)); - Variable var = mdl.getVariable((normalize(matcher.group(elementName)))); + Variable var = mdl.getVariable(normalize(matcher.group(elementName))); if (var == null) { System.err.println("could not find variable corresponding to "+normalize(matcher.group(elementName))); } @@ -618,13 +585,17 @@ public class MdlUtil { int offset = 0; - Matcher matcher = Pattern.compile(VAR_NAME).matcher(str); + Matcher matcher = Pattern.compile(VAR_NAME_NORMAL).matcher(str); while (matcher.find()) { result.append(str.substring(offset, matcher.start())); String replacement = matcher.group(); - if (replacement.startsWith("\"")) { + // TODO: comment this + if (replacement.equalsIgnoreCase("time")) { + replacement = "time"; + } + else if (replacement.startsWith("\"")) { // if the variable name is quoted, change the quotes to modelica syntax replacement = "'" + replacement.substring(1, replacement.length() - 1) + "'"; } 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 89e68cbf..7bf61c52 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 @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import org.simantics.sysdyn.modelImport.model.support.Enumeration; import org.simantics.sysdyn.modelImport.model.support.Variable; public class MdlModel { @@ -12,7 +13,7 @@ public class MdlModel { private HashMap variables; private HashMap> groups; - private HashMap subscripts; + private HashMap subscripts; private ArrayList sketches; public MdlModel(String name) { @@ -20,7 +21,7 @@ public class MdlModel { this.variables = new HashMap(); this.groups = new HashMap>(); - this.subscripts = new HashMap(); + this.subscripts = new HashMap(); this.sketches = new ArrayList(); } @@ -31,6 +32,8 @@ public class MdlModel { public void addVariable(Variable variable, String group) { if (variables.get(variable.getName()) != null) { System.err.println("warning, duplicate variable "+variable.getName()); + variables.get(variable.getName()).mergeWithVariable(variable); + return; } variables.put(variable.getName(), variable); @@ -51,16 +54,16 @@ public class MdlModel { return new ArrayList(variables.values()); } - public void addSubscript(Subscript subscript) { + public void addSubscript(Enumeration subscript) { subscripts.put(subscript.getName(), subscript); } - public Subscript getSubscript(String name) { + public Enumeration getSubscript(String name) { return subscripts.get(name); } - public List getSubscripts() { - return new ArrayList(subscripts.values()); + public List getSubscripts() { + return new ArrayList(subscripts.values()); } public void addSketch(Sketch sketch) { 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 619b1f00..28405d64 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,6 +2,7 @@ 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.element.Symbol; @@ -21,6 +22,8 @@ public class Sketch { private ArrayList valves; private ArrayList variables; + private HashMap objects; + public Sketch() { edgesOutOfDate = true; @@ -28,6 +31,8 @@ public class Sketch { connections = new ArrayList(); valves = new ArrayList(); variables = new ArrayList(); + + objects = new HashMap(); } public String getName() { @@ -82,12 +87,22 @@ public class Sketch { return bottomEdge - topEdge; } - public void addObject(SketchComment comment) { - + public void setOffset(double horizontal, double vertical) { + hOffset = horizontal; + vOffset = vertical; + } + + public double getHorizontalOffset() { + return hOffset; + } + + public double getVerticalOffset() { + return vOffset; } public void addComment(SketchComment comment) { comments.add(comment); + objects.put(comment.getId(), comment); } public List getComments() { @@ -96,6 +111,7 @@ public class Sketch { public void addConnection(SketchConnection connection) { connections.add(connection); + objects.put(connection.getId(), connection); } public List getConnections() { @@ -104,6 +120,10 @@ public class Sketch { public void addValve(SketchValve valve) { valves.add(valve); + objects.put(valve.getId(), valve); + // replace the attached variable with the valve in order to redirect + // possible connections to the variable to the valve + objects.put(valve.getAttachedVariable().getId(), valve); } public List getValves() { @@ -112,12 +132,27 @@ public class Sketch { public void addVariable(SketchVariable variable) { variables.add(variable); + objects.put(variable.getId(), variable); } public List getVariables() { return variables; } + public SketchObject getObject(int id) { + return objects.get(id); + } + + public List getShadowVariables() { + List variables = new ArrayList(); + for (SketchVariable var : getVariables()) { + if (!var.allowsIn()) { + variables.add(var); + } + } + return variables; + } + public List getAllElements() { ArrayList list = new ArrayList(); list.addAll(getComments()); @@ -126,6 +161,13 @@ public class Sketch { return list; } + public List getIndependentElements() { + List elements = new ArrayList(); + elements.addAll(getAllElements()); + elements.removeAll(getShadowVariables()); + return elements; + } + // relevant for sysdyn model creation, this is not the cleanest place to // store this information but it works 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 53ef8f7e..9aaac03c 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 @@ -27,21 +27,18 @@ public class SketchComment extends SketchElement { return icon; } - public Symbol getModelElement(double hOffset, double vOffset) { - if (icon.equals(CommentIcon.CLOUD)) { - return new Cloud( - getSysdyndX() + hOffset, - getSysdyndY() + vOffset, - getSysdynWidth(), - getSysdynHeight()); + @Override + public Symbol getSymbol(Sketch sketch) { + double[] dimensions = getDimensions(sketch); + + if (isInputOutput()) { + return new Comment(dimensions, "input / output objects are not supported yet"); } - else { - return new Comment( - getSysdyndX() + hOffset, - getSysdyndY() + vOffset, - getSysdynWidth(), - getSysdynHeight(), - text); + + switch (icon) { + case CLOUD: return new Cloud(dimensions); + case OTHER: return new Comment(dimensions, 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 58f23af4..bfd34066 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 @@ -32,36 +32,48 @@ public class SketchConnection extends SketchObject { public ConnectionType getType() { return type; } + + public int[] getPoints() { + return points; + } - public Connection getWriteableConnection(Symbol head, Symbol tail, double offset) { - if (type.equals(ConnectionType.ARROW)) { - return new Dependency(head, tail, false, false, getSysdynAngle(tail, head, offset)); + public Connection getConnection(Sketch sketch) { + Symbol tail = (Symbol)sketch.getObject(from).getModelObject(); + if (tail == null) { + System.err.println("connection tail not created"); + return null; } - else if (type.equals(ConnectionType.LINE_ARROW)) { - return new Flow(head, tail); + Symbol head = (Symbol)sketch.getObject(to).getModelObject(); + if (head == null) { + System.err.println("connection head not created"); + return null; } - else if (type.equals(ConnectionType.LINE_SEGMENT)) { - // TODO: what is this I don't even... - return new Flow(tail, head); + + switch (type) { + case ARROW: return new Dependency(tail, head, true, false, getAngle(sketch)); + case LINE_ARROW: return new Flow(tail, head); + case LINE_SEGMENT: return new Flow(head, tail); + default: return null; } - return null; } - // TODO: comment this? - public double getSysdynAngle(Symbol from, Symbol to, double voffset) { - if (points == null || points.length == 0) { + public double getAngle(Sketch sketch) { + if (points == null || points.length < 2) { return 0; } + SketchElement tail = (SketchElement)sketch.getObject(from); + SketchElement head = (SketchElement)sketch.getObject(to); + // 'from' element is in (x0, y0) and 'to' element is in (x2, y2) - double x0 = from.getX() + (from.getWidth() / 2); - double y0 = from.getY() + (from.getHeight() / 2); - double x2 = to.getX() + (to.getWidth() / 2); - double y2 = to.getY() + (to.getHeight() / 2); + double x0 = tail.getX(); + double y0 = tail.getY(); + double x2 = head.getX(); + double y2 = head.getY(); // treat the first points in points as the control point (x1, y1) - double x1 = (double)points[0] * SCALE_MULTIPLIER; - double y1 = (double)points[1] * SCALE_MULTIPLIER + voffset; + double x1 = (double)points[0]; + double y1 = (double)points[1]; //System.err.println("("+x0+","+y0+") -> ("+x1+","+y1+") -> ("+x2+","+y2+")"); @@ -82,8 +94,8 @@ public class SketchConnection extends SketchObject { else { // (p1-p0) * (p1-p2) / dd double offset = (dx0*dx1 + dy0*dy1) / dd; - double angle = Math.PI*0.5 - Math.atan(offset); - if (dd > 0.0) + double angle = Math.PI/2 - Math.atan(offset); + if (dd > 0) angle = angle - Math.PI; return angle; } 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 2af38c2a..3ab316ff 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 @@ -105,6 +105,25 @@ public abstract class SketchElement extends SketchObject { return textLine; } - public abstract Symbol getModelElement(double hOffset, double vOffset); + public abstract Symbol getSymbol(Sketch sketch); + + private static final double SCALE_MULTIPLIER = 0.4; + + public double[] getDimensions(Sketch sketch) { + double[] dimensions = getDimensions(); + dimensions[0] = dimensions[0] + sketch.getHorizontalOffset(); + dimensions[1] = dimensions[1] + sketch.getVerticalOffset(); + return dimensions; + } + + // get element dimensions, [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; + } } 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 d554ec59..62eba880 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 @@ -1,10 +1,11 @@ package org.simantics.sysdyn.modelImport.mdl; +import org.simantics.sysdyn.modelImport.model.IWriteableObject; + public abstract class SketchObject { - - protected static final double SCALE_MULTIPLIER = 0.4; private int id; + private IWriteableObject modelObject; public SketchObject(int id) { this.id = id; @@ -14,4 +15,12 @@ public abstract class SketchObject { return id; } + public void setModelObject(IWriteableObject modelObject) { + this.modelObject = modelObject; + } + + public IWriteableObject getModelObject() { + return 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 7685c056..c393d1ba 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 @@ -1,5 +1,6 @@ package org.simantics.sysdyn.modelImport.mdl; +import org.simantics.sysdyn.modelImport.model.IWriteableObject; import org.simantics.sysdyn.modelImport.model.element.Symbol; import org.simantics.sysdyn.modelImport.model.element.Valve; import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition; @@ -22,15 +23,20 @@ public class SketchValve extends SketchElement { this.variable = variable; } + public TextPosition getTextPosition() { + return textPosition; + } + + @Override + public Symbol getSymbol(Sketch sketch) { + return new Valve(getDimensions(sketch), variable.getVariable(), textPosition); + } + + // TODO: do we really want to do this? @Override - public Symbol getModelElement(double hOffset, double vOffset) { - return new Valve( - getSysdyndX() + hOffset, - getSysdyndY() + vOffset, - getSysdynWidth(), - getSysdynHeight(), - variable.getVariable(), - textPosition); + public void setModelObject(IWriteableObject modelObject) { + super.setModelObject(modelObject); + variable.setModelObject(modelObject); } } 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 2770b5e1..9cef8184 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 @@ -20,23 +20,13 @@ public class SketchVariable extends SketchElement { } @Override - public Symbol getModelElement(double hOffset, double vOffset) { + public Symbol getSymbol(Sketch sketch) { if (variable.getExpression() instanceof IntegralExpression) { - return new Stock( - getSysdyndX() + hOffset, - getSysdyndY() + vOffset, - getSysdynWidth(), - getSysdynHeight(), - variable); + return new Stock(getDimensions(sketch), variable); } else { - return new Auxiliary( - getSysdyndX() + hOffset, - getSysdyndY() + vOffset, - getSysdynWidth(), - getSysdynHeight(), - variable); + return new Auxiliary(getDimensions(sketch), variable); } } -} +} \ 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 deleted file mode 100644 index eb6e8852..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.simantics.sysdyn.modelImport.mdl; - -import java.util.ArrayList; -import java.util.Arrays; - -public class Subscript { - - // TODO: THIS IS PROBABLY NOT NEEDED AT ALL, COULD USE ENUMERATION DIRECTLY - - private String name; - private ArrayList values; - - public Subscript(String name, String...values) { - this.name = name; - this.values = new ArrayList(Arrays.asList(values)); - } - - public Subscript(String name, Subscript other) { - this.name = name; - this.values = other.getValues(); - } - - public String getName() { - return name; - } - - public ArrayList getValues() { - return values; - } - -} 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 2cc8e282..a0727b88 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 @@ -38,10 +38,10 @@ public class Model implements IWriteableObject { private double start, stop, step; // the structure of the model - private HashMap enumerations; private HashMap variables; + private HashMap enumerations; - private ArrayList elements; + private ArrayList symbols; private ArrayList shadows; private ArrayList connections; @@ -50,10 +50,10 @@ public class Model implements IWriteableObject { public Model(String name) { this.name = name; - enumerations = new HashMap(); variables = new HashMap(); + enumerations = new HashMap(); - elements = new ArrayList(); + symbols = new ArrayList(); shadows = new ArrayList(); connections = new ArrayList(); } @@ -82,13 +82,6 @@ public class Model implements IWriteableObject { this.step = step; } - public void addEnumeration(Enumeration enumeration) { - if (enumerations.get(enumeration.getName()) != null) { - System.err.println("enumeration "+enumeration.getName()+" already defined"); - } - enumerations.put(enumeration.getName(), enumeration); - } - public Enumeration getEnumeration(String name) { return enumerations.get(name); } @@ -97,15 +90,6 @@ public class Model implements IWriteableObject { return enumerations.values(); } - public void addVariable(ModelVariable variable) { - if (enumerations.get(variable.getName()) != null) { - System.err.println("variable "+variable.getName()+" already defined"); - } - variables.put(variable.getName(), variable); - - addElement(variable); - } - public ModelVariable getVariable(String name) { return variables.get(name); } @@ -113,18 +97,37 @@ public class Model implements IWriteableObject { public Collection getVariables() { return variables.values(); } + + public void addSymbol(ModelVariable variable) { + if (variables.get(variable.getName()) != null) { + System.err.println("variable "+variable.getName()+" already defined"); + } + variables.put(variable.getName(), variable); + + System.err.println("variable added "+variable.getName()); - public void addElement(Symbol element) { - elements.add(element); + symbols.add(variable); } - public void addShadow(Shadow shadow) { + public void addSymbol(Shadow shadow) { shadows.add(shadow); } + + public void addSymbol(Symbol element) { + symbols.add(element); + } public void addConnection(Connection connection) { + // TODO: make sure connection head and tail exist connections.add(connection); } + + public void addEnumeration(Enumeration enumeration) { + if (enumerations.get(enumeration.getName()) != null) { + System.err.println("enumeration "+enumeration.getName()+" already defined"); + } + enumerations.put(enumeration.getName(), enumeration); + } @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { @@ -144,7 +147,7 @@ public class Model implements IWriteableObject { e.write(graph, configuration, context); } - for (Symbol e : elements) { + for (Symbol e : symbols) { e.write(graph, configuration, context); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java index 39e978e4..1cc4f3d2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/WriteContext.java @@ -1,7 +1,12 @@ package org.simantics.sysdyn.modelImport.model; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import org.simantics.db.Resource; import org.simantics.sysdyn.modelImport.model.support.Enumeration; public class WriteContext { @@ -14,6 +19,7 @@ public class WriteContext { private long flowCount; private HashMap enumerations; + private HashMap> enumerationValues; public WriteContext() { objectCount = 0; @@ -24,6 +30,7 @@ public class WriteContext { flowCount = 0; enumerations = new HashMap(); + enumerationValues = new HashMap>(); } public String getNextObject() { @@ -50,18 +57,49 @@ public class WriteContext { return "Flow" + flowCount++; } - // register an enumeration, necessary for enumeration expressions - public void registerEnumeration(Enumeration enumeration) { - + public void registerEnumeration(Enumeration enumeration, boolean copy) { + enumerations.put(enumeration.getName(), enumeration); + if (enumerationValues.get(enumeration.getName()) == null) { + enumerationValues.put(enumeration.getName(), new HashSet()); + } + enumerationValues.get(enumeration.getName()).add(enumeration); + if (!copy) { + for (String value : enumeration.getValues()) { + if (enumerationValues.get(value) == null) { + enumerationValues.put(value, new HashSet()); + } + enumerationValues.get(value).add(enumeration); + } + } } - - // attempt to obtain a reference to an enumeration based on either the - // name of the enumeration or the name of one of the elements of the - // enumeration - // (TODO: this must be updated to something more clever if there are - // collisions between names of enumerations and elements of enumerations) - public Enumeration resolveEnumeration(String lookup) { - return null; + + public Enumeration getEnumeration(Set indices) { + System.err.println("size is "+indices.size()); + if (indices.size() == 1) { + Enumeration enumeration = enumerations.get(indices.iterator().next()); + if (enumeration != null) { + return enumeration; + } + } + + // the set of potential matches is the intersection of all sets of + // enumerations that contain any of the given indices as elements + Set potential = new HashSet(enumerations.values()); + for (String index : indices) { + potential.retainAll(enumerationValues.get(index)); + } + + // if there is more than one potential match, the enumerations are not + // well defined (there are multiple enumerations with similar indices) + if (potential.size() == 1) { + return potential.iterator().next(); + } + else { + System.err.println("enumeration could not be resolved "+potential.size()); + for (String index : indices) + System.err.println(" "+index); + return null; + } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java index add03008..11ddf9d3 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java @@ -14,6 +14,10 @@ public class Auxiliary extends ModelVariable { super(x, y, w, h, name, expression, range, unit, description); } + public Auxiliary(double[] dimensions, Variable variable) { + super(dimensions, variable); + } + public Auxiliary(double x, double y, double w, double h, Variable variable) { super(x, y, w, h, variable); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java index bb1f2723..10050518 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java @@ -17,6 +17,10 @@ public class Cloud extends Symbol { public Cloud(double x, double y, double w, double h) { super(x, y, w, h); } + + public Cloud(double[] dimensions) { + super(dimensions); + } @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java index fb737ba7..2c724553 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java @@ -17,6 +17,11 @@ public class Comment extends Symbol { super(x, y, w, h); this.text = text; } + + public Comment(double[] dimensions, String text) { + super(dimensions); + this.text = text; + } @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java index d920ea75..967bd04b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java @@ -22,9 +22,9 @@ public abstract class Connection implements IWriteableObject { private Resource connection; private Resource diagramConnection; - public Connection(Symbol head, Symbol tail) { - this.head = head; + public Connection(Symbol tail, Symbol head) { this.tail = tail; + this.head = head; } @Override diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java index 72e34cb6..8da35e27 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java @@ -15,8 +15,8 @@ public class Dependency extends Connection { private boolean showDelay; private double angle; - public Dependency(Symbol head, Symbol tail, boolean showArrow, boolean showDelay, double angle) { - super(head, tail); + public Dependency(Symbol tail, Symbol head, boolean showArrow, boolean showDelay, double angle) { + super(tail, head); this.showArrow = showArrow; this.showDelay = showDelay; this.angle = angle; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java index da51170f..747819da 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java @@ -11,8 +11,8 @@ import org.simantics.sysdyn.modelImport.model.WriteContext; public class Flow extends Connection { - public Flow(Symbol head, Symbol tail) { - super(head, tail); + public Flow(Symbol tail, Symbol head) { + super(tail, head); } @Override diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java index cd2f452f..c297535f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java @@ -34,6 +34,15 @@ public abstract class ModelVariable extends Symbol { this.description = description; } + public ModelVariable(double[] dimensions, Variable variable) { + super(dimensions); + this.name = variable.getName(); + this.expression = variable.getExpression(); + this.range = variable.getRange(); + this.unit = variable.getUnit(); + this.description = variable.getDescription(); + } + public ModelVariable(double x, double y, double w, double h, Variable variable) { super(x, y, w, h); this.name = variable.getName(); @@ -61,7 +70,7 @@ public abstract class ModelVariable extends Symbol { // TODO: write references to enumerations here? if (expression != null) { - Resource expressionList = expression.write(graph, variable, context); + expression.write(graph, variable, context); } if (range != null) { range.write(graph, variable, context); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java index 8f3e33eb..0ad1822e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java @@ -22,6 +22,11 @@ public class Shadow extends Symbol { this.original = original; } + public Shadow(double[] dimensions, ModelVariable original) { + super(dimensions); + this.original = original; + } + public Shadow(SketchVariable variable, double hOffset, double vOffset, ModelVariable original) { super(variable, hOffset, vOffset); this.original = original; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java index 03351daa..0272567f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java @@ -18,6 +18,10 @@ public class Stock extends ModelVariable { super(x, y, w, h, variable); } + public Stock(double[] dimensions, Variable variable) { + super(dimensions, variable); + } + @Override public Resource getVariableType(ReadGraph graph) { return SysdynResource.getInstance(graph).Stock; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java index 58e99174..70fc0709 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java @@ -31,6 +31,13 @@ public abstract class Symbol implements IWriteableObject { this.height = height; } + public Symbol(double[] dimensions) { + this.x = dimensions[0]; + this.y = dimensions[1]; + this.width = dimensions[2]; + this.height = dimensions[3]; + } + public Symbol(SketchElement element, double hOffset, double vOffset) { this.x = element.getSysdyndX() + hOffset; this.y = element.getSysdyndY() + vOffset; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java index 521fc1e3..72cd9124 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java @@ -28,6 +28,11 @@ public class Valve extends ModelVariable { this.position = position; } + public Valve(double[] dimensions, Variable variable, TextPosition position) { + super(dimensions, variable); + this.position = position; + } + public Valve(double x, double y, double w, double h, Variable variable, TextPosition position) { super(x, y, w, h, variable); this.position = position; 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 bdb43e10..a39be3c3 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 @@ -1,8 +1,9 @@ package org.simantics.sysdyn.modelImport.model.expression; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -10,88 +11,87 @@ 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.sysdyn.modelImport.model.WriteContext; public class EnumerationExpression extends Expression { - private SubscriptSubExpression active; - private ArrayList subExpressions; + private List subExpressions; - public EnumerationExpression(String[] indices, Expression expression) { - active = new SubscriptSubExpression(indices, expression); + public EnumerationExpression(Expression expression, String...indices) { subExpressions = new ArrayList(); - subExpressions.add(active); + addExpression(expression, indices); } - public void addExpression(String[] indices, Expression expression) { - subExpressions.add(new SubscriptSubExpression(indices, expression)); - } - - public List getExpressions() { - return subExpressions; + public void addExpression(Expression expression, String...indices) { + subExpressions.add(new SubscriptSubExpression(expression, indices)); } public void addExpressions(List expressions) { subExpressions.addAll(expressions); } + public List getExpressions() { + return subExpressions; + } + @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - for (SubscriptSubExpression subexpr : subExpressions) { - subexpr.write(graph, parent, context); - } - - // write array index list - //graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, Arrays.asList())); - - // set active expression? - - return active.getResource(); - } - - private class SubscriptSubExpression extends Expression { - private String[] indices; - private Expression expression; - - public SubscriptSubExpression(String[] indices, Expression expression) { - this.indices = indices; - this.expression = expression; + ArrayList> enumerationIndices = new ArrayList>(); + for (int i = 0; i < subExpressions.get(0).indices.length; i++) { + enumerationIndices.add(new HashSet()); } - @Override - public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - + for (SubscriptSubExpression subexpr : subExpressions) { StringBuilder range = new StringBuilder(); range.append('['); - for (String index : indices) { - if (range.length() > 1) + for (int i = 0; i < subexpr.indices.length; i++) { + if (i > 0) range.append(','); - range.append(index); + range.append(subexpr.indices[i]); + + enumerationIndices.get(i).add(subexpr.indices[i]); } range.append(']'); - Resource expr = super.write(graph, parent, context); - - graph.claimLiteral(expr, sr.Expression_arrayRange, range.toString(), Bindings.STRING); + Resource res = subexpr.expression.write(graph, parent, context); - return expr; + graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING); } - - @Override - public Resource getExpressionType(ReadGraph graph) { - return expression.getExpressionType(graph); + + ArrayList enumerations = new ArrayList(); + for (Set set : enumerationIndices) { + enumerations.add(context.getEnumeration(set).getResource()); } + // write array index list + graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, enumerations)); + + // set active expression? + + return getResource(); + } + + private class SubscriptSubExpression { + + Expression expression; + String[] indices; + + public SubscriptSubExpression(Expression expression, String...indices) { + this.expression = expression; + this.indices = indices; + } } @Override public Resource getExpressionType(ReadGraph graph) { - return active.getExpressionType(graph); + return subExpressions.get(0).expression.getExpressionType(graph); + } + + @Override + public Resource getResource() { + return subExpressions.get(0).expression.getResource(); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java index 4bac0c81..84eac663 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java @@ -28,11 +28,13 @@ public abstract class Expression implements IWriteableObject { // insert the expression into the expression list of the variable // (necessary because of enumeration expressions) Resource list = graph.getPossibleObject(parent, sr.Variable_expressionList); - if (list != null) + if (list != null) { ListUtils.insertBack(graph, list, Arrays.asList(expr)); - else + } + else { graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expr))); - + } + return expr; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IntegralExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IntegralExpression.java index 3a144117..bc75c359 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IntegralExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IntegralExpression.java @@ -26,6 +26,7 @@ public class IntegralExpression extends Expression { graph.claimLiteral(expr, sr.StockExpression_integralEquation, integral, Bindings.STRING); graph.claimLiteral(expr, sr.StockExpression_initialEquation, initial, Bindings.STRING); + graph.claim(expr, sr.StockExpression_useCustomIntegral, expr); return expr; } 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 64d9df06..de793edb 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,39 +1,76 @@ package org.simantics.sysdyn.modelImport.model.support; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; 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.sysdyn.modelImport.model.IWriteableObject; import org.simantics.sysdyn.modelImport.model.WriteContext; public class Enumeration implements IWriteableObject { private String name; - private ArrayList values; + private Set values; + private boolean copy; - private Resource resource; + private Resource enumeration; - public Enumeration(String name, ArrayList values) { + public Enumeration(String name, String...values) { this.name = name; - this.values = values; + this.values = new HashSet(Arrays.asList(values)); + + copy = false; + } + + public Enumeration(String name, Enumeration other) { + this.name = name; + this.values = other.getValues(); + + copy = true; } public String getName() { return name; } + + public Set getValues() { + return values; + } @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - System.err.println("write enumeration"); - return null; + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + enumeration = GraphUtils.create2(graph, sr.Enumeration, + l0.HasName, name, + l0.PartOf, parent); + + Set indices = new HashSet(); + for (String index : values) { + indices.add(GraphUtils.create2(graph, sr.EnumerationIndex, + l0.HasName, index)); + } + + graph.claim(enumeration, sr.Enumeration_enumerationIndexList, ListUtils.create(graph, indices)); + + // register this enumeration with the context + context.registerEnumeration(this, copy); + + return enumeration; } @Override public Resource getResource() { - return resource; + return enumeration; } } -- 2.47.1