Piero
Piero

Reputation: 99

Anylogic ports alternate use (?)

after doing a few models using the predefined libraries, I'm building a model from scratch. In this model, I need a few different agents exchanging information between them, and I made it possible using Dynamic Variables and making them visible from the upper agent. Connectors will join the blocks. In picture one example of what I did and what I meant.

Agent Example

Since I've much info to exchange, I made one variable for inputs and one for output, using the "Array" option and using proper dimensions. I was wondering if I could make them look cleaner and to have a better functionality, using ports. I saw them connecting to already existing library components, but never in an agent made from scratch. In the documentation I read that I could use the commands "port.send(obj)" and "port.receive(obj)" but I couldn't do anything working. So my question is: is it possible to use ports to send objects via code (maybe agents, or dynamic variable or whatever) between agents? If yes, how to trigger the receiving action in the destination agent? Thanks a lot in advance! P

Upvotes: 0

Views: 428

Answers (2)

Piero
Piero

Reputation: 99

I finally understood how to do what I had in mind. Ports are capable of both sending and receiving data with just one connector. No more subsequent blocks are needed as suggested by @Artem, but his answer gave me the correct suggestion to find my own solution.

When ports exchange messages between them, when they receive it they call it "msg"; so "msg" could be fed to functions or it can be used to address inner fields (ie: msg.custom_field).

To use ports at it fullest, a low-level use detail should be achieved by enabling developer mode. To do so, follow the direction: Tool -> Preferences -> Development -> Library developer mode. Selecting this voice, now ports have those new voices when clicked on:

port developer menus

Ports now can be configured with the "in" and "out" message type (if needed they can be also different), and it is possible to associate a callback function when the port receives or sends a message. For example, in figure 1, I set a "doSomething" function that takes the received message class and does something on it.

To send a message it should be used the "port.send(something)" code and the OnSend function will be taken into account before sending.

It is also possible to create custom ports inheriting the port class type, but I didn't push myself so deep in the white rabbit hole, sorry.

Blocks connected by ports

In figure 2, there is a small testbed I created with (working) ports. I hope that this little guide could help you all guys, even if this kind of use is just for very specific needing.

Upvotes: 1

Artem P.
Artem P.

Reputation: 816

Generally, you can add a port to your object or even create a custom port type as described here. However in order to send message to / from ports the message has to extend an Agent and ports have to be connected via a connector. The implementation pattern is:

  1. Inside Sender agent: have an Enter block connected to a port
  2. Inside Receiver agent: have a port connected to an Exit block
  3. in Main agent: connect Sender port to Receiver port
  4. Inside Sender: create a msg agent and use enter.take(msg)
  5. The above will execute: Enter -> Sender port -> Receiver port -> Exit
  6. Perform some action in on Exit property of Exit block in Receiver

Upvotes: 1

Related Questions