--- daVinci_orig.ecl	2007-12-06 10:44:25.000000000 +0100
+++ daVinci.ecl	2007-12-06 15:23:50.000000000 +0100
@@ -47,6 +47,7 @@
 :-local variable(daVinciOut).
 :-local variable(daVinciEdge).
 :-local variable(daVinciAttribute).
+:-local variable(daVinciMore). % flag to continue updating the graph after 1 solution
 
 :-comment(summary,"This library provides a simple interface to the daVinci graph drawing tool").
 
@@ -130,13 +131,8 @@
 	;
 	    da
 	),
-	setval(daVinciOut,0),
-	setval(daVinciEdge,0),
-	setval(daVinciAttribute,0),
-	open(string(""),write,daVinciEdge),
-	open(string(""),write,daVinciAttribute),
 	da('menu(file(new))'),
-	printf(daVinciOut,'graph(update([',[]),
+	daVinci_init,
         (
 	    true
         ;
@@ -144,6 +140,19 @@
 	    fail
 	).
 
+% daVinci_init/0 internal function
+% Initialises streams,
+% used when openenin the connection for the first time
+% and when reopening it for more solutions
+daVinci_init:-
+	setval(daVinciOut,0),
+	setval(daVinciEdge,0),
+	setval(daVinciAttribute,0),
+	setval(daVinciMore,0),
+	open(string(""),write,daVinciEdge),
+	open(string(""),write,daVinciAttribute),
+	printf(daVinciOut,'graph(update([',[]).
+
 :-comment(daVinci_end/0,[
 summary:"This commands ends the information about a graph and calls daVinci to draw it.",
 
@@ -168,6 +177,7 @@
 	current_stream(daVinciEdge),
 	get_stream_info(daVinciEdge,name,Edge),
 	close(daVinciEdge),
+	current_stream(daVinciAttribute),
 	get_stream_info(daVinciAttribute,name,Attribute),
 	close(daVinciAttribute),
 	da_still_open,
@@ -175,9 +185,11 @@
 	da('],[%s]))',[Edge]),
         % comment next line out to check without attributes
 	da('graph(change_attr([%s]))',[Attribute]), 
+	da('menu(layout(improve_all))'), % Auto layout search tree
+ 	% Set flag to reopen connection for the next solution
+	setval(daVinciMore,1),
         true.
 
-
 :-comment(daVinci_exit/0,[
 summary:"This commands closes the connection and exits daVinci.",
 args:[],
@@ -249,6 +261,12 @@
 ]).
 
 daVinci_node(Node,Info):-
+	( getval(daVinciMore, 1) ->
+		% reopening the connection after first solution
+		daVinci_init
+	;
+		true
+	),
 	da_comma(daVinciOut),
 	printf(daVinciOut,'new_node("%w","",[a("OBJECT","%w")])',[Node,Info]).
 
@@ -302,6 +320,12 @@
 ]).
 
 daVinci_node_attribute(Node,Type,Value):-
+	( getval(daVinciMore, 1) ->
+		% reopening the connection after first solution
+		daVinci_init
+	;
+		true
+	),
 	da_comma(daVinciAttribute),
 	printf(daVinciAttribute,'node("%w",[a("%w","%w")])',[Node,Type,Value]).
 
@@ -327,6 +351,12 @@
 ]).
 
 daVinci_edge_attribute(Node,Type,Value):-
+	( getval(daVinciMore, 1) ->
+		% reopening the connection after first solution
+		daVinci_init
+	;
+		true
+	),
 	da_comma(daVinciAttribute),
 	printf(daVinciAttribute,'edge("%w",[a("%w","%w")])',[Node,Type,Value]).
 
