Swarm is designed to help researchers build models in which low-level actors interact (often called "complex systems"). The researcher has to give content to "agents," possibly by thinking of them as honey bees, investors, trees, or (the ubiquitous) "bugs." One research goal is to discern overall patterns that emerge from these detailed behaviors at the individual level.
Object oriented programming is ideally suited to represent models of this sort. As we shall see, the objects are self-contained. Objects may be designed to convey information (answer questions) from other objects and also they can retain, categorize, and summarize information.
A careful study of either of the object-oriented programming languages (Java or Objective-C) is required before any significant progress can be made in building a Swarm model. The material presented here is intended as a summary or reminder of such a study, rather than a substitute.
An object consists of two kinds of information
Variables. The list of variables summarizes the "state" of the agent--its age, wealth, its ability, and so forth. These variables may be of any type that is allowed in C, such as integer (int), floating-point number (float), an array, a pointer, and so forth. These variables might also be of type id, which means they might also be instances of classes, and;
Methods. Methods determine what the object can do. Typically, there will be methods that receive information from the "outside world", methods that send messages to the outside, and methods that process information.
Variables and methods are given meaningful names, so code is easier to read. The custom is to run words together to make meaningful tags, such as goToStore or goHome.
Objects are created through a process called "instantiation." Put tersely, code is written in "classes" and then objects are created as instances of their classes. The varibles that an instance, or object, keeps inside itself are called "instance variables". The information contained inside instance variables is available to all methods inside that object. If one of the methods in an object needs to have "private" information that is not available to other methods in the object, then "method variables" can be created to hold that information.
In both Objective-C and Java, the term message is often used to refer to an instruction that tells an object to carry out one of its methods. (For readers more familiar to C++, the term member function, refers to the same thing as the term method). Here is an example of a message that tells an object known as bobDole to execute its method runForPresident.
Objective C example | Java example | ||
---|---|---|---|
|
|
In Objective C, some methods have parameters that specify details and they are added with colons (:) after the name of the method to be executed. In Java, the entire method name is listed before the parameters are given [1]. For example, if the method runForPresident required additional parameters, such as the year and the name of the runningmate, then the message might look like so:
Objective C example | Java example | ||
---|---|---|---|
|
|
We will have plenty of additional examples in the rest of the Guide.
[1] | In our Java example we use a dollar sign ($) inline between the parts of the method that are separated in the Objective C case. This is purely a convention introduced to stay as close to conventions adopted by the Java Swarm libraries. This is in no way enforced by the Java language itself. |