Main Page | Recent changes | View source | Page history

Printable version | All content dual-licensed under the GNU FDL and the Creative Commons Share-alike Attribution license. | Privacy policy

Not logged in
Log in | Help
 

Swarm FAQ:Writing Java

From SwarmWiki

Contents

Q: What are the *C, *Impl, and *CImpl versions of JavaSwarm classes?

These different classes are work-arounds for the Objective-C "createBegin" and "createEnd" methods, that are needed for some JavaSwarm classes, some of the time.

Most JavaSwarm classes can be instantiated using standard constructor methods, and some have "create" methods that can initialize instances. But some classes have optional, or mandatory, creation phases that are more elaborate, and correspond to using the "createBegin" and "createEnd" methods in Objective-C. For example, to use an EZBin histogram, you need not only to instantiate an EZBin but also to do stuff like set the collection of objects it reports, set axis labels, bin sizes, etc. The methods to do this initialization are in "EZBinCImpl", not in "EZBinImpl".

In this case, what you must do is:

  • Instantiate a *Impl object (where "*" is the class you are trying to instantiate).
  • Instantiate a *CImpl object, passing it the *Impl object as a construction parameter.
  • Send create-phase (initialization) messages to the *CImpl object---such as "setAxisLabelsX$Y".
  • Send the "createEnd" message to the *CImpl object, at which time it returns an object that conforms to the "*" protocol and is an instance of *Impl.

Here is an example for creating and initializing an instance of EZBin:

 ...
 import swarm.analysis.EZBin;
 import swarm.analysis.EZBinCImpl;
 import swarm.analysis.EZBinImpl;

 ...
	// Declarations
 ...
 private EZBinCImpl bugSizeHistogramC;
 private EZBin bugSizeHistogram;
 
 ...
 // First, create a new *CImpl, passing it a new *Impl
 
   bugSizeHistogramC = new EZBinCImpl (new EZBinImpl());

 // Now, send the intialization messages to the CImpl

   bugSizeHistogramC.createBegin(getZone());
   bugSizeHistogramC.setCollection (stupidModelSwarm.getBugList());
   bugSizeHistogramC.setAxisLabelsX$Y("Bug size", "Number of  bugs");
   bugSizeHistogramC.setBinCount(10);
   bugSizeHistogramC.setLowerBound(0);
   bugSizeHistogramC.setUpperBound(10);
   bugSizeHistogramC.setTitle("Bug Sizes");
   try {
    bugSizeHistogramC.setProbedSelector	(new Selector 
   		(Class.forName ("stupidModel14.StupidBug"), "getMySize", false));
       } catch (NonUniqueMethodSignatureException e) {
           e.printStackTrace();
       } catch (SignatureNotFoundException e) {
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }

 // Finish up by sending the "createEnd" message

   bugSizeHistogram = (EZBin) bugSizeHistogramC.createEnd();
 
 // Now bugSizeHistogram is the object we use by sending messages like "update()" and "output()"
 // that are in the interface "EZBin"

Q: How do you make QSort work in JavaSwarm?

QSort is Swarm's implementation of the C standard library's QSort routine, to sort a collection of objects. QSort requires a method that compares two objects and reports which one should be first. In C and Objective-C Swarm, QSort assumes this compare method is called "Compare" (unless another method name is specified). However, in Java Swarm there are some quirks:

  • QSort (according to Marcus Daniels, Nov. 2005) works on Swarm collections but not Java collections.
  • QSort must be given a selector specifying the name of the compare method,
  • But the compare method's name must not be "Compare".

Here is an example use of QSort provided by Marcus. (There is another example in the StupidModel template code at the template software page


import swarm.Globals;
import swarm.collections.List;
import swarm.collections.ListImpl;
/*
import java.util.List;
import java.util.LinkedList;
*/

import swarm.simtools.QSort;
import swarm.simtools.QSortImpl;
import swarm.Selector;

public class TestQSort {
    class Agent {
     public int value;

     Agent (int value) {
         this.value = value;
     }
     public int compareAgents (Agent peer) {
         return value - peer.value;
     }
    }

    List list;
    TestQSort () {
     list = new ListImpl (Globals.env.globalZone);
     list.addLast (new Agent (5));
     list.addLast (new Agent (1));
     list.addLast (new Agent (10));
     /*
     list = new LinkedList ();
     list.add (new Agent (5));
     list.add (new Agent (1));
     list.add (new Agent (10));
     */
    }
    
    void doSort () {
     Selector sel;
     try {
         sel = new Selector (Agent.class, "compareAgents", false);
         QSort qsort = new QSortImpl (Globals.env.globalZone);
         qsort.sortObjectsIn$using (list, sel);
     } catch (Exception e) {
         System.err.println ("could not find selector " + e);
         System.exit (1);
     }
     
     for (int i = 0; i < 3; i++)
         System.out.println (((Agent) (list.atOffset (i))).value);
     /*
     for (int i = 0; i < 3; i++)
         System.out.println (((Agent) (list.get (i))).value);
     */
    }

    public static void main(String[] args) {
     Globals.env.initSwarm ("TestQSort", "0.0", "bug-swarm@swarm.org", args);

     new TestQSort ().doSort ();
    }
}

Q: What is a blank question to make the TOC show up?

(The wiki software only puts a table of contents at the top of the page if there are at least 4 subheadings in the page.)

Q: What is another blank question to make the TOC show up?


[Main Page]
Main page
About SwarmWiki
News
Recent changes
Random page
Help

View source
Discuss this page
Page history
What links here
Related changes

Special pages