package example.myswarm; import swarm.Globals; import swarm.NonUniqueMethodSignatureException; import swarm.Selector; import swarm.SignatureNotFoundException; import swarm.activity.Activity; import swarm.activity.Schedule; import swarm.activity.ScheduleImpl; import swarm.analysis.EZGraph; import swarm.analysis.EZGraphImpl; import swarm.analysis.EZSequence; import swarm.collections.List; import swarm.collections.ListImpl; import swarm.collections.ListIndex; import swarm.defobj.Zone; import swarm.objectbase.Swarm; import swarm.simtoolsgui.GUISwarmImpl; /** * 智能体类 * @author Administrator * */ class Agent{ double value; Agent(double value){ this.value = value; } void newValue(){ value=Globals.env.uniformDblRand.getDoubleWithMin$withMax(.1, 1); System.out.println("Standlone new value="+value); } /** * 主要返回智能体的值 * @return */ public double getAgentValue(){ return value; } } /** * * @author Administrator * */ public class EZGraphDemo extends GUISwarmImpl{ EZSequence sequence ,averagingSequence; List list; Schedule schedule; EZGraph ezgraph; Agent standloneAgent; EZGraphDemo(Zone aZone){ super(aZone); list = new ListImpl(aZone); } /** * 智能体链表的创建 */ void addValueToList(){ double v = Globals.env.uniformDblRand.getDoubleWithMin$withMax(.1, 0.8); list.addLast(new Agent(v)); } public Object step(){ standloneAgent.newValue(); addValueToList(); ezgraph.step(); getActionCache().doTkEvents(); return this; } public Object buildObjects(){ super.buildObjects(); addValueToList(); standloneAgent = new Agent(1.0); ezgraph = new EZGraphImpl(getZone(),"setUnsignedArgDemo","t","prob.","ezgraph"); try { Selector sel = new Selector(Agent.class,"getAgentValue",false); sequence =ezgraph.createSequence$withFeedFrom$andSelector("standlone", standloneAgent, sel); averagingSequence = ezgraph.createAverageSequence$withFeedFrom$andSelector("av(list)", list, sel); } catch (NonUniqueMethodSignatureException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SignatureNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } 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 (NonUniqueMethodSignatureException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SignatureNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return this; } public void pri(){ ListIndex index = list.listBegin(Globals.env.globalZone); while(index.next()!= null){ System.out.println(((Agent)index.get()).getAgentValue()); } } public Activity activateIn(Swarm swarmcontext){ super.activateIn(swarmcontext); schedule.activateIn(this); return getActivity(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Globals.env.initSwarm("TestEZGraph", "0.0", "a@a", args); EZGraphDemo ezgraphTest = new EZGraphDemo(Globals.env.globalZone); ezgraphTest.buildObjects(); ezgraphTest.buildActions(); ezgraphTest.activateIn(null); ezgraphTest.go(); ezgraphTest.pri(); } }