简体   繁体   中英

Anylogic event triggering via variable updatee

in the agent I'm trying to build, I have a Dynamic Variable that could be changed from outside and I would like to link this change to trigger an event or a statechart transition. I tried the condition "variable != 0" but it has 2 errors: It doesn't consider the 0 in the case it happens to appear and it locks my agent because that's an always true condition. Do you know if this is something that could be done with Anylogic? Or should I try a different approach? Thanks a lot for your help, P

This is a lengthy solution, but I use it when nothing else works. Create dummy source and sink blocks inside the agent that flowchart lives. Be sure to select "calls of inject() function" inside the source.

在此处输入图片说明

And in the main, whenever that condition occurs, inject a dummy agent into that source block.

在此处输入图片说明

There you can control the transitions whenever the agent is created in the dummy source block with the variables that you have inside that agent.

The technical correct solution would be:

  1. make your variable private (so it cannot actually be changed at all from external sources)
  2. create a public function named setMyVariable that accepts a double argument named newValue
  3. in that function, write myVariable = newValue; . Below that line, you can now execute any additional code that should be triggered whenever your variable is updated

This is the OOP way of doing things :)

To add just one more type of solution for completeness: for these kind of situation I prefer to use message passing. The first part is similar to OOP method - make the variable private so it can't be directly changed from outside the agent. Then to change the variable send agent a message using AnyLogic's agent communication functions. Once the message that value has changed is received, it changes the value and then forwards it to the statechart to trigger a transition. This addresses your issues:

  1. '0' can be acted on when necessary and
  2. statechart isn't locked up as you only react to value change not to it's value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM