Step 11. Making People Contacts Distance-Based
Now people are located randomly in 650x300 miles square area. Let people only contact if they are not more than 25 miles away.
- Open the Main class diagram by double-clicking the Main item in the Project view.
- Select the environment object.
- Open the Advanced' tab of the environments Properties view.
- Change the Network type to Distance based and modify the Connection range: 25.
- Open the Person class diagram by double-clicking the Person item in the Project view.
- Click on the internal transition of the Adopter state.
- Modify the transition's Action. Substitute the existing string for the following:
send (“Buy!”, RANDOM_CONNECTED);
People will now contact only with their neighbors.
Now we will modify our animation a little bit to visualize the network of contacts.
- Open the Person class diagram.
- Draw a line like shown in the figure below.
Choose the Line element from the Presentation page of the Palette view. Then click in the graphical editor at the point (0,0) to place the start point of the line and finally click aside where you want to draw the end point.
- Open the Dynamic' tab of the lines Properties view and modify some dynamic properties of the line.
- In the Replication field, type:
(you can use code completion assist when typing).
Here we specify the number of contact lines for the agent. We need to draw a line for each connection of this agent. For that, we call the getConnectionsNumber() function, returning the number of agents currently connected to this one in our network.
- Define line's location on the animation:
In the dX field, type:
getConnectedAgent(index).getX() - getX()
In the dY field, type:
getConnectedAgent(index).getY() - getY()
Here we place the end point of each line to the point with the coordinates of the corresponding connected agent. Connected agents are alternately accessed using the getConnectedAgent(index) function.
Now those people who contact with each other will be shown on our presentation connected with lines.
We have finished creating the animation. Run the model and watch the dynamics on the network.