:- dynamic state/4.

build_automaton(Files,Type) :-
        set_var(automaton,1),
	(retractall(state(_,_,_,_)),!;true),
	(Files=[File|_],!;File=Files),
        search(Files,breadth),
	concat_atom([File,'.dot'],DotFile),
	tell(DotFile),
        write("digraph {\n"),
	repeat,(
	    trans(X,Y,C),
	    write_all(['\"',X,'\" -> \"',Y,'\" [ label=\"',C,'\"];\n']),
	fail;!),
	write("}\n"),
	told,
	(Type=dot,!
        ;Type=pdf,!,
         concat_atom([File,'.pdf'],OutFile),
	 concat_atom(['dot -Tpdf ',DotFile,' -o ',OutFile],Command),
	 shell(Command,_),
	 delete_file(DotFile)
	;Type=xml,!,

         concat_atom([File,'.txt'],TxtFile),
	 concat_atom(['dot -Tplain ',DotFile,' -o ',TxtFile],Command),
	 shell(Command,_),
	 see(TxtFile),
	 set_var(nextstate,0),
         gettoken([10],_,_), % remove first line
	 read_states,
	 seen,
	 write_xml(File),
 	 delete_file(DotFile),
 	 delete_file(TxtFile)
	),
        set_var(automaton,0).
  
write_xml(File) :-
    concat_atom([File,'.xml'],XMLFile),
    tell(XMLFile),
    write('<?xml version=\"1.0\" ?>\n'),
    write('<SEARCH>\n'),
    write('\t<GRAPH>\n'),
    write('\t\t<DESCRIPTION>\n'),
    write('\t\t\t<SHORT>\n'),
    write_all(['Auto-generated from file ',File,
	       ' using search.pl tool (Pedro Cabalar, Univ. of Corunna, 2017).\n']),
    write('\t\t\t</SHORT>\n'),
    write('\t\t\t<DETAILED></DETAILED>\n'),
    write('\t\t</DESCRIPTION>\n'),
    write('\t\t<!-- Node Definitions -->\n'),
    repeat,(
	state(I,S,X,Y),X1 is X*2.5,Y1 is Y*2.5,
	write('\t\t<NODE>\n'),
        write('\t\t\t<NAME>'),write_state(S),write('</NAME>\n'),
	write_all(['\t\t\t<INDEX>',I,'</INDEX>\n']),
	write_all(['\t\t\t<XPOS>',X1,'</XPOS>\n']),
	write_all(['\t\t\t<YPOS>',Y1,'</YPOS>\n']),
	write('\t\t\t<TYPE>'), write_state_type(S), write('</TYPE>\n'),
	heur(S,H),
	write_all(['\t\t\t<HEURISTICVALUE>',H,'</HEURISTICVALUE>\n']),
        write('\t\t</NODE>\n'),	
    fail;!),
    write('\t\t<!-- Edge Definitions -->\n'),
    repeat,(
	trans(A,B,C),state(I,A,_,_),state(J,B,_,_),
	write('\t\t<EDGE>\n'),
	write_all(['\t\t\t<STARTINDEX>',I,'</STARTINDEX>\n']),
	write_all(['\t\t\t<ENDINDEX>',J,'</ENDINDEX>\n']),
	write_all(['\t\t\t<COST>',C,'</COST>\n']),
        write('\t\t</EDGE>\n'),	
    fail;!),
    write('\t\t<HEURISTIC value=\"AUTOMATIC\"></HEURISTIC>\n'),
    write('\t\t<EDGECOSTS value="MANUAL"></EDGECOSTS>\n'),
    write('\t</GRAPH>\n'),
    write('</SEARCH>\n'),
    told.

heur(S,H) :- h(S,H),!; H=0.

write_state_type(S) :-
    initial(S),!,write('START'); goal(S),!,write('GOAL'); write('REGULAR').

read_states :-
    gettoken([0' ],T,_),T=node,!, % one more node
    gettoken([0' ],A,_),atom_to_term(A,S,_),
    gettoken([0' ],X,_),atom_number(X,X1),
    gettoken([0' ],Y,_),atom_number(Y,Y1),
    gettoken([10],_,_),
    incr_var(nextstate,N,1),
    assert(state(N,S,X1,Y1)),
    read_states.
read_states.

gettoken(Delims,Tok,Delim):-
    gettokenchars(Delims,Chars,Delim),
    % remove quotes
    (Chars=[0'"|Cs0],!,append(Cs,[0'"],Cs0),!; Cs=Chars),
    atom_codes(Tok,Cs).
	     
gettokenchars(Delims,Cs,Delim):-
  get0(C),!,
  ( C = -1,!,fail
  ; member(C,Delims),!,Delim=C,Cs=[]  
  ; gettokenchars(Delims,Cs0,Delim), Cs=[C|Cs0]
  ).
