]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Solving problems in custom integral equation in stocks (refs #4657).
authormiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 14 Jan 2014 10:42:30 +0000 (10:42 +0000)
committermiettinen <miettinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 14 Jan 2014 10:42:30 +0000 (10:42 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28628 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ontology/graph.tg
org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph
org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java

index 29f035b174aa8a019ad1cf386ec714ebd5cfa85a..27edbee88049cbfcdf0649ef11d89f8fc0a5b046 100644 (file)
Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ
index 7aacf9fe8a60e886e50afca77df8b89b113638f1..b14b917b2a78708a0de982b818365aa7446b7da8 100644 (file)
@@ -264,6 +264,8 @@ SYSDYN.DelayExpression <T SYSDYN.Expression
     >-- SYSDYN.DelayExpression.initialValue --> L0.String <R SYSDYN.HasEquationOrEmpty : L0.FunctionalRelation
         
 SYSDYN.StockExpression <T SYSDYN.Expression
+    >-- SYSDYN.StockExpression.useCustomIntegral --> SYSDYN.StockExpression <R L0.IsRelatedTo : L0.Tag
+        @L0.symmetric
     >-- SYSDYN.StockExpression.integralEquation --> L0.String <R SYSDYN.Expression.equation : L0.FunctionalRelation
     >-- SYSDYN.StockExpression.initialEquation --> L0.String <R SYSDYN.Expression.equation : L0.FunctionalRelation
 
index 4e6dc66c953b6f5393ef411e6fa4e6dcb3319629..d5f9e0c517dbb5fc02698206d8c07b57c8e00e72 100644 (file)
@@ -611,6 +611,7 @@ public class SysdynResource {
     public final Resource StockExpression_initialEquation_Inverse;\r
     public final Resource StockExpression_integralEquation;\r
     public final Resource StockExpression_integralEquation_Inverse;\r
+    public final Resource StockExpression_useCustomIntegral;\r
     public final Resource StockSymbol;\r
     public final Resource SymbolReferences;\r
     public final Resource SymbolReferences_BasicSymbols;\r
@@ -1373,6 +1374,7 @@ public class SysdynResource {
         public static final String StockExpression_initialEquation_Inverse = "http://www.simantics.org/Sysdyn-1.1/StockExpression/initialEquation/Inverse";\r
         public static final String StockExpression_integralEquation = "http://www.simantics.org/Sysdyn-1.1/StockExpression/integralEquation";\r
         public static final String StockExpression_integralEquation_Inverse = "http://www.simantics.org/Sysdyn-1.1/StockExpression/integralEquation/Inverse";\r
+        public static final String StockExpression_useCustomIntegral = "http://www.simantics.org/Sysdyn-1.1/StockExpression/useCustomIntegral";\r
         public static final String StockSymbol = "http://www.simantics.org/Sysdyn-1.1/StockSymbol";\r
         public static final String SymbolReferences = "http://www.simantics.org/Sysdyn-1.1/SymbolReferences";\r
         public static final String SymbolReferences_BasicSymbols = "http://www.simantics.org/Sysdyn-1.1/SymbolReferences/BasicSymbols";\r
@@ -2145,6 +2147,7 @@ public class SysdynResource {
         StockExpression_initialEquation_Inverse = getResourceOrNull(graph, URIs.StockExpression_initialEquation_Inverse);\r
         StockExpression_integralEquation = getResourceOrNull(graph, URIs.StockExpression_integralEquation);\r
         StockExpression_integralEquation_Inverse = getResourceOrNull(graph, URIs.StockExpression_integralEquation_Inverse);\r
+        StockExpression_useCustomIntegral = getResourceOrNull(graph, URIs.StockExpression_useCustomIntegral);\r
         StockSymbol = getResourceOrNull(graph, URIs.StockSymbol);\r
         SymbolReferences = getResourceOrNull(graph, URIs.SymbolReferences);\r
         SymbolReferences_BasicSymbols = getResourceOrNull(graph, URIs.SymbolReferences_BasicSymbols);\r
index 4ef1734f087b6882b2a898197f847df0f82d587e..53e51b9114703d7b892d474829a5ecfd63a48b50 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management in\r
+ * Copyright (c) 2007, 2014 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
@@ -64,7 +64,7 @@ public class StockExpression implements IExpression {
     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
         String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
-        String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
+        String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression);\r
 \r
 \r
         Label label = new Label(parent, SWT.NONE);\r
@@ -102,7 +102,8 @@ public class StockExpression implements IExpression {
                                                // Delete the possible integral expression from the database to note \r
                                                // that we are in the "default" mode.\r
                                                SysdynResource sr = SysdynResource.getInstance(graph);\r
-                                               graph.deny(input.expression, sr.StockExpression_integralEquation);\r
+                                               graph.deny(input.expression, sr.StockExpression_useCustomIntegral);\r
+                                               graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
                                        }\r
                                });\r
                                break;\r
@@ -151,7 +152,7 @@ public class StockExpression implements IExpression {
                            if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
                                String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);\r
                                String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);\r
-                               initialEquation = (initialEquation != null) ? initialEquation : "";\r
+                               initialEquation = (initialEquation != null) ? initialEquation : "";\r
                                return new Pair<String, String>(integralEquation, initialEquation);\r
                            } else {\r
                                return new Pair<String, String>(null, "");\r
@@ -166,6 +167,21 @@ public class StockExpression implements IExpression {
         }\r
 \r
         if (equations.first == null) {\r
+               try {\r
+                               SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
+                                       }\r
+\r
+                               });\r
+                       } catch (DatabaseException e) {\r
+                               // TODO Auto-generated catch block\r
+                               e.printStackTrace();\r
+                       }\r
+               \r
                data.put("integral", getDefaultIntegral(expression));\r
         } else {\r
                data.put("integral", equations.first);\r
@@ -191,10 +207,12 @@ public class StockExpression implements IExpression {
     public void save(final Resource expression, final Map<String, Object> data) {\r
        final String currentInitial = this.initialExpression.getExpression();\r
        final String currentIntegral = this.integralExpression.getExpression();\r
-        if(currentInitial == null \r
-                       || !currentInitial.equals(data.get("initialEquation"))\r
-                       || (currentIntegral != null\r
-                               && !currentIntegral.equals(data.get("integral")))) {\r
+       if (currentIntegral == null) {\r
+               this.integralExpression.setExpression(getDefaultIntegral(expression));\r
+        }\r
+        if(!currentInitial.equals(data.get("initialEquation"))\r
+                       || currentIntegral == null\r
+                       || !currentIntegral.equals(data.get("integral"))) {\r
             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
 \r
                 @Override\r
@@ -222,17 +240,17 @@ public class StockExpression implements IExpression {
                     }\r
                     g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);\r
                     \r
-                    // If the value is same as default, do not save to database.\r
-                    if (currentIntegral != null && !currentIntegral.equals(data.get("integral"))) {\r
-                       g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
+                    if (!currentIntegral.equals(data.get("integral"))) {\r
+                       // If the value is not same as default, enable the custom tag\r
+                       g.claim(expression, sr.StockExpression_useCustomIntegral, expression);\r
                     }\r
+                    g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
                 }\r
 \r
             });\r
         }\r
         this.initialExpression.setExpression(currentInitial);\r
-        if (currentIntegral != null)\r
-               this.integralExpression.setExpression(currentIntegral);\r
+        this.integralExpression.setExpression(currentIntegral);\r
     }\r
 \r
     @Override\r
