anylogic

Can I extend an AnyLogic agent that was created as a custom flowchart block?


With a model consisting of several blocks, I can select a few of them and (using the right-click menu) "create a custom flowchart block", which is a new agent type, which I've descriptively called "Process". I can use several instances of this Process agent in my model.

What I'd like to do now is extend the Process agent, i.e. make a subclass with some specialised behavior.

To do this, I can create a new agent from scratch called "RateControlledProcess". Then in the Advanced properties, in the Extends Other Agent dropdown, I choose Process. I see a greyed-out version of the Process components appear as expected.

At this point, given that I haven't added any new behaviors to the RateControlledProcess, I would expect that I should be able to drag an instance onto my Main model and use it in the same way that I can use Process. But it doesn't work: There is an icon rectangle, but there are no ports that allow me to connect any process (or fluid, in my case) to the new instance. It seems that the subclass is missing key behavior of the parent class, breaking the concept of inheritance.

Is there a way to make a subclass of an agent that has been created as custom flowchart block?

Steps to Reproduce

  1. Start with Main like this.

enter image description here

  1. Now, select the valve and tank, right-click and choose Create flowchart block, and call the new block/agent Process.

enter image description here

  1. Now, I can add several instances of process.

enter image description here

  1. Now create a new agent, and note that it will be used in the flowchart.

enter image description here

  1. Indicate that the new agent extends Process by selecting Process from the Extends other agent dropdown. The greyed-out components from Process appear in RateControlledProcess.

enter image description here

  1. Switch back to Main, and drag a RateControlledProcess onto the canvas. Observe that the new component doesn't have any ports (small grey circles on the sides of the blue rectangle) that I can connect to. I should be able to substitute a RateControlledProcess for one of the existing Processes, but I can't find a way to connect it to the flowchart.

enter image description here


Solution

  • In general, agent inheritance is designed for reusing basic Java features like parameters, variables, and functions. However, it does not support ports or editing inherited process flowcharts. As a result, the inheritance may not be the best approach when creating custom blocks or libraries.
    As an alternative, consider adding an instance of the "Process" agent type into the "RateControlledProcess" agent type. This is also how AnyLogic library blocks are developed — when a block from the Process Modeling Library or another library is needed while creating a new one, it is typically embedded and reused within the new block. In other words, we relay on a composition approach rather than inheritance (https://www.digitalocean.com/community/tutorials/composition-vs-inheritance)