Skip to content
This repository
tag: v1666
Fetching contributors…

Cannot retrieve contributors at this time

file 357 lines (264 sloc) 11.044 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

module List = BaseList
let (|>) = InfixOperator.(|>)

(*#load "/shared/base.cmo"*)


let mapStringSet f set = StringSet.fold (fun v acc -> StringSet.add (f v) acc) set StringSet.empty ;;

let toString = Printf.sprintf ;;


(* DOC *)
type node = string
type nodeSet = StringSet.t
type nodeMap = nodeSet StringMap.t

let rec stringset_of_list l =
  List.fold_left (fun acc v-> StringSet.add v acc) StringSet.empty l

let rec stringmap_of_list l =
  List.fold_left (fun acc (key,v)-> StringMap.add key v acc) StringMap.empty l

let rec fix_point compare show f x =
  (*Printf.printf "------------------------\n";*)
  show x;
  let x' = f x in if (compare x x')=0 then x' else fix_point compare show f x'
;;



(* EXPANSION DE DEPENDANCES *)
type deps_dones_nexts = nodeSet * nodeSet * nodeSet

let init1_ddn node deps =
  let dones = StringSet.singleton node in
  (deps, StringSet.singleton node, (StringSet.diff deps dones))

let init_ddns list =
  (
    list
  |> List.map (fun (key,deps)-> key, (init1_ddn key ( stringset_of_list deps)))
  |> stringmap_of_list
  )


let init_set_ddns list =
  (
    list
  |> List.map (fun (key,deps)-> key, (init1_ddn key (deps)))
  |> stringmap_of_list
  )



let get_deps ddn node = let (deps,_,_)=StringMap.find node ddn in deps

let show ddn =
  StringMap.iter ( fun key (_deps,dones,nexts) ->
Printf.printf " %s : " key;
StringSet.iter (fun v-> Printf.printf " %s " v ) (get_deps ddn key);
Printf.printf " \t\t\t\t\t | ";
StringSet.iter (fun v-> Printf.printf " %s " v ) dones;
Printf.printf " | ";
StringSet.iter (fun v-> Printf.printf " %s " v ) nexts;
Printf.printf "\n\n";
) ddn ;;