@@ -243,7 +261,42 @@ public class StockExpression implements IExpression {
             data.put("integral", this.integralExpression.getExpression());\r
     }\r
 \r
-    private String getDefaultIntegral(final Resource expression) {\r
+    \r
+    private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {\r
+        SysdynResource sr = SysdynResource.getInstance(graph);\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+    \r
+           // find the variable\r
+           Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
+           if(variable == null)\r
+               return "";\r
+           \r
+               SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
+               SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
+               model.update(graph);\r
+               \r
+               Stock stock = (Stock)model.getElement(variable);\r
+           \r
+               String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\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() + range);\r
+           }\r
+           for(Valve out : stock.getOutgoingValves()) {\r
+               builder.append(" - " + out.getName() + range);\r
+           }\r
+           if (builder.indexOf(" + ") == 0)\r
+               builder.delete(0, 3);\r
+           \r
+           return builder.toString().trim();   \r
+    }\r
+    \r
+    \r
+    private static String getDefaultIntegral(final Resource expression) {\r
        \r
         String integral = "";\r
         if(expression == null)\r
@@ -253,36 +306,7 @@ public class StockExpression implements IExpression {
 \r
                 @Override\r
                 public String perform(ReadGraph graph) throws DatabaseException {\r
-                       SysdynResource sr = SysdynResource.getInstance(graph);\r
-                   Layer0 l0 = Layer0.getInstance(graph);\r
-                   \r
-                   // find the variable\r
-                   Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
-                   if(variable == null)\r
-                       return "";\r
-                   \r
-                       SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
-                       SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
-                       model.update(graph);\r
-                       \r
-                       Stock stock = (Stock)model.getElement(variable);\r
-                   \r
-                       String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\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() + range);\r
-                   }\r
-                   for(Valve out : stock.getOutgoingValves()) {\r
-                       builder.append(" - " + out.getName() + range);\r
-                   }\r
-                   if (builder.indexOf(" + ") == 0)\r
-                       builder.delete(0, 3);\r
-                   \r
-                   return builder.toString().trim();\r
+                       return getDefaultIntegral(graph, expression);\r
                 }\r
 \r
             });\r
