*******************************************************************************/\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
\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
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
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
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
*******************************************************************************/\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
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
*******************************************************************************/\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
\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
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
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
\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
\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
}\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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
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
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
+import java.util.Map;\r
+\r
public class Derivative implements IExpression {\r
\r
Variable variable;\r
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
*******************************************************************************/\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
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
\r
import java.util.Collection;\r
import java.util.Iterator;\r
+import java.util.Map;\r
\r
public class ElementwiseProduct implements IExpression {\r
\r
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
--- /dev/null
+/*******************************************************************************\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
--- /dev/null
+/*******************************************************************************\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
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
});\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
}\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
\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
\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
// 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
*******************************************************************************/\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
\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
\r
import java.util.ArrayList;\r
import java.util.Collection;\r
+import java.util.Map;\r
\r
public class ForArray implements IExpression {\r
\r
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
\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
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
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
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
@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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
*******************************************************************************/\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
\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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
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
cache.store(line, (SimpleNode)node);\r
} \r
\r
- parser.currentFrame = model;\r
parser.walk((SimpleNode)node, 0, model);\r
\r
}\r
cache.store(line, (SimpleNode)node);\r
} \r
\r
- parser.currentFrame = model;\r
parser.walk((SimpleNode)node, 0, model);\r
\r
}\r
cache.store(line, (SimpleNode)node);\r
} \r
\r
- parser.currentFrame = model;\r
parser.walk((SimpleNode)node, 0, model);\r
\r
}\r
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
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
\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
\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
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
\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
\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
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
\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
--- /dev/null
+/*******************************************************************************\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
*******************************************************************************/\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
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
*******************************************************************************/\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
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
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
*******************************************************************************/\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
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
--- /dev/null
+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
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
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
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
+import java.util.Map;\r
+\r
\r
public class ParameterDeclaration implements IExpression {\r
\r
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
\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
\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
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
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
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
} 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
\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
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
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
}\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
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
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
\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
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
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
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
\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
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
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
}\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
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
// 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
--- /dev/null
+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
*******************************************************************************/\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
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
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
+import java.util.Map;\r
+\r
public class TimeVariable implements IExpression {\r
\r
@Override\r
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
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
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
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
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
\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
}\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
\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
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
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
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
+import java.util.Map;\r
+\r
\r
public class VariableDeclaration implements IExpression {\r
\r
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
| "*" | "/" | ".*" | "./"\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
// 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
// ( ( 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
\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