Home > Software engineering >  Evaluating/executing strings dynamically in AnyLogic
Evaluating/executing strings dynamically in AnyLogic

Time:10-27

Is there any way of dynamically checking and setting parameters of the agents in AnyLogic by evaluating/executing strings?

My case:

  • I have parking lots numbered from 0 to 200, named as parkingLot0, parkingLot1,...,parkingLot200 (each with capacity of 1).
  • I have an agent called DetachedTrailer.
  • I want to find an empty parking slot for the arriving DetachedTrailer and set agent.parking to that parkingLot.
  • I do not want to do this manually one-by-one, instead would like to have a short loop that would do the trick.

I try using executeExpression, but it throws error saying *Exception during discrete event execution:

root:

null*

Please see the function body of the screenshot below.

Unfortunately the documentation does not have much information about executeExpression. Any help would be appreciated.

enter image description here

CodePudding user response:

In order to make the expression you need to do the following:

put all your parking spots in a collection, for this example i will put the network itself in a variable

the variable will be of type RoadNetwork and will have initial value roadNetwork... let's call this variable rn

then on the expression...

CodeValue myCode = new CodeValue(this,"rn.getParkingLots().get(0).nFree()");

This will work. You can do the same with parkingLot0, put it in a variable called pl0 of type ParkingLot with initial value parkingLot0 and the expression will look like this:

  CodeValue myCode = new CodeValue(this,"pl0.nFree()");

This will work aswell

CodePudding user response:

would this work? Remembering that your road network is a network that you can use. The roadNetwork is automatically created when you create your first road, and the network includes the parking spots.

ParkingLot pl=findFirst(roadNetwork.getParkingLots(),x->x.nFree()>0);
agent.parking=pl;

why would you do this executeExpression at all? Do you want it for general puporses or to solve this particular problem?

  • Related