:- lib(os_lib).

:- lib( default_tmp_directory/1 ).
:- lib( mktemp_in_dir/3 ).
:- lib( bn_to_dot_dag/3 ).
:- lib( options_remove/3 ).

:- lib( stoics_lib:en_list/2 ).

/** disp_asia.

    Display the Asia BN on screen, with default settings.

*/
disp_asia :-
    Asia = [1-[],2-[1],3-[2,5],4-[],5-[4],6-[4],7-[3],8-[3,6]],
    disp_bn( Asia, [output(x11)] ).

/** disp_bn( +Bn ).
    disp_bn( +Bn, +Opts ).

    Construct a dot file for graph BN and display it on a number of output devices.
    Unused options are passed to bn_to_dot_dag/3.

Opts in

  * dot_file(Dot)     
    dot file to use. Default is constructed by mktemp_in_dir/3.
  * rmv(Rmv) 
    whether to remove dot file before exiting. Default is *false* if no dot_file/1 was given and *true* otherwise.
  * output(Dev)
    device to use for output. Should be x11 or something that will work with dot -T<Dev>.
  * output_stem(Stem)
    the stem of the file on which dot will write its output. Defaults to the stem of Dot (option dot_file/1 above).

==
disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]] ).
disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]], [edges_attrs([2-1-color(blue)]) ] ).
disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]], [edges_attrs([2-1-[color(red),penwidth(4)]])] ).
disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]], [edges_attrs([2-1-[color(red),penwidth(4)]]), nodes_attrs(3-shape(egg))] ).
disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]], [edges_attrs([2-1-[color(red),penwidth(4)]]), nodes_attrs(3-[shape(box),style(rounded)])] ).
disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]], [edges_attrs([2-1-[color(red),penwidth(4)]]), nodes_attrs(3-[shape(box),style('rounded,filled'),fillcolor(yellow)])] ).

disp_bn( [1-[2,3], 2-[3,4],3-[4], 4-[]], [output(x11),edges_attrs([2-1-[color(red),penwidth(4)]]), nodes_attrs(3-[shape(box),style('radial,rounded'),fillcolor('white:darkorange')])] ).

style="radial",color="white:darkorange"


disp_bn( [1-[],2-[1],3-[2,5],4-[],5-[4],6-[4],7-[3],8-[3,6]] ).
disp_bn( [1-[],2-[1,4],3-[1],4-[1],5-[],6-[],7-[1,2,3,4,5,6],8-[2]], [1-[],2-[1],3-[2,5],4-[],5-[4],6-[4],7-[3],8-[3,6]] ).
% red edges (ok) yellow edges (new) magenta (missing)
disp_bn( [1-[],2-[1],3-[2,5],4-[],5-[4],6-[4],7-[3],8-[3,6]], [1-[],2-[1],3-[5],4-[],5-[4],6-[4],7-[5],8-[3,6]] ). % the best learned...
disp_bn( [1-[],2-[1],3-[2,5],4-[],5-[4],6-[4],7-[3],8-[3,6]], [1-[],2-[1],3-[5],4-[],5-[4],6-[4],7-[5],8-[3,6]], [node_style(empty)] ). % as above with non-filled style nodes
disp_dag /usr/nicos/islp/bn_lbl_kernel/slp/disp/asia.dot 
 
disp_bn( [1-[2],3-[4,5]] ).

?- 
   Opts1 = [ output(x11),colour(bnw),type(graph),graph(bgcolor("#D3D3D3")),
           node_shape(box),rmv(false),node_style(rounded),
           nodes_dichromatic(false,false) ] ),
   assert( bn1([1-[2],3-[4,5]]) ), assert( opts1(Opts1) ).

?- opts1(Opts), bn1(Bn1), disp_bn(Bn1,Opts).

?- Eats = edges_attrs([1-2-color(red)]) 

==

Other background colours (latexcolor.com)
 * graph(bgcolor("#B2BEB5")), % Ash grey
 * graph(bgcolor("#91A3B0")), % Cadet gray
 * graph(bgcolor("#A3C1AD")), % Cambridge Blue
 * graph(bgcolor("#8C92AC")), % Cool gray
 * graph(bgcolor("#A9A9A9")), % Dark gray
 * graph(bgcolor("#BEBEBE")), % Gray (X11 gray)Gray (X11 gray)
 * graph(bgcolor("#C4C3D0")), % Lavender gray

 
@author nicos angelopoulos
@version  0.2 2014/03/13

*/

disp_bn( Bn ) :-
    disp_bn( Bn, [output(x11)] ).

disp_bn( Bn, OptSIn ) :-
    en_list( OptSIn, OptsIn ),
    ( memberchk(dot_file(DotFile),OptsIn) -> 
        true
        ;
        default_tmp_directory( TmpDir ),
        mktemp_in_dir( tmp_XXXXXX, TmpDir, DotFile )
    ),
    options_remove( OptsIn, [dot_file/1,rmv/1,output/1,output_stem/1], Opts ),
    bn_to_dot_dag( Bn, DotFile, Opts ),
    disp_bn_outputs( OptsIn, DotFile ),
    % exec_dag_disp( DotFile ),
    % ( bb_get(first_bn,false) -> sleep( 1 ) 
        % ; sleep(2), bb_put( first_bn, false ) ),
     ( memberchk(rmv(true),OptsIn) ->
         sleep( 1 ),
         delete_file( DotFile )
          ;
        ( memberchk(dot_file(_),OptsIn) ->
        % ((memberchk(rmv(Oth),OptsIn),Oth\==false) -> % this USED TO BE THE CASE. Change calling code if you stamble on this)
            % rename_file( DotFile, Oth )
            true
            ;
            ( memberchk(rmv(false),OptsIn) ->
                true
                ;
                sleep( 1 ),
                delete_file( DotFile )
            )
        )
     ).

disp_bn_outputs( Opts, DotF ) :-
    ( memberchk(output_stem(Stem),Opts) -> true
                ; 
                os_ext(_,Stem,DotF)
    ),
    AbsG = absolute_file_name(path(dot), _DotBin, [access(exist),file_errors(fail)]),
    ( AbsG ->
        foreach( member(output(Format),Opts), disp_bn_output(Format,DotF,Stem) )
        ;
        print_message( informational, disp_bn(dot_off(DotF)) )
    ).

disp_bn_output( x11, DotFile, _Stem ) :-
    !,
    debug( disp_bn, 'Displaying file: ~p', DotFile ),
    atomic_list_concat( [dot,DotFile,'-Tx11','2> /dev/null &'], ' ', Disp ),
    shell( Disp ).
    % doesn't detach !:
    % process_create( path(Doted), [DotFile,'&'], [detached(true)] ). % redirect errors ? 
    % exec( Disp, [null,null,null], _Pid ).
disp_bn_output( Format, DotFile, Stem ) :-  % works for png, svg, ...
    atom_concat( '-T', Format, Tormat ),
    file_name_extension( Stem, Format, Output ),
    debug( disp_bn, 'Output file:~p', Output ),
    atomic_list_concat( [dot,DotFile,Tormat,'-o',Output], ' ', Dot ),
    shell( Dot ).