Dynamic model changes

Sometimes you want to make changes of the model during a simulation run. Connections have to be rewired, an agent has to move to another domain or removed from the model completely. For these actions agents have to generate change requests. Change requests are handled after a simulation cycle has finished.

Requesting a model change

The change request class determines the type of change. There are five classes for basic requests:

  • EntityRemoveRequest
  • DomainChangeRequest
  • PortConnectRequest
  • PortDisconnectRequest
  • PortReconnectRequest (combining the two above)

You can also extend these by making your own implementation of the ChangeRequest interface. To issue a request, it has to be added to the global queue by using the static method addModelChangeRequest. The following example moves an agent to an other domain and reconnects its outport:

private moveAgentToNewDomain() {
		AbstractAgent.addModelChangeRequest(new DomainChangeRequest(this,newDomain));
		AbstractAgent.addModelChangeRequest(new PortDisonnectRequest(outport,oldDestination));
		AbstractAgent.addModelChangeRequest(new PortConnectRequest(outport,newDestination));
}

Enable dynamic changes

By default, the simulator assumes a constant model. To enable changes during the simulation run, a special adapter has to be added to the simulator. This DynamicDecoratorhooks into the simulation loop and handles all change requests.

public static void main(String[] args) {
	AbstractDomain model=new MyModel();
	ForwardingStrategy fs=new DefaultMessageForwarding(model);
	Simulator simulator=new DynamicDecorator(new SequentialDESimulator(model,fs));
	simulator.runSimulation(Time.INFINITY);
}