Documentation Set for Swarm 2.2
Prev |  | Next

# Chapter 4. Mag 4x:Sketch of Code

Now that we have multiple Swarms and an experimental apparatus, it's time to learn how to use the objects themselves inside an application. Some examples are provided here: to understand this better, it will be necessary to read through example applications and the library documentation. These examples come from the Heatbugs application.

# 4.1. Building a Model Swarm

The key component of a simulation is the model Swarm. Here is the definition of a HeatbugModelSwarm, from HeatbugModelSwarm.h

    @interface HeatbugModelSwarm : Swarm {
     int numBugs; // simulation parameters
     double evaporationRate;
     double diffuseConstant;
     int worldXSize, worldYSize;
     int minIdealTemp, maxIdealTemp;
     int minOutputHeat, maxOutputHeat;
     double randomMoveProbability;

     id modelActions; // scheduling data structures
     id modelSchedule;

     id heatbugList; // list of all the heatbugs
     Grid2d * world; // objects representing
     HeatSpace * heat; // the world
    }

    -getHeatbugList; // access methods into the
    -(Grid2d *) getWorld; // model swarm. These methods
    -(HeatSpace *) getHeat; // allow the model swarm to be observed.

    +createBegin: aZone; // extra methods you
    -createEnd; // provide for Swarms
    -buildObjects;
    -buildActions;
    -activateIn: swarmContext;

The first section of code says that a HeatbugModelSwarm is a kind of Swarm. HeatbugModelSwarm inherits a lot of behavior from generic Swarm, but also adds new variables and methods.

The new variables are enclosed in the braces in the definition of HeatbugModelSwarm. They are split into three general classes of things: simulation parameters, schedule data structures, and objects in the world. This is a typical sort of model swarm.

Finally, a HeatbugModelSwarm defines new methods. The first few methods are used to allow the model to be observed: a HeatbugModelSwarm will give out its list of Heatbugs, for instance, or its HeatSpace. Observers use these methods to monitor the model.

In addition to the observation methods, there are several Swarm-specific methods for the building of Swarms. These are fairly stereotyped. The `createBegin` and `createEnd` messages are used to create the Swarm object itself. `buildObjects` builds the model objects, and `buildActions` builds the model schedule - more on these later. Finally, `activateIn` arranges for the execution machinery to execute the Swarm itself.

Prev | Home | Next
Summary | Up | Defining an Agent