]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Getting closer
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 31 Mar 2014 11:16:46 +0000 (11:16 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 31 Mar 2014 11:16:46 +0000 (11:16 +0000)
refs #4765

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29221 ac1ea38d-2e2b-0410-8846-a27921b304fc

34 files changed:
fi.semantum.sysdyn.solver/META-INF/MANIFEST.MF
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Addition.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/And.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Application.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Argument.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ArraySliceExpression.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Assignment.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Constant.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Declaration.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Derivate.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Derivative.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Division.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseProduct.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Equals.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ForArray.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/GreaterOrEqualThan.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/GreaterThan.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IExpression.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IfThenElse.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessOrEqualThan.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/LessThan.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Multiplication.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Negation.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NotEquals.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Or.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ParameterDeclaration.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Subtraction.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/TimeVariable.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Variable.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableDeclaration.java

index 7f8fe97d19ac5dcb28aaff4086048b5e9ba9a302..3256e51361bff8c9fb4984c3f4df3bca96a84c30 100644 (file)
@@ -7,7 +7,8 @@ Bundle-Activator: fi.semantum.sysdyn.solver.Activator
 Bundle-Vendor: Semantum Oy
 Require-Bundle: org.eclipse.core.runtime,
  org.simantics.utils;bundle-version="1.1.0",
- org.simantics.databoard;bundle-version="0.6.5"
+ org.simantics.databoard;bundle-version="0.6.5",
+ org.simantics.utils.datastructures;bundle-version="1.1.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-ActivationPolicy: lazy
 Export-Package: fi.semantum.sysdyn.solver
index 27ee6841018a5f8788a7df75d5b6e6a6fdb2c49e..a407ca4e2dec1adcc04b73f440e49f2b88514341 100644 (file)
@@ -61,4 +61,11 @@ public class Addition implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index d77a191c0972e1653f330af130da141fef10bfe4..dc3ab53d03f92e0821797fc86514ff45242692aa 100644 (file)
@@ -13,6 +13,8 @@ package fi.semantum.sysdyn.solver;
 import java.util.ArrayList;\r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class And implements IExpression {\r
        \r
     public IExpression[] exps;\r
@@ -59,4 +61,10 @@ public class And implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               for(IExpression exp : exps) exp.accept(visitor);\r
+       }\r
+       \r
 }\r
index c9c2282c923e576c1896f88607db1b94f93cab41..a59ece28b39daa68866aeade33baba2d9cfdabbb 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Application implements IExpression {\r
 \r
        public String name;\r
@@ -48,4 +50,11 @@ public class Application implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               for(Argument a : args.args)\r
+                       a.modification.accept(visitor);\r
+       }\r
+       \r
 }\r
index 3a7013f4eddc77158f81711c13845a7935135c06..2712ab905037bf5dd5fa026b1d7a0cebd63a17df 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Argument implements IExpression {\r
 \r
        public String name;\r
@@ -47,4 +49,10 @@ public class Argument implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               modification.accept(visitor);\r
+       }\r
+       \r
 }\r
index 72fcd4050e1532c88b1aceb62e6baf7f6fd98607..4544936274cd53fdc4bcebd8b3e900421320d939 100644 (file)
@@ -15,6 +15,7 @@ import java.util.Collection;
 import java.util.Iterator;\r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
 import fi.semantum.sysdyn.solver.SolverUtils.Slice;\r
 \r
 public class Array implements IExpression {\r
@@ -227,4 +228,15 @@ public class Array implements IExpression {
                return result;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               for(int i=0;i<elements.size();i++) {\r
+                       Object element = elements.get(i);\r
+                       if(element instanceof IExpression) {\r
+                               ((IExpression)element).accept(visitor);\r
+                       }\r
+               }\r
+       }\r
+       \r
 }\r
index b2813e698071da7e4b4a7c1e1342055b00b24961..9ed04b14069c72af50efe648cc0f30216b09f8cc 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class ArraySliceExpression implements IExpression {\r
 \r
        public IExpression start;\r
@@ -54,4 +56,11 @@ public class ArraySliceExpression implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               start.accept(visitor);\r
+               end.accept(visitor);\r
+       }\r
+       \r
 }\r
index fd7f1d06932b6016278a7dcf4474a9cf88e15aa3..acaee56a3aefd664b8b2994dd047a015f7cad7e2 100644 (file)
@@ -10,7 +10,7 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
-import org.omg.PortableInterceptor.SUCCESSFUL;\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
 \r
 public class Assignment {\r
        \r
index d00f236293d2da28c44a8653674bb8606d5112b7..9fd58ddd0df7279a1443e1f22c30706b18a7e659 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Constant implements IExpression {\r
 \r
        public Object value;\r
@@ -49,5 +51,10 @@ public class Constant implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+       }\r
+       \r
 }\r
 \r
