--------------------------------------------- ----------------------------------- -- -- -- OCARINA COMPONENTS -- -- -- -- O C A R I N A . D I A . P A R S E R . C O R E . N O D E S -- -- -- -- B o d y -- -- -- -- Copyright (C) 2005-2007, GET-Telecom Paris. -- -- -- -- Ocarina is free software; you can redistribute it and/or modify -- -- it under terms of the GNU General Public License as published by the -- -- Free Software Foundation; either version 2, or (at your option) any -- -- later version. 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. See the GNU General -- -- Public License for more details. You should have received a copy of the -- -- GNU General Public License distributed with Ocarina; see file COPYING. -- -- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth -- -- Floor, Boston, MA 02111-1301, USA. -- -- -- -- As a special exception, if other files instantiate generics from this -- -- unit, or you link this unit with other files to produce an executable, -- -- this unit does not by itself cause the resulting executable to be -- -- covered by the GNU General Public License. This exception does not -- -- however invalidate any other reasons why the executable file might be -- -- covered by the GNU Public License. -- -- -- -- Ocarina is maintained by the Ocarina team -- -- (ocarina-users@listes.enst.fr) -- -- -- ------------------------------------------------------------------------------ -- This package contains the definition of the different nodes that may -- appear in a Dia implementation with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_IO; use Ada.Text_IO; with DOM.Core.Nodes; with Ocarina.Dia.Parser.Core.Ports; with Ocarina.Dia.Parser.Core.Connections; with Ocarina.Dia.Parser.Core.Attributes; with Ocarina.Dia.Parser.Translate.Misc; package body Ocarina.Dia.Parser.Core.Nodes is -- List : Item function Item (List : node_list; Index : Integer) return node is begin if Index <= List.Last then return List.Items (Index); else return null; end if; end Item; -- List : Length function Length (List : node_list) return Integer is begin return List.Last + 1; end Length; -- Prints a node type function Print_Node_Type (Node_Type_Arg : node_type) return String is begin case Node_Type_Arg is when process => return "Process"; when thread => return "Thread"; when data => return "Data"; when processor => return "Processor"; when memory => return "Memory"; when bus => return "Bus"; when system => return "System"; when subprogram => return "Subprogram"; when thread_group => return "Thread_Group"; when device => return "Device"; when link => return "Link"; when void => return "Void"; end case; end Print_Node_Type; -- Prints a node to the screen (for debugging purposes) procedure Print_Node (N : node) is use Ocarina.Dia.Parser.Core.Ports; begin if N = null then Put_Line (""); else Put_Line ("--- Node ---"); Put ("> ID : "); Put_Line (To_String (N.ID)); Put ("> Version : "); Put_Line (Integer'image (N.Version)); Put_Line ("> Type : " & Print_Node_Type (N.Node_Type_Arg)); case N.Node_Type_Arg is when aadlobject_type => Put_Line ("> Name : " & To_String (N.Name)); Put_Line ("> Declaration : " & To_String (N.Declaration)); Put_Line ("> Ports : "); Print_Ports (N.Ports); Put_Line ("> Parent Node : " & To_String (N.Parent)); when link => Put_Line ("> Start Point : (" & To_String (N.Conn.Start_Point.Object_ID) & ", " & Integer'image (N.Conn.Start_Point.Port) & ")"); Put_Line ("> End Point : (" & To_String (N.Conn.End_Point.Object_ID) & ", " & Integer'image (N.Conn.End_Point.Port) & ")"); Put_Line ("> Style : " & Integer'image (N.Style)); Put_Line ("> Head Styles : (" & Integer'image (N.Start_Head_Style) & " ; " & Integer'image (N.End_Head_Style) & ")"); when others => null; end case; Put_Line ("------------"); end if; end Print_Node; -- returns the Node_Type associated with S; raises Not_Matching if none function Match_Type (S : String) return node_type is begin if S = "AADL - Process" then return process; elsif S = "AADL - Thread" then return thread; elsif S = "AADL - Data" then return data; elsif S = "AADL - Processor" then return processor; elsif S = "AADL - Memory" then return memory; elsif S = "AADL - Bus" then return bus; elsif S = "AADL - System" then return system; elsif S = "AADL - Subprogram" then return subprogram; elsif S = "AADL - Thread Group" then return thread_group; elsif S = "AADL - Device" then return device; elsif S = "Standard - Line" or else S = "Standard - Arc" or else S = "Standard - ZigZagLine" or else S = "Standard - BezierLine" then return link; else Put_Line (S); raise Not_Matching; end if; end Match_Type; -- Reads from the XML/DIA document and returns the next AADL object function Get_Node (XMLN : DOM.Core.node; Id : Natural) return node is use DOM.Core; use Ocarina.Dia.Parser.Core.Connections; use Ocarina.Dia.Parser.Core.Attributes; use Ocarina.Dia.Parser.Core.Ports; pragma unreferenced (Id); N : node; begin if XMLN.Node_Type = element_node and then DOM.Core.Nodes.Node_Name (XMLN) = "dia:object" then declare AS : DOM.Core.named_node_map := DOM.Core.Nodes.Attributes (XMLN); NT : node_type; Children : constant DOM.Core.node_list := DOM.Core.Nodes.Child_Nodes (XMLN); E : DOM.Core.node; begin NT := Match_Type (Get_Required_Attribute (AS, "type")); N := new node_record (NT); -- id begins with an 'O' and not a '0' => we keep it as a string N.ID := To_Unbounded_String (Get_Required_Attribute (AS, "id")); N.Version := Integer'value (Get_Required_Attribute (AS, "version")); -- Retrieve the list of attributes case NT is when aadlobject_type => -- Name E := Get_Attribute_Node (Children, "name"); N.Name := Get_Name_Attribute (E); -- Declaration E := Get_Attribute_Node (Children, "declaration"); N.Declaration := Get_String_Attribute (E); -- Ports N.Ports := Get_Ports (Children); -- Connection Points -- Parent Node declare P : Natural := 0; begin Get_Required_Child (Children, "dia:childnode", P, E); AS := DOM.Core.Nodes.Attributes (E); N.Parent := To_Unbounded_String (Get_Required_Attribute (AS, "parent")); exception when Missing_Child => null; end; when link => declare E : DOM.Core.node; P : Natural := 0; begin -- Connections Get_Required_Child (Children, "dia:connections", P, E); N.Conn := Get_Connection (E); begin -- Style E := Get_Attribute_Node (Children, "line_style"); N.Style := Integer'value (To_String (Get_Val_Attribute (E))); exception when Missing_Dia_Attribute => N.Style := 0; end; -- Heads' Styles begin E := Get_Attribute_Node (Children, "start_arrow"); N.Start_Head_Style := Integer'value (To_String (Get_Val_Attribute (E))); exception when Missing_Dia_Attribute => N.Start_Head_Style := 0; end; begin E := Get_Attribute_Node (Children, "end_arrow"); N.End_Head_Style := Integer'value (To_String (Get_Val_Attribute (E))); exception when Missing_Dia_Attribute => N.End_Head_Style := 0; end; end; when others => null; end case; return N; end; else return null; end if; exception when Missing_Attribute => return null; end Get_Node; -- get the name of the node N function Get_Node_Name (N : node) return String is use Ocarina.Dia.Parser.Translate.Misc; begin return Node_Name (To_String (N.Name)); end Get_Node_Name; function Count_Ports (N : node) return Natural is Ports : port_list := N.Ports; Compteur : Natural := 0; begin while Ports /= null loop case Ports.Hd.Port_Type_Arg is when real_port => Compteur := Compteur + 1; when others => null; end case; Ports := Ports.Tl; end loop; return Compteur; end Count_Ports; -- returns the number of conections of a node function Count_Connections (N : node) return Natural is Ports : port_list := N.Ports; Compteur : Natural := 0; begin while Ports /= null loop case Ports.Hd.Port_Type_Arg is when point => Compteur := Compteur + 1; when others => null; end case; Ports := Ports.Tl; end loop; return Compteur; end Count_Connections; -- Returns the Id of the node function Get_Node_Id (N : node) return Natural is begin return Natural'value (To_String (N.ID)); end Get_Node_Id; -- Returns the Node corresponding to the Id function Get_Node_By_Id (Id : Unbounded_String; List : node_list) return node is N : node := null; begin for I in 0 .. (Length (List) - 1) loop N := Item (List, I); if N.ID = Id then return N; end if; end loop; return null; end Get_Node_By_Id; ------------------------------ -- Has_Implementation_Arrow -- ------------------------------ function Has_Implementation_Arrow (N : node; List : node_list) return node is O : node; begin for I in 0 .. (Length (List) - 1) loop O := Item (List, I); if O.Node_Type_Arg = link and then O.Style = 4 and then O.Start_Head_Style = 0 and then O.End_Head_Style = 2 and then O.Conn.Start_Point.Object_ID = N.ID then return Get_Node_By_Id (O.Conn.End_Point.Object_ID, List); end if; end loop; return null; end Has_Implementation_Arrow; end Ocarina.Dia.Parser.Core.Nodes;