Search code examples
anylogic

agent cannot be resolved to a variable in function body but works in other blocks


I currently have a function that gets checked at certain time intervals. I want a hold to unblock upon satisfaction of the condition in the function body. However, agent can never be resolved to a variable, even though this convention of referring to the agent works in other process blocks. The agent that is present in the (((Patient)agent) is the agent that cannot be resolved to a variable. Patient is an agent type and has the variable isMorning that gets set to true or false upstream in the model. Below is the code in the function block, and the parameters are set to Just action (returns nothing). The (((Patient)agent) would be in a queue block prior to the hold.

if (((Patient)agent).isMorning == true && SurgeonMorning.idle() > 0){
    hold.unblock();}
else if (((Patient)agent).isMorning == false && SurgeonAfternoon.idle()  == 0){
    hold.unblock();}

Solution

  • If this is code in a function, you need to provide a function argument and use that instead of agent.

    In the properties of the function under arguments, add an entry myPatient and specify the type as Patient (right side from the dropdown).

    Then, you can use the code as

    if (myPatient.isMorning == true && SurgeonMorning.idle() > 0){
        hold.unblock();}
    else if (myPatient.isMorning == false && SurgeonAfternoon.idle()  == 0){
        hold.unblock();}
    

    Check the help to understand functions more. And this article to understand when (and when not) you can use these little keywords like agent: https://www.benjamin-schumann.com/blog/2016/2/4/the-magic-lightbulb-and-how-it-can-help-your-anylogic-modelling