index a1490f5270d93fdf7ae7e6069f3b40b234718a5c..90b2cfc2a1eb7dfaddd305855b23eafb8097e5dd 100644 (file)
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.nio.channels.UnsupportedAddressTypeException;\r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Declaration implements IExpression {\r
 \r
        public Variable variable;\r
@@ -48,4 +51,9 @@ public class Declaration implements IExpression {
                throw new UnsupportedOperationException();\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
+\r
 }\r
index 426e3fa72ad39adb9d53aaddfaa902e827b28f87..ef3b60121f104e749982486eabaeacd86550b530 100644 (file)
@@ -12,11 +12,13 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Derivate implements IExpression {\r
 \r
        private Variable variable;\r
        private IExpression[] subscripts;\r
-       private IExpression e;\r
+       public IExpression e;\r
        \r
        public Derivate(Variable variable, IExpression[] subscripts, IExpression e) {\r
                this.variable = variable;\r
@@ -64,4 +66,14 @@ public class Derivate implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               if(subscripts != null) {\r
+                       for(int i=0;i<subscripts.length;i++)\r
+                               subscripts[i].accept(visitor);\r
+               }\r
+               e.accept(visitor);\r
+       }\r
+       \r
 }\r
index 51dbf974ae394ed64f8f4d62efc603e1fee3a814..037a17453fa4c3eb558b7aa8998dec32ac90f9f5 100644 (file)
@@ -45,4 +45,9 @@ public class Derivative implements IExpression {
                throw new UnsupportedOperationException();\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
+       \r
 }\r
index 0f643949c853c569ec05cb762de3be69c7e4096f..6b7658f20de06f4cea1fd128f15109fe553794ec 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Division implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -66,4 +68,11 @@ public class Division implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index d8d3aafb70908657313e88f493311b6bfb89922b..5888029f8b1f713cd64e9f3fc706379692ec1565 100644 (file)
@@ -14,6 +14,8 @@ import java.util.Collection;
 import java.util.Iterator;\r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class ElementwiseProduct implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -68,4 +70,11 @@ public class ElementwiseProduct implements IExpression {
                return this;\r
        }\r
 \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index a92ed1a32adf2bf2282756e1e1fdadee78d7a43e..110c99bd97c06f657b862b96f53900dd0d29a0df 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Equals implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -55,4 +57,11 @@ public class Equals implements IExpression {
                return this;\r
        }\r
 \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index 1f66818ac9ba2a4bcdd11c60bddbd087949cc175..2a2687329d90be5ce8d2c3824cb08d3ec38fb789 100644 (file)
@@ -14,6 +14,8 @@ import java.util.ArrayList;
 import java.util.Collection;\r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class ForArray implements IExpression {\r
 \r
        private ArrayList<Object> elements = new ArrayList<Object>();\r
@@ -110,4 +112,15 @@ public class ForArray implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               for(int i=0;i<elements.size();i++) {\r
+                       Object o = elements.get(i);\r
+                       if(o instanceof IExpression) {\r
+                               ((IExpression)o).accept(visitor);\r
+                       }\r
+               }\r
+       }\r
+       \r
 }\r
index 6d4620146e00ef9748e6b4e8b59a080178d52cc0..4876199cfaa7d106bbbe6da7210a95d55cbb95f8 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class GreaterOrEqualThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -49,4 +51,11 @@ public class GreaterOrEqualThan implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index 21d81acc5364a2d24a351c8137e1c67692fedac4..1a9791d6652c55a439a0189530d0d4ef6ac9dd15 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class GreaterThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -49,4 +51,11 @@ public class GreaterThan implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index 6377a3ad513eea1e391f9fa29b02c2f199e5f959..5d58d7f3fe71028fe081766d278a9b465b11ff7b 100644 (file)
@@ -14,9 +14,14 @@ import java.util.Map;
 \r
 public interface IExpression {\r
 \r
+       public interface ExpressionVisitor {\r
+               void visit(IExpression expression);\r
+       }\r
+       \r
        public Object evaluate(IEnvironment environment);\r
        public IExpression withBase(IFrame frame, String prefix);\r
        public Object getPossibleConstant();\r
        public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies);\r
+       public void accept(ExpressionVisitor visitor);\r
        \r
 }\r
index 2176faf49c1be298a0078e83119195a91504bc54..a95c99e1265edba02eda76e22c31ab35c1e6069b 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class IfThenElse implements IExpression {\r
        \r
     public IExpression exp;\r
@@ -56,4 +58,12 @@ public class IfThenElse implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp.accept(visitor);\r
+               t.accept(visitor);\r
+               e.accept(visitor);\r
+       }\r
+\r
 }\r
