mivandev
mivandev

Reputation: 321

Access value of parameter of type 'Agent' in AnyLogic

The model I am working on is set up as follows. It contains four agent types:

  1. Patient
  2. SmallHospital
  3. MediumHospital
  4. BigHospital

. All these agents are located in Main. SmallHospital and MediumHospital agents are connected in a network to BigHospitals using link to agent objects e.g.: mediumHospitalLink and bigHospitalLink.

When generated, Patients move, via a statechart in Patient, to nearest hospital agent (regardless of type, using this function in Patient). The hospital agent to which the Patient moved to is stored in a parameter in Patient called p_myFirstHospital. This parameter has type 'Agent' as I do not beforehand to which hospital type the agent will be move to. This results in no error and functions as expected.enter image description here

However, I want to move the Patient-agent to a second hospital after a while. If the Patient is currently in a smallHospital, it should move to the mediumHospital to which the smallHospital is linked to, and likewise, if the Patient is in a mediumHospital, it should move to a bigHospital to which the mediumHospital is linked to.

I expected that I would be able to call from within Patient the following code to access those agents: p_myFirstHospital.mediumHospitalLink.getConnectedAgent();

or through a simple function I created. It should retrieve the agent from the Link to agents located in the hospital agent stored in p_myFirstHospital.

Unfortunately, this does not work. The compiler returns the following error message: Description: smallHospitalLink cannot be resolved or is not a field. Location: model/Patient/f_DetermineSecondHospital - Function

Am I missing some Java or AnyLogic subtleties? Your help and suggestions are very welcome.

Upvotes: 0

Views: 549

Answers (1)

Felipe
Felipe

Reputation: 9431

That's because p_myFirstHospital is of type Agent

So you need to make java magic as follows:

case1:
p_mySecondHospital=((SmallHospital)p_myFirstHospital).mediumHospitalLink.getConnectedAgent();
case2:
p_mySecondHospital=((MediumHospital)p_myFirstHospital).mediumHospitalLink.getConnectedAgent();

Upvotes: 1

Related Questions