//MGD June 15, 2001
//  KR> I'm trying to create a very simple batch swarm, 

//  The file should have the same name as the first argument to initSwarm.

//  Here's an example.  First, the batchDemo.scm parameters:

//  (list
//   (cons 'batchSwarm
//         (make-instance 'BatchDemo #:when 2 #:messageString "Hello World")))

//  ("batchSwarm" is the parameter you give getObject or putObject.)

//  ..and the program:

import swarm.objectbase.SwarmImpl;
import swarm.Globals;
import swarm.defobj.Zone;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.Activity;
import swarm.objectbase.Swarm;
import swarm.Selector;

public class BatchDemo extends SwarmImpl {
    public int when;
    public String messageString;
    
    Schedule schedule;

    BatchDemo (Zone aZone, int when, String messageString) {
        super (aZone);
        
        this.when = when;
        this.messageString = messageString;
    }

    BatchDemo (Zone aZone) {
        super (aZone);
    }

    
    public void frob (String val) {
        System.out.println ("@" + Globals.env.getCurrentTime () + " " +
val);
    }

    public Object buildActions () {
        super.buildActions ();

        schedule = new ScheduleImpl (getZone (), true);

        try {
            Selector sel = new Selector (getClass (), "frob", false);

            schedule.at$createActionTo$message (when, this, sel,
                                                (Object) messageString);
        } catch (Exception e) {
            e.printStackTrace (System.err);
            System.exit (1);
        }

        return this;
    }

    public Activity activateIn (Swarm swarmContext) {
        super.activateIn (swarmContext);

        schedule.activateIn (this);

        return getActivity ();
    }
    
    static void main (String args[]) {
        Globals.env.initSwarm ("batchDemo", "0.0", "bug-swarm@swarm.org",
                               args);

        BatchDemo batchDemo =
            (BatchDemo) Globals.env.lispAppArchiver.getObject
("batchSwarm");
        
        if (batchDemo == null)
            {
                Globals.env.verboseMessage ("Creating object");
                batchDemo = new BatchDemo (Globals.env.globalZone,
                                           1, "Hello World");
            }
        
        batchDemo.buildObjects ();
        batchDemo.buildActions ();
        batchDemo.activateIn (null).run ();
    }
}

