Custom Port Classes
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
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.
a custom port class
set the created class as a port class for a port instance
- In the Projects view,
right-click (Mac OS: Ctrl+click) the model item, and choose New > Java Class…
from the popup menu.
- The New Java
Class dialog box is displayed.
- Specify the name of the new class in the Name edit box.
- Specify the name of the base class. Type in Port in the Superclass edit
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.
- Click Finish to complete
- 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.
- Select the port in the graphical editor.
- In the Advanced section
of the Properties
view, select the Custom port
- In the Constructor code
field, type the constructor of the created port class, e.g. MyPort(this)