AnyLogic allows the user to create their own Java classes in the model with any required functionality.
To add a Java class
- In the Projects view, right-click (macOS: Ctrl + click) the model item you are currently working with, and choose New > Java Class… from the popup menu.
- The New Java Class wizard is displayed.
- On the first page of the wizard, specify the name of the new Java class in the Name field and optionally type in the superclass name in the Superclass edit box.
- Click Next to go to the next page of the wizard.
- On the second page of the wizard, specify Java class fields. Fields are specified in the table, each class field is defined in the separate row. Enter the type of the field in the Type cell, name of the field in the Name cell and optionally name the access modifier in the Access cell and specify the initial value in the Initial value cell.
- Using Create constructor and Create toString() method check boxes, specify whether you want default class constructor and toString() method to be created automatically.
- Click Finish to complete the process. You will see the code editor for the created class opened.
In the Projects view, Java classes are visualized with the icon:
You write Java code for your Java class in Java editor, which can be opened by double-clicking the required Java class in the Projects view.
Java class has the only property, Ignore, allowing you to exclude the class from the model.
In AnyLogic you can create your own Java classes and include them in the model. This may serve various purposes.
For example:
- To define entities in process models with additional fields and/or functions (and nothing else). Usually, an object of the modeled system is defined as an agent. But you can define such an object using a Java class instead: if multiple objects of this class are dynamically generated in the model, you can do this to improve the performance of your model. Defining data structure as a Java class, not an agent type, may reduce model's memory footprint. But if the model performance is not an issue, we recommend you to use an agent type instead - it will be more flexible and will allow you to add any other functionality (e.g. statecharts) to these entities if required. In the example model below you will find the Task Java class used for this purpose. Demo model: Exposure to Radiation Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
- To create auxiliary data structures or algorithms. Demo model: Product Portfolio Management Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
- To create a Java class which will be launched in case you develop a complex simulation application. Demo model: Launching AnyLogic Model from External Application Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
- To include classes borrowed from somewhere else in source code form and used in the model, such as problem-oriented optimization algorithms. Demo model: Optimization in Custom Experiment Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
-
How can we improve this article?
-