Every agent that lives in environment has visual non-removable element connections that defines the contact network for this agent defined by the environment.
However, you may need to define more contact networks or single links.
For example, if a person has parents, spouse, and children, then the corresponding agent can have these references:
You may want your agent to communicate only with those agents that are among this agent connections. Links may be single or multiple, unidirectional or bidirectional.
- Bidirectional link. Examples: friendship, common interest, information exchange
- Unidirectional link. Examples with possibly different meanings: child-parent, boss-subordinate, client-salesman, workstation-server, producer-consumer
You can establish contact links using the element Link to agents.
To create a new link to agents
- Drag the Link to agents element from the Agent palette onto the agent's diagram.
- Go to the properties of the link and specify the name of the link in the Name field. e.g. father.
- Choose whether this link is Single (Single link option), or Multiple (Collection of links option). You may notice that the link icon changes when you change the link type.
- You may display links connecting your agents on the model animation. To show links on the animation, open the Animation section of the link's properties, and select the option Draw line connecting agents. Then configure the visual appearance of the link using the properties below: choose whether you want to draw the link behind agents, or on top of them, and set the link's line color, width, style (solid, dotted, or dashed), and choose the arrow position (end or center).
Name — The name of the link.
Show name — If selected, the link name is displayed on the agent diagram.
Ignore — If selected, the link is excluded from the model.
Visible — If selected, the link icon is visible on the animation at the model runtime.
Agent type — Choose here the type of the agent .
Collection of links — If selected, the link is multiple — it connects the agent with several other agents.
Single link — If selected, the link is single — it connects the agent with just one agent.
Make bidirectional/unidirectional — Click the button if you want to make the link bidirectional/unidirectional. Example of a bidirectional link: friend. Example of a unidirectional link: mother.
Message type — Specify the type of the messages that will be received by these connections. You may need this to simplify access to the message fields in the On message received action. You can choose one of the most-used types (int, double, boolean, String) using the corresponding option to the right. However, if your messages are of some other Java class, choose the Other option and specify the message type in the control to the right.
On message received — Code to be executed when the agent receives a message from some another agent (possibly, from itself). Message sender is available in the code as sender, and the just received message as msg.
Forward message to — Here you can choose the message recipient statechart(s) that you want to process the received messages. To enable processing messages in some statechart, select the checkbox corresponding to this statechart in the given table.
You may display links connecting your agents on the model animation. To show links on the animation, select the option Draw line connecting agents. Then configure the visual appearance of the link using the properties below: choose whether you want to draw the link behind agents, or on top of them, and set the link's line color, width, style (solid, dotted, or dashed), and choose the arrow position (end or center).
Having created the contact link, you should make this link the reference to the required agent(s).
These references can be set when the agents are created or dynamically during simulation. The connections can be managed manually via agent functions, please refer to the Standard agent contacts network article for the details.
Here is the API you may use to change the connections at runtime. It differs depending on the type of the link (single or multiple):
- Single link
Function Description Agent getConnectedAgent() Returns the connected agent. connectTo(Agent a) Adds a given agent to the connections of this agent and vice versa. boolean disconnect() Disconnects this agent from the currently connected agent.
- Collection of links
Function Description LinkedList<Agent> getConnections() Returns the list of all connected agents. int getConnectionsNumber() Returns the number of connected agents. boolean isConnectedTo(Agent a) Tests if this agent is connected to a given agent Agent getConnectedAgent(int index) Returns the connected agent with the given index. Agent getRandomConnectedAgent() Returns the randomly chosen connected agent. connectTo(Agent a) Adds a given agent to the connections of this agent and vice versa.
boolean disconnectFrom(Agent a) Disconnects this agent from a given other agent. disconnectFromAll() Disconnects the agent from all other agents.
Having created and initialized the link, you can send messages to the connected agent.
- Single link
Function Description send(Object msg) Sends the message to the connected agent.
- Collection of links
Function Description send(Object msg, Agent dest) Sends the message to the specified connection.
To send a message "Hi!" to the link named father, call father.send("Hi!")
sendToAllConnected(Object msg) Sends the message to all connections. sendToRandomConnected(Object msg) Sends the message to a randomly chosen connection.
How can we improve this article?