10.4. Lists: Passing Information Among Levels in a Swarm Model

Simplified scheduling is not the only usage for lists. It is equally important that list objects can be used to quickly communicate a great deal of information between objects. This is done by creating methods that can get a list and pass it to another object.

In the PplModelSwarm.m file, for example, one finds this method:

- getPplList
{
  return pplList;
}
      

When another object needs a list of people, the PplModelSwarm is able to supply it.

This design is extremely convenient when it comes time to consider the observer swarm level of the simulation. The PplObserverSwarm.m file gets the list of people from the PplModelSwarm and uses that list to collect data in order to construct graphs. Consider the avgFriendGraph object, for example, which charts the average number of friends per person. The buildObjects section of PplObserverSwarm.m has this command:

[avgFriendGraph createAverageSequence: "avgNumFriends"
        withFeedFrom: [pplModelSwarm getPplList]
         andSelector: M(getNumFriends)];
      

The method createAverageSequence:withFeedFrom:andSelector is equipped to take a list of objects, ask each one to supply a piece of data (the getNumFriends returns an integer from the person object), and builds an average that is plotted. This powerful, easy method of passing information for presentation is possible because the various Swarm libraries are designed to work together. While the user could certainly ignore the List protocol and design her own setup for managing collections, doing so would indeed be costly because one would be forced to forfeit the convenient features of the other libraries that can handle Swarm List objects.

The ability to pass a list to the observer swarm in order to create a graph is just one benefit of Swarm List protocol. Note in the PplModelSwarm example that when people are created, one of the set messages (setWorld:Room:Party:) tells the individual person in which list it is currently residing. When that method executes, it sets the value of an instance variable called pplPresent inside the person. (Look at the code in Person.m to verify it!) Since each individual person has that list available, it can ask the list for information. For example, to find out how many other people are still in the list, the Person object can do this:

[pplPresent getCount]-1

which returns an integer equal to the number of objects in the pplList minus 1. The Person object does not have to do anything to update the pplPresent variable to reflect current conditions. Since the pplPresent variable is actually a pointer to the pplList as it currently exists in the PplModelSwarm, this is always "up to date". Some additional usages that the Person class might include require the creation of Index objects, which are introduced in the next section.