From e1bcfda16fc5b078e566077d6d962dc09251d0c6 Mon Sep 17 00:00:00 2001 From: jkauttio Date: Fri, 11 Apr 2014 15:14:17 +0000 Subject: [PATCH] Update vensim import, subscripts might finally work refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches@29281 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/modelImport/MdlParser.java | 56 ++- .../simantics/sysdyn/modelImport/MdlUtil.java | 432 ++++++++++-------- .../modelImport/mdl/LookupVariable.java | 84 ++++ .../sysdyn/modelImport/mdl/MdlModel.java | 102 ++++- .../sysdyn/modelImport/mdl/SketchComment.java | 15 +- .../modelImport/mdl/SketchConnection.java | 4 + .../sysdyn/modelImport/mdl/SketchElement.java | 17 +- .../sysdyn/modelImport/mdl/SketchValve.java | 19 +- .../modelImport/mdl/SketchVariable.java | 22 +- .../sysdyn/modelImport/mdl/Subscript.java | 96 ++++ .../modelImport/mdl/SubscriptVariable.java | 127 +++++ .../sysdyn/modelImport/mdl/Variable.java | 177 +++++++ .../modelImport/model/element/Auxiliary.java | 14 +- .../modelImport/model/element/Cloud.java | 8 +- .../modelImport/model/element/Comment.java | 19 +- .../modelImport/model/element/Connection.java | 24 +- .../modelImport/model/element/Dependency.java | 36 +- .../modelImport/model/element/Flow.java | 6 +- .../model/element/ModelVariable.java | 74 ++- .../modelImport/model/element/Shadow.java | 21 +- .../modelImport/model/element/Stock.java | 14 +- .../modelImport/model/element/Symbol.java | 56 ++- .../modelImport/model/element/Valve.java | 56 ++- .../expression/EnumerationExpression.java | 45 +- .../model/support/Enumeration.java | 18 +- .../modelImport/model/support/Range.java | 8 +- .../modelImport/model/support/Variable.java | 61 --- 27 files changed, 1143 insertions(+), 468 deletions(-) create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java create mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java delete mode 100644 dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java index 62e8a668..a26110b6 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.HashMap; import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon; +import org.simantics.sysdyn.modelImport.mdl.LookupVariable; import org.simantics.sysdyn.modelImport.mdl.MdlModel; import org.simantics.sysdyn.modelImport.mdl.Sketch; import org.simantics.sysdyn.modelImport.mdl.SketchComment; @@ -28,6 +29,9 @@ 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.mdl.SubscriptVariable; +import org.simantics.sysdyn.modelImport.mdl.Variable; import org.simantics.sysdyn.modelImport.model.Model; import org.simantics.sysdyn.modelImport.model.element.Auxiliary; import org.simantics.sysdyn.modelImport.model.element.Cloud; @@ -41,7 +45,6 @@ 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 { @@ -55,15 +58,20 @@ public class MdlParser { e.printStackTrace(); return null; } - + // generate a sysdyn model from the mdl model + Model model = new Model(mdl.getName()); double offset = 0; // add anumerations - for (Enumeration enumeration : mdl.getSubscripts()) { - model.addEnumeration(enumeration); + for (Subscript subscript : mdl.getAllSubscripts()) { + if (subscript.isSuperSet()) { + Enumeration enumeration = new Enumeration(subscript.getName(), subscript.getValues()); + subscript.setEnumeration(enumeration); + model.addEnumeration(enumeration); + } } // add sketch labels and independent elements @@ -71,27 +79,26 @@ public class MdlParser { sketch.setOffset(0, offset); - model.addSymbol(new Comment(0, offset, -1, -1, sketch.getName())); + //model.addSymbol(new Comment(0, offset, -1, -1, sketch.getName())); for (SketchElement element : sketch.getIndependentElements()) { - Symbol symbol = element.getSymbol(sketch); + Symbol symbol = element.getSymbol(mdl, sketch); element.setModelObject(symbol); model.addSymbol(symbol); } offset += 200; } - + // add dependent elements for (Sketch sketch : mdl.getSketches()) { 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); + Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(mdl, sketch); variable.setModelObject(symbol); model.addSymbol(symbol); } @@ -117,7 +124,9 @@ public class MdlParser { private static MdlModel parseFile(File file) throws Exception { - MdlModel mdl = new MdlModel(file.getName()); + String name = file.getName().substring(0, file.getName().length()-4); + + MdlModel mdl = new MdlModel(name); // peek at the first line to see if we need to use UTF-8 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); @@ -162,26 +171,25 @@ public class MdlParser { String cat; Variable var; - Enumeration sub; + SubscriptVariable svar; + LookupVariable lvar; + Subscript subs; // parse the (possible) variable declaration if ((cat = MdlUtil.getPossibleCategory(str)) != null) { category = cat; } - else if ((var = MdlUtil.getPossibleNormalVariable(str)) != null) { + else if ((var = Variable.getPossibleVariable(str)) != null) { mdl.addVariable(var, category); } - else if ((var = MdlUtil.getPossibleSubscriptVariable(str)) != null) { - mdl.addVariable(var, category); - } - else if ((var = MdlUtil.getPossibleLookUpVariable(str)) != null) { - mdl.addVariable(var, category); + else if ((svar = SubscriptVariable.getPossibleSubscriptVariable(str)) != null) { + mdl.addVariable(svar, category); } - else if ((var = MdlUtil.getPossibleNoExpressionVariable(str)) != null) { - mdl.addVariable(var, category); + else if ((lvar = LookupVariable.getPossibleLookUpVariable(str)) != null) { + mdl.addVariable(lvar, category); } - else if ((sub = MdlUtil.getPossibleSubscript(str, mdl)) != null) { - mdl.addSubscript(sub); + else if ((subs = Subscript.getPossibleSubscript(str, mdl)) != null) { + mdl.addSubscript(subs); } else { // if we got this far, the variable could not be parsed @@ -189,8 +197,6 @@ public class MdlParser { } } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START)); - - // add all variables to the model // END READING VARIABLE DATA @@ -207,6 +213,8 @@ public class MdlParser { // skip empty lines if (line.isEmpty()) continue; + + ArrayList connections = new ArrayList(); SketchObject so; @@ -223,7 +231,7 @@ public class MdlParser { else if (line.startsWith("$")) { // font declaration, nothing to do here } - else if ((so = MdlUtil.getPossibleSketchConnection(line)) != null) { + else if ((so = MdlUtil.getPossibleSketchConnection(line, sketch)) != null) { sketch.addConnection((SketchConnection)so); } else if ((so = MdlUtil.getPossibleSketchVariable(line, mdl)) != null) { diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java index d1787bbd..7b547672 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java @@ -1,15 +1,33 @@ package org.simantics.sysdyn.modelImport; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.simantics.sysdyn.modelImport.mdl.LookupVariable; import org.simantics.sysdyn.modelImport.mdl.MdlModel; +import org.simantics.sysdyn.modelImport.mdl.Sketch; import org.simantics.sysdyn.modelImport.mdl.SketchComment; 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.mdl.SubscriptVariable; +import org.simantics.sysdyn.modelImport.mdl.Variable; +import org.simantics.sysdyn.modelImport.model.element.Auxiliary; +import org.simantics.sysdyn.modelImport.model.element.Cloud; +import org.simantics.sysdyn.modelImport.model.element.Comment; +import org.simantics.sysdyn.modelImport.model.element.Connection; +import org.simantics.sysdyn.modelImport.model.element.Dependency; +import org.simantics.sysdyn.modelImport.model.element.Flow; +import org.simantics.sysdyn.modelImport.model.element.ModelVariable; +import org.simantics.sysdyn.modelImport.model.element.Stock; +import org.simantics.sysdyn.modelImport.model.element.Symbol; +import org.simantics.sysdyn.modelImport.model.element.Valve; +import org.simantics.sysdyn.modelImport.model.element.Valve.Orientation; 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; @@ -19,7 +37,6 @@ 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; public class MdlUtil { @@ -32,7 +49,7 @@ public class MdlUtil { } private static final String NUMBER = - "-?\\d+(?:.\\d+)?"; + "-?\\d+(?:\\.\\d+)?"; // most of this data is from the documentation of the .mdl file format // available in http://www.vensim.com/documentation/24305.htm @@ -62,12 +79,12 @@ public class MdlUtil { private static final String NORMAL_NAME_PATTERN = VAR_NAME_NORMAL+"\\s*=\\s*"; + public static final String NO_EXPRESSION_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*"; @@ -79,19 +96,27 @@ public class MdlUtil { public static final String normalVariablePattern = NORMAL_NAME_PATTERN +EXPRESSION_PATTERN+UNIT_PATTERN+DESC_PATTERN; + public static final String noExpressionVariablePattern = + NO_EXPRESSION_NAME_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 normalVariableName = 1; private static final int normalVariableExpression = 2; + private static final int normalVariableUnit = 3; + private static final int normalVariableDesc = 4; + + private static final int noExpressionVariableName = 1; + private static final int noExpressionVariableUnit = 2; + private static final int noExpressionVariableDesc = 3; private static final int subscriptVariableName = 1; private static final int subscriptVariableIndices = 2; private static final int subscriptVariableExpression = 3; + private static final int subscriptVariableUnit = 4; + private static final int subscriptVariableDesc = 5; private static final int lookupVariableName = 1; private static final int lookupVariableRangeXMin = 2; @@ -99,86 +124,63 @@ public class MdlUtil { private static final int lookupVariableRangeXMax = 4; private static final int lookupVariableRangeYMax = 5; private static final int lookupVariablePoints = 6; - - private static final int noExpressionVariableName = 1; + private static final int lookupVariableUnit = 7; + private static final int lookupVariableDesc = 8; public static Variable getPossibleNormalVariable(String line) { Matcher matcher = Pattern.compile(normalVariablePattern).matcher(line); - - if (!matcher.matches()) { + if (!matcher.matches()) return null; - } - String name = normalize(matcher.group(normalVariableName)); - Expression expression = parseExpression(matcher.group(normalVariableExpression)); + String name = matcher.group(normalVariableName); + String expression = matcher.group(normalVariableExpression); + String unit = matcher.group(normalVariableUnit); + String desc = matcher.group(normalVariableDesc); - return getVariable(matcher, 2, name, expression); + return new Variable(name, expression, unit, desc); } - public static Variable getPossibleSubscriptVariable(String line) { - Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line); - - if (!matcher.matches()) { + public static Variable getPossibleNoExpressionVariable(String line) { + Matcher matcher = Pattern.compile(noExpressionVariablePattern).matcher(line); + if (!matcher.matches()) return null; - } - String name = normalize(matcher.group(subscriptVariableName)); - String[] indices = normalize(matcher.group(subscriptVariableIndices)).split(","); - Expression expression = parseExpression(matcher.group(subscriptVariableExpression)); - EnumerationExpression e = new EnumerationExpression(expression, indices); + String name = matcher.group(noExpressionVariableName); + String unit = matcher.group(noExpressionVariableUnit); + String desc = matcher.group(noExpressionVariableDesc); - return getVariable(matcher, 3, name, e); + return new Variable(name, null, unit, desc); } - public static Variable getPossibleLookUpVariable(String line) { - Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line); - - if (!matcher.matches()) { + public static SubscriptVariable getPossibleSubscriptVariable(String line) { + Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line); + if (!matcher.matches()) return null; - } - String name = normalize(matcher.group(lookupVariableName)); - double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin)); - double xMax = Double.parseDouble(matcher.group(lookupVariableRangeYMin)); - 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); + String name = matcher.group(subscriptVariableName); + String[] subscript = matcher.group(subscriptVariableIndices).split(","); + String expression = matcher.group(subscriptVariableExpression); + String unit = matcher.group(subscriptVariableUnit); + String desc = matcher.group(subscriptVariableDesc); - return getVariable(matcher, 6, name, expression); + return new SubscriptVariable(name, expression, unit, desc, subscript); } - private static double[] parseLookup(String points) { - // a hack to remove the leading empty string otherwise produced by split() - points = points.replaceFirst("[(),]+", ""); - String[] parts = points.split("[(),]+"); - double[] result = new double[parts.length]; - for (int i = 0; i < parts.length; i++) { - result[i] = Double.parseDouble(parts[i]); - } - return result; - } - - public static Variable getPossibleNoExpressionVariable(String line) { - Matcher matcher = Pattern.compile(noExpressionVariablePattern).matcher(line); - - if (!matcher.matches()) { + public static LookupVariable getPossibleLookUpVariable(String line) { + Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line); + if (!matcher.matches()) return null; - } - String name = normalize(matcher.group(noExpressionVariableName)); + String name = matcher.group(lookupVariableName); + double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin)); + double yMin = Double.parseDouble(matcher.group(lookupVariableRangeYMin)); + double xMax = Double.parseDouble(matcher.group(lookupVariableRangeXMax)); + double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax)); + String expression = matcher.group(lookupVariablePoints); + String unit = matcher.group(lookupVariableUnit); + String desc = matcher.group(lookupVariableDesc); - 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(groups + 2); - return new Variable(name, expression, unit, range, description); + return new LookupVariable(name, expression, unit, desc, xMin, yMin, xMax, yMax); } private static final String SUBSCRIPT_PATTERN = @@ -191,129 +193,32 @@ public class MdlUtil { private static final int subscriptType = 2; private static final int subscriptExpression = 3; - public static Enumeration getPossibleSubscript(String line, MdlModel mdl) { - Matcher matcher = Pattern.compile(subscriptPattern).matcher(line); - - if (!matcher.matches()) { + public static Subscript getPossibleSubscript(String line, MdlModel mdl) { + Matcher matcher = Pattern.compile(subscriptPattern).matcher(line); + if (!matcher.matches()) return null; - } - String name = normalize(matcher.group(subscriptName)); - boolean equivalence = matcher.group(subscriptType).equals("<->"); - String expression = normalize(matcher.group(subscriptExpression)); + String name = matcher.group(subscriptName); + boolean equivalent = matcher.group(subscriptType).equals("<->"); + String expression = matcher.group(subscriptExpression); - if (equivalence) { - return new Enumeration(name, mdl.getSubscript(expression)); - } - else { - return new Enumeration(name, expression.split(",")); - } - } - - private static Expression parseExpression(String equation) { - Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(equation); - - if (!matcher.matches()) { - return new NormalExpression(normalize(equation)); - } - - String function = matcher.group(1); - String[] parameters = splitParameters(matcher.group(2)); - - if (function.startsWith("INTEG")) { - if (parameters.length != 2) { - System.err.println("malformed integral expression: "+equation); + if (equivalent) { + Subscript parent = mdl.getSubscript(expression); + if (parent == null) { + System.err.println("equivalent subscript not found for "+name); return null; } - String integral = normalize(parameters[0]); - String initial = normalize(parameters[1]); - return new IntegralExpression(integral, initial); - } - else if (function.startsWith("DELAY N")) { - if (parameters.length != 4) { - System.err.println("malformed delay expression: "+equation); - return null; - } - String expression = normalize(parameters[0]); - String time = normalize(parameters[1]); - String initial = normalize(parameters[2]); - int degree = Integer.parseInt(parameters[3]); - return new DelayExpression(expression, time, initial, degree); - } - else if (function.startsWith("GAME")) { - if (parameters.length != 1) { - System.err.println("malformed game expression: "+equation); - return null; - } - // game expressions are currently treated as normal expressions - return new NormalExpression(normalize(parameters[0])); + return new Subscript(name, parent.getValues()); } else { - // the function does not require special handling - return new NormalExpression(normalize(equation)); - } - } - - private static String[] splitParameters(String str) { - ArrayList list = new ArrayList(); - - int i; - int last = 0; - int level = 0; - boolean comment = false; - boolean brackets = false; - - for (i = 0; i < str.length(); i++) { - char current = str.charAt(i); - if (current == '"') - // note that this does not currently support escaped quotation marks inside quoted variable names - comment = !comment; - else if (current == '[' && !comment) - brackets = true; - else if (current == ']' && !comment) - brackets = false; - else if (current == '(' && !comment && !brackets) - level++; - else if (current == ')' && !comment && !brackets) - level--; - else if (current == ',' && !comment && !brackets && level == 0) { - list.add(str.substring(last, i).trim()); - last = i + 1; - } - } - if (last < i) { - list.add(str.substring(last, i).trim()); - } - - return list.toArray(new String[list.size()]); - } - - private static Range parseRange(String unit) { - Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(("+NUMBER+"),("+NUMBER+")|\\?)\\]").matcher(unit); - if (matcher.find()) { - Double start, end, step; - start = Double.parseDouble(matcher.group(1)); - if (matcher.group(2).equals("?")) { - end = null; - step = null; - } - else { - end = Double.parseDouble(matcher.group(3)); - step = Double.parseDouble(matcher.group(4)); - } - return new Range(start, end, step, matcher.group()); - } - else { - return null; + return new Subscript(name, new HashSet(Arrays.asList(expression.split(",")))); } } public static String getPossibleCategory(String line) { - Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line); - - if (!matcher.matches()) { + Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line); + if (!matcher.matches()) return null; - } return matcher.group(1); } @@ -335,12 +240,22 @@ public class MdlUtil { private static final int connectionShape = 4; private static final int connectionPoints = 5; - public static SketchConnection getPossibleSketchConnection(String line) { + public static int getConnectionId(Matcher matcher) { + return Integer.parseInt(matcher.group(connectionId)); + } + + public static int getConnectionFrom(Matcher matcher) { + return Integer.parseInt(matcher.group(connectionFrom)); + } + + public static int getConnectionTo(Matcher matcher) { + return Integer.parseInt(matcher.group(connectionTo)); + } + + public static SketchConnection getPossibleSketchConnection(String line, Sketch sketch) { Matcher matcher = Pattern.compile(sketchConnection).matcher(line); - - if (!matcher.matches()) { + if (!matcher.matches()) return null; - } int id = Integer.parseInt(matcher.group(connectionId)); int from = Integer.parseInt(matcher.group(connectionFrom)); @@ -391,15 +306,13 @@ public class MdlUtil { public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) { Matcher matcher = Pattern.compile(sketchVariable).matcher(line); - - if (!matcher.matches()) { + if (!matcher.matches()) return null; - } int id = Integer.parseInt(matcher.group(elementId)); Variable var = mdl.getVariable(normalize(matcher.group(elementName))); if (var == null) { - System.err.println("could not find variable corresponding to "+normalize(matcher.group(elementName))); + System.err.println("could not find variable corresponding to "+matcher.group(elementName)); } SketchVariable variable = new SketchVariable(id, var); @@ -411,10 +324,8 @@ public class MdlUtil { public static SketchValve getPossibleSketchValve(String line) { Matcher matcher = Pattern.compile(sketchValve).matcher(line); - - if (!matcher.matches()) { + if (!matcher.matches()) return null; - } int id = Integer.parseInt(matcher.group(elementId)); TextPosition pos = getTextPos(matcher); @@ -428,10 +339,8 @@ public class MdlUtil { public static SketchComment getPossibleSketchComment(String line) { Matcher matcher = Pattern.compile(sketchComment).matcher(line); - - if (!matcher.matches()) { + if (!matcher.matches()) return null; - } int id = Integer.parseInt(matcher.group(elementId)); CommentIcon icon = getCommentIcon(matcher); @@ -443,6 +352,22 @@ public class MdlUtil { return comment; } + public static int getElementX(Matcher matcher) { + return Integer.parseInt(matcher.group(elementX)); + } + + public static int getElementY(Matcher matcher) { + return Integer.parseInt(matcher.group(elementY)); + } + + public static int getElementWidth(Matcher matcher) { + return Integer.parseInt(matcher.group(elementWidth)); + } + + public static int getElementHeight(Matcher matcher) { + return Integer.parseInt(matcher.group(elementHeight)); + } + public static void initializeElement(SketchElement element, Matcher matcher) { int x = Integer.parseInt(matcher.group(elementX)); int y = Integer.parseInt(matcher.group(elementY)); @@ -627,4 +552,131 @@ public class MdlUtil { return result.toString(); } + + + public static void initializeVariable(MdlModel mdl, ModelVariable variable, Variable mdlVariable) { + variable.setName(mdlVariable.getName()); + variable.setExpression(mdlVariable.getModelExpression(mdl)); + + String unit = mdlVariable.getUnit(); + Range range = parseRange(unit); + if (range != null) + unit = unit.substring(0, unit.indexOf('[')).trim(); + variable.setUnit(unit); + variable.setRange(range); + + variable.setDescription(mdlVariable.getDescription()); + } + + private static Range parseRange(String unit) { + Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(\\?|("+NUMBER+")|("+NUMBER+"),("+NUMBER+"))\\]").matcher(unit); + if (matcher.find()) { + Double start, end, step; + start = Double.parseDouble(matcher.group(1)); + if (matcher.group(2).equals("?")) { + end = null; + step = null; + } + else if (matcher.group(2).contains(",")){ + end = Double.parseDouble(matcher.group(4)); + step = Double.parseDouble(matcher.group(5)); + } + else { + end = Double.parseDouble(matcher.group(3)); + step = null; + } + return new Range(start, end, step); + } + else { + return null; + } + } + + public static double[] parseLookup(String lookup) { + // a hack to remove the leading empty string otherwise produced by split() + lookup = lookup.replaceFirst("[(),]+", ""); + String[] parts = lookup.split("[(),]+"); + double[] points = new double[parts.length]; + for (int i = 0; i < parts.length; i++) { + points[i] = Double.parseDouble(parts[i]); + } + return points; + } + + public static Expression parseExpression(String expression) { + Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(expression); + if (!matcher.matches()) + return new NormalExpression(normalize(expression)); + + String function = matcher.group(1); + String[] parameters = splitParameters(matcher.group(2)); + + if (function.startsWith("INTEG")) { + if (parameters.length != 2) { + System.err.println("malformed integral expression: "+expression); + return null; + } + String integral = normalize(parameters[0]); + String initial = normalize(parameters[1]); + return new IntegralExpression(integral, initial); + } + else if (function.startsWith("DELAY N")) { + if (parameters.length != 4) { + System.err.println("malformed delay expression: "+expression); + return null; + } + String expr = normalize(parameters[0]); + String time = normalize(parameters[1]); + String initial = normalize(parameters[2]); + int degree = Integer.parseInt(parameters[3]); + return new DelayExpression(expr, time, initial, degree); + } + else if (function.startsWith("GAME")) { + if (parameters.length != 1) { + System.err.println("malformed game expression: "+expression); + return null; + } + // game expressions are currently treated as normal expressions + return new NormalExpression(normalize(parameters[0])); + } + else { + // the function does not require special handling + return new NormalExpression(normalize(expression)); + } + } + + public static String[] splitParameters(String str) { + ArrayList list = new ArrayList(); + + int i; + int last = 0; + int level = 0; + boolean comment = false; + boolean brackets = false; + + for (i = 0; i < str.length(); i++) { + char current = str.charAt(i); + if (current == '"') + // note that this does not currently support escaped quotation marks inside quoted variable names + comment = !comment; + else if (current == '[' && !comment) + brackets = true; + else if (current == ']' && !comment) + brackets = false; + else if (current == '(' && !comment && !brackets) + level++; + else if (current == ')' && !comment && !brackets) + level--; + else if (current == ',' && !comment && !brackets && level == 0) { + list.add(str.substring(last, i).trim()); + last = i + 1; + } + } + if (last < i) { + list.add(str.substring(last, i).trim()); + } + + return list.toArray(new String[list.size()]); + } + } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java new file mode 100644 index 00000000..441e360d --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/LookupVariable.java @@ -0,0 +1,84 @@ +package org.simantics.sysdyn.modelImport.mdl; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.simantics.sysdyn.modelImport.MdlUtil; +import org.simantics.sysdyn.modelImport.model.expression.LookupExpression; + +public class LookupVariable extends Variable { + + // 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 LOOKUP_VARIABLE = + LOOKUP_NAME+"\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)"; + + public static final String lookupVariablePattern = + LOOKUP_VARIABLE+DELIM+UNIT+DELIM+DESCRIPTION+END; + 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; + + private double xMin; + private double yMin; + private double xMax; + private double yMax; + + public LookupVariable(String name, String expression, String unit, String description, + double xMin, double yMin, double xMax, double yMax) { + super(name, expression, unit, description); + this.xMin = xMin; + this.yMin = yMin; + this.xMax = xMax; + this.yMax = yMax; + } + + public static LookupVariable getPossibleLookUpVariable(String line) { + Matcher matcher = Pattern.compile(lookupVariablePattern).matcher(line); + if (!matcher.matches()) + return null; + + String name = MdlUtil.normalize(matcher.group(lookupVariableName)); + double xMin = Double.parseDouble(matcher.group(lookupVariableRangeXMin)); + double yMin = Double.parseDouble(matcher.group(lookupVariableRangeYMin)); + double xMax = Double.parseDouble(matcher.group(lookupVariableRangeXMax)); + double yMax = Double.parseDouble(matcher.group(lookupVariableRangeYMax)); + String expression = matcher.group(lookupVariablePoints); + String unit = matcher.group(lookupVariableUnit); + String desc = matcher.group(lookupVariableDesc); + + return new LookupVariable(name, expression, unit, desc, xMin, yMin, xMax, yMax); + } + + public double getxMin() { + return xMin; + } + + public double getyMin() { + return yMin; + } + + public double getxMax() { + return xMax; + } + + public double getyMax() { + return yMax; + } + + @Override + public LookupExpression getModelExpression(MdlModel mdl) { + return new LookupExpression(xMin, yMin, xMax, yMax, MdlUtil.parseLookup(getExpression())); + } + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java index 7bf61c52..b86b2a03 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/MdlModel.java @@ -2,10 +2,9 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; - -import org.simantics.sysdyn.modelImport.model.support.Enumeration; -import org.simantics.sysdyn.modelImport.model.support.Variable; +import java.util.Set; public class MdlModel { @@ -13,7 +12,8 @@ public class MdlModel { private HashMap variables; private HashMap> groups; - private HashMap subscripts; + private HashMap subscripts; + private HashMap subscriptValues; private ArrayList sketches; public MdlModel(String name) { @@ -21,7 +21,8 @@ public class MdlModel { this.variables = new HashMap(); this.groups = new HashMap>(); - this.subscripts = new HashMap(); + this.subscripts = new HashMap(); + this.subscriptValues = new HashMap(); this.sketches = new ArrayList(); } @@ -29,10 +30,26 @@ public class MdlModel { return name; } + public void addVariable(SubscriptVariable variable, String group) { + SubscriptVariable original = (SubscriptVariable)variables.get(variable.getName()); + if (original != null) { + original.addLast(variable); + } + else { + variables.put(variable.getName(), variable); + + if (group != null) { + if (groups.get(group) == null) { + groups.put(group, new ArrayList()); + } + groups.get(group).add(variable); + } + } + } + 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); + System.err.println("duplicate variable "+variable.getName()); return; } @@ -50,20 +67,75 @@ public class MdlModel { return variables.get(name); } - public List getVariables() { - return new ArrayList(variables.values()); + public Set getAllVariables() { + return new HashSet(variables.values()); + } + + public void addSubscript(Subscript subscript) { + Subscript duplicate = resolveSubscript(subscript.getValues()); + if (duplicate != null) { + subscripts.put(subscript.getName(), duplicate); + System.err.println("dublicate subscript "+subscript.getName()+" added"); + } + else { + subscripts.put(subscript.getName(), subscript); + System.err.println("subscript "+subscript.getName()+" added"); + } + } + + public Subscript getSubscript(String key) { + return subscripts.get(key); + } + + public Set getAllSubscripts() { + return new HashSet(subscripts.values()); } - public void addSubscript(Enumeration subscript) { - subscripts.put(subscript.getName(), subscript); + private Subscript getSuperSet(Subscript subscript) { + for (Subscript potential : getAllSubscripts()) { + if (potential.isSuperSet() && + potential.getValues().containsAll(subscript.getValues())) { + return potential; + } + } + return null; } - public Enumeration getSubscript(String name) { - return subscripts.get(name); + private Set getSubSets(Subscript subscript) { + Set subsets = new HashSet(); + for (Subscript potential : getAllSubscripts()) { + if (subscript.getValues().size() > potential.getValues().size() && + subscript.getValues().containsAll(potential.getValues())) { + subsets.add(potential); + } + } + return subsets; } - public List getSubscripts() { - return new ArrayList(subscripts.values()); + // return a subscript that contains precisely the requested values + public Subscript resolveSubscript(Set values) { + Set allValues = new HashSet(); + for (String value : values) { + if (subscripts.get(value) != null) { + allValues.addAll(subscripts.get(value).getValues()); + } + else { + allValues.add(value); + } + } + + for (Subscript subscript : getAllSubscripts()) { + if (allValues.equals(subscript.getValues())) { + return subscript; + } + } + + System.err.print("ASDF subscript not found for value set:"); + for (String s : allValues) + System.err.print(" "+s); + System.err.println(); + + return null; } public void addSketch(Sketch sketch) { diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java index 9aaac03c..6e7a8c8e 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchComment.java @@ -28,16 +28,13 @@ public class SketchComment extends SketchElement { } @Override - public Symbol getSymbol(Sketch sketch) { - double[] dimensions = getDimensions(sketch); + public Symbol getSymbol(MdlModel mdl, Sketch sketch) { + if (isInputOutput()) + return new Comment(getDimensions(), "I/O objects are not supported"); - if (isInputOutput()) { - return new Comment(dimensions, "input / output objects are not supported yet"); - } - - switch (icon) { - case CLOUD: return new Cloud(dimensions); - case OTHER: return new Comment(dimensions, text); + switch(icon) { + case CLOUD: return new Cloud(getDimensions()); + case OTHER: return new Comment(getDimensions(), text); default: return null; } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java index bfd34066..8d6e1c1c 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchConnection.java @@ -1,5 +1,9 @@ package org.simantics.sysdyn.modelImport.mdl; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.simantics.sysdyn.modelImport.MdlUtil; import org.simantics.sysdyn.modelImport.MdlUtil.ConnectionType; import org.simantics.sysdyn.modelImport.model.element.Connection; import org.simantics.sysdyn.modelImport.model.element.Dependency; diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java index 3ab316ff..c1f3486c 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java @@ -18,6 +18,21 @@ public abstract class SketchElement extends SketchObject { super(id); } + public SketchElement(int id, int x, int y, int width, int height, + boolean attached, boolean allowsIn, boolean allowsOut, + boolean inputOutput, boolean textLine) { + super(id); + 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 void init(int x, int y, int width, int height, boolean attached, boolean allowsIn, boolean allowsOut, boolean inputOutput, boolean textLine) { @@ -105,7 +120,7 @@ public abstract class SketchElement extends SketchObject { return textLine; } - public abstract Symbol getSymbol(Sketch sketch); + public abstract Symbol getSymbol(MdlModel mdl, Sketch sketch); private static final double SCALE_MULTIPLIER = 0.4; diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java index c393d1ba..fb4c6135 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java @@ -1,8 +1,11 @@ package org.simantics.sysdyn.modelImport.mdl; +import org.simantics.sysdyn.modelImport.MdlUtil; import org.simantics.sysdyn.modelImport.model.IWriteableObject; +import org.simantics.sysdyn.modelImport.model.element.ModelVariable; import org.simantics.sysdyn.modelImport.model.element.Symbol; import org.simantics.sysdyn.modelImport.model.element.Valve; +import org.simantics.sysdyn.modelImport.model.element.Valve.Orientation; import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition; public class SketchValve extends SketchElement { @@ -28,15 +31,13 @@ public class SketchValve extends SketchElement { } @Override - public Symbol getSymbol(Sketch sketch) { - return new Valve(getDimensions(sketch), variable.getVariable(), textPosition); - } - - // TODO: do we really want to do this? - @Override - public void setModelObject(IWriteableObject modelObject) { - super.setModelObject(modelObject); - variable.setModelObject(modelObject); + public Valve getSymbol(MdlModel mdl, Sketch sketch) { + Valve valve = new Valve(Orientation.HORIZONTAL, textPosition); + + variable.getVariable().initializeModelVariable(valve, mdl); + valve.setDimensions(getDimensions(sketch)); + + return valve; } } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java index 9cef8184..b73afe76 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchVariable.java @@ -1,10 +1,11 @@ package org.simantics.sysdyn.modelImport.mdl; +import org.simantics.sysdyn.modelImport.MdlUtil; import org.simantics.sysdyn.modelImport.model.element.Auxiliary; +import org.simantics.sysdyn.modelImport.model.element.ModelVariable; import org.simantics.sysdyn.modelImport.model.element.Symbol; import org.simantics.sysdyn.modelImport.model.element.Stock; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; -import org.simantics.sysdyn.modelImport.model.support.Variable; public class SketchVariable extends SketchElement { @@ -20,13 +21,18 @@ public class SketchVariable extends SketchElement { } @Override - public Symbol getSymbol(Sketch sketch) { - if (variable.getExpression() instanceof IntegralExpression) { - return new Stock(getDimensions(sketch), variable); - } - else { - return new Auxiliary(getDimensions(sketch), variable); - } + public Symbol getSymbol(MdlModel mdl, Sketch sketch) { + ModelVariable var; + + if (variable.getModelExpression(mdl) instanceof IntegralExpression) + var = new Stock(); + else + var = new Auxiliary(); + + variable.initializeModelVariable(var, mdl); + var.setDimensions(getDimensions(sketch)); + + return var; } } \ No newline at end of file diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java new file mode 100644 index 00000000..587d9428 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java @@ -0,0 +1,96 @@ +package org.simantics.sysdyn.modelImport.mdl; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.simantics.sysdyn.modelImport.MdlUtil; +import org.simantics.sysdyn.modelImport.model.support.Enumeration; + +public class Subscript { + + // tODO: might need some rework + private static final String subscriptPattern = + "([A-Za-z]\\w*)\\s*(:|<->)\\s*([^~]*?)\\s*~\\s*~\\s*\\|"; + private static final int subscriptName = 1; + private static final int subscriptType = 2; + private static final int subscriptExpression = 3; + + private String name; + private Set values; + + private Subscript superSet; + + private Enumeration enumeration; + + public Subscript(String name, Setvalues) { + this.name = name; + this.values = values; + } + + public static Subscript getPossibleSubscript(String line, MdlModel mdl) { + Matcher matcher = Pattern.compile(subscriptPattern).matcher(line); + if (!matcher.matches()) + return null; + + String name = MdlUtil.normalize(matcher.group(subscriptName)); + boolean equivalent = matcher.group(subscriptType).equals("<->"); + String expression = MdlUtil.normalize(matcher.group(subscriptExpression)); + + if (equivalent) { + Subscript parent = mdl.getSubscript(expression); + if (parent == null) { + System.err.println("equivalent subscript not found for "+name); + return null; + } + return new Subscript(name, parent.getValues()); + } + else { + return new Subscript(name, new HashSet(Arrays.asList(expression.split(",")))); + } + } + + public String getName() { + return name; + } + + public Set getValues() { + return values; + } + + public Subscript getSuperSet() { + return superSet; + } + + public void setSuperSet(Subscript superSet) { + this.superSet = superSet; + } + + public boolean isSuperSet() { + return superSet == null; + } + + public String getRepresentation() { + StringBuilder buffer = new StringBuilder(); + buffer.append('{'); + for (String value : values) { + if (buffer.length() > 1) { + buffer.append(','); + } + buffer.append(value); + } + buffer.append('}'); + return buffer.toString(); + } + + public void setEnumeration(Enumeration enumeration) { + this.enumeration = enumeration; + } + + public Enumeration getEnumeration() { + return enumeration != null ? enumeration : superSet.getEnumeration(); + } + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java new file mode 100644 index 00000000..78b18675 --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SubscriptVariable.java @@ -0,0 +1,127 @@ +package org.simantics.sysdyn.modelImport.mdl; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.simantics.sysdyn.modelImport.MdlUtil; +import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression; +import org.simantics.sysdyn.modelImport.model.expression.Expression; +import org.simantics.sysdyn.modelImport.model.support.Enumeration; + +public class SubscriptVariable extends Variable { + + // a subscript variable is a variable that has a different equation + // depending on the value of the subscript it references + private static final String SUBSCRIPT_VARIABLE = + "("+VARIABLE_SIMPLE+")\\[("+VARIABLE_SIMPLE+"(?:,"+VARIABLE_SIMPLE+")*)\\]"; + + public static final String subscriptVariablePattern = + SUBSCRIPT_VARIABLE+EXPRESSION+DELIM+UNIT+DELIM+DESCRIPTION+END; + private static final int subscriptVariableName = 1; + private static final int subscriptVariableIndices = 2; + private static final int subscriptVariableExpression = 3; + private static final int subscriptVariableUnit = 4; + private static final int subscriptVariableDesc = 5; + + private String[] subscript; + private SubscriptVariable next; + + public SubscriptVariable(String name, String expression, String unit, String description, String[] subscript) { + super(name, expression, unit, description); + this.subscript = subscript; + this.next = null; + } + + public static SubscriptVariable getPossibleSubscriptVariable(String line) { + Matcher matcher = Pattern.compile(subscriptVariablePattern).matcher(line); + if (!matcher.matches()) + return null; + + String name = MdlUtil.normalize(matcher.group(subscriptVariableName)); + String[] subscript = MdlUtil.normalize(matcher.group(subscriptVariableIndices)).split(","); + String expression = MdlUtil.normalize(matcher.group(subscriptVariableExpression)); + String unit = matcher.group(subscriptVariableUnit); + String desc = matcher.group(subscriptVariableDesc); + + return new SubscriptVariable(name, expression, unit, desc, subscript); + } + + public String[] getSubscript() { + return subscript; + } + + public SubscriptVariable getNext() { + return next; + } + + public void setNext(SubscriptVariable next) { + this.next = next; + } + + public void addLast(SubscriptVariable variable) { + // find the last variable in the list and add the new variable after it + SubscriptVariable last = this; + while (last.getNext() != null) + last = last.getNext(); + last.setNext(variable); + } + + @Override + public EnumerationExpression getModelExpression(MdlModel mdl) { + SubscriptVariable var; + // combine the list of subscript expressions into an enumeration + // expression + + List> indices = new ArrayList>(); + for (int i = 0; i < subscript.length; i++) { + indices.add(new HashSet()); + } + + var = this; + while (var != null) { + for (int i = 0; i < var.getSubscript().length; i++) { + indices.get(i).add(var.getSubscript()[i]); + } + + var = var.getNext(); + } + + List enumerations = new ArrayList(); + for (Set values : indices) { + if (mdl.resolveSubscript(values) == null) { + System.err.print("subscript not found for value set:"); + for (String s : values) + System.err.print(" "+s); + System.err.println(); + } + enumerations.add(mdl.resolveSubscript(values).getEnumeration()); + } + + EnumerationExpression expr = new EnumerationExpression(enumerations); + + var = this; + while (var != null) { + String[] array = new String[var.getSubscript().length]; + for (int i = 0; i < var.getSubscript().length; i++) { + String value = var.getSubscript()[i]; + Subscript script = mdl.getSubscript(value); + if (script == null || script.getValues().equals(enumerations.get(i).getValues())) { + array[i] = value; + } + else { + array[i] = script.getRepresentation(); + } + } + expr.addExpression(super.getModelExpression(mdl), array); + + var = var.getNext(); + } + + return expr; + } + +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java new file mode 100644 index 00000000..bbfc849c --- /dev/null +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java @@ -0,0 +1,177 @@ +package org.simantics.sysdyn.modelImport.mdl; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.simantics.sysdyn.modelImport.MdlUtil; +import org.simantics.sysdyn.modelImport.model.element.ModelVariable; +import org.simantics.sysdyn.modelImport.model.expression.DelayExpression; +import org.simantics.sysdyn.modelImport.model.expression.Expression; +import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; +import org.simantics.sysdyn.modelImport.model.expression.NormalExpression; +import org.simantics.sysdyn.modelImport.model.support.Range; + +public class Variable { + + protected static final String NUMBER = "-?\\d+(?:\\.\\d+)?"; + protected static final String DELIM = "\\s*~\\s*"; + protected static final String END = "\\s*\\|"; + + // a Vensim variable name is either a plain string that contains letters, + // numbers and whitespace, or a quoted string that can also contain any + // special characters (including other quotation marks that must be escaped) + protected static final String VARIABLE_QUOTED = + "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\""; + protected static final String VARIABLE_SIMPLE = + "[A-Za-z](?![ \\w]*\\()(?: *\\w+)*"; + + protected static final String NORMAL_VARIABLE = + "("+VARIABLE_QUOTED+"|"+VARIABLE_SIMPLE+")"; + // the expression does not necessarily exist + protected static final String EXPRESSION = + "(?:\\s*=\\s*([^~]*?(?:"+VARIABLE_QUOTED+"[^~]*?)*))?"; + protected static final String UNIT = + "([^~]*?)"; + protected static final String DESCRIPTION = + "([^\\|]*?)"; + + private static final String variablePattern = + NORMAL_VARIABLE+EXPRESSION+DELIM+UNIT+DELIM+DESCRIPTION+END; + private static final int variableName = 1; + private static final int variableExpression = 2; + private static final int variableUnit = 3; + private static final int variableDesc = 4; + + private String name; + private String expression; + private String unit; + private String description; + + public Variable(String name, String expression, String unit, String description) { + this.name = name; + this.expression = expression; + this.unit = unit; + this.description = description; + } + + public static Variable getPossibleVariable(String line) { + Matcher matcher = Pattern.compile(variablePattern).matcher(line); + if (!matcher.matches()) + return null; + + String name = MdlUtil.normalize(matcher.group(variableName)); + String expression = matcher.group(variableExpression); + if (expression != null) { + expression = MdlUtil.normalize(expression); + } + String unit = matcher.group(variableUnit); + String desc = matcher.group(variableDesc); + + return new Variable(name, expression, unit, desc); + } + + public String getName() { + return name; + } + + public String getExpression() { + return expression; + } + + public String getUnit() { + return unit; + } + + public String getDescription() { + return description; + } + + public void initializeModelVariable(ModelVariable variable, MdlModel mdl) { + variable.setName(getName()); + variable.setExpression(getModelExpression(mdl)); + + String unit = getUnit(); + Range range = parseRange(unit); + if (range != null) + unit = unit.substring(0, unit.indexOf('[')).trim(); + variable.setUnit(unit); + variable.setRange(range); + + variable.setDescription(getDescription()); + } + + public Expression getModelExpression(MdlModel mdl) { + if (expression == null) { + // if the expression has not been defined, return an empty expression + return new NormalExpression(""); + } + + // the type of the returned expression object depends on the expression + Matcher matcher = Pattern.compile("([A-Za-z](?: *\\w+)*)\\s*\\((.*)\\)").matcher(expression); + if (!matcher.matches()) { + // the expression is not one of the special function calls + return new NormalExpression(MdlUtil.normalize(expression)); + } + + String function = matcher.group(1); + String[] parameters = MdlUtil.splitParameters(matcher.group(2)); + + if (function.startsWith("INTEG")) { + // an integral expression + if (parameters.length != 2) { + System.err.println("malformed integral expression: "+expression); + return null; + } + String integral = MdlUtil.normalize(parameters[0]); + String initial = MdlUtil.normalize(parameters[1]); + return new IntegralExpression(integral, initial); + } + else if (function.startsWith("DELAY N")) { + // a delay expression + if (parameters.length != 4) { + System.err.println("malformed delay expression: "+expression); + return null; + } + String expr = MdlUtil.normalize(parameters[0]); + String time = MdlUtil.normalize(parameters[1]); + String initial = MdlUtil.normalize(parameters[2]); + int degree = Integer.parseInt(parameters[3]); + return new DelayExpression(expr, time, initial, degree); + } + else if (function.startsWith("GAME")) { + // a game expression, currently treated as a normal expression + if (parameters.length != 1) { + System.err.println("malformed game expression: "+expression); + return null; + } + return new NormalExpression(MdlUtil.normalize(parameters[0])); + } + else { + return new NormalExpression(MdlUtil.normalize(expression)); + } + } + + private static Range parseRange(String unit) { + Matcher matcher = Pattern.compile("\\[("+NUMBER+"),(\\?|("+NUMBER+")|("+NUMBER+"),("+NUMBER+"))\\]").matcher(unit); + if (matcher.find()) { + Double start, end, step; + start = Double.parseDouble(matcher.group(1)); + if (matcher.group(2).equals("?")) { + end = null; + step = null; + } + else if (matcher.group(2).contains(",")){ + end = Double.parseDouble(matcher.group(4)); + step = Double.parseDouble(matcher.group(5)); + } + else { + end = Double.parseDouble(matcher.group(3)); + step = null; + } + return new Range(start, end, step); + } + else { + return null; + } + } +} diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java index 11ddf9d3..e30545b6 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java @@ -5,21 +5,15 @@ import org.simantics.db.Resource; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; -import org.simantics.sysdyn.modelImport.model.support.Variable; public class Auxiliary extends ModelVariable { - public Auxiliary(double x, double y, double w, double h, - String name, Expression expression, Range range, String unit, String description) { - super(x, y, w, h, name, expression, range, unit, description); + public Auxiliary() { + } - 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); + public Auxiliary(double[] dim, String name, Expression expression, Range range, String unit, String description) { + super(dim, name, expression, range, unit, description); } @Override diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java index 10050518..9687c533 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java @@ -14,12 +14,12 @@ public class Cloud extends Symbol { private Resource cloud; - public Cloud(double x, double y, double w, double h) { - super(x, y, w, h); + public Cloud() { + } - public Cloud(double[] dimensions) { - super(dimensions); + public Cloud(double[] dim) { + super(dim); } @Override diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java index 2c724553..b632ca1d 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java @@ -13,13 +13,24 @@ public class Comment extends Symbol { private String text; - public Comment(double x, double y, double w, double h, String text) { - super(x, y, w, h); + public Comment() { + + } + + public Comment(String text) { this.text = text; } - public Comment(double[] dimensions, String text) { - super(dimensions); + public Comment(double[] dim, String text) { + super(dim); + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { this.text = text; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java index 967bd04b..dda8b59a 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java @@ -16,17 +16,37 @@ import org.simantics.sysdyn.modelImport.model.WriteContext; public abstract class Connection implements IWriteableObject { - protected Symbol head; - protected Symbol tail; + private Symbol head; + private Symbol tail; private Resource connection; private Resource diagramConnection; + + public Connection() { + + } public Connection(Symbol tail, Symbol head) { this.tail = tail; this.head = head; } + public Symbol getHead() { + return head; + } + + public void setHead(Symbol head) { + this.head = head; + } + + public Symbol getTail() { + return tail; + } + + public void setTail(Symbol tail) { + this.tail = tail; + } + @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { DiagramResource dr = DiagramResource.getInstance(graph); diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java index 8da35e27..a2c486bb 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java @@ -15,6 +15,16 @@ public class Dependency extends Connection { private boolean showDelay; private double angle; + public Dependency() { + + } + + public Dependency(boolean showArrow, boolean showDelay, double angle) { + this.showArrow = showArrow; + this.showDelay = showDelay; + this.angle = angle; + } + public Dependency(Symbol tail, Symbol head, boolean showArrow, boolean showDelay, double angle) { super(tail, head); this.showArrow = showArrow; @@ -22,9 +32,33 @@ public class Dependency extends Connection { this.angle = angle; } + public boolean isShowArrow() { + return showArrow; + } + + public void setShowArrow(boolean showArrow) { + this.showArrow = showArrow; + } + + public boolean isShowDelay() { + return showDelay; + } + + public void setShowDelay(boolean showDelay) { + this.showDelay = showDelay; + } + + public double getAngle() { + return angle; + } + + public void setAngle(double angle) { + this.angle = angle; + } + @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - if (head == null || tail == null) { + if (getHead() == null || getTail() == null) { System.err.println("dependency missing head or tail"); return null; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java index 747819da..9e3b28d5 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java @@ -11,13 +11,17 @@ import org.simantics.sysdyn.modelImport.model.WriteContext; public class Flow extends Connection { + public Flow() { + + } + public Flow(Symbol tail, Symbol head) { super(tail, head); } @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - if (head == null || tail == null) { + if (getHead() == null || getTail() == null) { System.err.println("flow missing head or tail"); return null; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java index c297535f..d5e96f00 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java @@ -12,7 +12,6 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; -import org.simantics.sysdyn.modelImport.model.support.Variable; public abstract class ModelVariable extends Symbol { @@ -23,39 +22,68 @@ public abstract class ModelVariable extends Symbol { private String description; private Resource variable; - - public ModelVariable(double x, double y, double w, double h, - String name, Expression expression, Range range, String unit, String description) { - super(x, y, w, h); + + public ModelVariable() { + + } + + public ModelVariable(String name, Expression expression, Range range, String unit, String description) { this.name = name; this.expression = expression; this.range = range; this.unit = unit; 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(); - this.expression = variable.getExpression(); - this.range = variable.getRange(); - this.unit = variable.getUnit(); - this.description = variable.getDescription(); + + public ModelVariable(double[] dim, String name, Expression expression, Range range, String unit, String description) { + super(dim); + this.name = name; + this.expression = expression; + this.range = range; + this.unit = unit; + this.description = description; } - + public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + + public Expression getExpression() { + return expression; + } + + public void setExpression(Expression expression) { + this.expression = expression; + } + + public Range getRange() { + return range; + } + + public void setRange(Range range) { + this.range = 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; + } + @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java index 0ad1822e..716c2aa7 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java @@ -8,7 +8,6 @@ import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.mdl.SketchVariable; import org.simantics.sysdyn.modelImport.model.WriteContext; public class Shadow extends Symbol { @@ -17,18 +16,24 @@ public class Shadow extends Symbol { private Resource shadow; - public Shadow(double x, double y, double w, double h, ModelVariable original) { - super(x, y, w, h); - this.original = original; + public Shadow() { + } - public Shadow(double[] dimensions, ModelVariable original) { - super(dimensions); + public Shadow(ModelVariable original) { this.original = original; } - public Shadow(SketchVariable variable, double hOffset, double vOffset, ModelVariable original) { - super(variable, hOffset, vOffset); + public Shadow(double[] dim, ModelVariable original) { + super(dim); + this.original = original; + } + + public ModelVariable getOriginal() { + return original; + } + + public void setOriginal(ModelVariable original) { this.original = original; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java index 0272567f..9c4b11c2 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java @@ -5,21 +5,15 @@ import org.simantics.db.Resource; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; import org.simantics.sysdyn.modelImport.model.support.Range; -import org.simantics.sysdyn.modelImport.model.support.Variable; public class Stock extends ModelVariable { - public Stock(double x, double y, double w, double h, - String name, IntegralExpression expression, Range range, String unit, String description) { - super(x, y, w, h, name, expression, range, unit, description); + public Stock() { + } - public Stock(double x, double y, double w, double h, Variable variable) { - super(x, y, w, h, variable); - } - - public Stock(double[] dimensions, Variable variable) { - super(dimensions, variable); + public Stock(double[] dim, String name, IntegralExpression expression, Range range, String unit, String description) { + super(dim, name, expression, range, unit, description); } @Override diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java index 70fc0709..e4f23be6 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java @@ -11,7 +11,6 @@ import org.simantics.diagram.stubs.G2DResource; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; -import org.simantics.sysdyn.modelImport.mdl.SketchElement; import org.simantics.sysdyn.modelImport.model.IWriteableObject; import org.simantics.sysdyn.modelImport.model.WriteContext; @@ -23,44 +22,57 @@ public abstract class Symbol implements IWriteableObject { private double height; private Resource symbol; - - public Symbol(double x, double y, double width, double height) { - this.x = x; - this.y = y; - this.width = width; - 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() { + } - - public Symbol(SketchElement element, double hOffset, double vOffset) { - this.x = element.getSysdyndX() + hOffset; - this.y = element.getSysdyndY() + vOffset; - this.width = element.getSysdynWidth(); - this.height = element.getSysdynHeight(); + + public Symbol(double[] dim) { + x = dim[0]; + y = dim[1]; + width = dim[2]; + height = dim[3]; } + public void setDimensions(double[] dim) { + x = dim[0]; + y = dim[1]; + width = dim[2]; + height = dim[3]; + } + public double getX() { return x; } - + + public void setX(double x) { + this.x = x; + } + public double getY() { return y; } - + + public void setY(double y) { + this.y = y; + } + public double getWidth() { return width; } - + + public void setWidth(double width) { + this.width = width; + } + public double getHeight() { return height; } + public void setHeight(double height) { + this.height = height; + } + @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { DiagramResource dr = DiagramResource.getInstance(graph); diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java index 72cd9124..f9451118 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java @@ -8,7 +8,6 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; -import org.simantics.sysdyn.modelImport.model.support.Variable; public class Valve extends ModelVariable { @@ -20,21 +19,38 @@ public class Valve extends ModelVariable { INSIDE, BELOW, LEFT, ABOVE, RIGHT, UNSET } + private Orientation orientation; private TextPosition position; - public Valve(double x, double y, double w, double h, - String name, Expression expression, Range range, String unit, String description, TextPosition position) { - super(x, y, w, h, name, expression, range, unit, description); - this.position = position; + public Valve() { + } - public Valve(double[] dimensions, Variable variable, TextPosition position) { - super(dimensions, variable); + public Valve(Orientation orientation, TextPosition position) { + this.orientation = orientation; this.position = position; } - public Valve(double x, double y, double w, double h, Variable variable, TextPosition position) { - super(x, y, w, h, variable); + public Valve(double[] dim, String name, Expression expression, Range range, String unit, String description, + Orientation orientation, TextPosition position) { + super(dim, name, expression, range, unit, description); + this.orientation = orientation; + this.position = position; + } + + public Orientation getOrientation() { + return orientation; + } + + public void setOrientation(Orientation orientation) { + this.orientation = orientation; + } + + public TextPosition getPosition() { + return position; + } + + public void setPosition(TextPosition position) { this.position = position; } @@ -44,22 +60,12 @@ public class Valve extends ModelVariable { Resource location = null; switch (position) { - case BELOW: - location = sr.Bottom; - break; - case LEFT: - location = sr.Left; - break; - case ABOVE: - location = sr.Top; - break; - case RIGHT: - location = sr.Right; - break; - default: - System.err.println("unrecognized text location"); - location = sr.Bottom; - break; + case BELOW: location = sr.Bottom; break; + case LEFT: location = sr.Left; break; + case ABOVE: location = sr.Top; break; + case RIGHT: location = sr.Right; break; + case UNSET: + default: System.err.println("valve text location not set"); location = sr.Bottom; break; } Resource valve = super.write(graph, parent, context); diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java index a39be3c3..4b08a671 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java @@ -13,24 +13,40 @@ import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; +import org.simantics.sysdyn.modelImport.model.support.Enumeration; public class EnumerationExpression extends Expression { + private List enumerations; private List subExpressions; - public EnumerationExpression(Expression expression, String...indices) { - subExpressions = new ArrayList(); - addExpression(expression, indices); + public EnumerationExpression(List enumerations) { + this.enumerations = enumerations; + this.subExpressions = new ArrayList(); + + System.err.print("create enumeration expression for "); + for (Enumeration e : enumerations) { + System.err.print(e.getName()+" "); + } + System.err.println(); } public void addExpression(Expression expression, String...indices) { + System.err.print(" add expression for indices "); + for (String s : indices) { + System.err.print(s+" "); + } + System.err.println(); + +// for (int i = 0; i < indices.length; i++) { +// if (!enumerations.get(i).getValues().contains(indices)) { +// System.err.println("tried adding an enumeration expression with incorrect indices"); +// return; +// } +// } subExpressions.add(new SubscriptSubExpression(expression, indices)); } - public void addExpressions(List expressions) { - subExpressions.addAll(expressions); - } - public List getExpressions() { return subExpressions; } @@ -39,11 +55,6 @@ public class EnumerationExpression extends Expression { public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - ArrayList> enumerationIndices = new ArrayList>(); - for (int i = 0; i < subExpressions.get(0).indices.length; i++) { - enumerationIndices.add(new HashSet()); - } - for (SubscriptSubExpression subexpr : subExpressions) { StringBuilder range = new StringBuilder(); range.append('['); @@ -51,8 +62,6 @@ public class EnumerationExpression extends Expression { if (i > 0) range.append(','); range.append(subexpr.indices[i]); - - enumerationIndices.get(i).add(subexpr.indices[i]); } range.append(']'); @@ -61,13 +70,13 @@ public class EnumerationExpression extends Expression { graph.claimLiteral(res, sr.Expression_arrayRange, range.toString(), Bindings.STRING); } - ArrayList enumerations = new ArrayList(); - for (Set set : enumerationIndices) { - enumerations.add(context.getEnumeration(set).getResource()); + ArrayList indexlist = new ArrayList(); + for (Enumeration enumeration : enumerations) { + indexlist.add(enumeration.getResource()); } // write array index list - graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, enumerations)); + graph.claim(parent, sr.Variable_arrayIndexesList, ListUtils.create(graph, indexlist)); // set active expression? diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java index de793edb..8b571f81 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Enumeration.java @@ -1,6 +1,5 @@ package org.simantics.sysdyn.modelImport.model.support; -import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -18,22 +17,12 @@ public class Enumeration implements IWriteableObject { private String name; private Set values; - private boolean copy; private Resource enumeration; - public Enumeration(String name, String...values) { + public Enumeration(String name, Set values) { this.name = name; - this.values = new HashSet(Arrays.asList(values)); - - copy = false; - } - - public Enumeration(String name, Enumeration other) { - this.name = name; - this.values = other.getValues(); - - copy = true; + this.values = values; } public String getName() { @@ -62,9 +51,6 @@ public class Enumeration implements IWriteableObject { graph.claim(enumeration, sr.Enumeration_enumerationIndexList, ListUtils.create(graph, indices)); - // register this enumeration with the context - context.registerEnumeration(this, copy); - return enumeration; } diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java index 03ecc6d5..e28c6b68 100644 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java +++ b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java @@ -11,15 +11,13 @@ public class Range implements IWriteableObject { private Double start; private Double end; private Double step; - private String original; private Resource resource; - public Range(Double start, Double end, Double step, String original) { + public Range(Double start, Double end, Double step) { this.start = start; this.end = end; this.step = step; - this.original = original; } public Double getStart() { @@ -34,10 +32,6 @@ public class Range implements IWriteableObject { return step; } - public String originalString() { - return original; - } - @Override public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException { return null; diff --git a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java b/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java deleted file mode 100644 index aa08fb80..00000000 --- a/dev-jkauttio/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.simantics.sysdyn.modelImport.model.support; - -import org.simantics.sysdyn.modelImport.model.expression.Expression; -import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression; - -public class Variable { - - private String name; - private Expression expression; - private String unit; - private Range range; - private String description; - - public Variable(String name, Expression expression, String unit, Range range, String description) { - this.name = name; - this.expression = expression; - this.unit = unit; - this.range = range; - this.description = description; - } - - public String getName() { - return name; - } - - public Expression getExpression() { - return expression; - } - - public Range getRange() { - return range; - } - - public String getUnit() { - return unit; - } - - public String getDescription() { - return description; - } - - public void mergeWithVariable(Variable other) { - if (!name.equals(other.getName())) { - System.err.println("attempted to merge variable "+name+" with variable "+other.getName()); - return; - } - if (expression instanceof EnumerationExpression && other.getExpression() instanceof EnumerationExpression) { - ((EnumerationExpression)expression).addExpressions(((EnumerationExpression)other.getExpression()).getExpressions()); - } - if (range == null) { - range = other.getRange(); - } - if (unit == null || unit.isEmpty()) { - unit = other.getUnit(); - } - if (description == null || description.isEmpty()) { - description = other.getDescription(); - } - } - -} -- 2.47.1