Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.12 KB | None | 0 0
  1. with Ada.Text_IO, Ada.Characters.Handling;
  2. procedure bead is
  3.  
  4.     type Color_Type is (Red, RedYellow, Green, Yellow);
  5.  
  6.     task type Signal;
  7.  
  8.     protected Lamp is
  9.         procedure Switch;
  10.         function Color return Color_Type;
  11.     private
  12.         Agent : access Signal;
  13.         Actual_Color : Color_Type := Red;
  14.     end Lamp;
  15.  
  16.     protected body Lamp is     
  17.         function Color return Color_Type is
  18.         begin
  19.             return Actual_Color;
  20.         end Color;
  21.  
  22.         procedure Switch is
  23.         begin
  24.             Agent := new Signal;
  25.             if Actual_Color = Yellow then
  26.                 Actual_Color := Red;
  27.             else
  28.                 Actual_Color := Color_Type'Val(Color_Type'Pos(Actual_Color) + 1);
  29.             end if;
  30.         end Switch;
  31.     end Lamp;
  32.  
  33.     task Controller is
  34.         entry Stop;
  35.     end Controller;
  36.  
  37.     task body Controller is
  38.     begin
  39.         loop
  40.             select
  41.                 accept Stop;
  42.                 exit;
  43.             else
  44.                 delay 3.0;
  45.                 Lamp.Switch;
  46.                 delay 1.0;
  47.                 Lamp.Switch;
  48.                 delay 3.0;
  49.                 Lamp.Switch;
  50.                 delay 2.0;
  51.                 Lamp.Switch;
  52.             end select;
  53.         end loop;
  54.     end Controller;
  55.  
  56.     protected Crossroad is
  57.         entry Cross(Time_To_Cross : Duration);
  58.         procedure Wake_Up;
  59.     end Crossroad;
  60.  
  61.     task body Signal is
  62.     begin
  63.         Crossroad.Wake_Up;
  64.     end Signal;
  65.  
  66.     type String_Access is access String;
  67.  
  68.     task type Vehicle(Plate: String_Access);
  69.  
  70.     task body Vehicle is
  71.     Crossed : Boolean := False;
  72.     Time_To_Cross : Duration := 1.0;
  73.     begin
  74.         Ada.Text_IO.Put_Line(Plate.all & " a lampahoz erkezett.");
  75.         while not Crossed loop
  76.             select
  77.                 Crossroad.Cross(Time_To_Cross);
  78.                 Crossed := True;   
  79.             else
  80.                 if Time_To_Cross = 1.0 then
  81.                     Time_To_Cross := 3.0;
  82.                 end if;
  83.             end select;
  84.         end loop;
  85.         Ada.Text_IO.Put_Line(Plate.all & " athaladt.");    
  86.     end Vehicle;
  87.  
  88.     protected body Crossroad is
  89.  
  90.         entry Cross(Time_To_Cross : Duration) when Lamp.Color = Green is
  91.         begin
  92.                 delay Time_To_Cross;
  93.         end Cross;
  94.  
  95.         procedure Wake_Up is
  96.         begin
  97.             null;
  98.         end Wake_Up;
  99.  
  100.     end Crossroad;
  101.  
  102.     Car : access Vehicle;
  103.  
  104. begin
  105.     for i in 1..10 loop
  106.         Car := new Vehicle(new String'("Auto" & Integer'Image(i)));
  107.         delay 1.0
  108.     end loop;
  109.     Ada.Text_IO.Skip_Line;
  110.     Controller.Stop;
  111.     Ada.Text_IO.Put_Line("A vezerlo leallt.");
  112. end bead;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement