⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 observerswarm.java

📁 本源码应用于Java的JDK1.4.2版本+Swarm2.2 程序在Cygwin终端运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import swarm.analysis.*;

import swarm.Globals;
import swarm.Selector;

import swarm.defobj.Zone;
import swarm.defobj.ZoneImpl;
import swarm.defobj.Symbol;
import swarm.defobj.Drop;
import swarm.objectbase.VarProbe;
import swarm.gui.Colormap;
import swarm.gui.ColormapImpl;
import swarm.gui.ZoomRaster;
import swarm.gui.ZoomRasterImpl;
import  swarm.gui.HistogramImpl;

import swarm.space.Value2dDisplay;
import swarm.space.Value2dDisplayImpl;
//import swarm.space.Object2dDisplay;
import swarm.space.Object2dDisplayImpl;

import swarm.simtoolsgui.GUISwarm;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.simtoolsgui.GUIComposite;

import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.Activity;

import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;

import swarm.analysis.EZBin;
import swarm.analysis.EZBinImpl;
import swarm.analysis.EZBinC;
import swarm.analysis.EZBinCImpl;

import swarm.objectbase.MessageProbe;
import swarm.objectbase.EmptyProbeMap;
import swarm.objectbase.EmptyProbeMapImpl;
import swarm.collections.ListImpl;
//import sugarscape.ModelSwarm;

public class ObserverSwarm extends GUISwarmImpl
{
        ModelSwarm _modelSwarm;
      //  char parameterFile;
       public int displayFrequency;//how often to update
       //public int zoomFactor = 8;
       public int drawPopulationGraph;//flag
       public int drawWealthHistogram;//flag
       public  Colormap   colormap;//colours
       public  ZoomRasterImpl  worldRaster;//window on world
       public  Value2dDisplayImpl  sugarDisplay;//sugar display
       public  Object2dDisplayImpl  agentDisplay;//agent displayer
       public  EZGraphImpl  populationGraph;//population graph
       public  EZGraphImpl  attributeGraph;//agent attributes graph
       public  EZBinC   wealthHistogram;//histogram of wealth
       public  EZBin wealthEZBin;
       public  ScheduleImpl displaySchedule;//schedule objects
       public  ActionGroupImpl displayActions;

   public ObserverSwarm(Zone azone)
    {
      // Use the parent class to create an observer swarm.
       super(azone);
       displayFrequency=1;
       drawWealthHistogram=1;
       drawPopulationGraph=1;

      EmptyProbeMapImpl _probeMap = new EmptyProbeMapImpl(azone, getClass ());
      _probeMap.addProbe(probeMessage("drawPopulationGraph"));
     //_probeMap.addProbe(getProbeForVariable("drawWealthHistogram"));
      _probeMap.addProbe(probeVariable("displayFrequency"));

      Globals.env.probeLibrary.setProbeMap$For
                               (_probeMap,getClass());
  }

    protected MessageProbe probeMessage (String name)
    {
       return Globals.env.probeLibrary.getProbeForMessage$inClass
        (name, ObserverSwarm.this.getClass ());
    }

    protected VarProbe probeVariable (String name)
    {
       return Globals.env.probeLibrary.getProbeForVariable$inClass
        (name, ObserverSwarm.this.getClass ());
    }

