home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / os2apipm.zip / APIEXAM / PHIL / ROOM.ADB < prev    next >
Text File  |  1996-06-05  |  5KB  |  131 lines

  1. --room.adb
  2. with Windows;
  3. with Phil;
  4. with Society; use Society;
  5. with Calendar;
  6. pragma Elaborate (Phil);
  7. package body Room is
  8.  
  9.   -- Dining Philosophers, Ada 95 edition
  10.   -- A line-oriented version of the Room package
  11.   -- Michael B. Feldman, The George Washington University,
  12.   -- July, 1995.
  13.  
  14.   -- philosophers sign into dining room, giving Maitre_D their DNA code
  15.  
  16.   Dijkstra  : aliased Phil.Philosopher ;
  17.   Stroustrup: aliased Phil.Philosopher ;
  18.   Anderson  : aliased Phil.Philosopher ;
  19.   Ichbiah   : aliased Phil.Philosopher ;
  20.   Taft      : aliased Phil.Philosopher ;
  21.  
  22.   type Philosopher_Ptr is access all Phil.Philosopher;
  23.  
  24.   Phils : array (Table_Type) of Philosopher_Ptr;
  25.   Phil_Windows : array(Table_Type) of Windows.Window;
  26.   Phil_Seats : array (Society.Unique_DNA_Codes) of Table_Type;
  27.  
  28.   task body Maitre_D is
  29.  
  30.     T          : Natural;
  31.     Start_Time : Calendar.Time;
  32.     Blanks : constant String := "     ";
  33.  
  34.   begin
  35.     accept Start_Serving;
  36.     Start_Time := Calendar.Clock;
  37.     -- now Maitre_D assigns phils to seats at the table
  38.     Phils(1):= Dijkstra'Access;
  39.     Phils(2):= Anderson'Access;
  40.     Phils(3):= Taft'Access;
  41.     Phils(4):= Ichbiah'Access;
  42.     Phils(5):= Stroustrup'Access ;
  43.  
  44.     Phil_Seats   := (1, 3, 5, 4, 2);   -- which seat each phil occupies
  45.     Phil_Windows :=  -- access to function
  46.       (Windows.Open (( 1, 24), 7, 30),
  47.        Windows.Open (( 9,  2), 7, 30),
  48.        Windows.Open (( 9, 46), 7, 30),
  49.        Windows.Open ((17,  7), 7, 30),
  50.        Windows.Open ((17, 41), 7, 30));
  51.  
  52.     for Which_Window in Phil_Windows'range loop
  53.       Windows.Borders (Phil_Windows(Which_Window), '+', '│', '─');
  54.     end loop;
  55.  
  56.     -- and assigns them their chopsticks.
  57.  
  58.     Phils ( 1).Start_Eating (1, 2);
  59.     Phils ( 3).Start_Eating (3, 4);
  60.     Phils ( 2).Start_Eating (2, 3);
  61.     Phils ( 5).Start_Eating (1, 5);
  62.     Phils ( 4).Start_Eating (4, 5);
  63.  
  64.     loop
  65.       select
  66.         accept Report_State (Which_Phil : in Society.Unique_DNA_Codes;
  67.                              State      : in Phil.States;
  68.                              How_Long   : in Natural := 0;
  69.                              Which_Meal : in Natural := 0) do
  70.  
  71.           T := Natural (Calendar."-" (Calendar.Clock, Start_Time));
  72.           case State is
  73.             when Phil.Breathing =>
  74.               Windows.Title(Phil_Windows(Phil_Seats(Which_Phil)),
  75.                             Society.Name_Register(Which_Phil), '-');
  76.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  77.                      "T =" & Integer'Image (T) & " "
  78.                       & "Breathing...");
  79.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  80.  
  81.             when Phil.Thinking =>
  82.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  83.                      "T =" & Integer'Image (T) & " "
  84.                       & "Thinking"
  85.                       & Integer'Image (How_Long) & " seconds.");
  86.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  87.  
  88.             when Phil.Eating =>
  89.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  90.                      "T =" & Integer'Image (T) & " "
  91.                       & "Meal"
  92.                       & Integer'Image (Which_Meal)
  93.                       & ","
  94.                       & Integer'Image (How_Long) & " seconds.");
  95.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  96.  
  97.             when Phil.Done_Eating =>
  98.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  99.                      "T =" & Integer'Image (T) & " "
  100.                       & "Yum-yum (burp)");
  101.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  102.  
  103.             when Phil.Got_One_Stick =>
  104.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  105.                      "T =" & Integer'Image (T) & " "
  106.                       & "First chopstick"
  107.                       & Integer'Image (How_Long));
  108.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  109.  
  110.             when Phil.Got_Other_Stick =>
  111.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  112.                      "T =" & Integer'Image (T) & " "
  113.                       & "Second chopstick"
  114.                       & Integer'Image (How_Long));
  115.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  116.  
  117.             when Phil.Dying =>
  118.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  119.                      "T =" & Integer'Image (T) & " "
  120.                       & "Croak");
  121.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  122.           end case; -- State
  123.           Society.Draw_screen.Need_draw; -- output to terminal
  124.         end Report_State;
  125.       or
  126.         terminate;
  127.       end select;
  128.     end loop;
  129.   end Maitre_D;
  130. end Room;
  131.