/* //Marcus Daniels August 1, 2001 swarm-support OJ> Because when an Action is executed, I have to remove it from the OJ> list and I do not know how I can do that. When an Action is OJ> executed, is there a way to know is Id or something that can OJ> describe it ? If there is I can remove it easily I think. It's tricky business trying to use an Index to track running Activities. I think you'll end-up with logic that just tracks what Swarm is doing. The code will be hard to get right. Here's an example of removing Actions at runtime. To extend it to an arbitrary pending set of Actions, one approach would be to have each agent have a List of Actions, and then iterate over it (when dropping the agent) to remove the actions from the Schedule per the remove method as shown below. The Schedule would either need to be created with auto drop disabled ("false" for the second argument to the ScheduleImpl constructor), or the iteration wouldn't start removing until it got past Globals.env.getCurrentTime. It be easiest and most clear to have the Agents have a method that did the scheduling (on the common Schedule). The reason for putting agent-specific scheduling in the agents themselves is to make model building a more modular process and to avoid these issues.. */ import swarm.activity.ScheduleImpl; import swarm.activity.Schedule; import swarm.activity.Activity; import swarm.activity.ActionTo; import swarm.objectbase.SwarmImpl; import swarm.objectbase.Swarm; import swarm.defobj.Zone; import swarm.Selector; import swarm.Globals; abstract class Agent { public void step () { System.out.println ("step " + getClass ().getName () + " @ " + Globals.env.getCurrentTime ()); } } class Agent1 extends Agent { } class Agent2 extends Agent { } public class RemovalDemo extends SwarmImpl { RemovalDemo (Zone aZone) { super (aZone); } Schedule schedule; Agent1 agent1; Agent2 agent2; ActionTo action1; ActionTo action2; public void clean () { schedule.remove (action1); schedule.remove (action2); } public Object buildObjects () { super.buildObjects (); agent1 = new Agent1 (); agent2 = new Agent2 (); return this; } public Object buildActions () { super.buildActions (); schedule = new ScheduleImpl (getZone (), true); try { Selector stepSel = new Selector (Agent.class, "step", false); Selector cleanSel = new Selector (getClass (), "clean", false); schedule.at$createActionTo$message (0, agent1, stepSel); schedule.at$createActionTo$message (10, agent2, stepSel); schedule.at$createActionTo$message (10, this, cleanSel); action1 = schedule.at$createActionTo$message (100, agent1, stepSel); action2 = schedule.at$createActionTo$message (110, agent2, stepSel); schedule.at$createActionTo$message (200, agent1, stepSel); schedule.at$createActionTo$message (210, agent2, stepSel); } 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 (); } static void main (String args[]) { Globals.env.initSwarm ("RemovalDemo", "0.0", "bug-swarm@swarm.org", args); RemovalDemo demo = new RemovalDemo (Globals.env.globalZone); demo.buildObjects (); demo.buildActions (); demo.activateIn (null).run (); } }