59 lines
2 KiB
OCaml
59 lines
2 KiB
OCaml
open Graph
|
|
open Gfile
|
|
open Tools
|
|
|
|
(*same as fst but for 3 elements*)
|
|
let fst3 x = match x with
|
|
| (a,_,_) -> a
|
|
|
|
(*same as snd but for 3 elements*)
|
|
let snd3 = function
|
|
| (_, b, _ ) -> b
|
|
|
|
(*returns the 3rd value of a 3-elements tuple*)
|
|
let thrd3 = function
|
|
| (_, _, c ) -> c
|
|
|
|
|
|
(*takes in a list of (String : Name, int Id, int Capacity)
|
|
returns the corresponding graph (a sink with a node for each activity and an edge with the corresponding capacity)*)
|
|
let buildGraph xs =
|
|
let g = new_node empty_graph 0 (*Defines the end*)
|
|
in
|
|
let rec inner acc = function
|
|
| [] -> acc
|
|
| (_,id,capa) :: xs -> inner (add_arc (new_node acc id) id 0 capa) xs
|
|
in
|
|
|
|
inner g xs
|
|
|
|
let getMaximumNodeId g =
|
|
n_fold g (fun x id -> max x id) 0
|
|
|
|
(*Adds all the choices of the children to the graph (i.e. create a node for each and then an edge from the
|
|
node toward the desired node(s). This function also completes the graph by adding a starting node*)
|
|
let addChildren xxs g =
|
|
(*the id of the start is the maximum id + the number of added nodes + 1 to get the next maximum id *)
|
|
let startId = (getMaximumNodeId g) + (List.length xxs) + 1 in
|
|
(*We create the node*)
|
|
let g2 = new_node g startId in
|
|
|
|
(*goes over all the activities of a children*)
|
|
let rec activitiesIterator childNode acc = function
|
|
| [] -> acc
|
|
| x :: xs -> activitiesIterator childNode (add_arc acc childNode x 1) xs
|
|
in
|
|
|
|
(*tool function to create the node for said children and create the edge from the start to this node*)
|
|
let addChildrenNode g a =
|
|
let g1 = (new_node g a) in
|
|
add_arc g1 startId a 1
|
|
in
|
|
|
|
(*goes over all the children of the list*)
|
|
let rec childrenIterator acc = function
|
|
| [] -> acc
|
|
| (a,b,c) :: xs -> childrenIterator (activitiesIterator a (addChildrenNode acc a) c) xs
|
|
in
|
|
childrenIterator g2 xxs
|
|
|