swarm-support
[Top][All Lists]
Advanced

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

Re: [Swarm-Support] EZBin in Java


From: Marcus G. Daniels
Subject: Re: [Swarm-Support] EZBin in Java
Date: Sun, 06 Nov 2005 19:02:37 -0700
User-agent: Mozilla Thunderbird 0.9 (Windows/20041103)

Steve Railsback wrote:

I am having trouble building and using an EZBin in JavaSwarm...could somebody please post an example?

Sure, attached.
import swarm.Globals;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.defobj.Zone;
import swarm.activity.Activity;
import swarm.objectbase.Swarm;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.analysis.EZBinImpl;
import swarm.analysis.EZBinCImpl;
import swarm.analysis.EZBinC;
import swarm.analysis.EZBin;
import swarm.Selector;
import java.util.LinkedList;
import java.util.List;

public class TestEZBin extends GUISwarmImpl {
  class Agent {
    double value;

    Agent (double value) {
      this.value = value;
    }

    void newValue () {
      value = Globals.env.uniformDblRand.getDoubleWithMin$withMax (.1, 1);
    }

    public double getAgentValue () {
      return value;
    }
  }
 
  List list;
  Schedule schedule;
  EZBin ezbin;

  TestEZBin (Zone aZone) {
    super (aZone);
  }
  
  void addValueToList () {
    double v = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0, 1);

    list.add (new Agent (v));
  }

  public void step () {
    addValueToList ();
    ezbin.reset ();
    ezbin.update ();
    ezbin.output ();
    
    getActionCache ().doTkEvents ();
  }

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

    list = new LinkedList ();
    addValueToList ();

    EZBinC ezbinC = new EZBinCImpl (new EZBinImpl ());
    ezbinC.createBegin (getZone ());
    ezbinC.setTitle ("EZBin");
    ezbinC.setAxisLabelsX$Y ("X label", "Y label");
    ezbinC.setBinCount (5);
    ezbinC.setLowerBound (0);
    ezbinC.setUpperBound (1);
    ezbinC.setCollection (list);
    try { 
      Selector sel = new Selector (Agent.class,
                                   "getAgentValue",
                                   false);
      
      ezbinC.setProbedSelector (sel);
    } catch (Exception e) {
      e.printStackTrace (System.err);
      System.exit (1);
    }

    ezbin = (EZBin) ezbinC.createEnd ();
    return this;
  }

  public Object buildActions () {
    super.buildActions ();
   
    schedule = new ScheduleImpl (getZone (), 1);

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

      schedule.at$createActionTo$message (0, this, sel);
    } 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 ();
  }
  

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

    TestEZBin ezbinTest = new TestEZBin (Globals.env.globalZone);

    ezbinTest.buildObjects ();
    ezbinTest.buildActions ();
    ezbinTest.activateIn (null);

    ezbinTest.go ();
  }
}

reply via email to

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