// Convert Vensim model to Simantics SysDyn format using MdlParser\r
final Model model;\r
try {\r
- model = MdlParser.parse(file);\r
+ model = MdlParser.parse(file);\r
}\r
catch (Exception e) {\r
e.printStackTrace();\r
}\r
\r
SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
- \r
@Override\r
public void perform(WriteGraph graph) throws DatabaseException {\r
model.write(graph, project, new WriteContext());\r
\r
File file = new File(selected);\r
\r
- final Model model = MdlParser.parse(file);\r
+ // TODO: is this wizard actually used anywhere?\r
+ final Model model;\r
+ \r
+ try {\r
+ model = MdlParser.parse(file);\r
+ }\r
+ catch (Exception e) {\r
+ e.printStackTrace();\r
+ return false;\r
+ }\r
\r
SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
- \r
@Override\r
public void perform(WriteGraph graph) throws DatabaseException {\r
model.write(graph, project, new WriteContext());\r
import java.io.File;\r
import java.io.FileInputStream;\r
import java.io.InputStreamReader;\r
+import java.util.regex.Pattern;\r
\r
import org.simantics.sysdyn.modelImport.mdl.Declaration;\r
import org.simantics.sysdyn.modelImport.mdl.Lookup;\r
import org.simantics.sysdyn.modelImport.mdl.Subscript;\r
import org.simantics.sysdyn.modelImport.mdl.SubscriptVariable;\r
import org.simantics.sysdyn.modelImport.mdl.Variable;\r
+import org.simantics.sysdyn.modelImport.model.Connection;\r
import org.simantics.sysdyn.modelImport.model.Model;\r
-import org.simantics.sysdyn.modelImport.model.element.Connection;\r
-import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
-import org.simantics.sysdyn.modelImport.model.element.Shadow;\r
-import org.simantics.sysdyn.modelImport.model.element.Valve;\r
+import org.simantics.sysdyn.modelImport.model.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.Shadow;\r
+import org.simantics.sysdyn.modelImport.model.Symbol;\r
+import org.simantics.sysdyn.modelImport.model.Valve;\r
\r
public class MdlParser {\r
+ \r
+ private static final String UTF_8 = "{UTF-8}";\r
+ private static final String SKETCH_VERSION = "V300";\r
+ private static final String SKETCH_START = "\\\\\\---///";\r
+ private static final String SKETCH_END = "///---\\\\\\";\r
+ \r
+ private static final String PARAMETER_START = "INITIAL TIME";\r
+ private static final String PARAMETER_STOP = "FINAL TIME";\r
+ private static final String PARAMETER_STEP = "TIME STEP";\r
\r
- public static Model parse(File file) {\r
+ public static Model parse(File file) throws Exception {\r
// generate a mdl model based on the contents of the file\r
- MdlModel mdl;\r
- try {\r
- mdl = parseFile(file);\r
- }\r
- catch (Exception e) {\r
- e.printStackTrace();\r
- return null;\r
- }\r
- \r
- System.err.println("model parsed");\r
+ MdlModel mdl = parseFile(file);\r
\r
// generate a sysdyn model from the mdl model\r
- \r
Model model = new Model(mdl.getName());\r
\r
double offset = 0;\r
\r
+ // add (necessary) enumerations\r
for (Subscript subscript : mdl.getAllSubscripts()) {\r
if (!subscript.isEquivalent())\r
model.addEnumeration(subscript.getEnumeration(mdl));\r
}\r
\r
+ // add lookup functions\r
for (Lookup lookup : mdl.getAllLookups()) {\r
model.addFunction(lookup.getFunction());\r
}\r
\r
- try {\r
// add sketch labels and independent elements\r
for (Sketch sketch : mdl.getSketches()) {\r
\r
offset += 200;\r
}\r
\r
- }\r
- catch (NullPointerException e) {\r
- e.printStackTrace();\r
- }\r
- \r
// add dependent elements\r
for (Sketch sketch : mdl.getSketches()) {\r
for (SketchVariable variable : sketch.getShadowVariables()) {\r
ModelVariable original = model.getVariable(variable.getName());\r
- if (original == null)\r
- System.err.println("original variable not found for "+variable.getName());\r
Symbol symbol = original != null ? new Shadow(variable.getDimensions(sketch), original) : variable.getSymbol(mdl, sketch);\r
if (symbol != null) {\r
variable.setModelObject(symbol);\r
}\r
\r
// Set simulation parameters\r
+ Variable start = mdl.getVariable(PARAMETER_START);\r
+ if (start != null && Pattern.matches(MdlUtil.DBL, start.getExpressionString()))\r
+ model.setStart(Double.parseDouble(start.getExpressionString()));\r
+ \r
+ Variable stop = mdl.getVariable(PARAMETER_STOP);\r
+ if (stop != null && Pattern.matches(MdlUtil.DBL, stop.getExpressionString()))\r
+ model.setStop(Double.parseDouble(stop.getExpressionString()));\r
+ \r
+ Variable step = mdl.getVariable(PARAMETER_STEP);\r
+ if (step != null && Pattern.matches(MdlUtil.DBL, step.getExpressionString()))\r
+ model.setStep(Double.parseDouble(step.getExpressionString()));\r
+ \r
+ // TODO: unit and result step thingamajig\r
\r
- System.err.println("DONE");\r
-\r
return model;\r
}\r
\r
- private static final String UTF_8 = "{UTF-8}";\r
- private static final String SKETCH_VERSION = "V300";\r
- private static final String SKETCH_START = "\\\\\\---///";\r
- private static final String SKETCH_END = "///---\\\\\\";\r
- \r
- private static MdlModel parseFile(File file) \r
- throws Exception {\r
- \r
+ private static MdlModel parseFile(File file) throws Exception {\r
// strip the file suffix from file name\r
String name = file.getName();\r
if (file.getName().contains(".")) {\r
// combine the (possible) variable declaration into one line \r
StringBuilder buffer = new StringBuilder();\r
do {\r
+ // strip leading whitespace if present\r
+ line = line.replaceFirst("^\\s*", "");\r
// strip trailing '\' if present\r
- buffer.append(line.endsWith("\\") ? line.substring(0, line.length()-1) : line);\r
+ line = line.replaceFirst("\\\\$", "");\r
+ \r
+ buffer.append(line);\r
+ \r
// each variable declaration ends with '|'\r
if (line.endsWith("|"))\r
break;\r
String str = buffer.toString();\r
\r
String cat;\r
- Variable var;\r
- Lookup lookup;\r
- Subscript subscript;\r
+ Declaration decl;\r
\r
// parse the (possible) variable declaration\r
if ((cat = Declaration.parseCategory(str)) != null) {\r
category = cat;\r
}\r
- else if ((var = Variable.getPossible(str)) != null) {\r
- mdl.addVariable(var, category);\r
+ else if ((decl = Variable.getPossible(str)) != null) {\r
+ mdl.addVariable((Variable)decl, category);\r
}\r
- else if ((var = SubscriptVariable.getPossible(str)) != null) {\r
- mdl.addSubscriptVariable((SubscriptVariable)var, category);\r
+ else if ((decl = SubscriptVariable.getPossible(str)) != null) {\r
+ mdl.addSubscriptVariable((SubscriptVariable)decl, category);\r
}\r
- else if ((lookup = Lookup.getPossible(str)) != null) {\r
- mdl.addLookup(lookup);\r
+ else if ((decl = Lookup.getPossible(str)) != null) {\r
+ mdl.addLookup((Lookup)decl);\r
}\r
- else if ((subscript = Subscript.getPossible(str)) != null) {\r
- mdl.addSubscript(subscript);\r
+ else if ((decl = Subscript.getPossible(str)) != null) {\r
+ mdl.addSubscript((Subscript)decl);\r
}\r
else {\r
// if we got this far, the variable could not be parsed\r
import org.simantics.sysdyn.modelImport.mdl.MdlModel;\r
\r
public class MdlUtil {\r
- \r
+\r
// most of this is based on the documentation of the .mdl file format \r
// available in http://www.vensim.com/documentation/24305.htm\r
- \r
+\r
// some regular expressions that are used commonly in the parser\r
- public static final String DBL = "-?\\d+(?:\\.\\d+)?";\r
+ public static final String DBL = "[+-]?(?:NaN|Infinity|\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)";\r
public static final String DBL_G = "("+DBL+")";\r
- public static final String INT = "-?\\d+";\r
+ public static final String INT = "[+-]?\\d+";\r
public static final String INT_G = "("+INT+")";\r
- \r
+\r
// matches a basic vensim name (starts with a letter, may contain any word \r
// characters and white space)\r
public static final String BASIC_NAME =\r
// may contain escaped special characters)\r
public static final String SPECIAL_NAME =\r
"\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"";\r
- \r
+\r
// matches a vensim variable (either basic name or special name)\r
public static final String VARIABLE =\r
- "("+BASIC_NAME+"|"+SPECIAL_NAME+")";\r
+ "("+BASIC_NAME+"|"+SPECIAL_NAME+")";\r
// matches a vensim variable inside an expression (basic name not followed \r
// by an open parenthesis or a special name)\r
public static final String VARIABLE_EXPRESSION =\r
// matches a vensim function (basic name followed by an open parenthesis)\r
public static final String FUNCTION =\r
"("+BASIC_NAME+")\\s*\\(";\r
- \r
+\r
public static String normalize(String str) {\r
// start by removing all tabs from the string, not really necessary \r
// but does make the equation cleaner\r
str = normalizeFunctions(str);\r
// normalize variables\r
str = normalizeVariables(str);\r
- \r
+\r
// replace inline operations\r
str = str.replaceAll(":AND:", " and ");\r
str = str.replaceAll(":OR:", " or ");\r
\r
return str;\r
}\r
- \r
+\r
private static String normalizeVariables(String expression) {\r
StringBuilder result = new StringBuilder();\r
int offset = 0;\r
result.append(expression.substring(offset, matcher.start()));\r
\r
String variable = matcher.group(1);\r
- \r
+\r
if (variable.equalsIgnoreCase("time")) {\r
result.append("time");\r
}\r
result.append(parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1));\r
}\r
}\r
- \r
+\r
offset = matcher.end();\r
}\r
if (offset < expression.length()) {\r
result.append(expression.substring(offset));\r
}\r
- \r
+\r
return result.toString();\r
}\r
- \r
+\r
private static String normalizeFunctions(String expression) {\r
StringBuilder result = new StringBuilder();\r
int offset = 0;\r
result.append(expression.substring(offset, matcher.start()));\r
\r
String function = matcher.group(1);\r
- \r
+\r
if (function.equalsIgnoreCase("sum")) {\r
// vensim "sum" is similar to modelica "sum" if operations \r
// are replaced with dot-operations (e.g. * with .*) inside\r
if (offset < expression.length()) {\r
result.append(expression.substring(offset));\r
}\r
- \r
+\r
return result.toString();\r
}\r
- \r
+\r
public static String finalize(String expression, MdlModel mdl) {\r
expression = expandIterations(expression, mdl);\r
expression = renameLookups(expression, mdl);\r
- \r
+\r
return expression;\r
}\r
- \r
+\r
private static String expandIterations(String expression, MdlModel mdl) {\r
StringBuilder result = new StringBuilder();\r
int offset = 0;\r
Matcher matcher = Pattern.compile(SUBSCRIPT).matcher(expression);\r
while (matcher.find()) {\r
result.append(expression.substring(offset, matcher.start()));\r
- \r
+\r
String name = matcher.group(1);\r
String[] indices = matcher.group(2).split(",");\r
- \r
+\r
result.append(name);\r
- \r
+\r
result.append('[');\r
for (int i = 0; i < indices.length; i++) {\r
if (i > 0)\r
result.append(',');\r
- \r
+\r
if (indices[i].endsWith("!")) {\r
String subscript = indices[i].substring(0, indices[i].length() - 1);\r
List<String> values = mdl.getSubscript(subscript).getValues(mdl);\r
}\r
}\r
result.append(']');\r
- \r
+\r
offset = matcher.end();\r
}\r
if (offset < expression.length()) {\r
result.append(expression.substring(offset));\r
}\r
- \r
+\r
return result.toString();\r
}\r
- \r
+\r
private static String renameLookups(String expression, MdlModel mdl) {\r
StringBuilder result = new StringBuilder();\r
int offset = 0;\r
result.append(expression.substring(offset, matcher.start()));\r
\r
String name = matcher.group(1);\r
- \r
+\r
Lookup potential = mdl.getLookup(name);\r
- \r
+\r
if (potential != null) {\r
- System.err.println("found lookup "+name);\r
name = name.replaceAll("\\s+", "");\r
}\r
- \r
+\r
result.append(name+"(");\r
offset = matcher.end();\r
}\r
if (offset < expression.length()) {\r
result.append(expression.substring(offset));\r
}\r
- \r
+\r
return result.toString();\r
}\r
- \r
+\r
public static String replaceSubscripts(String expression, String subscript, String replacement) {\r
StringBuilder result = new StringBuilder();\r
int offset = 0;\r
- \r
+\r
Matcher matcher = Pattern.compile(SUBSCRIPT).matcher(expression);\r
while (matcher.find()) {\r
result.append(expression.substring(offset, matcher.start()));\r
- \r
+\r
String name = matcher.group(1);\r
String[] indices = matcher.group(2).split(",");\r
- \r
+\r
result.append(name);\r
- \r
+\r
result.append('[');\r
for (int i = 0; i < indices.length; i++) {\r
if (i > 0)\r
result.append(',');\r
- \r
+\r
if (indices[i].equals(subscript)) {\r
result.append(replacement);\r
}\r
}\r
}\r
result.append(']');\r
- \r
+\r
offset = matcher.end();\r
}\r
if (offset < expression.length()) {\r
result.append(expression.substring(offset));\r
}\r
- \r
+\r
return result.toString();\r
}\r
- \r
+\r
public static String[] splitFunctionParameters(String str) {\r
ArrayList<String> list = new ArrayList<String>();\r
\r
int level = 0;\r
boolean comment = false;\r
boolean brackets = false;\r
- \r
+\r
for (i = 0; i < str.length(); i++) {\r
char current = str.charAt(i);\r
if (current == '"')\r
if (last < i) {\r
list.add(str.substring(last, i).trim());\r
}\r
- \r
+\r
return list.toArray(new String[list.size()]);\r
}\r
}\r
\r
public abstract class Declaration {\r
\r
- private static final String CATEGORY = "\\*{56}\\s*\\.(\\S+)\\s*\\*{56}.*";\r
+ private static final String CATEGORY = "\\*{56}\\s*\\.(.+)\\s*\\*{56}.*";\r
\r
protected static final String DECL_DELIM = "\\s*~\\s*";\r
protected static final String DECL_END = "\\s*\\|";\r
\r
public static String parseCategory(String line) {\r
Matcher matcher = Pattern.compile(CATEGORY).matcher(line);\r
- return matcher.matches() ? matcher.group(1) : null;\r
+ return matcher.matches() ? matcher.group(1).trim() : null;\r
}\r
\r
}\r
+++ /dev/null
-package org.simantics.sysdyn.modelImport.mdl;\r
-\r
-public interface IMdlObject {\r
- \r
-}\r
import java.util.regex.Pattern;\r
\r
import org.simantics.sysdyn.modelImport.MdlUtil;\r
-import org.simantics.sysdyn.modelImport.model.expression.LookupExpression;\r
import org.simantics.sysdyn.modelImport.model.support.Function;\r
\r
public class Lookup extends Declaration {\r
"("+MdlUtil.BASIC_NAME+")\\s*\\(\\s*"+LOOKUP_RANGE+","+LOOKUP_POINTS+"\\s*\\)";\r
\r
private static final String LOOKUP_DECL = \r
- LOOKUP+"("+DECL_SUFFIX+")"; \r
+ LOOKUP+"("+DECL_SUFFIX+")";\r
private static final int lookupName = 1;\r
private static final int lookupRangeXMin = 2;\r
private static final int lookupRangeYMin = 3;\r
\r
private Function function;\r
\r
- // TODO: generate functions from lookup variables\r
-\r
protected Lookup(String name, double xMin, double yMin, double xMax, double yMax, double[] points) {\r
super(name);\r
this.xMin = xMin;\r
import java.util.Iterator;\r
import java.util.List;\r
\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
+import org.simantics.sysdyn.modelImport.model.Symbol;\r
\r
public class Sketch {\r
\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
-import org.simantics.sysdyn.modelImport.model.element.Cloud;\r
-import org.simantics.sysdyn.modelImport.model.element.Comment;\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
+import org.simantics.sysdyn.modelImport.model.Cloud;\r
+import org.simantics.sysdyn.modelImport.model.Comment;\r
+import org.simantics.sysdyn.modelImport.model.Symbol;\r
\r
public class SketchComment extends SketchElement {\r
\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
-import org.simantics.sysdyn.modelImport.MdlUtil;\r
-import org.simantics.sysdyn.modelImport.model.element.Connection;\r
-import org.simantics.sysdyn.modelImport.model.element.Dependency;\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
-import org.simantics.sysdyn.modelImport.model.element.Flow;\r
+import org.simantics.sysdyn.modelImport.model.Connection;\r
+import org.simantics.sysdyn.modelImport.model.Dependency;\r
+import org.simantics.sysdyn.modelImport.model.Flow;\r
+import org.simantics.sysdyn.modelImport.model.Symbol;\r
\r
public class SketchConnection extends SketchObject {\r
\r
ARROW, LINE_ARROW, LINE_SEGMENT, OTHER\r
}\r
\r
- // some shorthands\r
- private static final String KEEP_I = MdlUtil.INT_G+",";\r
- private static final String SKIP_I = MdlUtil.INT+",";\r
- private static final String SKIP_ANY = "[^,]*,";\r
- \r
private static final String POINTS = "(\\d+\\|(?:\\(-?\\d+,-?\\d+\\)\\|)+)";\r
\r
// each sketch connection is declared with a string:\r
}\r
}\r
\r
- public double getAngle(Sketch sketch) {\r
+ private double getAngle(Sketch sketch) {\r
if (points == null || points.length < 2) {\r
return 0;\r
}\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
-import org.simantics.sysdyn.modelImport.MdlUtil;\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
-import org.simantics.sysdyn.modelImport.model.element.Valve.TextPosition;\r
+import org.simantics.sysdyn.modelImport.model.Symbol;\r
+import org.simantics.sysdyn.modelImport.model.Valve.TextPosition;\r
\r
public abstract class SketchElement extends SketchObject {\r
\r
- // some shorthands\r
- protected static final String KEEP_I = MdlUtil.INT_G+",";\r
- protected static final String SKIP_I = MdlUtil.INT+",";\r
+ // multiplier for dimension conversion between vensim and sysdyn\r
+ private static final double SCALE_MULTIPLIER = 0.4;\r
\r
// each sketch element is declared with a string:\r
// n,id,name,x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font)\r
protected static final String ELEMENT_SUFFIX =\r
KEEP_I+KEEP_I+KEEP_I+KEEP_I+KEEP_I+KEEP_I+SKIP_I+SKIP_I+KEEP_I+".*";\r
// corresponding group indices\r
- protected static final int elementX = 1;\r
- protected static final int elementY = 2;\r
- protected static final int elementWidth = 3;\r
- protected static final int elementHeight = 4;\r
- protected static final int elementShape = 5;\r
- protected static final int elementBits = 6;\r
- protected static final int elementTextPos = 7;\r
+ private static final int elementX = 1;\r
+ private static final int elementY = 2;\r
+ private static final int elementWidth = 3;\r
+ private static final int elementHeight = 4;\r
+ private static final int elementShape = 5;\r
+ private static final int elementBits = 6;\r
+ private static final int elementTextPos = 7;\r
\r
private int x;\r
private int y;\r
\r
public abstract Symbol getSymbol(MdlModel mdl, Sketch sketch);\r
\r
- private static final double SCALE_MULTIPLIER = 0.4;\r
- \r
public double[] getDimensions(Sketch sketch) {\r
double[] dimensions = getDimensions();\r
dimensions[0] = dimensions[0] + sketch.getHorizontalOffset();\r
return dimensions;\r
}\r
\r
- // get element dimensions, [x, y, width, height]\r
+ // get element dimensions [x, y, width, height]\r
public double[] getDimensions() {\r
double[] dimensions = new double[4];\r
dimensions[0] = (getX() - getWidth()) * SCALE_MULTIPLIER;\r
package org.simantics.sysdyn.modelImport.mdl;\r
\r
+import org.simantics.sysdyn.modelImport.MdlUtil;\r
import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
\r
public abstract class SketchObject {\r
+ \r
+ // some shorthands\r
+ protected static final String KEEP_I = MdlUtil.INT_G+",";\r
+ protected static final String SKIP_I = MdlUtil.INT+",";\r
+ protected static final String SKIP_ANY = "[^,]*,";\r
\r
private int id;\r
private IWriteableObject modelObject;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
-import org.simantics.sysdyn.modelImport.model.element.Valve;\r
-import org.simantics.sysdyn.modelImport.model.element.Valve.Orientation;\r
+import org.simantics.sysdyn.modelImport.model.Valve;\r
+import org.simantics.sysdyn.modelImport.model.Valve.Orientation;\r
\r
public class SketchValve extends SketchElement {\r
\r
import java.util.regex.Pattern;\r
\r
import org.simantics.sysdyn.modelImport.MdlUtil;\r
-import org.simantics.sysdyn.modelImport.model.element.Auxiliary;\r
-import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
-import org.simantics.sysdyn.modelImport.model.element.Stock;\r
+import org.simantics.sysdyn.modelImport.model.Auxiliary;\r
+import org.simantics.sysdyn.modelImport.model.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.Stock;\r
+import org.simantics.sysdyn.modelImport.model.Symbol;\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
\r
public class SketchVariable extends SketchElement {\r
import java.util.regex.Pattern;\r
\r
import org.simantics.sysdyn.modelImport.MdlUtil;\r
-import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
+import org.simantics.sysdyn.modelImport.model.ModelVariable;\r
import org.simantics.sysdyn.modelImport.model.expression.DelayExpression;\r
import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.expression.IntegralExpression;\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public class Cloud extends Symbol {\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.diagram.stubs.DiagramResource;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public class Comment extends Symbol {\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.structural.stubs.StructuralResource2;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public abstract class Connection implements IWriteableObject {\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public class Dependency extends Connection {\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public class Flow extends Connection {\r
\r
import java.util.ArrayList;\r
import java.util.Collection;\r
import java.util.HashMap;\r
-import java.util.List;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.Resource;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.sysdyn.modelImport.model.element.Connection;\r
-import org.simantics.sysdyn.modelImport.model.element.Symbol;\r
-import org.simantics.sysdyn.modelImport.model.element.ModelVariable;\r
-import org.simantics.sysdyn.modelImport.model.element.Shadow;\r
+import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
import org.simantics.sysdyn.modelImport.model.support.Function;\r
import org.simantics.sysdyn.utils.ModelUtils;\r
private String name;\r
// necessary simulation parameters\r
private double start, stop, step;\r
+ private String unit;\r
\r
// the structure of the model\r
private HashMap<String, ModelVariable> variables;\r
symbols = new ArrayList<Symbol>();\r
shadows = new ArrayList<Shadow>();\r
connections = new ArrayList<Connection>();\r
+ \r
+ this.start = 0;\r
+ this.stop = 100;\r
+ this.step = 1;\r
+ this.unit = null;\r
+ }\r
+ \r
+ public void setParameters(double start, double stop, double step, String unit) {\r
+ \r
}\r
\r
public double getStart() {\r
\r
variables.put(variable.getName(), variable);\r
\r
+ \r
symbols.add(variable);\r
}\r
\r
Layer0 l0 = Layer0.getInstance(graph);\r
ModelingResources mr = ModelingResources.getInstance(graph);\r
SimulationResource sim = SimulationResource.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
model = ModelUtils.createModel(graph);\r
graph.claimLiteral(model, l0.HasLabel, name, Bindings.STRING);\r
\r
- // TODO: set simulation parameters\r
+ // TODO: this must be updated if/when simulation parameters are moved \r
+ // from model to experiment\r
+ //graph.claimLiteral(model, sr.SysdynModel_startTime, start, Bindings.DOUBLE);\r
+ //graph.claimLiteral(model, sr.SysdynModel_stopTime, stop, Bindings.DOUBLE);\r
+ //graph.claimLiteral(model, sr.SysdynModel_simulationStepLength, step, Bindings.DOUBLE);\r
+ //graph.claimLiteral(model, sr.SysdynModel_timeUnit, unit, Bindings.STRING);\r
\r
Resource configuration = graph.getSingleObject(model, sim.HasConfiguration);\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public class Shadow extends Symbol {\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.modeling.ModelingResources;\r
-import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
\r
public abstract class Symbol implements IWriteableObject {\r
\r
-package org.simantics.sysdyn.modelImport.model.element;\r
+package org.simantics.sysdyn.modelImport.model;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.modelImport.model.WriteContext;\r
import org.simantics.sysdyn.modelImport.model.expression.Expression;\r
import org.simantics.sysdyn.modelImport.model.support.Range;\r
\r
package org.simantics.sysdyn.modelImport.model;\r
\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Set;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.sysdyn.modelImport.model.support.Enumeration;\r
-\r
public class WriteContext {\r
\r
private long objectCount;\r
private long dependencyCount;\r
private long flowCount;\r
\r
- private HashMap<String, Enumeration> enumerations;\r
- private HashMap<String, Set<Enumeration>> enumerationValues;\r
- \r
public WriteContext() {\r
objectCount = 0;\r
\r
shadowCount = 0;\r
dependencyCount = 0;\r
flowCount = 0;\r
- \r
- enumerations = new HashMap<String, Enumeration>();\r
- enumerationValues = new HashMap<String, Set<Enumeration>>();\r
}\r
\r
public String getNextObject() {\r
public String getNextFlow() {\r
return "Flow" + flowCount++;\r
}\r
- \r
- public void registerEnumeration(Enumeration enumeration, boolean copy) {\r
- enumerations.put(enumeration.getName(), enumeration);\r
- if (enumerationValues.get(enumeration.getName()) == null) {\r
- enumerationValues.put(enumeration.getName(), new HashSet<Enumeration>());\r
- }\r
- enumerationValues.get(enumeration.getName()).add(enumeration);\r
- if (!copy) {\r
- for (String value : enumeration.getValues()) {\r
- if (enumerationValues.get(value) == null) {\r
- enumerationValues.put(value, new HashSet<Enumeration>());\r
- }\r
- enumerationValues.get(value).add(enumeration);\r
- }\r
- }\r
- }\r
- \r
- public Enumeration getEnumeration(Set<String> indices) {\r
- System.err.println("size is "+indices.size());\r
- if (indices.size() == 1) {\r
- Enumeration enumeration = enumerations.get(indices.iterator().next());\r
- if (enumeration != null) {\r
- return enumeration;\r
- }\r
- }\r
- \r
- // the set of potential matches is the intersection of all sets of\r
- // enumerations that contain any of the given indices as elements\r
- Set<Enumeration> potential = new HashSet<Enumeration>(enumerations.values());\r
- for (String index : indices) {\r
- potential.retainAll(enumerationValues.get(index));\r
- }\r
- \r
- // if there is more than one potential match, the enumerations are not\r
- // well defined (there are multiple enumerations with similar indices)\r
- if (potential.size() == 1) {\r
- return potential.iterator().next();\r
- }\r
- else {\r
- System.err.println("enumeration could not be resolved "+potential.size());\r
- for (String index : indices)\r
- System.err.println(" "+index);\r
- return null;\r
- }\r
- }\r
\r
}\r
package org.simantics.sysdyn.modelImport.model.expression;\r
\r
import java.util.ArrayList;\r
-import java.util.HashSet;\r
import java.util.List;\r
-import java.util.Set;\r
\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
}\r
\r
public void addExpression(Expression expression, String...indices) {\r
-// for (int i = 0; i < indices.length; i++) {\r
-// if (!enumerations.get(i).getValues().contains(indices)) {\r
-// System.err.println("tried adding an enumeration expression with incorrect indices");\r
-// return;\r
-// }\r
-// }\r
subExpressions.add(new EnumerationSubExpression(expression, indices));\r
}\r
\r
- public List<EnumerationSubExpression> getExpressions() {\r
- return subExpressions;\r
- }\r
- \r
@Override\r
public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r
return getResource();\r
}\r
+\r
+ @Override\r
+ public Resource getExpressionType(ReadGraph graph) {\r
+ return subExpressions.get(0).expression.getExpressionType(graph);\r
+ }\r
+ \r
+ @Override\r
+ public Resource getResource() {\r
+ return subExpressions.get(0).expression.getResource();\r
+ }\r
\r
private class EnumerationSubExpression {\r
\r
this.indices = indices;\r
}\r
}\r
-\r
- @Override\r
- public Resource getExpressionType(ReadGraph graph) {\r
- return subExpressions.get(0).expression.getExpressionType(graph);\r
- }\r
- \r
- @Override\r
- public Resource getResource() {\r
- return subExpressions.get(0).expression.getResource();\r
- }\r
}\r
@Override\r
public Resource write(WriteGraph graph, Resource parent, WriteContext context) \r
throws DatabaseException {\r
- System.err.println("write function");\r
- \r
Layer0 l0 = Layer0.getInstance(graph);\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
\r