index 7c2f28707e198cfb44c0d5dc24a0ffa8c9a1cc72..b9ab757e3900428ecd3204f8e81449636cd05574 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class LessOrEqualThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -51,4 +53,11 @@ public class LessOrEqualThan implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index 9a8085384eda48bd838361a633bd577df4567a44..db79808367fb69b8baefdd83ea78316a44a51131 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class LessThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -51,4 +53,11 @@ public class LessThan implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index 00397a37d3cefa148736d570a415ac051a0f099d..cc4724ab3fad0d7320b38951f79855778b76b5a8 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Multiplication implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -62,4 +64,11 @@ public class Multiplication implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index cdf125bae2928a02ff74428a53ffa10917d0b65e..15e9e2df8fa13057435be334767d431e21ecbd8a 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Negation implements IExpression {\r
        \r
     public IExpression exp;\r
@@ -46,4 +48,10 @@ public class Negation implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp.accept(visitor);\r
+       }\r
+       \r
 }\r
index 41ca66a5b47c03d5fd420c47d0df38e4c88d9ed5..5c5499cecc435f801a96796bcb19055da6904345 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class NotEquals implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -49,4 +51,11 @@ public class NotEquals implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index 287d61807e1bd5e45492816ff88364cf1b919ef0..cbafd0d9c6fc3afe1d1b0f3b008e4e56f5820802 100644 (file)
@@ -23,5 +23,10 @@ public class NullModification implements IExpression {
        public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
                return this;\r
        }\r
+       \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+       }\r
 \r
 }\r
index e08c3282151961afb82cd2cba52f1683e22cd629..375c9240e1e70e1d74cb9b7b59ecdd824c6b4e3d 100644 (file)
@@ -13,6 +13,8 @@ package fi.semantum.sysdyn.solver;
 import java.util.ArrayList;\r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Or implements IExpression {\r
        \r
     public IExpression[] exps;\r
@@ -59,4 +61,10 @@ public class Or implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               for(IExpression exp : exps) exp.accept(visitor);\r
+       }\r
+       \r
 }\r
index 1aa811f74d0af9b2430286b906c6ac30438ef06f..72cbab5e08ade7e3be89addaf70c2c97deba1e04 100644 (file)
@@ -49,4 +49,9 @@ public class ParameterDeclaration implements IExpression {
                throw new UnsupportedOperationException();\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
+       \r
 }\r
index bcd050d4a931231b88ea8f9b2343ef97678248a3..ca56e4bb48762a4e26ea70ac801e391f7bc3dd3f 100644 (file)
@@ -13,7 +13,15 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.io.StringReader;\r
 import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
 \r
+import org.simantics.utils.datastructures.MapList;\r
+\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
 import fi.semantum.sysdyn.solver.Model.Globals;\r
 import fi.semantum.sysdyn.solver.parser.ModelParser;\r
 import fi.semantum.sysdyn.solver.parser.SimpleNode;\r
