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