Skip to content

Commit

Permalink
* Simplify code structure
Browse files Browse the repository at this point in the history
        For #111
  • Loading branch information
yoogx committed Jan 3, 2018
1 parent b20d54f commit 4368f99
Showing 1 changed file with 45 additions and 55 deletions.
100 changes: 45 additions & 55 deletions src/backends/alloy/ocarina-backends-alloy.adb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- --
-- B o d y --
-- --
-- Copyright (C) 2014-2016 ESA & ISAE. --
-- Copyright (C) 2014-2018 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- --
Expand All @@ -29,16 +29,18 @@
-- --
------------------------------------------------------------------------------

with Ada.Text_IO;

with Charset; use Charset;
with Ocarina.Namet;
with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.Instances; use Ocarina.Instances;
with Ocarina.ME_AADL.AADL_Instances.Entities;
use Ocarina.ME_AADL.AADL_Instances.Entities;

with Ocarina.Backends.Helper;
with Ocarina.Backends.Utils;
with Ada.Text_IO;

package body Ocarina.Backends.Alloy is

Expand All @@ -47,14 +49,16 @@ package body Ocarina.Backends.Alloy is
use Ocarina.Namet;
use Ada.Text_IO;
use Ocarina.ME_AADL;
use Ocarina.ME_AADL.AADL_Instances.Entities;
use Ocarina.ME_AADL.AADL_Instances.Nutils;
use Ocarina.Backends.Helper;
use Ocarina.Backends.Utils;
use AIN;

procedure Visit (E : Node_Id);

procedure Visit_Architecture_Instance (E : Node_Id);

procedure Visit_Component_Instance (E : Node_Id);
procedure Visit_Subcomponents_Of is new Visit_Subcomponents_Of_G (Visit);

FD : File_Type;
Root_System_Name : Name_Id;
Expand Down Expand Up @@ -111,7 +115,9 @@ package body Ocarina.Backends.Alloy is

Category : constant Component_Category := Get_Category_Of_Component (E);

T : Node_Id;
E_Subcomponents : constant Node_Array := Subcomponents_Of (E);
E_Properties : constant Node_Array := Properties_Of (E);

begin
-- Create Alloy component

Expand Down Expand Up @@ -146,21 +152,20 @@ package body Ocarina.Backends.Alloy is
-- Rule #2: list subcomponents

Put (FD, ASCII.HT & "subcomponents=");
if Present (Subcomponents (E)) then
T := First_Node (Subcomponents (E));
while Present (T) loop

if E_Subcomponents'Length > 0 then
for J in E_Subcomponents'Range loop
declare
Subcomponent_Name : constant String :=
To_Lower
(Get_Name_String
(Normalize_Name
(Fully_Qualified_Instance_Name
(Corresponding_Instance (T)))));
(Corresponding_Instance (E_Subcomponents (J))))));
begin
Put (FD, Subcomponent_Name);

T := Next_Node (T);
if Present (T) then
if J < E_Subcomponents'Last then
Put (FD, "+");
end if;
end;
Expand All @@ -174,16 +179,17 @@ package body Ocarina.Backends.Alloy is
-- Rule #3: list properties

Put (FD, ASCII.HT & "properties=");
if Present (AIN.Properties (E)) then
T := First_Node (AIN.Properties (E));
while Present (T) loop

if E_Properties'Length > 0 then
for J in E_Properties'Range loop
Put
(FD,
To_Lower
(Get_Name_String
(Normalize_Name (Display_Name (Identifier (T))))));
T := Next_Node (T);
if Present (T) then
(Normalize_Name (Display_Name (Identifier
(E_Properties (J)))))));

if J < E_Properties'Last then
Put (FD, "+");
end if;
end loop;
Expand All @@ -200,14 +206,8 @@ package body Ocarina.Backends.Alloy is

-- Iterate over subcomponents

if Present (Subcomponents (E)) then
T := First_Node (Subcomponents (E));
while Present (T) loop
Visit (Corresponding_Instance (T));
Visit_Subcomponents_Of (E);

T := Next_Node (T);
end loop;
end if;
end Visit_Component_Instance;

----------
Expand All @@ -226,16 +226,13 @@ package body Ocarina.Backends.Alloy is
--------------

procedure Generate (AADL_Root : Node_Id) is
Instance_Root : Node_Id;

begin
-- Instantiate the AADL tree
Instance_Root : constant Node_Id := Instantiate_Model (AADL_Root);

Instance_Root := Instantiate_Model (AADL_Root);
if No (Instance_Root) then
raise Program_Error;
end if;
Root_Subcomponents : constant Node_Array := Subcomponents_Of
(Root_System (Instance_Root));

begin
-- Open a new .als file

Create (File => FD, Name => "con_model.als");
Expand Down Expand Up @@ -271,45 +268,38 @@ package body Ocarina.Backends.Alloy is

declare
Print_Subcomponents : Boolean := True;
E : constant Node_Id := Root_System (Instance_Root);
T : Node_Id;
begin
-- We consider two patterns
-- a) system with subcomponents as system/bus/device only
-- b) general case

if Present (Subcomponents (E)) then
T := First_Node (Subcomponents (E));
while Present (T) loop
Print_Subcomponents :=
Print_Subcomponents
and then
(Get_Category_Of_Component (T) = CC_System
or else Get_Category_Of_Component (T) = CC_Device
or else Get_Category_Of_Component (T) = CC_Bus);
T := Next_Node (T);
end loop;
end if;
for Sub of Root_Subcomponents loop
Print_Subcomponents :=
Print_Subcomponents
and then
(Get_Category_Of_Component (Sub) = CC_System
or else Get_Category_Of_Component (Sub) = CC_Device
or else Get_Category_Of_Component (Sub) = CC_Bus);
end loop;

-- We are in case a), generate all subcomponents of root system

if Print_Subcomponents then
Put (FD, ASCII.HT & "output=");
if Present (Subcomponents (E)) then
T := First_Node (Subcomponents (E));
while Present (T) loop
if Root_Subcomponents'Length > 0 then
for J in Root_Subcomponents'Range loop
declare
Subcomponent_Name : constant String :=
To_Lower
(Get_Name_String
(Normalize_Name
(Fully_Qualified_Instance_Name
(Corresponding_Instance (T)))));
(Get_Name_String
(Normalize_Name
(Fully_Qualified_Instance_Name
(Corresponding_Instance
(Root_Subcomponents (J))))));
begin
Put (FD, Subcomponent_Name);

T := Next_Node (T);
if Present (T) then
if J < Root_Subcomponents'Last then
Put (FD, "+");
end if;
end;
Expand Down

0 comments on commit 4368f99

Please sign in to comment.