ocarina-utils.adb 9.82 KB
Newer Older
yoogx's avatar
yoogx committed
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                        O C A R I N A . U T I L S                         --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
yoogx's avatar
yoogx committed
9
--                   Copyright (C) 2013-2015 ESA & ISAE.                    --
yoogx's avatar
yoogx committed
10
--                                                                          --
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
-- 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/>.                                          --
yoogx's avatar
yoogx committed
26 27 28 29 30
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------
31

yoogx's avatar
yoogx committed
32 33 34 35
with Ada.Command_Line;           use Ada.Command_Line;
with GNAT.Directory_Operations;  use GNAT.Directory_Operations;
with GNAT.OS_Lib;                use GNAT.OS_Lib;

yoogx's avatar
yoogx committed
36 37
with Errors;                     use Errors;
with Locations;                  use Locations;
38 39
with Ocarina.Namet;                      use Ocarina.Namet;
with Ocarina.Output;                     use Ocarina.Output;
yoogx's avatar
yoogx committed
40
with Utils;                      use Utils;
yoogx's avatar
yoogx committed
41

yoogx's avatar
yoogx committed
42
with Ocarina.Analyzer;           use Ocarina.Analyzer;
yoogx's avatar
yoogx committed
43 44 45
with Ocarina.Backends;           use Ocarina.Backends;
with Ocarina.Configuration;      use Ocarina.Configuration;
with Ocarina.FE_AADL;            use Ocarina.FE_AADL;
yoogx's avatar
yoogx committed
46
with Ocarina.FE_AADL.Parser;
yoogx's avatar
yoogx committed
47
with Ocarina.FE_REAL;            use Ocarina.FE_REAL;
yoogx's avatar
yoogx committed
48 49 50 51
with Ocarina.Instances;          use Ocarina.Instances;
with Ocarina.Parser;             use Ocarina.Parser;
with Ocarina.Options;            use Ocarina.Options;
with Ocarina.Files;              use Ocarina.Files;
52
with Ocarina.Backends.Properties.Utils;
53 54 55

package body Ocarina.Utils is

yoogx's avatar
yoogx committed
56
   AADL_Root             : Node_Id := No_Node;
57
   File_Name             : Name_Id := No_Name;
yoogx's avatar
yoogx committed
58
   Buffer                : Location;
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
   Language              : Name_Id := No_Name;

   -----------
   -- Reset --
   -----------

   procedure Reset is
   begin
      --  Reset Ocarina

      Ocarina.Configuration.Reset_Modules;
      Ocarina.Reset;

      --  Initialize Ocarina

      Ocarina.AADL_Version := Get_Default_AADL_Version;
      AADL_Version         := Ocarina.AADL_V2;
      Ocarina.Initialize;
      Ocarina.Configuration.Init_Modules;

      --  Reset library

      AADL_Root := No_Node;
      File_Name := No_Name;
      Language := No_Name;
   end Reset;
yoogx's avatar
yoogx committed
85

