swarm-support
[Top][All Lists]
Advanced

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

Re: none


From: Marcus G. Daniels
Subject: Re: none
Date: 10 Dec 2001 23:47:25 -0700
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.7

>>>>> "SP" == Snow Paul <address@hidden> writes:

SP> I do not know how to add more new agents into a running Swarm.
SP> Give me an example please.

The Java code below has one parent (called "Agent") that makes a child
(a "BabyAgent") every 100 steps.  Meanwhile, the older children do
their thing (print a message identifying themselves) while the new
babies are born by the parent.  Soon there are babies stepping at the
same time with the parent (e.g. timestep # 225).

[The complicated alternative logic associated with the "else" of the
"if (!randomized)" checks can be ignored, initially.  It shows how to
set up custom Swarm scheduling policies in order to get randomization
of events from the logically concurrent events of the parent and
children.  To activate that, pass "-m randomize" on the command line,
with "--varyseed".]

Note how both Agent and BabyAgent inherit from BaseSwarmImpl.
BaseSwarmImpl is an abstract class that encapsulates potentially-
complex argument passing using creteFAction closures.  (That would
follow the "fa = new FArgumentsImpl", e.g. a call to "addDouble".)

This test code also contrasts simple Java constructors for Swarm
object creation with the more complex phase-switched object creation
idioms.

import swarm.objectbase.SwarmImpl;
import swarm.objectbase.SwarmCImpl;
import swarm.defobj.Zone;
import swarm.Globals;
import swarm.activity.ScheduleImpl;
import swarm.activity.ScheduleCImpl;
import swarm.activity.ScheduleC;
import swarm.activity.Schedule;
import swarm.activity.Activity;
import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmC;
import swarm.defobj.FArgumentsImpl;
import swarm.defobj.FArguments;
import swarm.defobj.FCall;
import swarm.defobj.FCallImpl;
import swarm.Selector;

import swarm.activity.ConcurrentGroupC;
import swarm.activity.ConcurrentGroupCImpl;

abstract class BaseSwarmImpl extends SwarmImpl {
    Schedule schedule;
    int stepCount;

    BaseSwarmImpl () { }

    BaseSwarmImpl (Zone aZone) {
        super (aZone);
        init ();
    }
    
    void init () {
        stepCount = 0;
    }
    FCall createCall (String name) {
        try {
            Selector sel = new Selector (getClass (), name, false);
            
            FArguments fa = new FArgumentsImpl (getZone (), sel);
            
            return new FCallImpl (getZone (), this, sel, fa);
        } catch (Exception e) {
            e.printStackTrace (System.err);
            System.exit (1);
        }
        return null;
    }

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

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

    abstract public String getId ();

    public void stepAgent () {
        System.out.println (getId () +
                            " step #" + stepCount + " @ " +
                            Globals.env.getCurrentTime ());
        stepCount++;
    }

    public void terminate () {
        getActivity ().stop ();
    }
}

class Agent extends BaseSwarmImpl {
    Schedule stopSchedule;

    Agent () { super (); }
    
    Agent (Zone aZone) {
        super (aZone);
    }
    
    // only used in randomize configuration
    Agent (AddAgentDemo parent) {
        if (AddAgentDemo.randomize) {
            SwarmC swarmC = new SwarmCImpl (this);
            
            swarmC.createBegin (parent.getZone ());
            swarmC.setSynchronizationType
                (parent.getSynchronizationType ());
            swarmC.createEnd ();
            init ();
        }
    }
    
    public String getId () {
        return getName ();
    }

    public Object buildActions () {
        schedule = new ScheduleImpl (getZone (), 100);
        stopSchedule = new ScheduleImpl (getZone (), true);

        schedule.at$createFAction (25, createCall ("stepAgent"));
        stopSchedule.at$createFAction (500, createCall ("terminate"));
        return this;
    }

    public void stepAgent () {
        super.stepAgent ();
        
        BabyAgent baby = new BabyAgent (getZone ());

        baby.buildObjects ();
        baby.buildActions ();
        baby.activateIn (this);
    }

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

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

class BabyAgent extends BaseSwarmImpl {
    static int instanceCounter = 0;
    int instanceNumber;

    BabyAgent (Zone aZone) {
        super (aZone);
        instanceNumber = instanceCounter++;
    }

    public String getId () {
        return getName () + "#" + instanceNumber;
    }

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

        ScheduleC scheduleC = new ScheduleCImpl (new ScheduleImpl ());
        scheduleC.createBegin (getZone ());
        scheduleC.setRelativeTime (true);
        scheduleC.setRepeatInterval (9);
        schedule = (Schedule) scheduleC.createEnd ();

        schedule.at$createFAction (2, createCall ("stepAgent"));

        return this;
    }
}

public class AddAgentDemo extends SwarmImpl {
    public static boolean randomize = false;

    static Object createSyncScheduleType () {
        if (randomize) {
            ConcurrentGroupC groupProtoC = new ConcurrentGroupCImpl ();
            groupProtoC.customizeBegin (Globals.env.globalZone);
            groupProtoC.setDefaultOrder (Globals.env.Randomized);
            Object groupType = groupProtoC.customizeEnd ();
            
            ScheduleCImpl syncScheduleProtoC = new ScheduleCImpl ();
            syncScheduleProtoC.customizeBegin (Globals.env.globalZone);
            syncScheduleProtoC.setConcurrentGroupType (groupType);
            syncScheduleProtoC.setAutoDrop (true);
            return syncScheduleProtoC.customizeEnd ();
        }
        else
            return null;
    }

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

        if (Globals.env.arguments.getAppModeString ().equals ("randomize"))
            randomize = true;

        if (randomize) 
            {
                SwarmC demoC = new SwarmCImpl (new AddAgentDemo ());
                demoC.createBegin (Globals.env.globalZone);
                demoC.setSynchronizationType (createSyncScheduleType ());
                demo = (AddAgentDemo) demoC.createEnd ();
            }
        else
            demo = new AddAgentDemo (Globals.env.globalZone);
        
        demo.buildObjects ();
        demo.buildActions ();
        Activity activity = demo.activateIn (null);

        
        activity.run ();
    }
    
    public Agent agent;

    public Object buildObjects () {
        super.buildObjects ();
        
        if (!randomize)
            agent = new Agent (getZone ());
        else
            agent = new Agent (this);
        return this;
    }

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

        agent.buildActions ();
        return this;
    }

    public Activity activateIn (Swarm swarmContext) {
        super.activateIn (swarmContext);
        agent.activateIn (this);
        return getActivity ();
    }

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

    AddAgentDemo () {
    }
}

                  ==================================
   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]