index 96c1c981c6ab86a6161cfb1a5bce5937ae255aa0..d90fd9601fe6494febb22002e43b0911e63bfa04 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2007, 2014 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
@@ -12,7 +12,6 @@
 package org.simantics.sysdyn.ui.validation;\r
 \r
 import java.io.StringReader;\r
-import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.HashMap;\r
 import java.util.HashSet;\r
@@ -127,17 +126,8 @@ public class ValidationUtils {
             SysdynResource sr = SysdynResource.getInstance(graph);\r
             Layer0 l0 = Layer0.getInstance(graph);\r
 \r
+            // Add incoming dependencies and flows\r
             Collection<Resource> dependencies = graph.getObjects(r, sr.Variable_isHeadOf);\r
-            \r
-            // Add also the outgoing flows \r
-            Collection<Resource> flows = graph.getObjects(r, sr.Variable_isTailOf);\r
-            ArrayList<Resource> outgoingFlows = new ArrayList<Resource>();\r
-            for (Resource f : flows) {\r
-               if (graph.isInstanceOf(f, sr.Flow))\r
-                       outgoingFlows.add(f);\r
-            }\r
-            dependencies.addAll(outgoingFlows);\r
-\r
             for(Resource d : dependencies) {\r
                 if(graph.isInstanceOf(d, sr.Dependency)\r
                                || (graph.isInstanceOf(d, sr.Flow) && graph.isInstanceOf(r, sr.Stock))) {\r
@@ -156,6 +146,20 @@ public class ValidationUtils {
                     }\r
                 }\r
             }\r
+            \r
+            // Add also the outgoing flows \r
+            Collection<Resource> outgoingFlows = graph.getObjects(r, sr.Variable_isTailOf);\r
+            for(Resource f : outgoingFlows) {\r
+                if(graph.isInstanceOf(f, sr.Flow) && graph.isInstanceOf(r, sr.Stock)) {\r
+                    Resource head = graph.getPossibleObject(f, sr.Variable_HasHead);\r
+                    if(head != null) {\r
+\r
+                        Object name = graph.getPossibleRelatedValue(head, l0.HasName);\r
+                        if(name != null)\r
+                            variables.add((String)name);\r
+                    }\r
+                }\r
+            }\r
         }\r
         return variables;\r
     }\r
