//program.beh //Name : Cengiz GOK //Account name: cgok //Original program written by Binoy Samuel //rewritten in adaptive way for demjava by Cengiz Gok : 12/04/97 Main{ (@ static public void main(String args[]) throws Exception { BusRoute iBusRoute = BusRoute.parse(System.in); int i, stime; //Simulation time. System simulates this amount of time (stime=25 min) stime = 20; // Display the initial state of the bus route System.out.println("Initial state of the input bus route : \n\n"); iBusRoute.g_print(); // Do the simulation here for (i=1; i < stime; i++) { System.out.println("\n\nNext state (after " + i + " minutes) : \n\n"); iBusRoute.simulate(); iBusRoute.g_print(); } // Display the final state of the bus route System.out.print("\n\nFinal state of the input bus route (after "); System.out.println( stime + " minutes) : \n\n"); iBusRoute.simulate(); iBusRoute.g_print(); System.out.println("\n\n*** FINISHED ***" ); } @) } //--------------------------------------------------------------------- BusRoute { //Simulate () function does 1 min. simulation each time. void simulate() to Bus { (@ BusRoute busRoute; @) before BusRoute (@ busRoute = host; @) before Bus (@ StopId stopId = host.get_currentStop(); if (stopId != null) { // waiting at a stop host.drop_passengers(stopId); host.load_passengers( busRoute.find_stop(stopId) ); host.set_currentStop(null); // prepare to move } else { // moving along the route int prevPos = host.get_loc(); int nextPos = (prevPos + host.get_spd()) % busRoute.get_len(); BusStop stop = busRoute.any_stop_around(prevPos, nextPos); if (stop == null) host.proceed_to(nextPos); else if (host.have_stop_request( stop.get_id() )) { host.proceed_to( stop.get_loc() ); host.set_currentStop( stop.get_id() ); // stop here } else { int have_room = host.get_cap() - host.count_passengers(); if ( (have_room > 0) && stop.anybody_waiting() ) { System.out.println("proceeding to stop at " + stop.get_loc()); host.proceed_to( stop.get_loc() ); host.set_currentStop( stop.get_id() ); // stop here } else host.proceed_to(nextPos); } } @) } //------------------------------------------------------------------- // function to find the stop from given StopId. BusStop find_stop(StopId iStopId) to BusStop { init (@ @) before BusStop (@ if (iStopId.g_equal( host.get_id() )) return_val = host; @) } //---------------------------------------------------------------- // function to search for a stop in the given route positions. BusStop any_stop_around(int prevPos, int nextPos) to BusStop { before BusStop (@ int stopLoc = host.get_loc(); if (prevPos < nextPos) { if ((stopLoc > prevPos) && (stopLoc <= nextPos)) return_val = host; } else if ((stopLoc > prevPos) || (stopLoc <= nextPos)) return_val = host; @) } //---------------------------------------------- // function to get the length of BusRoute. int get_len() to RouteLen { before RouteLen (@ return_val = host.get_v().intValue(); @) } //------------------------------------------------------------------ //function to print the information about simulation results. void g_print() to {*} { before -> BusRoute,name,RouteName (@ System.out.print("BusRoute: "); @) before -> BusRoute,totalLength,RouteLen (@ System.out.print("total route length: "); @) before -> BusRoute,busStops,BusStop_List (@ System.out.print("\nconsisting of bus stops: \n"); @) before -> BusRoute,buses,Bus_List (@ System.out.print("\nwith assigned busses: "); @) before -> BusStop,location,RouteLoc (@ System.out.print("at: "); @) before -> BusStop,waitingList,Person_List (@ System.out.print("\nwith waiting list: "); @) before -> Bus,position,RouteLoc (@ System.out.print("at: "); @) before -> Bus,currentStop,StopId (@ System.out.print("currently at stop: "); @) before -> Bus,drivername,DriverName (@ System.out.print("Dirver: ");@) before -> Bus,capacity,BusCapac (@ System.out.print("capacity: "); @) before -> Bus,speed,BusSpeed (@ System.out.print("speed: "); @) before -> Bus,passengers,Person_List (@ System.out.print("carrying passenger(s): "); @) before -> Person,destination,StopId (@ System.out.print("destination: "); @) before BusStop_List (@ System.out.print("("); @) after BusStop_List (@ System.out.print(")"); @) before Bus_List (@ System.out.print("("); @) after Bus_List (@ System.out.print(")"); @) before Person_List (@ System.out.print("("); @) after Person_List (@ System.out.print(")\n"); @) before RouteName (@ System.out.print(" " + host.get_v() + " "); @) before RouteLen (@ System.out.print(" " + host.get_v() + " "); @) after RouteLen (@ System.out.print(" ft "); @) before RouteLoc (@ System.out.print(" " + host.get_v() + " "); @) after RouteLoc (@ System.out.print(" ft "); @) before BusCapac (@ System.out.print(" " + host.get_v() + " "); @) after BusCapac (@ System.out.print(" passengers "); @) before BusSpeed (@ System.out.print(" " + host.get_v() + " "); @) after BusSpeed (@ System.out.print(" ft/min "); @) before DriverName (@ System.out.print(" "+ host.get_v() + " "); @) before StopId (@ System.out.print(" " + host.get_v() + " "); @) before BusId (@ System.out.print(" " + host.get_v() + " "); @) before PersonId (@ System.out.print(" " + host.get_v() + " "); @) } } //---------------------------------------------------------------- // Bus Bus { //function to drop the passenger who wants to get off at this BusStop void drop_passengers(StopId iStopId) to Person { (@ Person_List newPasList = new Person_List(); @) before Person (@ if (!iStopId.g_equal( host.get_destination() )) newPasList.append(host); @) after Bus (@ host.set_passengers(newPasList); @) } //---------------------------------------------------------------- // function to load the waiting people at the stop void load_passengers(BusStop iStop) to Person_List { (@ Person_List newPasList = new Person_List(); @) before Bus (@ int allowance = host.get_cap() - host.count_passengers(); newPasList = iStop.give_passengers(allowance); @) before Person_List (@ host.concatenate(newPasList); @) } //---------------------------------------------------------- //function to count the passengers on the bus. int count_passengers() to Person_List { before Person_List (@ return_val = host.list_length(); @) } //--------------------------------------------------------- //function to move the bus to its next position. void proceed_to(int newPos) to RouteLoc { before RouteLoc (@ host.set_v(new Integer(newPos)); @) } //--------------------------------------------------------------------- // function to find out if there is any passenger who wants to get off boolean have_stop_request(StopId iStopId) to Person { (@ Integer ret = new Integer(0); @) before Person (@ if (iStopId.g_equal( host.get_destination() )) ret = new Integer(1); @) after Bus (@ return_val=(ret.intValue()>0); @) } //-------------------------------------------------------------------- //function to get the location of Bus int get_loc() to RouteLoc { before RouteLoc (@ return_val = host.get_v().intValue(); @) } //--------------------------------------------------------------------- // function to get the capacity of Bus int get_cap() to BusCapac { before BusCapac (@ return_val = host.get_v().intValue(); @) } //--------------------------------------------------------------------- // function to get the speed of Bus int get_spd () to BusSpeed { before BusSpeed (@ return_val = host.get_v().intValue(); @) } } //--------------------------------------------------- BusStop { // function to take the pasenger from BusStop and give them to Bus // as many as allowance.If the # of passengers is more than allowance, add // those passengers new waiting list. Person_List give_passengers(int allowance) to Person { (@ Person_List newWaitList = new Person_List(); @) before BusStop (@ return_val=new Person_List();@) before Person (@ if (return_val.list_length() < allowance) return_val.append(host); else newWaitList.append(host); @) after BusStop (@ host.set_waitingList(newWaitList); @) } //---------------------------------------------- //function to find if there is anybody waiting at the stop boolean anybody_waiting() to Person_List { (@ Boolean ret = new Boolean(false); @) before Person_List (@ ret = new Boolean(! host.empty()); ret.booleanValue(); @) } //=--------------------------------------------------- // function to get the location of BusStop int get_loc() to RouteLoc { before RouteLoc (@ return_val = host.get_v().intValue(); @) } } //--------------------------------------------------------------------- Person_List { // function to append the given person to Person_List void append (Person p) to Person { before Person_List (@ Nonempty_Person_List newlist = new Nonempty_Person_List( p , host.get_first()); host.set_first(newlist); @) } //-------------------------------------------------------------------- // function to concatenate the given Person Lists void concatenate(Person_List pl) to Person { before Person_List (@ if(host.get_first() == null) host.set_first( pl.get_first()); @) } //----------------------------------------------------------------- //function to count # of person in the List, and returns length of List int list_length() to Person { (@ int i=0 ; @) before Person (@ i++; @) after Person_List (@ return_val =i; @) } //--------------------------------------------------------------- // function to check the Person_List for emptiness boolean empty() to Person_List { before Person_List (@ return_val = (!(host.list_length() > 0)); @) } } //--------------------------------------------------------------------- StopId { (@ boolean g_equal(StopId it) { return this.get_v().equals(it.get_v()); } @) } //----------------------------------------------------------------------