swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scheduling in java


From: Marcus G. Daniels
Subject: Re: Scheduling in java
Date: 22 Apr 2001 19:26:35 -0600
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.7

>>>>> "KR" == Katherine Roberts <address@hidden> writes:

KR> At each timestep I want each of the agents to perform their step method
[..]
KR> there doesn't seem to be a way to create an ActionGroup
KR> acting on more than one class (because of the way the selector is
KR> created).

Here's one way to do it: set up a protocol for all agents and have
each of them implement it.  This way, you don't have to have them all
inherit from the same base class (also a solution), and you don't have
to rely on dynamic dispatch.

[You _can_ make a Selector using any class that happens to have the
method, and then rely on dynamic message dispatch for the others.  If
you do that, and the method doesn't exist in one of the other agents,
then Swarm will send the message of the form `doesNotRecognize (Selector)' 
to the agent(s) in question, and if that method is not implemented Swarm will
simply abort.]

import swarm.Globals;
import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;
import swarm.defobj.Zone;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.Activity;
import swarm.Selector;
import java.util.LinkedList;
import java.util.List;

interface Agent {
    void stepAgent (); 
}

class Agent1 implements Agent {
    public void stepAgent () {
        System.out.println ("stepping agent 1");
    }
}

class Agent2 implements Agent {
    public void stepAgent () {
        System.out.println ("stepping agent 2");
    }
}


public class DualStep extends SwarmImpl {
    DualStep (Zone aZone) {
        super (aZone);
    }

    List agentList;
    Schedule stepSchedule;

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

        agentList = new LinkedList ();

        agentList.add (new Agent1 ());
        agentList.add (new Agent2 ());
        agentList.add (new Agent2 ());
        agentList.add (new Agent1 ());
        return this;
    }

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

        stepSchedule = new ScheduleImpl (getZone (), false);

        try {
            Selector sel = new Selector (Agent.class, "stepAgent", false);
            
            stepSchedule.at$createActionForEach$message (0,
                                                         agentList,
                                                         sel);
        } catch (Exception e) {
            e.printStackTrace (System.err);
            System.exit (1);
        }
        
        return this;
    }

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

        stepSchedule.activateIn (this);
        return getActivity ();
    }

    static void main (String args[]) {
        Globals.env.initSwarm ("DualStep", "0.0", "address@hidden",
                               args);

        
        DualStep dualStepModel = new DualStep (Globals.env.globalZone);

        dualStepModel.buildObjects ();
        dualStepModel.buildActions ();
        dualStepModel.activateIn (null).run ();
    }
}

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]