import org.eclipse.core.commands.AbstractHandler;\r
import org.eclipse.core.commands.ExecutionEvent;\r
import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.core.runtime.Platform;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.widgets.FileDialog;\r
import org.eclipse.swt.widgets.Shell;\r
import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.sysdyn.mdlImport.MdlParser;\r
-import org.simantics.sysdyn.mdlImport.mdlElements.*;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
import org.simantics.ui.SimanticsUI;\r
\r
public class ImportMdlHandler extends AbstractHandler {\r
\r
@Override\r
public void perform(WriteGraph graph) throws DatabaseException {\r
- model.writeToProject(graph, project);\r
+ model.write(graph, project);\r
}\r
});\r
\r
\r
public class MdlFile {\r
\r
- private ArrayList<String> elements = new ArrayList<String>();\r
+ private ArrayList<String> variables = new ArrayList<String>();\r
private ArrayList<String> controls = new ArrayList<String>();\r
private ArrayList<String> sketchData = new ArrayList<String>();\r
private ArrayList<String> otherData = new ArrayList<String>();\r
\r
\r
\r
- public void addElement(String element) {\r
- elements.add(element);\r
+ public void addVariable(String variable) {\r
+ variables.add(variable);\r
}\r
\r
- public ArrayList<String> getElements() {\r
- return elements;\r
+ public ArrayList<String> getVariables() {\r
+ return variables;\r
}\r
\r
public void addControl(String control) {\r
import org.simantics.sysdyn.mdlImport.mdlElements.Auxiliary;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Cloud;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Connection;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.Dependency;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Element;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Expression;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.Flow;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Stock;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Subscript;\r
import org.simantics.sysdyn.mdlImport.mdlElements.Valve;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.Variable;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.View;\r
\r
public class MdlParser {\r
\r
\r
MdlFile mdlFile = getMdlContents(file);\r
\r
- getElementData(model, mdlFile.getElements());\r
+ getVariableData(model, mdlFile.getVariables());\r
\r
getSketchData(model, mdlFile.getSketchData());\r
\r
line = input.readLine();\r
}\r
\r
- String element = elementBuilder.toString();\r
- if(element.contains(".Control")) {\r
+ String variable = elementBuilder.toString();\r
+ if(variable.contains(".Control")) {\r
// Start of control variables\r
break;\r
} else {\r
// Add element string to model\r
- mdlFile.addElement(element);\r
+ mdlFile.addVariable(variable);\r
}\r
}\r
\r
return mdlFile;\r
}\r
\r
- private static void getElementData(Model model, ArrayList<String> elements) {\r
+ private static void getVariableData(Model model, ArrayList<String> elements) {\r
for(String elementString : elements) {\r
- createElement(model, elementString);\r
+ createVariable(model, elementString);\r
}\r
}\r
\r
}\r
\r
\r
- private static Element createElement(Model model, String elementString) {\r
+ private static Variable createVariable(Model model, String elementString) {\r
\r
String[] elementExpressions = elementString.split("\\~\\~\\|");\r
\r
- Element element = null;\r
+ Variable variable = null;\r
\r
for(String s : elementExpressions) {\r
// Skip these definitions at least for now\r
String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
\r
// Get element in the first iteration\r
- if(element == null) {\r
- element = model.getElement(nameAndRange[0]);\r
- if(element == null) {\r
- element = new Auxiliary();\r
- element.setName(nameAndRange[0].replace("\"", ""));\r
- model.addElement(element);\r
+ if(variable == null) {\r
+ Element e = model.getElement(nameAndRange[0]);\r
+ if(e != null && e instanceof Variable)\r
+ variable = (Variable)e;\r
+ if(variable == null) {\r
+ variable = new Auxiliary();\r
+ variable.setName(nameAndRange[0].replace("\"", ""));\r
+ model.addElement(variable);\r
}\r
}\r
\r
String[] rangeParts = range.split(",");\r
\r
try {\r
- element.setRangeStart(Double.parseDouble(rangeParts[0]));\r
+ variable.setRangeStart(Double.parseDouble(rangeParts[0]));\r
if(rangeParts.length >= 2)\r
- element.setRangeEnd(Double.parseDouble(rangeParts[1]));\r
+ variable.setRangeEnd(Double.parseDouble(rangeParts[1]));\r
if(rangeParts.length >= 3)\r
- element.setRangeStep(Double.parseDouble(rangeParts[2]));\r
+ variable.setRangeStep(Double.parseDouble(rangeParts[2]));\r
} catch (NumberFormatException e) {\r
// Not a double\r
}\r
expressionUnitsAndComments[1] = expressionUnitsAndComments[1].substring(0, expressionUnitsAndComments[1].lastIndexOf("["));\r
}\r
- element.setUnits(expressionUnitsAndComments[1].trim());\r
+ variable.setUnits(expressionUnitsAndComments[1].trim());\r
\r
\r
- element.setComments(expressionUnitsAndComments[2].trim());\r
+ variable.setComments(expressionUnitsAndComments[2].trim());\r
}\r
} else {\r
// SUBSCRIPT\r
String[] subscript = s.split(":"); \r
if(subscript.length == 2) {\r
// Subscript\r
- element = new Subscript();\r
- model.addElement(element);\r
- element.setName(subscript[0].replace("\"", ""));\r
+ variable = new Subscript();\r
+ model.addElement(variable);\r
+ variable.setName(subscript[0].replace("\"", ""));\r
String[] expressionUnitsAndComments = subscript[1].split("[\\~|\\|]");\r
expression.setExpression(expressionUnitsAndComments[0].trim());\r
- element.setUnits(expressionUnitsAndComments[1].trim());\r
- element.setComments(expressionUnitsAndComments[2].trim());\r
- } else {\r
+ variable.setUnits(expressionUnitsAndComments[1].trim());\r
+ variable.setComments(expressionUnitsAndComments[2].trim());\r
+ } else if(s.contains("(")){\r
// TABLE: These are actually functions in vensim. When function library is created, use these to create interpolation functions\r
- element = new Auxiliary();\r
- element.setName(s.substring(0, s.indexOf("(")).replace("\"", ""));\r
- model.addElement(element);\r
+ variable = new Auxiliary();\r
+ variable.setName(s.substring(0, s.indexOf("(")).replace("\"", ""));\r
+ model.addElement(variable);\r
String theRest = s.substring(s.indexOf("(") + 1);\r
String[] expressionUnitsAndComments = theRest.split("[\\~|\\|]");\r
- element.setUnits(expressionUnitsAndComments[1].trim());\r
- element.setComments(expressionUnitsAndComments[2].trim());\r
+ variable.setUnits(expressionUnitsAndComments[1].trim());\r
+ variable.setComments(expressionUnitsAndComments[2].trim());\r
// [(0,0)-(2,5)],(0,5),(0.5,3),(1,0.5),(2,0.5) => ,(0,5),(0.5,3),(1,0.5),(2,0.5)\r
String table = expressionUnitsAndComments[0].trim().split("[\\[|\\]]")[2];\r
// ,(0,5),(0.5,3),(1,0.5),(2,0.5) => (0,5),(0.5,3),(1,0.5),(2,0.5)\r
table = table.replace("(", "{");\r
table = table.replace(")", "}");\r
expression.setExpression(table); \r
+ } else {\r
+ // Just an empty variable, Data-driven variable\r
+ variable = new Auxiliary();\r
+ String[] nameUnitsAndComments = s.split("[\\~|\\|]");\r
+ variable.setName(nameUnitsAndComments[0].trim().replace("\"", ""));\r
+ model.addElement(variable);\r
+ expression.setExpression("");\r
+ variable.setUnits(nameUnitsAndComments[1].trim());\r
+ variable.setComments(nameUnitsAndComments[2].trim());\r
}\r
}\r
\r
- if(element != null) {\r
+ if(variable != null) {\r
// Finally add the expression to element\r
- element.getExpressions().add(expression);\r
+ variable.getExpressions().add(expression);\r
}\r
}\r
- return element;\r
+ return variable;\r
}\r
\r
+ private static int SCALE = 4;\r
private static void getSketchData(Model model, ArrayList<String> sketchData) {\r
- HashMap<String, Element> elementNumbers = new HashMap<String, Element>();\r
- ArrayList<String[]> connections = new ArrayList<String[]>();\r
- HashMap<String, String[]> emptyValves = new HashMap<String, String[]>(); // map for valves that don't have an element \r
- \r
+\r
String line = null;\r
- for(int i = 0; i < sketchData.size(); i++) {\r
+ View view = null;\r
+ int i = 0;\r
+ while(i < sketchData.size()) {\r
line = sketchData.get(i);\r
- if(line.startsWith("*"))\r
- continue;\r
+ if(line.startsWith("*")) {\r
+ view = new View();\r
+ model.addView(view);\r
+ \r
+ view.setName(line.substring(1));\r
\r
- String[] data = line.split(",");\r
+ // STARTED A NEW VIEW\r
\r
- if(data[0].equals("1")) {\r
- // Connections are handled after all elements\r
- connections.add(data);\r
+ HashMap<String, Element> elementNumbers = new HashMap<String, Element>();\r
+ ArrayList<String[]> connections = new ArrayList<String[]>();\r
+ HashMap<String, String[]> emptyValves = new HashMap<String, String[]>(); // map for valves that don't have an element \r
\r
- } else if(data[0].equals("11")){\r
- // Valve\r
- i = i + 1;\r
- String elementLine = sketchData.get(i);\r
- String[] elementData = elementLine.split(",");\r
- // FIXME: Assumes that element is always attached to the valve\r
- Element element = model.getElement(elementData[2].replace("\"", ""));\r
- Valve valve = new Valve();\r
- if(element != null) {\r
- valve.setName(element.getName());\r
- valve.setExpressions(element.getExpressions());\r
- valve.setUnits(element.getUnits());\r
- valve.setComments(element.getComments());\r
- valve.setX(Integer.parseInt(data[3]) / 5); // SCALE\r
- valve.setY(Integer.parseInt(data[4]) / 5); // SCALE\r
- \r
+ \r
+ i++;\r
+ line = sketchData.get(i);\r
+ while(i < sketchData.size() && !sketchData.get(i).startsWith("*")) {\r
+ line = sketchData.get(i);\r
\r
- model.removeElement(element);\r
- model.addElement(valve);\r
+ if(line.startsWith("$")) {\r
+ view.setFontParameters(line);\r
+ i++;\r
+ continue;\r
+ }\r
+ \r
+ String[] data = line.split(",");\r
+\r
+ if(data[0].equals("1")) {\r
+ // Connections are handled after all elements\r
+ connections.add(data);\r
+ \r
+ } else if(data[0].equals("11")){\r
+ // Valve\r
+ i = i + 1;\r
+ String elementLine = sketchData.get(i);\r
+ String[] elementData = elementLine.split(",");\r
+ // FIXME: Assumes that element is always attached to the valve\r
+ Element element = model.getElement(elementData[2].replace("\"", ""));\r
+ Valve valve = new Valve();\r
+ if(element != null && element instanceof Variable) {\r
+ Variable v = (Variable) element;\r
+ valve.setName(v.getName());\r
+ valve.setExpressions(v.getExpressions());\r
+ valve.setUnits(v.getUnits());\r
+ valve.setComments(v.getComments());\r
+ valve.setX(Integer.parseInt(data[3]) / SCALE);\r
+ valve.setY(Integer.parseInt(data[4]) / SCALE);\r
+ \r
+ model.removeElement(element);\r
+ model.addElement(view, valve);\r
+ \r
+ // Add valve to the element list with both valve and variable symbol numbers\r
+ elementNumbers.put(elementData[1], valve);\r
+ elementNumbers.put(data[1], valve);\r
+ } else {\r
+ i = i - 1;\r
+ emptyValves.put(data[1], data);\r
+ }\r
+ } else if(data[0].equals("12")){\r
+ // Cloud\r
+ Cloud cloud = new Cloud();\r
+ cloud.setX(Integer.parseInt(data[3]) / SCALE); \r
+ cloud.setY(Integer.parseInt(data[4]) / SCALE); \r
+ elementNumbers.put(data[1], cloud);\r
+ model.addElement(view, cloud);\r
+ } else if(data[0].equals("10") && data.length <= 15){\r
+ // Some variable\r
+ Element e = model.getElement(data[2].replace("\"", "").trim());\r
+ if(e != null && e instanceof Variable) {\r
+ Variable v = (Variable) e;\r
+ if(v.getExpressions().get(0).getExpression().startsWith("INTEG (") && !(e instanceof Stock)) {\r
+ // Stock\r
+ Stock s = new Stock();\r
+ s.setName(v.getName());\r
+ s.setUnits(v.getUnits());\r
+ s.setComments(v.getComments());\r
+ s.setExpressions(v.getExpressions());\r
+ model.removeElement(e);\r
+ e = s;\r
+ model.addElement(view, e);\r
+ }\r
+ }\r
+ e.setX(Integer.parseInt(data[3]) / SCALE); \r
+ e.setY(Integer.parseInt(data[4]) / SCALE); \r
+ elementNumbers.put(data[1], e);\r
+ model.relocateElement(view, e);\r
+ } else if(data[0].equals("10") && data.length > 15){\r
+ // TODO: Ghost\r
+ \r
+ // for now, direct back to the original element\r
+ Element originalElement = model.getElement(data[2].replace("\"", ""));\r
+ if(originalElement != null)\r
+ elementNumbers.put(data[1], originalElement);\r
+ }\r
\r
- // Add valve to the element list with both valve and variable symbol numbers\r
- elementNumbers.put(elementData[1], valve);\r
- elementNumbers.put(data[1], valve);\r
- } else {\r
- i = i - 1;\r
- emptyValves.put(data[1], data);\r
+ i++;\r
}\r
- } else if(data[0].equals("12")){\r
- // Cloud\r
- Cloud cloud = new Cloud();\r
- cloud.setX(Integer.parseInt(data[3]) / 5); // SCALE\r
- cloud.setY(Integer.parseInt(data[4]) / 5); // SCALE\r
- elementNumbers.put(data[1], cloud);\r
- model.addElement(cloud);\r
- } else if(data[0].equals("10") && data.length <= 15){\r
- // Some variable\r
- Element e = model.getElement(data[2].replace("\"", ""));\r
- if(e.getExpressions().get(0).getExpression().startsWith("INTEG (") && !(e instanceof Stock)) {\r
- // Stock\r
- Stock s = new Stock();\r
- s.setName(e.getName());\r
- s.setUnits(e.getUnits());\r
- s.setComments(e.getComments());\r
- s.setExpressions(e.getExpressions());\r
- model.removeElement(e);\r
- e = s;\r
- model.addElement(e);\r
+ i--;\r
+ \r
+ // Find the first variable that is connected to an empty valve\r
+ for(String[] connectionData : connections) {\r
+ if(!connectionData[9].equals("64"))\r
+ continue; // not dependency\r
+ String[] end = emptyValves.get(connectionData[3]);\r
+ if(end != null && elementNumbers.get(connectionData[3]) == null) {\r
+ // Use the connected element to create a valve and give it a name \r
+ Element start = elementNumbers.get(connectionData[2]);\r
+ if(start == null)\r
+ continue;\r
+ \r
+ Valve valve = new Valve();\r
+ valve.setName(start.getName() + " Rate");\r
+ valve.setX(Integer.parseInt(end[3]) / SCALE); \r
+ valve.setY(Integer.parseInt(end[4]) / SCALE);\r
+ \r
+ model.addElement(view, valve);\r
+ elementNumbers.put(connectionData[3], valve);\r
+ valve.setUnits("");\r
+ valve.setComments("");\r
+ }\r
}\r
- e.setX(Integer.parseInt(data[3]) / 5); // SCALE\r
- e.setY(Integer.parseInt(data[4]) / 5); // SCALE\r
- elementNumbers.put(data[1], e);\r
- } else if(data[0].equals("10") && data.length > 15){\r
- // TODO: Ghost\r
\r
- // for now, direct back to the original element\r
- Element originalElement = model.getElement(data[2].replace("\"", ""));\r
- if(originalElement != null)\r
- elementNumbers.put(data[1], originalElement);\r
- }\r
- }\r
-\r
- \r
- \r
- \r
- \r
- // Find the first variable that is connected to an empty valve\r
- for(String[] connectionData : connections) {\r
- if(!connectionData[9].equals("64"))\r
- continue; // not dependency\r
- String[] end = emptyValves.get(connectionData[3]);\r
- if(end != null && elementNumbers.get(connectionData[3]) == null) {\r
- // Use the connected element to create a valve and give it a name \r
- Element start = elementNumbers.get(connectionData[2]);\r
- if(start == null)\r
- continue;\r
\r
- Valve valve = new Valve();\r
- valve.setName(start.getName() + " Rate");\r
- valve.setX(Integer.parseInt(end[3]) / 5); // Scale\r
- valve.setY(Integer.parseInt(end[4]) / 5); // Scale\r
\r
- model.addElement(valve);\r
- elementNumbers.put(connectionData[3], valve);\r
- valve.setUnits("");\r
- valve.setComments("");\r
- }\r
- }\r
- \r
- \r
- \r
- for(String[] connectionData : connections) {\r
- Connection connection = new Connection();\r
- connection.setStart(elementNumbers.get(connectionData[2]));\r
- connection.setEnd(elementNumbers.get(connectionData[3]));\r
- // Discard connection if one of the ends is null\r
- if(connection.getStart() == null || connection.getEnd() == null)\r
- continue;\r
- \r
- if(connectionData[9].equals("64")) {\r
- // Dependency\r
- connection.setType(Connection.Type.DEPENDENCY);\r
- } else {\r
- // Flow\r
- connection.setType(Connection.Type.FLOW);\r
- if(connectionData[4].equals("100")) {\r
- // Flip the flow\r
- Element temp = connection.getStart();\r
- connection.setStart(connection.getEnd());\r
- connection.setEnd(temp);\r
- }\r
- }\r
- model.addConnection(connection);\r
- }\r
- \r
- \r
- // Generate expressions for empty valves \r
- for(String key : emptyValves.keySet()) {\r
- Element valve = elementNumbers.get(key);\r
- if(valve != null && valve.getExpressions().isEmpty()) {\r
- // Find the stock\r
- Element stock = null;\r
- for(Connection connection : valve.getConnections()) {\r
- if(!connection.getType().equals(Connection.Type.FLOW))\r
+ for(String[] connectionData : connections) {\r
+ \r
+ Element start = elementNumbers.get(connectionData[2]);\r
+ Element end = elementNumbers.get(connectionData[3]);\r
+ // Discard connection if one of the ends is null\r
+ if(start == null || end == null)\r
continue;\r
- if(connection.getStart().equals(valve) &&\r
- connection.getEnd() instanceof Stock) {\r
- stock = connection.getEnd();\r
- break;\r
+ \r
+ \r
+ Connection connection; \r
+ if(connectionData[9].equals("64")) {\r
+ // Dependency\r
+ connection = new Dependency();\r
+ } else {\r
+ // Flow\r
+ connection = new Flow();\r
+ if(connectionData[4].equals("100")) {\r
+ // Flip the flow\r
+ Element temp = connection.getStart();\r
+ connection.setStart(connection.getEnd());\r
+ connection.setEnd(temp);\r
+ }\r
}\r
+ connection.setStart(start);\r
+ connection.setEnd(end);\r
+ model.addConnection(connection);\r
}\r
\r
- // Create the expression. Use the expression of the stock, and undo the effect of other valves\r
- if(stock != null && stock instanceof Stock) {\r
- Expression expression = new Expression();\r
- \r
- StringBuilder sb = new StringBuilder();\r
- sb.append(((Stock)stock).getIntegralParts(stock.getExpressions().get(0))[0]);\r
- \r
- for(Connection c : stock.getConnections()) {\r
- if(c.getType().equals(Connection.Type.FLOW)) {\r
- if(c.getStart().equals(stock) && !c.getEnd().equals(valve)) {\r
- sb.append("+");\r
- sb.append(c.getEnd().getName());\r
- } else if(!c.getStart().equals(valve)){\r
- sb.append("-");\r
- sb.append(c.getStart().getName());\r
+ \r
+ // Generate expressions for empty valves \r
+ for(String key : emptyValves.keySet()) {\r
+ Element e = elementNumbers.get(key);\r
+ if(e instanceof Valve && ((Valve)e).getExpressions().isEmpty()) {\r
+ Valve valve = (Valve)e;\r
+ // Find the stock\r
+ Stock stock = null;\r
+ for(Connection connection : valve.getConnections()) {\r
+ if(!(connection instanceof Flow))\r
+ continue;\r
+ if(connection.getStart().equals(valve) &&\r
+ connection.getEnd() instanceof Stock) {\r
+ stock = (Stock)connection.getEnd();\r
+ break;\r
}\r
}\r
+ \r
+ // Create the expression. Use the expression of the stock, and undo the effect of other valves\r
+ if(stock != null && stock instanceof Stock) {\r
+ Expression expression = new Expression();\r
+ \r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append(((Stock)stock).getIntegralParts(stock.getExpressions().get(0))[0]);\r
+ \r
+ for(Connection c : stock.getConnections()) {\r
+ if(c instanceof Flow) {\r
+ if(c.getStart().equals(stock) && !c.getEnd().equals(valve)) {\r
+ sb.append("+");\r
+ sb.append(c.getEnd().getName());\r
+ } else if(!c.getStart().equals(valve)){\r
+ sb.append("-");\r
+ sb.append(c.getStart().getName());\r
+ }\r
+ }\r
+ }\r
+ expression.setExpression(sb.toString());\r
+ ArrayList<Expression> expressions = new ArrayList<Expression>();\r
+ expressions.add(expression);\r
+ valve.setExpressions(expressions);\r
+ }\r
}\r
- expression.setExpression(sb.toString());\r
- ArrayList<Expression> expressions = new ArrayList<Expression>();\r
- expressions.add(expression);\r
- valve.setExpressions(expressions);\r
}\r
}\r
+ i++;\r
}\r
+\r
}\r
\r
private static void getOthertData(Model model, String otherData) {\r
private static void setAllSubscripts(Model model) {\r
\r
// Set subscripts for all elements\r
- for(Element e : model.getElements()) { \r
- // Subscript definition can be in any expression\r
- for(Expression ex : e.getExpressions()) {\r
- if(ex.getRange() != null) {\r
- \r
- // Subscripts exist, check that subscripts -array is initialized\r
- if(e.getSubscripts() == null)\r
- e.setSubscripts(new ArrayList<Subscript>());\r
- \r
- String[] elements = ex.getRange().split(",");\r
- // Search the corresponding subscript for each element, if it has not been searched already\r
- for(int i = 0; i < elements.length; i++) {\r
- // The subscript has been defined, move to next\r
- if(e.getSubscripts().size() > i)\r
- continue;\r
- \r
- String element = elements[i].trim();\r
- for(Subscript sub : model.getSubscripts()) {\r
- if(sub.getName().equals(element)) {\r
- e.getSubscripts().add(sub);\r
- break;\r
- }\r
- for(String index : sub.getExpressions().get(0).getExpression().split(",")) {\r
- if(index.trim().equals(element)) {\r
- e.getSubscripts().add(sub);\r
- break;\r
- }\r
- }\r
- // Subscript was defined for this index in previous for-loop\r
- if(e.getSubscripts().size() == i + 1)\r
- break;\r
- }\r
- }\r
- }\r
- }\r
+ ArrayList<Element> elements = new ArrayList<Element>();\r
+ elements.addAll(model.getUnlocatedElements());\r
+ for(View view : model.getViews()) {\r
+ elements.addAll(view.getElements());\r
+ }\r
+ \r
+ for(Element e : elements) { \r
+ if(!(e instanceof Variable))\r
+ continue;\r
+ Variable v = (Variable)e;\r
+ v.initializeSubscripts(model);\r
}\r
}\r
\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.mdlImport.ImportUtils;\r
\r
-public class Auxiliary extends Element {\r
-\r
- @Override\r
- public void writeElement(WriteGraph graph, Resource configuration) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- createVariable(graph, configuration, sr.Auxiliary, sr.AuxiliarySymbol);\r
- }\r
+public class Auxiliary extends Variable {\r
\r
@Override\r
public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException {\r
return e;\r
}\r
\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, double xOffset,\r
+ double yOffset) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(!graph.isInstanceOf(parent, sr.Configuration))\r
+ return;\r
+ createVariable(graph, parent, sr.Auxiliary, sr.AuxiliarySymbol, xOffset, yOffset);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
}\r
public class Cloud extends Element {\r
\r
@Override\r
- public void writeElement(WriteGraph graph, Resource configuration) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Layer0 l0 = Layer0.getInstance(graph); \r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- DiagramResource dr = DiagramResource.getInstance(graph);\r
- G2DResource g2d = G2DResource.getInstance(graph);\r
- \r
- Resource cloud = GraphUtils.create2(graph, \r
- sr.Cloud);\r
- \r
- graph.claim(configuration, l0.ConsistsOf, cloud);\r
-\r
- Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
- \r
- Resource symbol = GraphUtils.create2(graph, \r
- sr.CloudSymbol,\r
- mr.ElementToComponent, cloud);\r
- \r
- double[] transform = {1.0, 0.0, 0.0, 1.0, getX(), getY()};\r
- graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform);\r
- \r
- OrderedSetUtils.add(graph, diagram, symbol);\r
- \r
- setResource(cloud);\r
- \r
+ public void write(WriteGraph graph, Resource parent, double xOffset,\r
+ double yOffset) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ if(!graph.isInstanceOf(parent, sr.Configuration))\r
+ return;\r
+ \r
+ Layer0 l0 = Layer0.getInstance(graph); \r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ G2DResource g2d = G2DResource.getInstance(graph);\r
+\r
+ Resource cloud = GraphUtils.create2(graph, \r
+ sr.Cloud);\r
+\r
+ graph.claim(parent, l0.ConsistsOf, cloud);\r
+\r
+ Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
+\r
+ Resource symbol = GraphUtils.create2(graph, \r
+ sr.CloudSymbol,\r
+ mr.ElementToComponent, cloud);\r
+\r
+ double[] transform = {1.0, 0.0, 0.0, 1.0, getX() + xOffset, getY() + yOffset};\r
+ graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform);\r
+\r
+ OrderedSetUtils.add(graph, diagram, symbol);\r
+\r
+ setResource(cloud);\r
+\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
}\r
\r
}\r
import org.simantics.structural.stubs.StructuralResource2;\r
import org.simantics.sysdyn.SysdynResource;\r
\r
-public class Connection {\r
- public static enum Type {DEPENDENCY, FLOW};\r
+public abstract class Connection implements IWriteableMDLObject {\r
+ protected Element start, end;\r
\r
- private Type type;\r
- private Element start, end;\r
- \r
- public void writeConnection(WriteGraph graph, Resource configuration) throws DatabaseException {\r
+ public Resource writeConnection(WriteGraph graph, Resource configuration, Resource connectionType, Resource connectionSymbol) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
Layer0 l0 = Layer0.getInstance(graph); \r
ModelingResources mr = ModelingResources.getInstance(graph);\r
DiagramResource dr = DiagramResource.getInstance(graph);\r
StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
\r
- \r
- Resource connectionType = sr.Dependency;\r
- Resource connectionSymbol = sr.DependencyConnection;\r
- if(Type.FLOW.equals(type)) {\r
- connectionType = sr.Flow;\r
- connectionSymbol = sr.FlowConnection;\r
- }\r
- \r
// Build the connection to configuration\r
Resource connection = GraphUtils.create2(graph, \r
connectionType,\r
mr.DiagramConnectionToConnection, connection,\r
dr.HasArrowConnector, headConnector,\r
dr.HasPlainConnector, tailConnector);\r
-\r
- if(Type.DEPENDENCY.equals(type)) {\r
- graph.claimLiteral(diagramConnection, sr.angle, -0.1);\r
- }\r
\r
Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
\r
OrderedSetUtils.add(graph, diagram, diagramConnection);\r
- }\r
-\r
- public Type getType() {\r
- return type;\r
- }\r
-\r
- public void setType(Type type) {\r
- this.type = type;\r
+ \r
+ return connection;\r
}\r
\r
public Element getStart() {\r
public void setEnd(Element end) {\r
this.end = end;\r
}\r
- \r
- \r
}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
+public class Dependency extends Connection {\r
+\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent) {\r
+ try {\r
+ System.out.println("DEPENDENCY:");\r
+ System.out.println(start.getName() + " => " + end.getName());\r
+ System.out.println("==============================================");\r
+ if(start.getName().equals("Kotihoidon hoitopaikat") && end.getName().equals("Kotihoidon vuosittaiset kustannukset")) {\r
+ System.out.println("MORO");\r
+ }\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Resource connection = writeConnection(graph, parent, sr.Dependency, sr.DependencyConnection);\r
+ \r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ Resource diagramConnection = graph.getSingleObject(connection, mr.ConnectionToDiagramConnection);\r
+ graph.claimLiteral(diagramConnection, sr.angle, -0.1);\r
+ \r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+\r
+}\r
package org.simantics.sysdyn.mdlImport.mdlElements;\r
\r
import java.util.ArrayList;\r
-import java.util.Iterator;\r
\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.stubs.G2DResource;\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.SysdynResource;\r
-import org.simantics.sysdyn.mdlImport.ImportUtils;\r
\r
-public abstract class Element {\r
+public abstract class Element implements IWriteableMDLElement {\r
\r
- public static enum Type {AUXILIARY, STOCK, VALVE, CLOUD, SUBSCRIPT};\r
-\r
protected int x, y;\r
- protected String name;\r
- protected String units;\r
- protected String comments;\r
- protected ArrayList<Expression> expressions;\r
- protected ArrayList<Subscript> subscripts;\r
protected Resource resource;\r
+ protected String name;\r
protected ArrayList<Connection> connections;\r
\r
- private Double rangeStart, rangeEnd, rangeStep;\r
- \r
- \r
- public abstract void writeElement(WriteGraph graph, Resource configuration) throws DatabaseException;\r
- \r
protected Resource getExpression(WriteGraph graph, Expression expression)\r
throws DatabaseException {\r
return null;\r
} \r
\r
- protected void createVariable(WriteGraph graph, Resource configuration, Resource variableType, Resource symbolType) throws DatabaseException {\r
- System.out.println("ELEMENT: " + name);\r
- if("Tyƶvoiman poisto Rate".equals(getName())) {\r
- System.out.println("STOP");\r
- }\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Layer0 l0 = Layer0.getInstance(graph); \r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- DiagramResource dr = DiagramResource.getInstance(graph);\r
- G2DResource g2d = G2DResource.getInstance(graph);\r
- \r
- System.out.println("Expressions");\r
- Resource expressionList = OrderedSetUtils.create(graph, sr.Expressions);\r
- \r
- // Make sure at least one expression exist\r
- if(getExpressions().isEmpty()) {\r
- Expression e = new Expression();\r
- e.setExpression("");\r
- getExpressions().add(e);\r
- }\r
- \r
- for(Expression e : getExpressions()) {\r
- \r
- // Get expression from the variable. They have different types\r
- Resource expression = getExpression(graph, e);\r
- \r
- if(e.getRange() != null) {\r
- graph.claimLiteral(expression, sr.HasArrayRange, "[" + e.getRange().trim() + "]");\r
- }\r
- OrderedSetUtils.add(graph, expressionList, expression);\r
- }\r
- \r
- Resource arrayIndexList = OrderedSetUtils.create(graph, sr.ArrayIndexes);\r
- \r
-\r
- System.out.println("Variable");\r
- Resource variable = GraphUtils.create2(graph, \r
- variableType,\r
- l0.HasName, ImportUtils.escapeName(name),\r
- sr.HasExpressions, expressionList);\r
- graph.claim(variable, mr.Mapped, variable);\r
- \r
- \r
- System.out.println("Subscripts");\r
- if(subscripts != null) {\r
- for(Subscript sub : subscripts) {\r
- OrderedSetUtils.add(graph, arrayIndexList, sub.getResource());\r
- }\r
- graph.claim(variable, sr.HasArrayIndexes, arrayIndexList);\r
- }\r
- \r
- if(units != null && units.length() > 0)\r
- graph.claimLiteral(variable, sr.HasUnit, units);\r
- if(comments != null && comments.length() > 0)\r
- graph.claimLiteral(variable, l0.HasDescription, comments);\r
- if(rangeStart != null)\r
- graph.claimLiteral(variable, sr.HasRangeStart, rangeStart);\r
- if(rangeEnd != null)\r
- graph.claimLiteral(variable, sr.HasRangeEnd, rangeEnd);\r
- if(rangeStep != null)\r
- graph.claimLiteral(variable, sr.HasRangeStep, rangeStep);\r
- \r
- graph.claim(configuration, l0.ConsistsOf, variable);\r
- \r
- System.out.println("Diagram");\r
-\r
- Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
- \r
- Resource symbol = GraphUtils.create2(graph, \r
- symbolType,\r
- mr.ElementToComponent, variable);\r
- \r
- double[] transform = {1.0, 0.0, 0.0, 1.0, x, y};\r
- graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform);\r
- \r
- OrderedSetUtils.add(graph, diagram, symbol);\r
- \r
- resource = variable;\r
- System.out.println("===================================================");\r
- }\r
- \r
- \r
- @Override\r
- public String toString() {\r
- StringBuilder sb = new StringBuilder();\r
- sb.append(this.getClass().getSimpleName().toString());\r
- sb.append(": ");\r
- if(name != null) \r
- sb.append(name);\r
- sb.append(", ");\r
-\r
- if(units != null) \r
- sb.append(units);\r
- sb.append(", ");\r
-\r
- if(comments != null) \r
- sb.append(comments);\r
- sb.append(", ");\r
-\r
- if(expressions != null) {\r
- Iterator<Expression> i = expressions.iterator();\r
- while(i.hasNext()) {\r
- Expression e = i.next();\r
- sb.append(e.toString());\r
- if(i.hasNext())\r
- sb.append(", ");\r
- }\r
- }\r
- return sb.toString();\r
- }\r
-\r
public int getX() {\r
return x;\r
}\r
this.name = name;\r
}\r
\r
- public String getUnits() {\r
- return units;\r
- }\r
-\r
- public void setUnits(String units) {\r
- this.units = units;\r
- }\r
-\r
- public String getComments() {\r
- return comments;\r
- }\r
-\r
- public void setComments(String comments) {\r
- this.comments = comments;\r
- }\r
-\r
- public ArrayList<Expression> getExpressions() {\r
- if(expressions == null) {\r
- expressions = new ArrayList<Expression>();\r
- }\r
- return expressions;\r
- }\r
-\r
- public void setExpressions(ArrayList<Expression> expressions) {\r
- this.expressions = expressions;\r
- }\r
-\r
- public ArrayList<Subscript> getSubscripts() {\r
- if(subscripts == null)\r
- subscripts = new ArrayList<Subscript>();\r
- return subscripts;\r
- }\r
-\r
- public void setSubscripts(ArrayList<Subscript> subscripts) {\r
- this.subscripts = subscripts;\r
- }\r
-\r
public Resource getResource() {\r
return resource;\r
}\r
public void setResource(Resource resource) {\r
this.resource = resource;\r
}\r
-\r
- public Double getRangeStart() {\r
- return rangeStart;\r
- }\r
-\r
- public void setRangeStart(Double rangeStart) {\r
- this.rangeStart = rangeStart;\r
- }\r
-\r
- public Double getRangeEnd() {\r
- return rangeEnd;\r
- }\r
-\r
- public void setRangeEnd(Double rangeEnd) {\r
- this.rangeEnd = rangeEnd;\r
- }\r
-\r
- public Double getRangeStep() {\r
- return rangeStep;\r
- }\r
-\r
- public void setRangeStep(Double rangeStep) {\r
- this.rangeStep = rangeStep;\r
- }\r
-\r
+ \r
public ArrayList<Connection> getConnections() {\r
if(connections == null)\r
connections = new ArrayList<Connection>();\r
public void addConnection(Connection connection) {\r
getConnections().add(connection);\r
}\r
- \r
- \r
- \r
}\r
\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\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
+\r
+public class Flow extends Connection {\r
+\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ writeConnection(graph, parent, sr.Flow, sr.FlowConnection);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+\r
+public interface IWriteableMDLElement {\r
+\r
+ /**\r
+ * Writes an element with coordinates (variable, cloud) to the given resource.\r
+ * \r
+ * Offsets determine where the parent view is located in the combined diagram\r
+ * \r
+ * @param graph WriteGraph\r
+ * @param parent The resource where the object is located\r
+ * @param xOffset xOffset of the view in the diagram\r
+ * @param yOffset yOffset of the view in the diagram\r
+ */\r
+ public void write(WriteGraph graph, Resource parent, double xOffset, double yOffset);\r
+}
\ No newline at end of file
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+\r
+public interface IWriteableMDLObject {\r
+ \r
+ /**\r
+ * Writes an object with no coordinates (connection, model, view) to the given resource\r
+ * \r
+ * @param graph WriteGraph\r
+ * @param parent The resource where the object is located\r
+ */\r
+ public void write(WriteGraph graph, Resource parent);\r
+}\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.modeling.ModelingResources;\r
\r
-public class Model {\r
+public class Model implements IWriteableMDLObject {\r
\r
private String name, timeUnit, saveper;\r
private double startTime, endTime, timeStep;\r
\r
private HashMap<String, Element> elementMap = new HashMap<String, Element>();\r
- private ArrayList<Element> elements = new ArrayList<Element>();\r
private ArrayList<Subscript> subscripts = new ArrayList<Subscript>();\r
private ArrayList<Connection> connections = new ArrayList<Connection>();\r
+ private ArrayList<View> views = new ArrayList<View>();\r
+ private ArrayList<Element> unlocatedElements = new ArrayList<Element>();\r
\r
public void addElement(Element element) {\r
if(element instanceof Subscript)\r
- subscripts.add((Subscript)element);\r
+ addSubscript((Subscript)element);\r
else\r
- elements.add(element);\r
+ unlocatedElements.add(element);\r
if(element.getName() != null)\r
elementMap.put(element.getName(), element);\r
}\r
\r
+ public void addSubscript(Subscript subscript) {\r
+ subscripts.add(subscript);\r
+ }\r
+ \r
+ public void addElement(View view, Element element) {\r
+ if(element instanceof Subscript)\r
+ addSubscript((Subscript)element);\r
+ else {\r
+ if(unlocatedElements.contains(element))\r
+ unlocatedElements.remove(element);\r
+ view.addElement(element);\r
+ }\r
+ if(element.getName() != null)\r
+ elementMap.put(element.getName(), element);\r
+ }\r
+ \r
+ public void relocateElement(View view, Element element) {\r
+ if(unlocatedElements.contains(element))\r
+ unlocatedElements.remove(element);\r
+ for(View v : views) {\r
+ if(v.getElements().contains(element))\r
+ v.getElements().remove(element);\r
+ }\r
+ view.addElement(element);\r
+ }\r
+ \r
public void removeElement(Element element) {\r
- elements.remove(element);\r
+ if(unlocatedElements.contains(element))\r
+ unlocatedElements.remove(element);\r
\r
+ for(View view : views) {\r
+ if(view.getElements().contains(element)) {\r
+ view.getElements().remove(element);\r
+ }\r
+ }\r
+ \r
+ // just to be sure: loop the whole elementMap and don't trust the element's name\r
String toBeRemoved = null;\r
for(String key : elementMap.keySet()) {\r
if(element.equals(elementMap.get(key))) {\r
elementMap.remove(toBeRemoved);\r
}\r
\r
- public ArrayList<Element> getElements() {\r
- return elements;\r
+ public ArrayList<Element> getUnlocatedElements() {\r
+ return unlocatedElements;\r
}\r
\r
public Element getElement(String name) {\r
public ArrayList<Subscript> getSubscripts() {\r
return subscripts;\r
}\r
- \r
- public void writeToProject(WriteGraph graph, Resource project) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- DiagramResource dr = DiagramResource.getInstance(graph);\r
- SimulationResource simu = SimulationResource.getInstance(graph);\r
- \r
- // Diagram creation copied from SysdynProject. \r
- // Could the same code be reused? \r
- Resource model = GraphUtils.create2(\r
- graph,\r
- sr.SysdynModel,\r
- l0.PartOf, project,\r
- l0.HasName, name,\r
- l0.HasLabel, name,\r
- sr.HasStartTime, startTime,\r
- sr.HasStopTime, endTime \r
- );\r
-\r
- Resource diagram = OrderedSetUtils.create(graph, sr.ConfigurationDiagram);\r
- GraphLayer l = new GraphLayerUtil(graph).createLayer(DiagramConstants.DEFAULT_LAYER_NAME, true);\r
- graph.claim(diagram, dr.HasLayer, l.getLayer());\r
- graph.claimLiteral(diagram, l0.HasName, "Diagrammi", Bindings.STRING);\r
-\r
- Resource conf = GraphUtils.create2(graph,\r
- sr.Configuration,\r
- l0.PartOf, model,\r
- l0.HasName, name\r
- );\r
-\r
- graph.claim(conf, mr.CompositeToDiagram, diagram);\r
- graph.claim(model, simu.HasConfiguration, conf);\r
-\r
- Resource mapping = graph.newResource();\r
- graph.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
- graph.claim(diagram, l0.HasTrigger, mapping);\r
-\r
- Resource report = GraphUtils.create2(graph, l0.Report, l0.HasDocumentation, "===Report===");\r
-\r
- GraphUtils.create2(graph, simu.Experiment,\r
- l0.HasName, "Experiment",\r
- l0.HasLabel, "Experiment",\r
- l0.HasReportFactory, report,\r
- l0.PartOf, model);\r
- \r
- for(Subscript s : subscripts) {\r
- s.writeElement(graph, conf);\r
- }\r
- \r
- for(Element e : elements) {\r
- e.writeElement(graph, conf);\r
- }\r
- \r
- for(Connection c : connections) {\r
- c.writeConnection(graph, conf);\r
- }\r
- \r
- \r
- }\r
\r
public String getName() {\r
return name;\r
this.elementMap = elementMap;\r
}\r
\r
- public void setElements(ArrayList<Element> elements) {\r
- this.elements = elements;\r
- }\r
-\r
public void setSubscripts(ArrayList<Subscript> subscripts) {\r
this.subscripts = subscripts;\r
}\r
this.connections = connections;\r
}\r
\r
+ public void addView(View view) {\r
+ views.add(view);\r
+ }\r
+ \r
+ public ArrayList<View> getViews() {\r
+ return views;\r
+ }\r
+\r
+ /**\r
+ * Write the model to a project\r
+ * @param graph WriteGraph\r
+ * @param parent Project resource\r
+ */\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ SimulationResource simu = SimulationResource.getInstance(graph);\r
+\r
+ // Diagram creation copied from SysdynProject. \r
+ // Could the same code be reused? \r
+ Resource model = GraphUtils.create2(\r
+ graph,\r
+ sr.SysdynModel,\r
+ l0.PartOf, parent,\r
+ l0.HasName, name,\r
+ l0.HasLabel, name,\r
+ sr.HasStartTime, startTime,\r
+ sr.HasStopTime, endTime \r
+ );\r
+\r
+ Resource diagram = OrderedSetUtils.create(graph, sr.ConfigurationDiagram);\r
+ GraphLayer l = new GraphLayerUtil(graph).createLayer(DiagramConstants.DEFAULT_LAYER_NAME, true);\r
+ graph.claim(diagram, dr.HasLayer, l.getLayer());\r
+ graph.claimLiteral(diagram, l0.HasName, "Diagrammi", Bindings.STRING);\r
+\r
+ Resource conf = GraphUtils.create2(graph,\r
+ sr.Configuration,\r
+ l0.PartOf, model,\r
+ l0.HasName, name\r
+ );\r
+\r
+ graph.claim(conf, mr.CompositeToDiagram, diagram);\r
+ graph.claim(model, simu.HasConfiguration, conf);\r
+\r
+ Resource mapping = graph.newResource();\r
+ graph.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
+ graph.claim(diagram, l0.HasTrigger, mapping);\r
+\r
+ Resource report = GraphUtils.create2(graph, l0.Report, l0.HasDocumentation, "===Report===");\r
+\r
+ GraphUtils.create2(graph, simu.Experiment,\r
+ l0.HasName, "Experiment",\r
+ l0.HasLabel, "Experiment",\r
+ l0.HasReportFactory, report,\r
+ l0.PartOf, model);\r
+\r
+ for(Subscript s : subscripts) {\r
+ s.write(graph, conf, 0, 0);\r
+ }\r
+\r
+ // Create the grid n*n of views:\r
+ \r
+ double n = Math.sqrt(views.size());\r
+ n = Math.ceil(n);\r
+ \r
+ int width = 0, height = 0;\r
+ for(View v : views) {\r
+ if(v.getWidth() > width)\r
+ width = v.getWidth();\r
+ if(v.getHeight() > height)\r
+ height = v.getHeight();\r
+ }\r
+ // FIXME: INDEXING\r
+ for(int i = 0; i < n; i++) {\r
+ for(int j = 0; j < n; j++) {\r
+ if(i * (int)n + j < views.size()) {\r
+ View v = views.get(i * (int)n + j);\r
+ v.setxOffset(width * j);\r
+ v.setyOffset(height * i);\r
+ v.write(graph, conf);\r
+ }\r
+ }\r
+ }\r
+ \r
+ for(Element e : unlocatedElements) {\r
+ e.write(graph, conf, 0, 0);\r
+ }\r
+\r
+ for(Connection c : connections) {\r
+ c.write(graph, conf);\r
+ }\r
+\r
+\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
\r
}\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.exception.DatabaseException;\r
\r
-public class ModelControl extends Element {\r
+public class ModelControl extends Variable {\r
+\r
\r
- @Override\r
- public void writeElement(WriteGraph graph, Resource configuration) {\r
- \r
- }\r
\r
@Override\r
public Resource getExpression(WriteGraph graph, Expression expression)\r
return null;\r
}\r
\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, double xOffset,\r
+ double yOffset) {\r
+ // TODO Auto-generated method stub\r
+ \r
+ }\r
+\r
}\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.mdlImport.ImportUtils;\r
\r
-public class Stock extends Element {\r
-\r
- @Override\r
- public void writeElement(WriteGraph graph, Resource configuration) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- createVariable(graph, configuration, sr.Stock, sr.StockSymbol);\r
- }\r
+public class Stock extends Variable {\r
\r
@Override\r
public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { \r
return new String[] {integral, initialEquation};\r
}\r
\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, double xOffset,\r
+ double yOffset) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(!graph.isInstanceOf(parent, sr.Configuration))\r
+ return;\r
+ createVariable(graph, parent, sr.Stock, sr.StockSymbol, xOffset, yOffset);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+\r
}\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.sysdyn.SysdynResource;\r
\r
-public class Subscript extends Element {\r
-\r
- @Override\r
- public void writeElement(WriteGraph graph, Resource configuration) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Layer0 l0 = Layer0.getInstance(graph); \r
- Resource enumerationIndexes = OrderedSetUtils.create(graph, sr.EnumerationIndexes);\r
- \r
- if(expressions != null && expressions.get(0) != null) {\r
- String[] indexes = expressions.get(0).getExpression().split(",");\r
- for(String s : indexes) {\r
- Resource ei = GraphUtils.create2(graph, \r
- sr.EnumerationIndex,\r
- l0.HasName, s.trim());\r
- OrderedSetUtils.add(graph, enumerationIndexes, ei);\r
- }\r
- }\r
- \r
- Resource enumeration = GraphUtils.create2(graph, \r
- sr.Enumeration,\r
- l0.HasName, name,\r
- sr.HasEnumerationIndexes, enumerationIndexes);\r
- \r
- graph.claim(configuration, l0.ConsistsOf, enumeration);\r
- \r
- resource = enumeration;\r
- }\r
+public class Subscript extends Variable {\r
\r
@Override\r
public Resource getExpression(WriteGraph graph, Expression expression)\r
return null;\r
}\r
\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, double xOffset,\r
+ double yOffset) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(!graph.isInstanceOf(parent, sr.Configuration))\r
+ return;\r
+ Layer0 l0 = Layer0.getInstance(graph); \r
+ Resource enumerationIndexes = OrderedSetUtils.create(graph, sr.EnumerationIndexes);\r
+\r
+ if(expressions != null && expressions.get(0) != null) {\r
+ String[] indexes = expressions.get(0).getExpression().split(",");\r
+ for(String s : indexes) {\r
+ Resource ei = GraphUtils.create2(graph, \r
+ sr.EnumerationIndex,\r
+ l0.HasName, s.trim());\r
+ OrderedSetUtils.add(graph, enumerationIndexes, ei);\r
+ }\r
+ }\r
+\r
+ Resource enumeration = GraphUtils.create2(graph, \r
+ sr.Enumeration,\r
+ l0.HasName, name,\r
+ sr.HasEnumerationIndexes, enumerationIndexes);\r
+\r
+ graph.claim(parent, l0.ConsistsOf, enumeration);\r
+\r
+ resource = enumeration;\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
}\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.mdlImport.ImportUtils;\r
\r
-public class Valve extends Element {\r
-\r
- @Override\r
- public void writeElement(WriteGraph graph, Resource configuration) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- createVariable(graph, configuration, sr.Valve, sr.ValveSymbol);\r
- }\r
+public class Valve extends Variable {\r
\r
@Override\r
public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException {\r
sr.HasEquation, expressionString.trim());\r
return e;\r
}\r
+\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent, double xOffset,\r
+ double yOffset) {\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(!graph.isInstanceOf(parent, sr.Configuration))\r
+ return;\r
+ createVariable(graph, parent, sr.Valve, sr.ValveSymbol, xOffset, yOffset);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.stubs.G2DResource;\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.SysdynResource;\r
+import org.simantics.sysdyn.mdlImport.ImportUtils;\r
+\r
+public abstract class Variable extends Element {\r
+ protected String units;\r
+ protected String comments;\r
+ protected ArrayList<Expression> expressions;\r
+ protected ArrayList<Subscript> subscripts;\r
+\r
+ private Double rangeStart, rangeEnd, rangeStep;\r
+ \r
+ protected void createVariable(WriteGraph graph, Resource configuration, Resource variableType, Resource symbolType, double xOffset, double yOffset) throws DatabaseException {\r
+ System.out.println("ELEMENT: " + name);\r
+ if("Tyƶvoiman poisto Rate".equals(getName())) {\r
+ System.out.println("STOP");\r
+ }\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph); \r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ G2DResource g2d = G2DResource.getInstance(graph);\r
+ \r
+ System.out.println("Expressions");\r
+ Resource expressionList = OrderedSetUtils.create(graph, sr.Expressions);\r
+ \r
+ // Make sure at least one expression exist\r
+ if(getExpressions().isEmpty()) {\r
+ Expression e = new Expression();\r
+ e.setExpression("");\r
+ getExpressions().add(e);\r
+ }\r
+ \r
+ for(Expression e : getExpressions()) {\r
+ \r
+ // Get expression from the variable. They have different types\r
+ Resource expression = getExpression(graph, e);\r
+ \r
+ if(e.getRange() != null) {\r
+ graph.claimLiteral(expression, sr.HasArrayRange, "[" + e.getRange().trim() + "]");\r
+ }\r
+ OrderedSetUtils.add(graph, expressionList, expression);\r
+ }\r
+ \r
+ Resource arrayIndexList = OrderedSetUtils.create(graph, sr.ArrayIndexes);\r
+ \r
+\r
+ System.out.println("Variable");\r
+ Resource variable = GraphUtils.create2(graph, \r
+ variableType,\r
+ l0.HasName, ImportUtils.escapeName(name),\r
+ sr.HasExpressions, expressionList);\r
+ graph.claim(variable, mr.Mapped, variable);\r
+ \r
+ \r
+ System.out.println("Subscripts");\r
+ if(subscripts != null) {\r
+ for(Subscript sub : subscripts) {\r
+ OrderedSetUtils.add(graph, arrayIndexList, sub.getResource());\r
+ }\r
+ graph.claim(variable, sr.HasArrayIndexes, arrayIndexList);\r
+ }\r
+ \r
+ if(units != null && units.length() > 0)\r
+ graph.claimLiteral(variable, sr.HasUnit, units);\r
+ if(comments != null && comments.length() > 0)\r
+ graph.claimLiteral(variable, l0.HasDescription, comments);\r
+ if(rangeStart != null)\r
+ graph.claimLiteral(variable, sr.HasRangeStart, rangeStart);\r
+ if(rangeEnd != null)\r
+ graph.claimLiteral(variable, sr.HasRangeEnd, rangeEnd);\r
+ if(rangeStep != null)\r
+ graph.claimLiteral(variable, sr.HasRangeStep, rangeStep);\r
+ \r
+ graph.claim(configuration, l0.ConsistsOf, variable);\r
+ \r
+ System.out.println("Diagram");\r
+\r
+ Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram);\r
+ \r
+ Resource symbol = GraphUtils.create2(graph, \r
+ symbolType,\r
+ mr.ElementToComponent, variable);\r
+ \r
+ double[] transform = {1.0, 0.0, 0.0, 1.0, x + xOffset, y + yOffset};\r
+ graph.claimLiteral(symbol, dr.HasTransform, g2d.Transform, transform);\r
+ \r
+ OrderedSetUtils.add(graph, diagram, symbol);\r
+ \r
+ resource = variable;\r
+ System.out.println("===================================================");\r
+ }\r
+ \r
+ \r
+ public String getUnits() {\r
+ return units;\r
+ }\r
+\r
+ public void setUnits(String units) {\r
+ this.units = units;\r
+ }\r
+\r
+ public String getComments() {\r
+ return comments;\r
+ }\r
+\r
+ public void setComments(String comments) {\r
+ this.comments = comments;\r
+ }\r
+\r
+ public ArrayList<Expression> getExpressions() {\r
+ if(expressions == null) {\r
+ expressions = new ArrayList<Expression>();\r
+ }\r
+ return expressions;\r
+ }\r
+\r
+ public void setExpressions(ArrayList<Expression> expressions) {\r
+ this.expressions = expressions;\r
+ }\r
+\r
+ public ArrayList<Subscript> getSubscripts() {\r
+ if(subscripts == null)\r
+ subscripts = new ArrayList<Subscript>();\r
+ return subscripts;\r
+ }\r
+\r
+ public void setSubscripts(ArrayList<Subscript> subscripts) {\r
+ this.subscripts = subscripts;\r
+ }\r
+ \r
+\r
+ public Double getRangeStart() {\r
+ return rangeStart;\r
+ }\r
+\r
+ public void setRangeStart(Double rangeStart) {\r
+ this.rangeStart = rangeStart;\r
+ }\r
+\r
+ public Double getRangeEnd() {\r
+ return rangeEnd;\r
+ }\r
+\r
+ public void setRangeEnd(Double rangeEnd) {\r
+ this.rangeEnd = rangeEnd;\r
+ }\r
+\r
+ public Double getRangeStep() {\r
+ return rangeStep;\r
+ }\r
+\r
+ public void setRangeStep(Double rangeStep) {\r
+ this.rangeStep = rangeStep;\r
+ }\r
+ \r
+ /**\r
+ * Use this to set subscripts after all elements have been read\r
+ * \r
+ * @param model The model where the variable is located\r
+ */\r
+ public void initializeSubscripts(Model model) {\r
+ for(Expression ex : getExpressions()) {\r
+ if(ex.getRange() != null) {\r
+ \r
+ // Subscripts exist, check that subscripts -array is initialized\r
+ getSubscripts();\r
+ \r
+ String[] elements = ex.getRange().split(",");\r
+ // Search the corresponding subscript for each element, if it has not been searched already\r
+ for(int i = 0; i < elements.length; i++) {\r
+ // The subscript has been defined, move to next\r
+ if(subscripts.size() > i)\r
+ continue;\r
+ \r
+ String element = elements[i].trim();\r
+ for(Subscript sub : model.getSubscripts()) {\r
+ if(sub.getName().equals(element)) {\r
+ subscripts.add(sub);\r
+ break;\r
+ }\r
+ for(String index : sub.getExpressions().get(0).getExpression().split(",")) {\r
+ if(index.trim().equals(element)) {\r
+ subscripts.add(sub);\r
+ break;\r
+ }\r
+ }\r
+ // Subscript was defined for this index in previous for-loop\r
+ if(subscripts.size() == i + 1)\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+\r
+public class View implements IWriteableMDLObject {\r
+ \r
+ private int minX = 0, maxX = 0, minY = 0, maxY = 0;\r
+ private double xOffset = 0, yOffset = 0;\r
+ private String name, fontParameters;\r
+ \r
+ private ArrayList<Element> elements = new ArrayList<Element>();\r
+ \r
+\r
+ @Override\r
+ public void write(WriteGraph graph, Resource parent) {\r
+ xOffset = xOffset - minX;\r
+ yOffset = yOffset - minY;\r
+ for(Element e : elements) {\r
+ e.write(graph, parent, xOffset, yOffset);\r
+ }\r
+ }\r
+ \r
+ public void addElement(Element e) {\r
+ if(e.getX()<minX)\r
+ minX = e.getX();\r
+ if(e.getX()>maxX)\r
+ maxX = e.getX();\r
+ if(e.getY()<minY)\r
+ minY = e.getY();\r
+ if(e.getY()>maxY)\r
+ maxY = e.getY();\r
+ this.elements.add(e);\r
+ }\r
+ \r
+ public ArrayList<Element> getElements() {\r
+ return elements;\r
+ }\r
+\r
+ public void setElements(ArrayList<Element> elements) {\r
+ this.elements = elements;\r
+ }\r
+ \r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ public void setName(String name) {\r
+ this.name = name;\r
+ }\r
+\r
+\r
+ public String getFontParameters() {\r
+ return fontParameters;\r
+ }\r
+\r
+\r
+ public void setFontParameters(String fontParameters) {\r
+ this.fontParameters = fontParameters;\r
+ }\r
+\r
+\r
+ public int getMinX() {\r
+ return minX;\r
+ }\r
+\r
+ public void setMinX(int minX) {\r
+ this.minX = minX;\r
+ }\r
+\r
+ public int getMaxX() {\r
+ return maxX;\r
+ }\r
+\r
+ public void setMaxX(int maxX) {\r
+ this.maxX = maxX;\r
+ }\r
+\r
+ public int getMinY() {\r
+ return minY;\r
+ }\r
+\r
+ public void setMinY(int minY) {\r
+ this.minY = minY;\r
+ }\r
+\r
+ public int getMaxY() {\r
+ return maxY;\r
+ }\r
+\r
+ public void setMaxY(int maxY) {\r
+ this.maxY = maxY;\r
+ }\r
+\r
+ public double getxOffset() {\r
+ return xOffset;\r
+ }\r
+\r
+ public void setxOffset(double xOffset) {\r
+ this.xOffset = xOffset;\r
+ }\r
+\r
+ public double getyOffset() {\r
+ return yOffset;\r
+ }\r
+\r
+ public void setyOffset(double yOffset) {\r
+ this.yOffset = yOffset;\r
+ }\r
+ \r
+ public int getWidth() {\r
+ return maxX - minX;\r
+ }\r
+ \r
+ public int getHeight() {\r
+ return maxY - minY;\r
+ }\r
+}\r