From 5332f4fac7fafa9ca25cf0db3fd3857a14aced30 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 16 Feb 2011 13:56:53 +0000 Subject: [PATCH] First version of vensim import git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19791 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/handlers/ImportMdlHandler.java | 65 +++ .../sysdyn/mdlImport/ImportUtils.java | 103 ++++ .../simantics/sysdyn/mdlImport/MdlFile.java | 58 +++ .../simantics/sysdyn/mdlImport/MdlParser.java | 487 ++++++++++++++++++ .../mdlImport/mdlElements/Auxiliary.java | 38 ++ .../sysdyn/mdlImport/mdlElements/Cloud.java | 55 ++ .../mdlImport/mdlElements/Connection.java | 108 ++++ .../sysdyn/mdlImport/mdlElements/Element.java | 273 ++++++++++ .../mdlImport/mdlElements/Expression.java | 39 ++ .../sysdyn/mdlImport/mdlElements/Model.java | 222 ++++++++ .../mdlImport/mdlElements/ModelControl.java | 31 ++ .../sysdyn/mdlImport/mdlElements/Stock.java | 67 +++ .../mdlImport/mdlElements/Subscript.java | 56 ++ .../sysdyn/mdlImport/mdlElements/Valve.java | 38 ++ 14 files changed, 1640 insertions(+) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportMdlHandler.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlFile.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/ModelControl.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Valve.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 new file mode 100644 index 00000000..0e72f961 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportMdlHandler.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.ui.handlers; + +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; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.db.Resource; +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.ui.SimanticsUI; + +public class ImportMdlHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + final Resource project = SimanticsUI.getProject().get(); + if(project == null) return null; + + Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.OPEN); + fd.setText("Import .mdl"); + fd.setFilterPath("D:\\DATA\\Openprod\\vensim-malleja"); + String[] filterExt = {"*.mdl"}; + fd.setFilterExtensions(filterExt); + String selected = fd.open(); + if(selected == null) return null; + + File file = new File(selected); + if(!file.isFile()) return null; + + final Model model = MdlParser.parse(file); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + model.writeToProject(graph, project); + } + }); + + return null; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java new file mode 100644 index 00000000..d04e9dfe --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/ImportUtils.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * 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; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ImportUtils { + + public static String escapeExpression(String string) { + string = string.replace("Ä", "A"); + string = string.replace("ä", "a"); + string = string.replace("ö", "o"); + string = string.replace("Ö", "O"); + string = string.replace("\\", "\n"); + + StringBuilder sb = new StringBuilder(); + + if(string.contains("\"")) { + boolean startedQuote = false; + StringBuilder var = new StringBuilder();; + for(char c : string.toCharArray()) { + if(c == '"') { + if(!startedQuote) { + startedQuote = true; + var = new StringBuilder(); + break; + } else { + startedQuote = false; + sb.append(var.toString().replaceAll("[^a-zA-Z 0-9]+", "")); + } + } + if(startedQuote) { + var.append(c); + } else { + sb.append(c); + } + } + + string = sb.toString(); + } + + string = toCamelCase(string); + + return string; + + } + + public static String escapeName(String string) { + string = string.replace("Ä", "A"); + string = string.replace("ä", "a"); + string = string.replace("ö", "o"); + string = string.replace("Ö", "O"); + string = string.replaceAll("[^a-zA-Z 0-9]+", ""); + return toCamelCase(string); + } + + + public static String toCamelCase(String string) { + if(string.length() < 1) { + return string; + } + + try { + String result = new String(string); + BufferedReader br = new BufferedReader(new StringReader(string)); + Pattern p = Pattern.compile("[a-zA-Z0-9]+"); + Matcher m; + m = p.matcher(br.readLine()); + String word; + boolean first = true; + while (m.find()) { + word = m.group(); + String replacement; + if(first) { + replacement = word; + first = false; + } else { + replacement = word.substring(0, 1).toUpperCase() + word.substring(1); + } + result = result.replace(word, replacement); + } + return result.replace(" ", ""); + } catch (IOException e) { + e.printStackTrace(); + return string; + } + + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlFile.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlFile.java new file mode 100644 index 00000000..661e1005 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlFile.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * 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; + +import java.util.ArrayList; + +public class MdlFile { + + private ArrayList elements = 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 ArrayList getElements() { + return elements; + } + + public void addControl(String control) { + controls.add(control); + } + + public ArrayList getControls() { + return controls; + } + + public void addSketchData(String sketchRow) { + sketchData.add(sketchRow); + } + + public ArrayList getSketchData() { + return sketchData; + } + + public void addOtherData(String dataRow) { + otherData.add(dataRow); + } + + public ArrayList getOtherData() { + return otherData; + } + + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java new file mode 100644 index 00000000..33abdc4c --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java @@ -0,0 +1,487 @@ +/******************************************************************************* + * 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; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +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.Element; +import org.simantics.sysdyn.mdlImport.mdlElements.Expression; +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; + +public class MdlParser { + + public static Model parse(File file) { + + Model model = new Model(); + + String[] name = file.getName().split("\\.mdl"); + model.setName(name[0]); + + MdlFile mdlFile = getMdlContents(file); + + getElementData(model, mdlFile.getElements()); + + getSketchData(model, mdlFile.getSketchData()); + + getControlData(model, mdlFile.getControls()); + +// getOthertData(model, mdlFile.getOtherData()); + + setAllSubscripts(model); + + return model; + } + + + private static MdlFile getMdlContents(File aFile) { + MdlFile mdlFile = new MdlFile(); + + try { + BufferedReader input = new BufferedReader(new FileReader(aFile)); + + try { + String line = null; //not declared within while loop + + // Skip document definitions like {UTF-8} from the beginning + input.mark(30); + while (( line = input.readLine()) != null && + line.trim().startsWith("{") && line.trim().endsWith("}")){ + input.mark(30); + } + input.reset(); + + while (( line = input.readLine()) != null){ + // Build an element (combine the lines to one string) + StringBuilder elementBuilder = new StringBuilder(); + while(line != null) { + // Add a new line for the element + elementBuilder.append(line); + if(line.endsWith("|") && !line.endsWith("~~|")) { + //Element definition has ended + break; + } + line = input.readLine(); + } + + String element = elementBuilder.toString(); + if(element.contains(".Control")) { + // Start of control variables + break; + } else { + // Add element string to model + mdlFile.addElement(element); + } + } + + while (( line = input.readLine()) != null && !line.contains("\\\\\\---///")){ + if(line.trim().isEmpty()) + continue; + + // Build a control element (combine the lines to one string) + StringBuilder elementBuilder = new StringBuilder(); + while(line != null) { + // Add a new line for the element + elementBuilder.append(line); + if(line.endsWith("|") && !line.endsWith("~~|")) { + //Element definition has ended + break; + } + line = input.readLine(); + } + String control = elementBuilder.toString(); + mdlFile.addControl(control); + } + + + while (( line = input.readLine()) != null && !line.contains("///---\\\\\\")){ + mdlFile.addSketchData(line); + } + + while (( line = input.readLine()) != null){ + mdlFile.addOtherData(line); + } + } + finally { + input.close(); + } + } + catch (IOException ex){ + ex.printStackTrace(); + } + + return mdlFile; + } + + private static void getElementData(Model model, ArrayList elements) { + for(String elementString : elements) { + createElement(model, elementString); + } + } + + + private static void getControlData(Model model, ArrayList controls) { + for(String controlString : controls) { + String[] nameAndData = controlString.split("="); + String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); + + if(nameAndData[0].trim().equals("FINAL TIME")) { + model.setEndTime(Double.parseDouble(expressionUnitsAndComments[0])); + } else if(nameAndData[0].trim().equals("INITIAL TIME")) { + model.setStartTime(Double.parseDouble(expressionUnitsAndComments[0])); + } else if(nameAndData[0].trim().equals("TIME STEP")) { + model.setTimeStep(Double.parseDouble(expressionUnitsAndComments[0])); + } else if(nameAndData[0].trim().equals("SAVEPER")) { + model.setSaveper(expressionUnitsAndComments[0]); + model.setTimeUnit(expressionUnitsAndComments[1]); + } + } + + } + + + private static Element createElement(Model model, String elementString) { + + String[] elementExpressions = elementString.split("\\~\\~\\|"); + + Element element = null; + + for(String s : elementExpressions) { + // Skip these definitions at least for now + if(elementExpressions.length > 1 && s.contains("A FUNCTION OF")) + continue; + + Expression expression = new Expression(); + String[] nameAndData = s.split("=", 2); + if(nameAndData.length == 2) { + // A normal variable of some sort + 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(nameAndRange.length == 2) + expression.setRange(nameAndRange[1]); + + if(!nameAndData[1].trim().endsWith("|")) { + // Multiple expressions + expression.setExpression(nameAndData[1].trim()); + } else { + String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); + expression.setExpression(expressionUnitsAndComments[0].trim()); + + expressionUnitsAndComments[1] = expressionUnitsAndComments[1].trim(); + + if(expressionUnitsAndComments[1].contains("[") && + expressionUnitsAndComments[1].contains("]") && + expressionUnitsAndComments[1].lastIndexOf("]") == expressionUnitsAndComments[1].length() - 1) { + // Range definitions are at the end + String range = expressionUnitsAndComments[1].substring( + expressionUnitsAndComments[1].lastIndexOf("[") + 1, + expressionUnitsAndComments[1].length() - 1); + String[] rangeParts = range.split(","); + + try { + element.setRangeStart(Double.parseDouble(rangeParts[0])); + if(rangeParts.length >= 2) + element.setRangeEnd(Double.parseDouble(rangeParts[1])); + if(rangeParts.length >= 3) + element.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()); + + + element.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("\"", "")); + String[] expressionUnitsAndComments = subscript[1].split("[\\~|\\|]"); + expression.setExpression(expressionUnitsAndComments[0].trim()); + element.setUnits(expressionUnitsAndComments[1].trim()); + element.setComments(expressionUnitsAndComments[2].trim()); + } else { + // 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); + String theRest = s.substring(s.indexOf("(") + 1); + String[] expressionUnitsAndComments = theRest.split("[\\~|\\|]"); + element.setUnits(expressionUnitsAndComments[1].trim()); + element.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) + table = table.substring(table.indexOf(",") + 1, table.lastIndexOf(")")); + table = "{" + table + "}"; + table = table.replace("(", "{"); + table = table.replace(")", "}"); + expression.setExpression(table); + } + } + + if(element != null) { + // Finally add the expression to element + element.getExpressions().add(expression); + } + } + return element; + } + + 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++) { + line = sketchData.get(i); + if(line.startsWith("*")) + 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) { + 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 + + + model.removeElement(element); + model.addElement(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]) / 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); + } + 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)) + continue; + if(connection.getStart().equals(valve) && + connection.getEnd() instanceof 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.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()); + } + } + } + expression.setExpression(sb.toString()); + ArrayList expressions = new ArrayList(); + expressions.add(expression); + valve.setExpressions(expressions); + } + } + } + } + + private static void getOthertData(Model model, String otherData) { + + } + + 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; + } + } + } + } + } + } + +} 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 new file mode 100644 index 00000000..c7555802 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Auxiliary.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * 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.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); + } + + @Override + public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource e = GraphUtils.create2(graph, + sr.NormalExpression, + sr.HasEquation, ImportUtils.escapeExpression(expression.getExpression()).trim()); + return e; + } + +} 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 new file mode 100644 index 00000000..5a9342fd --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Cloud.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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.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; + +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); + + } + +} 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 new file mode 100644 index 00000000..619c81a9 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * 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.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; + +public class Connection { + public static enum Type {DEPENDENCY, FLOW}; + + private Type type; + private Element start, end; + + public void writeConnection(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); + 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, + sr.HasHead, end.getResource(), + sr.HasTail, start.getResource(), + l0.PartOf, configuration); + graph.claim(connection, mr.Mapped, connection); + + + // Build diagram connectors and connection + Resource startElement = graph.getSingleObject(start.getResource(), mr.ComponentToElement); + Resource tailConnector = GraphUtils.create2(graph, + dr.Connector, + sr.HasTailTerminal, startElement); + + Resource endElement = graph.getSingleObject(end.getResource(), mr.ComponentToElement); + Resource headConnector = GraphUtils.create2(graph, + dr.Connector, + sr.HasHeadTerminal, endElement, + dr.AreConnected, tailConnector); + + Resource diagramConnection = GraphUtils.create2(graph, + connectionSymbol, + sr2.HasConnectionType, sr.SysdynConnectionType, + 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; + } + + public Element getStart() { + return start; + } + + public void setStart(Element start) { + this.start = start; + } + + public Element getEnd() { + return end; + } + + public void setEnd(Element end) { + this.end = end; + } + + +} 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 new file mode 100644 index 00000000..684e91f5 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Element.java @@ -0,0 +1,273 @@ +/******************************************************************************* + * 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 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 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 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; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public String getName() { + return name; + } + + public void setName(String name) { + 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; + } + + 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(); + return connections; + } + + public void setConnections(ArrayList connections) { + this.connections = connections; + } + + public void addConnection(Connection connection) { + getConnections().add(connection); + } + + + +} + diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java new file mode 100644 index 00000000..b2b20f6b --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Expression.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * 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; + + +public class Expression { + private String range, expression; + + @Override + public String toString() { + return (range != null ? "[" + range + "]: " : "") + expression; + } + + public String getRange() { + return range; + } + + public void setRange(String range) { + this.range = range; + } + + public String getExpression() { + return expression; + } + + public void setExpression(String expression) { + this.expression = expression; + } + +} 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 new file mode 100644 index 00000000..526c59a2 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java @@ -0,0 +1,222 @@ +/******************************************************************************* + * 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 java.util.HashMap; + +import org.simantics.databoard.Bindings; +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.DiagramConstants; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.synchronization.graph.layer.GraphLayer; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerUtil; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.modeling.ModelingResources; + +public class Model { + + 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(); + + public void addElement(Element element) { + if(element instanceof Subscript) + subscripts.add((Subscript)element); + else + elements.add(element); + if(element.getName() != null) + elementMap.put(element.getName(), element); + } + + public void removeElement(Element element) { + elements.remove(element); + + String toBeRemoved = null; + for(String key : elementMap.keySet()) { + if(element.equals(elementMap.get(key))) { + toBeRemoved = key; + break; + } + } + if(toBeRemoved != null) + elementMap.remove(toBeRemoved); + } + + public ArrayList getElements() { + return elements; + } + + public Element getElement(String name) { + return elementMap.get(name); + } + + public void addConnection(Connection connection) { + connections.add(connection); + if(connection.getStart() != null && + !connection.getStart().getConnections().contains(connection)) { + connection.getStart().addConnection(connection); + } + if(connection.getEnd() != null && + !connection.getEnd().getConnections().contains(connection)) { + connection.getEnd().addConnection(connection); + } + } + + public ArrayList getConnections() { + return connections; + } + + 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; + } + + public void setName(String name) { + this.name = name; + } + + public String getTimeUnit() { + return timeUnit; + } + + public void setTimeUnit(String timeUnit) { + this.timeUnit = timeUnit; + } + + public String getSaveper() { + return saveper; + } + + public void setSaveper(String saveper) { + this.saveper = saveper; + } + + public double getStartTime() { + return startTime; + } + + public void setStartTime(double startTime) { + this.startTime = startTime; + } + + public double getEndTime() { + return endTime; + } + + public void setEndTime(double endTime) { + this.endTime = endTime; + } + + public double getTimeStep() { + return timeStep; + } + + public void setTimeStep(double timeStep) { + this.timeStep = timeStep; + } + + public HashMap getElementMap() { + return elementMap; + } + + public void setElementMap(HashMap elementMap) { + this.elementMap = elementMap; + } + + public void setElements(ArrayList elements) { + this.elements = elements; + } + + public void setSubscripts(ArrayList subscripts) { + this.subscripts = subscripts; + } + + public void setConnections(ArrayList connections) { + this.connections = connections; + } + + +} 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 new file mode 100644 index 00000000..559a5d1c --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/ModelControl.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; + +public class ModelControl extends Element { + + @Override + public void writeElement(WriteGraph graph, Resource configuration) { + + } + + @Override + public Resource getExpression(WriteGraph graph, Expression expression) + throws DatabaseException { + return null; + } + +} 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 new file mode 100644 index 00000000..c0bda252 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * 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.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); + } + + @Override + public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { + + SysdynResource sr = SysdynResource.getInstance(graph); + Resource e = GraphUtils.create2(graph, + sr.StockExpression, + sr.HasInitialEquation, ImportUtils.escapeExpression(getIntegralParts(expression)[1])); + + return e; + } + + public String[] getIntegralParts(Expression expression) { + // Does not work, if the integral has some other logic than +inflows -outflows! + + // Parsing the possible functions. Searching ',' that divides the INTEG -function + int parenthesiscount = 0; + int location = 0; + char[] charArray = expression.getExpression().toCharArray(); + for(int i = 0; i < charArray.length; i++) { + char c = charArray[i]; + if(c == '(') + parenthesiscount++; + else if(c == ')') + parenthesiscount--; + else if(c == ',' && parenthesiscount == 1) { + location = i + 1; + break; + } + } + + String exp = expression.getExpression(); + String initialEquation = exp.substring(location, exp.lastIndexOf(')')).trim(); + String integral = exp.substring(exp.indexOf("(") + 1, location - 1).trim(); + + + return new String[] {integral, initialEquation}; + } + +} 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 new file mode 100644 index 00000000..99dc5927 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Subscript.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * 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.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +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; + } + + @Override + public Resource getExpression(WriteGraph graph, Expression expression) + throws DatabaseException { + return null; + } + +} 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 new file mode 100644 index 00000000..2ee72861 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Valve.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * 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.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); + } + + @Override + public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + String expressionString = ImportUtils.escapeExpression(expression.getExpression()); + Resource e = GraphUtils.create2(graph, + sr.NormalExpression, + sr.HasEquation, expressionString.trim()); + return e; + } +} -- 2.47.1