From: jkauttio Date: Wed, 2 Apr 2014 15:39:17 +0000 (+0000) Subject: Update vensim import with support for most equation types, subscript support still... X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=55e1db0922d504514084f9f7ee6c0cdd509e224e;p=simantics%2Fsysdyn.git Update vensim import with support for most equation types, subscript support still incomplete refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/dev-jkauttio@29237 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 e4b6e4ef..82e8e07e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java @@ -19,7 +19,7 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; -import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon; +import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon; import org.simantics.sysdyn.modelImport.mdl.MdlModel; import org.simantics.sysdyn.modelImport.mdl.Sketch; import org.simantics.sysdyn.modelImport.mdl.SketchComment; @@ -28,17 +28,19 @@ 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.Subscript2; +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; import org.simantics.sysdyn.modelImport.model.element.Comment; import org.simantics.sysdyn.modelImport.model.element.Connection; -import org.simantics.sysdyn.modelImport.model.element.Element; +import org.simantics.sysdyn.modelImport.model.element.ModelVariable; +import org.simantics.sysdyn.modelImport.model.element.Symbol; import org.simantics.sysdyn.modelImport.model.element.Shadow; 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.Variable; public class MdlParser { @@ -68,19 +70,14 @@ public class MdlParser { } // generate a sysdyn model from the mdl model - Model model = new Model(file.getName()); + Model model = new Model(mdl.getName()); double offset = 0; // do this in several passes - HashMap variableToElement = new HashMap(); - ArrayList shadows = new ArrayList(); - - for (Subscript2 ss : mdl.getSubscripts()) { - System.err.println("SUBSCRIPT "+ss.getName()+": "+ss.getValues()); - } - + HashMap variableToElement = new HashMap(); + // add sketch labels and independent elements for (Sketch sketch : mdl.getSketches()) { sketch.setEdges(); @@ -97,13 +94,13 @@ public class MdlParser { continue; } - Element modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset); + Symbol modelElement = comment.getModelElement(sketch.hOffset, sketch.vOffset); model.addElement(modelElement); sketch.elements.put(comment.getId(), modelElement); } for (SketchValve valve : sketch.getValves()) { - Element modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset); + Symbol modelElement = valve.getModelElement(sketch.hOffset, sketch.vOffset); model.addElement(modelElement); sketch.elements.put(valve.getId(), modelElement); sketch.elements.put(valve.getAttachedVariable().getId(), modelElement); @@ -121,7 +118,7 @@ public class MdlParser { continue; } - Element modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset); + Symbol modelElement = variable.getModelElement(sketch.hOffset, sketch.vOffset); model.addElement(modelElement); sketch.elements.put(variable.getId(), modelElement); variableToElement.put(variable.getVariable(), modelElement); @@ -135,25 +132,30 @@ public class MdlParser { for (SketchVariable variable : sketch.getVariables()) { if (!variable.allowsIn()) { // the variable is a shadow variable - Element original = variableToElement.get(variable.getVariable()); + Symbol original = variableToElement.get(variable.getVariable()); if (original == null) { - System.err.println("original not found"); - continue; + 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); } - Shadow modelElement = new Shadow( - variable.getSysdyndX() + sketch.hOffset, - variable.getSysdyndY() + sketch.vOffset, - variable.getSysdynWidth(), - variable.getSysdynHeight(), - variableToElement.get(variable.getVariable())); - model.addShadow(modelElement); - sketch.elements.put(variable.getId(), modelElement); } } for (SketchConnection connection : sketch.getConnections()) { - Element head = sketch.elements.get(connection.getTo()); - Element tail = sketch.elements.get(connection.getFrom()); + 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); @@ -168,6 +170,9 @@ public class MdlParser { throws IOException { 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(); @@ -186,114 +191,81 @@ public class MdlParser { line = reader.readLine(); } - // read variable data - line = parseVariables(reader, line, mdl); - - if (line == null) { - System.err.println("unexpected end of file"); - reader.close(); - return null; - } - - // read sketch data - line = parseSketches(reader, line, mdl); - - if (line == null) { - System.err.println("unexpected end of file"); - reader.close(); - return null; - } - - // read other data - do { - // TODO: is there anything relevant here? - } while ((line = reader.readLine()) != null); + // START READING VARIABLE DATA - reader.close(); - return mdl; - } - - private static String parseVariables(BufferedReader reader, String line, MdlModel mdl) - throws IOException { + // keep track of the current category String category = null; do { - if (line.isEmpty()) + // skip empty lines + if (line.isEmpty()) continue; + // combine the (possible) variable declaration into one line StringBuilder buffer = new StringBuilder(); - do { - if (line.endsWith("\\")) - buffer.append(line.substring(0, line.length()-1)); - else - buffer.append(line); - + // strip trailing '\' if present + buffer.append(line.endsWith("\\") ? line.substring(0, line.length()-1) : line); + // each variable declaration ends with '|' if (line.endsWith("|")) break; } while ((line = reader.readLine()) != null); - String str = buffer.toString(); - String cat = MdlUtils.getPossibleCategory(str); - if (cat != null) { + String cat; + Variable var; + Subscript sub; + + // parse the (possible) variable declaration + if ((cat = MdlUtil.getPossibleCategory(str)) != null) { category = cat; - continue; } - - Variable var = MdlUtils.getPossibleVariable(str, category); - if (var != null) { + else if ((var = MdlUtil.getPossibleVariable(str, category)) != null) { mdl.addVariable(var, category); - continue; } - - Variable ss = MdlUtils.getPossibleSubscriptVariable(str, category, mdl); - if (ss != null) { - Variable original = mdl.getVariable(ss.getName()); - if (original == null) { - mdl.addVariable(ss, category); - } - else { - original.mergeExpression(ss); - } - continue; + 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); } - - Variable lu = MdlUtils.getPossibleLookUpVariable(str, category); - if (lu != null) { - System.err.println("found lookup variable "+lu.getName()); - continue; + else if ((var = MdlUtil.getPossibleLookUpVariable(str, category)) != null) { + mdl.addVariable(var, category); } - - Variable plain = MdlUtils.getPossibleNoExpressionVariable(str); - if (plain != null) { - System.err.println("found plain variable "+plain.getName()); - continue; + else if ((var = MdlUtil.getPossibleNoExpressionVariable(str)) != null) { + mdl.addVariable(var, category); } - - Subscript2 subscript = MdlUtils.getPossibleSubscript(str, mdl); - if (subscript != null) { - mdl.addSubscript(subscript); - continue; + else if ((sub = MdlUtil.getPossibleSubscript(str, mdl)) != null) { + mdl.addSubscript(sub); + } + else { + // if we got this far, the variable could not be parsed + System.err.println("unrecognized variable "+str); } - // if we got this far, the variable could not be parsed - System.err.println("unrecognized variable "+str); } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_START)); - return line; - } + // END READING VARIABLE DATA - private static String parseSketches(BufferedReader reader, String line, MdlModel mdl) - throws IOException { + if (line == null) { + System.err.println("unexpected end of file"); + reader.close(); + return null; + } + + // START READING SKETCH DATA + + // keep track of the current sketch Sketch sketch = null; do { + // skip empty lines if (line.isEmpty()) continue; - + SketchObject so; - + if (line.startsWith(SKETCH_START)) { sketch = new Sketch(); mdl.addSketch(sketch); @@ -307,16 +279,17 @@ public class MdlParser { else if (line.startsWith("$")) { // font declaration, nothing to do here } - else if ((so = MdlUtils.getPossibleSketchConnection(line)) != null) { + else if ((so = MdlUtil.getPossibleSketchConnection(line)) != null) { sketch.addConnection((SketchConnection)so); } - else if ((so = MdlUtils.getPossibleSketchVariable(line, mdl)) != null) { - sketch.addVariable((SketchVariable)so); + else if ((so = MdlUtil.getPossibleSketchVariable(line, mdl)) != null) { + SketchVariable variable = (SketchVariable)so; + sketch.addVariable(variable); } - else if ((so = MdlUtils.getPossibleSketchValve(line)) != null) { + 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 = MdlUtils.getPossibleSketchVariable(reader.readLine(), mdl); + SketchVariable attached = MdlUtil.getPossibleSketchVariable(reader.readLine(), mdl); if (attached == null || !attached.isAttached()) { System.err.println("attached variable not found for valve"); } @@ -324,7 +297,7 @@ public class MdlParser { sketch.addValve((SketchValve)valve); sketch.addVariable(attached); } - else if ((so = MdlUtils.getPossibleSketchComment(line)) != null) { + else if ((so = MdlUtil.getPossibleSketchComment(line)) != null) { SketchComment comment = (SketchComment)so; if (comment.hasTextLine()) { comment.setText(reader.readLine()); @@ -335,10 +308,27 @@ public class MdlParser { // if we got this far, the element could not be parsed System.err.println("unrecognized element "+line); } - + } while ((line = reader.readLine()) != null && !line.startsWith(SKETCH_END)); - return line; + // END READING SKETCH DATA + + if (line == null) { + System.err.println("unexpected end of file"); + reader.close(); + return null; + } + + // START READING OTHER DATA + + do { + // TODO: is there anything relevant here? + } while ((line = reader.readLine()) != null); + + // END READING OTHER DATA + + reader.close(); + return mdl; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java similarity index 79% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java index 6bce7af0..51d104e3 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java @@ -1,5 +1,6 @@ package org.simantics.sysdyn.modelImport; +import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -9,18 +10,18 @@ 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.Subscript2; +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.IExpression; +import org.simantics.sysdyn.modelImport.model.expression.Expression; 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.SubscriptExpression; +import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression; import org.simantics.sysdyn.modelImport.model.support.Range; import org.simantics.sysdyn.modelImport.model.support.Variable; -public class MdlUtils { +public class MdlUtil { public enum ConnectionType { ARROW, LINE_ARROW, LINE_SEGMENT, OTHER @@ -50,6 +51,7 @@ public class MdlUtils { VAR_NAME+"\\s*=\\s*"; private static final String EQUATION_PATTERN = "([^~]*?(?:"+VAR_NAME_QUOTED+"[^~]*?)*)\\s*~\\s*"; + private static final String UNIT_PATTERN = "([^~]*?)\\s*~\\s*"; private static final String DESC_PATTERN = @@ -71,7 +73,7 @@ public class MdlUtils { } String name = normalize(matcher.group(variableName)); - IExpression expression = parseEquation(matcher.group(variableEquation)); + Expression expression = parseExpression(matcher.group(variableEquation)); String unit = matcher.group(variableUnit); Range range = parseRange(unit); @@ -107,12 +109,12 @@ public class MdlUtils { return null; } - String name = matcher.group(subscriptVariableName); + 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(","); - IExpression expression = parseEquation(matcher.group(subscriptVariableEquation)); - SubscriptExpression e = new SubscriptExpression(indices, expression); + Expression expression = parseExpression(matcher.group(subscriptVariableEquation)); + EnumerationExpression e = new EnumerationExpression(indices, expression); String unit = matcher.group(subscriptVariableUnit); Range range = parseRange(unit); @@ -153,12 +155,12 @@ public class MdlUtils { return null; } - String name = matcher.group(lookupVariableName); + 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)); - String points = matcher.group(lookupVariablePoints); + double[] points = parseLookup(matcher.group(lookupVariablePoints)); LookupExpression e = new LookupExpression(xMin, yMin, xMax, yMax, points); @@ -172,6 +174,17 @@ public class MdlUtils { return new Variable(name, e, unit, range, description); } + 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 final String NO_EXP_PATTERN = VAR_NAME+"\\s*~\\s*"; @@ -211,58 +224,101 @@ public class MdlUtils { private static final int subscriptType = 2; private static final int subscriptEquation = 3; - public static Subscript2 getPossibleSubscript(String line, MdlModel mdl) { + public static Subscript getPossibleSubscript(String line, MdlModel mdl) { Matcher matcher = Pattern.compile(subscriptPattern).matcher(line); if (!matcher.matches()) { return null; } - String name = matcher.group(subscriptName); + String name = normalize(matcher.group(subscriptName)); boolean equivalence = matcher.group(subscriptType).equals("<->"); String expression = matcher.group(subscriptEquation); if (equivalence) { - return new Subscript2(name, mdl.getSubscript(expression)); + return new Subscript(name, mdl.getSubscript(normalize(expression))); } else { - return new Subscript2(name, expression.split(",")); + return new Subscript(name, expression.split(",")); } } - private static IExpression parseEquation(String equation) { - if (equation.startsWith("INTEG")) { - return parseIntegralExpression(equation); + 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); + return null; + } + String integral = normalize(parameters[0]); + String initial = normalize(parameters[1]); + return new IntegralExpression(integral, initial); } - else if (equation.startsWith("GAME")) { - // game expressions are treated as normal parameters as we do not - // have equivalent functionality in sysdyn - return parseNormalExpression(equation); + 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 not supported + return new NormalExpression(normalize(parameters[0])); } else { - return parseNormalExpression(equation); + // the function does not require special handling + return new NormalExpression(normalize(equation)); } } - private static IntegralExpression parseIntegralExpression(String equation) { - Matcher matcher = Pattern.compile("INTEG \\((.*),(.*)\\)").matcher(equation); - if (!matcher.matches()) { - System.err.println("could not parse integral expression: "+equation); - return null; + 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()); } - String integral = normalize(matcher.group(1)); - String initial = normalize(matcher.group(2)); - - return new IntegralExpression(integral, initial); - } - - private static DelayExpression parseDelayExpression(String equation) { - return null; - } - - private static NormalExpression parseNormalExpression(String equation) { - return new NormalExpression(normalize(equation)); + return list.toArray(new String[list.size()]); } private static Range parseRange(String unit) { @@ -285,10 +341,8 @@ public class MdlUtils { } } - private static final String categoryPattern = "\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*"; - public static String getPossibleCategory(String line) { - Matcher matcher = Pattern.compile(categoryPattern).matcher(line); + Matcher matcher = Pattern.compile("\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*").matcher(line); if (!matcher.matches()) { return null; @@ -314,29 +368,6 @@ public class MdlUtils { private static final int connectionShape = 4; private static final int connectionPoints = 5; - private static final String COMMON = - // x, y, w, h, sh, bits,hid, hasf,tpos,bw,nav1,nav2(,box,fill,font) - SAVE+SAVE+SAVE+SAVE+SAVE+SAVE+SKIP+SKIP+SAVE+".*"; - - 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; - public static final String sketchValve = - "11,"+SAVE+SAVE+ COMMON; - public static final String sketchComment = - "12,"+SAVE+SAVE+ COMMON; - - // group indices for later use - private static final int elementId = 1; - private static final int elementName = 2; - private static final int elementX = 3; - private static final int elementY = 4; - private static final int elementWidth = 5; - private static final int elementHeight = 6; - private static final int elementShape = 7; - private static final int elementBits = 8; - private static final int elementTextPos = 9; - public static SketchConnection getPossibleSketchConnection(String line) { Matcher matcher = Pattern.compile(sketchConnection).matcher(line); @@ -368,6 +399,29 @@ public class MdlUtils { return array; } + private static final String COMMON = + // x, y, w, h, sh, bits,hid, hasf,tpos,bw,nav1,nav2(,box,fill,font) + SAVE+SAVE+SAVE+SAVE+SAVE+SAVE+SKIP+SKIP+SAVE+".*"; + + 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; + public static final String sketchValve = + "11,"+SAVE+SAVE+ COMMON; + public static final String sketchComment = + "12,"+SAVE+SAVE+ COMMON; + + // group indices for later use + private static final int elementId = 1; + private static final int elementName = 2; + private static final int elementX = 3; + private static final int elementY = 4; + private static final int elementWidth = 5; + private static final int elementHeight = 6; + private static final int elementShape = 7; + private static final int elementBits = 8; + private static final int elementTextPos = 9; + public static SketchVariable getPossibleSketchVariable(String line, MdlModel mdl) { Matcher matcher = Pattern.compile(sketchVariable).matcher(line); @@ -377,6 +431,9 @@ public class MdlUtils { 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))); + } SketchVariable variable = new SketchVariable(id, var); @@ -487,7 +544,7 @@ public class MdlUtils { // method above) str = str.replaceAll("\t", ""); - // remove inline :AND: and :OR: etc (not done currently) + // remove inline operations str = removeInlineOperation(str, "AND"); str = removeInlineOperation(str, "OR"); @@ -507,6 +564,8 @@ public class MdlUtils { // this should work in most cases, even with nested parentheses // in the left and right hand side expression + // TODO: does not work in all cases + // the regular expression can be split into 5 parts: // 1. delimiter character on the left (either start of string, ',' or '(': // (?:^|[(,]) 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 43fc4ce9..89e68cbf 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 @@ -1,7 +1,6 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -13,19 +12,22 @@ public class MdlModel { private HashMap variables; private HashMap> groups; - - private HashMap subscripts; - + private HashMap subscripts; private ArrayList sketches; public MdlModel(String name) { this.name = name; + this.variables = new HashMap(); this.groups = new HashMap>(); - this.subscripts = new HashMap(); + this.subscripts = new HashMap(); this.sketches = new ArrayList(); } + public String getName() { + return name; + } + public void addVariable(Variable variable, String group) { if (variables.get(variable.getName()) != null) { System.err.println("warning, duplicate variable "+variable.getName()); @@ -49,16 +51,16 @@ public class MdlModel { return new ArrayList(variables.values()); } - public void addSubscript(Subscript2 subscript) { + public void addSubscript(Subscript subscript) { subscripts.put(subscript.getName(), subscript); } - public Subscript2 getSubscript(String name) { + public Subscript 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 471f5f84..619b1f00 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 @@ -4,18 +4,26 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import org.simantics.sysdyn.modelImport.model.element.Element; +import org.simantics.sysdyn.modelImport.model.element.Symbol; public class Sketch { private String name; + private boolean edgesOutOfDate; + public double topEdge = 0; + public double bottomEdge = 0; + public double leftEdge = 0; + public double rightEdge = 0; + private ArrayList comments; private ArrayList connections; private ArrayList valves; private ArrayList variables; public Sketch() { + edgesOutOfDate = true; + comments = new ArrayList(); connections = new ArrayList(); valves = new ArrayList(); @@ -30,6 +38,54 @@ public class Sketch { this.name = name; } + private void updateEdges() { + if (edgesOutOfDate) { + boolean first = true; + for (SketchElement e : getAllElements()) { + topEdge = first ? e.getSysdynTopEdge() : Math.min(topEdge, e.getSysdynTopEdge()); + bottomEdge = first ? e.getSysdynBottomEdge() : Math.max(bottomEdge, e.getSysdynBottomEdge()); + leftEdge = first ? e.getSysdynLeftEdge() : Math.min(leftEdge, e.getSysdynLeftEdge()); + rightEdge = first ? e.getSysdynRightEdge() : Math.max(rightEdge, e.getSysdynRightEdge()); + first = false; + } + edgesOutOfDate = false; + } + } + + public double getTopEdge() { + updateEdges(); + return topEdge; + } + + public double getBottomEdge() { + updateEdges(); + return bottomEdge; + } + + public double getLeftEdge() { + updateEdges(); + return leftEdge; + } + + public double getRightEdge() { + updateEdges(); + return rightEdge; + } + + public double getWidth() { + updateEdges(); + return rightEdge - leftEdge; + } + + public double getHeight() { + updateEdges(); + return bottomEdge - topEdge; + } + + public void addObject(SketchComment comment) { + + } + public void addComment(SketchComment comment) { comments.add(comment); } @@ -76,12 +132,7 @@ public class Sketch { public double hOffset = 0; public double vOffset = 0; - public double topEdge = 0; - public double bottomEdge = 0; - public double leftEdge = 0; - public double rightEdge = 0; - - public HashMap elements = new HashMap(); + public HashMap elements = new HashMap(); public void setEdges() { boolean first = true; 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 20c5ff7d..53ef8f7e 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 @@ -1,9 +1,9 @@ package org.simantics.sysdyn.modelImport.mdl; -import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon; +import org.simantics.sysdyn.modelImport.MdlUtil.CommentIcon; import org.simantics.sysdyn.modelImport.model.element.Cloud; import org.simantics.sysdyn.modelImport.model.element.Comment; -import org.simantics.sysdyn.modelImport.model.element.Element; +import org.simantics.sysdyn.modelImport.model.element.Symbol; public class SketchComment extends SketchElement { @@ -27,7 +27,7 @@ public class SketchComment extends SketchElement { return icon; } - public Element getModelElement(double hOffset, double vOffset) { + public Symbol getModelElement(double hOffset, double vOffset) { if (icon.equals(CommentIcon.CLOUD)) { return new Cloud( getSysdyndX() + hOffset, 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 ed5d0691..58f23af4 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 @@ -1,9 +1,9 @@ package org.simantics.sysdyn.modelImport.mdl; -import org.simantics.sysdyn.modelImport.MdlUtils.ConnectionType; +import org.simantics.sysdyn.modelImport.MdlUtil.ConnectionType; import org.simantics.sysdyn.modelImport.model.element.Connection; import org.simantics.sysdyn.modelImport.model.element.Dependency; -import org.simantics.sysdyn.modelImport.model.element.Element; +import org.simantics.sysdyn.modelImport.model.element.Symbol; import org.simantics.sysdyn.modelImport.model.element.Flow; public class SketchConnection extends SketchObject { @@ -33,7 +33,7 @@ public class SketchConnection extends SketchObject { return type; } - public Connection getWriteableConnection(Element head, Element tail, double offset) { + 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)); } @@ -48,7 +48,7 @@ public class SketchConnection extends SketchObject { } // TODO: comment this? - public double getSysdynAngle(Element from, Element to, double voffset) { + public double getSysdynAngle(Symbol from, Symbol to, double voffset) { if (points == null || points.length == 0) { return 0; } @@ -75,7 +75,7 @@ public class SketchConnection extends SketchObject { // length of (p1-p0) x (p2-p0) double dd = dx0*dy - dy0*dx; - if (Math.abs(dd) < 1e-5) { + if (Math.abs(dd) < 0.01) { // Points are (almost) collinear return 0; } 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 1220b61c..2af38c2a 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 @@ -1,6 +1,6 @@ package org.simantics.sysdyn.modelImport.mdl; -import org.simantics.sysdyn.modelImport.model.element.Element; +import org.simantics.sysdyn.modelImport.model.element.Symbol; public abstract class SketchElement extends SketchObject { @@ -105,6 +105,6 @@ public abstract class SketchElement extends SketchObject { return textLine; } - public abstract Element getModelElement(double hOffset, double vOffset); + public abstract Symbol getModelElement(double hOffset, double vOffset); } 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 ee5bd15a..7685c056 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,6 +1,6 @@ package org.simantics.sysdyn.modelImport.mdl; -import org.simantics.sysdyn.modelImport.model.element.Element; +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; @@ -23,7 +23,7 @@ public class SketchValve extends SketchElement { } @Override - public Element getModelElement(double hOffset, double vOffset) { + public Symbol getModelElement(double hOffset, double vOffset) { return new Valve( getSysdyndX() + hOffset, getSysdyndY() + vOffset, 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 fe1a375f..2770b5e1 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 @@ -1,7 +1,7 @@ package org.simantics.sysdyn.modelImport.mdl; import org.simantics.sysdyn.modelImport.model.element.Auxiliary; -import org.simantics.sysdyn.modelImport.model.element.Element; +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; @@ -20,7 +20,7 @@ public class SketchVariable extends SketchElement { } @Override - public Element getModelElement(double hOffset, double vOffset) { + public Symbol getModelElement(double hOffset, double vOffset) { if (variable.getExpression() instanceof IntegralExpression) { return new Stock( getSysdyndX() + hOffset, diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript2.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java similarity index 64% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript2.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java index dddcc7b4..eb6e8852 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript2.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Subscript.java @@ -3,17 +3,19 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.ArrayList; import java.util.Arrays; -public class Subscript2 { +public class Subscript { + + // TODO: THIS IS PROBABLY NOT NEEDED AT ALL, COULD USE ENUMERATION DIRECTLY private String name; private ArrayList values; - public Subscript2(String name, String...values) { + public Subscript(String name, String...values) { this.name = name; this.values = new ArrayList(Arrays.asList(values)); } - public Subscript2(String name, Subscript2 other) { + public Subscript(String name, Subscript other) { this.name = name; this.values = other.getValues(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableObject.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableObject.java index c704b7c4..0d327b0c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableObject.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableObject.java @@ -18,9 +18,20 @@ import org.simantics.db.exception.DatabaseException; public interface IWriteableObject { /** - * @param graph WriteGraph - * @param parent The resource where the object is located + * Write this object to the graph and return the corresponding resource. + * + * @param graph + * @param parent + * @param context + * @return Resource * @throws DatabaseException */ - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException; + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException; + + /** + * Return the resource corresponding to this object or null if this object has not been written yet. + * + * @return Resource + */ + public Resource getResource(); } 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 733b16a2..2cc8e282 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 @@ -25,7 +25,7 @@ import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.modelImport.model.element.Connection; -import org.simantics.sysdyn.modelImport.model.element.Element; +import org.simantics.sysdyn.modelImport.model.element.Symbol; import org.simantics.sysdyn.modelImport.model.element.ModelVariable; import org.simantics.sysdyn.modelImport.model.element.Shadow; import org.simantics.sysdyn.modelImport.model.support.Enumeration; @@ -41,17 +41,19 @@ public class Model implements IWriteableObject { private HashMap enumerations; private HashMap variables; - private ArrayList elements; + private ArrayList elements; private ArrayList shadows; private ArrayList connections; + private Resource model; + public Model(String name) { this.name = name; enumerations = new HashMap(); variables = new HashMap(); - elements = new ArrayList(); + elements = new ArrayList(); shadows = new ArrayList(); connections = new ArrayList(); } @@ -112,7 +114,7 @@ public class Model implements IWriteableObject { return variables.values(); } - public void addElement(Element element) { + public void addElement(Symbol element) { elements.add(element); } @@ -125,13 +127,13 @@ public class Model implements IWriteableObject { } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { DiagramResource dr = DiagramResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); SimulationResource sim = SimulationResource.getInstance(graph); - Resource model = ModelUtils.createModel(graph); + model = ModelUtils.createModel(graph); graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING); // TODO: set simulation parameters @@ -142,7 +144,7 @@ public class Model implements IWriteableObject { e.write(graph, configuration, context); } - for (Element e : elements) { + for (Symbol e : elements) { e.write(graph, configuration, context); } @@ -156,6 +158,13 @@ public class Model implements IWriteableObject { Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); graph.claimLiteral(diagram, dr.HasModCount, context.getObjectCount(), Bindings.LONG); + + return model; + } + + @Override + public Resource getResource() { + return model; } } 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 80f3ba1e..39e978e4 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,5 +1,9 @@ package org.simantics.sysdyn.modelImport.model; +import java.util.HashMap; + +import org.simantics.sysdyn.modelImport.model.support.Enumeration; + public class WriteContext { private long objectCount; @@ -9,8 +13,17 @@ public class WriteContext { private long dependencyCount; private long flowCount; + private HashMap enumerations; + public WriteContext() { - this.objectCount = 0; + objectCount = 0; + + cloudCount = 0; + shadowCount = 0; + dependencyCount = 0; + flowCount = 0; + + enumerations = new HashMap(); } public String getNextObject() { @@ -36,5 +49,19 @@ public class WriteContext { public String getNextFlow() { return "Flow" + flowCount++; } + + // register an enumeration, necessary for enumeration expressions + public void registerEnumeration(Enumeration 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; + } } 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 3e2654fa..add03008 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 @@ -1,43 +1,31 @@ package org.simantics.sysdyn.modelImport.model.element; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.model.WriteContext; -import org.simantics.sysdyn.modelImport.model.expression.IExpression; +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, IExpression expression, Range range, String unit, String description) { + String name, Expression expression, Range range, String unit, String description) { super(x, y, w, h, name, expression, range, unit, description); } public Auxiliary(double x, double y, double w, double h, Variable variable) { super(x, y, w, h, variable); } - - @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource variable = createVariable(graph, sr.Auxiliary, parent, context); - createSymbol(graph, sr.AuxiliarySymbol, variable, parent, context); - - setResource(variable); - } - + @Override - public boolean supportsDependencies() { - return true; + public Resource getVariableType(ReadGraph graph) { + return SysdynResource.getInstance(graph).Auxiliary; } - + @Override - public boolean supportsFlows() { - return false; + public Resource getSymbolType(ReadGraph graph) { + return SysdynResource.getInstance(graph).AuxiliarySymbol; } } 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 1aba2584..bb1f2723 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 @@ -1,41 +1,47 @@ package org.simantics.sysdyn.modelImport.model.element; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; -public class Cloud extends Element { +public class Cloud extends Symbol { + + private Resource cloud; public Cloud(double x, double y, double w, double h) { super(x, y, w, h); } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); - Resource cloud = GraphUtils.create2(graph, sr.Cloud, + cloud = GraphUtils.create2(graph, sr.Cloud, l0.HasName, context.getNextCloud(), l0.PartOf, parent); - createSymbol(graph, sr.CloudSymbol, cloud, parent, context); + Resource symbol = super.write(graph, parent, context); + graph.claim(symbol, mr.ElementToComponent, cloud); - setResource(cloud); + return cloud; } - + @Override - public boolean supportsDependencies() { - return false; + public Resource getSymbolType(ReadGraph graph) { + return SysdynResource.getInstance(graph).CloudSymbol; } - + @Override - public boolean supportsFlows() { - return true; + public Resource getResource() { + return cloud; } } 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 264ec411..fb737ba7 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 @@ -1,16 +1,15 @@ package org.simantics.sysdyn.modelImport.model.element; import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; -public class Comment extends Element { +public class Comment extends Symbol { private String text; @@ -20,25 +19,24 @@ public class Comment extends Element { } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { DiagramResource dr = DiagramResource.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - Resource comment = createSymbol(graph, sr.AdditionalSymbols_MultilineText, null, parent, context); + Resource symbol = super.write(graph, parent, context); - graph.claimLiteral(comment, dr.HasText, text, Bindings.STRING); + graph.claimLiteral(symbol, dr.HasText, text, Bindings.STRING); - setResource(comment); + return symbol; } - + @Override - public boolean supportsDependencies() { - return false; + public Resource getSymbolType(ReadGraph graph) { + return SysdynResource.getInstance(graph).AdditionalSymbols_MultilineText; } @Override - public boolean supportsFlows() { - return false; + public Resource getResource() { + // comments only have diagram resources + return getDiagramResource(); } - } 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 00119d31..d920ea75 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 @@ -1,5 +1,6 @@ package org.simantics.sysdyn.modelImport.model.element; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; @@ -15,49 +16,41 @@ import org.simantics.sysdyn.modelImport.model.WriteContext; public abstract class Connection implements IWriteableObject { - protected Element head; - protected Element tail; + protected Symbol head; + protected Symbol tail; + + private Resource connection; + private Resource diagramConnection; - public Connection(Element head, Element tail) { + public Connection(Symbol head, Symbol tail) { this.head = head; this.tail = tail; } - - protected Resource createConnection(WriteGraph graph, Resource type, Resource parent, WriteContext context) - throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - ModelingResources mr = ModelingResources.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource connection = GraphUtils.create2(graph, type, - sr.Variable_HasHead, head.getResource(), - sr.Variable_HasTail, tail.getResource(), - l0.PartOf, parent); - graph.claim(connection, mr.Mapped, connection); - - return connection; - } - protected Resource createSymbol(WriteGraph graph, Resource type, Resource connection, Resource parent, WriteContext context) - throws DatabaseException { + @Override + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { DiagramResource dr = DiagramResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); StructuralResource2 sr2 = StructuralResource2.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); + connection = GraphUtils.create2(graph, getConnectionType(graph), + sr.Variable_HasHead, head.getResource(), + sr.Variable_HasTail, tail.getResource(), + l0.PartOf, parent); + graph.claim(connection, mr.Mapped, connection); + Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram); - Resource diagramTail = graph.getPossibleObject(tail.getResource(), mr.ComponentToElement); Resource tailConnector = GraphUtils.create2(graph, dr.Connector, - sr.HasTailTerminal, diagramTail); + sr.HasTailTerminal, tail.getDiagramResource()); - Resource diagramHead = graph.getPossibleObject(head.getResource(), mr.ComponentToElement); Resource headConnector = GraphUtils.create2(graph, dr.Connector, - sr.HasHeadTerminal, diagramHead, + sr.HasHeadTerminal, head.getDiagramResource(), dr.AreConnected, tailConnector); - Resource diagramConnection = GraphUtils.create2(graph, type, + diagramConnection = GraphUtils.create2(graph, getDiagramConnectionType(graph), l0.HasName, context.getNextObject(), l0.PartOf, diagram, sr2.HasConnectionType, sr.SysdynConnectionType, @@ -67,6 +60,18 @@ public abstract class Connection implements IWriteableObject { OrderedSetUtils.add(graph, diagram, diagramConnection); + return connection; + } + + public abstract Resource getConnectionType(ReadGraph graph); + public abstract Resource getDiagramConnectionType(ReadGraph graph); + + @Override + public Resource getResource() { + return connection; + } + + public Resource getDiagramResource() { return diagramConnection; } 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 3fab1e5c..72e34cb6 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 @@ -1,6 +1,7 @@ package org.simantics.sysdyn.modelImport.model.element; import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; @@ -14,7 +15,7 @@ public class Dependency extends Connection { private boolean showDelay; private double angle; - public Dependency(Element head, Element tail, boolean showArrow, boolean showDelay, double angle) { + public Dependency(Symbol head, Symbol tail, boolean showArrow, boolean showDelay, double angle) { super(head, tail); this.showArrow = showArrow; this.showDelay = showDelay; @@ -22,21 +23,32 @@ public class Dependency extends Connection { } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { if (head == null || tail == null) { System.err.println("dependency missing head or tail"); - return; + return null; } Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - Resource dependency = createConnection(graph, sr.Dependency, parent, context); + Resource dependency = super.write(graph, parent, context); graph.claimLiteral(dependency, l0.HasName, context.getNextDependency(), Bindings.STRING); - Resource symbol = createSymbol(graph, sr.DependencyConnection, dependency, parent, context); - graph.claimLiteral(symbol, sr.Dependency_angle, angle, Bindings.DOUBLE); + graph.claimLiteral(getDiagramResource(), sr.Dependency_angle, angle, Bindings.DOUBLE); + + return dependency; + } + + @Override + public Resource getConnectionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).Dependency; } + + @Override + public Resource getDiagramConnectionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).DependencyConnection; + } + } 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 1b40f2fc..da51170f 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 @@ -1,6 +1,7 @@ package org.simantics.sysdyn.modelImport.model.element; import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; @@ -10,24 +11,33 @@ import org.simantics.sysdyn.modelImport.model.WriteContext; public class Flow extends Connection { - public Flow(Element head, Element tail) { + public Flow(Symbol head, Symbol tail) { super(head, tail); } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { if (head == null || tail == null) { System.err.println("flow missing head or tail"); - return; + return null; } Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource flow = createConnection(graph, sr.Flow, parent, context); + Resource flow = super.write(graph, parent, context); graph.claimLiteral(flow, l0.HasName, context.getNextFlow(), Bindings.STRING); - Resource symbol = createSymbol(graph, sr.FlowConnection, flow, parent, context); + + return flow; + } + + @Override + public Resource getConnectionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).Flow; + } + + @Override + public Resource getDiagramConnectionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).FlowConnection; } } 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 fd669dc6..cd2f452f 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 @@ -1,8 +1,7 @@ package org.simantics.sysdyn.modelImport.model.element; -import java.util.List; - import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; @@ -11,22 +10,22 @@ import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; -import org.simantics.sysdyn.modelImport.model.expression.IExpression; -import org.simantics.sysdyn.modelImport.model.support.Enumeration; +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 Element { +public abstract class ModelVariable extends Symbol { - protected String name; - protected IExpression expression; - protected Range range; - protected String unit; - protected String description; + private String name; + private Expression expression; + private Range range; + private String unit; + private String description; - protected List enumerations; + private Resource variable; - public ModelVariable(double x, double y, double w, double h, String name, IExpression expression, Range range, String unit, String description) { + 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); this.name = name; this.expression = expression; @@ -48,21 +47,22 @@ public abstract class ModelVariable extends Element { return name; } - public Resource createVariable(WriteGraph graph, Resource type, Resource parent, WriteContext context) - throws DatabaseException { + @Override + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); - Resource variable = GraphUtils.create2(graph, type, + variable = GraphUtils.create2(graph, getVariableType(graph), l0.HasName, name, l0.PartOf, parent); graph.claim(variable, mr.Mapped, variable); - expression.write(graph, variable, context); - // TODO: write references to enumerations here? + if (expression != null) { + Resource expressionList = expression.write(graph, variable, context); + } if (range != null) { range.write(graph, variable, context); } @@ -72,8 +72,18 @@ public abstract class ModelVariable extends Element { if (description != null && !description.isEmpty()) { graph.claimLiteral(variable, l0.HasDescription, description, Bindings.STRING); } + + Resource symbol = super.write(graph, parent, context); + graph.claim(symbol, mr.ElementToComponent, variable); return variable; } + + public abstract Resource getVariableType(ReadGraph graph); + + @Override + public Resource getResource() { + return variable; + } } 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 b699fa9e..8f3e33eb 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 @@ -1,52 +1,58 @@ package org.simantics.sysdyn.modelImport.model.element; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.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 Element { +public class Shadow extends Symbol { - private Element original; + private ModelVariable original; - public Shadow(double x, double y, double w, double h, Element original) { + 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(SketchVariable variable, double hOffset, double vOffset, Element original) { + public Shadow(SketchVariable variable, double hOffset, double vOffset, ModelVariable original) { super(variable, hOffset, vOffset); this.original = original; } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); - Resource shadow = GraphUtils.create2(graph, sr.Shadow, + shadow = GraphUtils.create2(graph, sr.Shadow, l0.HasName, context.getNextShadow(), l0.PartOf, parent); graph.claim(shadow, sr.Shadow_original, original.getResource()); - createSymbol(graph, sr.ShadowSymbol, shadow, parent, context); + Resource symbol = super.write(graph, parent, context); + graph.claim(symbol, mr.ElementToComponent, shadow); - setResource(shadow); + return shadow; } - + @Override - public boolean supportsDependencies() { - return original.supportsDependencies(); + public Resource getSymbolType(ReadGraph graph) { + return SysdynResource.getInstance(graph).ShadowSymbol; } - + @Override - public boolean supportsFlows() { - return original.supportsFlows(); + public Resource getResource() { + return shadow; } } 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 3c958ec1..03351daa 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 @@ -1,10 +1,8 @@ package org.simantics.sysdyn.modelImport.model.element; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.model.WriteContext; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; import org.simantics.sysdyn.modelImport.model.support.Range; import org.simantics.sysdyn.modelImport.model.support.Variable; @@ -20,23 +18,14 @@ public class Stock extends ModelVariable { super(x, y, w, h, variable); } - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource stock = createVariable(graph, sr.Stock, parent, context); - createSymbol(graph, sr.StockSymbol, stock, parent, context); - - setResource(stock); - } - @Override - public boolean supportsDependencies() { - return true; + public Resource getVariableType(ReadGraph graph) { + return SysdynResource.getInstance(graph).Stock; } - + @Override - public boolean supportsFlows() { - return true; + public Resource getSymbolType(ReadGraph graph) { + return SysdynResource.getInstance(graph).StockSymbol; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Element.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java similarity index 68% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Element.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java index 8baf0a2a..58e99174 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Element.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java @@ -1,6 +1,7 @@ package org.simantics.sysdyn.modelImport.model.element; import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; @@ -14,36 +15,28 @@ import org.simantics.sysdyn.modelImport.mdl.SketchElement; import org.simantics.sysdyn.modelImport.model.IWriteableObject; import org.simantics.sysdyn.modelImport.model.WriteContext; -public abstract class Element implements IWriteableObject { +public abstract class Symbol implements IWriteableObject { private double x; private double y; private double width; private double height; - private Resource resource; + private Resource symbol; - public Element(double x, double y, double width, double height) { + public Symbol(double x, double y, double width, double height) { this.x = x; this.y = y; this.width = width; this.height = height; } - public Element(SketchElement element, double hOffset, double vOffset) { + 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 Resource getResource() { - return resource; - } - - protected void setResource(Resource resource) { - this.resource = resource; - } public double getX() { return x; @@ -61,7 +54,8 @@ public abstract class Element implements IWriteableObject { return height; } - public Resource createSymbol(WriteGraph graph, Resource type, Resource variable, Resource parent, WriteContext context) throws DatabaseException { + @Override + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { DiagramResource dr = DiagramResource.getInstance(graph); G2DResource g2d = G2DResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); @@ -69,23 +63,22 @@ public abstract class Element implements IWriteableObject { Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram); - Resource symbol = GraphUtils.create2(graph, type, + symbol = GraphUtils.create2(graph, getSymbolType(graph), l0.HasName, context.getNextObject(), l0.PartOf, diagram); - - if (variable != null) { - graph.claim(symbol, mr.ElementToComponent, variable); - } double[] transform = { 1.0, 0.0, 0.0, 1.0, x, y }; graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform, Bindings.DOUBLE_ARRAY); OrderedSetUtils.add(graph, diagram, symbol); - + return symbol; } - public abstract boolean supportsDependencies(); - public abstract boolean supportsFlows(); + public abstract Resource getSymbolType(ReadGraph graph); + + public Resource getDiagramResource() { + return symbol; + } } 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 559eff52..521fc1e3 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 @@ -1,11 +1,12 @@ package org.simantics.sysdyn.modelImport.model.element; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; -import org.simantics.sysdyn.modelImport.model.expression.IExpression; +import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; import org.simantics.sysdyn.modelImport.model.support.Variable; @@ -22,7 +23,7 @@ public class Valve extends ModelVariable { private TextPosition position; public Valve(double x, double y, double w, double h, - String name, IExpression expression, Range range, String unit, String description, TextPosition position) { + 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; } @@ -33,12 +34,9 @@ public class Valve extends ModelVariable { } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Resource valve = createVariable(graph, sr.Valve, parent, context); - Resource symbol = createSymbol(graph, sr.ValveSymbol, valve, parent, context); - Resource location = null; switch (position) { case BELOW: @@ -59,19 +57,21 @@ public class Valve extends ModelVariable { break; } - graph.claim(symbol, sr.ValveSymbol_textLocation, location); - - setResource(valve); + Resource valve = super.write(graph, parent, context); + + graph.claim(getDiagramResource(), sr.ValveSymbol_textLocation, location); + + return valve; } - + @Override - public boolean supportsDependencies() { - return true; + public Resource getVariableType(ReadGraph graph) { + return SysdynResource.getInstance(graph).Valve; } - + @Override - public boolean supportsFlows() { - return true; + public Resource getSymbolType(ReadGraph graph) { + return SysdynResource.getInstance(graph).ValveSymbol; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/DelayExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/DelayExpression.java index 07a5b976..f740d201 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/DelayExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/DelayExpression.java @@ -1,29 +1,43 @@ package org.simantics.sysdyn.modelImport.model.expression; -import java.util.Arrays; - +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; 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 DelayExpression implements IExpression { +public class DelayExpression extends Expression { + + private String expression; + private String time; + private String initial; + private int order; + + public DelayExpression(String expression, String time, String initial, int order) { + this.expression = expression; + this.time = time; + this.initial = initial; + this.order = order; + } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); -// Resource expression = GraphUtils.create2(graph, sr.DelayExpression, -// sr.StockExpression_integralEquation, integral, -// sr.StockExpression_initialEquation, initial, -// l0.PartOf, parent); -// // TODO: why is the expression stored in two places? -// graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); + Resource expr = super.write(graph, parent, context); + + graph.claimLiteral(expr, sr.DelayExpression_expression, expression, Bindings.STRING); + graph.claimLiteral(expr, sr.DelayExpression_delayTime, time, Bindings.STRING); + graph.claimLiteral(expr, sr.DelayExpression_initialValue, initial, Bindings.STRING); + graph.claimLiteral(expr, sr.DelayExpression_order, order, Bindings.INTEGER); + + return expr; + } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).DelayExpression; } - } 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 new file mode 100644 index 00000000..bdb43e10 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java @@ -0,0 +1,97 @@ +package org.simantics.sysdyn.modelImport.model.expression; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +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; + + public EnumerationExpression(String[] indices, Expression expression) { + active = new SubscriptSubExpression(indices, expression); + subExpressions = new ArrayList(); + subExpressions.add(active); + } + + public void addExpression(String[] indices, Expression expression) { + subExpressions.add(new SubscriptSubExpression(indices, expression)); + } + + public List getExpressions() { + return subExpressions; + } + + public void addExpressions(List expressions) { + subExpressions.addAll(expressions); + } + + @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; + } + + @Override + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + StringBuilder range = new StringBuilder(); + range.append('['); + for (String index : indices) { + if (range.length() > 1) + range.append(','); + range.append(index); + } + range.append(']'); + + Resource expr = super.write(graph, parent, context); + + graph.claimLiteral(expr, sr.Expression_arrayRange, range.toString(), Bindings.STRING); + + return expr; + } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return expression.getExpressionType(graph); + } + + } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return active.getExpressionType(graph); + } +} 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 new file mode 100644 index 00000000..4bac0c81 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/Expression.java @@ -0,0 +1,45 @@ +package org.simantics.sysdyn.modelImport.model.expression; + +import java.util.Arrays; + +import org.simantics.db.ReadGraph; +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 abstract class Expression implements IWriteableObject { + + private Resource expr; + + @Override + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + expr = GraphUtils.create2(graph, getExpressionType(graph), + l0.PartOf, parent); + + // 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) + ListUtils.insertBack(graph, list, Arrays.asList(expr)); + else + graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expr))); + + return expr; + } + + public abstract Resource getExpressionType(ReadGraph graph); + + @Override + public Resource getResource() { + return expr; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IExpression.java deleted file mode 100644 index 234a1059..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/IExpression.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.simantics.sysdyn.modelImport.model.expression; - -import org.simantics.sysdyn.modelImport.model.IWriteableObject; - -public interface IExpression extends IWriteableObject { - -} 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 6c3ccfa8..3a144117 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 @@ -1,17 +1,14 @@ package org.simantics.sysdyn.modelImport.model.expression; -import java.util.Arrays; - +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; 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 IntegralExpression implements IExpression { +public class IntegralExpression extends Expression { private String integral; private String initial; @@ -22,16 +19,19 @@ public class IntegralExpression implements IExpression { } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = GraphUtils.create2(graph, sr.StockExpression, - sr.StockExpression_integralEquation, integral, - sr.StockExpression_initialEquation, initial, - l0.PartOf, parent); - // TODO: why is the expression stored in two places? - graph.claim(parent, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); + Resource expr = super.write(graph, parent, context); + + graph.claimLiteral(expr, sr.StockExpression_integralEquation, integral, Bindings.STRING); + graph.claimLiteral(expr, sr.StockExpression_initialEquation, initial, Bindings.STRING); + + return expr; + } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).StockExpression; } - } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/LookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/LookupExpression.java index ccbfdb00..c94b7155 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/LookupExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/LookupExpression.java @@ -1,19 +1,22 @@ package org.simantics.sysdyn.modelImport.model.expression; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.WriteContext; -public class LookupExpression implements IExpression { +public class LookupExpression extends Expression { private double xMin; private double yMin; private double xMax; private double yMax; - private String points; + private double[] points; - public LookupExpression(double xMin, double yMin, double xMax, double yMax, String points) { + public LookupExpression(double xMin, double yMin, double xMax, double yMax, double[] points) { this.xMin = xMin; this.yMin = yMin; this.xMax = xMax; @@ -22,8 +25,34 @@ public class LookupExpression implements IExpression { } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - // TODO: implementation + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + StringBuilder lookup = new StringBuilder(); + lookup.append('{'); + for (int i = 0; i < points.length / 2; i++) { + if (i > 0) + lookup.append(','); + lookup.append('{').append(points[2*i]).append(',').append(points[2*i+1]).append('}'); + } + lookup.append('}'); + + Resource expr = super.write(graph, parent, context); + + // TODO: fix this + graph.claimLiteral(expr, sr.WithLookupExpression_expression, "", Bindings.STRING); + graph.claimLiteral(expr, sr.WithLookupExpression_lookup, lookup.toString(), Bindings.STRING); + graph.claimLiteral(expr, sr.WithLookupExpression_minX, xMin, Bindings.DOUBLE); + graph.claimLiteral(expr, sr.WithLookupExpression_minY, yMin, Bindings.DOUBLE); + graph.claimLiteral(expr, sr.WithLookupExpression_maxX, xMax, Bindings.DOUBLE); + graph.claimLiteral(expr, sr.WithLookupExpression_maxY, yMax, Bindings.DOUBLE); + + return expr; + } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).WithLookupExpression; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/NormalExpression.java index 296fe5c5..a940a643 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/NormalExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/NormalExpression.java @@ -1,33 +1,34 @@ package org.simantics.sysdyn.modelImport.model.expression; -import java.util.Arrays; - +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; 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 NormalExpression implements IExpression { +public class NormalExpression extends Expression { - private String equation; + private String expression; - public NormalExpression(String equation) { - this.equation = equation; + public NormalExpression(String expression) { + this.expression = expression; } @Override - public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - - Resource expression = GraphUtils.create2(graph, sr.NormalExpression, - sr.Expression_equation, equation, - l0.PartOf, variable); - // TODO: why is the expression stored in two places? - graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, Arrays.asList(expression))); + + Resource expr = super.write(graph, parent, context); + + graph.claimLiteral(expr, sr.Expression_equation, expression, Bindings.STRING); + + return expr; + } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return SysdynResource.getInstance(graph).NormalExpression; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/SubscriptExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/SubscriptExpression.java deleted file mode 100644 index e315e321..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/SubscriptExpression.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.simantics.sysdyn.modelImport.model.expression; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.sysdyn.modelImport.model.WriteContext; - -public class SubscriptExpression implements IExpression { - - private ArrayList elements; - - public SubscriptExpression(String[] indices, IExpression expression) { - elements = new ArrayList(); - elements.add(new SubscriptExpressionElement(indices, expression)); - } - - public List getElements() { - return elements; - } - - public void merge(SubscriptExpression other) { - elements.addAll(other.getElements()); - } - - @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) - throws DatabaseException { - System.err.println("write subscript expression"); - } - - private class SubscriptExpressionElement { - - String[] indices; - IExpression expression; - - public SubscriptExpressionElement(String[] indices, IExpression expression) { - this.indices = indices; - this.expression = expression; - } - } - -} 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 feb0f801..64d9df06 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 @@ -23,17 +23,17 @@ public class Enumeration implements IWriteableObject { public String getName() { return name; } - - public Resource getResource() { - return resource; - } @Override - public void write(WriteGraph graph, Resource parent, WriteContext context) + public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { System.err.println("write enumeration"); - - resource = null; + return null; + } + + @Override + public Resource getResource() { + return resource; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java index fb51d0a0..03ecc6d5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Range.java @@ -13,6 +13,8 @@ public class Range implements IWriteableObject { private Double step; private String original; + private Resource resource; + public Range(Double start, Double end, Double step, String original) { this.start = start; this.end = end; @@ -37,8 +39,13 @@ public class Range implements IWriteableObject { } @Override - public void write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException { - // TODO: implementation + public Resource write(WriteGraph graph, Resource variable, WriteContext context) throws DatabaseException { + return null; + } + + @Override + public Resource getResource() { + return resource; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java index 63969c01..aa08fb80 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Variable.java @@ -1,17 +1,17 @@ package org.simantics.sysdyn.modelImport.model.support; -import org.simantics.sysdyn.modelImport.model.expression.IExpression; -import org.simantics.sysdyn.modelImport.model.expression.SubscriptExpression; +import org.simantics.sysdyn.modelImport.model.expression.Expression; +import org.simantics.sysdyn.modelImport.model.expression.EnumerationExpression; public class Variable { private String name; - private IExpression expression; + private Expression expression; private String unit; private Range range; private String description; - public Variable(String name, IExpression expression, String unit, Range range, String description) { + public Variable(String name, Expression expression, String unit, Range range, String description) { this.name = name; this.expression = expression; this.unit = unit; @@ -23,7 +23,7 @@ public class Variable { return name; } - public IExpression getExpression() { + public Expression getExpression() { return expression; } @@ -39,9 +39,22 @@ public class Variable { return description; } - public void mergeExpression(Variable variable) { - if (expression instanceof SubscriptExpression) { - ((SubscriptExpression)expression).merge((SubscriptExpression)variable.getExpression()); + 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(); } }