Bundle-Vendor: Semantum Oy
Require-Bundle: org.eclipse.core.runtime,
org.simantics.utils;bundle-version="1.1.0",
- org.simantics.databoard;bundle-version="0.6.5"
+ org.simantics.databoard;bundle-version="0.6.5",
+ org.simantics.utils.datastructures;bundle-version="1.1.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: fi.semantum.sysdyn.solver
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
import java.util.ArrayList;\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class And implements IExpression {\r
\r
public IExpression[] exps;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ for(IExpression exp : exps) exp.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Application implements IExpression {\r
\r
public String name;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ for(Argument a : args.args)\r
+ a.modification.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Argument implements IExpression {\r
\r
public String name;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ modification.accept(visitor);\r
+ }\r
+ \r
}\r
import java.util.Iterator;\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
import fi.semantum.sysdyn.solver.SolverUtils.Slice;\r
\r
public class Array implements IExpression {\r
return result;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ for(int i=0;i<elements.size();i++) {\r
+ Object element = elements.get(i);\r
+ if(element instanceof IExpression) {\r
+ ((IExpression)element).accept(visitor);\r
+ }\r
+ }\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class ArraySliceExpression implements IExpression {\r
\r
public IExpression start;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ start.accept(visitor);\r
+ end.accept(visitor);\r
+ }\r
+ \r
}\r
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
-import org.omg.PortableInterceptor.SUCCESSFUL;\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
\r
public class Assignment {\r
\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Constant implements IExpression {\r
\r
public Object value;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ }\r
+ \r
}\r
\r
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
+import java.nio.channels.UnsupportedAddressTypeException;\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Declaration implements IExpression {\r
\r
public Variable variable;\r
throw new UnsupportedOperationException();\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Derivate implements IExpression {\r
\r
private Variable variable;\r
private IExpression[] subscripts;\r
- private IExpression e;\r
+ public IExpression e;\r
\r
public Derivate(Variable variable, IExpression[] subscripts, IExpression e) {\r
this.variable = variable;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ if(subscripts != null) {\r
+ for(int i=0;i<subscripts.length;i++)\r
+ subscripts[i].accept(visitor);\r
+ }\r
+ e.accept(visitor);\r
+ }\r
+ \r
}\r
throw new UnsupportedOperationException();\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Division implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
import java.util.Iterator;\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class ElementwiseProduct implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Equals implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
import java.util.Collection;\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class ForArray implements IExpression {\r
\r
private ArrayList<Object> elements = new ArrayList<Object>();\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ for(int i=0;i<elements.size();i++) {\r
+ Object o = elements.get(i);\r
+ if(o instanceof IExpression) {\r
+ ((IExpression)o).accept(visitor);\r
+ }\r
+ }\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class GreaterOrEqualThan implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class GreaterThan implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
public interface IExpression {\r
\r
+ public interface ExpressionVisitor {\r
+ void visit(IExpression expression);\r
+ }\r
+ \r
public Object evaluate(IEnvironment environment);\r
public IExpression withBase(IFrame frame, String prefix);\r
public Object getPossibleConstant();\r
public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies);\r
+ public void accept(ExpressionVisitor visitor);\r
\r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class IfThenElse implements IExpression {\r
\r
public IExpression exp;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp.accept(visitor);\r
+ t.accept(visitor);\r
+ e.accept(visitor);\r
+ }\r
+\r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class LessOrEqualThan implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class LessThan implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Multiplication implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Negation implements IExpression {\r
\r
public IExpression exp;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class NotEquals implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
public IExpression rewrite(IFrame frame, Map<String, VariableBase> copies) {\r
return this;\r
}\r
+ \r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ }\r
\r
}\r
import java.util.ArrayList;\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Or implements IExpression {\r
\r
public IExpression[] exps;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ for(IExpression exp : exps) exp.accept(visitor);\r
+ }\r
+ \r
}\r
throw new UnsupportedOperationException();\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
}\r
\r
import java.io.StringReader;\r
import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
\r
+import org.simantics.utils.datastructures.MapList;\r
+\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
import fi.semantum.sysdyn.solver.Model.Globals;\r
import fi.semantum.sysdyn.solver.parser.ModelParser;\r
import fi.semantum.sysdyn.solver.parser.SimpleNode;\r
private Environment env;\r
\r
private Object[] newValues;\r
+ private Object[] derivatives;\r
\r
private double step;\r
private double start;\r
ready = false;\r
}\r
\r
+ private void sortAssignments() {\r
+\r
+ final MapList<String,Assignment> asses = new MapList<String,Assignment>();\r
+ for(Assignment ass : model.assignments) asses.add(ass.target.base.name, ass);\r
+ \r
+ final List<VariableBase> found = new ArrayList<VariableBase>();\r
+ final Set<String> visited = new HashSet<String>();\r
+\r
+ ExpressionVisitor sortVisitor = new ExpressionVisitor() {\r
+ @Override\r
+ public void visit(IExpression expression) {\r
+ if(expression instanceof Variable) {\r
+ Variable var = (Variable)expression;\r
+ if(visited.add(var.base.name)) {\r
+ for(Assignment ass : asses.getValues(var.base.name)) {\r
+ ass.expression.accept(this);\r
+ if(ass.subscripts != null)\r
+ for(IExpression e : ass.subscripts) e.accept(this);\r
+ }\r
+ found.add(var.base);\r
+ }\r
+ }\r
+ }\r
+ };\r
+ \r
+ // Sort assignments\r
+ for(Assignment ass : model.assignments) {\r
+ ass.target.accept(sortVisitor);\r
+ }\r
+\r
+ Collections.sort(model.assignments, new Comparator<Assignment>() {\r
+\r
+ @Override\r
+ public int compare(Assignment o1, Assignment o2) {\r
+ int i1 = found.indexOf(o1.target.base); \r
+ int i2 = found.indexOf(o2.target.base);\r
+ if(i1 < i2) return -1;\r
+ else if (i1 > i2) return 1;\r
+ return 0;\r
+ }\r
+\r
+ \r
+ });\r
+ \r
+ }\r
+ \r
public void prepare(String input) throws Exception {\r
\r
long startNanos = System.nanoTime();\r
\r
env.valueTable = new Object[size+STACK_SIZE];\r
\r
+ sortAssignments();\r
+ \r
model.assignmentArray = model.assignments.toArray(new Assignment[model.assignments.size()]);\r
model.derivativeArray = model.derivatives.toArray(new Assignment[model.derivatives.size()]);\r
model.parameterArray = model.parameters.toArray(new ParameterDeclaration[model.parameters.size()]);\r
\r
newValues = new Object[Math.max(model.assignments.size(),model.derivatives.size())];\r
+ derivatives = new Object[model.derivatives.size()];\r
\r
int condition = 1;\r
int loops = 0;\r
return env.getValueArray();\r
}\r
\r
+ \r
+ \r
public void step() {\r
// TODO: do something more sensible if the solver is not ready\r
if (!ready) return;\r
Assignment[] assignments = model.assignmentArray;\r
Assignment[] derivatives = model.derivativeArray;\r
\r
+ // first we evaluate, integrate and assign derivatives at previous time step\r
+ for(int i=0;i<model.derivatives.size();i++) {\r
+ newValues[i] = derivatives[i].expression.evaluate(env);\r
+ }\r
+ for(int i=0;i<model.derivatives.size();i++) {\r
+ derivatives[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
+ }\r
+\r
+ // Increment time\r
// should probably be wrapped inside a method or something\r
env.time += env.step;\r
- \r
- // Solve algebraic equations\r
+\r
+ // Next solve algebraic equations\r
for(int i=0;i<assignments.length;i++) {\r
newValues[i] = assignments[i].expression.evaluate(env);\r
}\r
assignments[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
}\r
\r
- // Solve derivatives\r
- for(int i=0;i<model.derivatives.size();i++) {\r
- newValues[i] = derivatives[i].expression.evaluate(env);\r
- }\r
- for(int i=0;i<model.derivatives.size();i++) {\r
- derivatives[i].target.assign(env, assignments[i].subscripts, newValues[i]);\r
- }\r
}\r
\r
// TODO: implement some on the fly parameter change stuff for different experiment types\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class Subtraction implements IExpression {\r
\r
public IExpression exp1;\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ exp1.accept(visitor);\r
+ exp2.accept(visitor);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
public class TimeVariable implements IExpression {\r
\r
@Override\r
return this;\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ }\r
+ \r
}\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
\r
\r
public class Variable implements IExpression {\r
\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ visitor.visit(this);\r
+ }\r
+ \r
}\r
*******************************************************************************/\r
package fi.semantum.sysdyn.solver;\r
\r
-import java.util.Arrays;\r
-\r
import fi.semantum.sysdyn.solver.SolverUtils.Slice;\r
\r
public class VariableBase {\r
\r
import java.util.Map;\r
\r
+import fi.semantum.sysdyn.solver.IExpression.ExpressionVisitor;\r
+\r
\r
public class VariableDeclaration implements IExpression {\r
\r
throw new UnsupportedOperationException();\r
}\r
\r
+ @Override\r
+ public void accept(ExpressionVisitor visitor) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
}\r