]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Enumeration support
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 1 Mar 2011 14:04:18 +0000 (14:04 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 1 Mar 2011 14:04:18 +0000 (14:04 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19859 ac1ea38d-2e2b-0410-8846-a27921b304fc

15 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java
org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/EquivalenceSubscript.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Stock.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/IndexUtils.java

index c58aa2724f8838d261a8a17852cfbde927662890..f07b19fb1c941c1e725e7b85f2abdb1472dc636a 100644 (file)
@@ -167,6 +167,7 @@ public class StockExpression implements IExpression {
 \r
 \r
     private String getIntegral(final Resource expression) {\r
+       \r
         String integral = "";\r
         if(expression == null)\r
                return integral;\r
@@ -189,14 +190,17 @@ public class StockExpression implements IExpression {
                        \r
                        Stock stock = (Stock)model.getElement(variable);\r
                     \r
+                       String range = graph.getPossibleRelatedValue(expression, sr.HasArrayRange);\r
+                       if(range == null)\r
+                               range = "";\r
 \r
                     StringBuilder builder = new StringBuilder();\r
                     builder.append("");\r
                     for(Valve in : stock.getIncomingValves()) {\r
-                       builder.append(" + " + in.getName());\r
+                       builder.append(" + " + in.getName() + range);\r
                     }\r
                     for(Valve out : stock.getOutgoingValves()) {\r
-                       builder.append(" - " + out.getName());\r
+                       builder.append(" - " + out.getName() + range);\r
                     }\r
                     if (builder.indexOf(" + ") == 0)\r
                         builder.delete(0, 3);\r
index 2caf1d20c2e134340fad0b4e67149cddd8f735c5..180b2fc594aa0f2cee34a1ec013ee821b497c531 100644 (file)
@@ -278,6 +278,7 @@ public class ExpressionUtils {
                                                boolean isFound = false;\r
                                                Enumeration e = (Enumeration)v;\r
                                                \r
+                                               // TODO: add .elements and .size  \r
                                                if(enumeration.equals(et.image)) {\r
                                                        // The full enumeration\r
                                                        isFound = true;\r
index 4425ce9c596605678e5cafa19ead3ad9ff86c309..38e7b92c2ecaa6264c80f249b9333acf411cbf78 100644 (file)
@@ -26,6 +26,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.common.utils.OrderedSetUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
@@ -161,6 +162,8 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                        rvis.put(rvi, rvi);\r
                } else {\r
                        List<Resource> arrayIndexList= OrderedSetUtils.toList(g, arrayIndexes);\r
+                       resolveActiveArrayIndexes(g, variable, arrayIndexList);\r
+                       \r
                        if(arrayIndexList.size() > 0)\r
                                traverseIndexes(g, rvi, rvis, arrayIndexList);\r
                        else\r
@@ -187,6 +190,40 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
 \r
        }\r
        \r
+       private static List<Resource> resolveActiveArrayIndexes(ReadGraph graph, Variable variable, List<Resource> enumerations) {\r
+               try {\r
+                       Layer0 l0 = Layer0.getInstance(graph);\r
+                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                       String uri = variable.getURI(graph);\r
+                       uri = uri.substring(0, uri.lastIndexOf("/"));\r
+                       Variable v = Variables.getPossibleVariable(graph, uri);\r
+                       if(v != null) {\r
+                               RepresentsProperty rp = (RepresentsProperty)v.getProperty(graph, Variables.REPRESENTS);\r
+                               Resource module = rp.getValue(graph);\r
+                               if(module != null && graph.isInheritedFrom(graph.getSingleObject(module, l0.InstanceOf), sr.Module)) {\r
+                                       \r
+                                       boolean somethingIsReplaced = false;\r
+                                       for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.HasRedeclaration, sr.Redeclaration))) {\r
+                                               Resource replaced = graph.getSingleObject(redeclaration, sr.ReplacedEnumeration);\r
+                                               if(enumerations.contains(replaced)) {\r
+                                                       enumerations.add(enumerations.indexOf(replaced), graph.getSingleObject(redeclaration, sr.ReplacingEnumeration));\r
+                                                       enumerations.remove(replaced);\r
+                                                       somethingIsReplaced = true;\r
+                                               }\r
+                                       }\r
+                                       \r
+                                       if(somethingIsReplaced) {\r
+                                               resolveActiveArrayIndexes(graph, v, enumerations);\r
+                                       }\r
+                               }\r
+                       }\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+                       \r
+               }\r
+               return enumerations;\r
+       }\r
+       \r
        private void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes) throws DatabaseException {\r
                traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(0), "", "");\r
        }\r
index 567a9e4625c54dce36a62410c3e11256025aba63..57cbe03617bea5e09e8d7036eb24125532dbbb7e 100644 (file)
@@ -15,8 +15,21 @@ import java.util.HashMap;
 \r
 public class ExpressionParser {\r
 \r
+    public class ForRange {\r           public Token start;\r
+               public Token end;\r
+    }\r
+\r
     boolean forIndex = false;\r
+    Token firstToken;\r
 \r
+    public Token getFirstToken() {\r            return firstToken;\r
+     }\r\r
+       List<ForRange> forRanges = new ArrayList<ForRange>();\r
+    \r
+    public List<ForRange> getForRanges() {\r
+        return forRanges;\r
+    }\r
+    \r
        HashMap<String, List<Token>> references = new HashMap<String, List<Token>>();\r
     \r
     public HashMap<String, List<Token>> getReferences() {\r
@@ -94,11 +107,17 @@ TOKEN:
 // { add_op term } -> ( add_op() term() )*\r
 \r
 void expr() : {\r
-       references = new HashMap<String, List<Token>>();\r
 } {\r
-       simple_expression() <EOF> \r
-       | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )*\r
-       "else" expression() <EOF> \r
+       {\r
+               firstToken = token;\r
+       }\r
+       simple_expression() <EOF>\r
+       |\r
+       {\r
+               firstToken = token;\r
+       }\r
+       "if" expression() "then" expression() ( "elseif" expression() "then" expression() )*\r
+       "else" expression() <EOF>\r
 }\r
 \r
 void expression() : {\r
@@ -242,7 +261,14 @@ void for_index() : {
        <IDENT>\r        {\r
                forIndices.put(token, currentRange);\r
                forIndex = true;\r       }\r
-               ( "in" expression() )?\r
+               ( "in"\r         {\r
+                 ForRange forRange = new ForRange();\r
+                 forRange.start = token;\r              }\r
+               expression()\r           {\r
+                 forRange.start = forRange.start.next;\r
+                 forRange.end = token;\r
+                 forRanges.add(forRange);\r             }\r
+               )?\r
        {\r              forIndex = false;\r
        }\r
 }\r
@@ -282,7 +308,7 @@ void array_subscripts() : {\r        if(ranges.get(token.image) == null) {
 \r
 void subscript(List<Token> currentRange) : {\r
   Token rangeToken = new Token(token.kind, "");\r
-  rangeToken.beginColumn = token.beginColumn;\r
+  rangeToken.beginColumn = token.beginColumn + 1;\r
   rangeToken.beginLine = token.beginLine;\r
 } {\r   ":"\r    {\r              currentRange.add(token);\r
        }\r
index dd73915f5849b39b6cb0e03fe94fbda63ba3e62c..66f44e266aa9f0fc565fb85e3a62df46f44459e1 100644 (file)
@@ -27,6 +27,7 @@ import org.simantics.sysdyn.mdlImport.mdlElements.Cloud;
 import org.simantics.sysdyn.mdlImport.mdlElements.Connection;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Dependency;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Element;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.EquivalenceSubscript;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Expression;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Flow;\r
 import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
@@ -81,10 +82,13 @@ public class MdlParser {
                                        input.reset();\r
                                }\r
                                \r
+                               \r
+                               boolean isControl = false;\r
+                               \r
                                while (( line = input.readLine()) != null){\r
                                        // Build an element (combine the lines to one string)\r
                                        StringBuilder elementBuilder = new StringBuilder();\r
-                                       while(line != null) {\r
+                                       while(line != null && !line.contains("\\\\\\---///")) {\r
                                                // Add a new line for the element\r
                                                elementBuilder.append(line);\r
                                                if(line.endsWith("|") && !line.endsWith("~~|")) {\r
@@ -94,36 +98,28 @@ public class MdlParser {
                                                line = input.readLine();\r
                                        }\r
                                        \r
-                                       String variable = elementBuilder.toString();\r
-                                       if(variable.contains(".Control")) {\r
-                                               // Start of control variables\r
+                                       if(line.contains("\\\\\\---///"))\r
                                                break;\r
-                                       } else {\r
-                                               // Add element string to model\r
-                                               mdlFile.addVariable(variable);\r
-                                       }\r
-                               }\r
-\r
-                               while (( line = input.readLine()) != null && !line.contains("\\\\\\---///")){\r
-                                       if(line.trim().isEmpty())\r
-                                                continue;\r
                                        \r
-                                       // Build a control element (combine the lines to one string)\r
-                                       StringBuilder elementBuilder = new StringBuilder();\r
-                                       while(line != null) {\r
-                                               // Add a new line for the element\r
-                                               elementBuilder.append(line);\r
-                                               if(line.endsWith("|") && !line.endsWith("~~|")) {\r
-                                                       //Element definition has ended\r
-                                                       break;\r
+                                       String variable = elementBuilder.toString();\r
+\r
+                                       if(variable.trim().matches("[\\*]{46}.+[\\*]{46}.+")) {\r
+                                               if(variable.contains(".Control")) {\r
+                                                       isControl = true;\r
+                                               } else {\r
+                                                       isControl = false;\r
                                                }\r
-                                               line = input.readLine();\r
+                                               continue;\r
+                                       }\r
+\r
+                                       // Add element string to model\r
+                                       if(isControl) {\r
+                                               mdlFile.addControl(variable);\r
+                                       } else {\r
+                                       mdlFile.addVariable(variable);\r
                                        }\r
-                                       String control = elementBuilder.toString();\r
-                                       mdlFile.addControl(control);\r
                                }\r
                                \r
-                               \r
                                while (( line = input.readLine()) != null && !line.contains("///---\\\\\\")){\r
                                        mdlFile.addSketchData(line);\r
                                }\r
@@ -144,9 +140,21 @@ public class MdlParser {
        }\r
        \r
        private static void getVariableData(Model model, ArrayList<String> elements) {\r
-                       for(String elementString : elements) {\r
-                               createVariable(model, elementString);\r
+               ArrayList<EquivalenceSubscript> equivalenceSubscripts = new ArrayList<EquivalenceSubscript>();\r
+               for(String elementString : elements) {\r
+                       Variable v = createVariable(model, elementString);\r
+                       if(v instanceof EquivalenceSubscript){\r
+                               equivalenceSubscripts.add((EquivalenceSubscript) v);\r
+                       }\r
+               }\r
+               \r
+               // All variables are ready, determine subscript equivalences\r
+               for(EquivalenceSubscript es : equivalenceSubscripts) {\r
+                       Element e = model.getElement(es.getEquivalentToName());\r
+                       if(e != null && e instanceof Subscript) {\r
+                               es.setEquivalentTo((Subscript)e);\r
                        }\r
+               }\r
        }\r
        \r
        \r
@@ -169,6 +177,55 @@ public class MdlParser {
                \r
        }\r
        \r
+       private static Variable getVariable(Model model, String name) {\r
+               Element e = model.getElement(name);\r
+               Variable variable = null;\r
+               if(e != null && e instanceof Variable)\r
+                       variable = (Variable)e;\r
+               return variable;\r
+       }\r
+       \r
+       private static String[] getNormalVariableNameDataAndRange(String element) {\r
+               String[] nameAndData = element.split("=", 2);\r
+               String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
+               if(nameAndData.length == 2)\r
+                       return new String[] {nameAndRange[0], nameAndData[1], nameAndRange.length == 2 ? nameAndRange[1] : null};\r
+               return null;\r
+       }\r
+       \r
+       private static String[] getSubscriptNameAndData(String element) {\r
+               String[] nameAndData = element.split(":");\r
+               if(nameAndData.length == 2)\r
+                       return nameAndData;\r
+               return null;\r
+       }       \r
+       \r
+       private static String[] getEquivalenceSubscriptNameAndData(String element) {\r
+               String[] nameAndData = element.split("\\<\\-\\>");\r
+               if(nameAndData.length == 2)\r
+                       return nameAndData;\r
+               return null;\r
+       }\r
+       \r
+       private static String[] getTableNameDataAndRange(String element) {\r
+               String[] parts = element.split("\\~"); \r
+               if(!parts[0].contains("(") || !parts[0].contains(")"))\r
+                       return null;\r
+               String name = element.substring(0, element.indexOf("("));\r
+               String theRest = element.substring(element.indexOf("("));\r
+               String[] nameAndData = {name, theRest};\r
+               String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
+               if(nameAndData.length == 2)\r
+                       return new String[] {nameAndRange[0], nameAndData[1], nameAndRange.length == 2 ? nameAndRange[1] : null};\r
+               return nameAndData;\r
+       }\r
+       \r
+       private static String[] getDataVariableNameAndData(String element) {\r
+               String[] nameAndData = {\r
+                               element.substring(0, element.indexOf("~")),\r
+                               " " + element.substring(element.indexOf("~"))};\r
+               return nameAndData;\r
+       }\r
        \r
        private static Variable createVariable(Model model, String elementString) {\r
 \r
@@ -182,106 +239,132 @@ public class MdlParser {
                                continue;\r
                        \r
                        Expression expression = new Expression();\r
-                       String[] nameAndData = s.split("=", 2);                 \r
-                       if(nameAndData.length == 2) {\r
-                               // A normal variable of some sort\r
-                               String[] nameAndRange = nameAndData[0].trim().split("[\\[|\\]]");\r
+\r
+                       String[] nameAndData = null;\r
+                       String name;\r
+                       \r
+                       // Create the expression based on the expression string\r
+                       if((nameAndData = getNormalVariableNameDataAndRange(s)) != null) {\r
                                \r
-                               // Get element in the first iteration\r
+                               name = nameAndData[0].replace("\"", "");\r
+                               variable = getVariable(model, name);\r
                                if(variable == null) {\r
-                                       Element e = model.getElement(nameAndRange[0]);\r
-                                       if(e != null && e instanceof Variable)\r
-                                               variable = (Variable)e;\r
-                                       if(variable == null) {\r
-                                               variable = new Auxiliary();\r
-                                               variable.setName(nameAndRange[0].replace("\"", ""));\r
-                                               model.addElement(variable);\r
-                                       }\r
+                                       variable = new Auxiliary();\r
+                                       variable.setName(name);\r
+                                       model.addElement(variable);\r
                                }\r
                                \r
-                               \r
-                               if(nameAndRange.length == 2)\r
-                                       expression.setRange(nameAndRange[1]);\r
-                               \r
                                if(!nameAndData[1].trim().endsWith("|")) {\r
                                        // Multiple expressions\r
                                        expression.setExpression(nameAndData[1].trim());\r
                                } else {\r
                                        String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
                                        expression.setExpression(expressionUnitsAndComments[0].trim());\r
-                                       \r
-                                       expressionUnitsAndComments[1] = expressionUnitsAndComments[1].trim();\r
-                                       \r
-                                       if(expressionUnitsAndComments[1].contains("[") &&\r
-                                                       expressionUnitsAndComments[1].contains("]") &&\r
-                                                       expressionUnitsAndComments[1].lastIndexOf("]") == expressionUnitsAndComments[1].length() - 1) {\r
-                                               // Range definitions are at the end\r
-                                               String range = expressionUnitsAndComments[1].substring(\r
-                                                               expressionUnitsAndComments[1].lastIndexOf("[") + 1, \r
-                                                               expressionUnitsAndComments[1].length() - 1);\r
-                                               String[] rangeParts = range.split(",");\r
-                                               \r
-                                               try {\r
-                                                       variable.setRangeStart(Double.parseDouble(rangeParts[0]));\r
-                                                       if(rangeParts.length >= 2)\r
-                                                               variable.setRangeEnd(Double.parseDouble(rangeParts[1]));\r
-                                                       if(rangeParts.length >= 3)\r
-                                                               variable.setRangeStep(Double.parseDouble(rangeParts[2]));\r
-                                               } catch (NumberFormatException e) {\r
-                                                       // Not a double\r
-                                               }\r
-                                               expressionUnitsAndComments[1] = expressionUnitsAndComments[1].substring(0, expressionUnitsAndComments[1].lastIndexOf("["));\r
-                                       }\r
-                                       variable.setUnits(expressionUnitsAndComments[1].trim());\r
-                                       \r
-                                       \r
-                                       variable.setComments(expressionUnitsAndComments[2].trim());\r
                                }\r
-                       } else {\r
-                               // SUBSCRIPT\r
-                               String[] subscript = s.split(":");      \r
-                               if(subscript.length == 2) {\r
-                                       // Subscript\r
+                               \r
+                       } else if((nameAndData = getSubscriptNameAndData(s)) != null) {\r
+                               \r
+                               name = nameAndData[0].replace("\"", "");\r
+                               variable = getVariable(model, name);\r
+                               if(variable == null) {\r
                                        variable = new Subscript();\r
+                                       variable.setName(name);\r
                                        model.addElement(variable);\r
-                                       variable.setName(subscript[0].replace("\"", ""));\r
-                                       String[] expressionUnitsAndComments = subscript[1].split("[\\~|\\|]");\r
-                                       expression.setExpression(expressionUnitsAndComments[0].trim());\r
-                                       variable.setUnits(expressionUnitsAndComments[1].trim());\r
-                                       variable.setComments(expressionUnitsAndComments[2].trim());\r
-                               } else if(s.contains("(")){\r
-                                       // TABLE: These are actually functions in vensim. When function library is created, use these to create interpolation functions\r
+                               }\r
+                               \r
+                               // No support for multidimensional variables. Don't know what that would mean\r
+                               String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+                               expression.setExpression(expressionUnitsAndComments[0].trim());\r
+                               variable.setUnits(expressionUnitsAndComments[1].trim());\r
+                               variable.setComments(expressionUnitsAndComments[2].trim());\r
+                               \r
+                       } else if((nameAndData = getEquivalenceSubscriptNameAndData(s)) != null) {\r
+                               \r
+                               name = nameAndData[0].replace("\"", "");\r
+                               variable = getVariable(model, name);\r
+                               if(variable == null) {\r
+                                       variable = new EquivalenceSubscript();\r
+                                       variable.setName(name);\r
+                                       model.addElement(variable);\r
+                               }\r
+                               \r
+                               // No support for multidimensional variables. Don't know what that would mean\r
+                               String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+                               expression.setExpression(expressionUnitsAndComments[0].trim());\r
+                               variable.setUnits(expressionUnitsAndComments[1].trim());\r
+                               variable.setComments(expressionUnitsAndComments[2].trim());\r
+                               \r
+                       } else if((nameAndData = getTableNameDataAndRange(s)) != null) {\r
+                               \r
+                               name =(nameAndData[0].replace("\"", ""));\r
+                               variable = getVariable(model, name);\r
+                               if(variable == null) {\r
                                        variable = new Auxiliary();\r
-                                       variable.setName(s.substring(0, s.indexOf("(")).replace("\"", ""));\r
+                                       variable.setName(name);\r
                                        model.addElement(variable);\r
-                                       String theRest = s.substring(s.indexOf("(") + 1);\r
-                                       String[] expressionUnitsAndComments = theRest.split("[\\~|\\|]");\r
-                                       variable.setUnits(expressionUnitsAndComments[1].trim());\r
-                                       variable.setComments(expressionUnitsAndComments[2].trim());\r
-                                       // [(0,0)-(2,5)],(0,5),(0.5,3),(1,0.5),(2,0.5) => ,(0,5),(0.5,3),(1,0.5),(2,0.5)\r
-                                       String table = expressionUnitsAndComments[0].trim().split("[\\[|\\]]")[2];\r
-                                       // ,(0,5),(0.5,3),(1,0.5),(2,0.5) => (0,5),(0.5,3),(1,0.5),(2,0.5)\r
-                                       table = table.substring(table.indexOf(",") + 1, table.lastIndexOf(")"));\r
-                                       table = "{" + table + "}";\r
-                                       table = table.replace("(", "{");\r
-                                       table = table.replace(")", "}");\r
-                                       expression.setExpression(table);                \r
-                               } else {\r
-                                       // Just an empty variable, Data-driven variable\r
+                               }\r
+                               \r
+                               String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+                               // ([(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)\r
+                               String table = expressionUnitsAndComments[0].trim().split("[\\[|\\]]")[2];\r
+                               // ,(0,5),(0.5,3),(1,0.5),(2,0.5) => (0,5),(0.5,3),(1,0.5),(2,0.5)\r
+                               table = table.substring(table.indexOf(",") + 1, table.lastIndexOf(")"));\r
+                               table = "{" + table + "}";\r
+                               table = table.replace("(", "{");\r
+                               table = table.replace(")", "}");\r
+                               expression.setExpression(table);                \r
+                               \r
+                               \r
+                       }  else if((nameAndData = getDataVariableNameAndData(s)) != null) {\r
+                               \r
+                               name = nameAndData[0].replace("\"", "");\r
+                               variable = getVariable(model, name);\r
+                               if(variable == null) {\r
                                        variable = new Auxiliary();\r
-                                       String[] nameUnitsAndComments = s.split("[\\~|\\|]");\r
-                                       variable.setName(nameUnitsAndComments[0].trim().replace("\"", ""));\r
+                                       variable.setName(name);\r
                                        model.addElement(variable);\r
-                                       expression.setExpression("");\r
-                                       variable.setUnits(nameUnitsAndComments[1].trim());\r
-                                       variable.setComments(nameUnitsAndComments[2].trim());\r
                                }\r
-                       }\r
+                               \r
+                               expression.setExpression("");\r
+                       } \r
+                       \r
+                       if(nameAndData == null || variable == null)\r
+                               continue;\r
+                       \r
+                       // Set possible range for the expression\r
+                       if(nameAndData.length == 3)\r
+                               expression.setRange(nameAndData[2]);\r
                        \r
-                       if(variable != null) {\r
-                               // Finally add the expression to element\r
-                               variable.getExpressions().add(expression);\r
+                       // Set units and comments for the variable \r
+                       if(nameAndData[1].trim().endsWith("|")) {\r
+                               String[] expressionUnitsAndComments = nameAndData[1].split("[\\~|\\|]");\r
+                               String units = expressionUnitsAndComments[1].trim();\r
+                               if(units.contains("[") &&\r
+                                               units.contains("]") &&\r
+                                               units.lastIndexOf("]") == units.length() - 1) {\r
+                                       // Range definitions are at the end\r
+                                       String range = units.substring(\r
+                                                       units.lastIndexOf("[") + 1, \r
+                                                       units.length() - 1);\r
+                                       String[] rangeParts = range.split(",");\r
+                                       \r
+                                       try {\r
+                                               variable.setRangeStart(Double.parseDouble(rangeParts[0]));\r
+                                               if(rangeParts.length >= 2)\r
+                                                       variable.setRangeEnd(Double.parseDouble(rangeParts[1]));\r
+                                               if(rangeParts.length >= 3)\r
+                                                       variable.setRangeStep(Double.parseDouble(rangeParts[2]));\r
+                                       } catch (NumberFormatException e) {\r
+                                               // Not a double\r
+                                       }\r
+                                       expressionUnitsAndComments[1] = units.substring(0, units.lastIndexOf("["));\r
+                               }\r
+                               variable.setUnits(expressionUnitsAndComments[1].trim());\r
+                               variable.setComments(expressionUnitsAndComments[2].trim());\r
                        }\r
+                       \r
+                       // Finally add the expression to element\r
+                       variable.getExpressions().add(expression);\r
                }\r
                return variable;\r
        }\r
@@ -322,7 +405,7 @@ public class MdlParser {
                                        String[] data = line.split(",");\r
                                        if(data[0].equals("1")) {\r
                                                // Connections are handled after all elements\r
-                                               String[] connectionData = line.split("[,\\|\\(\\)]");\r
+                                               String[] connectionData = line.split(",");\r
                                                connections.add(connectionData);\r
                                                \r
                                        } else if(data[0].equals("11")){\r
@@ -376,6 +459,7 @@ public class MdlParser {
                                                                model.addElement(view, e);\r
                                                        }\r
                                                }\r
+\r
                                                e.setX(Integer.parseInt(data[3]) / SCALE); \r
                                                e.setY(Integer.parseInt(data[4]) / SCALE); \r
                                                elementNumbers.put(data[1], e);\r
@@ -433,9 +517,9 @@ public class MdlParser {
                                                // Dependency\r
                                                Point2D startPoint = new Point2D.Double(start.getX(), start.getY());\r
                                                Point2D endPoint = new Point2D.Double(end.getX(), end.getY());\r
-                                               Double controlX = Double.parseDouble(connectionData[15]) / SCALE;\r
-                                               Double controlY = Double.parseDouble(connectionData[16]) / SCALE;\r
-                                               Point2D controlPoint = new Point2D.Double(controlX, controlY);\r
+                                               String controlX = connectionData[13].substring(connectionData[13].indexOf("(") + 1);\r
+                                               String controlY = connectionData[14].substring(0, connectionData[14].indexOf(")"));\r
+                                               Point2D controlPoint = new Point2D.Double(Double.parseDouble(controlX) / SCALE, Double.parseDouble(controlY) / SCALE);\r
                                                \r
                                                if(ghostNumbers.contains(connectionData[2]) ||\r
                                                                ghostNumbers.contains(connectionData[3])) {\r
diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/EquivalenceSubscript.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/EquivalenceSubscript.java
new file mode 100644 (file)
index 0000000..74741b3
--- /dev/null
@@ -0,0 +1,37 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.mdlImport.mdlElements;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+\r
+public class EquivalenceSubscript extends Subscript {\r
+       \r
+       public String getEquivalentToName() {\r
+               String name = "";\r
+               if(expressions != null && expressions.get(0) != null) {\r
+                       name = expressions.get(0).getExpression().trim();\r
+               }\r
+               return name;\r
+       }\r
+\r
+       public void setEquivalentTo(Subscript equivalentTo) {\r
+               setExpressions(equivalentTo.getExpressions());\r
+       }\r
+\r
+       @Override\r
+       public void write(WriteGraph graph, Resource parent, double xOffset,\r
+                       double yOffset) {\r
+               super.write(graph, parent, xOffset, yOffset);\r
+       }\r
+\r
+}\r
index fdb9054190d8d38caddea61ad654cc4ec6ed46ed..e3437f38e3d463793f4b470ea763d799e526796a 100644 (file)
@@ -23,10 +23,11 @@ public class Stock extends Variable {
        @Override\r
        public Resource getExpression(WriteGraph graph, Expression expression) throws DatabaseException {               \r
 \r
+               String integralEquation = ImportUtils.escapeExpression(getIntegralParts(expression)[1]);\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
                Resource e = GraphUtils.create2(graph, \r
                                sr.StockExpression,\r
-                               sr.HasInitialEquation, ImportUtils.escapeExpression(getIntegralParts(expression)[1]));\r
+                               sr.HasInitialEquation, integralEquation);\r
                \r
                return e;\r
        }\r
index ebde6003c7f90ed0bab161263341922931f5d219..6a4a480752f09f5d2029ffed86a2319c4cf66457 100644 (file)
@@ -1,9 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
 package org.simantics.sysdyn.representation;\r
 \r
 import java.util.ArrayList;\r
 \r
 import org.simantics.objmap.annotations.GraphType;\r
 import org.simantics.objmap.annotations.RelatedElement;\r
+import org.simantics.objmap.annotations.RelatedElements;\r
 import org.simantics.objmap.annotations.RelatedValue;\r
 import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
 \r
@@ -15,6 +27,11 @@ public class Enumeration extends Variable {
     \r
     @RelatedValue("http://www.simantics.org/Sysdyn-1.0/IsReplaceable")\r
     private Boolean isReplaceable;\r
+    \r
+    @RelatedElements(\r
+            value = "http://www.simantics.org/Sysdyn-1.0/ReplacedEnumeration/Inverse",\r
+            composition = true)\r
+            private ArrayList<Redeclaration> redeclarations = new ArrayList<Redeclaration>();\r
 \r
        @Override\r
        public void accept(IElementVisitorVoid v) {\r
@@ -23,13 +40,10 @@ public class Enumeration extends Variable {
        \r
        public String getDeclaration() {\r
                StringBuilder sb = new StringBuilder();\r
+               sb.append(getEnumerationClassDefinition());\r
                sb.append("    ");\r
-               if(isReplaceable())\r
-                       sb.append("replaceable ");\r
-               sb.append(this.getType());\r
+               sb.append(this.name + "_class");\r
                sb.append(" " + this.name);\r
-               sb.append(" = ");\r
-               sb.append(getEnumerationDefinition());\r
                sb.append(";\n");\r
                return sb.toString();\r
        }\r
@@ -42,16 +56,37 @@ public class Enumeration extends Variable {
                return Boolean.TRUE.equals(isReplaceable);\r
        }\r
        \r
-       public String getEnumerationDefinition() {\r
+       public String getEnumerationClassDefinition() {\r
                StringBuilder sb = new StringBuilder();\r
-               sb.append("enumeration(");\r
-               for(int i = 0; i < enumerationIndexes.getEnumerationIndexes().size(); i++) {\r
-                       sb.append(enumerationIndexes.getEnumerationIndexes().get(i).getName());\r
-                       if(i < enumerationIndexes.getEnumerationIndexes().size() - 1)\r
-                               sb.append(", ");\r
+               ArrayList<EnumerationIndex> indexes = getEnumerationIndexes();\r
+               sb.append("    class ");\r
+               sb.append(this.name);\r
+               sb.append("_class\n        parameter Integer size = ");\r
+               sb.append(indexes.size());\r
+               sb.append(";\n");\r
+               StringBuilder arrayBuilder = new StringBuilder();\r
+               arrayBuilder.append("{");\r
+               for(int i = 1; i <= indexes.size(); i++) {\r
+                       sb.append("        constant Integer ");\r
+                       sb.append(indexes.get(i - 1).getName());\r
+                       sb.append(" = " + i + ";\n");\r
+                       \r
+                       arrayBuilder.append(i);\r
+                       if(i < indexes.size())\r
+                               arrayBuilder.append(",");\r
                }\r
-               sb.append(")");\r
+               arrayBuilder.append("}");\r
+               sb.append("        constant Integer[" + indexes.size() + "] elements = ");\r
+               sb.append(arrayBuilder);\r
+               sb.append(";\n    end ");\r
+               sb.append(this.name);\r
+               sb.append("_class;\n");\r
+               \r
                return sb.toString();\r
        }\r
+       \r
+       public ArrayList<Redeclaration> getRedeclarations() {\r
+               return redeclarations;\r
+       }\r
 \r
 }\r
index 746685c31065761a1cfda87bdc0f616727e47d64..fa6b9f59afffbee309804f544432ba8fb53ebf7b 100644 (file)
@@ -15,7 +15,6 @@ import java.util.ArrayList;
 \r
 import org.simantics.sysdyn.representation.expressions.IExpression;\r
 import org.simantics.sysdyn.representation.expressions.ParameterExpression;\r
-import org.simantics.sysdyn.representation.expressions.StockExpression;\r
 \r
 public abstract class IndependentVariable extends Variable {\r
     \r
@@ -76,7 +75,7 @@ public abstract class IndependentVariable extends Variable {
        if(ai != null) \r
                enumerations = ai.getEnumerations();\r
                IExpression firstExpression = expressions.get(0);\r
-       if(enumerations == null || enumerations.size() < 1 || firstExpression instanceof StockExpression) {\r
+       if(enumerations == null || enumerations.size() < 1) {\r
                // NOT an array variable, get equation from the only expression.\r
                if(firstExpression == null || firstExpression instanceof ParameterExpression)\r
                        return null;\r
index 8a2a543c18bc5d2b22a489e62d025c60671d6771..2c387726e25c0a29ce373f9d1c8c781bdc9202d2 100644 (file)
@@ -71,7 +71,7 @@ public class Input extends Variable {
                sb.append("[");\r
                Iterator<Enumeration> iterator = enumerations.iterator();\r
                while(iterator.hasNext()) {\r
-                       sb.append(iterator.next().getName());\r
+                       sb.append(iterator.next().getName() + ".size");\r
                        if(iterator.hasNext()) {\r
                                sb.append(", ");\r
                        }\r
index 7cddf5a7407b1a1b73886aa199adab5d73018b0f..672713f4e889f396560398dca6b186c6d637db07 100644 (file)
@@ -28,12 +28,22 @@ public class Redeclaration {
                return "";\r
        }\r
        StringBuilder sb = new StringBuilder();\r
-       sb.append("redeclare type ");\r
        sb.append(replacedEnumeration.getName());\r
-       sb.append(" = ");\r
-       sb.append(replacingEnumeration.getEnumerationDefinition());\r
+       sb.append(".size = ");\r
+       sb.append(replacingEnumeration.getName());\r
+       sb.append(".size");\r
        \r
        return sb.toString();\r
     }\r
 \r
+       public Enumeration getReplacedEnumeration() {\r
+               return replacedEnumeration;\r
+       }\r
+\r
+       public Enumeration getReplacingEnumeration() {\r
+               return replacingEnumeration;\r
+       }\r
+    \r
+    \r
+\r
 }\r
index af48ccada09775fa6621f9e082302081dbd25a1d..a528afae5466211d106b804feffc9ae7924ba50a 100644 (file)
@@ -41,7 +41,7 @@ public class NormalExpression extends Expression {
                sb.append("[");\r
                Iterator<Enumeration> iterator = enumerations.iterator();\r
                while(iterator.hasNext()) {\r
-                       sb.append(iterator.next().getName());\r
+                       sb.append(iterator.next().getName() + ".size");\r
                        if(iterator.hasNext()) {\r
                                sb.append(", ");\r
                        }\r
index 94d3c8a2866ba8eef34e331b99c00b82f26a3599..47a9c8d6b5b52ad682e9633271d0684faf73b889 100644 (file)
@@ -43,7 +43,7 @@ public class ParameterExpression extends Expression {
                sb.append("[");\r
                Iterator<Enumeration> iterator = enumerations.iterator();\r
                while(iterator.hasNext()) {\r
-                       sb.append(iterator.next().getName());\r
+                       sb.append(iterator.next().getName() + ".size");\r
                        if(iterator.hasNext()) {\r
                                sb.append(", ");\r
                        }\r
index 2212b1eb157b0ffbbb3eaf25e0575761765a1b0d..620c8b5169ed1d13d23ae987cb7424b1745a5c84 100644 (file)
@@ -18,8 +18,8 @@ import org.simantics.objmap.annotations.GraphType;
 import org.simantics.objmap.annotations.RelatedValue;\r
 import org.simantics.sysdyn.representation.ArrayIndexes;\r
 import org.simantics.sysdyn.representation.Enumeration;\r
-import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.IndependentVariable;\r
+import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.Valve;\r
 import org.simantics.sysdyn.representation.utils.IndexUtils;\r
 \r
@@ -44,7 +44,7 @@ public class StockExpression extends Expression {
                sb.append("[");\r
                Iterator<Enumeration> iterator = enumerations.iterator();\r
                while(iterator.hasNext()) {\r
-                       sb.append(iterator.next().getName());\r
+                       sb.append(iterator.next().getName() + ".size");\r
                        if(iterator.hasNext()) {\r
                                sb.append(", ");\r
                        }\r
@@ -53,57 +53,55 @@ public class StockExpression extends Expression {
                range = sb.toString();\r
        }\r
         \r
-        String each = "";\r
-        if( variable.getArrayIndexes() == null ||variable.getArrayIndexes().getEnumerations().isEmpty())\r
-               each = "";\r
-        else \r
-               each =  "each";\r
+        String each = variable.getArrayIndexes().getEnumerations().isEmpty() ? "" : "each";\r
         if (value == null) {\r
             return "    " + variable.getType() + " " + variable.getName() + range + "(" + each + " fixed=false);\n";\r
         } else {\r
             return "    " + variable.getType() + " " + variable.getName() + range + "(" + each+ " start=" + value + "," + each + " fixed=true);\n";\r
         }\r
-\r
     }\r
 \r
     @Override\r
     public String getEquation(IndependentVariable variable) {\r
+       String range = IndexUtils.rangeToIndexes(variable, this.getArrayRange());\r
         StringBuilder b = new StringBuilder();\r
         b.append("    der(")\r
-        .append(variable.getName())\r
+        .append(variable.getName() + range)\r
         .append(") =");\r
         for(Valve valve : ((Stock)variable).getIncomingValves())\r
-            b.append("\n        + ").append(valve.getName());\r
+            b.append("\n        + ").append(valve.getName() + range);\r
         for(Valve valve : ((Stock)variable).getOutgoingValves())\r
-            b.append("\n        - ").append(valve.getName());\r
+            b.append("\n        - ").append(valve.getName() + range);\r
         b.append(";\n");\r
         return b.toString();\r
     }\r
 \r
     @Override\r
     public String getInitialEquation(IndependentVariable variable) {\r
-       Double value = getStartValue(variable);\r
-       if (value != null) {\r
-            return null;\r
-        } else {\r
-               String range = IndexUtils.rangeToIndexes(variable, this.getArrayRange());\r
-            return "    " + variable.getName() + (range.equals("[:]") ? "" : range) + " = " + initialEquation + ";\n";\r
-        }\r
+       try {\r
+               Double.parseDouble(initialEquation);\r
+               return null;\r
+       } catch (Exception e){\r
+               // Has an initial equation\r
+       } \r
+       String range = IndexUtils.rangeToIndexes(variable, this.getArrayRange());\r
+       if(range == null)\r
+               range = "";\r
+       return "    " + variable.getName() + range + " = " + initialEquation + ";\n";\r
+\r
     }\r
     \r
     \r
     private Double getStartValue(IndependentVariable variable) {\r
-        Double value = null;\r
-        try {\r
-            value = Double.parseDouble(initialEquation);\r
-        } catch (Exception e){\r
-            // Has an initial equation\r
-               return null;\r
-        } \r
-        // Has initial equations if has multiple expressions\r
-        if(variable.getExpressions().getExpressions().size() > 1)\r
-               return null;\r
-        \r
+       Double value = null;\r
+       ArrayList<IExpression> expressions = variable.getExpressions().getExpressions();\r
+       if(expressions.size() == 1) {\r
+               IExpression e = expressions.get(0);\r
+            if(e.getInitialEquation(variable) == null) {\r
+               // Has start value\r
+               value = Double.parseDouble(initialEquation);\r
+            }\r
+       }\r
        return value;\r
     }\r
 \r
index defd75cd085ae068425863195aa5476742c788c3..6c379a42443d332b8f650265dc4b6d800594789c 100644 (file)
@@ -1,8 +1,23 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
 package org.simantics.sysdyn.representation.utils;\r
 \r
+import java.io.StringReader;\r
 import java.util.ArrayList;\r
 import java.util.StringTokenizer;\r
 \r
+import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
+import org.simantics.sysdyn.expressionParser.ExpressionParser.ForRange;\r
+import org.simantics.sysdyn.expressionParser.ParseException;\r
 import org.simantics.sysdyn.representation.ArrayIndexes;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.Enumeration;\r
@@ -54,6 +69,24 @@ public class IndexUtils {
                return sb.toString();\r
        }\r
        \r
+       private static String fixForRangeEnumerations(Variable variable, String equation) {\r
+       ExpressionParser parser = new ExpressionParser(new StringReader(equation));\r
+       try {\r
+               parser.expr();\r
+               for(ForRange forRange : parser.getForRanges()) {\r
+                       if(forRange.start.equals(forRange.end)) {\r
+                               Variable v = getVariable(variable.getParentConfiguration(), forRange.start.image);\r
+                               if(v instanceof Enumeration) {\r
+                                       equation = equation.replaceAll("in[\\s]*" + forRange.start.image + "($|[^\\.])", "in " + forRange.start.image + ".elements$1");\r
+                               }\r
+                       }\r
+               }\r
+       } catch (ParseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               return equation;\r
+       }\r
+       \r
        public static String equationRangesToIndexes(Variable variable, String equation) {\r
                if(equation == null || !equation.contains("[")) return equation;\r
                \r
@@ -81,7 +114,9 @@ public class IndexUtils {
                }\r
                prevToken = nextToken;\r
            }\r
-           return result.toString();\r
+           \r
+           equation = fixForRangeEnumerations(variable, result.toString());\r
+           return equation;\r
        }\r
        \r
        private static Variable getVariable(Configuration configuration, String name) {\r