@@ -27,6 +35,7 @@ public class Solver {
        private Environment env;\r
 \r
        private Object[] newValues;\r
+       private Object[] derivatives;\r
        \r
        private double step;\r
        private double start;\r
@@ -51,6 +60,52 @@ public class Solver {
                ready = false;\r
        }\r
        \r
+       private void sortAssignments() {\r
+\r
+               final MapList<String,Assignment> asses = new MapList<String,Assignment>();\r
+               for(Assignment ass : model.assignments) asses.add(ass.target.base.name, ass);\r
+               \r
+               final List<VariableBase> found = new ArrayList<VariableBase>();\r
+               final Set<String> visited = new HashSet<String>();\r
+\r
+               ExpressionVisitor sortVisitor = new ExpressionVisitor() {\r
+                       @Override\r
+                       public void visit(IExpression expression) {\r
+                               if(expression instanceof Variable) {\r
+                                       Variable var = (Variable)expression;\r
+                                       if(visited.add(var.base.name)) {\r
+                                               for(Assignment ass : asses.getValues(var.base.name)) {\r
+                                                       ass.expression.accept(this);\r
+                                                       if(ass.subscripts != null)\r
+                                                               for(IExpression e : ass.subscripts) e.accept(this);\r
+                                               }\r
+                                               found.add(var.base);\r
+                                       }\r
+                               }\r
+                       }\r
+               };\r
+               \r
+               // Sort assignments\r
+               for(Assignment ass : model.assignments) {\r
+                       ass.target.accept(sortVisitor);\r
+               }\r
+\r
+               Collections.sort(model.assignments, new Comparator<Assignment>() {\r
+\r
+                       @Override\r
+                       public int compare(Assignment o1, Assignment o2) {\r
+                               int i1 = found.indexOf(o1.target.base); \r
+                               int i2 = found.indexOf(o2.target.base);\r
+                               if(i1 < i2) return -1;\r
+                               else if (i1 > i2) return 1;\r
+                               return 0;\r
+                       }\r
+\r
+                       \r
+               });\r
+               \r
+       }\r
+       \r
        public void prepare(String input) throws Exception {\r
                \r
                long startNanos = System.nanoTime();\r
@@ -79,11 +134,14 @@ public class Solver {
                \r
                env.valueTable = new Object[size+STACK_SIZE];\r
                \r
+               sortAssignments();\r
+               \r
                model.assignmentArray = model.assignments.toArray(new Assignment[model.assignments.size()]);\r
                model.derivativeArray = model.derivatives.toArray(new Assignment[model.derivatives.size()]);\r
                model.parameterArray = model.parameters.toArray(new ParameterDeclaration[model.parameters.size()]);\r
                \r
                newValues = new Object[Math.max(model.assignments.size(),model.derivatives.size())];\r
+               derivatives = new Object[model.derivatives.size()];\r
                \r
                int condition = 1;\r
                int loops = 0;\r
@@ -185,6 +243,8 @@ public class Solver {
                return env.getValueArray();\r
        }\r
        \r
+       \r
+       \r
        public void step() {\r
                // TODO: do something more sensible if the solver is not ready\r
                if (!ready) return;\r
@@ -192,10 +252,19 @@ public class Solver {
                Assignment[] assignments = model.assignmentArray;\r
                Assignment[] derivatives = model.derivativeArray;\r
                \r
+               // first we evaluate, integrate and assign derivatives at previous time step\r
+               for(int i=0;i<model.derivatives.size();i++) {\r
+                       newValues[i] = derivatives[i].expression.evaluate(env);\r
+               }\r
+               for(int i=0;i<model.derivatives.size();i++) {\r
+                       derivatives[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
+               }\r
+\r
+               // Increment time\r
                // should probably be wrapped inside a method or something\r
                env.time += env.step;\r
-               \r
-               // Solve algebraic equations\r
+\r
+               // Next solve algebraic equations\r
                for(int i=0;i<assignments.length;i++) {\r
                        newValues[i] = assignments[i].expression.evaluate(env);\r
                }\r
@@ -203,13 +272,6 @@ public class Solver {
                        assignments[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
                }\r
 \r
-               // Solve derivatives\r
-               for(int i=0;i<model.derivatives.size();i++) {\r
-                       newValues[i] = derivatives[i].expression.evaluate(env);\r
-               }\r
-               for(int i=0;i<model.derivatives.size();i++) {\r
-                       derivatives[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
-               }\r
        }\r
        \r
        // TODO: implement some on the fly parameter change stuff for different experiment types\r
index f7f8763d32767665308ef28b6d45a39d3d4944bc..79d1404caf5c64594da13de9b89e8819627bfef4 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class Subtraction implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -50,4 +52,11 @@ public class Subtraction implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+               exp1.accept(visitor);\r
+               exp2.accept(visitor);\r
+       }\r
+       \r
 }\r
index d2d3e4c6044f5cf73f41bf242a6139233b26cb60..19e5f299e9c035c2f5e3d54822995db15dc3a5cc 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 public class TimeVariable implements IExpression {\r
 \r
        @Override\r
@@ -40,4 +42,9 @@ public class TimeVariable implements IExpression {
                return this;\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+       }\r
+       \r
 }\r
index 183d0cc3be23cfada5bb2d5b8841c58fe64aa112..8144fba00de810baf85a3f4d9691845cc99620ea 100644 (file)
@@ -13,6 +13,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 \r
 \r
 public class Variable implements IExpression {\r
@@ -236,4 +238,9 @@ public class Variable implements IExpression {
                \r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               visitor.visit(this);\r
+       }\r
+       \r
 }\r
index 1466574bd3b71002e442951eaed9ba246abe588b..b00a48cf9c892c2670488ac5f43428e34462fa7e 100644 (file)
@@ -10,8 +10,6 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
-import java.util.Arrays;\r
-\r
 import fi.semantum.sysdyn.solver.SolverUtils.Slice;\r
 \r
 public class VariableBase {\r
index 08c8a49d78d7b03ef0f231b57ed085039a74ac27..51164eac35cdce8fe1332e463779339d2cc139d4 100644 (file)
@@ -12,6 +12,8 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Map;\r
 \r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
 \r
 public class VariableDeclaration implements IExpression {\r
 \r
@@ -53,4 +55,9 @@ public class VariableDeclaration implements IExpression {
                throw new UnsupportedOperationException();\r
        }\r
        \r
+       @Override\r
+       public void accept(ExpressionVisitor visitor) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
+       \r
 }\r