Advertisement
Guest User

ada se wklada xDDD

a guest
Oct 15th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 10.57 KB | None | 0 0
  1. -- A skeleton of a program for an assignment in programming languages
  2. -- The students should rename the tasks of producers, consumers, and the buffer
  3. -- Then, they should change them so that they would fit their assignments
  4. -- They should also complete the code with constructions that lack there
  5. with Ada.Text_IO; use Ada.Text_IO;
  6. with Ada.Integer_Text_IO;
  7. with Ada.Numerics.Discrete_Random;
  8.  
  9.  
  10. procedure Mainxd is
  11.    Number_Of_Products: constant Integer := 5;
  12.    Number_Of_Assemblies: constant Integer := 3;
  13.    Number_Of_Consumers: constant Integer := 2;
  14.    subtype Product_Type is Integer range 1 .. Number_Of_Products;
  15.    subtype Assembly_Type is Integer range 1 .. Number_Of_Assemblies;
  16.    subtype Consumer_Type is Integer range 1 .. Number_Of_Consumers;
  17.    Product_Name: constant array (Product_Type) of String(1 .. 8)
  18.      := ("Wood    ", "Diamond ", "Iron    ", "Gold    ", "Obsidian");
  19.    Assembly_Name: constant array (Assembly_Type) of String(1 .. 5)
  20.      := ("Sword", "Armor", "Axe  ");
  21.    package Random_Assembly is new
  22.      Ada.Numerics.Discrete_Random(Assembly_Type);
  23.    type My_Str is new String(1 ..256);
  24.  
  25.    -- Producer produces determined product
  26.    task type Producer is
  27.       -- Give the Producer an identity, i.e. the product type
  28.       entry Start(Product: in Product_Type; Production_Time: in Integer);
  29.    end Producer;
  30.  
  31.    -- Consumer gets an arbitrary assembly of several products from the buffer
  32.    task type Consumer is
  33.       -- Give the Consumer an identity
  34.       entry Start(Consumer_Number: in Consumer_Type;
  35.                   Consumption_Time: in Integer);
  36.    end Consumer;
  37.  
  38.    -- In the Buffer, products are assemblied into an assembly
  39.    task type Buffer is
  40.       -- Accept a product to the storage provided there is a room for it
  41.       entry Take(Product: in Product_Type; Number: in Integer; Success: out Boolean);
  42.       -- Deliver an assembly provided there are enough products for it
  43.       entry Deliver(Assembly: in Assembly_Type; Delivered: out Boolean);
  44.    end Buffer;
  45.  
  46.    P: array ( 1 .. Number_Of_Products ) of Producer;
  47.    K: array ( 1 .. Number_Of_Consumers ) of Consumer;
  48.    B: Buffer;
  49.  
  50.  
  51.    task body Producer is
  52.       Temp: Boolean;
  53.       subtype Production_Time_Range is Integer range 3 .. 6;
  54.       package Random_Production is new
  55.         Ada.Numerics.Discrete_Random(Production_Time_Range);
  56.       G: Random_Production.Generator;   --  generator liczb losowych
  57.       Product_Type_Number: Integer;
  58.       Product_Number: Integer;
  59.       Production: Integer;
  60.    begin
  61.       accept Start(Product: in Product_Type; Production_Time: in Integer) do
  62.          Random_Production.Reset(G);    --  start random number generator
  63.          Product_Number := 1;
  64.          Product_Type_Number := Product;
  65.          Production := Production_Time;
  66.       end Start;
  67.       Put_Line("[P]Started producer of " & Product_Name(Product_Type_Number));
  68.       loop
  69.          delay Duration(Random_Production.Random(G)); --  symulacja produkcji
  70.          Put_Line("[P]Produced item" & Product_Name(Product_Type_Number)
  71.                   & " numer "  & Integer'Image(Product_Number));
  72.          loop
  73.             select
  74.                B.Take(Product_Type_Number, Product_Number, Temp);
  75.                Product_Number := Product_Number + 1;
  76.                  if Temp = True then
  77.                     Put_Line("[P]Producer: Take");
  78.                     exit;
  79.                 else
  80.                     Put_Line("[P]Producer: Cannot Take");
  81.                 end if;
  82.             or
  83.                delay 4.0;
  84.                Put_Line("[P]Bufor is busy - wait.");
  85.             end select;
  86.          end loop;
  87.       end loop;
  88.    end Producer;
  89.  
  90.    task body Consumer is
  91.       Temp: Boolean;
  92.       subtype Consumption_Time_Range is Integer range 4 .. 8;
  93.       package Random_Consumption is new
  94.         Ada.Numerics.Discrete_Random(Consumption_Time_Range);
  95.       G: Random_Consumption.Generator--  random number generator (time)
  96.       G2: Random_Assembly.Generator;    --  also (assemblies)
  97.       Consumer_Nb: Consumer_Type;
  98.       --Assembly_Number: Integer;
  99.       Consumption: Integer;
  100.       Assembly_Type: Integer;
  101.       Consumer_Name: constant array (1 .. Number_Of_Consumers)
  102.         of String(1 .. 5)
  103.         := ("Joker", "Roman");
  104.    begin
  105.       accept Start(Consumer_Number: in Consumer_Type;
  106.                    Consumption_Time: in Integer) do
  107.          Random_Consumption.Reset(G);   --  ustaw generator
  108.  
  109.          Consumer_Nb := Consumer_Number;
  110.          Consumption := Consumption_Time;
  111.       end Start;
  112.       Put_Line("[C]Started consumer " & Consumer_Name(Consumer_Nb));
  113.       loop
  114.          delay Duration(Random_Consumption.Random(G)); --  simulate consumption
  115.          Assembly_Type := Random_Assembly.Random(G2);
  116.          -- take an assembly for consumption
  117.          Temp := false;  
  118.          loop
  119.             select
  120.                B.Deliver(Consumer_Nb, Temp);      
  121.                 if Temp = True then
  122.                     Put_Line("[C]Delivered");
  123.                     exit;
  124.                  else
  125.                     Put_Line("[C]Not delivered");
  126.                 end if;
  127.             or
  128.                delay 3.0;
  129.                Put_Line("[C]Not enough items");  
  130.             end select;
  131.         end loop;
  132.       end loop;
  133.    end Consumer;
  134.  
  135.    task body Buffer is
  136.       Storage_Capacity: constant Integer := 30;
  137.       type Storage_type is array (Product_Type) of Integer;
  138.       Storage: Storage_type
  139.         := (0, 0, 0, 0, 0);
  140.       Assembly_Content: array(Assembly_Type, Product_Type) of Integer
  141.         := ((2, 1, 2, 1, 2),
  142.             (2, 2, 0, 1, 0),
  143.             (1, 1, 2, 0, 1));
  144.       Max_Assembly_Content: array(Product_Type) of Integer;
  145.       Assembly_Number: array(Assembly_Type) of Integer
  146.         := (1, 2, 3);
  147.       In_Storage: Integer := 0;
  148.  
  149.       procedure Setup_Variables is
  150.       begin
  151.          for W in Product_Type loop
  152.             Max_Assembly_Content(W) := 0;
  153.             for Z in Assembly_Type loop
  154.                if Assembly_Content(Z, W) > Max_Assembly_Content(W) then
  155.                   Max_Assembly_Content(W) := Assembly_Content(Z, W);
  156.                end if;
  157.             end loop;
  158.          end loop;
  159.       end Setup_Variables;
  160.  
  161.       function Can_Accept(Product: Product_Type) return Boolean is
  162.          Free: Integer;     --  free room in the storage
  163.          -- how many products are for production of arbitrary assembly
  164.          Lacking: array(Product_Type) of Integer;
  165.          -- how much room is needed in storage to produce arbitrary assembly
  166.          Lacking_room: Integer;
  167.          MP: Boolean;           --  can accept
  168.       begin
  169.          if In_Storage >= Storage_Capacity then
  170.             Put_Line("[B]Too many products in storage.");
  171.             return False;
  172.          end if;
  173.          -- There is free room in the storage
  174.          Free := Storage_Capacity - In_Storage;
  175.          MP := True;
  176.          for W in Product_Type loop
  177.             if Storage(W) < Max_Assembly_Content(W) then
  178.                MP := False;
  179.             end if;
  180.          end loop;
  181.          if MP then
  182.             return True;        --  storage has products for arbitrary
  183.             --  assembly
  184.          end if;
  185.          if Integer'Max(0, Max_Assembly_Content(Product) - Storage(Product)) > 0 then
  186.             -- exactly this product lacks
  187.             return True;
  188.          end if;
  189.          Lacking_room := 1;         --  insert current product
  190.          for W in Product_Type loop
  191.             Lacking(W) := Integer'Max(0, Max_Assembly_Content(W) - Storage(W));
  192.             Lacking_room := Lacking_room + Lacking(W);
  193.          end loop;
  194.          if Free >= Lacking_room then
  195.             -- there is enough room in storage for arbitrary assembly
  196.             return True;
  197.          else
  198.             -- no room for this product
  199.             return False;
  200.          end if;
  201.       end Can_Accept;
  202.  
  203.       function Can_Deliver(Assembly: Assembly_Type) return Boolean is
  204.       begin
  205.          for W in Product_Type loop
  206.             if Storage(W) < Assembly_Content(Assembly, W) then
  207.                return False;
  208.             end if;
  209.          end loop;
  210.          return True;
  211.       end Can_Deliver;
  212.  
  213.       procedure Storage_Contents is
  214.       begin
  215.          for W in Product_Type loop
  216.             Put_Line("[B]Storage contents: " & Integer'Image(Storage(W)) & " "
  217.                      & Product_Name(W));
  218.          end loop;
  219.       end Storage_Contents;
  220.  
  221.    begin
  222.       Put_Line("[B]Buffer started");
  223.       Setup_Variables;
  224.       loop
  225.          select
  226.             accept Take(Product: in Product_Type; Number: in Integer; Success: out Boolean) do
  227.                Put_Line("[B]Accepted TAKE " & Product_Name(Product) & " number " &
  228.                           Integer'Image(Number));
  229.                if Can_Accept(Product) then
  230.                   Put_Line("[B]Accepted product " & Product_Name(Product) & " number " &
  231.                              Integer'Image(Number));
  232.                   Storage(Product) := Storage(Product) + 1;
  233.                   In_Storage := In_Storage + 1;
  234.                   Success := True;
  235.                else
  236.                   Put_Line("[B]Rejected product " & Product_Name(Product) & " number " &
  237.                              Integer'Image(Number));
  238.                   Success := False;
  239.                end if;
  240.             end Take;
  241.             Storage_Contents;
  242.          or
  243.             accept Deliver(Assembly: in Assembly_Type; Delivered: out Boolean) do
  244.                Put_Line("[B]Started DELIVERY of " & Assembly_Name(Assembly) & "-" & Integer'Image(Assembly));
  245.                if Can_Deliver(Assembly) then
  246.                   Put_Line("[B]Delivered assembly " & Assembly_Name(Assembly) & " number " &
  247.                              Integer'Image(Assembly_Number(Assembly)));
  248.                   for W in Product_Type loop
  249.                      Storage(W) := Storage(W) - Assembly_Content(Assembly, W);
  250.                      In_Storage := In_Storage - Assembly_Content(Assembly, W);
  251.                   end loop;
  252.                   Assembly_Number(Assembly) := Assembly_Number(Assembly) + 1;
  253.                   Delivered := True;
  254.                else
  255.                   Put_Line("[B]Lacking products for assembly " & Assembly_Name(Assembly));
  256.                   Delivered := False;
  257.                end if;
  258.             end Deliver;
  259.             Storage_Contents;
  260.          or
  261.             delay 2.0;
  262.             Put_Line("[B]Bufor is waiting for work to be found.");
  263.          end select;
  264.       end loop;
  265.    end Buffer;
  266.  
  267. begin
  268.    for I in 1 .. Number_Of_Products loop
  269.       P(I).Start(I, 10);
  270.    end loop;
  271.    for J in 1 .. Number_Of_Consumers loop
  272.       K(J).Start(J,12);
  273.    end loop;
  274. end Mainxd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement