ocarina-backends-replication_properties.adb 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                 OCARINA.BACKENDS.REPLICATION_PROPERTIES                  --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2016 ESA & ISAE.                       --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.Namet;
with Utils;

with Ocarina.ME_AADL;
with Ocarina.AADL_Values;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Entities;
with Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.Analyzer.AADL.Queries;
with Ocarina.Me_AADL.AADL_Tree.Entities.Properties;

package body Ocarina.Backends.Replication_Properties is

   use Ocarina.Namet;
   use Utils;

   use Ocarina.ME_AADL;
   use Ocarina.AADL_Values;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.ME_AADL.AADL_Tree.Entities;
   use Ocarina.Analyzer.AADL.Queries;
   use Ocarina.Me_AADL.AADL_Tree.Entities.Properties;

   ----------------------------
   -- Replication properties --
   ----------------------------

   Replication_Description                          : Name_Id;
   Replication_Replica_Number                       : Name_Id;
   Replication_Min_Nbr_Replica                      : Name_Id;
   Replication_Max_Nbr_Replica                      : Name_Id;
   Replication_Replica_Identifiers                  : Name_Id;
   Replication_Replica_Type                         : Name_Id;
   Replication_Active_Name                          : Name_Id;
   Replication_Passive_Name                         : Name_Id;
   Replication_Consensus_Algorithm_Source_Text_Name : Name_Id;
   Replication_Consensus_Algorithm_Ref_Name         : Name_Id;
   Replication_Consensus_Algorithm_Class_Name       : Name_Id;

   ----------
   -- Init --
   ----------

   procedure Init is

   begin

      Replication_Description := Get_String_Name
                              ("Replication_Properties::Description");
      Replication_Replica_Number := Get_String_Name
                              ("Replication_Properties::Replica_Number");
      Replication_Min_Nbr_Replica := Get_String_Name
                              ("Replication_Properties::Min_Nbr_Replica");
      Replication_Max_Nbr_Replica := Get_String_Name
                              ("Replication_Properties::Max_Nbr_Replica");
      Replication_Replica_Identifiers := Get_String_Name
                              ("Replication_Properties::Replica_Identifiers");
      Replication_Active_Name := Get_String_Name ("active");
      Replication_Passive_Name := Get_String_Name ("passive");
      Replication_Replica_Type := Get_String_Name ("replica_type");
      Replication_Consensus_Algorithm_Source_Text_Name := Get_String_Name
              ("consensus_algorithm_source_text");
      Replication_Consensus_Algorithm_Class_Name := Get_String_Name
              ("consensus_algorithm_class");
      Replication_Consensus_Algorithm_Ref_Name := Get_String_Name
              ("consensus_algorithm_ref");
   end Init;

   ----------------------------
   -- Replication_Properties --
   ----------------------------
   ---------------------------------
   -- Get_Replication_Description --
   ---------------------------------

   function Get_Replication_Description (C : Node_Id)
      return Name_Id
   is
   begin
      if Is_Defined_String_Property (C, Replication_Description) then
         return Get_String_Property (C, Replication_Description);
      else
         return No_Name;
      end if;
   end Get_Replication_Description;

   function Get_Replication_Description
     (Property_Node    : Node_Id;
      Applies_To_Node  : Node_Id)
      return String
   is
      pragma Assert (Kind (Property_Node)   = K_Property_association);
      pragma Assert (Kind (Applies_To_Node) = K_SUBCOMPONENT);
      Property_Expression : Node_Id;
      Property_Name       : Name_Id;
      Property_Value      : Node_Id;
      Applies_To_List     : List_Id;
      Entity_Of_Property  : Node_Id;
      Pointed_Node        : Node_Id := No_Node;
      Prefix_Name         : constant Name_Id :=
        Get_String_Name ("Replication_Properties::Description");
   begin
      Property_Name := Get_Name_Of_Entity (Property_Node, true);
      Property_Value :=  Property_Association_Value (Property_Node);
      Property_Expression := Compute_Property_Value (Property_Value);
      Applies_To_List := List_Items (First_Node
                      (Applies_To_Prop (Property_Node)));
      if not Is_Empty (Applies_To_List) then
         Pointed_Node := First_Node (Applies_To_List);
         if Kind (Pointed_Node) = K_Array_Selection then
            Entity_Of_Property := Corresponding_Entity
                     (Identifier (Pointed_Node));
         else
            Entity_Of_Property := Corresponding_Entity
                                  (Pointed_Node);
         end if;
      end if;
      if kind (Property_Expression) = K_LITERAL and then
         Is_Prefix (Prefix_Name, Property_Name) and then
        Entity_Of_Property = Applies_To_Node
      then
         return Ocarina.AADL_Values.Image
                (Value (Property_Expression));
      else
         return "";
      end if;
   end Get_Replication_Description;

   ------------------------------------
   -- Get_Replication_Replica_Number --
   ------------------------------------

   function Get_Replication_Replica_Number (C : Node_Id)
      return Unsigned_Long_Long is
   begin
      if Is_Defined_Integer_Property (C, Replication_Replica_Number) then
         return Get_Integer_Property (C, Replication_Replica_Number);
      else
         return 0;
      end if;
   end Get_Replication_Replica_Number;

   ------------------------------------
   -- Get_Replication_Replica_Number --
   ------------------------------------

   function Get_Replication_Replica_Number
      (Property_Node   : Node_Id;
       Applies_To_Node : Node_Id)
      return Unsigned_Long_Long
   is
      pragma Assert (Kind (Property_Node)   = K_Property_association);
      pragma Assert (Kind (Applies_To_Node) = K_SUBCOMPONENT);
      Property_Expression : Node_Id;
      Property_Name       : Name_Id;
      Property_Value      : Node_Id;
      Applies_To_List     : List_Id;
      Entity_Of_Property  : Node_Id;
      Pointed_Node        : Node_Id := No_Node;
      Prefix_Name         : constant Name_Id :=
        Get_String_Name ("Replication_Properties::Replica_Number");
   begin
      Property_Name := Get_Name_Of_Entity (Property_Node, true);
      Property_Value :=  Property_Association_Value (Property_Node);
      Property_Expression := Compute_Property_Value (Property_Value);
      Applies_To_List := List_Items (First_Node
                      (Applies_To_Prop (Property_Node)));
      if not Is_Empty (Applies_To_List) then
         Pointed_Node := First_Node (Applies_To_List);
         if Kind (Pointed_Node) = K_Array_Selection then
            Entity_Of_Property := Corresponding_Entity
                     (Identifier (Pointed_Node));
         else
            Entity_Of_Property := Corresponding_Entity
                                  (Pointed_Node);
         end if;
      end if;
      if kind (Property_Expression) = K_Signed_AADLNUMBER and then
         Is_Prefix (Prefix_Name, Property_Name) and then
        Entity_Of_Property = Applies_To_Node
      then
         return Get_Integer_Of_Property_Value (Property_Expression);
      else
         return 0;
      end if;
   end Get_Replication_Replica_Number;

   -------------------------------------
   -- Get_Replication_Min_Nbr_Replica --
   -------------------------------------

   function Get_Replication_Min_Nbr_Replica (C : Node_Id)
      return Unsigned_Long_Long is
   begin
      if Is_Defined_Integer_Property (C, Replication_Min_Nbr_Replica) then
         return Get_Integer_Property (C, Replication_Min_Nbr_Replica);
      else
         return 0;
      end if;
   end Get_Replication_Min_Nbr_Replica;

   -------------------------------------
   -- Get_Replication_Max_Nbr_Replica --
   -------------------------------------

   function Get_Replication_Max_Nbr_Replica (C : Node_Id)
      return Unsigned_Long_Long is
   begin
      if Is_Defined_Integer_Property (C, Replication_Max_Nbr_Replica) then
         return Get_Integer_Property (C, Replication_Max_Nbr_Replica);
      else
         return 0;
      end if;
   end Get_Replication_Max_Nbr_Replica;

   -----------------------------------------
   -- Get_Replication_Replica_Identifiers --
   -----------------------------------------

   function Get_Replication_Replica_Identifiers (C : Node_Id)
      return List_Id is
      L : List_Id;
   begin
      if Is_Defined_Property (C, Replication_Replica_Identifiers) then
         L := Get_List_Property (C, Replication_Replica_Identifiers);
         if L /= No_List then
            return L;
         else
            return No_List;
         end if;
      else
            return No_List;
      end if;
   end Get_Replication_Replica_Identifiers;

   -----------------------------------------
   -- Get_Replication_Replica_Identifiers --
   -----------------------------------------

   function Get_Replication_Replica_Identifiers
   (Property_Node   : Node_Id;
    Applies_To_Node : Node_Id)
   return List_Id
   is
      pragma Assert (Kind (Property_Node)   = K_Property_association);
      pragma Assert (Kind (Applies_To_Node) = K_SUBCOMPONENT);
      Property_Name       : Name_Id;
      Property_Value      : Node_Id;
      Applies_To_List     : List_Id;
      Entity_Of_Property  : Node_Id;
      Pointed_Node        : Node_Id := No_Node;
      Prefix_Name         : constant Name_Id :=
        Get_String_Name ("Replication_Properties::Replica_Identifiers");
      L : List_Id := No_List;
   begin
      Property_Name := Get_Name_Of_Entity (Property_Node, true);
      Property_Value :=  Property_Association_Value (Property_Node);
      L :=  Expanded_Multi_Value (Property_Value);
      --  to determine the node to each the property is applied.
      Applies_To_List := List_Items (First_Node
                      (Applies_To_Prop (Property_Node)));
      if not Is_Empty (Applies_To_List) then
         Pointed_Node := First_Node (Applies_To_List);
         if Kind (Pointed_Node) = K_Array_Selection then
            Entity_Of_Property := Corresponding_Entity
                     (Identifier (Pointed_Node));
         else
            Entity_Of_Property := Corresponding_Entity
                                  (Pointed_Node);
         end if;
      end if;

      if Is_Prefix (Prefix_Name, Property_Name) and then
        Entity_Of_Property = Applies_To_Node
      then
         return L;
      else
         return No_list;
      end if;
   end Get_Replication_Replica_Identifiers;

   ----------------------------------
   -- Get_Replication_Replica_Type --
   ----------------------------------

   function Get_Replication_Replica_Type (C : Node_Id)
      return Replication_Types is
      Type_Name : Name_Id;
   begin
      if Is_Defined_Enumeration_Property (C, Replication_Replica_Type) then
         Type_Name := Get_Enumeration_Property (C, Replication_Replica_Type);

         if Type_Name = Replication_Active_Name then
            return Replication_Active;

         elsif Type_Name = Replication_Passive_Name then
            return Replication_Passive;
         else
            return Replication_None;
         end if;
      else
         return Replication_None;
      end if;
   end Get_Replication_Replica_Type;

   ----------------------------------
   -- Get_Replication_Replica_Type --
   ----------------------------------

   function Get_Replication_Replica_Type
   (Property_Node   : Node_Id;
    Applies_To_Node : Node_Id)
   return Replication_Types is
   pragma Assert (Kind (Property_Node)   = K_Property_association);
      pragma Assert (Kind (Applies_To_Node) = K_SUBCOMPONENT);
      Property_Name       : Name_Id;
      Property_Value      : Node_Id;
      Applies_To_List     : List_Id;
      Entity_Of_Property  : Node_Id;
      Pointed_Node        : Node_Id := No_Node;
      Prefix_Name         : constant Name_Id :=
        Get_String_Name ("Replication_Properties::Replica_Type");
   begin
      Property_Name := Get_Name_Of_Entity (Property_Node, true);
      --  the kind of Property_Value = K_Enumeration_Term
      Property_Value := Single_Value (Property_Association_Value
                        (Property_Node));
      --  to determine the node to each the property is applied.
      Applies_To_List := List_Items (First_Node
                      (Applies_To_Prop (Property_Node)));
      if not Is_Empty (Applies_To_List) then
         Pointed_Node := First_Node (Applies_To_List);
         if Kind (Pointed_Node) = K_Array_Selection then
            Entity_Of_Property := Corresponding_Entity
                     (Identifier (Pointed_Node));
         else
            Entity_Of_Property := Corresponding_Entity
                                  (Pointed_Node);
         end if;
      end if;

      if Is_Prefix (Prefix_Name, Property_Name) and then
        Entity_Of_Property = Applies_To_Node
      then
         if Get_Enumeration_Of_Property_Value (Property_Value) = "active"
         then
            return Replication_Active;
         elsif Get_Enumeration_Of_Property_Value (Property_Value) = "passive"
         then
            return Replication_Passive;
         else
            return Replication_None;
         end if;
      else
         return Replication_None;
      end if;
   end Get_Replication_Replica_Type;

   -----------------------------------------
   -- Get_Replication_Consensus_Algorithm --
   -----------------------------------------

   function Get_Replication_Consensus_Algorithm
     (C       : Node_Id;
      In_Mode : Name_Id := No_Name)
     return Name_Id
   is
   begin
      if Is_Defined_String_Property
        (C, Replication_Consensus_Algorithm_Source_Text_Name, In_Mode)
      then
         return Get_String_Property
           (C, Replication_Consensus_Algorithm_Source_Text_Name, In_Mode);
      else
         return No_Name;
      end if;
   end Get_Replication_Consensus_Algorithm;

   -----------------------------------------
   -- Get_Replication_Consensus_Algorithm --
   -----------------------------------------

   procedure Get_Replication_Consensus_Algorithm
   (Property_Node   : Node_Id;
    Consensus_Node_List : in out Node_List) is

      pragma Assert (Kind (Property_Node)   = K_Property_association);
      Property_Name       : Name_Id;
      Prefix_Name : constant Name_Id := Get_String_Name
      ("Replication_Properties::Consensus_Algorithm");
   begin
      Property_Name := Get_Name_Of_Entity (Property_Node, true);
      --  to determine the node to each the property is applied.
      if Is_Prefix (Prefix_Name, Property_Name) then
         Append_Node_To_Node_List (Property_Node,
                                  Consensus_Node_List, True);
      else
         null;
      end if;
   end Get_Replication_Consensus_Algorithm;

   -----------------------------------------
   -- Get_Replication_Consensus_Algorithm --
   -----------------------------------------

   function Get_Replication_Consensus_Algorithm
     (C       : Node_Id;
      In_Mode : Name_Id := No_Name)
     return Node_Id
   is
   begin
      --  if Kind (C) = K_Port_Spec_Instance and then not AIN.Is_Out (C) then
         --  Display_Located_Error
         --  (AIN.Loc (C),
         --  "Consensus algorithm cannot be specified for In-only ports",
         --  Fatal => True);
      --  end if;

      if Is_Defined_String_Property
        (C, Replication_Consensus_Algorithm_Source_Text_Name, In_Mode)
      then
         return Get_Property_Association
           (C, Replication_Consensus_Algorithm_Source_Text_Name, In_Mode);
      elsif Is_Defined_Reference_Property
        (C, Replication_Consensus_Algorithm_Ref_Name, In_Mode)
      then
         return Get_Property_Association
           (C, Replication_Consensus_Algorithm_Ref_Name, In_Mode);
      elsif Is_Defined_Property
        (C, Replication_Consensus_Algorithm_Class_Name, In_Mode)
      then
         return Get_Classifier_Property
            (C, Replication_Consensus_Algorithm_Class_Name, In_Mode);
      else
         return No_Node;
      end if;

   end Get_Replication_Consensus_Algorithm;

   ---------------------------------------------
   -- Is_Defined_Consensus_Algorithm_Property --
   ---------------------------------------------

   function Is_Defined_Consensus_Algorithm_Property
     (C       : Node_Id;
      In_Mode : Name_Id := No_Name)
     return Boolean
   is
   begin
      if Is_Defined_String_Property
        (C, Replication_Consensus_Algorithm_Source_Text_Name, In_Mode)
      then
         return True;
      elsif Is_Defined_Reference_Property
        (C, Replication_Consensus_Algorithm_Ref_Name, In_Mode)
      then
         return True;
      elsif Is_Defined_Property
        (C, Replication_Consensus_Algorithm_Class_Name, In_Mode)
      then
         return True;
      else
         return False;
      end if;
   end Is_Defined_Consensus_Algorithm_Property;

end Ocarina.Backends.Replication_Properties;