From fb612ef3b0ce009aed820ce95398497d00260916 Mon Sep 17 00:00:00 2001 From: jkauttio Date: Tue, 27 May 2014 12:55:45 +0000 Subject: [PATCH] Start cleanup of vensim import for release. refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29552 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/handlers/imports/ImportMdlHandler.java | 3 +- .../ui/wizards/mdl/WizardMdlImportPage.java | 12 ++- .../sysdyn/modelImport/MdlParser.java | 95 ++++++++++--------- .../simantics/sysdyn/modelImport/MdlUtil.java | 83 ++++++++-------- .../sysdyn/modelImport/mdl/Declaration.java | 4 +- .../sysdyn/modelImport/mdl/IMdlObject.java | 5 - .../sysdyn/modelImport/mdl/Lookup.java | 5 +- .../sysdyn/modelImport/mdl/Sketch.java | 2 +- .../sysdyn/modelImport/mdl/SketchComment.java | 6 +- .../modelImport/mdl/SketchConnection.java | 16 +--- .../sysdyn/modelImport/mdl/SketchElement.java | 28 +++--- .../sysdyn/modelImport/mdl/SketchObject.java | 6 ++ .../sysdyn/modelImport/mdl/SketchValve.java | 4 +- .../modelImport/mdl/SketchVariable.java | 8 +- .../sysdyn/modelImport/mdl/Variable.java | 2 +- .../model/{element => }/Auxiliary.java | 2 +- .../model/{element => }/Cloud.java | 3 +- .../model/{element => }/Comment.java | 3 +- .../model/{element => }/Connection.java | 4 +- .../model/{element => }/Dependency.java | 3 +- .../modelImport/model/{element => }/Flow.java | 3 +- .../sysdyn/modelImport/model/Model.java | 25 +++-- .../model/{element => }/ModelVariable.java | 3 +- .../model/{element => }/Shadow.java | 3 +- .../model/{element => }/Stock.java | 2 +- .../model/{element => }/Symbol.java | 4 +- .../model/{element => }/Valve.java | 3 +- .../modelImport/model/WriteContext.java | 60 ------------ .../expression/EnumerationExpression.java | 32 ++----- .../modelImport/model/support/Function.java | 2 - 30 files changed, 177 insertions(+), 254 deletions(-) delete mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/IMdlObject.java rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Auxiliary.java (89%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Cloud.java (87%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Comment.java (87%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Connection.java (90%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Dependency.java (91%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Flow.java (87%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/ModelVariable.java (92%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Shadow.java (89%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Stock.java (89%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Symbol.java (88%) rename org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/{element => }/Valve.java (91%) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java index ae11a1fa..8d35a4ea 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportMdlHandler.java @@ -68,7 +68,7 @@ public class ImportMdlHandler extends AbstractHandler { // Convert Vensim model to Simantics SysDyn format using MdlParser final Model model; try { - model = MdlParser.parse(file); + model = MdlParser.parse(file); } catch (Exception e) { e.printStackTrace(); @@ -76,7 +76,6 @@ public class ImportMdlHandler extends AbstractHandler { } SimanticsUI.getSession().asyncRequest(new WriteRequest() { - @Override public void perform(WriteGraph graph) throws DatabaseException { model.write(graph, project, new WriteContext()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java index 94bf2f7d..93766ed4 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java @@ -179,10 +179,18 @@ public class WizardMdlImportPage extends WizardPage{ File file = new File(selected); - final Model model = MdlParser.parse(file); + // TODO: is this wizard actually used anywhere? + final Model model; + + try { + model = MdlParser.parse(file); + } + catch (Exception e) { + e.printStackTrace(); + return false; + } SimanticsUI.getSession().asyncRequest(new WriteRequest() { - @Override public void perform(WriteGraph graph) throws DatabaseException { model.write(graph, project, new WriteContext()); 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 b60deac5..f580493a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java @@ -15,6 +15,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; +import java.util.regex.Pattern; import org.simantics.sysdyn.modelImport.mdl.Declaration; import org.simantics.sysdyn.modelImport.mdl.Lookup; @@ -28,44 +29,44 @@ 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.Connection; import org.simantics.sysdyn.modelImport.model.Model; -import org.simantics.sysdyn.modelImport.model.element.Connection; -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.Valve; +import org.simantics.sysdyn.modelImport.model.ModelVariable; +import org.simantics.sysdyn.modelImport.model.Shadow; +import org.simantics.sysdyn.modelImport.model.Symbol; +import org.simantics.sysdyn.modelImport.model.Valve; public class MdlParser { + + private static final String UTF_8 = "{UTF-8}"; + private static final String SKETCH_VERSION = "V300"; + private static final String SKETCH_START = "\\\\\\---///"; + private static final String SKETCH_END = "///---\\\\\\"; + + private static final String PARAMETER_START = "INITIAL TIME"; + private static final String PARAMETER_STOP = "FINAL TIME"; + private static final String PARAMETER_STEP = "TIME STEP"; - public static Model parse(File file) { + public static Model parse(File file) throws Exception { // generate a mdl model based on the contents of the file - MdlModel mdl; - try { - mdl = parseFile(file); - } - catch (Exception e) { - e.printStackTrace(); - return null; - } - - System.err.println("model parsed"); + MdlModel mdl = parseFile(file); // generate a sysdyn model from the mdl model - Model model = new Model(mdl.getName()); double offset = 0; + // add (necessary) enumerations for (Subscript subscript : mdl.getAllSubscripts()) { if (!subscript.isEquivalent()) model.addEnumeration(subscript.getEnumeration(mdl)); } + // add lookup functions for (Lookup lookup : mdl.getAllLookups()) { model.addFunction(lookup.getFunction()); } - try { // add sketch labels and independent elements for (Sketch sketch : mdl.getSketches()) { @@ -100,17 +101,10 @@ public class MdlParser { offset += 200; } - } - catch (NullPointerException e) { - e.printStackTrace(); - } - // add dependent elements for (Sketch sketch : mdl.getSketches()) { for (SketchVariable variable : sketch.getShadowVariables()) { ModelVariable original = model.getVariable(variable.getName()); - if (original == null) - System.err.println("original variable not found for "+variable.getName()); Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(mdl, sketch); if (symbol != null) { variable.setModelObject(symbol); @@ -128,20 +122,24 @@ public class MdlParser { } // Set simulation parameters + Variable start = mdl.getVariable(PARAMETER_START); + if (start != null && Pattern.matches(MdlUtil.DBL, start.getExpressionString())) + model.setStart(Double.parseDouble(start.getExpressionString())); + + Variable stop = mdl.getVariable(PARAMETER_STOP); + if (stop != null && Pattern.matches(MdlUtil.DBL, stop.getExpressionString())) + model.setStop(Double.parseDouble(stop.getExpressionString())); + + Variable step = mdl.getVariable(PARAMETER_STEP); + if (step != null && Pattern.matches(MdlUtil.DBL, step.getExpressionString())) + model.setStep(Double.parseDouble(step.getExpressionString())); + + // TODO: unit and result step thingamajig - System.err.println("DONE"); - return model; } - private static final String UTF_8 = "{UTF-8}"; - private static final String SKETCH_VERSION = "V300"; - private static final String SKETCH_START = "\\\\\\---///"; - private static final String SKETCH_END = "///---\\\\\\"; - - private static MdlModel parseFile(File file) - throws Exception { - + private static MdlModel parseFile(File file) throws Exception { // strip the file suffix from file name String name = file.getName(); if (file.getName().contains(".")) { @@ -181,8 +179,13 @@ public class MdlParser { // combine the (possible) variable declaration into one line StringBuilder buffer = new StringBuilder(); do { + // strip leading whitespace if present + line = line.replaceFirst("^\\s*", ""); // strip trailing '\' if present - buffer.append(line.endsWith("\\") ? line.substring(0, line.length()-1) : line); + line = line.replaceFirst("\\\\$", ""); + + buffer.append(line); + // each variable declaration ends with '|' if (line.endsWith("|")) break; @@ -190,25 +193,23 @@ public class MdlParser { String str = buffer.toString(); String cat; - Variable var; - Lookup lookup; - Subscript subscript; + Declaration decl; // parse the (possible) variable declaration if ((cat = Declaration.parseCategory(str)) != null) { category = cat; } - else if ((var = Variable.getPossible(str)) != null) { - mdl.addVariable(var, category); + else if ((decl = Variable.getPossible(str)) != null) { + mdl.addVariable((Variable)decl, category); } - else if ((var = SubscriptVariable.getPossible(str)) != null) { - mdl.addSubscriptVariable((SubscriptVariable)var, category); + else if ((decl = SubscriptVariable.getPossible(str)) != null) { + mdl.addSubscriptVariable((SubscriptVariable)decl, category); } - else if ((lookup = Lookup.getPossible(str)) != null) { - mdl.addLookup(lookup); + else if ((decl = Lookup.getPossible(str)) != null) { + mdl.addLookup((Lookup)decl); } - else if ((subscript = Subscript.getPossible(str)) != null) { - mdl.addSubscript(subscript); + else if ((decl = Subscript.getPossible(str)) != null) { + mdl.addSubscript((Subscript)decl); } else { // if we got this far, the variable could not be parsed diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java index b3546407..819988ae 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlUtil.java @@ -9,16 +9,16 @@ import org.simantics.sysdyn.modelImport.mdl.Lookup; import org.simantics.sysdyn.modelImport.mdl.MdlModel; public class MdlUtil { - + // most of this is based on the documentation of the .mdl file format // available in http://www.vensim.com/documentation/24305.htm - + // some regular expressions that are used commonly in the parser - public static final String DBL = "-?\\d+(?:\\.\\d+)?"; + public static final String DBL = "[+-]?(?:NaN|Infinity|\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)"; public static final String DBL_G = "("+DBL+")"; - public static final String INT = "-?\\d+"; + public static final String INT = "[+-]?\\d+"; public static final String INT_G = "("+INT+")"; - + // matches a basic vensim name (starts with a letter, may contain any word // characters and white space) public static final String BASIC_NAME = @@ -27,10 +27,10 @@ public class MdlUtil { // may contain escaped special characters) public static final String SPECIAL_NAME = "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\""; - + // matches a vensim variable (either basic name or special name) public static final String VARIABLE = - "("+BASIC_NAME+"|"+SPECIAL_NAME+")"; + "("+BASIC_NAME+"|"+SPECIAL_NAME+")"; // matches a vensim variable inside an expression (basic name not followed // by an open parenthesis or a special name) public static final String VARIABLE_EXPRESSION = @@ -41,7 +41,7 @@ public class MdlUtil { // matches a vensim function (basic name followed by an open parenthesis) public static final String FUNCTION = "("+BASIC_NAME+")\\s*\\("; - + public static String normalize(String str) { // start by removing all tabs from the string, not really necessary // but does make the equation cleaner @@ -51,14 +51,14 @@ public class MdlUtil { str = normalizeFunctions(str); // normalize variables str = normalizeVariables(str); - + // replace inline operations str = str.replaceAll(":AND:", " and "); str = str.replaceAll(":OR:", " or "); return str; } - + private static String normalizeVariables(String expression) { StringBuilder result = new StringBuilder(); int offset = 0; @@ -68,7 +68,7 @@ public class MdlUtil { result.append(expression.substring(offset, matcher.start())); String variable = matcher.group(1); - + if (variable.equalsIgnoreCase("time")) { result.append("time"); } @@ -90,16 +90,16 @@ public class MdlUtil { result.append(parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1)); } } - + offset = matcher.end(); } if (offset < expression.length()) { result.append(expression.substring(offset)); } - + return result.toString(); } - + private static String normalizeFunctions(String expression) { StringBuilder result = new StringBuilder(); int offset = 0; @@ -109,7 +109,7 @@ public class MdlUtil { result.append(expression.substring(offset, matcher.start())); String function = matcher.group(1); - + if (function.equalsIgnoreCase("sum")) { // vensim "sum" is similar to modelica "sum" if operations // are replaced with dot-operations (e.g. * with .*) inside @@ -134,17 +134,17 @@ public class MdlUtil { if (offset < expression.length()) { result.append(expression.substring(offset)); } - + return result.toString(); } - + public static String finalize(String expression, MdlModel mdl) { expression = expandIterations(expression, mdl); expression = renameLookups(expression, mdl); - + return expression; } - + private static String expandIterations(String expression, MdlModel mdl) { StringBuilder result = new StringBuilder(); int offset = 0; @@ -152,17 +152,17 @@ public class MdlUtil { Matcher matcher = Pattern.compile(SUBSCRIPT).matcher(expression); while (matcher.find()) { result.append(expression.substring(offset, matcher.start())); - + String name = matcher.group(1); String[] indices = matcher.group(2).split(","); - + result.append(name); - + result.append('['); for (int i = 0; i < indices.length; i++) { if (i > 0) result.append(','); - + if (indices[i].endsWith("!")) { String subscript = indices[i].substring(0, indices[i].length() - 1); List values = mdl.getSubscript(subscript).getValues(mdl); @@ -179,16 +179,16 @@ public class MdlUtil { } } result.append(']'); - + offset = matcher.end(); } if (offset < expression.length()) { result.append(expression.substring(offset)); } - + return result.toString(); } - + private static String renameLookups(String expression, MdlModel mdl) { StringBuilder result = new StringBuilder(); int offset = 0; @@ -198,42 +198,41 @@ public class MdlUtil { result.append(expression.substring(offset, matcher.start())); String name = matcher.group(1); - + Lookup potential = mdl.getLookup(name); - + if (potential != null) { - System.err.println("found lookup "+name); name = name.replaceAll("\\s+", ""); } - + result.append(name+"("); offset = matcher.end(); } if (offset < expression.length()) { result.append(expression.substring(offset)); } - + return result.toString(); } - + public static String replaceSubscripts(String expression, String subscript, String replacement) { StringBuilder result = new StringBuilder(); int offset = 0; - + Matcher matcher = Pattern.compile(SUBSCRIPT).matcher(expression); while (matcher.find()) { result.append(expression.substring(offset, matcher.start())); - + String name = matcher.group(1); String[] indices = matcher.group(2).split(","); - + result.append(name); - + result.append('['); for (int i = 0; i < indices.length; i++) { if (i > 0) result.append(','); - + if (indices[i].equals(subscript)) { result.append(replacement); } @@ -242,16 +241,16 @@ public class MdlUtil { } } result.append(']'); - + offset = matcher.end(); } if (offset < expression.length()) { result.append(expression.substring(offset)); } - + return result.toString(); } - + public static String[] splitFunctionParameters(String str) { ArrayList list = new ArrayList(); @@ -260,7 +259,7 @@ public class MdlUtil { int level = 0; boolean comment = false; boolean brackets = false; - + for (i = 0; i < str.length(); i++) { char current = str.charAt(i); if (current == '"') @@ -282,7 +281,7 @@ public class MdlUtil { if (last < i) { list.add(str.substring(last, i).trim()); } - + return list.toArray(new String[list.size()]); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java index 4c3a1e1e..45d1bb41 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Declaration.java @@ -5,7 +5,7 @@ import java.util.regex.Pattern; public abstract class Declaration { - private static final String CATEGORY = "\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*"; + private static final String CATEGORY = "\\*{56}\\s*\\.(.+)\\s*\\*{56}.*"; protected static final String DECL_DELIM = "\\s*~\\s*"; protected static final String DECL_END = "\\s*\\|"; @@ -52,7 +52,7 @@ public abstract class Declaration { public static String parseCategory(String line) { Matcher matcher = Pattern.compile(CATEGORY).matcher(line); - return matcher.matches() ? matcher.group(1) : null; + return matcher.matches() ? matcher.group(1).trim() : null; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/IMdlObject.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/IMdlObject.java deleted file mode 100644 index cfa4129b..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/IMdlObject.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.simantics.sysdyn.modelImport.mdl; - -public interface IMdlObject { - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java index b166a32d..d6528be1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Lookup.java @@ -4,7 +4,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.simantics.sysdyn.modelImport.MdlUtil; -import org.simantics.sysdyn.modelImport.model.expression.LookupExpression; import org.simantics.sysdyn.modelImport.model.support.Function; public class Lookup extends Declaration { @@ -17,7 +16,7 @@ public class Lookup extends Declaration { "("+MdlUtil.BASIC_NAME+")\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)"; private static final String LOOKUP_DECL = - LOOKUP+"("+DECL_SUFFIX+")"; + LOOKUP+"("+DECL_SUFFIX+")"; private static final int lookupName = 1; private static final int lookupRangeXMin = 2; private static final int lookupRangeYMin = 3; @@ -34,8 +33,6 @@ public class Lookup extends Declaration { private Function function; - // TODO: generate functions from lookup variables - protected Lookup(String name, double xMin, double yMin, double xMax, double yMax, double[] points) { super(name); this.xMin = xMin; 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 865f8da2..cba3e0c9 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 @@ -5,7 +5,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; -import org.simantics.sysdyn.modelImport.model.element.Symbol; +import org.simantics.sysdyn.modelImport.model.Symbol; public class Sketch { 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 8cae21d9..e0d0eb13 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 @@ -3,9 +3,9 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.simantics.sysdyn.modelImport.model.element.Cloud; -import org.simantics.sysdyn.modelImport.model.element.Comment; -import org.simantics.sysdyn.modelImport.model.element.Symbol; +import org.simantics.sysdyn.modelImport.model.Cloud; +import org.simantics.sysdyn.modelImport.model.Comment; +import org.simantics.sysdyn.modelImport.model.Symbol; public class SketchComment extends SketchElement { 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 74cb1bf7..cd20a7b0 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 @@ -3,11 +3,10 @@ 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.Connection; -import org.simantics.sysdyn.modelImport.model.element.Dependency; -import org.simantics.sysdyn.modelImport.model.element.Symbol; -import org.simantics.sysdyn.modelImport.model.element.Flow; +import org.simantics.sysdyn.modelImport.model.Connection; +import org.simantics.sysdyn.modelImport.model.Dependency; +import org.simantics.sysdyn.modelImport.model.Flow; +import org.simantics.sysdyn.modelImport.model.Symbol; public class SketchConnection extends SketchObject { @@ -15,11 +14,6 @@ public class SketchConnection extends SketchObject { ARROW, LINE_ARROW, LINE_SEGMENT, OTHER } - // some shorthands - private static final String KEEP_I = MdlUtil.INT_G+","; - private static final String SKIP_I = MdlUtil.INT+","; - private static final String SKIP_ANY = "[^,]*,"; - private static final String POINTS = "(\\d+\\|(?:\\(-?\\d+,-?\\d+\\)\\|)+)"; // each sketch connection is declared with a string: @@ -132,7 +126,7 @@ public class SketchConnection extends SketchObject { } } - public double getAngle(Sketch sketch) { + private double getAngle(Sketch sketch) { if (points == null || points.length < 2) { 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 b0418646..3507db92 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchElement.java @@ -3,15 +3,13 @@ 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.Symbol; -import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition; +import org.simantics.sysdyn.modelImport.model.Symbol; +import org.simantics.sysdyn.modelImport.model.Valve.TextPosition; public abstract class SketchElement extends SketchObject { - // some shorthands - protected static final String KEEP_I = MdlUtil.INT_G+","; - protected static final String SKIP_I = MdlUtil.INT+","; + // multiplier for dimension conversion between vensim and sysdyn + private static final double SCALE_MULTIPLIER = 0.4; // each sketch element is declared with a string: // n,id,name,x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font) @@ -24,13 +22,13 @@ public abstract class SketchElement extends SketchObject { protected static final String ELEMENT_SUFFIX = KEEP_I+KEEP_I+KEEP_I+KEEP_I+KEEP_I+KEEP_I+SKIP_I+SKIP_I+KEEP_I+".*"; // corresponding group indices - protected static final int elementX = 1; - protected static final int elementY = 2; - protected static final int elementWidth = 3; - protected static final int elementHeight = 4; - protected static final int elementShape = 5; - protected static final int elementBits = 6; - protected static final int elementTextPos = 7; + private static final int elementX = 1; + private static final int elementY = 2; + private static final int elementWidth = 3; + private static final int elementHeight = 4; + private static final int elementShape = 5; + private static final int elementBits = 6; + private static final int elementTextPos = 7; private int x; private int y; @@ -119,8 +117,6 @@ public abstract class SketchElement extends SketchObject { public abstract Symbol getSymbol(MdlModel mdl, Sketch sketch); - private static final double SCALE_MULTIPLIER = 0.4; - public double[] getDimensions(Sketch sketch) { double[] dimensions = getDimensions(); dimensions[0] = dimensions[0] + sketch.getHorizontalOffset(); @@ -128,7 +124,7 @@ public abstract class SketchElement extends SketchObject { return dimensions; } - // get element dimensions, [x, y, width, height] + // get element dimensions [x, y, width, height] public double[] getDimensions() { double[] dimensions = new double[4]; dimensions[0] = (getX() - getWidth()) * SCALE_MULTIPLIER; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java index 8bf51055..2ddb1e4c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchObject.java @@ -1,8 +1,14 @@ package org.simantics.sysdyn.modelImport.mdl; +import org.simantics.sysdyn.modelImport.MdlUtil; import org.simantics.sysdyn.modelImport.model.IWriteableObject; public abstract class SketchObject { + + // some shorthands + protected static final String KEEP_I = MdlUtil.INT_G+","; + protected static final String SKIP_I = MdlUtil.INT+","; + protected static final String SKIP_ANY = "[^,]*,"; private int id; private IWriteableObject modelObject; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/SketchValve.java index 2d0c88ce..f9d5bd38 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 @@ -3,8 +3,8 @@ package org.simantics.sysdyn.modelImport.mdl; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.simantics.sysdyn.modelImport.model.element.Valve; -import org.simantics.sysdyn.modelImport.model.element.Valve.Orientation; +import org.simantics.sysdyn.modelImport.model.Valve; +import org.simantics.sysdyn.modelImport.model.Valve.Orientation; public class SketchValve extends SketchElement { 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 5137203e..f6f3a1f0 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 @@ -4,10 +4,10 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; 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.Auxiliary; +import org.simantics.sysdyn.modelImport.model.ModelVariable; +import org.simantics.sysdyn.modelImport.model.Stock; +import org.simantics.sysdyn.modelImport.model.Symbol; import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression; public class SketchVariable extends SketchElement { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java index 7dceeaef..25f21fa2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/mdl/Variable.java @@ -5,7 +5,7 @@ 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.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; 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/Auxiliary.java similarity index 89% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java index e30545b6..871d2106 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Auxiliary.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; 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/Cloud.java similarity index 87% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java index 9687c533..c93d003f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Cloud.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -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.model.WriteContext; public class Cloud extends Symbol { 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/Comment.java similarity index 87% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment.java index b632ca1d..06c651c9 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Comment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Comment.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -7,7 +7,6 @@ import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.model.WriteContext; public class Comment extends Symbol { 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/Connection.java similarity index 90% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection.java index dda8b59a..acd124c5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Connection.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -11,8 +11,6 @@ import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.modeling.ModelingResources; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.model.IWriteableObject; -import org.simantics.sysdyn.modelImport.model.WriteContext; public abstract class Connection implements IWriteableObject { 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/Dependency.java similarity index 91% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency.java index a2c486bb..a09c3e82 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Dependency.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -7,7 +7,6 @@ import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.model.WriteContext; public class Dependency extends Connection { 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/Flow.java similarity index 87% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow.java index 9e3b28d5..6fb0d8d0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Flow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -7,7 +7,6 @@ import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.model.WriteContext; public class Flow extends Connection { 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 0b56e6e6..65cc051d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java @@ -14,7 +14,6 @@ package org.simantics.sysdyn.modelImport.model; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.List; import org.simantics.databoard.Bindings; import org.simantics.db.Resource; @@ -24,10 +23,7 @@ import org.simantics.diagram.stubs.DiagramResource; 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.Symbol; -import org.simantics.sysdyn.modelImport.model.element.ModelVariable; -import org.simantics.sysdyn.modelImport.model.element.Shadow; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelImport.model.support.Enumeration; import org.simantics.sysdyn.modelImport.model.support.Function; import org.simantics.sysdyn.utils.ModelUtils; @@ -37,6 +33,7 @@ public class Model implements IWriteableObject { private String name; // necessary simulation parameters private double start, stop, step; + private String unit; // the structure of the model private HashMap variables; @@ -59,6 +56,15 @@ public class Model implements IWriteableObject { symbols = new ArrayList(); shadows = new ArrayList(); connections = new ArrayList(); + + this.start = 0; + this.stop = 100; + this.step = 1; + this.unit = null; + } + + public void setParameters(double start, double stop, double step, String unit) { + } public double getStart() { @@ -92,6 +98,7 @@ public class Model implements IWriteableObject { variables.put(variable.getName(), variable); + symbols.add(variable); } @@ -152,11 +159,17 @@ public class Model implements IWriteableObject { Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); SimulationResource sim = SimulationResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); model = ModelUtils.createModel(graph); graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING); - // TODO: set simulation parameters + // TODO: this must be updated if/when simulation parameters are moved + // from model to experiment + //graph.claimLiteral(model, sr.SysdynModel_startTime, start, Bindings.DOUBLE); + //graph.claimLiteral(model, sr.SysdynModel_stopTime, stop, Bindings.DOUBLE); + //graph.claimLiteral(model, sr.SysdynModel_simulationStepLength, step, Bindings.DOUBLE); + //graph.claimLiteral(model, sr.SysdynModel_timeUnit, unit, Bindings.STRING); Resource configuration = graph.getSingleObject(model, sim.HasConfiguration); 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/ModelVariable.java similarity index 92% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java index d5e96f00..5e64585d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/ModelVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelVariable.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -9,7 +9,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.model.WriteContext; import org.simantics.sysdyn.modelImport.model.expression.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; 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/Shadow.java similarity index 89% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow.java index 716c2aa7..2e7d2689 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Shadow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Shadow.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -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.model.WriteContext; public class Shadow extends Symbol { 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/Stock.java similarity index 89% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java index 9c4b11c2..76842a2d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Stock.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Symbol.java similarity index 88% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Symbol.java index e4f23be6..18e76cf7 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Symbol.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Symbol.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -11,8 +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.model.IWriteableObject; -import org.simantics.sysdyn.modelImport.model.WriteContext; public abstract class Symbol implements IWriteableObject { 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/Valve.java similarity index 91% rename from org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java rename to org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java index f9451118..9a84e094 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/element/Valve.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java @@ -1,11 +1,10 @@ -package org.simantics.sysdyn.modelImport.model.element; +package org.simantics.sysdyn.modelImport.model; 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.Expression; import org.simantics.sysdyn.modelImport.model.support.Range; 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 1cc4f3d2..4a00b01a 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,14 +1,5 @@ package org.simantics.sysdyn.modelImport.model; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; - -import org.simantics.db.Resource; -import org.simantics.sysdyn.modelImport.model.support.Enumeration; - public class WriteContext { private long objectCount; @@ -18,9 +9,6 @@ public class WriteContext { private long dependencyCount; private long flowCount; - private HashMap enumerations; - private HashMap> enumerationValues; - public WriteContext() { objectCount = 0; @@ -28,9 +16,6 @@ public class WriteContext { shadowCount = 0; dependencyCount = 0; flowCount = 0; - - enumerations = new HashMap(); - enumerationValues = new HashMap>(); } public String getNextObject() { @@ -56,50 +41,5 @@ public class WriteContext { public String getNextFlow() { return "Flow" + flowCount++; } - - public void registerEnumeration(Enumeration enumeration, boolean copy) { - enumerations.put(enumeration.getName(), enumeration); - if (enumerationValues.get(enumeration.getName()) == null) { - enumerationValues.put(enumeration.getName(), new HashSet()); - } - enumerationValues.get(enumeration.getName()).add(enumeration); - if (!copy) { - for (String value : enumeration.getValues()) { - if (enumerationValues.get(value) == null) { - enumerationValues.put(value, new HashSet()); - } - enumerationValues.get(value).add(enumeration); - } - } - } - - public Enumeration getEnumeration(Set indices) { - System.err.println("size is "+indices.size()); - if (indices.size() == 1) { - Enumeration enumeration = enumerations.get(indices.iterator().next()); - if (enumeration != null) { - return enumeration; - } - } - - // the set of potential matches is the intersection of all sets of - // enumerations that contain any of the given indices as elements - Set potential = new HashSet(enumerations.values()); - for (String index : indices) { - potential.retainAll(enumerationValues.get(index)); - } - - // if there is more than one potential match, the enumerations are not - // well defined (there are multiple enumerations with similar indices) - if (potential.size() == 1) { - return potential.iterator().next(); - } - else { - System.err.println("enumeration could not be resolved "+potential.size()); - for (String index : indices) - System.err.println(" "+index); - return null; - } - } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java index 00d99797..c3246adc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/expression/EnumerationExpression.java @@ -1,9 +1,7 @@ package org.simantics.sysdyn.modelImport.model.expression; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -26,19 +24,9 @@ public class EnumerationExpression extends Expression { } public void addExpression(Expression expression, String...indices) { -// 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 EnumerationSubExpression(expression, indices)); } - public List getExpressions() { - return subExpressions; - } - @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); @@ -70,6 +58,16 @@ public class EnumerationExpression extends Expression { return getResource(); } + + @Override + public Resource getExpressionType(ReadGraph graph) { + return subExpressions.get(0).expression.getExpressionType(graph); + } + + @Override + public Resource getResource() { + return subExpressions.get(0).expression.getResource(); + } private class EnumerationSubExpression { @@ -81,14 +79,4 @@ public class EnumerationExpression extends Expression { this.indices = indices; } } - - @Override - public Resource getExpressionType(ReadGraph graph) { - return subExpressions.get(0).expression.getExpressionType(graph); - } - - @Override - public Resource getResource() { - return subExpressions.get(0).expression.getResource(); - } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java index faa5f5a6..8aa761f1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/support/Function.java @@ -41,8 +41,6 @@ public class Function implements IWriteableObject { @Override public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException { - System.err.println("write function"); - Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); -- 2.47.1