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

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

45 files changed:
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/ArgumentList.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/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/EnumElementsVariableBase.java [new file with mode: 0644]
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java [new file with mode: 0644]
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.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/Function.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/IFrame.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/LineReader.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/MultiStatement.java [new file with mode: 0644]
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/NodeClass.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NotEquals.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java [new file with mode: 0644]
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/Parser.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/SolverUtils.java [new file with mode: 0644]
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
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jjt

index 5e30339752068f92732c8167416e06d27dead1b8..27ee6841018a5f8788a7df75d5b6e6a6fdb2c49e 100644 (file)
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+\r
 public class Addition implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -27,9 +31,17 @@ public class Addition implements IExpression {
     \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
-               Double d1 = (Double)exp1.evaluate(environment);\r
-               Double d2 = (Double)exp2.evaluate(environment);\r
-               return d1 + d2;\r
+               Object o1 = exp1.evaluate(environment);\r
+               Object o2 = exp2.evaluate(environment);\r
+               if(o1 instanceof Double && o2 instanceof Double) {\r
+                       return (Double)o1 + (Double)o2;\r
+               }\r
+               if(o1 instanceof Array && o2 instanceof Array) {\r
+                       Array la = (Array)o1;\r
+                       Array ra = (Array)o2;\r
+                       return la.add(ra);\r
+               }\r
+               throw new IllegalStateException();\r
        }\r
        \r
        @Override\r
