diff --git a/src/main/java/org/simulator/sbml/astnode/ASTNodeInterpreter.java b/src/main/java/org/simulator/sbml/astnode/ASTNodeInterpreter.java index 27b2d466..86a299e4 100644 --- a/src/main/java/org/simulator/sbml/astnode/ASTNodeInterpreter.java +++ b/src/main/java/org/simulator/sbml/astnode/ASTNodeInterpreter.java @@ -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 pushFunctionArguments(List variables, + ASTNodeValue[] children, int nArguments, double[] values, double time, double delay) { + + Map oldFuncArgs = funcArgs; + Map 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 @@ -163,11 +190,15 @@ public boolean compileBoolean(CallableSBase nsb, double time) { */ public double functionDouble(ASTNodeValue rightChild, List 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 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; } /** @@ -562,11 +593,17 @@ public boolean functionBoolean(String name, List children) { */ public boolean functionBoolean(ASTNodeValue rightChild, List 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 oldFuncArgs = + pushFunctionArguments(variables, children, nArguments, values, time, 0d); + + try { + return rightChild.compileBoolean(time); + } finally { + funcArgs = oldFuncArgs; } - boolean value = rightChild.compileBoolean(time); - return value; } /** @@ -1025,4 +1062,4 @@ public double rateOf(EquationSystem eqnSystem, CallableSBase sBase, double time, return 0d; } } -} +} \ No newline at end of file