swarm-support
[Top][All Lists]
Advanced

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

[Swarm-Support] Re: Lists & createActionForEach


From: Marcus G. Daniels
Subject: [Swarm-Support] Re: Lists & createActionForEach
Date: Thu, 27 Oct 2005 21:31:15 -0600
User-agent: Mozilla Thunderbird 1.0.6-1.1.fc4 (X11/20050720)

Steve Railsback wrote:

I am messing around with Java Swarm for the first time.

We had quite an adventure coming to the conclusion that this statement:

    bugActions.createActionForEach$message (bugList, sel);

only works if the bugList is a java LinkedList, not a Swarm list. Is that correct?


I tried this with Swarm 2.2, and it worked ok for me. One thing is that it's easy to forget to do the phase initializations. Once you grok the create idioms for Swarm objects (as opposed to pure Java objects), then it becomes automatic.

Marcus
import swarm.objectbase.SwarmImpl;
import swarm.objectbase.Swarm;
import swarm.Globals;
import swarm.defobj.Zone;
import swarm.SwarmEnvironmentImpl;
import swarm.collections.List;
import swarm.collections.ListImpl;
import swarm.activity.Activity;
import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.Selector;

public class TestSwarm extends SwarmImpl {
  List l;
  ActionGroup actionGroup;

  class Agent {
    public void step () {
      System.out.println ("step " + this);
    }
  }

  public static void main (String[] args) {
    System.out.println ("main: starting initSwarm");
    Globals.env.initSwarm("TestSwarm", "0.0", "address@hidden", args);
    System.out.println ("main: finished initSwarm");
    
    Swarm swarm = new TestSwarm (Globals.env.globalZone);
    swarm.buildObjects ();
    swarm.buildActions ();
    swarm.activateIn (null).run ();
    System.exit (0);
  } 

  TestSwarm (Zone aZone) {
    super (aZone);
  }
 
  public Object buildObjects () {
    super.buildObjects ();
    
    l = new ListImpl (this);

    l.addLast (new Agent ());
    l.addLast (new Agent ());
    return this;
  }

  public Object buildActions () {
    Selector sel = null;

    super.buildActions ();

    try { 
      sel = new Selector (l.getFirst().getClass (), "step", false);
    } catch (Exception e) { 
      System.err.println ("could not find selector");
      System.exit (1);
    }

    actionGroup = new ActionGroupImpl (this);
    actionGroup.createActionForEach$message (l, sel);
    return this;
  }

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

reply via email to

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