index ba6916ccc4393609346d6bf9f27b322a5a5284e0..c96d01e42788e45c310336d17281421d9fc31764 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************\r
- * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2010, 2014 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
@@ -42,12 +42,15 @@ import org.simantics.sysdyn.representation.utils.UnitUtils;
 @GraphType(SysdynResource.URIs.StockExpression)\r
 public class StockExpression extends Expression {\r
 \r
-    @RelatedValue(SysdynResource.URIs.StockExpression_initialEquation)\r
+       @RelatedValue(SysdynResource.URIs.StockExpression_initialEquation)\r
     private String initialEquation;\r
 \r
+       @RelatedValue(SysdynResource.URIs.StockExpression_integralEquation)\r
+    private String integralEquation;\r
+\r
     @Override\r
     public String getExpression() {\r
-        return initialEquation;\r
+        return "This + is + not + a + parameter + at + any + time";\r
     }\r
 \r
     @Override\r
@@ -67,33 +70,39 @@ public class StockExpression extends Expression {
         .append(parent.getModelicaName() + range)\r
         .append(") =");\r
         \r
-        // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows)\r
-        ArrayList<Valve> incoming = ((Stock)parent).getIncomingValves();\r
-        ArrayList<Valve> outgoing = ((Stock)parent).getOutgoingValves();\r
-        if(incoming.isEmpty() && outgoing.isEmpty()) {\r
-            // No connections, add 0 for each array index if any.\r
-            ArrayList<Enumeration> enumerations = parent.getArrayIndexes();\r
-            if(enumerations == null || enumerations.isEmpty()) {\r
-                b.append(" 0.0");\r
-            } else {\r
-                b.append(" zeros(");\r
-                for(int i = 0; i < enumerations.size(); i++) {\r
-                    b.append(enumerations.get(i).getModelicaName() + ".size");\r
-                    if(i != enumerations.size() - 1)\r
-                        b.append(", ");\r
-                }\r
-                b.append(")");\r
-            }\r
-\r
+        if (integralEquation != null) {\r
+               String equation = FormatUtils.replaceWhitespace(this.integralEquation);\r
+               equation = FormatUtils.formatExpressionForModelica(parent, equation);\r
+               b.append(equation).append(";\n");\r
         } else {\r
-            // incoming valves add and outgoing valves reduce the stock\r
-            for(Valve valve : outgoing)\r
-                b.append("\n        - ").append(valve.getModelicaName() + range);\r
-            for(Valve valve : incoming)\r
-                b.append("\n        + ").append(valve.getModelicaName() + range);\r
+               // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows)\r
+                   ArrayList<Valve> incoming = ((Stock)parent).getIncomingValves();\r
+                   ArrayList<Valve> outgoing = ((Stock)parent).getOutgoingValves();\r
+                   if(incoming.isEmpty() && outgoing.isEmpty()) {\r
+                       // No connections, add 0 for each array index if any.\r
+                       ArrayList<Enumeration> enumerations = parent.getArrayIndexes();\r
+                       if(enumerations == null || enumerations.isEmpty()) {\r
+                           b.append(" 0.0");\r
+                       } else {\r
+                           b.append(" zeros(");\r
+                           for(int i = 0; i < enumerations.size(); i++) {\r
+                               b.append(enumerations.get(i).getModelicaName() + ".size");\r
+                               if(i != enumerations.size() - 1)\r
+                                   b.append(", ");\r
+                           }\r
+                           b.append(")");\r
+                       }\r
+               \r
+                   } else {\r
+                       // incoming valves add and outgoing valves reduce the stock\r
+                       for(Valve valve : outgoing)\r
+                           b.append("\n        - ").append(valve.getModelicaName() + range);\r
+                       for(Valve valve : incoming)\r
+                           b.append("\n        + ").append(valve.getModelicaName() + range);\r
+                   }\r
+                   b.append(";\n");\r
         }\r
-        b.append(";\n");\r
-        return b.toString();\r
+           return b.toString();\r
     }\r
     \r
     /** \r
@@ -188,7 +197,7 @@ public class StockExpression extends Expression {
     public String validateUnits(ReadGraph graph, SysdynModel model) {\r
         String result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), initialEquation);\r
         if(result == null) {\r
-            String integralEquation = getEquation();\r
+               String integralEquation = getEquation();\r
             if(integralEquation.contains("="))\r
                 result = UnitUtils.matchUnits(\r
                         graph, \r