// ============================================================ // simulate.pp : Propagation patterns for bus route simulation. // Written by Ali Ozmez for COM3360 project. // Last update on Tue Nov 28 21:51:23 1995. // ============================================================ // Following PP does a unit time long (1 min) simulation. *operation* void simulate() *traverse* *from* BusRoute *to* Bus *carry* *in* BusRoute *busRoute = (@ this @) *along* *from* BusRoute *to* Bus *wrapper* Bus (@ StopId *stopId = this->get_currentStop(); if (stopId != NULL) { // waiting at a stop this->drop_passengers(stopId); this->load_passengers( busRoute->find_stop(stopId) ); this->set_currentStop(NULL); // prepare to move } else { // moving along the route int prevPos = this->get_loc(); int nextPos = (prevPos + this->get_spd()) % busRoute->get_len(); BusStop *stop = busRoute->any_stop_around(prevPos, nextPos); if (stop == NULL) this->proceed_to(nextPos); else if (this->have_stop_request( stop->get_id() )) { this->proceed_to( stop->get_loc() ); this->set_currentStop( stop->get_id() ); // stop here } else { int have_room = this->get_cap() - this->count_passengers(); if ( have_room && stop->anybody_waiting() ) { this->proceed_to( stop->get_loc() ); this->set_currentStop( stop->get_id() ); // stop here } else this->proceed_to(nextPos); } } @) // PP to drop the passengers who want to get off at the current stop. *operation* void drop_passengers(StopId *iStopId) *traverse* *from* Bus *to* Person *carry* *out* Person_List *newPasList = (@ new Person_List() @) *along* *from* Bus *to* Person *wrapper* Person (@ if (!iStopId->g_equal( this->get_destination() )) newPasList->append(this); @) *wrapper* Bus *suffix* (@ this->set_passengers(newPasList); @) // PP to load some (or all) of the people waiting at the stop. *operation* void load_passengers(BusStop *iStop) *traverse* *from* Bus *to* Person_List *carry* *in* Person_List *newPasList *along* *from* Bus *to* Person_List *wrapper* Bus (@ int allowance = this->get_cap() - this->count_passengers(); newPasList = iStop->give_passengers(allowance); @) *wrapper* Person_List (@ this->concatenate(newPasList); @) // PP to find the stop from its id. *operation* BusStop *find_stop(StopId *iStopId) *init* (@ NULL @) *traverse* *from* BusRoute *to* BusStop *wrapper* BusStop (@ if (iStopId->g_equal( this->get_id() )) return_val = this; @) *wrapper* ~> BusStop_List, BusStop (@ if (return_val) return; @) // PP to count the passengers travelling on a bus. *operation* int count_passengers() *traverse* *from* Bus *to* Person_List *wrapper* Person_List (@ return_val = this->list_length(); @) // PP to transfer people from a stop (to a bus). *operation* Person_List *give_passengers(int allowance) *init* (@ new Person_List() @) *traverse* *from* BusStop *to* Person *carry* *out* Person_List *newWaitList = (@ new Person_List() @) *along* *from* BusStop *to* Person *wrapper* Person (@ if (return_val->list_length() < allowance) return_val->append(this); else newWaitList->append(this); @) *wrapper* BusStop *suffix* (@ this->set_waitingList(newWaitList); @) // PP to search for a stop in the given interval of route. *operation* BusStop *any_stop_around(int prevPos, int nextPos) *init* (@ NULL @) *traverse* *from* BusRoute *to* BusStop *wrapper* BusStop (@ int stopLoc = this->get_loc(); if (prevPos < nextPos) { if ((stopLoc > prevPos) && (stopLoc <= nextPos)) return_val = this; } else if ((stopLoc > prevPos) || (stopLoc <= nextPos)) return_val = this; @) *wrapper* ~> BusStop_List, BusStop (@ if (return_val) return; @) // PP to move the bus to its new position. *operation* void proceed_to(int newPos) *traverse* *from* Bus *to* RouteLoc *wrapper* RouteLoc (@ this->get_v()->set_val(newPos); @) // PP to see if there is any passenger who wants to get off at // the specified stop. *operation* int have_stop_request(StopId *iStopId) *init* (@ 0 @) *traverse* *from* Bus *to* Person *wrapper* Person (@ if (iStopId->g_equal( this->get_destination() )) return_val = 1; @) *wrapper* ~> Person_List, Person (@ if (return_val) return; @) // PP to see if anybody waiting at the specified stop. *operation* int anybody_waiting() *init* (@ 0 @) *traverse* *from* BusStop *to* Person_List *wrapper* Person_List (@ return_val = this->empty(); @) // PP to get the get the length of a BusRoute object as an int. *operation* int get_len() *traverse* *from* BusRoute *to* RouteLen *wrapper* RouteLen (@ return_val = this->get_v()->get_val(); @) // PP to get the get the position of a Bus or BusStop object as an int. *operation* int get_loc() *traverse* *from* {Bus, BusStop} *to* RouteLoc *wrapper* RouteLoc (@ return_val = this->get_v()->get_val(); @) // PP to get the get the capacity of a Bus object as an int. *operation* int get_cap() *traverse* *from* Bus *to* BusCapac *wrapper* BusCapac (@ return_val = this->get_v()->get_val(); @) // PP to get the get the speed of a Bus object as an int. *operation* int get_spd() *traverse* *from* Bus *to* BusSpeed *wrapper* BusSpeed (@ return_val = this->get_v()->get_val(); @)