    //create all the objects.
  public Object buildObjects()
  {
      ZoneImpl modelZone;
      Colormap colormap;
      Selector sel;

      int maxSugarValue;
      SugarSpace  _sugarspace;

     //Use the parent class to initialize the process.
     super.buildObjects();

     //fist ,create a zone and swarm for the model.
      modelZone = new ZoneImpl(getZone());
       _modelSwarm =
        (ModelSwarm)Globals.env.lispAppArchiver.getWithZone$key(
          modelZone,"_modelSwarm"); //create probes for the Swarms
    //  _modelSwarm = new ModelSwarm(getZone());
    // Now create probe objects on the model and ourselves. This gives a
    // simple user interface to let the user change parameters.

     Globals.env.createArchivedProbeDisplay (_modelSwarm, "_modelSwarm");
     Globals.env.createArchivedProbeDisplay (this, "observerSwarm");

     // getControlPanel().setStateStopped();
      getActionCache ().waitForControlEvent ();

   if (getControlPanel ().getState () == Globals.env.ControlStateQuit)
     return null;


      //now build the modelswarm'objects.
     _modelSwarm.buildObjects();

     _sugarspace =_modelSwarm.getSugarSpace();// Read some handy objects out of the model swarm

      // Now tell each of the agent in the model to set its default
      //display color to green.  We do this by getting the list
      // of agents created in modelSwarm and interating through it.

      maxSugarValue =(_modelSwarm.getSugarSpace()).getGlobalMaxSugar();// Compute the colourmap

      colormap = new ColormapImpl(getZone());
     // Colours - shades of yellow for sugar values
     for(int i=0; i < maxSugarValue; i++)//如何使用colormap
      colormap.setColor$ToRed$Green$Blue((byte)i, (double)i/(maxSugarValue - 1.0 ),
                                         (double)i/(maxSugarValue - 1.0 ),0);
     colormap.setColor$ToName((byte)5, "red");//agent color

     ListImpl AgentList;
     AgentList =(ListImpl)_modelSwarm.getAgentList();
     for(int i=0;i<AgentList.getCount();i++)
      {
           SugarAgent Agent=(SugarAgent)AgentList.atOffset(i);
           Agent.setAgentColor((byte)5);
      }

        // Next, create a 2d window for displaying the world.
         worldRaster =new ZoomRasterImpl(getZone(),"worldRaster");
      try{
             sel =new Selector(getClass(),"_worldRasterDeath_",false);
             worldRaster.enableDestroyNotification$notificationMethod(this, sel);
       }catch(Exception e){
             System.err.println("Exception worldRasterDeath: "+e.getMessage()); }

        worldRaster.setColormap (colormap);
        worldRaster.setZoomFactor (6);
        worldRaster.setWidth$Height((_sugarspace.getAgentGrid()).getSizeX(),
                        (_sugarspace.getAgentGrid()).getSizeY());
        worldRaster.setWindowTitle ("Sugarscape");
        // This instructs the raster to digest all the information we
        // have just given it and to initialize itself for display.
        worldRaster.pack();
        //-------------------------
       // Create an object to display the sugar values
        sugarDisplay = new Value2dDisplayImpl(getZone(),worldRaster,
                                       colormap,
                                       _sugarspace); //_sugarspace
        sugarDisplay.setDisplayMappingM$C(1,0); //map to colormap
       //----------------------------------
        //And an object to display the agents
    try{    //use Object2dDisplay
             sel=new Selector(Class.forName("SugarAgent"),"drawSelfOn",false);
             agentDisplay =new Object2dDisplayImpl(getZone(),worldRaster,
                                             _sugarspace.getAgentGrid(),sel);
          }catch(Exception e){
            System.err.println("Exception SugarAgent/drawSelfOn"+e.getMessage());}

            agentDisplay.setObjectCollection(_modelSwarm.getAgentList());
                 // enable probes on the world
  //Configure at mouse button to send a message to a given client object
    try{
            sel=new  Selector(agentDisplay.getClass(),"makeProbeAtX$Y",true);
            worldRaster.setButton$Client$Message(3, agentDisplay, sel);//int:ButtonRight
        }catch(Exception e){
        System.err.println("Exception makeProbeAtX$Y:"+e.getMessage());  }
    if(drawPopulationGraph==1)
      {
         //and creat a graph of population in  the world
         populationGraph = new EZGraphImpl(getZone(),
                                          "Population over time",
                                             "time",
                                           "population",
                                           "populationGraph");
      //One data sequence in the graph-total population
      try {
         populationGraph.enableDestroyNotification$notificationMethod
           (this, new Selector (getClass (),
                                "_populationGraphDeath_",
                                false));
       } catch (Exception e) {
         System.err.println ("Exception _populationGraphDeath_: "

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -