409a853146bd15d5666d04a1f9f9c9f6aca7597a
[IRC.git] / Robust / src / Benchmarks / SingleTM / Vacation / Manager.java
1 /* =============================================================================
2  *
3  * manager.c
4  * -- Travel reservation resource manager
5  *
6  * =============================================================================
7  *
8  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
9  * Author: Chi Cao Minh
10  *
11  * =============================================================================
12  *
13  * For the license of bayes/sort.h and bayes/sort.c, please see the header
14  * of the files.
15  * 
16  * ------------------------------------------------------------------------
17  * 
18  * For the license of kmeans, please see kmeans/LICENSE.kmeans
19  * 
20  * ------------------------------------------------------------------------
21  * 
22  * For the license of ssca2, please see ssca2/COPYRIGHT
23  * 
24  * ------------------------------------------------------------------------
25  * 
26  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
27  * header of the files.
28  * 
29  * ------------------------------------------------------------------------
30  * 
31  * For the license of lib/rbtree.h and lib/rbtree.c, please see
32  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33  * 
34  * ------------------------------------------------------------------------
35  * 
36  * Unless otherwise noted, the following license applies to STAMP files:
37  * 
38  * Copyright (c) 2007, Stanford University
39  * All rights reserved.
40  * 
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions are
43  * met:
44  * 
45  *     * Redistributions of source code must retain the above copyright
46  *       notice, this list of conditions and the following disclaimer.
47  * 
48  *     * Redistributions in binary form must reproduce the above copyright
49  *       notice, this list of conditions and the following disclaimer in
50  *       the documentation and/or other materials provided with the
51  *       distribution.
52  * 
53  *     * Neither the name of Stanford University nor the names of its
54  *       contributors may be used to endorse or promote products derived
55  *       from this software without specific prior written permission.
56  * 
57  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
67  * THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  * =============================================================================
70  */
71
72 /* =============================================================================
73  * DECLARATION OF TM_CALLABLE FUNCTIONS
74  * =============================================================================
75  */
76 public class Manager {
77   RBTree carTablePtr;
78   RBTree roomTablePtr;
79   RBTree flightTablePtr;
80   RBTree customerTablePtr;
81
82   /* =============================================================================
83    * manager_alloc
84    * =============================================================================
85    */
86   public Manager() {
87     carTablePtr = new RBTree();
88     roomTablePtr = new RBTree();
89     flightTablePtr = new RBTree();
90     customerTablePtr = new RBTree();
91   }
92   
93 /* =============================================================================
94  * addReservation
95  * -- If 'num' > 0 then add, if < 0 remove
96  * -- Adding 0 seats is error if does not exist
97  * -- If 'price' < 0, do not update price
98  * -- Returns TRUE on success, else FALSE
99  * =============================================================================
100  */
101   boolean addReservation (RBTree tablePtr, int id, int num, int price) {
102     Reservation reservationPtr;
103
104     reservationPtr = (Reservation)tablePtr.find(id);
105     if (reservationPtr == null) {
106       /* Create new reservation */
107       if (num < 1 || price < 0) {
108         return false;
109       }
110       reservationPtr = new Reservation(id, num, price);
111       //      assert(reservationPtr != NULL);
112       tablePtr.insert(id, reservationPtr);
113     } else {
114       /* Update existing reservation */
115       if (!reservationPtr.reservation_addToTotal(num)) {
116         return false;
117       }
118       if (reservationPtr.numTotal == 0) {
119         boolean status = tablePtr.remove(id);
120         if (!status) {
121           System.out.println("TMRESTART7");
122           System.exit(-1);
123         }
124       } else {
125         reservationPtr.reservation_updatePrice(price);
126       }
127     }
128     
129     return true;
130   }
131   
132
133   /* =============================================================================
134    * manager_addCar
135    * -- Add cars to a city
136    * -- Adding to an existing car overwrite the price if 'price' >= 0
137    * -- Returns TRUE on success, else FALSE
138    * =============================================================================
139    */
140   boolean manager_addCar (int carId, int numCars, int price) {
141     return addReservation(carTablePtr, carId, numCars, price);
142   }
143
144   
145 /* =============================================================================
146  * manager_deleteCar
147  * -- Delete cars from a city
148  * -- Decreases available car count (those not allocated to a customer)
149  * -- Fails if would make available car count negative
150  * -- If decresed to 0, deletes entire entry
151  * -- Returns TRUE on success, else FALSE
152  * =============================================================================
153  */
154   boolean manager_deleteCar (int carId, int numCar) {
155     /* -1 keeps old price */
156     return addReservation(carTablePtr, carId, -numCar, -1);
157   }
158
159
160 /* =============================================================================
161  * manager_addRoom
162  * -- Add rooms to a city
163  * -- Adding to an existing room overwrite the price if 'price' >= 0
164  * -- Returns TRUE on success, else FALSE
165  * =============================================================================
166  */
167   boolean manager_addRoom(int roomId, int numRoom, int price) {
168     return addReservation(roomTablePtr, roomId, numRoom, price);
169   }
170
171   
172
173
174 /* =============================================================================
175  * manager_deleteRoom
176  * -- Delete rooms from a city
177  * -- Decreases available room count (those not allocated to a customer)
178  * -- Fails if would make available room count negative
179  * -- If decresed to 0, deletes entire entry
180  * -- Returns TRUE on success, else FALSE
181  * =============================================================================
182  */
183   boolean manager_deleteRoom(int roomId, int numRoom) {
184     /* -1 keeps old price */
185     return addReservation(roomTablePtr, roomId, -numRoom, -1);
186   }
187
188
189 /* =============================================================================
190  * manager_addFlight
191  * -- Add seats to a flight
192  * -- Adding to an existing flight overwrite the price if 'price' >= 0
193  * -- Returns TRUE on success, FALSE on failure
194  * =============================================================================
195  */
196   boolean manager_addFlight(int flightId, int numSeat, int price)  {
197     return addReservation(flightTablePtr, flightId, numSeat, price);
198   }
199
200
201
202 /* =============================================================================
203  * manager_deleteFlight
204  * -- Delete an entire flight
205  * -- Fails if customer has reservation on this flight
206  * -- Returns TRUE on success, else FALSE
207  * =============================================================================
208  */
209   boolean manager_deleteFlight(int flightId) {
210     Reservation reservationPtr = (Reservation)flightTablePtr.find(flightId);
211     if (reservationPtr == null) {
212       return false;
213     }
214     
215     if (reservationPtr.numUsed > 0) {
216       return false; /* somebody has a reservation */
217     }
218
219     return addReservation(flightTablePtr,
220                           flightId,
221                           -reservationPtr.numTotal,
222                           -1 /* -1 keeps old price */);
223   }
224
225
226 /* =============================================================================
227  * manager_addCustomer
228  * -- If customer already exists, returns failure
229  * -- Returns TRUE on success, else FALSE
230  * =============================================================================
231  */
232   boolean manager_addCustomer(int customerId) {
233     Customer customerPtr;
234     boolean status;
235     
236     if (customerTablePtr.contains(customerId)) {
237       return false;
238     }
239     
240     customerPtr = new Customer(customerId);
241     //  assert(customerPtr != null);
242     status = customerTablePtr.insert(customerId, customerPtr);
243     if (!status) {
244       System.out.println("TMRESTART8");
245       System.exit(-1);
246     }
247     
248     return true;
249   }
250
251
252 /* =============================================================================
253  * manager_deleteCustomer
254  * -- Delete this customer and associated reservations
255  * -- If customer does not exist, returns success
256  * -- Returns TRUE on success, else FALSE
257  * =============================================================================
258  */
259   boolean manager_deleteCustomer (int customerId) {
260     Customer customerPtr;
261     RBTree reservationTables[]=new RBTree[NUM_RESERVATION_TYPE];
262     List_t reservationInfoListPtr;
263     List_Node it;
264     boolean status;
265
266     customerPtr = (Customer) customerTablePtr.find(customerId);
267     if (customerPtr == null) {
268       return false;
269     }
270
271     reservationTables[RESERVATION_CAR] = carTablePtr;
272     reservationTables[RESERVATION_ROOM] = roomTablePtr;
273     reservationTables[RESERVATION_FLIGHT] = flightTablePtr;
274
275     /* Cancel this customer's reservations */
276     reservationInfoListPtr = customerPtr.reservationInfoListPtr;
277     it=reservationInfoListPtr.head;
278     while (it.nextPtr!=null) {
279       Reservation_Info reservationInfoPtr;
280       Reservation reservationPtr;
281       it=it.nextPtr;
282       reservationInfoPtr =(Reservation_Info)it.dataPtr;
283       reservationPtr = (Reservation)reservationTables[reservationInfoPtr.type].find(reservationInfoPtr.id);
284       if (reservationPtr == null) {
285         System.out.println("TMRESTART9");
286         System.exit(-1);
287       }
288       status = reservationPtr.reservation_cancel();
289
290       if (!status) {
291         System.out.println("TMRESTART10");
292         System.exit(-1);
293       }
294     }
295     
296     status = customerTablePtr.remove(customerId);
297     if (!status) {
298       System.out.println("TMRESTART11");
299       System.exit(-1);
300     }
301     return true;
302   }
303   
304
305 /* =============================================================================
306  * QUERY INTERFACE
307  * =============================================================================
308  */
309
310
311 /* =============================================================================
312  * queryNumFree
313  * -- Return numFree of a reservation, -1 if failure
314  * =============================================================================
315  */
316   int queryNumFree(RBTree tablePtr, int id) {
317     int numFree = -1;
318     Reservation reservationPtr = (Reservation)tablePtr.find(id);
319     if (reservationPtr != null) {
320       numFree = reservationPtr.numFree;
321     }
322     
323     return numFree;
324   }
325
326
327 /* =============================================================================
328  * queryPrice
329  * -- Return price of a reservation, -1 if failure
330  * =============================================================================
331  */
332   int queryPrice (RBTree tablePtr, int id) {
333     int price = -1;
334     Reservation reservationPtr=(Reservation) tablePtr.find(id);
335     if (reservationPtr != null) {
336       price = reservationPtr.price;
337     }
338     
339     return price;
340   }
341
342
343 /* =============================================================================
344  * manager_queryCar
345  * -- Return the number of empty seats on a car
346  * -- Returns -1 if the car does not exist
347  * =============================================================================
348  */
349   int manager_queryCar (int carId) {
350     return queryNumFree(carTablePtr, carId);
351   }
352
353
354 /* =============================================================================
355  * manager_queryCarPrice
356  * -- Return the price of the car
357  * -- Returns -1 if the car does not exist
358  * =============================================================================
359  */
360   int manager_queryCarPrice (int carId) {
361     return queryPrice(carTablePtr, carId);
362   }
363
364
365 /* =============================================================================
366  * manager_queryRoom
367  * -- Return the number of empty seats on a room
368  * -- Returns -1 if the room does not exist
369  * =============================================================================
370  */
371   int manager_queryRoom (int roomId) {
372     return queryNumFree(roomTablePtr, roomId);
373   }
374
375
376 /* =============================================================================
377  * manager_queryRoomPrice
378  * -- Return the price of the room
379  * -- Returns -1 if the room does not exist
380  * =============================================================================
381  */
382   int manager_queryRoomPrice (int roomId) {
383     return queryPrice(roomTablePtr, roomId);
384   }
385
386
387 /* =============================================================================
388  * manager_queryFlight
389  * -- Return the number of empty seats on a flight
390  * -- Returns -1 if the flight does not exist
391  * =============================================================================
392  */
393   int manager_queryFlight (int flightId) {
394     return queryNumFree(flightTablePtr, flightId);
395   }
396
397
398 /* =============================================================================
399  * manager_queryFlightPrice
400  * -- Return the price of the flight
401  * -- Returns -1 if the flight does not exist
402  * =============================================================================
403  */
404   int manager_queryFlightPrice(int flightId) {
405     return queryPrice(flightTablePtr, flightId);
406   }
407
408   
409   /* =============================================================================
410    * manager_queryCustomerBill
411    * -- Return the total price of all reservations held for a customer
412    * -- Returns -1 if the customer does not exist
413    * =============================================================================
414    */
415   int manager_queryCustomerBill(int customerId) {
416     int bill = -1;
417     Customer customerPtr;
418
419     customerPtr = (Customer) customerTablePtr.find(customerId);
420     
421     if (customerPtr != null) {
422       bill = customerPtr.customer_getBill();
423     }
424     
425     return bill;
426   }
427
428
429 /* =============================================================================
430  * RESERVATION INTERFACE
431  * =============================================================================
432  */
433
434
435 /* =============================================================================
436  * reserve
437  * -- Customer is not allowed to reserve same (type, id) multiple times
438  * -- Returns TRUE on success, else FALSE
439  * =============================================================================
440  */
441   static boolean reserve (RBTree tablePtr, RBTree customerTablePtr,
442                           int customerId, int id, int type) {
443     Customer customerPtr;
444     Reservation reservationPtr;
445
446     customerPtr = (Customer)customerTablePtr.find(customerId);
447
448     if (customerPtr == null) {
449       return false;
450     }
451     
452     reservationPtr = (Reservation)tablePtr.find(id);
453     if (reservationPtr == null) {
454       return false;
455     }
456
457     if (!reservationPtr.reservation_make()) {
458       return false;
459     }
460
461     if (!customerPtr.customer_addReservationInfo(type,
462             id,
463             reservationPtr.price)) {
464       /* Undo previous successful reservation */
465       boolean status = reservationPtr.reservation_cancel();
466       if (!status) {
467         System.out.println("TMRESTART12");
468         System.exit(-1);
469       }
470       return false;
471     }
472     
473     return true;
474   }
475
476
477 /* =============================================================================
478  * manager_reserveCar
479  * -- Returns failure if the car or customer does not exist
480  * -- Returns TRUE on success, else FALSE
481  * =============================================================================
482  */
483   boolean manager_reserveCar (int customerId, int carId) {
484     return reserve(carTablePtr,
485                    customerTablePtr,
486                    customerId,
487                    carId,
488                    RESERVATION_CAR);
489   }
490
491
492 /* =============================================================================
493  * manager_reserveRoom
494  * -- Returns failure if the room or customer does not exist
495  * -- Returns TRUE on success, else FALSE
496  * =============================================================================
497  */
498   boolean manager_reserveRoom (int customerId, int roomId) {
499     return reserve(roomTablePtr,
500                    customerTablePtr,
501                    customerId,
502                    roomId,
503                    RESERVATION_ROOM);
504   }
505
506
507 /* =============================================================================
508  * manager_reserveFlight
509  * -- Returns failure if the flight or customer does not exist
510  * -- Returns TRUE on success, else FALSE
511  * =============================================================================
512  */
513   boolean manager_reserveFlight (int customerId, int flightId) {
514     return reserve(flightTablePtr,
515                    customerTablePtr,
516                    customerId,
517                    flightId,
518                    RESERVATION_FLIGHT);
519   }
520
521
522 /* =============================================================================
523  * cancel
524  * -- Customer is not allowed to cancel multiple times
525  * -- Returns TRUE on success, else FALSE
526  * =============================================================================
527  */
528   static boolean cancel (RBTree tablePtr, RBTree customerTablePtr,
529                          int customerId, int id, int type) {
530     Customer customerPtr;
531     Reservation reservationPtr;
532
533     customerPtr = (Customer) customerTablePtr.find(customerId);
534     if (customerPtr == null) {
535       return false;
536     }
537
538     reservationPtr = (Reservation) tablePtr.find(id);
539     if (reservationPtr == null) {
540       return false;
541     }
542
543     if (!reservationPtr.reservation_cancel()) {
544       return false;
545     }
546
547     if (!customerPtr.customer_removeReservationInfo(type, id)) {
548         /* Undo previous successful cancellation */
549       boolean status = reservationPtr.reservation_make();
550       if (!status) {
551         System.out.println("TMRESTART13");
552         System.exit(-1);
553       }
554       return false;
555     }
556     
557     return true;
558   }
559
560
561 /* =============================================================================
562  * manager_cancelCar
563  * -- Returns failure if the car, reservation, or customer does not exist
564  * -- Returns TRUE on success, else FALSE
565  * =============================================================================
566  */
567   boolean manager_cancelCar(int customerId, int carId) {
568     return cancel(carTablePtr,
569                   customerTablePtr,
570                   customerId,
571                   carId,
572                   RESERVATION_CAR);
573   }
574
575
576 /* =============================================================================
577  * manager_cancelRoom
578  * -- Returns failure if the room, reservation, or customer does not exist
579  * -- Returns TRUE on success, else FALSE
580  * =============================================================================
581  */
582   boolean manager_cancelRoom (int customerId, int roomId) {
583     return cancel(roomTablePtr,
584                   customerTablePtr,
585                   customerId,
586                   roomId,
587                   RESERVATION_ROOM);
588   }
589
590
591
592   /* =============================================================================
593    * manager_cancelFlight
594    * -- Returns failure if the flight, reservation, or customer does not exist
595    * -- Returns TRUE on success, else FALSE
596    * =============================================================================
597    */
598   boolean manager_cancelFlight(int customerId, int flightId) {
599     return cancel(flightTablePtr,
600                   customerTablePtr,
601                   customerId,
602                   flightId,
603                   RESERVATION_FLIGHT);
604   }
605 }