%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   cgi_run_bip.html
%
%   Author J.P.E. Hodgson
%
%  date 2 february 1999
%
%   Based on earlier version (Dec 1998)
%
%   Error messages revised 
%   1999/06/16

%

%%%%%%%%%%%%%%%%%%%%
%
%  Extract enough to do run_bip
%  on a single line.
%  
%   cgi_run_bip(+Atom, -Results)
%
%   The arguments are passed as a single atom.
%   This is done to ensure that the variables
%   are properly named.
%


cgi_run_bip(Atom, Results) :-
         asserta(iso_current_op(700, xfx, '<--')),
         catch(read_term_from_atom(Atom, L, [end_of_term(eof)]),
               _,
               throw('Syntax error reading Goal or Expected Result')),
         treat_terms(L, L1),
         retract(iso_current_op(700, xfx, '<--')),
         retractall(error_in_goal(_)), sto_test(on),
         fake_write('Warning : The STO test flag is ON'),
         fake_write('================================='), 
         correction_test([L1]),
         sto_test(off),
         retract(renaming_number(_)),
         assertz(renaming_number(1)),
         findall(X, cgi_output(X), Results),
         retractall(cgi_output(_)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   build_ex/2,3 construct the
%   one line argument for cgi_run_bip
%   and then invoke it.
%



build_ex(Goal,Expected,R) :-
        catch(convert2ali(Expected, E),_, 
             throw('Error converting expected result')),
        atom_concat3('(', Goal, ')', Goal1),
        atom_concat('[', Goal1, G1),
        atom_concat(G1, ',', G2),
        atom_concat(G2, E, G3),
        atom_concat(G3, ']', G4),
        cgi_run_bip(G4, R).

build_ex3(Goal, Expected, PA, R) :-
        catch(convert2ali(Expected, E),_,
             throw('Error converting expected result')),
        atom_concat3('(', Goal, ')', Goal1),
        atom_concat('[', Goal1, G1),
        atom_concat(G1, ',', G2),
        catch(read_prog_from_text(PA, Prog),_,
              throw('Syntax error reading user program')),
        write_to_atom(ProgAtom , Prog),
        atom_concat(G2, ProgAtom, G3),
        atom_concat(G3, ',', G4),
        atom_concat(G4, E, G5),
        atom_concat(G5, ']', G6),
        cgi_run_bip(G6, R).
        
test3(Goal, Expected, PA, G6) :-
      convert2ali(Expected, E),
       atom_concat('[', Goal, G1),
        atom_concat(G1, ',', G2),
        read_prog_from_text(PA, Prog),
        write_to_atom(ProgAtom , Prog),
        atom_concat(G2, ProgAtom, G3),
        atom_concat(G3, ',', G4),
        atom_concat(G4, E, G5),
        atom_concat(G5, ']', G6).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%       Read a program from text that comes from
%  an online form.
%




read_prog_from_text(Atom, Prog) :-
        open_input_atom_stream(Atom, S),
        read_terms_from_stream(S,Prog),
        close_input_atom_stream(S).

read_terms_from_stream(S, L) :-
        read(S,T ),
        read_terms_from_stream(S,T, L).

read_terms_from_stream(_S, end_of_file, []).
read_terms_from_stream(S, T, [T|L]) :-
         read(S,T1), 
         read_terms_from_stream(S,T1, L).	



%%%%%%%%%%%%%%%%%
%
%    convert a goal from
%  the form
%
%   X <-- a , Y <-- b ; X <--a, Y <-- c
%
% to [[X<--a, Y <-- b], [X <-- a, Y <-- c]]
%



%  for goals that have no substitutions in them

atomg2ali(Atom, AtomG) :-
        \+ sub_atom(Atom, _, 3, _, '<--'),
         !,
        AtomG = Atom.


atomg2ali(Atom, AtomG) :-
	sub_atom(Atom, _, 1, _, ';'), !,
        split_at(';', Atom, Before, After),
        atomg2ali(Before, BG),
        atomg2ali(After, AG),
        atom_concat(BG, ',', B1),
        atom_concat(B1, AG, AtomG).
atomg2ali(Atom, AtomG) :-
	atom_concat3('[',Atom, ']', AtomG).

%  for goals that have no substitutions in them
convert2ali(Atom, Converted) :-
        \+ sub_atom(Atom, _, 3, _, '<--'),
         !,
         Converted = Atom.

convert2ali(Atom, Converted) :-
	atomg2ali(Atom, AtomG),
        atom_concat3('[', AtomG, ']', Converted).


atom_concat3(Before, Middle, After, Result) :-
      atom_concat(Before, Middle, Inter),
      atom_concat(Inter, After, Result).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Split an atom at first occurrence of a char
%
%  split_at(+Char,          % char to look for
%           +Atom,          % Atom to split
%           -Before,
%           +After).

% deal with cases where Char is the last character. 

 split_at(Char, Atom, Before, After) :-
	atom_concat(A, Char, Atom),
        \+ sub_atom(A,_,1,_,Char), !,
        Before = A, After = ''.
split_at(Char,Atom, Before, After) :-
	atom_concat(A,After, Atom),
        atom_concat(Before, Char, A),
        \+ sub_atom(Before, _,1, _, Char).




