%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                           %
%       file:           aux.pl                                              %
%       purpose:        auxiliary predicates for chart.pl                   %
%       author:         Sebastian Varges                                    %
%       date:           Wed Feb 19 16:42:24 MET 1997                        %
%       related files:  chart.pl                                            %
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%***********   user output: trace, display solutions and tables   ***********

trace(_,_,_) :- get_switch(trace,off).
trace(InfRule,Item,SelectionType) :- 
        get_switch(trace,on),
        show_trace(InfRule,Item,SelectionType),!.

%---  show_trace(+InfRule,(+Selected,+Head,+Body,+RuleIndex))
show_trace(newtask,CurrentTask,_) :- 
        write('***************************  new task ***************************'),
        nl, write_item(CurrentTask),step.

show_trace(blocking,_,_) :- write('%%%%  ... blocked'),nl,step.
show_trace(InfRule,Item,SelectionType) :- 
        write('% ------------------------------------------------------'),nl,
        write('%  inference rule:       '), write(InfRule),nl,
        write('%  reduced new item:'), nl, write_item(Item),
        tab(3),write('selection type:  '), write(SelectionType),nl,nl,
        step.

step :- write('[Options: a = abort, n = nonstop,  = creep]'),nl,
        write('||  '), read_line(ASCII),name(Option,ASCII),
        process_option(Option).

process_option(a) :- abort.
process_option(n) :- set_switch(trace,off).
process_option(_).

%--- display_solutions(Mode,Goal)
display_solutions(off,_,Fs) :- earley_solution(Fs).
display_solutions(on,all,Fs) :-
        nl,
        earley_solution(Fs),
        write(Fs),nl,
        fail.
display_solutions(on,first,Fs) :- 
        earley_solution(Fs),
        nl, write('% first solution:    '), write(Fs),nl.
display_solutions(on,_,_) :- write('% no (more) solutions'),nl.

%--- tables/0
tables :-
        nl, write('********************  agenda items  ********************'),nl,nl,
        write_agenda,
        nl, write('********************  table items   ********************'),nl,nl,
        write_table.

write_agenda :- 
        agenda_item(Item),
        nl,write('%------------------------------------------------'),nl,
        write_item(Item),nl,
        fail.
write_agenda.

write_table :-  
        table_item(Selected,Head,Body,N),
        nl,write('%------------------------------------------------'),nl,
        write_item((Selected,Head,Body,N)),nl,
        fail.
write_table.

write_item((nil,Head,[],(_,Index))) :-
        write('%         Passive Item:'),nl,nl,
        write(Head),write('.'),nl,
        tab(3),write('Grammar Rule Index:  '),write(Index),nl,nl,!.

write_item((Selected,Head,Body,(_,Index))) :-
        write('%        Active Item:'),nl,nl,
        tab(3),write(Head),tab(3),write('<---'),nl,
        write_body(Body),nl,
        tab(3),write('Selected:'),nl,nl,
        tab(8),write(Selected),nl,nl,
        tab(3),write('Grammar Rule Index:  '),write(Index),nl,nl,!.

write_body([]).
write_body([F|R]) :-
        tab(8),write(F),nl,
        write_body(R).

get_switch(Switch,Mode) :- switch(Switch,Mode).
set_switch(Switch,Mode) :- assertz(switch(Switch,Mode)).

% read_line/1 reads full line from standard input, terminates with return
read_line(Line) :- read_line([],Line),!.
read_line(Akku,Line) :-
         get0(X),
         ln_continuation(X,Akku,Line).

ln_continuation(X,Line,Line) :-   X = 10,!.
ln_continuation(X,Akku,Line) :-
         append(Akku,[X],Akku1),!,
         read_line(Akku1,Line).

% not necessary for Quintus Prolog
append([],L,L).
append([F|R],L,[F|R1]) :-
        append(R,L,R1).