📄 observerswarm.java.bak
字号:
package sugarscape;
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.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.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 EZBinImpl wealthHistogram;//histogram of wealth
public ScheduleImpl displaySchedule;//schedule objects
public ActionGroupImpl displayActions;
// A flag to signal the end of the simulation.
public boolean simulationFinished = false;
public ObserverSwarm(Zone azone)
{
// Use the parent class to create an observer swarm.
super(azone);
displayFrequency=1;
drawWealthHistogram=1;
drawPopulationGraph=1;
//parameter file
//parameterFile=(char)0;
EmptyProbeMapImpl _probeMap = new EmptyProbeMapImpl(getZone(), getClass ());
//_probeMap.addProbe(getProbeForVariable("parameterFile"));
_probeMap.addProbe(getProbeForVariable("drawPopulationGraph"));
//_probeMap.addProbe(getProbeForVariable("drawWealthHistogram"));
_probeMap.addProbe(getProbeForVariable("displayFrequency"));
Globals.env.probeLibrary.setProbeMap$For
(_probeMap,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
getControlPanel().setStateStopped();
//Creat we've read the parameters from the user,we can override them
/* if (parameterFile)
{
String name=new String[parameterFile.length() +7];
arguments.setDefaultAppDataPath("parameters");
System.out.println(name,"%s.model",parameterFile);
ObjectLoader.load$fromAppDataFileNamed(this,name);
}*/
//now build the modelswarm
_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.
ListImpl AgentList;
AgentList =(ListImpl)_modelSwarm.getAgentList();
for(int i=0;i<AgentList.getCount();i++)
{
SugarAgent Agent=(SugarAgent)AgentList.atOffset(i);
Agent.setAgentColor((byte)2);
}
maxSugarValue =_sugarspace.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 ),(double)0);
//{
// colormap.setColor$ToName((byte)0, "blue");
// colormap.setColor$ToName((byte)(i/(maxSugarValue-1.0)), "red");
// colormap.setColor$ToName((byte)i/(maxSugarValue-1.0), "green");
// colormap.setColor$ToName((byte)3, "yellow");
//}
colormap.setColor$ToName((byte)100, "red");//agent color
// 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("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.getSugarValues());
sugarDisplay.setDisplayMappingM$C(1,0);//map to colormap
//And an object to display the agents
try{
sel =new Selector(getClass(),"Sugarscape",false);
agentDisplay = new Object2dDisplayImpl(getZone(), worldRaster,
_sugarspace.getAgentGrid(), sel);
//use Object2dDisplay
sel=new Selector(Class.forName("sugarscape"),"drawSelfOn",false);
agentDisplay =new Object2dDisplayImpl(getZone(),worldRaster,
_sugarspace.getAgentGrid(),sel);
}catch(Exception e){
System.err.println("sugarscape.drawSelfOn"+e.getMessage());}
//agentDisplay.setDisplayWidget(worldRaster);
//agentDisplay.setDiscrete2dToDisplay(_sugarSpace.getAgentGrid());
agentDisplay.setObjectCollection(_modelSwarm.getAgentList());
// agentDisplay.setDisplayMessage(drawSelfOn);//draw method
// enable probes on the world
//Configure at mouse button to send a message to a given client object
try{
sel=new Selector(getClass(),"makeProbeAtX:Y:",true);
worldRaster.setButton$Client$Message(3,agentDisplay,sel);//int:ButtonRight
}catch(Exception e){
System.err.println("makeProbeAtX:Y"+"return"+e.getMessage()); }
if(drawPopulationGraph==1)
{
//and creat a graph of population in the world
populationGraph = new EZGraphImpl(getZone(),
"populationGraph",
"Population over time",
"time",
"population");
//One data sequence in the graph-total population
try{
sel=new Selector(getClass(),"getCount",false);
populationGraph.createSequence$withFeedFrom$andSelector
("population",AgentList,sel);
}catch(Exception e){System.err.println("population"+e.getMessage()); }
}
//Create a graph for various agent attributes
attributeGraph = new EZGraphImpl(getZone(),
"attributes",
"Agent attributes over time",
"time",
"attribute" );
//Two data sequence here.Average vision for all the bugs
try{
sel=new Selector(getClass(),"getVision",false);
attributeGraph.createAverageSequence$withFeedFrom$andSelector
("vision",AgentList,sel);
//And average metabolism
sel=new Selector(getClass(),"getMetabolism",false);
attributeGraph.createAverageSequence$withFeedFrom$andSelector
("metabolism",AgentList,sel);
}catch(Exception e){System.err.println("Exception"+e.getMessage());}
//-----------------------------
/* if(drawWealthHistogram==1)
{
//create a histogram of agent wealth distribution
wealthHistogram = new EZBinImpl();//"wealthHistogram");
// "Agent wealth distribution",
// "wealth","number of agents",
// 9,0,300,
// agentList,getCurrentSugar()
// );
wealthHistogram.setAxisLabelsX$Y("wealth", "number of agents");
wealthHistogram.setBinCount(9);
wealthHistogram.setLowerBound(0);
wealthHistogram.setUpperBound(300);
wealthHistogram.setCollection(AgentList);
try{
sel=new Selector(this.getClass(),"getcurrentsugar",false);
wealthHistogram.setProbedSelector(sel);
// sel=new Selector(this.getClass(),"_wealthHistogramDeath_", false);
// wealthHistogram.enableDestroyNotification$notificationMethod(this, sel);
}catch(Exception e)
{ System.err.println("Exception"+e.getMessage()); }
}*/
//-----------------------------------------------
return this;
}
public Object _wealthHistogramDeath_(Object caller)
{
wealthHistogram.drop();
wealthHistogram = null;
return this;
}
public Object _worldRasterDeath_(Object caller)
{
worldRaster.drop();
worldRaster = null;
return this;
}
public Object _updateHistogram_()
{
if(wealthHistogram!=null)
{
wealthHistogram.reset();
wealthHistogram.update();
wealthHistogram.output();
}
return this;
}
public Object _updateDisplay_()
{
if(worldRaster!=null)
{
sugarDisplay.display();
agentDisplay.display();
worldRaster.drawSelf();
}
return this;
}
//the display schedule
public Object buildActions()
{
super.buildActions();
_modelSwarm.buildActions();
Selector sel;
displayActions= new ActionGroupImpl(getZone());
try{
sel= new Selector(this.getClass(), "updateDisplay",false);
displayActions.createActionTo$message(this, sel);
sel = new Selector(attributeGraph.getClass(), "step", true);
displayActions.createActionTo$message(attributeGraph, sel);
if (drawPopulationGraph == 1) {
sel = new Selector(populationGraph.getClass(), "step", true);
displayActions.createActionTo$message(populationGraph, sel);}
//--------------------------------------
/* if (drawWealthHistogram == 1) {
sel = new Selector(this.getClass(), "_updateHistogram_", true);
displayActions.createActionTo$message(this, sel);}*/
//---------------------------------------
sel = new Selector(Globals.env.probeDisplayManager.getClass(), "update", true);
displayActions.createActionTo$message(Globals.env.probeDisplayManager, sel);
// sel = new Selector(this.getClass(), "updateSimulatedTime",true);
// displayActions.createActionTo$message(this, sel);
sel = new Selector(worldRaster.getClass(), "drawSelf ",true);
displayActions.createActionTo$message(worldRaster, sel);
sel = new Selector(getActionCache().getClass(), "doTkEvents", true);
displayActions.createActionTo$message(getActionCache(), sel);
sel = new Selector(this.getClass(), "checkForDone",true);
displayActions.createActionTo$message(this, sel);
/* sel = new Selector(this.getClass(), "writeFrame", true);
displayActions.createActionTo$message(this, sel);*/
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage ());}
displaySchedule = new ScheduleImpl(getZone(),displayFrequency);
displaySchedule.at$createAction(0, displayActions);
displaySchedule.setRepeatInterval(displayFrequency);
return this;
}
//scheduling details :the modelswarm is activated here.
public Activity activateIn(Swarm swarmContext)
{
super.activateIn(swarmContext);
_modelSwarm.activateIn(this);
displaySchedule.activateIn(this);
return getActivity();
}
/*public void updateSimulatedTime()
{
simulatedTime = Globals.env.getCurrentTime();
}*/
/* //使用文件输出
public void writeFrame()
{
String filename[]=new String[256];
System.out.println(filename +"$04d.ppm"+ getCurrentTime());
worldRaster.writeSelfToFile(filename);
}*/
public void checkForDone()
{
if (simulationFinished)
{
// The simulation is over. Presumably we got here because
// the user did not QUIT when told to do so after the
// modelSwarm activity was terminated. We therefore chide
// her and press QUIT ourselves.
System.out.println("I said to QUIT!");
_modelSwarm.getActivity().terminate();
_modelSwarm.drop();
getControlPanel().setStateQuit();
}
else if (_modelSwarm.getActivity().getStatus() .equals(
Globals.env.Completed))
{
// modelSwarm has signaled us that the simulation is
// finished by terminating itself. (ObserverSwarm sees
// this as Completed".) Press the STOP button on the
// control panel. Pressing STOP rather than QUIT leaves
// the raster window and the control panel on the display
// so that the user can look at the results of the
// simulation. (Those windows will disappear when the
// user presses QUIT.) We also set a flag to indicate
// that the simulation is over, in case the user presses
// START or NEXT instead of QUIT.
simulationFinished = true;
System.out.println("The simulation ended after "
+ Globals.env.getCurrentTime()
+ " periods.");
System.out.println("Press QUIT when ready.");
getControlPanel().setStateStopped();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -