/* MGD April 22, 2001 KR> At each timestep I want each of the agents to perform their step method [..] KR> there doesn't seem to be a way to create an ActionGroup KR> acting on more than one class (because of the way the selector is KR> created). Here's one way to do it: set up a protocol for all agents and have each of them implement it. This way, you don't have to have them all inherit from the same base class (also a solution), and you don't have to rely on dynamic dispatch. [You _can_ make a Selector using any class that happens to have the method, and then rely on dynamic message dispatch for the others. If you do that, and the method doesn't exist in one of the other agents, then Swarm will send the message of the form `doesNotRecognize (Selector)' to the agent(s) in question, and if that method is not implemented Swarm will simply abort.] (/ import swarm.Globals; import swarm.objectbase.Swarm; import swarm.objectbase.SwarmImpl; import swarm.defobj.Zone; import swarm.activity.Schedule; import swarm.activity.ScheduleImpl; import swarm.activity.Activity; import swarm.Selector; import java.util.LinkedList; import java.util.List; interface Agent { void stepAgent (); } class Agent1 implements Agent { public void stepAgent () { System.out.println ("stepping agent 1"); } } class Agent2 implements Agent { public void stepAgent () { System.out.println ("stepping agent 2"); } } public class DualStep extends SwarmImpl { DualStep (Zone aZone) { super (aZone); } List agentList; Schedule stepSchedule; public Object buildObjects () { super.buildObjects (); agentList = new LinkedList (); agentList.add (new Agent1 ()); agentList.add (new Agent2 ()); agentList.add (new Agent2 ()); agentList.add (new Agent1 ()); return this; } public Object buildActions () { super.buildActions (); stepSchedule = new ScheduleImpl (getZone (), false); try { Selector sel = new Selector (Agent.class, "stepAgent", false); stepSchedule.at$createActionForEach$message (0, agentList, sel); } catch (Exception e) { e.printStackTrace (System.err); System.exit (1); } return this; } public Activity activateIn (Swarm swarmContext) { super.activateIn (swarmContext); stepSchedule.activateIn (this); return getActivity (); } static void main (String args[]) { Globals.env.initSwarm ("DualStep", "0.0", "bug-swarm@swarm.org", args); DualStep dualStepModel = new DualStep (Globals.env.globalZone); dualStepModel.buildObjects (); dualStepModel.buildActions (); dualStepModel.activateIn (null).run (); } }