Reputation: 2803
I'm creating an AnyLogic Agent that I'd like to frequently re-use. In many AnyLogic agents and components, there's are spaces where you can type in code like "on receive" or "Action" or "function body". These are compiled into the Agent when the model is built. How does the average programmer do this?
For example, suppose I have this Agent (pseudocode, obviously):
Button extends Agent
name : Parameter // A normal parameter for recording the button's name.
color : Parameter // A normal parameter for recording the button's color.
onPress : <code> // User-provided code that is executed when the button is pressed.
A client might use it as follows:
userButton7 is a Button
name = "Emergency Button"
color = Red
onPress = {
for each Person p in simulation
p.tryToEscapeBuilding()
}
Is this possible?
Upvotes: 0
Views: 3306
Reputation: 2517
You can't write your own Agent (as a plain Java class) which includes elements in the AnyLogic model-building UI, if that's what you mean (plus, if you write your own Java class which extends Agent, you have to know the correct signatures to use for the two required constructors, which you can see from looking at the code generated for other Agents).
But, in AnyLogic 7, you can design an Agent visually and then use a limited form of inheritance to create subclasses of this Agent. (See the AnyLogic help on Agent Inheritance under Agent Based Modeling.) The inheritance is limited in that:
[Thanks to nikolaj for pointing out that function overrides are possible. I could have sworn I tried that in the initial AnyLogic 7 release and it had the same restriction as for parameters/variables; maybe it got changed in one of the 7.0.x point releases?]
Both of these can be pretty restrictive dependent on the circumstances but, in your example, this would be fine: have an Agent which has/'is' a button (with AnyLogic parameters for color, name, etc.).
Note that your onPress pseudo-code is problematic for a reusable Agent unless you do something like having a List<? extends Person>
as a parameter for your Agent (so that, when created, it has a generic reference to something that allows it to loop through Agents with a tryToEscapeBuilding
function).
Upvotes: 1
Reputation: 717
as monsieurRigsby says, you can create a subclass of the Agent class. but it is actually possible to override functions. just write: @Override in the "Custom modifiers" of the onPress function
Upvotes: 0