Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions src/main/java/org/simulator/sbml/astnode/ASTNodeInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ public boolean compileBoolean(CallableSBase nsb, double time) {
return false;
}

/**
* Evaluates function argument expressions, stores their values, and updates the
* {@code funcArgs} map so that identifiers inside a FunctionDefinition body resolve to
* the corresponding argument values. Returns the previous {@code funcArgs} map so it can
* be restored after evaluation.
*/
private Map<String, Double> pushFunctionArguments(List<String> variables,
ASTNodeValue[] children, int nArguments, double[] values, double time, double delay) {

Map<String, Double> oldFuncArgs = funcArgs;
Map<String, Double> newFuncArgs = new HashMap<>(oldFuncArgs);

int n = Math.min(nArguments, Math.min(children.length, variables.size()));
for (int i = 0; i < n; i++) {
double argVal = children[i].compileDouble(time, delay);
values[i] = argVal;

String varName = variables.get(i);
if (varName != null) {
newFuncArgs.put(varName, argVal);
}
}

funcArgs = newFuncArgs;
return oldFuncArgs;
}

/**
* @param rightChild
* @param variables
Expand All @@ -163,11 +190,15 @@ public boolean compileBoolean(CallableSBase nsb, double time) {
*/
public double functionDouble(ASTNodeValue rightChild, List<String> variables,
ASTNodeValue[] children, int nArguments, double[] values, double time, double delay) {
for (int i = 0; i < nArguments; i++) {
values[i] = children[i].compileDouble(time, delay);

Map<String, Double> oldFuncArgs =
pushFunctionArguments(variables, children, nArguments, values, time, delay);

try {
return rightChild.compileDouble(time, delay);
} finally {
funcArgs = oldFuncArgs;
}
double value = rightChild.compileDouble(time, delay);
return value;
}

/**
Expand Down Expand Up @@ -562,11 +593,17 @@ public boolean functionBoolean(String name, List<ASTNodeValue> children) {
*/
public boolean functionBoolean(ASTNodeValue rightChild, List<String> variables,
ASTNodeValue[] children, double[] values, double time) {
for (int i = 0; i < children.length; i++) {
values[i] = children[i].compileDouble(time, 0d);

int nArguments = children.length;

Map<String, Double> oldFuncArgs =
pushFunctionArguments(variables, children, nArguments, values, time, 0d);

try {
return rightChild.compileBoolean(time);
} finally {
funcArgs = oldFuncArgs;
}
boolean value = rightChild.compileBoolean(time);
return value;
}

/**
Expand Down Expand Up @@ -1025,4 +1062,4 @@ public double rateOf(EquationSystem eqnSystem, CallableSBase sBase, double time,
return 0d;
}
}
}
}