001 import swarm.*;
002 import swarm.activity.*;
003 import swarm.defobj.*;
004 import swarm.objectbase.*;
005 import swarm.space.*;
006 import swarm.collections.*;
007
008 public class ModelSwarm extends SwarmImpl{
009 /** lispAppArchiverのためにはpublicでなければならない。 */
010 public int worldXSize, worldYSize;
011 /** lispAppArchiverのためにはpublicでなければならない。 */
012 public double seedProb;
013 /** lispAppArchiverのためにはpublicでなければならない。 */
014 public double bugDensity;
015
016 FoodSpace food;
017 Grid2d world;
018 Bug reportBug;
019
020 List bugList;
021 ActionGroup modelActions;
022 Schedule modelSchedule;
023
024 public ModelSwarm(Zone aZone){
025 super(aZone);
026 worldXSize = 80;
027 worldYSize = 80;
028 seedProb = 0.8;
029 bugDensity=0.1;
030 }
031
032 public Object buildObjects(){
033 Bug aBug;
034 int x,y;
035
036 food=new FoodSpace(this,worldXSize,worldYSize);
037 food.seedFoodWithProb(seedProb);
038
039 world=new Grid2dImpl(this,worldXSize,worldYSize);
040 world.fillWithObject(null);
041
042 bugList=new ListImpl(this);
043
044 for (y = 0; y < worldYSize; y++){
045 for (x = 0; x < worldXSize; x++){
046 if (Globals.env.uniformDblRand.getDoubleWithMin$withMax(0.0,1.0) < bugDensity){
047 aBug=new Bug(this);
048 aBug.setWorld$Food(world,food);
049 aBug.setX$Y(x,y);
050 bugList.addLast(aBug);
051 }
052 }
053 }
054
055 reportBug=(Bug)bugList.removeFirst();
056 bugList.addFirst(reportBug);
057
058 return this;
059 }
060
061 public Object buildActions(){
062 modelActions=new ActionGroupImpl(this);
063 try{
064 modelActions.createActionForEach$message(bugList,
065 new Selector(Class.forName("Bug"),"step",false));
066 } catch (Exception e) {
067 System.out.println ("Exception: " + e.getMessage ());
068 System.exit(1);
069 }
070
071 try{
072 modelActions.createActionTo$message(reportBug,
073 new Selector(Class.forName("Bug"),"report",false));
074 } catch (Exception e) {
075 System.out.println ("Exception: " + e.getMessage ());
076 System.exit(1);
077 }
078
079 modelSchedule=new ScheduleImpl(this,1);
080 modelSchedule.at$createAction(0,modelActions);
081 return this;
082 }
083
084 public Activity activateIn(Swarm context){
085 super.activateIn(context);
086 modelSchedule.activateIn(this);
087 return getActivity();
088 }
089 }