Programming with objects

The central idea of OOP is message passing. To develop an appreciation for computing with objects, we'll make objects mobile in this assignment. Mobile objects are also called "mobile agents". Since objects are autonomous, each object maintains its own thread of execution. So multithreading is an essential requirement. In an open environment, you might run into unknown objects. How do you find information about these objects? And finally, when a mobile object transports itself to another location, how does it package itself -- keeping its state intact?

The features neeed for this project - mobility, multithreading, runtime object identification, and object packaging -- are known in Java as RMI (remote method invocation), threads, reflection, and serialization.

What's the problem?

You have to hunt for a Treasure object which can be in one of five sites. Each of the five sites has some distinctive characteristics. During the search, you should also learn something about the characteristics of these sites. If you find the Treasure object, you extract its secret message. That's your reward. What you do in case you do not find the treasure is discussed below.

Each of the five sites is managed by its Site Manager. The Site Manager keeps a pool of objects. This pool is populated by objects of type: Snake, Unicorn, Frog, and Flower. These objects are not mobile.

There is one Treasure object which is mobile. It visits one of these sites, stays in the object pool for a while, and then leaves for another site. The Treasure object can also decide to vanish altogether.

And finally a Player object. This is the object which searches for the Treasure object. It visits each site, interacts with the object pool through the Site Manager at that site. If it interacts with the Treasure object, the search is over; it extracts the secret message. Otherwise, it should request more interactions or visit another site. The Player starts with a limited amount of time (hops) and cash.

There is exactly one Player object and one Treasure object.

What does "interaction" mean? The player chooses an index into the pool, sends it to the Site Manager and asks the Site Manager for the corresponding object. The Site Manager returns the object. If the Player gets a Snake, it loses a hop. If it gets an Unicorn, it gains a hop. If it gets a Frog, it loses 10 units of money. If it gets a Flower, it gains 10 units of money. There is a fee for staying at a site: the site manager charges 5 units of money for each interaction. (This applies only to the Player, not to the Treasure object.)

Can the Player find the Treasure object without running out of running (hop count) and cash?

Details

The sites are known by their names: "Site0", ... "Site4". The site manager of each site contains an array (pool) of objects. The size of this array is available in a config file. This array is initialized with objects of type: Flower, Frog, Unicorn, and Snake. Each site has a different distribution of these objects. For example, Site0 might be infested with Snakes. This distribution is given by probabilitites. (In this example, in Site0 the probability of finding a Snake is 0.8.) The probabilities for each site are available in the config file.

If the Treasure object visits the site, the site manager puts it into a random position in the array. When the Treasure object leaves the site, the site manager removes it from the array.

Player's starting site is: "Site0". The Player chooses an index into the array and send it to the site manager. The site manager returns the object at that index position. Depending on the type of object returned, the player updates its state (cash and hops).

The Player can decide to leave at any time; it chooses one of the other sites and goes there. A good player has a strategy. For example, a good Player avoids sites having lots of Snakes and/or Frogs. The Player learns from experience where to stay and where to go in case of cash shortage. You need not do this. But you should keep track of the characteristics of each site: the approximate probability of finding a Snake, Unicorn, etc.

Note the Player is responsbile for reducing and increasing the cash and hops in hand based on the interactions. (There is a possibility of the Player cheating. To avoid this, one should to work with electronic cash which in turn needs Java Cryptographic Extension. We'll not be doing this in this project. You should write an honest Player.)

If the Player runs out of cash or hops, it should return to "Site0" and print the information it learned about the environment: the approximate probabilities of finding Snake, etc. in each of the sites and the hops and cash left. If it finds the Treasure object, it should again return to "Site0", print the message the Treasure object contains, and also other information like hops and site information.

How does the Player extract the secret string?

The class of the treasure object is Treasure. This class contains exactly one public method of zero arguments which returns a string. You should not assume anything about the name of this method. You should discover this method using Reflection. Using this method you should extract the string.

The classes

You should use Java for this assignment. Here's what you write.
  1. The classes Snake, Frog, Flower, and Unicorn. These are just names and they don't do anything (in this assignment). The Player accounts for their behavior (by being honest). Note that each site has a predominace of one of the objects.
  2. Site Manager: The Site Manager is an RMI service. All Site Managers can reside on the same machine. The Site Manager keeps
    1. An array of objects of various types. This array is initialized according to the probabilities provided in the config file. The size of this array is also available in the config file.
      This list may contain the Treasure object.
    2. Player, if it visits the site.
    3. Treasure object, if it visits the site. This is kept in the array of objects. When the Treasure object enters or leaves the site, the site manager should update its state.
    The Site Manager implements the methods in the interface Site.java (code provided below).
  3. Player:
    1. Data:
      1. the estimated probability of Snake, Frog, etc, in each site
      2. cash in hand
      3. hops left
    2. Behavior:
      1. ask for an interaction
      2. recognize Treasure object and extract the secret string
      3. move to next site
    If the Player finds the Treasure object, it gets secret message, returns to "Site0" and prints the message, number of hops used, the amount in hand, and the probabilities of objects at different sites.
    If the Player runs out of hops or cash, it return to "Site0" and prints the hops, cash, and probabilities.
  4. Treasure object: You are provided code for this. This basically stays at each site for a while, moves around, and vanishes after a while. The parameters for its behavior are available in the config file.
The Player and the Treasure object use RMI to hop among sites.

Relevant code

  1. Treasure.java, Task.java: This is the code for the Treasure object
  2. SiteConfig.java: This is the java class
  3. Site.java: This is the Java interface describing the Site Manager.

Code

You should keep all your Java sources and a Makefile (which compiles your code) in a tar file called object.tar and use bundleP3 to submit the tar file.

Report

This assignment involves lot of ideas: objects, threads, reflection, RMI, etc. You should carefully evaluate each of these features in terms of need, complexity, and the abstraction provided by Java.

You should also write about the complexity of doing this in C++. You should comment on: feasibility of doing this in C++, extra libraries required like networking, remote procedure calls, language support required, etc.

What class of real world problems are similar to the one in this assignment? How are the other frameworks like procedural and functional programming suitable for solving this class of problems? What the advantages of OOP features: abstraction, message passing, inheritance, reflection, etc.

Do you think the need for runtime support will increase in the future?

There are multiple theads running in this example. You should write about the shared resources and how your design ensures the consistency of shared resources and avoids deadlocks.

The minimum length of your report should be five pages.

This project is due: Feb 25, 2000. You should submit the report, hardcopy of your code, and the self-evaluation form to Renata Teixeira on Feb 25, 2000 during her office hours. Your report should contain the name of the account from which the code was turned in.

Some useful code and pointers can be found here