@@ -37,4 +49,16 @@ public class Addition implements IExpression {
                return new Addition(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
     \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 18f3f1588b59aa1101edd48ec45a177f10b17871..d77a191c0972e1653f330af130da141fef10bfe4 100644 (file)
 package fi.semantum.sysdyn.solver;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Map;\r
 \r
 public class And implements IExpression {\r
        \r
-    public ArrayList<IExpression> exps;\r
+    public IExpression[] exps;\r
     \r
     public And(ArrayList<IExpression> exps) {\r
-       this.exps = exps;\r
+       this.exps = exps.toArray(new IExpression[exps.size()]);\r
     }\r
     \r
     @Override\r
     public String toString() {\r
        StringBuilder b = new StringBuilder();\r
-       b.append(exps.get(0));\r
-       for(int i=1;i<exps.size();i++) {\r
+       b.append(exps[0]);\r
+       for(int i=1;i<exps.length;i++) {\r
                b.append(" and ");\r
-               b.append(exps.get(i));\r
+               b.append(exps[i]);\r
        }\r
        return b.toString();\r
     }\r
@@ -44,6 +45,18 @@ public class And implements IExpression {
                for(IExpression e : exps) ne.add(e.withBase(frame, prefix));\r
                return new And(ne);\r
        }\r
-\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               for(int i=0;i<exps.length;i++) {\r
+                       exps[i] = exps[i].rewrite(frame, copies);\r
+               }\r
+               return this;\r
+       }\r
        \r
 }\r
index a8089c411626b3ae23d47b6a10f092a73c44fe01..c9c2282c923e576c1896f88607db1b94f93cab41 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Application implements IExpression {\r
 \r
        public String name;\r
@@ -34,5 +36,16 @@ public class Application implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new Application(name, args.withBase(frame, prefix));\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               args = args.rewrite(frame, copies);\r
+               return this;\r
+       }\r
        \r
 }\r
index cbe534d024e6ec0ff3fb446f8670561f271666fb..3a7013f4eddc77158f81711c13845a7935135c06 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Argument implements IExpression {\r
 \r
        public String name;\r
@@ -32,7 +34,17 @@ public class Argument implements IExpression {
        \r
        @Override\r
        public IExpression withBase(IFrame frame, String prefix) {\r
-               return new Argument(name, modification.withBase(frame, prefix));\r
+               return new Argument(prefix+name, modification.withBase(frame, prefix));\r
+       }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       public Argument rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               modification = modification.rewrite(frame, copies);\r
+               return this;\r
        }\r
        \r
 }\r
index f2e637ad1e1ad0c314835b43a658e5666f267464..02d4d439690b6544607c008626aa9d25431e2149 100644 (file)
 package fi.semantum.sysdyn.solver;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Map;\r
 \r
 public class ArgumentList {\r
        \r
-    public ArrayList<Argument> args;\r
+    public Argument[] args;\r
     public String op;\r
     \r
     public ArgumentList(ArrayList<Argument> args) {\r
-       this.args = args;\r
+       this.args = args.toArray(new Argument[args.size()]);\r
        this.op = "";\r
     }\r
 \r
     public ArgumentList(ArrayList<Argument> args, String op) {\r
-       this.args = args;\r
+       this.args = args.toArray(new Argument[args.size()]);\r
        this.op = op;\r
     }\r
 \r
     public ArgumentList(String op) {\r
-       this.args = new ArrayList<Argument>();\r
+       this.args = new Argument[0];\r
        this.op = op;\r
     }\r
 \r
     @Override\r
     public String toString() {\r
-       if(args.size() == 0) return "()";\r
+       if(args.length == 0) return "()";\r
        StringBuilder b = new StringBuilder();\r
-       b.append("(" + args.get(0));\r
-       for(int i=1;i<args.size();i++) {\r
+       b.append("(" + args[0]);\r
+       for(int i=1;i<args.length;i++) {\r
                b.append(", ");\r
-               b.append(args.get(i));\r
+               b.append(args[i]);\r
        }\r
        b.append(")");\r
        return b.toString();\r
@@ -51,4 +52,10 @@ public class ArgumentList {
        return new ArgumentList(a2, op);\r
     }\r
     \r
+       public ArgumentList rewrite(IFrame frame, Map<String,VariableBase> copies) {\r
+               for(int i=0;i<args.length;i++)\r
+                       args[i] = args[i].rewrite(frame, copies);\r
+               return this;\r
+       }\r
+    \r
 }\r
index e6b9a4558d0a2684c6555e7df13365434cafe2e4..72fcd4050e1532c88b1aceb62e6baf7f6fd98607 100644 (file)
@@ -12,6 +12,10 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+\r
+import fi.semantum.sysdyn.solver.SolverUtils.Slice;\r
 \r
 public class Array implements IExpression {\r
 \r
@@ -21,9 +25,10 @@ public class Array implements IExpression {
                \r
        }\r
        \r
-       public void addElement(Object element) {\r
+       public Array addElement(Object element) {\r
                if(element instanceof Constant) addElement(((Constant)element).value);\r
                else elements.add(element);\r
+               return this;\r
        }\r
        \r
        public void setElement(int index, Object element) {\r
@@ -91,5 +96,135 @@ public class Array implements IExpression {
                }\r
                return result;\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               for(int i=0;i<elements.size();i++) {\r
+                       Object element = elements.get(i);\r
+                       if(element instanceof IExpression) {\r
+                               elements.set(i, ((IExpression)element).rewrite(frame, copies));\r
+                       }\r
+               }\r
+               return this;\r
+       }\r
+       \r
+       public int dimension() {\r
+               int result = 0;\r
+               for(int i=0;i<elements.size();i++) {\r
+                       Object element = elements.get(i);\r
+                       if(element instanceof Array) {\r
+                               result += ((Array)element).dimension();\r
+                       } else {\r
+                               result++;\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+       \r
+       public Array mul(double d) {\r
+               Array result = new Array();\r
+               for(Object o : elements) {\r
+               if(o instanceof Double) {\r
+                       result.addElement((Double)o*d);\r
+               } else if (o instanceof Array) {\r
+                       result.addElement(((Array)o).mul(d));\r
+               } else {\r
+                       throw new IllegalStateException();\r
+               }\r
+               }\r
+               return result;\r
+       }\r
+       \r
+       public boolean validateDimensions(int[] dimensions, int index) {\r
+               if(elements.size() != dimensions[index]) return false;\r
+               if(index < dimensions.length-1) {\r
+                       for(Object o : elements) {\r
+                               if(!(o instanceof Array)) return false;\r
+                               Array arr = (Array)o;\r
+                               if(!arr.validateDimensions(dimensions, index+1)) return false;\r
+                       }\r
+               } else {\r
+                       for(Object o : elements) {\r
+                               if((o instanceof Array)) return false;\r
+                       }\r
+               }\r
+               return true;\r
+       }\r
+\r
+       public Array slice(Slice[] indices) {\r
+               \r
+               if(indices.length == 1) {\r
+                       Array result = new Array();\r
+                       for(int i=indices[0].start;i<=indices[0].end;i++) {\r
+                               result.addElement(element(i));\r
+                       }\r
+                       return result;\r
+               }\r
+               \r
+               if(indices.length != 2) throw new IllegalStateException();\r
+               \r
+               if(indices[1] == Slice.FULL) {\r
+                       return (Array)elements.get(indices[0].start);\r
+               } else {\r
+                       Array result = new Array();\r
+                       for(Object o : elements) {\r
+                               Array a = (Array)o;\r
+                               result.addElement(a.element(indices[1].start));\r
+                       }\r
+                       return result;\r
+               }\r
+               \r
+       }\r
+       \r
+       public Array add(Array other) {\r
+               Array result = new Array();\r
+               Collection<Object> lae = elements();\r
+               Collection<Object> rae = other.elements();\r
+               if(lae.size() != rae.size()) throw new IllegalStateException();\r
+               Iterator<Object> li = lae.iterator();\r
+               Iterator<Object> ri = rae.iterator();\r
+               for(int i=0;i<lae.size();i++) {\r
+                       Object l = li.next();\r
+                       Object r = ri.next();\r
+                       if(l instanceof Double && r instanceof Double) {\r
+                               result.addElement(((Double)l)+((Double)r));\r
+                       } else if (l instanceof Array && r instanceof Array) {\r
+                               Array arr = (Array)l;\r
+                               Array otherArr = (Array)r;\r
+                               result.addElement(arr.add(otherArr));\r
+                       } else {\r
+                               throw new IllegalStateException();\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+\r
+       public Array mul(Array other) {\r
+               Array result = new Array();\r
+               Collection<Object> lae = elements();\r
+               Collection<Object> rae = other.elements();\r
+               if(lae.size() != rae.size()) throw new IllegalStateException();\r
+               Iterator<Object> li = lae.iterator();\r
+               Iterator<Object> ri = rae.iterator();\r
+               for(int i=0;i<lae.size();i++) {\r
+                       Object l = li.next();\r
+                       Object r = ri.next();\r
+                       if(l instanceof Double && r instanceof Double) {\r
+                               result.addElement(((Double)l)*((Double)r));\r
+                       } else if (l instanceof Array && r instanceof Array) {\r
+                               Array arr = (Array)l;\r
+                               Array otherArr = (Array)r;\r
+                               result.addElement(arr.add(otherArr));\r
+                       } else {\r
+                               throw new IllegalStateException();\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
        \r
 }\r
index e7017bfe380b46999707a64a993aa41161e03587..b2813e698071da7e4b4a7c1e1342055b00b24961 100644 (file)
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class ArraySliceExpression implements IExpression {\r
 \r
-       final public IExpression start;\r
-       final public IExpression end;\r
+       public IExpression start;\r
+       public IExpression end;\r
        \r
        public ArraySliceExpression(IExpression start, IExpression end) {\r
                this.start = start;\r
@@ -39,5 +41,17 @@ public class ArraySliceExpression implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new ArraySliceExpression(start.withBase(frame, prefix), end.withBase(frame, prefix));\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               start = start.rewrite(frame, copies);\r
+               end = end.rewrite(frame, copies);\r
+               return this;\r
+       }\r
        \r
 }\r
index 56bd8b641a4701a02d78ad1545f44b241f02cf72..d00f236293d2da28c44a8653674bb8606d5112b7 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Constant implements IExpression {\r
 \r
        public Object value;\r
@@ -36,6 +38,16 @@ public class Constant implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return this;\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               return this;\r
+       }\r
        \r
 }\r
 \r
index 2ef7fd73cd63945f8cbc6e4709e6869d58e4f185..a1490f5270d93fdf7ae7e6069f3b40b234718a5c 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Declaration implements IExpression {\r
 \r
        public Variable variable;\r
@@ -35,5 +37,15 @@ public class Declaration implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                throw new UnsupportedOperationException();\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
        \r
 }\r
index b67074e8ae9f1e4ccaef2996341b2150c0ce5b62..426e3fa72ad39adb9d53aaddfaa902e827b28f87 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Derivate implements IExpression {\r
 \r
        private Variable variable;\r
@@ -26,7 +28,7 @@ public class Derivate implements IExpression {
        public Object evaluate(IEnvironment _environment) {\r
                Environment environment = (Environment)_environment;\r
 //             Double old = (Double)environment.getValue(variable.name + variable.subscriptKey());\r
-               Double old = (Double)environment.getValue(variable.index(_environment, subscripts));\r
+               Double old = (Double)environment.getValue(variable.base.index(_environment, subscripts));\r
                return old+environment.step*(Double)e.evaluate(environment);\r
        }\r
        \r
@@ -46,5 +48,20 @@ public class Derivate implements IExpression {
                        return new Derivate((Variable)variable.withBase(frame, prefix), null, e.withBase(frame, prefix));\r
                }\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               if(subscripts != null) {\r
+                       for(int i=0;i<subscripts.length;i++)\r
+                               subscripts[i] = subscripts[i].rewrite(frame, copies);\r
+               }\r
+               e = e.rewrite(frame, copies);\r
+               return this;\r
+       }\r
        \r
 }\r
index f3fc63318073a2854f7e91f05adc6a239b463dc1..51dbf974ae394ed64f8f4d62efc603e1fee3a814 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Derivative implements IExpression {\r
 \r
        Variable variable;\r
@@ -32,5 +34,15 @@ public class Derivative implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                throw new UnsupportedOperationException();\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
        \r
 }\r
index 2d31b10783128f408ab27066f99cc8fa6bd4486e..0f643949c853c569ec05cb762de3be69c7e4096f 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Division implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -51,5 +53,17 @@ public class Division implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new Division(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
 \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index c2b9a5f67b96800bdd2cc36d2ca9f473b4945778..d8d3aafb70908657313e88f493311b6bfb89922b 100644 (file)
@@ -12,6 +12,7 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Collection;\r
 import java.util.Iterator;\r
+import java.util.Map;\r
 \r
 public class ElementwiseProduct implements IExpression {\r
        \r
@@ -54,5 +55,17 @@ public class ElementwiseProduct implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new ElementwiseProduct(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
        \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+\r
 }\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumElementsVariableBase.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumElementsVariableBase.java
new file mode 100644 (file)
index 0000000..d80d5df
--- /dev/null
@@ -0,0 +1,46 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 VTT Technical Research Centre of Finland.\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 fi.semantum.sysdyn.solver;\r
+\r
+public class EnumElementsVariableBase extends VariableBase {\r
+\r
+       private Array result;\r
+       \r
+       public EnumElementsVariableBase(Model clazz) {\r
+               super("elements");\r
+               result = new Array();\r
+               for(VariableDeclaration vd : clazz.variables) {\r
+                       Constant c = (Constant)vd.modification.args[0].modification;\r
+                       result.addElement(c.value);\r
+               }\r
+       }\r
+\r
+       public EnumElementsVariableBase(String name, Array result) {\r
+               super(name);\r
+               this.result = result;\r
+       }\r
+\r
+       @Override\r
+       public Object evaluate(IEnvironment environment, IExpression[] subscripts) {\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       VariableBase withBase(String prefix) {\r
+               return new EnumElementsVariableBase(prefix+name, result);\r
+       }\r
+       \r
+       @Override\r
+       public IExpression getPossibleConstant() {\r
+               return result;\r
+       }\r
+       \r
+}\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java
new file mode 100644 (file)
index 0000000..6ed388f
--- /dev/null
@@ -0,0 +1,42 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 VTT Technical Research Centre of Finland.\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 fi.semantum.sysdyn.solver;\r
+\r
+public class EnumSizeVariableBase extends VariableBase {\r
+\r
+       private Double value;\r
+       \r
+       public EnumSizeVariableBase(Model clazz) {\r
+               super("size");\r
+               value = (double)clazz.variables.size();\r
+       }\r
+\r
+       public EnumSizeVariableBase(String name, double value) {\r
+               super(name);\r
+               this.value = value;\r
+       }\r
+\r
+       @Override\r
+       public Object evaluate(IEnvironment environment, IExpression[] subscripts) {\r
+               return value;\r
+       }       \r
+\r
+       @Override\r
+       VariableBase withBase(String prefix) {\r
+               return new EnumSizeVariableBase(prefix+name, value);\r
+       }\r
+       \r
+       @Override\r
+       public IExpression getPossibleConstant() {\r
+               return new Constant(value.toString());\r
+       }\r
+       \r
+}\r
index 6ab0d2fdc784e7fcd5d84baeb7d80c92865872f6..1e1474087b83459f5546e51194c2acb2ced72856 100644 (file)
@@ -12,7 +12,9 @@
 package fi.semantum.sysdyn.solver;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Collection;\r
 import java.util.HashMap;\r
+import java.util.Iterator;\r
 import java.util.Map;\r
 import java.util.TreeMap;\r
 \r
@@ -85,14 +87,40 @@ final public class Environment implements IEnvironment, ISystem {
                });\r
                model.functions.put("zidz", new Fn1(2) {\r
 \r
-                       @Override\r
-                       public Object evaluate(IEnvironment environment, int argc) {\r
-                               Double p1 = (Double)environment.getValue(0);\r
-                               Double p2 = (Double)environment.getValue(1);\r
+                       private Object evaluate(Double p1, Double p2) {\r
                                if(Math.abs(p2) < 1e-12) return 0.0;\r
                                else return p1 / p2;\r
                        }\r
                        \r
+                       @Override\r
+                       public Object evaluate(IEnvironment environment, int argc) {\r
+                               Object o1 = environment.getValue(0);\r
+                               Object o2 = environment.getValue(1);\r
+                               \r
+                               if(o1 instanceof Double && o2 instanceof Double) {\r
+                                       return evaluate((Double)o1, (Double)o2);\r
+                               }\r
+                               if(o1 instanceof Array && o2 instanceof Array) {\r
+                                       Array la = (Array)o1;\r
+                                       Array ra = (Array)o2;\r
+                                       Collection<Object> lae = la.elements();\r
+                                       Collection<Object> rae = ra.elements();\r
+                                       if(lae.size() != rae.size()) throw new UnsupportedOperationException();\r
+                                       Iterator<Object> li = lae.iterator();\r
+                                       Iterator<Object> ri = rae.iterator();\r
+                                       Array result = new Array();\r
+                                       for(int i=0;i<lae.size();i++) {\r
+                                               double ld = (Double)li.next();\r
+                                               double rd = (Double)ri.next();\r
+                                               result.addElement(evaluate(ld,rd));\r
+                                       }\r
+                                       return result;\r
+                               }\r
+                               throw new IllegalStateException();\r
+                               \r
+                               \r
+                       }\r
+                       \r
                });\r
                model.functions.put("xidz", new Fn1(3) {\r
 \r
@@ -261,6 +289,30 @@ final public class Environment implements IEnvironment, ISystem {
                        }\r
                        \r
                });             \r
+               model.functions.put("cat", new Fn1(10) {\r
+                       \r
+                       @Override\r
+                       public Object evaluate(IEnvironment _environment, int argc) {\r
+                       \r
+                               Double dim = (Double)_environment.getValue(0);\r
+                               \r
+                               Array array = new Array();\r
+                               for(int i=1;i<argc;i++) {\r
+                                       Object ar = _environment.getValue(i);\r
+                                       if (ar instanceof Array) {\r
+                                               Array a = (Array)ar;\r
+                                               for(Object o : a.elements())\r
+                                                       array.addElement(o);\r
+                                       } else {\r
+                                               throw new IllegalStateException();\r
+                                       }\r
+                               }\r
+                               \r
+                               return array;\r
+                               \r
+                       }\r
+                       \r
+               });             \r
        }\r
        \r
        public TreeMap<Double,Double> getHistory(String ident) {\r
@@ -316,26 +368,56 @@ final public class Environment implements IEnvironment, ISystem {
 \r
        double[] valueArray;\r
        \r
+       public void addIndexed(int dimensions[], ArrayList<String> keys, String prefix, int d) {\r
+\r
+               if(d == dimensions.length) {\r
+                       keys.add(prefix);\r
+                       return;\r
+               }\r
+\r
+               for(int i=0;i<dimensions[d];i++) {\r
+                       String prefix2 = prefix + "[" + i + "]";\r
+                       addIndexed(dimensions, keys, prefix2, d+1);\r
+               }\r
+               \r
+       }\r
+       \r
+       public void addVariable(String name, int[] dimensions, ArrayList<String> keys) {\r
+\r
+//             int dimensions[] = base.dimensions;\r
+               if(dimensions == null) {\r
+                       keys.add(name);\r
+               } else {\r
+                       addIndexed(dimensions, keys, name, 0);\r
+               }\r
+\r
+       }\r
+       \r
+       public void addVariable(Variable var, ArrayList<String> keys) {\r
+               addVariable(var.base.name, var.base.dimensions, keys);\r
+       }\r
+\r
        public String[] getValueKeyArray() {\r
                \r
                ArrayList<String> keys = new ArrayList<String>();\r
                \r
                for (int i = 0; i < model.assignmentArray.length; i++) {\r
-                       Variable v = model.assignmentArray[i].target;\r
-                       keys.add(v.toString());\r
+                       addVariable(model.assignmentArray[i].target, keys);\r
                }\r
                \r
                for (int i = 0; i < model.derivativeArray.length; i++) {\r
-                       Variable v = model.derivativeArray[i].target;\r
-                       keys.add(v.toString());\r
+                       addVariable(model.derivativeArray[i].target, keys);\r
                }\r
                \r
                // NOTE: there is room for optimization as parameter values that do not\r
                // change should only be obtained once (and parameter values that do\r
                // change are (possibly) included in assignments or derivatives)\r
                for(int i = 0; i < model.parameterArray.length; i++) {\r
-                       Variable v = model.parameterArray[i].variable;\r
-                       keys.add(v.toString());\r
+                       addVariable(model.parameterArray[i].variable, keys);\r
+               }\r
+               \r
+               for(Map.Entry<String,VariableBase> entry : model.copies.entrySet()) {\r
+                       addVariable(entry.getKey(), entry.getValue().dimensions, keys);\r
                }\r
 \r
                valueArray = new double[keys.size()];\r
@@ -351,12 +433,14 @@ final public class Environment implements IEnvironment, ISystem {
                \r
                for (int i = 0; i < model.assignmentArray.length; i++) {\r
                        Variable v = model.assignmentArray[i].target;\r
-                       valueArray[offset++] = (Double)getValue(v.index(this, null));\r
+                       for(int index=0;index<v.base.dimension();index++)\r
+                               valueArray[offset++] = (Double)getValue(v.base.index(this, null)+index);\r
                }\r
                \r
                for (int i = 0; i < model.derivativeArray.length; i++) {\r
                        Variable v = model.derivativeArray[i].target;\r
-                       valueArray[offset++] = (Double)getValue(v.index(this, null));\r
+                       for(int index=0;index<v.base.dimension();index++)\r
+                               valueArray[offset++] = (Double)getValue(v.base.index(this, null)+index);\r
                }\r
                \r
                // NOTE: there is room for optimization as parameter values that do not\r
@@ -364,7 +448,14 @@ final public class Environment implements IEnvironment, ISystem {
                // change are (possibly) included in assignments or derivatives)\r
                for(int i = 0; i < model.parameterArray.length; i++) {\r
                        Variable v = model.parameterArray[i].variable;\r
-                       valueArray[offset++] = (Double)getValue(v.index(this, null));\r
+                       for(int index=0;index<v.base.dimension();index++)\r
+                               valueArray[offset++] = (Double)getValue(v.base.index(this, null)+index);\r
+               }\r
+\r
+               for(Map.Entry<String,VariableBase> entry : model.copies.entrySet()) {\r
+                       VariableBase base = entry.getValue();\r
+                       for(int index=0;index<base.dimension();index++)\r
+                               valueArray[offset++] = (Double)getValue(base.index(this, null)+index);\r
                }\r
                \r
                return valueArray;\r
index 33e5d461aa04303171b654d460fc19416d348c29..a92ed1a32adf2bf2282756e1e1fdadee78d7a43e 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Equals implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -27,12 +29,30 @@ public class Equals implements IExpression {
     \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
-               return ((Double)exp1.evaluate(environment)) == ((Double)exp2.evaluate(environment));\r
+               Object v1 = exp1.evaluate(environment);\r
+               Object v2 = exp2.evaluate(environment);\r
+               if(!(v1 instanceof Double))\r
+                       throw new IllegalStateException();\r
+               if(!(v2 instanceof Double))\r
+                       throw new IllegalStateException();\r
+               return (v1.equals(v2));\r
        }\r
 \r
        @Override\r
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new Equals(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
 \r
 }\r
index 550d7ce9c06bb2a2aec87bf3036f8ea8395ec402..1f66818ac9ba2a4bcdd11c60bddbd087949cc175 100644 (file)
@@ -12,6 +12,7 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
+import java.util.Map;\r
 \r
 public class ForArray implements IExpression {\r
 \r
@@ -20,7 +21,11 @@ public class ForArray implements IExpression {
        public ForArray() {\r
                \r
        }\r
-       \r
+\r
+       public ForArray(ArrayList<Object> elements) {\r
+               this.elements = elements;\r
+       }\r
+\r
        public void addElement(Object element) {\r
                if(element instanceof Constant) addElement(((Constant)element).value);\r
                else elements.add(element);\r
@@ -78,7 +83,31 @@ public class ForArray implements IExpression {
        \r
        @Override\r
        public IExpression withBase(IFrame frame, String prefix) {\r
-               throw new UnsupportedOperationException();\r
+               ArrayList<Object> e = new ArrayList<Object>();\r
+               for(Object o : elements) {\r
+                       if(o instanceof IExpression) {\r
+                               e.add(((IExpression)o).withBase(frame, prefix));\r
+                       } else {\r
+                               e.add(o);\r
+                       }\r
+               }\r
+               return new ForArray(e);\r
+       }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               for(int i=0;i<elements.size();i++) {\r
+                       Object o = elements.get(i);\r
+                       if(o instanceof IExpression) {\r
+                               elements.set(i, ((IExpression)o).rewrite(frame, copies));\r
+                       }\r
+               }\r
+               return this;\r
        }\r
        \r
 }\r
index 6b76f5b2e7333dec36691316b3cae01a0b194549..3c32e6e98901dd2f076cefd6813a419c6249dc83 100644 (file)
@@ -11,7 +11,6 @@
 package fi.semantum.sysdyn.solver;\r
 \r
 import java.util.ArrayList;\r
-import java.util.Arrays;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
 \r
@@ -69,6 +68,11 @@ final public class Function implements Fn, IFrame {
                return base;\r
        }\r
        \r
+       @Override\r
+       public VariableBase getBase(VariableBase base, String prefix) {\r
+               throw new IllegalStateException();\r
+       }\r
+       \r
        public void prepare() {\r
                int nextIndex = 0;\r
                if(PRINT)\r
@@ -84,15 +88,15 @@ final public class Function implements Fn, IFrame {
                for(int i=0;i<inputs.size();i++) {\r
                        VariableDeclaration decl = inputs.get(i);\r
                        parameters[i] = decl.variable;\r
-                       decl.variable.base.tellSubscripts(decl.variable.subscripts);\r
+                       decl.variable.base.tellSubscripts(decl.variable.subscripts, null);\r
                }\r
                for(int i=0;i<outputs.size();i++) {\r
                        VariableDeclaration decl = outputs.get(i);\r
-                       decl.variable.base.tellSubscripts(decl.variable.subscripts);\r
+                       decl.variable.base.tellSubscripts(decl.variable.subscripts, null);\r
                }\r
                for(int i=0;i<internals.size();i++) {\r
                        VariableDeclaration decl = internals.get(i);\r
-                       decl.variable.base.tellSubscripts(decl.variable.subscripts);\r
+                       decl.variable.base.tellSubscripts(decl.variable.subscripts, null);\r
                }\r
        }\r
        \r
@@ -109,7 +113,12 @@ final public class Function implements Fn, IFrame {
        @Override\r
        public Object evaluateInput(IEnvironment environment, int argPosition) {\r
                VariableDeclaration decl = inputs.get(argPosition);\r
-               return decl.modification.args.get(0).modification.evaluate(environment);\r
+               return decl.modification.args[0].modification.evaluate(environment);\r
+       }\r
+       \r
+       @Override\r
+       public Model getClass(String name) {\r
+               return null;\r
        }\r
        \r
 }\r
index 7b4d396d6bd79e09bba751d1a8da0878c8a10ef3..6d4620146e00ef9748e6b4e8b59a080178d52cc0 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class GreaterOrEqualThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -35,4 +37,16 @@ public class GreaterOrEqualThan implements IExpression {
                return new GreaterOrEqualThan(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
     \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 9f3244ea3a86f82863cc9ac98200eda26bcf37a5..21d81acc5364a2d24a351c8137e1c67692fedac4 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class GreaterThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -34,5 +36,17 @@ public class GreaterThan implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new GreaterThan(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
-\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 942b7a3aebb432775c2e240da73eb4c508691368..6377a3ad513eea1e391f9fa29b02c2f199e5f959 100644 (file)
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public interface IExpression {\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
        \r
 }\r
index 7edb141436aa33824ecd79f2901dd1ae113c8d3a..805ef557891e192be3abcaadfabc694bcf61ed4e 100644 (file)
@@ -12,4 +12,6 @@ package fi.semantum.sysdyn.solver;
 \r
 public interface IFrame {\r
        public VariableBase getBase(String name);\r
+       public VariableBase getBase(VariableBase base, String prefix);\r
+       public Model getClass(String name);\r
 }\r
index ba4bd80179e64d9ebef51d5b30aed4b8df74fb11..2176faf49c1be298a0078e83119195a91504bc54 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class IfThenElse implements IExpression {\r
        \r
     public IExpression exp;\r
@@ -40,5 +42,18 @@ public class IfThenElse implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new IfThenElse(exp.withBase(frame, prefix), t.withBase(frame, prefix), e.withBase(frame, prefix));\r
        }\r
-\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp = exp.rewrite(frame, copies);\r
+               t = t.rewrite(frame, copies);\r
+               e = e.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index a1fbc884a4346443da5388caabb154a6e6abc860..7c2f28707e198cfb44c0d5dc24a0ffa8c9a1cc72 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class LessOrEqualThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -37,4 +39,16 @@ public class LessOrEqualThan implements IExpression {
                return new LessOrEqualThan(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
     \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 4684ab52fc00be6b463265490e84c7984ba4640e..9a8085384eda48bd838361a633bd577df4567a44 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class LessThan implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -36,5 +38,17 @@ public class LessThan implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new LessThan(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
-\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 81ecabc475f2c8774711c233b1d08ed872ff8d50..d4e2f420942cd228f7bd62fd0f8f0ff5f8ad1593 100644 (file)
@@ -39,7 +39,7 @@ public class LineReader {
        public LineReader(String input, NodeCache cache) {\r
                chars = input.toCharArray();\r
                this.cache = cache;\r
-               model = new Model(new Globals());\r
+               model = new Model(new Globals(), "", false);\r
                parser = new Parser();\r
        }\r
        \r
@@ -90,7 +90,6 @@ public class LineReader {
                        cache.store(line, (SimpleNode)node);\r
                }               \r
                \r
-               parser.currentFrame = model;\r
                parser.walk((SimpleNode)node, 0, model);\r
                \r
        }\r
@@ -119,7 +118,6 @@ public class LineReader {
                        cache.store(line, (SimpleNode)node);\r
                }               \r
                \r
-               parser.currentFrame = model;\r
                parser.walk((SimpleNode)node, 0, model);\r
                \r
        }\r
@@ -143,7 +141,6 @@ public class LineReader {
                        cache.store(line, (SimpleNode)node);\r
                }               \r
                \r
-               parser.currentFrame = model;\r
                parser.walk((SimpleNode)node, 0, model);\r
                \r
        }\r
index 11dd72477a2167cefda227bd18d171f5bd1febd3..4aa58463633d37cf502f165bf9ce6e3a2378444d 100644 (file)
@@ -14,6 +14,7 @@ import java.util.ArrayList;
 import java.util.Arrays;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
+import java.util.TreeMap;\r
 \r
 class Model implements IFrame {\r
        \r
@@ -30,6 +31,7 @@ class Model implements IFrame {
        public ArrayList<ParameterDeclaration> parameters = new ArrayList<ParameterDeclaration>();\r
        public ArrayList<VariableDeclaration> variables = new ArrayList<VariableDeclaration>();\r
        public Map<String,Fn> functions = new HashMap<String,Fn>();\r
+       public Map<String,VariableBase> copies;\r
        \r
        public Assignment[] assignmentArray;\r
        public Assignment[] derivativeArray;\r
@@ -37,8 +39,17 @@ class Model implements IFrame {
        \r
        public final Globals globals;\r
        \r
-       public Model(Globals globals) {\r
+       public String name;\r
+       public boolean isEnumClass;\r
+       \r
+       public Model(Globals globals, String name, boolean isEnumClass) {\r
                this.globals = globals;\r
+               this.name = name;\r
+               this.isEnumClass = isEnumClass;\r
+       }\r
+       \r
+       public void addVariable(VariableDeclaration vd) {\r
+               this.variables.add(vd);\r
        }\r
        \r
        public Fn getFunction(String name) {\r
@@ -47,10 +58,24 @@ class Model implements IFrame {
        \r
        public HashMap<String,VariableBase> names = new HashMap<String,VariableBase>();\r
        \r
+       public VariableBase getBase(VariableBase original, String prefix) {\r
+               VariableBase base = names.get(prefix+original.name);\r
+               if(base != null) return base;\r
+               base = original.withBase(prefix);\r
+               names.put(base.name, base);\r
+               return base;\r
+       }\r
+       \r
        public VariableBase getBase(String name) {\r
                VariableBase base = names.get(name);\r
                if(base == null) {\r
-                       base = new VariableBase(name);\r
+                       \r
+                       if(isEnumClass) {\r
+                               if("size".equals(name)) base = new EnumSizeVariableBase(this);\r
+                               else if("elements".equals(name)) base = new EnumElementsVariableBase(this);\r
+                       } \r
+                       \r
+                       if(base == null) base = new VariableBase(name);\r
                        names.put(name, base);\r
                }\r
                return base;\r
@@ -68,14 +93,14 @@ class Model implements IFrame {
                        Frame frame = new Frame(environment, fn.offset());\r
 \r
                        ArrayList<Object> argh = new ArrayList<Object>();\r
-                       argh.add(args.args.get(0).modification.toString());\r
-                       for(int i=0;i<args.args.size();i++) {\r
-                               argh.add(args.args.get(i).modification.evaluate(environment));\r
+                       argh.add(args.args[0].modification.toString());\r
+                       for(int i=0;i<args.args.length;i++) {\r
+                               argh.add(args.args[1].modification.evaluate(environment));\r
                        }\r
                        \r
                        Variable[] parameters = fn.parameters();\r
                        \r
-                       for(int i=0;i<args.args.size()+1;i++) {\r
+                       for(int i=0;i<args.args.length+1;i++) {\r
                                Variable var = parameters[i];\r
                                Object value = argh.get(i);\r
                                var.assignPlain(frame, value);\r
@@ -83,15 +108,16 @@ class Model implements IFrame {
 \r
                        fn.setLocals(frame);\r
                        \r
-                       return fn.evaluate(frame, args.args.size()+1);\r
+                       return fn.evaluate(frame, args.args.length+1);\r
                        \r
                } else {\r
                        \r
                        Frame frame = new Frame(environment, fn.offset());\r
                        \r
                        ArrayList<Object> argh = new ArrayList<Object>();\r
-                       for(int i=0;i<args.args.size();i++) {\r
-                               Object arg = args.args.get(i).modification.evaluate(environment);\r
+                       for(int i=0;i<args.args.length;i++) {\r
+                               Object arg = args.args[i].modification.evaluate(environment);\r
+                               if(arg == null) throw new IllegalStateException("No value for " + args.args[i].modification);\r
                                argh.add(arg);\r
                        }\r
                        \r
@@ -110,12 +136,72 @@ class Model implements IFrame {
                        \r
                        fn.setLocals(frame);\r
 \r
-                       return fn.evaluate(frame, args.args.size());\r
+                       return fn.evaluate(frame, args.args.length);\r
                        \r
                }\r
                        \r
        }\r
        \r
+       private VariableBase resolveCopy(Map<String,VariableBase> work, VariableBase target) {\r
+               VariableBase deep = work.get(target.name);\r
+               if(deep == null) return target;\r
+               return resolveCopy(work, target);\r
+       }\r
+       \r
+       private void rewrite() {\r
+\r
+               ArrayList<ParameterDeclaration> parameterCopies = new ArrayList<ParameterDeclaration>();\r
+               ArrayList<Assignment> variableCopies = new ArrayList<Assignment>();\r
+\r
+               HashMap<String,VariableBase> work = new HashMap<String,VariableBase>();\r
+               \r
+               for(ParameterDeclaration pd : parameters) {\r
+                       if(pd.modification instanceof Variable) {\r
+                               Variable var = (Variable)pd.modification;\r
+                               if(!SolverUtils.isFullSubscript(var.subscripts)) continue;\r
+                               if(!SolverUtils.isFullSubscript(pd.variable.subscripts)) continue;\r
+                               parameterCopies.add(pd);\r
+                               work.put(pd.variable.base.name, var.base);\r
+                       }\r
+               }\r
+               for(Assignment ass : assignments) {\r
+                       if(ass.expression instanceof Variable) {\r
+                               Variable var = (Variable)ass.expression;\r
+                               if(!SolverUtils.isFullSubscript(var.subscripts)) continue;\r
+                               if(!SolverUtils.isFullSubscript(ass.target.subscripts)) continue;\r
+                               variableCopies.add(ass);\r
+                               work.put(ass.target.base.name, var.base);\r
+                       }\r
+               }\r
+\r
+               parameters.removeAll(parameterCopies);\r
+               assignments.removeAll(variableCopies);\r
+\r
+               copies = new TreeMap<String,VariableBase>();\r
+               for(String key : work.keySet()) {\r
+                       VariableBase b = resolveCopy(work, work.get(key));\r
+                       copies.put(key, b);\r
+               }\r
+\r
+               for(VariableDeclaration vd : variables) {\r
+                       vd.modification = vd.modification.rewrite(this, copies);\r
+                       vd.variable = (Variable)vd.variable.rewrite(this, copies);\r
+               }\r
+               for(ParameterDeclaration pd : parameters) {\r
+                       pd.modification = pd.modification.rewrite(this, copies);\r
+               }\r
+               for(Assignment ass : assignments) {\r
+                       ass.expression = ass.expression.rewrite(this, copies);\r
+               }\r
+               for(Assignment ass : derivatives) {\r
+                       ass.expression = ass.expression.rewrite(this, copies);\r
+               }\r
+               for(Assignment ass : initials) {\r
+                       ass.expression = ass.expression.rewrite(this, copies);\r
+               }\r
+               \r
+       }\r
+       \r
        public int prepare() {\r
 \r
                if(PRINT) {\r
@@ -123,19 +209,32 @@ class Model implements IFrame {
                        System.err.println("==================");\r
                }\r
                \r
-               for(VariableDeclaration vd : variables) {\r
-                       vd.variable.base.tellSubscripts(vd.variable.subscripts);\r
-               }\r
-               for(ParameterDeclaration pd : parameters) {\r
-                       pd.variable.base.tellSubscripts(pd.variable.subscripts);\r
+               rewrite();\r
+               \r
+               boolean done = true;\r
+               \r
+               for(int i=0;i<50;i++) {\r
+                       done = true;\r
+                       for(VariableDeclaration vd : variables) {\r
+                               done &= vd.variable.base.tellSubscripts(vd.variable.subscripts, null);\r
+                       }\r
+                       for(ParameterDeclaration pd : parameters) {\r
+                               done &= pd.variable.base.tellSubscripts(pd.variable.subscripts, pd.modification);\r
+                       }\r
+                       if(done) break;\r
                }\r
+               \r
+               if(!done) throw new IllegalStateException();\r
+               \r
                int nextIndex = 0;\r
                for(Map.Entry<String, VariableBase> entry : names.entrySet()) {\r
                        VariableBase base = entry.getValue();\r
                        base.index = nextIndex;\r
                        if(PRINT)\r
                                System.err.println("Variable: " + entry.getKey() + " " + base.index + " " + Arrays.toString(base.dimensions));\r
-                       nextIndex += base.dimension();\r
+                       int dim = base.dimension();\r
+                       if(dim == -1) dim = 1;\r
+                       nextIndex += dim;\r
                }\r
                \r
                if(PRINT)\r
@@ -174,4 +273,65 @@ class Model implements IFrame {
                \r
        }\r
        \r
+       @Override\r
+       public Model getClass(String name) {\r
+               return globals.classes.get(name);\r
+       }\r
+       \r
+       public boolean instantiateClass(String type_specifier, Declaration decl, IFrame currentFrame) {\r
+               \r
+               Model clazz = globals.classes.get(type_specifier);\r
+               if(clazz == null) return false;\r
+\r
+               Map<String,IExpression> modifications = new HashMap<String,IExpression>();\r
+               if(decl.modification instanceof ArgumentList) {\r
+                       ArgumentList args = (ArgumentList)decl.modification;\r
+                       for(Argument a : args.args) {\r
+                               modifications.put(a.name, a.modification);\r
+                       }\r
+               }\r
+               \r
+               for(VariableDeclaration vd : clazz.variables) {\r
+                       String base = decl.variable.base.name + ".";\r
+                       Variable var2 = vd.variable.withBase(currentFrame, base);\r
+                       VariableDeclaration vd2 = new VariableDeclaration(var2, vd.direction, vd.type, vd.modification.withBase(currentFrame, base));\r
+                       variables.add(vd2);\r
+               }\r
+               for(ParameterDeclaration pd : clazz.parameters) {\r
+                       IExpression modi = modifications.get(pd.variable.base.name);\r
+                       if(modi != null) {\r
+                               String base = decl.variable.base.name + ".";\r
+                               Variable var2 = pd.variable.withBase(currentFrame, base);\r
+                               var2.subscripts = null;\r
+                               ParameterDeclaration pd2 = new ParameterDeclaration(var2, modi);\r
+                               parameters.add(pd2);\r
+                               modifications.remove(pd.variable.base.name);\r
+                       } else {\r
+                               String base = decl.variable.base.name + ".";\r
+                               Variable var2 = pd.variable.withBase(currentFrame, base);\r
+                               ParameterDeclaration pd2 = new ParameterDeclaration(var2, pd.modification.withBase(currentFrame, base));\r
+                               parameters.add(pd2);\r
+                       }\r
+               }\r
+               \r
+               if(!modifications.isEmpty())\r
+                       throw new IllegalStateException();\r
+               \r
+               for(Assignment ass : clazz.assignments) {\r
+                       Assignment ass2 = ass.withBase(currentFrame, decl.variable.base.name + ".");\r
+                       assignments.add(ass2);\r
+               }\r
+               for(Assignment ass : clazz.initials) {\r
+                       Assignment ass2 = ass.withBase(currentFrame, decl.variable.base.name + ".");\r
+                       initials.add(ass2);\r
+               }\r
+               for(Assignment ass : clazz.derivatives) {\r
+                       Assignment ass2 = ass.withBase(currentFrame, decl.variable.base.name + ".");\r
+                       derivatives.add(ass2);\r
+               }\r
+               \r
+               return true;\r
+               \r
+       }\r
+       \r
 }
\ No newline at end of file
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/MultiStatement.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/MultiStatement.java
new file mode 100644 (file)
index 0000000..fd878fd
--- /dev/null
@@ -0,0 +1,40 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+import java.util.ArrayList;\r
+\r
+public class MultiStatement implements IStatement {\r
+       \r
+       public ArrayList<Variable> targets;\r
+       public Variable fn;\r
+    public ArgumentList args;\r
+    \r
+    public MultiStatement(ArrayList<Variable> targets, Variable fn, ArgumentList args) {\r
+       this.targets = targets;\r
+       this.fn = fn;\r
+       this.args = args;\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return targets + " := " + fn + " " + args;\r
+    }\r
+    \r
+    @Override\r
+    public void evaluate(IEnvironment environment) {\r
+       ArrayList<Object> values = (ArrayList<Object>)environment.getSystem().evaluateFunction(environment, fn.base.name, args);\r
+       for(int i=0;i<targets.size();i++) {\r
+               targets.get(i).assignPlain(environment, values.get(i));\r
+       }\r
+    }\r
+    \r
+}\r
index cb41f9dd51d1f0046c48f0f0aeb15d5e0dcf02c7..00397a37d3cefa148736d570a415ac051a0f099d 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Multiplication implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -24,32 +26,40 @@ public class Multiplication implements IExpression {
     public String toString() {\r
        return exp1 + " * " + exp2;\r
     }\r
-\r
-    private Array arrayMul(Array a, Double d) {\r
-       Array result = new Array();\r
-       for(Object o : a.elements()) {\r
-               if(o instanceof Double) {\r
-                       result.addElement((Double)o*d);\r
-               } else {\r
-                       throw new IllegalStateException();\r
-               }\r
-       }\r
-       return result;\r
-    }\r
     \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
                Object left = exp1.evaluate(environment);\r
                Object right = exp2.evaluate(environment);\r
-               if(left instanceof Double && right instanceof Double) return ((Double)left)*((Double)right);\r
-               else if (left instanceof Array && right instanceof Double) return arrayMul((Array)left, (Double)right);\r
-               else if (left instanceof Double && right instanceof Array) return arrayMul((Array)right, (Double)left);\r
-               else throw new UnsupportedOperationException();\r
+               if(left instanceof Double && right instanceof Double) {\r
+                       return ((Double)left)*((Double)right);\r
+               } \r
+               else if(left instanceof Array && right instanceof Array) {\r
+                       Array la = (Array)left;\r
+                       Array ra = (Array)right;\r
+                       return la.mul(ra);\r
+               }\r
+               else if (left instanceof Array && right instanceof Double) return ((Array)left).mul((Double)right);\r
+               else if (left instanceof Double && right instanceof Array) return ((Array)right).mul((Double)left);\r
+               throw new IllegalStateException();\r
+               \r
        }\r
        \r
        @Override\r
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new Multiplication(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
        \r
 }\r
index e810c27868536eb6799b338ebbfc9de802b70b1d..cdf125bae2928a02ff74428a53ffa10917d0b65e 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Negation implements IExpression {\r
        \r
     public IExpression exp;\r
@@ -32,5 +34,16 @@ public class Negation implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new Negation(exp.withBase(frame, prefix));\r
        }\r
-\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp = exp.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 9777c7d8847c640b3742eb6a5cce7e76402f0a8e..33415ec587043de52b068e1c94c8c4416ac8970f 100644 (file)
@@ -49,7 +49,9 @@ public enum NodeClass {
        add_op,\r
        mul_op,\r
        rel_op,\r
-       der_initial;\r
+       der_initial,\r
+       output_expression_list,\r
+       extends_clause;\r
        \r
        private NodeClass() {\r
                Parser.nodeNameMap.put(toString(), this);\r
index f6056732e5b470067b82baff16892f05aaeb545d..41ca66a5b47c03d5fd420c47d0df38e4c88d9ed5 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class NotEquals implements IExpression {\r
        \r
     public IExpression exp1;\r
@@ -34,5 +36,17 @@ public class NotEquals implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new NotEquals(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
-\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NullModification.java
new file mode 100644 (file)
index 0000000..287d618
--- /dev/null
@@ -0,0 +1,27 @@
+package fi.semantum.sysdyn.solver;\r
+\r
+import java.util.Map;\r
+\r
+public class NullModification implements IExpression {\r
+\r
+       @Override\r
+       public Object evaluate(IEnvironment environment) {\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public IExpression withBase(IFrame frame, String prefix) {\r
+               return this;\r
+       }\r
+\r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return this;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               return this;\r
+       }\r
+\r
+}\r
index cdc907e093a49dd5b1a2b70722692d91231ff3c6..e08c3282151961afb82cd2cba52f1683e22cd629 100644 (file)
 package fi.semantum.sysdyn.solver;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Map;\r
 \r
 public class Or implements IExpression {\r
        \r
-    public ArrayList<IExpression> exps;\r
+    public IExpression[] exps;\r
     \r
     public Or(ArrayList<IExpression> exps) {\r
-       this.exps = exps;\r
+       this.exps = exps.toArray(new IExpression[exps.size()]);\r
     }\r
     \r
     @Override\r
     public String toString() {\r
        StringBuilder b = new StringBuilder();\r
-       b.append(exps.get(0));\r
-       for(int i=1;i<exps.size();i++) {\r
+       b.append(exps[0]);\r
+       for(int i=1;i<exps.length;i++) {\r
                b.append(" or ");\r
-               b.append(exps.get(i));\r
+               b.append(exps[i]);\r
        }\r
        return b.toString();\r
     }\r
@@ -44,5 +45,18 @@ public class Or implements IExpression {
                for(IExpression e : exps) ne.add(e.withBase(frame, prefix));\r
                return new Or(ne);\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               for(int i=0;i<exps.length;i++) {\r
+                       exps[i] = exps[i].rewrite(frame, copies);\r
+               }\r
+               return this;\r
+       }\r
        \r
 }\r
index b3eca9028f34b0a67b44a35c6a72776c3be3a582..1aa811f74d0af9b2430286b906c6ac30438ef06f 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 \r
 public class ParameterDeclaration implements IExpression {\r
 \r
@@ -36,5 +38,15 @@ public class ParameterDeclaration implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                throw new UnsupportedOperationException();\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
        \r
 }\r
index 195e19d63bf9977893b1a212274988db71708ffb..cbfac5e79952a71a6aaa3fba2ad92b6f5aa59aaf 100644 (file)
@@ -25,11 +25,11 @@ import fi.semantum.sysdyn.solver.parser.SimpleNode;
 \r
 public class Parser {\r
        \r
-       public IFrame currentFrame;\r
+//     public IFrame currentFrame;\r
 \r
        private int PRINT = 0;\r
        \r
-       public Object walk(SimpleNode n, int indent, Model model) {\r
+       public Object walk(SimpleNode n, int indent, IFrame model) {\r
                Object result = walk_(n, indent, model);\r
                if(PRINT > 0) {\r
                        for(int i=0;i<indent;i++) System.err.print(" ");\r
@@ -40,25 +40,27 @@ public class Parser {
        \r
        static Map<String, NodeClass> nodeNameMap = new HashMap<String, NodeClass>();\r
        \r
-       public Object walk_(SimpleNode n, int indent, Model model) {\r
+       public Object walk_(SimpleNode n, int indent, IFrame frame) {\r
 \r
+               Model model = frame instanceof Model ? (Model)frame : null;\r
+               \r
                // TODO: most of this should probably be implemented in the parser\r
                \r
                NodeClass nc = NodeClass.of(n.toString());\r
                \r
                if (nc == null) {\r
-                       if(n.jjtGetNumChildren() == 1) return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                       if(n.jjtGetNumChildren() == 1) return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        // not sure if this is ever called\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               walk((SimpleNode)n.jjtGetChild(i),indent+2, model);\r
+                               walk((SimpleNode)n.jjtGetChild(i),indent+2, frame);\r
                        }\r
                        return null;\r
                }\r
                \r
                switch(nc) {\r
                case assignment:\r
-                       Object v = (Object)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                       IExpression exp = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
+                       Object v = (Object)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                       IExpression exp = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
                        if(v instanceof Derivative) {\r
                                Derivative der = (Derivative)v;\r
                                Assignment eq = new Assignment(der.variable, der.variable.subscripts, new Derivate(der.variable, der.variable.subscripts, exp));\r
@@ -77,110 +79,148 @@ public class Parser {
                        if("initial".equals(n.op)) {\r
                                model.initial = true;\r
                                for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                                       walk((SimpleNode)n.jjtGetChild(i),indent+2, model);\r
+                                       walk((SimpleNode)n.jjtGetChild(i),indent+2, frame);\r
                                }\r
                        } else {\r
                                model.initial = false;\r
                                for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                                       walk((SimpleNode)n.jjtGetChild(i),indent+2, model);\r
+                                       walk((SimpleNode)n.jjtGetChild(i),indent+2, frame);\r
                                }\r
                        }\r
                        return null;\r
                case composition:\r
-                       if(n.jjtGetNumChildren() == 1) return walk((SimpleNode)n.jjtGetChild(0),indent+2, model);\r
+                       if(n.jjtGetNumChildren() == 1) return walk((SimpleNode)n.jjtGetChild(0),indent+2, frame);\r
                        ArrayList<Object> comps = new ArrayList<Object>();\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               comps.add(walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               comps.add(walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return comps;\r
                case for_index:\r
-                       if(currentFrame instanceof Function)\r
-                               return new ForIndex((Function)currentFrame, n.op, (IExpression)walk((SimpleNode)n.jjtGetChild(0),indent+2, model));\r
+                       if(frame instanceof Function)\r
+                               return new ForIndex((Function)frame, n.op, (IExpression)walk((SimpleNode)n.jjtGetChild(0),indent+2, frame));\r
                        else\r
-                               return new ForIndex(null, n.op, (IExpression)walk((SimpleNode)n.jjtGetChild(0),indent+2, model));\r
+                               return new ForIndex(null, n.op, (IExpression)walk((SimpleNode)n.jjtGetChild(0),indent+2, frame));\r
                case for_indices:\r
                        ArrayList<ForIndex> indices = new ArrayList<ForIndex>();\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               indices.add((ForIndex)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               indices.add((ForIndex)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return indices;\r
                case for_statement:\r
-                       ArrayList<ForIndex> indices2 = (ArrayList<ForIndex>)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                       ArrayList<ForIndex> indices2 = (ArrayList<ForIndex>)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        ArrayList<IStatement> stms = new ArrayList<IStatement>();\r
                        for(int i=1;i<n.jjtGetNumChildren();i++) {\r
-                               stms.add((IStatement)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               stms.add((IStatement)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return new ForStatement(indices2, new StatementList(stms));\r
                case if_statement:\r
-                       IExpression condition = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                       IExpression condition = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        ArrayList<IStatement> thens = new ArrayList<IStatement>();\r
                        ArrayList<IStatement> elses = new ArrayList<IStatement>();\r
                        for(int i=1;i<n.jjtGetNumChildren();i++) {\r
                                SimpleNode child = (SimpleNode)n.jjtGetChild(i);\r
-                               if("then_statement".equals(child.toString())) thens.add((IStatement)walk(child, indent+2, model));\r
-                               else if("else_statement".equals(child.toString())) elses.add((IStatement)walk(child, indent+2, model));\r
+                               if("then_statement".equals(child.toString())) thens.add((IStatement)walk(child, indent+2, frame));\r
+                               else if("else_statement".equals(child.toString())) elses.add((IStatement)walk(child, indent+2, frame));\r
                        }\r
                        return new IfStatement(condition, new StatementList(thens), new StatementList(elses));\r
                case while_statement:\r
-                       IExpression expression = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                       IExpression expression = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        ArrayList<IStatement> whiles = new ArrayList<IStatement>();\r
                        for(int i=1;i<n.jjtGetNumChildren();i++) {\r
-                               whiles.add((IStatement)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               whiles.add((IStatement)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return new While(expression, new StatementList(whiles));\r
                case statement:\r
-                       if(n.jjtGetNumChildren() == 1) return (IStatement)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                       Variable variable = (Variable)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                       IExpression expression2 = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
-                       return new Statement(variable, variable.subscripts, expression2);\r
+                       \r
+//                     ( component_reference() ( ":=" expression() | function_call_args() )\r
+//                                     | "(" output_expression_list() ")" ":=" component_reference() function_call_args()\r
+//                                     | "break"\r
+//                                     | "return"\r
+//                                     | if_statement()\r
+//                                     | for_statement()\r
+//                                     | while_statement()\r
+//                                     | when_statement() )\r
+//                             comment() \r
+                       \r
+                       if(n.jjtGetNumChildren() == 1) {\r
+                               return (IStatement)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                       } else if (n.jjtGetNumChildren() == 2) {\r
+                               SimpleNode n0 = (SimpleNode)n.jjtGetChild(1);\r
+                               if(n0.toString().equals("expression")) {\r
+                                       Variable variable = (Variable)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                                       IExpression expression2 = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                                       return new Statement(variable, variable.subscripts, expression2);\r
+                               } else if (n0.toString().equals("function_call_args")) {\r
+                                       throw new IllegalStateException();\r
+                               } else {\r
+                                       throw new IllegalStateException();\r
+                               }\r
+                       } else if (n.jjtGetNumChildren() == 3) {\r
+                               ArrayList<Variable> vars = (ArrayList<Variable>)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               Variable fn = (Variable)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                               ArgumentList args = (ArgumentList)walk((SimpleNode)n.jjtGetChild(2), indent+2, frame);\r
+                               return new MultiStatement(vars, fn, args);\r
+                       } else {\r
+                               throw new IllegalStateException();\r
+                       }\r
+               case output_expression_list:\r
+                       ArrayList<Variable> vars = new ArrayList<Variable>();\r
+                       for(int i=0;i<n.jjtGetNumChildren();i++) {\r
+                               vars.add((Variable)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
+                       }\r
+                       return vars;\r
                case name:\r
                        return n.op;\r
                case element_list:\r
                        ArrayList<Object> elements = new ArrayList<Object>();\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               elements.add(walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               elements.add(walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return elements;\r
                case element:\r
 //                     if("inner".equals(n.op)) {\r
 //                             return null;\r
 //                     } else {\r
-                               return walk((SimpleNode)n.jjtGetChild(0),indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0),indent+2, frame);\r
 //                     }\r
                case element_modification:\r
                        if(n.jjtGetNumChildren() == 2) {\r
-                               String name = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                               IExpression modification = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
+                               String name = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               IExpression modification = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
                                return new Argument(name, modification);\r
                        }\r
                        break;\r
                case function_arguments:\r
-                       ArgumentList arguments = new ArgumentList(n.op);\r
+                       //ArgumentList arguments = new ArgumentList(n.op);\r
+                       \r
+                       ArrayList<Argument> arguments = new ArrayList<Argument>();\r
+                       \r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               Object o = walk((SimpleNode)n.jjtGetChild(i),indent+2, model);\r
+                               Object o = walk((SimpleNode)n.jjtGetChild(i),indent+2, frame);\r
                                if(o instanceof ArgumentList) {\r
-                                       arguments.args.addAll(((ArgumentList)o).args);\r
+                                       for(Argument a : ((ArgumentList)o).args)\r
+                                               arguments.add(a);\r
                                } else if (o instanceof IExpression) {\r
-                                       arguments.args.add(new Argument("", (IExpression)o));\r
+                                       arguments.add(new Argument("", (IExpression)o));\r
                                } else if (o instanceof ArrayList) {\r
                                        ArrayList list = (ArrayList)o;\r
                                        for(Object o2 : list) {\r
                                                if(o2 instanceof Argument) {\r
-                                                       arguments.args.add((Argument)o2);\r
+                                                       arguments.add((Argument)o2);\r
                                                } else if (o2 instanceof ForIndex) {\r
                                                        ForIndex fi = (ForIndex)o2;\r
-                                                       arguments.args.add(new Argument(fi.name, fi.expression));\r
+                                                       arguments.add(new Argument(fi.name, fi.expression));\r
                                                }\r
                                        }\r
                                } else {\r
                                        return null;\r
                                }\r
                        }\r
-                       return arguments;\r
+                       return new ArgumentList(arguments, n.op);\r
                case argument_list:\r
                        ArrayList<Argument> args = new ArrayList<Argument>();\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               args.add((Argument)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               args.add((Argument)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return new ArgumentList(args);\r
                case type_prefix:\r
@@ -188,112 +228,88 @@ public class Parser {
                case algorithm_section:\r
                        ArrayList<IStatement> statements = new ArrayList<IStatement>();\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               statements.add((IStatement)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                               statements.add((IStatement)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                        }\r
                        return new StatementList(statements);\r
                case subscript:\r
                        if(n.jjtGetNumChildren() == 0)\r
                                return new Constant("-1.0");\r
                        if(n.jjtGetNumChildren() == 1)\r
-                               return walk((SimpleNode)n.jjtGetChild(0),indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0),indent+2, frame);\r
                        throw new IllegalStateException();\r
                case subscript_2:\r
                        throw new IllegalStateException();\r
                case component_clause:\r
-                       String type_prefix = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                       String type_specifier = (String)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
-                       Object third = walk((SimpleNode)n.jjtGetChild(2), indent+2, model);\r
+                       String type_prefix = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                       String type_specifier = (String)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                       Object third = walk((SimpleNode)n.jjtGetChild(2), indent+2, frame);\r
                        IExpression[] subscripts = (third instanceof IExpression[])? (IExpression[])third : null;\r
                        int declarationStart = (third instanceof IExpression[]) ? 3 : 2;\r
                        ArrayList<Object> clauses = new ArrayList<Object>();\r
                        for(int i=declarationStart;i<n.jjtGetNumChildren();i++) {\r
-                               Declaration decl = (Declaration)walk((SimpleNode)n.jjtGetChild(i), indent+2, model);\r
+                               \r
+                               Declaration decl = (Declaration)walk((SimpleNode)n.jjtGetChild(i), indent+2, frame);\r
                                if(subscripts != null)\r
                                        decl.variable.subscripts = subscripts;\r
+                               \r
                                if("parameter".equals(type_prefix)) {\r
+                                       \r
                                        ParameterDeclaration pd = new ParameterDeclaration(decl.variable, (IExpression)decl.modification);\r
                                        model.parameters.add(pd);\r
                                        clauses.add(pd);\r
+                                       \r
                                } else {\r
-                                       if(decl.modification instanceof ArgumentList) {\r
-                                               VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, (ArgumentList)decl.modification);\r
-                                               model.variables.add(vd);\r
-                                               clauses.add(vd);\r
-                                       } else if (decl.modification instanceof IExpression) {\r
-                                               ArrayList<Argument> as = new ArrayList<Argument>();\r
-                                               as.add(new Argument("", (IExpression)decl.modification));\r
-                                               ArgumentList al = new ArgumentList(as);\r
-                                               VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, al);\r
-                                               model.variables.add(vd);\r
-                                               clauses.add(vd);\r
-                                       } else  {\r
-                                               if("Real".equals(type_specifier)) {\r
-                                                       VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, new ArgumentList(new ArrayList<Argument>()));\r
-                                                       model.variables.add(vd);\r
+                                       \r
+                                       boolean wasClass = model == null ? false : model.instantiateClass(type_specifier, decl, frame);\r
+                                       if(!wasClass) {\r
+                                               \r
+                                               if(decl.modification instanceof ArgumentList) {\r
+                                                       VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, (ArgumentList)decl.modification);\r
+                                                       if(model != null)\r
+                                                               model.addVariable(vd);\r
                                                        clauses.add(vd);\r
-                                               } else if("Integer".equals(type_specifier)) {\r
-                                                       VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, new ArgumentList(new ArrayList<Argument>()));\r
-                                                       model.variables.add(vd);\r
+                                               } else if (decl.modification instanceof IExpression) {\r
+                                                       ArrayList<Argument> as = new ArrayList<Argument>();\r
+                                                       as.add(new Argument("", (IExpression)decl.modification));\r
+                                                       ArgumentList al = new ArgumentList(as);\r
+                                                       VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, al);\r
+                                                       if(model != null)\r
+                                                               model.addVariable(vd);\r
                                                        clauses.add(vd);\r
-                                               } else if("Boolean".equals(type_specifier)) {\r
+                                               } else  {\r
                                                        VariableDeclaration vd = new VariableDeclaration(decl.variable, type_prefix, type_specifier, new ArgumentList(new ArrayList<Argument>()));\r
-                                                       model.variables.add(vd);\r
+                                                       if(model != null)\r
+                                                               model.addVariable(vd);\r
                                                        clauses.add(vd);\r
-                                               } else {\r
-                                                       Model clazz = model.globals.classes.get(type_specifier);\r
-                                                       for(VariableDeclaration vd : clazz.variables) {\r
-                                                               String base = decl.variable.base.name + ".";\r
-                                                               Variable var2 = vd.variable.withBase(currentFrame, base);\r
-                                                               VariableDeclaration vd2 = new VariableDeclaration(var2, vd.direction, vd.type, vd.modification.withBase(currentFrame, base));\r
-                                                               model.variables.add(vd2);\r
-                                                               clauses.add(vd2);\r
-                                                       }\r
-                                                       for(ParameterDeclaration pd : clazz.parameters) {\r
-                                                               String base = decl.variable.base.name + ".";\r
-                                                               Variable var2 = pd.variable.withBase(currentFrame, base);\r
-                                                               ParameterDeclaration pd2 = new ParameterDeclaration(var2, pd.modification.withBase(currentFrame, base));\r
-                                                               model.parameters.add(pd2);\r
-                                                       }\r
-                                                       for(Assignment ass : clazz.assignments) {\r
-                                                               Assignment ass2 = ass.withBase(currentFrame, decl.variable.base.name + ".");\r
-                                                               model.assignments.add(ass2);\r
-                                                       }\r
-                                                       for(Assignment ass : clazz.initials) {\r
-                                                               Assignment ass2 = ass.withBase(currentFrame, decl.variable.base.name + ".");\r
-                                                               model.initials.add(ass2);\r
-                                                       }\r
-                                                       for(Assignment ass : clazz.derivatives) {\r
-                                                               Assignment ass2 = ass.withBase(currentFrame, decl.variable.base.name + ".");\r
-                                                               model.derivatives.add(ass2);\r
-                                                       }\r
-                                                       \r
                                                }\r
+                                               \r
                                        }\r
+                                       \r
                                }\r
                        }\r
                        return clauses;\r
                case component_declaration:\r
-                       return (Declaration)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                       return (Declaration)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                case array_subscripts:\r
                        IExpression[] subs = new IExpression[n.jjtGetNumChildren()];\r
                        for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                               subs[i] = (IExpression)walk((SimpleNode)n.jjtGetChild(i),indent+2, model);\r
+                               subs[i] = (IExpression)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame);\r
                        }\r
                        return subs;\r
                case declaration:\r
                        if(n.jjtGetNumChildren() == 0) {\r
-                               return new Declaration(new Variable(currentFrame, n.op, null), null);\r
+                               return new Declaration(new Variable(frame, n.op, null), null);\r
                        } else if(n.jjtGetNumChildren() == 1) {\r
-                               Object o = walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               Object o = walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                                if(o instanceof IExpression[]) {\r
-                                       return new Declaration(new Variable(currentFrame, n.op, (IExpression[])o), null);\r
+                                       return new Declaration(new Variable(frame, n.op, (IExpression[])o), null);\r
                                } else {\r
-                                       return new Declaration(new Variable(currentFrame, n.op, null), o);\r
+                                       return new Declaration(new Variable(frame, n.op, null), o);\r
                                }\r
                        } else if(n.jjtGetNumChildren() == 2) {\r
-                               IExpression[] subscripts2 = (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                               Object modification = walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
-                               return new Declaration(new Variable(currentFrame, n.op,subscripts2), modification);\r
+                               IExpression[] subscripts2 = (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               Object modification = walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                               return new Declaration(new Variable(frame, n.op,subscripts2), modification);\r
                        } else {\r
                                StringBuilder b = new StringBuilder();\r
                                b.append("declaration: ");\r
@@ -308,8 +324,7 @@ public class Parser {
                                String functionName = child.op;\r
                                ArrayList<IStatement> stms2 = new ArrayList<IStatement>();\r
                                Function function = new Function(functionName, new StatementList(stms2));\r
-                               currentFrame = function;\r
-                               ArrayList<Object> composition = (ArrayList<Object>)walk(child, indent+2, model);\r
+                               ArrayList<Object> composition = (ArrayList<Object>)walk(child, indent+2, function);\r
                                for(int i=1;i<composition.size();i++) {\r
                                        Object comp = composition.get(i);\r
                                        if(comp instanceof IStatement) {\r
@@ -317,34 +332,45 @@ public class Parser {
                                        } else if(comp instanceof ArrayList) {\r
                                                ArrayList<Object> declarations = (ArrayList<Object>)composition.get(i);\r
                                                for(Object os_ : declarations) {\r
-                                                       ArrayList<Object> os = (ArrayList<Object>)os_;\r
-                                                       for(Object o : os) {\r
-                                                               VariableDeclaration decl = (VariableDeclaration)o;\r
-                                                               function.internals.add(decl);\r
+                                                       if(os_ instanceof VariableDeclaration) {\r
+                                                               VariableDeclaration decl = (VariableDeclaration)os_;\r
+                                                               if("input".equals(decl.direction)) function.inputs.add(decl);\r
+                                                               else if ("output".equals(decl.direction)) function.outputs.add(decl);\r
+                                                               else throw new IllegalStateException();\r
+                                                       } else {\r
+                                                               ArrayList<Object> os = (ArrayList<Object>)os_;\r
+                                                               for(Object o : os) {\r
+                                                                       VariableDeclaration decl = (VariableDeclaration)o;\r
+                                                                       function.internals.add(decl);\r
+                                                               }\r
                                                        }\r
                                                }\r
                                        }\r
                                }\r
                                ArrayList<Object> declarations = (ArrayList<Object>)composition.get(0);\r
                                for(Object os_ : declarations) {\r
-                                       ArrayList<Object> os = (ArrayList<Object>)os_;\r
-                                       for(Object o : os) {\r
-                                               VariableDeclaration decl = (VariableDeclaration)o;\r
+                                       if(os_ instanceof VariableDeclaration) {\r
+                                               VariableDeclaration decl = (VariableDeclaration)os_;\r
                                                if("input".equals(decl.direction)) function.inputs.add(decl);\r
                                                else if ("output".equals(decl.direction)) function.outputs.add(decl);\r
                                                else throw new IllegalStateException();\r
+                                       } else {\r
+                                               ArrayList<Object> os = (ArrayList<Object>)os_;\r
+                                               for(Object o : os) {\r
+                                                       VariableDeclaration decl = (VariableDeclaration)o;\r
+                                                       if("input".equals(decl.direction)) function.inputs.add(decl);\r
+                                                       else if ("output".equals(decl.direction)) function.outputs.add(decl);\r
+                                                       else throw new IllegalStateException();\r
+                                               }\r
                                        }\r
                                }\r
                                model.functions.put(functionName, function);\r
-                               currentFrame = model;\r
                                return function;\r
                                \r
                        } else if("model".equals(n.op)) {\r
 \r
-                               currentFrame = model;\r
-\r
                                if(n.jjtGetNumChildren() == 1) {\r
-                                       return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                                       return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                                } else {\r
                                        System.err.println("undefined children for class_definition model");    \r
                                }\r
@@ -352,12 +378,18 @@ public class Parser {
                                \r
                        } else if("class".equals(n.op)) {\r
                                \r
-                               Model clazz = new Model(model.globals);\r
                                SimpleNode specifier = (SimpleNode)n.jjtGetChild(0); \r
+                               Model clazz = new Model(model.globals, specifier.op, false);\r
                                model.globals.classes.put(specifier.op, clazz);\r
-                               currentFrame = clazz;\r
                                walk(specifier, indent+2, clazz);\r
-                               currentFrame = model;\r
+                               \r
+                               if(clazz.isEnumClass) {\r
+                                       Variable sizeVariable = new Variable(clazz, "size", null);\r
+                                       Variable elementsVariable = new Variable(clazz, "elements", new IExpression[] { new Constant(""+clazz.variables.size()) });\r
+                                       clazz.parameters.add(new ParameterDeclaration(sizeVariable, sizeVariable.getPossibleConstant()));\r
+                                       clazz.parameters.add(new ParameterDeclaration(elementsVariable, elementsVariable.getPossibleConstant()));\r
+                               }\r
+                               \r
                                return null;\r
                                \r
                        } else {\r
@@ -366,11 +398,11 @@ public class Parser {
                        break;\r
                case array:\r
                        if(n.jjtGetNumChildren() == 1) {\r
-                               ArgumentList al = (ArgumentList)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               ArgumentList al = (ArgumentList)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                                if("for".equals(al.op)) {\r
                                        ForArray array = new ForArray();\r
-                                       array.addElement(al.args.get(0).modification);\r
-                                       array.addElement(al.args.get(1));\r
+                                       array.addElement(al.args[0].modification);\r
+                                       array.addElement(al.args[1]);\r
                                        return array;\r
                                } else {\r
                                        Array array = new Array();\r
@@ -382,22 +414,22 @@ public class Parser {
                        if(n.op != null) {\r
                                return Utils.parsePrimitive(n.op);\r
                        } else {\r
-                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        }\r
                case component_reference:\r
                        if(n.jjtGetNumChildren() == 1) {\r
-                               return new Variable(currentFrame, n.op, (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, model));\r
+                               return Variable.make(frame, n.op, (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, frame));\r
                        } else {\r
                                if ("time".equals(n.op)) {\r
                                        return new TimeVariable();\r
                                }\r
-                               return new Variable(currentFrame, n.op, null);\r
+                               return Variable.make(frame, n.op, null);\r
                        }\r
                case relation:\r
                        if(n.jjtGetNumChildren() == 3) {\r
-                               IExpression exp1 = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                               String op = (String)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
-                               IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(2), indent+2, model);\r
+                               IExpression exp1 = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               String op = (String)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                               IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(2), indent+2, frame);\r
                                String trimmed = op != null ? op.trim() : null;\r
                                if("<".equals(trimmed)) {\r
                                        return new LessThan(exp1, exp2);\r
@@ -414,13 +446,13 @@ public class Parser {
                                }\r
                                else return null;\r
                        } else {\r
-                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        }\r
                case simple_expression:\r
-                       if(n.jjtGetNumChildren() == 1) return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                       if(n.jjtGetNumChildren() == 1) return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        else if(n.jjtGetNumChildren() == 2) {\r
-                               IExpression start = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model); \r
-                               IExpression end = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
+                               IExpression start = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame); \r
+                               IExpression end = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
                                return new ArraySliceExpression(start, end);\r
                        } else {\r
                                throw new UnsupportedOperationException();\r
@@ -429,21 +461,21 @@ public class Parser {
                        if(n.jjtGetNumChildren() > 1) {\r
                                ArrayList<IExpression> logs = new ArrayList<IExpression>();\r
                                for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                                       logs.add((IExpression)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                                       logs.add((IExpression)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                                }\r
                                return new Or(logs);\r
                        } else {\r
-                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        }\r
                case logical_term:\r
                        if(n.jjtGetNumChildren() > 1) {\r
                                ArrayList<IExpression> terms = new ArrayList<IExpression>();\r
                                for(int i=0;i<n.jjtGetNumChildren();i++) {\r
-                                       terms.add((IExpression)walk((SimpleNode)n.jjtGetChild(i),indent+2, model));\r
+                                       terms.add((IExpression)walk((SimpleNode)n.jjtGetChild(i),indent+2, frame));\r
                                }\r
                                return new And(terms);\r
                        } else {\r
-                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        }\r
                case arithmetic_expression:\r
                {\r
@@ -451,19 +483,19 @@ public class Parser {
                        IExpression left;\r
                        if(n.jjtGetNumChildren() % 2 == 0) {\r
                                String op = ((String)walk((SimpleNode)n.jjtGetChild(0), indent+2, model)).trim();\r
-                               left = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
+                               left = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
                                if("-".equals(op)) {\r
                                        left = new Negation(left);\r
                                }\r
                                i=2;\r
                        } else {\r
-                               left = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               left = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                                i=1;\r
                        }\r
 \r
                        for(;i<n.jjtGetNumChildren();i+=2) {\r
                                String op = ((String)walk((SimpleNode)n.jjtGetChild(i), indent+2, model)).trim();\r
-                               IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(i+1), indent+2, model);\r
+                               IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(i+1), indent+2, frame);\r
                                if("+".equals(op)) left = new Addition(left, exp2);\r
                                else if("-".equals(op)) left = new Subtraction(left, exp2);\r
                        }\r
@@ -473,23 +505,23 @@ public class Parser {
                        \r
                case term:\r
                        if(n.jjtGetNumChildren() > 1) {\r
-                               IExpression term = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               IExpression term = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                                for(int i=1;i<n.jjtGetNumChildren();i+=2) {\r
                                        String op = ((String)walk((SimpleNode)n.jjtGetChild(i), indent+2, model)).trim();\r
-                                       IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(i+1), indent+2, model);\r
+                                       IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(i+1), indent+2, frame);\r
                                        if("*".equals(op)) term = new Multiplication(term, exp2);\r
                                        else if(".*".equals(op)) term = new ElementwiseProduct(term, exp2);\r
                                        else if("/".equals(op)) term = new Division(term, exp2);\r
                                }\r
                                return term;\r
                        } else {\r
-                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
+                               return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        }\r
                case if_expression:\r
                        if(n.jjtGetNumChildren() == 3) {\r
-                               IExpression exp1 = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                               IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
-                               IExpression exp3 = (IExpression)walk((SimpleNode)n.jjtGetChild(2), indent+2, model);\r
+                               IExpression exp1 = (IExpression)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               IExpression exp2 = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                               IExpression exp3 = (IExpression)walk((SimpleNode)n.jjtGetChild(2), indent+2, frame);\r
                                return new IfThenElse(exp1, exp2, exp3);\r
                        }\r
                case add_op:\r
@@ -500,17 +532,22 @@ public class Parser {
                        return n.op;\r
                case der_initial:\r
                        if("der".equals(n.op)) {\r
-                               ArgumentList args2 = (ArgumentList)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                               if(args2.args.size() == 1) {\r
-                                       return new Derivative((Variable)args2.args.get(0).modification);\r
+                               ArgumentList args2 = (ArgumentList)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               if(args2.args.length == 1) {\r
+                                       return new Derivative((Variable)args2.args[0].modification);\r
                                }\r
                        } else if("initial".equals(n.op)) {\r
                                return new Application("initial", new ArgumentList(""));\r
                        } else if("application".equals(n.op)) {\r
-                               String name = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, model);\r
-                               ArgumentList args2 = (ArgumentList)walk((SimpleNode)n.jjtGetChild(1), indent+2, model);\r
+                               String name = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                               ArgumentList args2 = (ArgumentList)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
                                return new Application(name, args2);\r
                        }\r
+               case extends_clause:\r
+                       String extendsName = (String)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
+                       walk((SimpleNode)n.jjtGetChild(1), indent+2, frame);\r
+                       model.isEnumClass = extendsName.equals("Enumeration_class");\r
+                       return null;\r
                }\r
                        \r
                System.err.println("fall-through " + n);\r
@@ -564,27 +601,12 @@ public class Parser {
                StringReader reader = new StringReader(contents2);\r
                ModelParser modelParser = new ModelParser(reader);\r
                SimpleNode n = (SimpleNode)modelParser.stored_definition();\r
-           System.err.println("n: " +n );\r
            Parser parser = new Parser();\r
-           Model m = new Model(new Globals());\r
+           Model m = new Model(new Globals(), "", false);\r
                parser.walk(n, 0, m);\r
                \r
                m.prettyPrint();\r
                \r
-//              ModelicaCompilationUnit unit = ModelicaAnalyzer.analyze(contents2);\r
-//              System.err.println("unit: " + unit);\r
-               \r
-\r
-//             reader = new LineReader(contents2, cache);\r
-//             reader.parse();\r
-//             Model model = reader.model;\r
-               \r
-//             String code = FileUtils.getContents(f);\r
-//             Model model = new Parser().parseModelica(code);\r
-               \r
-//             duration = System.nanoTime()-start;\r
-//             System.err.println("parsed file in " + 1e-9*duration + "s.");\r
-\r
                Solver solver = new Solver();\r
                solver.prepare(contents2);\r
                \r
index 5db6b3347e375b5076cdd29a2a6ba502f61ec2c6..bcd050d4a931231b88ea8f9b2343ef97678248a3 100644 (file)
@@ -64,7 +64,7 @@ public class Solver {
                ModelParser modelParser = new ModelParser(reader);\r
                SimpleNode n = (SimpleNode)modelParser.stored_definition();\r
            Parser parser = new Parser();\r
-           model = new Model(new Globals());\r
+           model = new Model(new Globals(), "", false);\r
                parser.walk(n, 0, model);\r
                \r
                env = new Environment(model, step, start);\r
@@ -85,11 +85,14 @@ public class Solver {
                \r
                newValues = new Object[Math.max(model.assignments.size(),model.derivatives.size())];\r
                \r
-               int koss = 1;\r
+               int condition = 1;\r
+               int loops = 0;\r
                \r
-               while(koss > 0) {\r
+               while(condition > 0 && loops++ < 50) {\r
                        \r
-                       koss = 0;\r
+//                     System.err.println("== LOOP " + loops + " ==");\r
+                       \r
+                       condition = 0;\r
 \r
                        for(ParameterDeclaration pd : model.parameters) {\r
                                try {\r
@@ -98,7 +101,7 @@ public class Solver {
                                                pd.assigned = true;\r
                                        }\r
                                } catch (Exception e) {\r
-                                       koss++;\r
+                                       condition++;\r
                                        if(PRINT_EXCEPTIONS) {\r
                                                e.printStackTrace();\r
                                                System.err.println("failed to assign " + pd.variable.toString());\r
@@ -114,9 +117,19 @@ public class Solver {
                                try {\r
                                        if(!vd.assigned) {\r
                                                for(Argument arg : vd.modification.args) {\r
-                                                       if("start".equals(arg.name)) {\r
+                                                       if(arg.name.endsWith("start")) {\r
                                                                Object value = arg.modification.evaluate(env);\r
-                                                               vd.variable.assign(env, null, value);\r
+                                                               if(vd.variable.base.dimension() == 1) {\r
+                                                                       vd.variable.assign(env, null, value);\r
+                                                               } else {\r
+                                                                       if(value instanceof Double) {\r
+                                                                               Array array = new Array();\r
+                                                                               for(int i=0;i<vd.variable.base.dimension();i++) array.addElement(value);\r
+                                                                               vd.variable.assign(env, null, array);\r
+                                                                       } else {\r
+                                                                               throw new IllegalStateException();\r
+                                                                       }\r
+                                                               }\r
                                                                // make sure the variable is not initialized \r
                                                                // twice, this is probably not the most \r
                                                                // efficient way\r
@@ -127,10 +140,9 @@ public class Solver {
                                                                }\r
                                                        }\r
                                                }\r
-                                               vd.assigned = true;\r
                                        }\r
                                } catch (Exception e) {\r
-                                       koss++;\r
+                                       condition++;\r
                                        if(PRINT_EXCEPTIONS) {\r
                                                e.printStackTrace();\r
                                                System.err.println("failed to assign " + vd.variable.toString());\r
@@ -146,7 +158,7 @@ public class Solver {
                                                ass.assigned = true;\r
                                        }\r
                                } catch (Exception e) {\r
-                                       koss++;\r
+                                       condition++;\r
                                        if(PRINT_EXCEPTIONS) {\r
                                                e.printStackTrace();\r
                                                System.err.println("failed to assign " + ass.target.toString());\r
@@ -203,9 +215,9 @@ public class Solver {
        // TODO: implement some on the fly parameter change stuff for different experiment types\r
        \r
        public void printEnvironment() {\r
-               System.err.println("Environment");\r
                String[] keys = keys();\r
                double[] values = values();\r
+               System.err.println("Environment " + keys.length + " " + values.length);\r
                for(int i=0;i<keys.length;i++) {\r
                        System.err.println(keys[i] + " = " + values[i]);\r
                }\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/SolverUtils.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/SolverUtils.java
new file mode 100644 (file)
index 0000000..731de2d
--- /dev/null
@@ -0,0 +1,60 @@
+package fi.semantum.sysdyn.solver;\r
+\r
+public class SolverUtils {\r
+\r
+       public static class Slice {\r
+               public static final Slice FULL = new Slice(-2,-1);\r
+               public int start;\r
+               public int end;\r
+               public Slice(int start, int end) {\r
+                       this.start = start;\r
+                       this.end = end;\r
+               }\r
+       }\r
+       \r
+       public static boolean isFullSubscript(IExpression[] subscripts) {\r
+               if(subscripts == null) return true;\r
+               for(int i=0;i<subscripts.length;i++) {\r
+                       if(subscripts[i] instanceof Constant) {\r
+                               Constant c = (Constant)subscripts[i];\r
+                               if(c.value instanceof Double) {\r
+                                       Double d = (Double)c.value;\r
+                                       if(d == -1.0) continue;\r
+                               }\r
+                       }\r
+                       return false;\r
+               }\r
+               return true;\r
+       }\r
+       \r
+       public static boolean isArray(int[] dimensions) {\r
+               return dimensions != null && dimensions != VariableBase.UNINIT;\r
+       }\r
+       \r
+       public static Slice[] parseSubscripts(IEnvironment env, IExpression[] subscripts) {\r
+               Slice[] result = new Slice[subscripts.length];\r
+               for(int i=0;i<subscripts.length;i++) {\r
+                       IExpression e = subscripts[i];\r
+                       if(e instanceof ArraySliceExpression) {\r
+                               ArraySliceExpression ase = (ArraySliceExpression)e;\r
+                               Double start = (Double)ase.start.evaluate(env);\r
+                               Double end = (Double)ase.end.evaluate(env);\r
+                               result[i] = new Slice(start.intValue(), end.intValue());\r
+                       } else {\r
+                               Double v = (Double)e.evaluate(env);\r
+                               int index = v.intValue()-1;\r
+                               if(index == -2) result[i] = Slice.FULL;\r
+                               else result[i] = new Slice(index,index);\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+       \r
+       public static boolean isSlice(Slice[] subs) {\r
+               for(Slice s : subs) {\r
+                       if(s.start != s.end) return true;\r
+               }\r
+               return false;\r
+       }\r
+       \r
+}\r
index 4d45af7022663a2896ea270647e45970a6bbea41..f7f8763d32767665308ef28b6d45a39d3d4944bc 100644 (file)
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class Subtraction implements IExpression {\r
        \r
     public IExpression exp1;\r
     public IExpression exp2;\r
     \r
     public Subtraction(IExpression exp1, IExpression exp2) {\r
+       if(exp1 == null) exp1 = new Constant("0");\r
        this.exp1 = exp1;\r
        this.exp2 = exp2;\r
     }\r
@@ -34,6 +37,17 @@ public class Subtraction implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return new Subtraction(exp1.withBase(frame, prefix), exp2.withBase(frame, prefix));\r
        }\r
-       \r
     \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+    \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               exp1 = exp1.rewrite(frame, copies);\r
+               exp2 = exp2.rewrite(frame, copies);\r
+               return this;\r
+       }\r
+       \r
 }\r
index 8a4178ee78b0454695527568d061582eea057117..d2d3e4c6044f5cf73f41bf242a6139233b26cb60 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 public class TimeVariable implements IExpression {\r
 \r
        @Override\r
@@ -27,6 +29,15 @@ public class TimeVariable implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                return this;\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
 \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               return this;\r
+       }\r
        \r
 }\r
index 632fc537ec8a93f21e6fdcc79798f8c9cc3aeb88..183d0cc3be23cfada5bb2d5b8841c58fe64aa112 100644 (file)
@@ -11,7 +11,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
-import org.omg.PortableInterceptor.SUCCESSFUL;\r
+import java.util.Map;\r
+\r
 \r
 \r
 public class Variable implements IExpression {\r
@@ -24,6 +25,14 @@ public class Variable implements IExpression {
                this.subscripts = subscripts;\r
        }\r
        \r
+       public static Object make(IFrame frame, String name, IExpression[] subscripts) {\r
+//             Array enumElements = enumerationElements(frame, name);\r
+//             if(enumElements != null) return enumElements;\r
+//             Constant enumSize = enumerationSize(frame, name);\r
+//             if(enumSize != null) return enumSize;\r
+               return new Variable(frame, name, subscripts);\r
+       }\r
+       \r
        public Variable(VariableBase base) {\r
                this.base = base;\r
        }\r
@@ -32,28 +41,6 @@ public class Variable implements IExpression {
                this.base = base;\r
                this.subscripts = subscripts;\r
        }\r
-\r
-       private int makeSubscriptIndex(IEnvironment env) {\r
-               if(base.dimensions == null) return 0;\r
-               int result = 0;\r
-               for(int i=0;i<subscripts.length;i++) {\r
-                       int factor = 1;\r
-                       for(int j=i+1;j<subscripts.length;j++) factor *= base.dimensions[j];\r
-                       IExpression e = subscripts[i];\r
-                       Double v = (Double)e.evaluate(env);\r
-                       int index = v.intValue()-1;\r
-                       if(index == -2) return 0;\r
-                       result += factor*index;\r
-               }\r
-               if(result == -1) throw new IllegalStateException();\r
-               return result;\r
-\r
-       }\r
-       \r
-       public int subscriptIndex(IEnvironment env, IExpression[] subscripts) {\r
-               if(subscripts == null) return 0;\r
-               return makeSubscriptIndex(env);\r
-       }\r
        \r
        @Override\r
        public String toString() {\r
@@ -106,10 +93,6 @@ public class Variable implements IExpression {
                return asd;\r
        }\r
        \r
-       public int index(IEnvironment env, IExpression[] subscripts) {\r
-               return base.index + subscriptIndex(env, subscripts);\r
-       }\r
-       \r
        public void setArrayIndex(IEnvironment environment, Array array, Object value) {\r
                \r
                for(int i=0;i<subscripts.length-1;i++) {\r
@@ -132,31 +115,52 @@ public class Variable implements IExpression {
                \r
        }\r
        \r
-       public Object getArrayIndex(IEnvironment environment, IExpression[] subscripts, Array array) {\r
-               Object result = array;\r
-               for(IExpression e : subscripts) {\r
-                       int index = Utils.getIndex(e.evaluate(environment));\r
-                       result = ((Array)result).element(index);\r
-               }\r
-               if(result instanceof IExpression) {\r
-                       IExpression exp = (IExpression)result;\r
-                       return exp.evaluate(environment);\r
-               }\r
-               if(result instanceof Array)\r
-                       throw new IllegalStateException();\r
-               return result;\r
-       }\r
-\r
        public void assignPlain(IEnvironment env, Object value) {\r
                env.put(base.index, value);\r
        }\r
        \r
+       private void validateSize(IEnvironment env, IExpression[] subscripts, Object value) {\r
+               \r
+               if(base.isStoredAsArray()) return;\r
+               \r
+               int fullDimension = base.dimension();\r
+               if(fullDimension == 1) {\r
+                       if(!(value instanceof Double)) {\r
+                               Array arr = (Array)value;\r
+                               if(arr.size(0) != 1)\r
+                                       throw new IllegalStateException();\r
+                       }\r
+               } else {\r
+                       if(subscripts == null) {\r
+                               if(!(value instanceof Array))\r
+                                       throw new IllegalStateException();\r
+                               Array arr = (Array)value;\r
+                               int arrDim = arr.dimension();\r
+                               if(arrDim != fullDimension)\r
+                                       throw new IllegalStateException();\r
+                       } else {\r
+                               if(value instanceof Array) {\r
+                                       Array arr = (Array)value;\r
+                                       if(!arr.validateDimensions(base.dimensions, 0))\r
+                                               throw new IllegalStateException();\r
+                               } else {\r
+                                       throw new IllegalStateException();\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               \r
+       }\r
+       \r
        public void assign(IEnvironment env, IExpression[] subscripts, Object value) {\r
+               \r
+               validateSize(env, subscripts, value);\r
+               \r
                if(value instanceof Array) {\r
                        if(base.isStoredAsArray()) {\r
-                               env.put(index(env, subscripts), value);\r
+                               env.put(base.index(env, subscripts), value);\r
                        } else {\r
-                               assignArray(env, index(env, subscripts), (Array)value, 0);\r
+                               assignArray(env, base.index(env, subscripts), (Array)value, 0);\r
                        }\r
                } else {\r
                        if(base.isStoredAsArray()) {\r
@@ -168,77 +172,68 @@ public class Variable implements IExpression {
                                }\r
                                setArrayIndex(env, existing, value);\r
                        } else {\r
-                               env.put(index(env, subscripts), value);\r
-                       }\r
-               }\r
-       }\r
-       \r
-       public int intoArray(IEnvironment env, int index, int d, Array target) {\r
-\r
-               if(d == base.dimensions.length-1) {\r
-                       for(int i=0;i<base.dimensions[d];i++) {\r
-                               target.addElement(env.getValue(index));\r
-                               index++;\r
+                               env.put(base.index(env, subscripts), value);\r
                        }\r
-                       return index;\r
                }\r
-               \r
-               for(int i=0;i<base.dimensions[d];i++) {\r
-                       Array array = new Array();\r
-                       index = intoArray(env, index, d+1, array);\r
-                       target.addElement(array);\r
-               }\r
-               \r
-               return index;\r
-               \r
        }\r
        \r
        @Override\r
        public Object evaluate(IEnvironment environment) {\r
-               return evaluate(environment, subscripts);\r
+               return base.evaluate(environment, subscripts);\r
        }\r
 \r
-       public Object evaluate(IEnvironment environment, IExpression[] subscripts) {\r
-               if(base.isStoredAsArray()) {\r
-                       Array array = (Array)environment.getValue(base.index);\r
-                       if(hasScalarSubscript(subscripts)) {\r
-                               Object o = getArrayIndex(environment, subscripts, array);\r
-                               if(o instanceof Variable) throw new IllegalStateException();\r
-                               if(o instanceof Array)\r
-                                       throw new IllegalStateException();\r
-                               return o;\r
-                       }\r
-                       else return array;\r
-               } else {\r
-                       \r
-                       if(base.dimension() > 1) {\r
-                               Array array = new Array();\r
-                               intoArray(environment, base.index, 0, array);\r
-                               return array;\r
-                       }\r
-                       \r
-                       Object result = environment.getNamedValue(base.name);\r
-                       if(result == null) result = environment.getValue(index(environment, subscripts));\r
-                       if(result == null) throw new UnassignedVariableException("No value for " + base.name);\r
-                       \r
-                       return result;\r
-               }\r
-       }\r
-       \r
-       private boolean hasScalarSubscript(IExpression[] subscripts) {\r
-               if(subscripts == null) return false;\r
-               else return true;\r
-       }\r
        \r
+//     static Array enumerationElements(IFrame frame, String name) {\r
+//             if(!name.endsWith(".elements")) return null;\r
+//             String prefix = name.replace(".elements", "");\r
+//             Model m = frame.getClass(prefix + "_class");\r
+//             if(m == null) return null;\r
+//             Array result = new Array();\r
+//             for(VariableDeclaration vd : m.variables) {\r
+//                     Constant c = (Constant)vd.modification.args.get(0).modification;\r
+//                     result.addElement(c.value);\r
+//             }\r
+//             return result;\r
+//     }\r
+//     \r
+//     static Constant enumerationSize(IFrame frame, String name) {\r
+//             if(!name.endsWith(".size")) return null;\r
+//             String prefix = name.replace(".size", "");\r
+//             Model m = frame.getClass(prefix + "_class");\r
+//             if(m == null) return null;\r
+//             return new Constant("" + m.variables.size());\r
+//     }\r
+\r
        public Variable withBase(IFrame frame, String prefix) {\r
+               \r
                if(subscripts != null) {\r
                        IExpression[] subscripts2 = new IExpression[subscripts.length];\r
                        for(int i=0;i<subscripts.length;i++)\r
                                subscripts2[i] = subscripts[i].withBase(frame, prefix);\r
-                       return new Variable(frame, prefix + base.name, subscripts2);\r
+                       return new Variable(frame.getBase(base, prefix), subscripts2);\r
                } else {\r
-                       return new Variable(frame, prefix + base.name,null);\r
+                       return new Variable(frame.getBase(base, prefix), null);\r
                }\r
        }\r
+    \r
+       @Override\r
+       public IExpression getPossibleConstant() {\r
+               return base.getPossibleConstant();\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               VariableBase copy = copies.get(base.name);\r
+               if(copy == null) {\r
+                       if(subscripts != null) {\r
+                               for(int i=0;i<subscripts.length;i++)\r
+                                       subscripts[i] = subscripts[i].rewrite(frame, copies);\r
+                       }\r
+                       return this;\r
+               }\r
+\r
+               return new Variable(frame.getBase(copy.name), subscripts).rewrite(frame, copies);\r
+               \r
+       }\r
        \r
 }\r
index c0719f9df5c4e417b3793a92152a92d0eef2ac6b..1466574bd3b71002e442951eaed9ba246abe588b 100644 (file)
@@ -12,11 +12,15 @@ package fi.semantum.sysdyn.solver;
 \r
 import java.util.Arrays;\r
 \r
+import fi.semantum.sysdyn.solver.SolverUtils.Slice;\r
+\r
 public class VariableBase {\r
+\r
+       static final int[] UNINIT = new int[0];\r
        \r
        public String name;\r
        public int index;\r
-       public int[] dimensions;\r
+       public int[] dimensions = UNINIT;\r
        public VariableBase(String name) {\r
                this.name = name;\r
        }\r
@@ -29,20 +33,49 @@ public class VariableBase {
                this.index = index;\r
                this.dimensions = dimensions;\r
        }\r
-       public void tellSubscripts(IExpression[] e) {\r
-               if(e == null) return;\r
-               if(dimensions == null) \r
-                       dimensions = new int[e.length];\r
-               for(int i=0;i<e.length;i++) {\r
-                       if(!(e[i] instanceof Constant)) {\r
-                               dimensions[i] = -1;\r
-                       } else {\r
-                               Constant c = (Constant)e[i];\r
-                               int index = ((Double)c.value).intValue();\r
-                               if(index == -1)                dimensions[i] = -1;\r
-                               else if(index > dimensions[i]) dimensions[i] = index;\r
+       public boolean tellSubscripts(IExpression[] e, IExpression modification) {\r
+               \r
+               if(dimensions != UNINIT) return true;\r
+               \r
+               if(e != null) {\r
+                       \r
+                       if(dimensions == UNINIT) \r
+                               dimensions = new int[e.length];\r
+                       \r
+                       if(e.length == 0)\r
+                               throw new IllegalStateException();\r
+                       \r
+                       for(int i=0;i<e.length;i++) {\r
+                               \r
+                               IExpression exp = e[i];\r
+                               Object constant = exp.getPossibleConstant();\r
+                               if(constant != null) exp = (IExpression)constant;\r
+                               \r
+                               if(!(exp instanceof Constant)) {\r
+                                       dimensions[i] = -1;\r
+                               } else {\r
+                                       Constant c = (Constant)exp;\r
+                                       int index = ((Double)c.value).intValue();\r
+                                       if(index == -1) {\r
+                                               if(modification instanceof Array) {\r
+                                                       Array array = (Array)modification;\r
+                                                       dimensions[i] = array.size(i);\r
+                                               } else {\r
+                                                       dimensions[i] = -1;\r
+                                               }\r
+                                       }\r
+                                       else if(index > dimensions[i]) dimensions[i] = index;\r
+                               }\r
                        }\r
+                       \r
+               } else {\r
+                       \r
+                       dimensions = null;\r
+                       \r
                }\r
+               \r
+               return dimensions != UNINIT;\r
+               \r
        }\r
        public int dimension() {\r
                if(dimensions == null) return 1;\r
@@ -59,5 +92,126 @@ public class VariableBase {
        VariableBase withBase(String prefix) {\r
                return new VariableBase(prefix+name, index, dimensions);\r
        }\r
+\r
+       private int makeSubscriptIndex(IEnvironment env, IExpression[] subscripts) {\r
+               if(dimensions == null) return 0;\r
+               int result = 0;\r
+               for(int i=0;i<subscripts.length;i++) {\r
+                       int factor = 1;\r
+                       for(int j=i+1;j<subscripts.length;j++) factor *= dimensions[j];\r
+                       IExpression e = subscripts[i];\r
+                       Double v = (Double)e.evaluate(env);\r
+                       int index = v.intValue()-1;\r
+                       if(index == -2) return 0;\r
+                       result += factor*index;\r
+               }\r
+               if(result == -1) throw new IllegalStateException();\r
+               return result;\r
+       }\r
+       \r
+       public int subscriptIndex(IEnvironment env, IExpression[] subscripts) {\r
+               if(subscripts == null) return 0;\r
+               return makeSubscriptIndex(env, subscripts);\r
+       }\r
+       \r
+       public int index(IEnvironment env, IExpression[] subscripts) {\r
+               return index + subscriptIndex(env, subscripts);\r
+       }\r
+       \r
+       public Object getArrayIndex(IEnvironment environment, IExpression[] subscripts, Array array) {\r
+               Object result = array;\r
+               for(IExpression e : subscripts) {\r
+                       int index = Utils.getIndex(e.evaluate(environment));\r
+                       result = ((Array)result).element(index);\r
+               }\r
+               if(result instanceof IExpression) {\r
+                       IExpression exp = (IExpression)result;\r
+                       return exp.evaluate(environment);\r
+               }\r
+               if(result instanceof Array)\r
+                       throw new IllegalStateException();\r
+               return result;\r
+       }\r
+       \r
+       private boolean hasScalarSubscript(IExpression[] subscripts) {\r
+               if(subscripts == null) return false;\r
+               else return true;\r
+       }\r
+       \r
+       public int intoArray(IEnvironment env, int index, int d, Array target) {\r
+\r
+               if(d == dimensions.length-1) {\r
+                       for(int i=0;i<dimensions[d];i++) {\r
+                               Object value = env.getValue(index);\r
+                               if(value == null) {\r
+                                       throw new UnassignedVariableException("No value for " + name);\r
+                               }\r
+                               target.addElement(value);\r
+                               index++;\r
+                       }\r
+                       return index;\r
+               }\r
+               \r
+               for(int i=0;i<dimensions[d];i++) {\r
+                       Array array = new Array();\r
+                       index = intoArray(env, index, d+1, array);\r
+                       target.addElement(array);\r
+               }\r
+               \r
+               return index;\r
+               \r
+       }\r
+       \r
+       public Object evaluate(IEnvironment environment, IExpression[] subscripts) {\r
+               \r
+               if(isStoredAsArray()) {\r
+                       \r
+                       Array array = (Array)environment.getValue(index);\r
+                       if(hasScalarSubscript(subscripts)) {\r
+                               Object o = getArrayIndex(environment, subscripts, array);\r
+                               if(o instanceof Variable) throw new IllegalStateException();\r
+                               if(o instanceof Array)\r
+                                       throw new IllegalStateException();\r
+                               return o;\r
+                       }\r
+                       else return array;\r
+                       \r
+               } else {\r
+                       \r
+                       if(dimension() > 1) {\r
+                               if(subscripts != null) {\r
+                                       \r
+                                       Slice[] sub = SolverUtils.parseSubscripts(environment, subscripts);\r
+                                       if(SolverUtils.isSlice(sub)) {\r
+                                               Array arr = new Array();\r
+                                               intoArray(environment, index, 0, arr);\r
+                                               return arr.slice(sub);\r
+                                       } else { \r
+                                               return environment.getValue(index(environment, subscripts));\r
+                                       }\r
+                                       \r
+                               } else {\r
+                                       Array array = new Array();\r
+                                       intoArray(environment, index, 0, array);\r
+                                       return array;\r
+                               }\r
+                       }\r
+                       \r
+                       Object result = environment.getNamedValue(name);\r
+                       if(result == null) result = environment.getValue(index(environment, subscripts));\r
+                       if(result == null) throw new UnassignedVariableException("No value for " + name);\r
+\r
+                       if(SolverUtils.isArray(dimensions) && subscripts == null) {\r
+                               return new Array().addElement(result);\r
+                       } else {\r
+                               return result;\r
+                       }\r
+                       \r
+               }\r
+       }\r
+       \r
+       public IExpression getPossibleConstant() {\r
+               return null;\r
+       }\r
        \r
 }\r
index 89d0f456c189209ac304c04c8adc303948a78263..08c8a49d78d7b03ef0f231b57ed085039a74ac27 100644 (file)
@@ -10,6 +10,8 @@
  *******************************************************************************/\r
 package fi.semantum.sysdyn.solver;\r
 \r
+import java.util.Map;\r
+\r
 \r
 public class VariableDeclaration implements IExpression {\r
 \r
@@ -40,5 +42,15 @@ public class VariableDeclaration implements IExpression {
        public IExpression withBase(IFrame frame, String prefix) {\r
                throw new UnsupportedOperationException();\r
        }\r
+    \r
+       @Override\r
+       public Object getPossibleConstant() {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
        \r
 }\r
index d95e0cdd4f9b02ea4b6609b6dbe95ec84be45c89..422e38fdb919627fb17737d09d5b9b1fc05eb3a9 100644 (file)
@@ -71,10 +71,10 @@ TOKEN:
 | "*" | "/" | ".*" | "./"\r
 | "^" | ".^"\r
 | "=" | ":=" \r
-| <IDENT: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","_",".","0"-"9"])* >\r
+| <IDENT: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","_",".", "0"-"9"])* >\r
 | <STRING: "\"" (~["\"", "\\", "\n"] | "\\" ~["\n"])* "\"">\r
     { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); }\r
-| <UNSIGNED_INTEGER: (["-"])? (["0"-"9"])+ >\r
+| <UNSIGNED_INTEGER: (["0"-"9"])+ >\r
 | <UNSIGNED_NUMBER:  \r
     ( <UNSIGNED_INTEGER> "." (<UNSIGNED_INTEGER>)? (["e","E"] <UNSIGNED_INTEGER>)?\r
     | "." <UNSIGNED_INTEGER> (["e","E"] <UNSIGNED_INTEGER>)?\r
@@ -137,9 +137,9 @@ void class_specifier() : { Token t;
 //  end IDENT  \r
     LOOKAHEAD(2) t=<IDENT> { jjtThis.op = t.image; } string_comment() composition() "end" <IDENT>\r
     | LOOKAHEAD(2) <IDENT> "=" base_prefix() name() ( array_subscripts() )? ( class_modification() )? comment()\r
+    | LOOKAHEAD(3)  <IDENT> "=" "der" "(" name() "," <IDENT> ( "," <IDENT> )* ")" comment()\r
     | LOOKAHEAD(3) <IDENT> "=" "enumeration" "(" ( ( enum_list() )? | ":" ) ")" comment()\r
-    |LOOKAHEAD(3)  <IDENT> "=" "der" "(" name() "," <IDENT> ( "," <IDENT> )* ")" comment()\r
-    | "extends" <IDENT> ( class_modification() )? string_comment() composition() "end" <IDENT> \r
+    //| "extends" <IDENT> ( class_modification() )? string_comment() composition() "end" <IDENT> \r
 }\r
 \r
 void base_prefix() : {\r
@@ -208,8 +208,8 @@ Node element() : {
 //  ( ( class_definition | component_clause) |\r
 //  replaceable ( class_definition | component_clause)\r
 //  [constraining_clause comment])\r
-    import_clause() |\r
-    extends_clause() |\r
+    import_clause() { return jjtThis; } |\r
+    extends_clause() { return jjtThis; } |\r
     ( "redeclare" )?\r
     ( "final" )?\r
     ( "inner" { jjtThis.op = "inner"; } )? ( "outer" { jjtThis.op = "outer"; } )?\r
@@ -656,7 +656,12 @@ void mul_op() : {
 \r
 void factor() : {\r
 } {\r
-       primary() ( "^" | ".^" primary() )?\r
+       primary() ( factor_op() primary() )?\r
+}\r
+\r
+void factor_op() : {\r
+} {\r
+    "^" { jjtThis.op = "^";} | ".^" { jjtThis.op = ".^";}\r
 }\r
 \r
 void der_initial() : { \r