with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Command_Line; use Ada.Command_Line; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_IO.Text_Streams; with Vector; with Google_Naive; with Google_Creux; procedure pageRank is ERROR_args: Exception; ERROR_alpha: Exception; ERROR_ite: Exception; ERROR_filename: Exception; INFO_tips: Exception; INFO_help: Exception; -- définition du type T_Double Type T_Double is digits 18; -- on utilise le module générique Float_IO pour pouvoir afficher T_Double directement package Text_T_Double is new Ada.Text_IO.Float_IO(Num => T_Double); use Text_T_Double; -- procédure pour récupérer les arguments de la ligne de commande procedure get_args(filename: in out Unbounded_String; ite_max: in out Natural; alpha: in out T_Double; naif: in out Boolean) is i: Natural := 1; begin -- on affiche tous les arguments put("Argument_Count = "); put(Argument_Count, 1); new_line; for i in 1..Argument_Count loop Put("Argument("); Put(i, 0); Put(") = "); Put_line(Argument(i)); end loop; new_line; -- on vérifie d'abord que le nombre d'arguments est cohérent if not(0 < Argument_Count and Argument_Count <= 7) then raise ERROR_args; else -- sinon on parse les arguments loop if Argument(i) = "-h" or Argument(i) = "--help" then put_line("parsed help"); raise INFO_help; elsif Argument(i) = "-n" or Argument(i) = "--naif" then naif := True; i := i + 1; put_line("parsed naif"); elsif Argument(i) = "-a" or Argument(i) = "--alpha" then alpha := T_Double'Value(Argument(i+1)); if alpha < 0.0 or alpha > 1.0 then raise ERROR_alpha; end if; i := i + 2; put_line("parsed alpha"); elsif Argument(i) = "-i" or Argument(i) = "--ite-max" then ite_max := Natural'Value(Argument(i+1)); if ite_max > 150 then raise ERROR_ite; end if; i := i + 2; put_line("parsed ite_max"); elsif Argument(i)'Length > 3 and then Argument(i)(Argument(i)'Last-3 .. Argument(i)'Last) = ".net" then filename := To_Unbounded_String(Argument(i)(Argument(i)'First .. Argument(i)'Last-4)); i := i + 1; put_line("parsed filename"); else new_line; put("Argument: '"); put(Argument(i)); put("' non reconnu"); new_line; raise INFO_tips; end if; exit when i > Argument_Count; end loop; if Length(filename) = 0 then raise ERROR_filename; end if; end if; new_line; put("alpha = "); put(alpha, Fore=>1, Aft=>10); new_line; put("naif = "); put(Boolean'Pos(naif), 1); new_line; put("ite_max = "); put(ite_max, 1); new_line; put("filename = "); put_line(To_String(filename)); new_line; exception when CONSTRAINT_ERROR => if Argument(i) = "-a" or Argument(i) = "--alpha" then raise ERROR_alpha; elsif Argument(i) = "-i" or Argument(i) = "--ite-max" then raise ERROR_ite; else put_line("Unexpected contrain_error"); raise ERROR_args; end if; end get_args; -- procédure pour choisir le type d'algo, une fois N et les arguments récupérés procedure type_algo(N: Positive; N_links: Positive; filename: in Unbounded_String; file: in out Ada.Text_IO.File_Type; alpha: T_Double; ite_max: Natural; naif: Boolean) is -- on instancie le module générique Vecteur package Vector_T_Double is new Vector(T_Element => T_Double, N => N, N_links => N_links); use Vector_T_Double; -- pour le retour chariot stdout: constant Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output; -- procédure qui effectue l'algorithme du pageRank avec Google_Naive procedure algorithm_naif(file: in out Ada.Text_IO.File_Type; alpha: in T_Double; ite_max: in Natural; pi: out T_Vecteur_Element) is -- on instancie le module générique Naif de Google package Google is new Google_Naive(T_Element => T_Double, N => N, N_links => N_links, Vector_T_Element => Vector_T_Double); use Google; -- définition de la matrice Google G: T_Google; begin initialize(pi, 1.0/T_Double(N)); put_line("initialized pi"); -- put(pi); new_line; initialize(G); put_line("initialized G"); -- put(G); new_line; create_H(G, file); put_line("created H"); close(file); -- put(G); new_line; create_S(G); put_line("created S"); -- put(G); new_line; create_G(G, alpha); put_line("created G"); -- put(G); new_line; -- on applique l'algorithme itératif for i in 1..ite_max loop pi := pi * G; String'Write(Ada.Text_IO.Text_Streams.Stream(stdout), "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max) & ASCII.CR); end loop; new_line; -- new_line; -- put_line("final pi:"); -- put(pi); end algorithm_naif; -- procédure qui effectue l'algorithme du pageRank avec Google_Creux procedure algorithm_creux(file: in out Ada.Text_IO.File_Type; alpha: in T_Double; ite_max: in Natural; pi: out T_Vecteur_Element) is -- on instancie le module générique Creux de Google package Google is new Google_Creux(T_Element => T_Double, N => N, N_links => N_links, Vector_T_Element => Vector_T_Double); use Google; -- définition de la matrice Google H: T_Google; begin initialize(pi, 1.0/T_Double(N)); put_line("initialized pi:"); -- put(pi); new_line; create_H(H, file); put_line("created H:"); -- put(H); new_line; new_line; -- on applique l'algorithme itératif for i in 1..ite_max loop pi := calcul(pi, H, alpha); String'Write(Ada.Text_IO.Text_Streams.Stream(stdout), ASCII.CR & "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max)); end loop; new_line; -- new_line; -- put_line("final pi:"); -- put(pi); end algorithm_creux; -- procédure pour écrire les résultats dans les fichiers procedure write_to_files(filename: in Unbounded_String; pi_sorted: in T_Vecteur_Element; pi_index: in T_Vecteur_Natural) is file: Ada.Text_IO.File_Type; begin create(file, Out_File, To_String(filename & "_GH05.p")); put(file, N, 1); put(file, ' '); put(file, alpha, Fore=>1, Aft=>10); put(file, ' '); put(file, ite_max, 1); new_line(file); put(file, pi_sorted); close(file); create(file, Out_File, To_String(filename & "_GH05.ord")); put(file, pi_index); close(file); end write_to_files; -- définition des vecteurs pi: T_Vecteur_Element; pi_index: T_Vecteur_Natural; begin if naif then algorithm_naif(file, alpha, ite_max, pi); else algorithm_creux(file, alpha, ite_max, pi); end if; -- on trie les poids par ordre décroissant, on tri en même temps les indices des pages initialize(pi_index); sort_insert_desc(pi, pi_index); -- new_line; -- put_line("sorted pi:"); -- put(pi); new_line; -- on écrit les resultats dans des fichiers write_to_files(filename, pi, pi_index); end type_algo; -- définition des arguments filename: Unbounded_String; ite_max: Natural := 150; naif: Boolean := False; alpha: T_Double := 0.85; -- définition des variables pour créer les matices/vecteurs N: Positive; N_links: Integer := -1; file: Ada.Text_IO.File_Type; begin -- on récupère les arguments de la ligne de commande get_args(filename, ite_max, alpha, naif); put_line("args OK"); -- on ouvre le fichier .net put("ouverture de: "); put(To_String(filename & ".net")); new_line; open(file, In_File, To_String(filename & ".net")); put_line("file OK"); -- on récupère le nombre de liens while not End_Of_File(file) loop Skip_Line(file); N_links := N_links + 1; end loop; reset(file, In_File); put("N_links = "); put(N_links, 1); new_line; -- on récupère le nombre de pages get(file, N); put("N = "); put(N, 1); new_line; -- on peut maintenant choisir le type de matrice que l'on souhaite type_algo(N, N_links, filename, file, alpha, ite_max, naif); exception -- si il y a une erreur lors du parsing des arguments, on rappelle l'usage. when ERROR_args => new_line; put_line("Erreur lors de la saisi de la commande."); put_line("Usage: pagerank [-P] [-i max_iterations] [-a alpha] [-h] fichier_reseau.net"); when ERROR_alpha => new_line; put_line("Erreur lors de la saisi de alpha."); put_line("alpha ∈ [0, 1]"); when ERROR_ite => new_line; put_line("Erreur lors de la saisi de ite_max."); put_line("ite_max ∈ ⟦0, 150⟧"); when ERROR_filename => new_line; put_line("Erreur lors de la saisi du fichier réseau."); put_line("Veuillez rentrer un nom valide."); when INFO_tips => put_line("Essayez 'pagerank --help' pour plus d'informations."); when INFO_help => new_line; put_line("Usage: pagerank [OPTIONS] network.net"); put_line("Calcule le pagerank d'un réseau à partir de son réseau.net"); new_line; put_line("Options:"); put_line(" -P, --naif specifies which type of matrix to use"); put_line(" -a, --alpha specifies the alpha constant (alpha ∈ [0, 1])"); put_line(" -i, --ite-max specifies the maximum number of iterations (ite_max ∈ ⟦0, 150⟧)"); put_line(" -h, --help display this help message and exit"); end pageRank;