diff --git a/src/gfile.ml b/src/gfile.ml index 2d8aee7..9599cc3 100644 --- a/src/gfile.ml +++ b/src/gfile.ml @@ -45,9 +45,14 @@ let read_node id graph line = Printf.printf "Cannot read node in line - %s:\n%s\n%!" (Printexc.to_string e) line ; failwith "from_file" +(* Ensure that the given node exists in the graph. If not, create it. + * (Necessary because the website we use to create online graphs does not generate correct files when some nodes have been deleted.) *) +let ensure graph id = if node_exists graph id then graph else new_node graph id + (* Reads a line with an arc. *) let read_arc graph line = - try Scanf.sscanf line "e %d %d %s" (fun id1 id2 label -> new_arc graph id1 id2 label) + try Scanf.sscanf line "e %d %d %s" + (fun id1 id2 label -> new_arc (ensure (ensure graph id1) id2) id1 id2 label) with e -> Printf.printf "Cannot read arc in line - %s:\n%s\n%!" (Printexc.to_string e) line ; failwith "from_file" diff --git a/src/graph.mli b/src/graph.mli index 626ce55..416e158 100644 --- a/src/graph.mli +++ b/src/graph.mli @@ -17,9 +17,10 @@ val empty_graph: 'a graph * @raise Graph_error if the id already exists. *) val new_node: 'a graph -> id -> 'a graph -(* add_arc gr id1 id2 lbl : adds an arc from node id1 to node id2 with label lbl - * If an arc already exists between id1 and id2, its label is replaced by lbl. - * @raise Graph_error if id1 or id2 does not exist in the graph. *) +(* new_arc gr id1 id2 lbl : adds an arc from node id1 to node id2 with label lbl + * Both nodes must already exist in the graph. + * If the arc already exists, its label is replaced by lbl. + * @raise Graph_error if node id1 or id2 does not exist in the graph. *) val new_arc: 'a graph -> id -> id -> 'a -> 'a graph