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 class.

Connection case
Connection method

Disconnection method
A port with a port of an embedded object;

 Note that it is significant that the method map()/unmap() of the port and not of the port of an embedded object should be called. The following line of code is invalid:;

Ports of embedded objects

Note that in case you connect or disconnect a port of some Process Modeling Library object, you should call a special method of this port afterwards: source.out.refreshConnections();

Dynamic connection of  ports of Process Modeling Library objects is described in details here.

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 objects

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

To bring the end port connections up to date you need to call method refreshConnections() of the end ports that are dynamically reconnected. For example, if the out port of myService is connected programmatically, you should call:


Demonstration model: Connecting Library Objects Dynamically

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