#import <simtools.h>
#import <analysis.h>
#import <defobj/Create.h>
#import <objectbase.h>
#import <simtoolsgui/GUISwarm.h>
#import <objectbase/Swarm.h>
#import <random.h>


@interface Bug: Swarm
{
  int RI;
  id schedule;
}

- setRI: (int) x;
- buildActions;
- activateIn: swarmContext;

@end

@implementation Bug

-setRI: (int)x
{
  RI= x;
  printf("RI is %d \n",x);
  return self;
}

- buildActions
{
  [super buildActions];

  schedule= [Schedule createBegin: self];
  [schedule setRepeatInterval: RI];
  schedule = [schedule createEnd];

  [schedule at: 0 createActionTo: self message: M(step)];
  
  return self;
}

- activateIn: swarmContext
{
  printf("bug schedule activated \n");
   [super activateIn: swarmContext];
   [schedule activateIn: self ];
   return [self getActivity]; 
}


@end

@interface BugA: Bug
{

}
-step;

@end

@implementation BugA

-step
{
  printf("I'm bugA, and the current time is %d \n",getCurrentTime());
  return self;
}


@interface BugB: Bug
{
}
-step;

@end

@implementation BugB

-step
{
  printf("I'm bugB, and the current time is %d \n",getCurrentTime());
  return self;
}


@interface ModelSwarm: Swarm
{
  id <Schedule> schedule;
  id  bugA, bugB;
}
- announcement;
- buildObjects;
- buildActions;
- activateIn: swarmContext;

@end

@implementation ModelSwarm

-announcement
{
  printf("I'm model swarm, current time is %d \n",getCurrentTime());
  return self;
}
-buildObjects
{
 
  bugA=[BugA create: self];
  bugB=[BugB create: self];

  [bugA buildObjects];
  [bugB buildObjects];

  [bugA setRI:2];
  [bugB setRI:5];
  printf("set bug RI \n");
  return self;
}


- buildActions
{
  [super buildActions];
  [bugA buildActions];
 
  [bugB buildActions];

  schedule = [Schedule createBegin: self];
  [schedule setRepeatInterval: 1];
  schedule = [schedule createEnd];
  
  [schedule at: 0 createActionTo: self message:M(announcement)];
  
  printf("stop Sch \n");

  return self;
}

- activateIn: swarmContext
{
  xprint(self);
  [super activateIn: swarmContext];
  [bugA activateIn: self];
  [bugB activateIn: self];
  [schedule activateIn: self];
  return [self getActivity];
}  
@end




@interface ObserverSwarm: GUISwarm
{
  id modelSwarm;
}
+createBegin: aZone;
-buildObjects;
-buildActions;
-activateIn: aZone;
@end

@implementation ObserverSwarm

+createbegin: aZone
{
  return [super createBegin: aZone];  
}

-buildObjects
{
  [super buildObjects]; 
   modelSwarm=[ModelSwarm create: self];
  [controlPanel setStateStopped];
  [modelSwarm buildObjects];
  return self;
}  
-buildActions
{
  [super buildActions];
  [modelSwarm buildActions];
  return self;
}

-activateIn: aZone
{
  [super activateIn: aZone];
  [modelSwarm activateIn: self];
    return [self getActivity];
}


int
main (int argc, const char **argv)
{
  id swarm;

  initSwarm (argc, argv);

  swarm = [ObserverSwarm create: globalZone];

  [swarm buildObjects];
  [swarm buildActions];
  [swarm activateIn: nil];
  [swarm go];
  
  return 0;
}

/* 
Local Variables: 
compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -D_GNU_SOURCE -o pjrepeater  -g -Wno-import \
-I$SWARMHOME/include -I$SWARMHOME/include/swarm -L$SWARMHOME/lib \
-L$SWARMHOME/lib/swarm pjrepeater.m -lswarm -lobjc " 
End: 
*/






