// Paul Johnson March 2, 2000 //Simple Schedule usage with ActionGroup #import #import #import #import #import #import @interface Agent: CreateDrop { int ident; double value[2]; } - setAgentValue: (double*)value; - updateValue; - printValue; - (double)getValue1; - setID: (int)x; @end @implementation Agent - setAgentValue: (double*)theValue { value[0] = theValue[0]; value[1] = theValue[1]; return self; } - updateValue { int i; for (i=0; i<2;i++) { value[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10]; } return self; } -(double)getValue1 { return value[0]; } -getValueFrom: anAgent { double y; y=[anAgent getValue1]; value[0]=y+4; return self; } -setID: (int)x { ident=x; return self; } - printValue { int i; printf("Agent %d value is\n", ident); for(i=0; i<2; i++) printf("%f ", value[i]); printf("\n"); return self; } @end @interface ModelSwarm: Swarm { id schedule; id list; id agent, agent2; } - createEnd; - updateAgent: ag; - buildObjects; - buildActions; - step; @end @implementation ModelSwarm #define AGENT(val) [[[Agent createBegin: globalZone] setAgentValue: val] createEnd] - createEnd { [super createEnd]; return self; } -buildObjects { int i; double val[2]; [super buildObjects]; for(i=0;i<2;i++) val[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10]; agent= AGENT ( val ); [agent setID: 1]; for(i=0;i<2;i++) val[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10]; agent2= AGENT ( val ); [agent2 setID: 2]; return self; } - updateAgent:ag { int i; double val[2]; printf("Update agent to: \n"); for(i=0;i<2;i++) val[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10]; //[agent updateValue]; [ag setAgentValue: val]; [ag printValue]; return self; } - buildActions { id actions; [super buildActions]; actions = [ActionGroup create: self]; [actions createActionTo: agent message: M(printValue)]; [actions createActionTo: agent2 message: M(printValue)]; [actions createActionTo: self message: M(updateAgent:):agent]; [actions createActionTo: agent message: M(getValueFrom:):agent2]; [actions createActionTo: agent message: M(printValue)]; [actions createActionTo: agent2 message: M(printValue)]; [actions createActionTo: agent2 message: M(getValueFrom:):agent]; [actions createActionTo: agent message: M(printValue)]; [actions createActionTo: agent2 message: M(printValue)]; schedule = [[[Schedule createBegin: self] setRepeatInterval: 1] createEnd]; [schedule at: 0 createAction: actions]; ; return self; } - step { return self; } - activateIn: swarmContext { [super activateIn: swarmContext]; [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 testapp -g -Wno-import \ -I$SWARMHOME/include -I$SWARMHOME/include/swarm -L$SWARMHOME/lib \ -L$SWARMHOME/lib/swarm PJActivityProblem.m -lswarm -lobjc " End: */