Custom Port Classes

You can customize the port behavior by writing your own code in On Send and On Receive port properties. However, if the defined port functionality is frequently needed, defining your own port class is preferable. Thus, instead of writing the same code for all the port instances in your model, you need just to create new port class once and specify this class as the port class for the ports you need. Create your own port class as a new Java class, or in a library. Defining port class in a library has an advantage of future reuse of your custom ports in other models as well.

To define a custom port class

  1. In the Projects view, right-click (Mac OS: Ctrl click) the model item, and choose New > Java Class… from the popup menu.
  2. The New Java Class dialog box is displayed.
  3. Specify the name of the new class in the Name edit box.
  4. Specify the name of the base class. Type in Port in the Superclass edit box. Port class is the base class for all port classes in AnyLogic. This class provides basic port functionality and if you want to customize the default behavior of ports, you need to derive your own port classes from Port class.
  5. Click Finish to complete the process.
  6. You will see that the code editor for the just created class is opened. Write there code for your port. Here you can override some functions of the base class Port. When finished, you can use this class as a port class for any port instances in your model.
To set the created class as a port class for a port instance
  1. Select the port in the graphical editor.
  2. In the Advanced section of the Properties view, select the Custom port check box.
  3. In the Constructor code field, type the constructor of the created port class, e.g. MyPort(this)