Font size

Connecting ports at runtime

AnyLogic supports creation of truly dynamic models – the ones with dynamically evolving structure and component interconnection.

You can programmatically change port connections at runtime to model systems with dynamically changing connections, in particular, systems with mobile objects. It can be also helpful when you need to create a complex structure of objects with sophisticated topologies that cannot be established graphically.

You can write this code anywhere you like in the agent type.

Connection case Connection method Disconnection method
A port with a port of a flowchart block; port.unmap(source.out);
It is significant that the method map()/unmap() of the port and not of the port of a flowchart block should be called. The following line of code is invalid:;
Ports of flowchart blocks source.out.connect(; source.out.disconnect(;

AnyLogic also provides you the ability to programmatically disconnect a port from all connected ports using the following method of Port class:

void disconnectAndUnmapAll();

Connecting ports of Process Modeling Library blocks

If you rewire the connections dynamically (by calling connect()/disconnect() or map()/unmap() methods of ports of Process Modeling Library blocks), the end ports will not notice that and will continue behaving according to the out-of-date connections that were established at startup.

This demo model illustrates how to connect Process Modeling Library blocks dynamically. It has two separate parts of a flowchart that are connected by clicking on the button placed between them. Take a look at the button's Action to understand the approach.

How can we improve this article?