let compare_ddn (a,b,_c) (a',b',_c') =
  (StringSet.compare a a')*100 + (StringSet.compare b b')*10 + (StringSet.compare b b')*1

let compare_map_ddn m m' = StringMap.compare compare_ddn m m'
;;

(* les dépendances de mes dépendances sont mes dépendances !!! *)
(* les dépendances à traiter sont dans nexts *)
(* on evite de resuivre les dépendances déjà traités (dones) *)
let expand_1_ddn ddn (deps,dones,nexts) =
  let ddeps = StringSet.fold (fun v acc -> StringSet.union (get_deps ddn v) acc) nexts StringSet.empty in
    (*Printf.printf " [ "; StringSet.iter (fun v-> Printf.printf " %s " v ) nexts; Printf.printf " -> "; StringSet.iter (fun v-> Printf.printf " %s " v ) ddeps; Printf.printf " ]\n ";*)
  let dones = StringSet.union dones nexts in
  let nexts = StringSet.diff ddeps dones in
  let deps = StringSet.union deps nexts in
    (deps,dones,nexts)


let expand_map_ddn m = StringMap.map (expand_1_ddn m) m ;;

let complete_expansion deps_list =
  let show = (*show *) fun _ -> () in
  let ddn_final = fix_point compare_map_ddn show expand_map_ddn deps_list
  in StringMap.map ( fun (deps,_,_)-> deps ) ddn_final

(* FIN EXPANSION DE DEPENDANCES *)



(* PARTIES FORTEMENT CONNEXES *)
let get_deps deps node = try StringMap.find node deps with Not_found -> assert(false)

let get_strong_neigbours deps node =
  (
    (get_deps deps node)
  |> StringSet.filter (fun n-> StringSet.mem node (get_deps deps n) )
  |> StringSet.remove node
  )
;;

let get_many_strong_neigbours deps nodes =
  StringSet.fold (fun n acc -> StringSet.union (get_strong_neigbours deps n) acc ) nodes nodes
;;

let get_connex_part deps node =
  fix_point StringSet.compare (fun _->()) (get_many_strong_neigbours deps) (StringSet.singleton node) ;;

(* FIN PARTIES FORTEMENT CONNEXES *)





(*
let connext_part_decomposition ddns =
let (dones,list_part) as acc = (StringSet.empty,[]) in
StringMap.fold (fun v acc ->
if not (StringSet.mem dones) then (
let part= get_connex_part ddns v in
(StringSet. ,part:acc)
) else acc
) ddns
*)


let rec getTopoOrder1 deps cur (dones,order)=
  (*Printf.printf "\t\tGeneral : Try to add %s \n" cur;*)
  let (-) = StringSet.diff in
  let (+) = StringSet.union in
  if StringSet.mem cur dones then (dones,order)
  else
    let undones_deps = (try (StringMap.find cur deps) with Not_found -> failwith "getTopoOrder1 ") - (dones + (StringSet.singleton cur)) in
      if StringSet.is_empty undones_deps
      then (
(*Printf.printf "\t All dependencies Ok, adding %s \n" cur;*)
(StringSet.add cur dones , cur::order)

      ) else (
let (dones,order) = StringSet.fold (fun cur acc_order-> getTopoOrder1 deps cur acc_order) undones_deps (dones,order) in
getTopoOrder1 deps cur (dones,order)
      )
;;

(* ordre topologique à partir d'une liste de nom, permet de spécifier l'ordre des éléments *)
(* contient éventuellement des doublons *)
let getTopoOrder deps =
  (
    StringMap.fold (fun v _ acc ->
(*Printf.printf "Primary : Try to add %s" v;*)
getTopoOrder1 deps v acc) deps (StringSet.empty,[])
  |> snd |> List.rev
  )
;;

let getTopoOrderOfList list deps =
  (
    List.fold_left (fun acc v -> (*Printf.printf "Primary : Try to add %s\n" v;*)getTopoOrder1 deps v acc) (StringSet.empty,[]) list
  |> snd |> List.rev
  )
;;





let contactSetName set =
  if StringSet.cardinal set = 1 then StringSet.choose set
  else
    let buf=Buffer.create 10 in
    let _ = Buffer.add_string buf "group" in
      StringSet.iter (fun n -> Buffer.add_string buf (toString "_%s" n)) set;
      (Buffer.contents buf) ;;



(* *)
let majAccIncontructGroups deepDeps name _ (alreadyDones, groups, nameToGroupN) =
  if StringSet.mem name alreadyDones then (
    (*Printf.printf "Already done : %s \n" name;*)
    (alreadyDones, groups, nameToGroupN)

  )
  else (
    (*Printf.printf "Adding group of : %s \n" name;*)
    let (++) = StringSet.union in
      (* contenu du groupe *)
    let group = get_connex_part deepDeps name in
      (* nom du groupe, par défaut les fonctions du groupe *)
    let gN = contactSetName group in
      (* on ajoute la correspondance des noms du groupe à ce numéro de groupe *)
    let nameToGroupN = StringSet.fold (fun name acc -> StringMap.add name gN acc) group nameToGroupN in

      ( alreadyDones ++ group, StringMap.add gN group groups, nameToGroupN )

  )
;;

(* renvoie n groupe + les dépendances par groupe *)
(* avec ordre préférentiel de la liste *)
let contructGroups deepDeps val_list =
  let (<+) m (k,v) = StringMap.add k v m in
  let init_state = (StringSet.empty,StringMap.empty,StringMap.empty) in
  let (_,groups,nameToGroupN) =
    StringMap.fold (majAccIncontructGroups deepDeps) deepDeps init_state
      (*List.fold_left (majAccIncontructGroups deepDeps) init_state name_list*)
  in
    (* Les groupes ... *)
    (*StringMap.iter (fun gname group->
Printf.printf "Group % s: " gname;
StringSet.iter (fun v-> Printf.printf " %s " v ) group;
Printf.printf "\n";
(*Printf.printf " depends on externals : " ;
StringSet.iter (fun v-> Printf.printf " %s " v ) (StringSet.diff (get_deps deepDeps gname) group);
Printf.printf "\n";*)
Printf.printf " depends on Groups : " ;
StringSet.iter (fun v-> Printf.printf " %s " v ) (StringSet.diff (mapStringSet (fun n-> StringMap.find n nameToGroupN) (get_deps deepDeps gname)) (StringSet.singleton gname));
Printf.printf "\n";
) groups ; *)
    (* Constructions des dépendances entre groupes *)

    let groupsDeps = StringMap.fold (fun gname _ acc ->
(*Printf.printf "+ G %s\n" gname;*)
acc <+ (gname,
let fun_of_group = (StringMap.find gname groups) |> StringSet.choose in
(get_deps deepDeps fun_of_group (*dépendance en terme de fonction*)
|> mapStringSet (fun n-> StringMap.find n nameToGroupN) (*vers dependance en terme de groupe*))
)
) groups StringMap.empty in
    let groupsOrder = val_list |> List.map (fun n -> StringMap.find n nameToGroupN) in
    getTopoOrderOfList groupsOrder groupsDeps |> List.map (fun gname -> (*Printf.printf "G%s\n" gname;*) gname, StringMap.find gname groups)
(*(get_deps deepDeps node)*)
;;



let show_deps deps =
  Printf.printf "-------------\n Dependances (format Makefile) : \n";
  List.iter (fun (n,ln) ->
Printf.printf "%s : " n;
List.iter (Printf.printf "%s ") ln;
Printf.printf "\n";
) deps;
  Printf.printf "------------- Fin Dependances\n"
;;

let show_deps_set deps =
  Printf.printf "-------------\n Dependances (format Makefile) : \n";
  List.iter (fun (n,ln) ->
Printf.printf "%s : " n;
if StringSet.cardinal ln=0 then Printf.printf "NONE" else StringSet.iter (Printf.printf "%s ") ln;
Printf.printf "\n";
) deps;
  Printf.printf "------------- Fin Dependances\n"
;;

let show_deps_expanded deps =
  Printf.printf "-------------\n Dependances (format Makefile) : \n";
  StringMap.iter (fun n ln ->
Printf.printf "%s : " n;
if StringSet.cardinal ln=0 then Printf.printf "NONE" else StringSet.iter (Printf.printf "%s ") ln;
Printf.printf "\n";
) deps;
  Printf.printf "------------- Fin Dependances\n"
;;





let create_group_list deps_set=
  let ddns = init_set_ddns deps_set in
  let final = complete_expansion ddns in
  contructGroups final (List.map fst deps_set),final
;;

let show_groups gs=
  List.iteri (fun (gname,group) i ->
Printf.printf "Groupe %d = Groupe %s = { " i gname;
StringSet.iter (fun v-> Printf.printf " %s " v ) group;
Printf.printf "}\n";
) gs;;



(*
let test_deps = [
("A",["A";"B"]);
("B",["C";"E"]);
("C",["A"]);
("D",["C"]);
("E",[]);
("F",["D"])
]
;;

show_deps test_deps;;

let test = init_ddns test_deps ;;



let final = complete_expansion test ;;

(*
StringMap.iter (fun node _ ->
Printf.printf " %s :(Mergeable in \"let rec and\" with): " node;
StringSet.iter (fun v-> Printf.printf " %s " v ) (get_strong_neigbours final node);
Printf.printf "\n";
Printf.printf " %s :(getConnexPart): " node;
StringSet.iter (fun v-> Printf.printf " %s " v ) (get_many_strong_neigbours final (StringSet.singleton node)) (*get_connex_part final node *);
Printf.printf "\n";
) final ;;
*)

let gs = contructGroups final;;


List.iteri (fun (gname,group) i ->
Printf.printf "Groupe %d = Groupe %s = { " i gname;
StringSet.iter (fun v-> Printf.printf " %s " v ) group;
Printf.printf "}\n";
) gs;;





let depsListToGroups deps = init_ddns deps |> complete_expansion |> contructGroups ;;
*)


Something went wrong with that request. Please try again.