86 87 88 89 90 91 92 93 94
   ------------------
   -- Print_Status --
   ------------------

   procedure Print_Status is
   begin
      Write_Line ("AADL version: " & Ocarina.AADL_Version'Img);
      Write_Line ("Library Path: "
                    & Get_Name_String (Default_Library_Path));
yoogx's avatar
yoogx committed
95
      Write_Line ("Load predefined property sets: "
yoogx's avatar
yoogx committed
96
                    & Ocarina.FE_AADL.Parser.Add_Pre_Prop_Sets'Img);
97 98
   end Print_Status;

yoogx's avatar
yoogx committed
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
   --------------------
   -- Load_AADL_File --
   --------------------

   procedure Load_AADL_File (Filename : String) is
   begin
      Language := Get_String_Name ("aadl");
      Set_Str_To_Name_Buffer (Filename);

      File_Name := Search_File (Name_Find);
      if File_Name = No_Name then
         Write_Line ("Cannot find file " & Filename);
         return;
      end if;

      Buffer := Load_File (File_Name);
      if File_Name = No_Name then
         Write_Line ("Cannot read file " & Filename);
         return;
      end if;

      AADL_Root := Parse (Language, AADL_Root, Buffer);
      Exit_On_Error
        (No (AADL_Root),
         "Cannot parse AADL specifications");
      Write_Line
        ("File " & Filename
           & " loaded and parsed sucessfully");
   end Load_AADL_File;

   -------------
   -- Analyze --
   -------------

133
   function Analyze return Boolean is
yoogx's avatar
yoogx committed
134 135 136 137 138 139 140 141
      Success : Boolean;
   begin
      Success := Analyze (Language, AADL_Root);
      if not Success then
         Write_Line ("Cannot analyze AADL specifications");
      else
         Write_Line ("Model analyzed sucessfully");
      end if;
142
      return Success;
yoogx's avatar
yoogx committed
143 144 145 146 147 148
   end Analyze;

   -----------------
   -- Instantiate --
   -----------------

yoogx's avatar
yoogx committed
149 150
   function Instantiate (Root_System : String) return Boolean is
      Success : Boolean;
yoogx's avatar
yoogx committed
151 152 153 154 155 156 157 158
   begin
      if Root_System /= "" then
         Root_System_Name := To_Lower
           (Get_String_Name (Root_System));
      end if;
      AADL_Root := Instantiate_Model (AADL_Root);
      if Present (AADL_Root) then
         Write_Line ("Model instantiated sucessfully");
yoogx's avatar
yoogx committed
159 160 161
         Success := True;
      else
         Success := False;
yoogx's avatar
yoogx committed
162
      end if;
yoogx's avatar
yoogx committed
163 164

      return Success;
yoogx's avatar
yoogx committed
165 166 167 168 169 170 171 172 173 174 175 176 177
   end Instantiate;

   --------------
   -- Generate --
   --------------

   procedure Generate (Backend_Name : String) is
   begin
      Set_Current_Backend_Name (Backend_Name);
      Write_Line ("Generating code using backend " & Backend_Name);
      Generate_Code (AADL_Root);
   end Generate;

178 179 180 181 182 183 184 185 186
   -------------------
   -- Get_AADL_Root --
   -------------------

   function Get_AADL_Root return Node_Id is
   begin
      return AADL_Root;
   end Get_AADL_Root;

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
   ------------------------
   -- Get_Property_Value --
   ------------------------

   procedure Get_Property_Value (Data : in out Callback_Data'Class;
                                 PropId : String; PropName : String)
   is
      Result : constant String_List :=
        Ocarina.Backends.Properties.Utils.Check_And_Get_Property
        (Get_Node_Id_From_String (PropId),
         Get_Node_Id_From_String (PropName));
   begin
      Set_Return_Value_As_List (Data);

      for Elt of Result loop
         Set_Return_Value (Data, Elt.all);
      end loop;
   end Get_Property_Value;

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
   --------------------------------
   -- Get_Property_Value_By_Name --
   --------------------------------

   procedure Get_Property_Value_By_Name (Data : in out Callback_Data'Class;
                                         PropId : String; PropName : String)
   is
      Result : constant String_List :=
        Ocarina.Backends.Properties.Utils.Check_And_Get_Property
        (Get_Node_Id_From_String (PropId),
         Get_String_Name (PropName));
   begin
      Set_Return_Value_As_List (Data);

      for Elt of Result loop
         Set_Return_Value (Data, Elt.all);
      end loop;
   end Get_Property_Value_By_Name;

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
   -----------------
   -- Get_Node_Id --
   -----------------

   procedure Get_Node_Id (Data : in out Callback_Data'Class;
      N : String) is
   begin
      Set_Return_Value (Data, Integer'Image (Integer
         (Namet.Get_String_Name (N))));
   end Get_Node_Id;

   -----------------------------
   -- Get_Node_Id_From_String --
   -----------------------------

   function Get_Node_Id_From_String (Name : String) return Node_Id is
   begin
      return Node_Id (Integer'Value (Name));
   end Get_Node_Id_From_String;

   -----------------------------
   -- Get_List_Id_From_String --
   -----------------------------

   function Get_List_Id_From_String (Name : String) return List_Id is
   begin
      return List_Id (Integer'Value (Name));
   end Get_List_Id_From_String;

   -----------------------------
   -- Get_Boolean_From_String --
   -----------------------------

   function Get_Boolean_From_String (Name : String) return Boolean is
   begin
      return Boolean'Value (Name);
   end Get_Boolean_From_String;

   --------------------------
   -- Get_Byte_From_String --
   --------------------------

   function Get_Byte_From_String (Name : String) return Byte is
   begin
      return Byte (Integer'Value (Name));
   end Get_Byte_From_String;

   -------------------------
   -- Get_Int_From_String --
   -------------------------

   function Get_Int_From_String (Name : String) return Int is
   begin
      return Int (Integer'Value (Name));
   end Get_Int_From_String;

   ------------------------------
   -- Get_Value_Id_From_String --
   ------------------------------

   function Get_Value_Id_From_String (Name : String) return Value_Id is
   begin
      return Value_Id (Integer'Value (Name));
   end Get_Value_Id_From_String;

   -----------------------------
   -- Get_Name_Id_From_String --
   -----------------------------

   function Get_Name_Id_From_String (Name : String) return Name_Id is
      val : Integer;
   begin
      val := Integer'Value (Name);
      if val > 300_000_000 and then val < 399_999_999 then
         return Name_Id (val);
      else
         return No_Name;
      end if;
   end Get_Name_Id_From_String;

305
end Ocarina.Utils;