Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Ada.Text_IO, Ada.Numerics.Float_Random;
- use Ada.Text_IO, Ada.Numerics.Float_Random;
- procedure Traffic is
- protected Multi_printer is
- function Generate_random_number return Float;
- procedure Write(S: in String);
- end Multi_printer;
- protected body Multi_printer is
- function Generate_random_number return Float is
- gen : Generator;
- begin
- Reset(gen);
- return Random(gen);
- end generate_random_number;
- procedure Write(S: in String) is
- begin
- Put_Line(S);
- end;
- end Multi_printer;
- task Crossroads is
- entry Cross(T: in Duration);
- entry Wake_Up;
- end Crossroads;
- type Lamp_color is (Piros,PirosSarga,Zold,Sarga);
- task Lamp is
- entry Switch;
- entry GetColor(Lampc: out Lamp_color);
- end Lamp;
- task body Lamp is
- LC: Lamp_color := Piros;
- task Agens is
- entry Color_Changed;
- end Agens;
- task body Agens is
- begin
- accept Color_Changed
- do
- Crossroads.Wake_Up;
- end Color_Changed;
- end Agens;
- begin
- loop
- select
- accept Switch
- do
- case(LC) is
- when Piros => LC := PirosSarga;
- when PirosSarga => LC := Zold;
- when Zold => LC := Sarga;
- when Sarga => LC := Piros;
- end case;
- Multi_printer.Write(LC'Image);
- Agens.Color_Changed;
- end Switch;
- or
- accept GetColor(Lampc: out Lamp_color)
- do
- Lampc := LC;
- end GetColor;
- or
- terminate;
- end select;
- end loop;
- end Lamp;
- task Controller;
- task body Controller is
- begin
- loop
- delay 3.0;
- Lamp.Switch;
- delay 1.0;
- Lamp.Switch;
- delay 3.0;
- Lamp.Switch;
- delay 2.0;
- Lamp.Switch;
- end loop;
- end Controller;
- task body Crossroads is
- LC: Lamp_color;
- begin
- loop
- Lamp.GetColor(LC);
- select
- when LC = Zold => accept Cross(T: in Duration)
- do
- delay(T);
- end Cross;
- or
- accept Wake_Up
- do
- null;
- end Wake_Up;
- end select;
- end loop;
- end;
- type PString is access String;
- task type Vehicle(RegNum : PString);
- type Vehicle_Access is access Vehicle;
- P : Vehicle_Access;
- task body Vehicle is
- LC : Lamp_color;
- Waiting : Boolean := True;
- Time: Duration := Duration(Float'Remainder(Multi_printer.Generate_random_number,1.0)) + 0.5;
- begin
- Put_Line(RegNum.all);
- while Waiting loop
- Lamp.GetColor(LC);
- if LC = Zold then
- Crossroads.Cross(Time);
- Put_Line(RegNum.all);
- Waiting := False;
- else
- Time := Duration(Float'Remainder(Multi_printer.Generate_random_number,1.0)) + 2.5;
- delay 0.2;
- end if;
- end loop;
- end Vehicle;
- Auto : PString;
- begin
- Auto := new String'("EZR231");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("HGK831");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("SLE391");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("REW222");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("UYR494");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("WQQ121");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("BNM367");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("LKJ122");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("PYO345");
- P := new Vehicle(Auto);
- delay 0.5;
- Auto := new String'("DFG112");
- P := new Vehicle(Auto);
- end Traffic;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement