From: jkauttio Date: Tue, 1 Apr 2014 11:13:29 +0000 (+0000) Subject: Clean up vensim import, remove old files X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=4179ff2bd20a3a59a63a67b424a093eee2920b1b;p=simantics%2Fsysdyn.git Clean up vensim import, remove old files refs #2924 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/dev-jkauttio@29231 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/ImportUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/ImportUtils.java deleted file mode 100644 index 8c94b42f..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/ImportUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -/******************************************************************************* - * 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.modelImport; - -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(); - continue; - } else { - startedQuote = false; - sb.append(var.toString().replaceAll("[^a-zA-Z 0-9]+", "")); - continue; - } - } - if(startedQuote) { - var.append(c); - } else { - sb.append(c); - } - } - - string = sb.toString(); - } - - 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 string; - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlFile.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlFile.java deleted file mode 100644 index c84e291a..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlFile.java +++ /dev/null @@ -1,68 +0,0 @@ -/******************************************************************************* - * 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.modelImport; - -import java.util.ArrayList; - -public class MdlFile { - - private ArrayList variableData; - private ArrayList controlData; - private ArrayList otherData; - private ArrayList> sketchData; - private ArrayList currentSketch; - - public MdlFile() { - variableData = new ArrayList(); - controlData = new ArrayList(); - otherData = new ArrayList(); - sketchData = new ArrayList>(); - } - - public void addVariableData(String line) { - variableData.add(line); - } - - public ArrayList getVariableData() { - return variableData; - } - - public void addControlData(String line) { - controlData.add(line); - } - - public ArrayList getControlData() { - return controlData; - } - - public void addOtherData(String line) { - otherData.add(line); - } - - public ArrayList getOtherData() { - return otherData; - } - - public void startSketch() { - currentSketch = new ArrayList(); - sketchData.add(currentSketch); - } - - public void addSketchData(String line) { - currentSketch.add(line); - } - - public ArrayList> getSketchData() { - return sketchData; - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java deleted file mode 100644 index 0cccad3f..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/MdlParser.java +++ /dev/null @@ -1,1020 +0,0 @@ -/******************************************************************************* - * 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.modelImport; - -import java.awt.geom.Point2D; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.simantics.sysdyn.modelImport.model.Auxiliary; -import org.simantics.sysdyn.modelImport.model.Cloud; -import org.simantics.sysdyn.modelImport.model.Connection; -import org.simantics.sysdyn.modelImport.model.Dependency; -import org.simantics.sysdyn.modelImport.model.Element; -import org.simantics.sysdyn.modelImport.model.EquivalenceSubscript; -import org.simantics.sysdyn.modelImport.model.Expression; -import org.simantics.sysdyn.modelImport.model.Flow; -import org.simantics.sysdyn.modelImport.model.Function; -import org.simantics.sysdyn.modelImport.model.Model; -import org.simantics.sysdyn.modelImport.model.Model2; -import org.simantics.sysdyn.modelImport.model.Stock; -import org.simantics.sysdyn.modelImport.model.Subscript; -import org.simantics.sysdyn.modelImport.model.Valve; -import org.simantics.sysdyn.modelImport.model.Variable; -import org.simantics.sysdyn.modelImport.model.View; - -/* - * THINGS TO FIX: - * - vensim apparently supports infix operators (:AND: instead of AND()), these must be handled - * - "public" seems to be a keyboard of some sort which causes a problem in certain variable names - * - something is seriously wrong with the sketch import - * - how should models with multiple sketches be handled (this might already work) - * - instead of splitting the file into blocks, the parser could already process the data further which would greatly simplify later methods - */ - -public class MdlParser { - - private enum State { - VARIABLE, CONTROL, SKETCH, OTHER - } - - public static Model parse(File file) { - - Model2 model = new Model2(file.getName()); - - try { - // peek at the first line to see if we need to use UTF-8 - BufferedReader reader = new BufferedReader(new FileReader(file)); - String line = reader.readLine(); - reader.close(); - - if (line == null) { - // file is empty, nothing to do here - return null; - } - - if (line.startsWith(UTF_8)) { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); - // skip the first line - reader.readLine(); - } - else { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); - } - - // read the model structure from the file contents - readVariables(reader, model); - readControls(reader, model); - readSketches(reader, model); - readOthers(reader, model); - } - catch (IOException e) { - e.printStackTrace(); - } - - return null; - } - - public static Model parse2(File file) { - - Model2 model = new Model2(file.getName()); - - //String[] name = file.getName().split("\\.mdl"); - //model.setName(name[0]); - - MdlFile mdlFile = getMdlFile(file); - - for (String variable : mdlFile.getVariableData()) { - parseElement(variable); - } - - for (ArrayList sketch : mdlFile.getSketchData()) { - parseSketch(sketch); - } - - //MdlFile test = getMdlContents(file); - - //getVariableData(model, test.getVariableData()); - - //getSketchData(model, mdlFile.getSketchData()); - - //getControlData(model, mdlFile.getControlData()); - - //getOthertData(model, mdlFile.getOtherData()); - - //setAllSubscripts(model); - - return null; - } - - private static final String UTF_8 = "{UTF-8}"; - private static final String CONTROL_STR = ".Control"; - private static final String SKETCH_START = "\\\\\\---///"; - private static final String SKETCH_END = "///---\\\\\\"; - - private static MdlFile getMdlFile(File file) { - MdlFile mdl = new MdlFile(); - - try { - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); - String line = reader.readLine(); - - if (line != null && line.startsWith(UTF_8)) { - reader.close(); - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); - // skip the "{UTF-8}" line - reader.readLine(); - line = reader.readLine(); - } - - State state = State.VARIABLE; - - while (line != null) { - line = line.trim(); - - // skip all empty lines - if (line.isEmpty()) { - line = reader.readLine(); - continue; - } - - switch (state) { - // the file starts with variable declarations that always - // start with a variable name and end with a '|' - case VARIABLE: - if (line.startsWith(SKETCH_START)) { - state = State.SKETCH; - continue; - } - - String variable = readVariable(reader, line); - - // simulation control variables are separated from model - // variables with a control block that looks basically - // like a variable but can be safely ignored - if (variable.contains(CONTROL_STR)) { - state = State.CONTROL; - break; - } - - mdl.addVariableData(variable); - break; - - // simulation control variables look like model variables but - // are handled differently - case CONTROL: - if (line.startsWith(SKETCH_START)) { - state = State.SKETCH; - continue; - } - - String control = readVariable(reader, line); - mdl.addControlData(control); - break; - - // sketch information contains the details on the structure - // and the visual representation of the model and is situated - // in the file after variable declarations - case SKETCH: - if (line.startsWith(SKETCH_END)) { - state = State.OTHER; - break; - } - - if (line.startsWith(SKETCH_START)) { - mdl.startSketch(); - break; - } - - mdl.addSketchData(line); - break; - - // the file ends with a section of data that contains some - // additional information which is not handled currently - case OTHER: - mdl.addOtherData(line); - break; - - default: break; // should not get this far - } - - line = reader.readLine(); - } - - reader.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - - return mdl; - } - - private static void readVariables(BufferedReader reader, Model2 model) - throws IOException { - StringBuilder buffer = new StringBuilder(); - - String line = reader.readLine(); - while (line != null && !line.startsWith(SKETCH_START)) { - if (line.endsWith("\\")) { - buffer.append(line.substring(0, line.length()-1)); - } - else { - buffer.append(line); - } - - if (line.endsWith("|")) { - parseElement(buffer.toString()); - buffer.setLength(0); - } - } - } - - private static void readControls(BufferedReader reader, Model2 model) - throws IOException { - String line = reader.readLine(); - while (line != null && !line.startsWith(SKETCH_START)) { - - } - } - - private static void readSketches(BufferedReader reader, Model2 model) - throws IOException { - - } - - private static void readOthers(BufferedReader reader, Model2 model) - throws IOException { - - } - - - private static String readVariable(BufferedReader reader, String current) - throws IOException { - // TODO: does not support subscript stuff at all currently - StringBuilder element = new StringBuilder(); - - while (current != null) { - current = current.trim().replace('\t', ' '); - - if (current.endsWith("|")) { - element.append(current.substring(0, current.length() - 1)); - break; - } - else if (current.endsWith("\\")) { - element.append(current.substring(0, current.length() - 1)); - } - else { - element.append(current); - } - - current = reader.readLine(); - } - - return element.toString(); - } - - private static void parseElement(String element) { - String left, right, unit, desc; - - String[] data = element.split("~"); - - if (data.length != 3) { - System.err.println("INVALID ELEMENT DATA "+element); - return; - } - - String equation = normalize(data[0]); - - left = equation.substring(0, equation.indexOf('=')).trim(); - right = equation.substring(equation.indexOf('=') + 1).trim(); - unit = data[1].trim(); - desc = data[2].trim(); - - //System.err.println("FOUND VARIABLE "+left); - //System.err.println(" EQUATION "+right); - //System.err.println(" UNIT "+unit); - //System.err.println(" DESC "+desc); - - } - - // TODO: name should probably be escaped just before writing to graph - - // matches a quoted string that may contain escaped special characters - // (which includes other quotation marks) - private static final String QUOTED_PATTERN = "\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)\""; - // matches (possibly escaped) unsupported characters - private static final String BADCHARS_PATTERN = "\\\\?[-+*/()=<>\"]"; - // matches a substring that should be capitalized (see below for details) - private static final String NAMEPART_PATTERN = "([A-Za-z])[A-Za-z]*"; - - // normalize a variable name - private static String normalize(String str) { - StringBuilder result = new StringBuilder(str); - - Matcher matcher; - - matcher = Pattern.compile(QUOTED_PATTERN).matcher(str); - while (matcher.find()) { - // TODO: could do something more clever than just an underscore - String replacement = Pattern.compile(BADCHARS_PATTERN).matcher(matcher.group(1)).replaceAll("_"); - result.replace(matcher.start(), matcher.end(), replacement); - } - - // also capitalize all variable names to remove certain openmodelica - // keywords such as "public" or "private", this might not seem like - // the most sane variable name handling scheme possible, but it is - // nevertheless the one we are forced to use - matcher = Pattern.compile(NAMEPART_PATTERN).matcher(result.toString()); - while (matcher.find()) { - // replace the first character of the match with the same - // character in upper case - result.replace(matcher.start(1), matcher.end(1), matcher.group(1).toUpperCase()); - } - - return result.toString(); - } - - private static void parseSketch(ArrayList sketch) { - // the sketch should have at least three lines, version, name and font - if (sketch.size() < 3 || - !sketch.get(0).startsWith("V300") || - !sketch.get(1).startsWith("*") || - !sketch.get(2).startsWith("$")) { - System.err.println("INVALID SKETCH DATA"); - return; - } - - // parse name - String name = sketch.get(1).substring(1); - // parse font - String font = sketch.get(2).substring(2); - - for (int i = 3; i < sketch.size(); i++) { - String line = sketch.get(i); - if (line.startsWith(CONNECTION_PREFIX)) { - parseConnection(line.substring(2)); - } - else if (line.startsWith(VARIABLE_PREFIX)) { - parseVariable(line.substring(3)); - } - else if (line.startsWith(VALVE_PREFIX)) { - //parseValve(line.substring(3)); - } - else if (line.startsWith(COMMENT_PREFIX)) { - //parseComment(line.substring(3)); - } - else { - //System.err.println("UNSUPPORTED SKETCH OBJECT "+line); - } - } - } - - // these methods are implemented according to the documentation on the .mdl - // file format available in http://www.vensim.com/documentation/24305.htm - - // (1,)id,from,to,shape,hid,pol,thick,hasf,dtype,res,color,font,np|plist - private static final String CONNECTION_PREFIX = "1,"; - private static final String CONNECTION_PATTERN = - "(\\d+),(\\d+),(\\d+).*"; - // (n,)id,name,x,y,w,h,sh,bits,hid,hasf,tpos,bw,nav1,nav2(,box,fill,font) - private static final String VARIABLE_PREFIX = "10,"; - private static final String VALVE_PREFIX = "11,"; - private static final String COMMENT_PREFIX = "12,"; - private static final String ELEMENT_PATTERN = - "(\\d+),(\".*\"|[^\",]*),(-?\\d+),(-?\\d+),(\\d+),(\\d+).*"; - - private static void parseConnection(String line) { - Matcher matcher = Pattern.compile(CONNECTION_PATTERN).matcher(line); - if (!matcher.matches()) { - System.err.println("MALFORMED CONNECTION"); - return; - } - - // the fields of interest are: id, from, to, ... (TODO) - int id = Integer.parseInt(matcher.group(1)); - int from = Integer.parseInt(matcher.group(2)); - int to = Integer.parseInt(matcher.group(3)); - - System.err.println("connection "+id+": "+from+" -> "+to); - } - - private static void parseVariable(String line) { - Matcher matcher = Pattern.compile(ELEMENT_PATTERN).matcher(line); - if (!matcher.matches()) { - System.err.println("MALFORMED VARIABLE "+line); - return; - } - - int id = Integer.parseInt(matcher.group(1)); - String name = normalize(matcher.group(2)); - int x = Integer.parseInt(matcher.group(3)); - int y = Integer.parseInt(matcher.group(4)); - int width = Integer.parseInt(matcher.group(5)); - int height = Integer.parseInt(matcher.group(6)); - - System.err.println("variable "+id+": "+name+" ("+x+","+y+")"); - } - - private static void parseValve(String line) { - - } - - private static void parseComment(String line) { - - } - - - - - - - - - - - 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 - - mdlFile.startSketch(); - - // See if the document is encoded with UTF-8. It will be marked with {UTF-8} on the first line - input.mark(30); - if ((line = input.readLine()) != null && line.contains("{UTF-8}")){ - Reader in = new InputStreamReader(new FileInputStream(aFile), "UTF-8"); - input = new BufferedReader(in); - line = input.readLine(); - } else { - input.reset(); - } - - - boolean isControl = false; - - while ((line = input.readLine()) != null) { - // Build an element (combine the lines to one string) - StringBuilder elementBuilder = new StringBuilder(); - while (line != null && !line.contains("\\\\\\---///")) { - // Add a new line for the element - elementBuilder.append(line); - if(line.endsWith("|") && !line.endsWith("~~|")) { - //Element definition has ended - break; - } - line = input.readLine(); - } - - if (line.contains("\\\\\\---///")) - break; - - String variable = elementBuilder.toString(); - - if (variable.trim().matches("[\\*]{46}.+[\\*]{46}.+")) { - if(variable.contains(".Control")) { - isControl = true; - } else { - isControl = false; - } - continue; - } - - // Add element string to model - if(isControl) { - mdlFile.addControlData(variable); - } else { - mdlFile.addVariableData(variable); - } - } - - 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 getVariableData(Model model, ArrayList elements) { - ArrayList equivalenceSubscripts = new ArrayList(); - for(String elementString : elements) { - Variable v = createVariable(model, elementString); - if(v instanceof EquivalenceSubscript){ - equivalenceSubscripts.add((EquivalenceSubscript) v); - } - } - - // All variables are ready, determine subscript equivalences - for(EquivalenceSubscript es : equivalenceSubscripts) { - Element e = model.getElement(es.getEquivalentToName()); - if(e != null && e instanceof Subscript) { - es.setEquivalentTo((Subscript)e); - } - } - } - - - 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 Variable getVariable(Model model, String name) { - Element e = model.getElement(name); - Variable variable = null; - if(e != null && e instanceof Variable) - variable = (Variable)e; - return variable; - } - - private static String[] getNormalVariableNameDataAndRange(String element) { - String[] nameAndData = element.split("=", 2); - String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]"); - if(nameAndData.length == 2) - return new String[] {nameAndRange[0], nameAndData[1], nameAndRange.length == 2 ? nameAndRange[1] : null}; - return null; - } - - private static String[] getSubscriptNameAndData(String element) { - String[] nameAndData = element.split(":"); - if(nameAndData.length == 2) - return nameAndData; - return null; - } - - private static String[] getEquivalenceSubscriptNameAndData(String element) { - String[] nameAndData = element.split("\\<\\-\\>"); - if(nameAndData.length == 2) - return nameAndData; - return null; - } - - private static String[] getTableNameDataAndRange(String element) { - String[] parts = element.split("\\~"); - if(!parts[0].contains("(") || !parts[0].contains(")")) - return null; - String name = element.substring(0, element.indexOf("(")); - String theRest = element.substring(element.indexOf("(")); - String[] nameAndData = {name, theRest}; - String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]"); - if(nameAndData.length == 2) - return new String[] {nameAndRange[0], nameAndData[1], nameAndRange.length == 2 ? nameAndRange[1] : null}; - return nameAndData; - } - - private static String[] getDataVariableNameAndData(String element) { - String[] nameAndData = { - element.substring(0, element.indexOf("~")), - " " + element.substring(element.indexOf("~"))}; - return nameAndData; - } - - private static Variable createVariable(Model model, String elementString) { - - String[] elementExpressions = elementString.split("\\~\\~\\|"); - - Variable variable = null; - - System.err.println("CREATE VARIABLE "+elementString); - - for(String s : elementExpressions) { - System.err.println(" INSIDE FOR"); - - // Skip these definitions at least for now - if(elementExpressions.length > 1 && s.contains("A FUNCTION OF")) - continue; - - Expression expression = new Expression(); - - String[] nameAndData = null; - String name; - - // Create the expression based on the expression string - if((nameAndData = getNormalVariableNameDataAndRange(s)) != null) { - - name = nameAndData[0].replace("\"", ""); - variable = getVariable(model, name); - if(variable == null) { - variable = new Auxiliary(); - variable.setName(name); - model.addElement(variable); - } - - if(!nameAndData[1].trim().endsWith("|")) { - // Multiple expressions - expression.setExpression(nameAndData[1].trim()); - } else { - String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); - expression.setExpression(expressionUnitsAndComments[0].trim()); - } - - } else if((nameAndData = getSubscriptNameAndData(s)) != null) { - - name = nameAndData[0].replace("\"", ""); - variable = getVariable(model, name); - if(variable == null) { - variable = new Subscript(); - variable.setName(name); - model.addElement(variable); - } - - // No support for multidimensional variables. Don't know what that would mean - String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); - expression.setExpression(expressionUnitsAndComments[0].trim()); - variable.setUnits(expressionUnitsAndComments[1].trim()); - variable.setComments(expressionUnitsAndComments[2].trim()); - - } else if((nameAndData = getEquivalenceSubscriptNameAndData(s)) != null) { - - name = nameAndData[0].replace("\"", ""); - variable = getVariable(model, name); - if(variable == null) { - variable = new EquivalenceSubscript(); - variable.setName(name); - model.addElement(variable); - } - - // No support for multidimensional variables. Don't know what that would mean - String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); - expression.setExpression(expressionUnitsAndComments[0].trim()); - variable.setUnits(expressionUnitsAndComments[1].trim()); - variable.setComments(expressionUnitsAndComments[2].trim()); - - } else if((nameAndData = getTableNameDataAndRange(s)) != null) { - - name =(nameAndData[0].replace("\"", "")); - variable = getVariable(model, name); - if(variable == null) { - variable = new Function(); - variable.setName(name); - model.addElement(variable); - } - - String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); - // ([(0,0)-(2,5)],(0,5),(0.5,3),(1,0.5),(2,0.5) => ( ; (0,0)-(2,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); - - - } else if((nameAndData = getDataVariableNameAndData(s)) != null) { - - name = nameAndData[0].replace("\"", ""); - variable = getVariable(model, name); - if(variable == null) { - variable = new Auxiliary(); - variable.setName(name); - model.addElement(variable); - } - - expression.setExpression(""); - } - - if(nameAndData == null || variable == null) - continue; - - // Set possible range for the expression - if(nameAndData.length == 3) - expression.setRange(nameAndData[2]); - - // Set units and comments for the variable - if(nameAndData[1].trim().endsWith("|")) { - String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]"); - String units = expressionUnitsAndComments[1].trim(); - if(units.contains("[") && - units.contains("]") && - units.lastIndexOf("]") == units.length() - 1) { - // Range definitions are at the end - String range = units.substring( - units.lastIndexOf("[") + 1, - units.length() - 1); - String[] rangeParts = range.split(","); - - try { - variable.setRangeStart(Double.parseDouble(rangeParts[0])); - if(rangeParts.length >= 2) - variable.setRangeEnd(Double.parseDouble(rangeParts[1])); - if(rangeParts.length >= 3) - variable.setRangeStep(Double.parseDouble(rangeParts[2])); - } catch (NumberFormatException e) { - // Not a double - } - expressionUnitsAndComments[1] = units.substring(0, units.lastIndexOf("[")); - } - variable.setUnits(expressionUnitsAndComments[1].trim()); - variable.setComments(expressionUnitsAndComments[2].trim()); - } - - // Finally add the expression to element - variable.getExpressions().add(expression); - } - return variable; - } - - private static int SCALE = 4; - - private static void getSketchData(Model model, ArrayList sketchData) { - String line = null; - View view = null; - - int i = 0; - - while(i < sketchData.size()) { - line = sketchData.get(i); - if(line.startsWith("*")) { - view = new View(); - model.addView(view); - - view.setName(line.substring(1)); - - // STARTED A NEW VIEW - - HashMap elementNumbers = new HashMap(); - ArrayList ghostNumbers = new ArrayList(); - ArrayList connections = new ArrayList(); - HashMap emptyValves = new HashMap(); // map for valves that don't have an element - - - i++; - line = sketchData.get(i); - while(i < sketchData.size() && !sketchData.get(i).startsWith("*")) { - line = sketchData.get(i); - - if(line.startsWith("$")) { - view.setFontParameters(line); - i++; - continue; - } - - String[] data = line.split(","); - if (data[0].equals("1")) { - // Connections are handled after all elements - String[] connectionData = line.split(","); - connections.add(connectionData); - - } 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); - ghostNumbers.add(data[1]); - } - } - - i++; - } - - 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(""); - } - } - - - - 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; - - - Connection connection; - if(connectionData[9].equals("64")) { - // Dependency - Point2D startPoint = new Point2D.Double(start.getX(), start.getY()); - Point2D endPoint = new Point2D.Double(end.getX(), end.getY()); - String controlX = connectionData[13].substring(connectionData[13].indexOf("(") + 1); - String controlY = connectionData[14].substring(0, connectionData[14].indexOf(")")); - Point2D controlPoint = new Point2D.Double(Double.parseDouble(controlX) / SCALE, Double.parseDouble(controlY) / SCALE); - - if(ghostNumbers.contains(connectionData[2]) || - ghostNumbers.contains(connectionData[3])) { - connection = new Dependency(); - } else { - double angle = Dependency.angleOfArc( - startPoint.getX(), startPoint.getY(), - controlPoint.getX(), controlPoint.getY(), - endPoint.getX(), endPoint.getY()); - - connection = new Dependency(angle); - } - - } else { - // Flow - connection = new Flow(); - if(connectionData[4].equals("100")) { - // Flip the flow - Element temp = start; - start = end; - end = temp; - } - } - connection.setStart(start); - connection.setEnd(end); - model.addConnection(connection); - } - - - // 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); - } - } - } - } - - i++; - } - - } - - private static void getOthertData(Model model, String otherData) { - - } - - private static void setAllSubscripts(Model model) { - - // Set subscripts for all elements - 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/modelImport/model/Auxiliary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java deleted file mode 100644 index ea8eb6ef..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Auxiliary.java +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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.modelImport.ImportUtils; - -public class Auxiliary extends Variable { - - @Override - public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { - - SysdynResource sr = SysdynResource.getInstance(graph); - Resource e = GraphUtils.create2(graph, - sr.NormalExpression, - sr.Expression_equation, ImportUtils.escapeExpression(expression.getExpression()).trim()); - return e; - } - - @Override - public void write(WriteGraph graph, Resource parent, double xOffset, - double yOffset) { - if(parent == null || graph == null) - return; - - 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/modelImport/model/Cloud.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java deleted file mode 100644 index 35671258..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Cloud.java +++ /dev/null @@ -1,70 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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 write(WriteGraph graph, Resource parent, double xOffset, - double yOffset) { - if(parent == null || graph == null) - return; - - try { - 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); - - if(!graph.isInstanceOf(parent, sr.Configuration)) - return; - - Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram); - - if(diagram == null) - return; - - Resource cloud = GraphUtils.create2(graph, - sr.Cloud); - - graph.claim(parent, l0.ConsistsOf, cloud); - - - 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/modelImport/model/Connection.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection.java deleted file mode 100644 index 33b32c01..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Connection.java +++ /dev/null @@ -1,98 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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 abstract class Connection implements IWriteableMDLObject { - protected Element start, end; - - public Resource writeConnection(WriteGraph graph, Resource configuration, Resource connectionType, Resource connectionSymbol) throws DatabaseException { - if(configuration == null || graph == null - || start.getResource() == null || end.getResource() == null) { - return null; - } - 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 diagram = graph.getPossibleObject(configuration, mr.CompositeToDiagram); - Resource startElement = graph.getPossibleObject(start.getResource(), mr.ComponentToElement); - Resource endElement = graph.getPossibleObject(end.getResource(), mr.ComponentToElement); - if(diagram == null || startElement == null || endElement == null) - return null; - - - if(connectionType == null) - connectionType = sr.Dependency; - - if(connectionSymbol == null) - connectionSymbol = sr.DependencyConnection; - - // Build the connection to configuration - Resource connection = GraphUtils.create2(graph, - connectionType, - sr.Variable_HasHead, end.getResource(), - sr.Variable_HasTail, start.getResource(), - l0.PartOf, configuration); - graph.claim(connection, mr.Mapped, connection); - - - // Build diagram connectors and connection - Resource tailConnector = GraphUtils.create2(graph, - dr.Connector, - sr.HasTailTerminal, startElement); - - 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); - - OrderedSetUtils.add(graph, diagram, diagramConnection); - - return connection; - } - - 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/modelImport/model/Dependency.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency.java deleted file mode 100644 index 2fc3979b..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Dependency.java +++ /dev/null @@ -1,93 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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 { - - private double angle; - - public Dependency() { - this(-0.1); - } - - public Dependency(double angle) { - this.angle = angle; - } - - @Override - public void write(WriteGraph graph, Resource parent) { - if(parent == null || graph == null || start == null || end == null) - return; - try { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource connection = writeConnection(graph, parent, sr.Dependency, sr.DependencyConnection); - - if(connection != null) { - ModelingResources mr = ModelingResources.getInstance(graph); - Resource diagramConnection = graph.getSingleObject(connection, mr.ConnectionToDiagramConnection); - graph.claimLiteral(diagramConnection, sr.Dependency_angle, angle); - } - - } catch (DatabaseException e) { - e.printStackTrace(); - } - - } - - public double getAngle() { - return angle; - } - - public void setAngle(double angle) { - this.angle = angle; - } - - - - /** - * Returns an angle in radians between straight line from (x0,y0) to (x2,y2) - * and an arc from (x0,y0) to (x2,y2) thru (x1,y1). The angle - * is measured at (x0,y0) and is between -PI and PI. - */ - public static double angleOfArc( - double x0, double y0, - double x1, double y1, - double x2, double y2) { - double dx0 = x1-x0; - double dy0 = y1-y0; - double dx1 = x1-x2; - double dy1 = y1-y2; - double dx = x2-x0; - double dy = y2-y0; - // Length of cross product (p1-p0)x(p2-p0) - double dd = dx0*dy - dy0*dx; - - if(Math.abs(dd) < 1e-6) // Points are (almost) collinear - return 0.0; - else { - // (p1-p0)*(p1-p2) / dd - double offset = (dx0*dx1 + dy0*dy1) / dd; - double angle = Math.PI*0.5 - Math.atan(offset); - if(dd > 0.0) - angle = angle-Math.PI; - return angle; - - } - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element.java deleted file mode 100644 index ad7b0d62..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Element.java +++ /dev/null @@ -1,80 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -import java.util.ArrayList; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; - -public abstract class Element implements IWriteableMDLElement { - - protected int x, y; - protected Resource resource; - protected String name; - protected ArrayList connections; - - protected Resource getExpression(WriteGraph graph, Expression expression) - throws DatabaseException { - return null; - } - - 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() { - if(name == null) - return "Name"; - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Resource getResource() { - return resource; - } - - public void setResource(Resource resource) { - this.resource = resource; - } - - 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/modelImport/model/EquivalenceSubscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/EquivalenceSubscript.java deleted file mode 100644 index 81f7be7f..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/EquivalenceSubscript.java +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; - -public class EquivalenceSubscript extends Subscript { - - public String getEquivalentToName() { - String name = ""; - if(expressions != null && expressions.get(0) != null) { - name = expressions.get(0).getExpression().trim(); - } - return name; - } - - public void setEquivalentTo(Subscript equivalentTo) { - setExpressions(equivalentTo.getExpressions()); - } - - @Override - public void write(WriteGraph graph, Resource parent, double xOffset, - double yOffset) { - super.write(graph, parent, xOffset, yOffset); - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow.java deleted file mode 100644 index abf9e715..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Flow.java +++ /dev/null @@ -1,33 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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) { - if(parent == null || graph == null) - return; - 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/modelImport/model/Function.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Function.java deleted file mode 100644 index bd699ea7..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Function.java +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.modelImport.ImportUtils; - -public class Function extends Variable { - - @Override - public void write(WriteGraph graph, Resource parent, double xOffset, - double yOffset) { - if(parent == null || graph == null) - return; - - try { - SysdynResource sr = SysdynResource.getInstance(graph); - if(!graph.isInstanceOf(parent, sr.SysdynModel)) - return; - Layer0 l0 = Layer0.getInstance(graph); - - Resource function = GraphUtils.create2(graph, - sr.SysdynModelicaFunction, - l0.HasName, ImportUtils.escapeName(this.getName())); - - if(comments != null && comments.length() > 0) - graph.claimLiteral(function, l0.HasDescription, comments); - - if(expressions != null && expressions.get(0) != null) { - StringBuilder sb = new StringBuilder(); - sb.append(" input Real a;\n"); - sb.append(" output Real result;\n"); - sb.append("algorithm\n"); - sb.append(" result := interpolate(a, " + expressions.get(0).getExpression() + ");"); - graph.claimLiteral(function, sr.SysdynModelicaFunction_modelicaFunctionCode, sb.toString()); - } - - graph.claim(parent, l0.ConsistsOf, function); - resource = function; - } catch (DatabaseException e) { - e.printStackTrace(); - } - - } - - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableMDLElement.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableMDLElement.java deleted file mode 100644 index 49357f2a..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableMDLElement.java +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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/modelImport/model/IWriteableMDLObject.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableMDLObject.java deleted file mode 100644 index 088ae383..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/IWriteableMDLObject.java +++ /dev/null @@ -1,26 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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/modelImport/model/Model.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java deleted file mode 100644 index 43c4ac3f..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Model.java +++ /dev/null @@ -1,275 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -import java.util.ArrayList; -import java.util.HashMap; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.utils.ModelUtils; - -public class Model implements IWriteableMDLObject { - - private String name, timeUnit, saveper; - private double startTime = 0, endTime = 10, timeStep = 1; - - private HashMap elementMap = new HashMap(); - private ArrayList subscripts = new ArrayList(); - private ArrayList functions = 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) - addSubscript((Subscript)element); - else if(element instanceof Function) - addFunction((Function)element); - else - unlocatedElements.add(element); - if(element.getName() != null) - elementMap.put(element.getName(), element); - } - - public void addSubscript(Subscript subscript) { - subscripts.add(subscript); - } - - public void addFunction(Function function) { - functions.add(function); - } - - 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) { - 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))) { - toBeRemoved = key; - break; - } - } - if(toBeRemoved != null) - elementMap.remove(toBeRemoved); - } - - public ArrayList getUnlocatedElements() { - return unlocatedElements; - } - - 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 String getName() { - if(name == null) - return "ModelName"; - 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 setSubscripts(ArrayList subscripts) { - this.subscripts = subscripts; - } - - public void setConnections(ArrayList connections) { - 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) { - if(parent == null || graph == null) - return; - - try { - - - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - SimulationResource simu = SimulationResource.getInstance(graph); - - Resource model = ModelUtils.createModelAt(graph, parent); - graph.claimLiteral(model, l0.HasLabel, getName()); - graph.claimLiteral(model, sr.SysdynModel_startTime, startTime); - graph.claimLiteral(model, sr.SysdynModel_stopTime, endTime); - - - Resource conf = graph.getSingleObject(model, simu.HasConfiguration); - - 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(); - } - - for(int i = 0; i < n; i++) { - for(int j = 0; j < n; j++) { - int index = i * (int)n + j; - if(index < views.size()) { - View v = views.get(index); - 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); - } - - for(Function f : functions) { - f.write(graph, model, 0, 0); - } - - - } catch (DatabaseException e) { - e.printStackTrace(); - } - } - - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelElementFactory.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelElementFactory.java deleted file mode 100644 index 01b3a125..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/ModelElementFactory.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.simantics.sysdyn.modelImport.model; - -import org.simantics.sysdyn.modelImport.MdlUtils.CommentIcon; - -public class ModelElementFactory { - - public static void getConnection() { - - } - - public static void getDependency() { - - } - - public static void getFlow() { - - } - - public static Element2 getElement(SketchVariable variable) { - if (variable.getVariable().getExpression() instanceof IntegralExpression) { - return getStock(variable); - } - else { - return getAuxiliary(variable); - } - } - - public static Element2 getElement(SketchValve valve) { - return getValve(valve); - } - - public static Element2 getElement(SketchComment comment) { - if (comment.getIcon().equals(CommentIcon.CLOUD)) { - return getCloud(comment); - } - else { - return getComment(comment); - } - } - - public static Auxiliary2 getAuxiliary(SketchVariable variable) { - return null; - } - - public static Stock2 getStock(SketchVariable variable) { - assert variable.getVariable().getExpression() instanceof IntegralExpression; - - return null; - } - - public static Valve2 getValve(SketchValve valve) { - return null; - } - - public static Cloud2 getCloud(SketchComment comment) { - assert comment.getIcon().equals(CommentIcon.CLOUD); - return null; - } - - public static Comment2 getComment(SketchComment comment) { - return null; - } - - public static Shadow2 getShadow(SketchVariable variable) { - return null; - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java deleted file mode 100644 index 86fb8a04..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Stock.java +++ /dev/null @@ -1,79 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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.modelImport.ImportUtils; - -public class Stock extends Variable { - - @Override - public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException { - - String integralEquation = ImportUtils.escapeExpression(getIntegralParts(expression)[1]); - SysdynResource sr = SysdynResource.getInstance(graph); - Resource e = GraphUtils.create2(graph, - sr.StockExpression, - sr.StockExpression_initialEquation, integralEquation); - - 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}; - } - - @Override - public void write(WriteGraph graph, Resource parent, double xOffset, - double yOffset) { - if(parent == null || graph == null) - return; - - 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/modelImport/model/Subscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Subscript.java deleted file mode 100644 index 9aefc84a..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Subscript.java +++ /dev/null @@ -1,67 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -import java.util.ArrayList; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.ListUtils; -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 Variable { - - @Override - public Resource getExpression(WriteGraph graph, Expression expression) - throws DatabaseException { - return null; - } - - @Override - public void write(WriteGraph graph, Resource parent, double xOffset, - double yOffset) { - if(parent == null || graph == null) - return; - - try { - SysdynResource sr = SysdynResource.getInstance(graph); - if(!graph.isInstanceOf(parent, sr.Configuration)) - return; - Layer0 l0 = Layer0.getInstance(graph); - ArrayList enumerationIndexes = new ArrayList(); - 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()); - enumerationIndexes.add(ei); - } - } - - Resource enumeration = GraphUtils.create2(graph, - sr.Enumeration, - l0.HasName, this.getName(), - sr.Enumeration_enumerationIndexList, ListUtils.create(graph, enumerationIndexes)); - - graph.claim(parent, l0.ConsistsOf, enumeration); - - resource = enumeration; - } catch (DatabaseException e) { - e.printStackTrace(); - } - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java deleted file mode 100644 index 78354a4c..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Valve.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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.modelImport.ImportUtils; - -public class Valve extends Variable { - - @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.Expression_equation, 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/modelImport/model/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable.java deleted file mode 100644 index e829713a..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/Variable.java +++ /dev/null @@ -1,211 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -import java.util.ArrayList; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.ListUtils; -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.modelImport.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 { - 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 diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); - if(diagram == null) - return; - - // Make sure at least one expression exist - if(getExpressions().isEmpty()) { - Expression e = new Expression(); - e.setExpression(""); - getExpressions().add(e); - } - - Resource variable = GraphUtils.create2(graph, - variableType, - l0.HasName, ImportUtils.escapeName(name)); - graph.claim(variable, mr.Mapped, variable); - - ArrayList expressions = new ArrayList(); - 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.Expression_arrayRange, "[" + e.getRange().trim() + "]"); - } - expressions.add(expression); - graph.claim(variable, l0.ConsistsOf, expression); - } - graph.claim(variable, sr.Variable_expressionList, ListUtils.create(graph, expressions)); - - if(subscripts != null) { - ArrayList arrayIndexes = new ArrayList(); - for(Subscript sub : subscripts) { - if(sub.getResource() != null) - arrayIndexes.add(sub.getResource()); - } - graph.claim(variable, sr.Variable_arrayIndexesList, ListUtils.create(graph, arrayIndexes)); - } - - if(units != null && units.length() > 0) - graph.claimLiteral(variable, sr.Variable_unit, 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); - - - 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; - } - - - 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/modelImport/model/View.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/View.java deleted file mode 100644 index 6b7bb676..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelImport/model/View.java +++ /dev/null @@ -1,134 +0,0 @@ -/******************************************************************************* - * 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.modelImport.model; - -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 instanceof Subscript || e instanceof Function) - return; - - if (e.getX() < minX) - minX = e.getX(); - if (e.getX() > maxX) - maxX = e.getX(); - if (e.getY() < minY) - minY = e.getY(); - 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; - } -}