% --------------------------------------------------------------------------
%   FILE:       ACTION.PL
%   AUTHOR:     Tim Duncan
%   UPDATED:    Fri Aug 21 21:06:06 BST 1987
%   PURPOSE:    Action routines for Prosenet.
% --------------------------------------------------------------------------


:- op(100,fx,*).
:- op(100,xfy,:).


% Default, if action routine cannot be found.                   

ask(F,t) :- !.
ask(F,X) :-
    repeat,
    write(F:X),
    readline([R]),
    ( R = y
    ; R = n, !, fail
    ; bell, fail ), !.       



/* ------------------ ACTION ROUTINES ----------------------

Note that action routines main function is to succeed/fail to show
whether an arc should be taken or not. However, they frequently also                    
do a certain amount of setting up for later tests or functions
*/


prinnum(N) :-   /* how many principles */
    getenv(principle,P),
    length(P,N).

remprin :- /* &(setq,prinlist,(cdr,prinlist))) */
    getenv(principle,[_|T]),
    ( T = [],
      delenv(principle,_),
      !, fail                       /* remove last item and fail */
    ; setenv(principle,T) ).

exist_risks_or_benefits :-
    getenv(tlist,T),
    getenv(blist,B),
    ( not(null(T))
    ; not(null(B)) ), !.

nextalt :-
    getenv(alts,[Alt|Next]),
    setenv(alts,Next),          % remove the alternative just described
    getenv(altcount,[N]),
    N1 is N+1,                  % update number of alternatives described
    setenv(altcount,[N1]), !.

subtechalts :-
    getenv(chostech,[Tech,Subtree]),
    not(null(Subtree)),
    pushenv,
    chosen_technique(_,Subtree),
    ( not(getenv(alts,[]))      % There are alternates in subplan, so succeed.
    ; popenv, !, fail ), !.     % Restore old environment, and fail test.


/* "noway" used as test to print "thus there is no ideal way to <chosen tech>"
    True if there are alternatives to chosen technique, and even the best
    alternative in the subplan has some risk. Note: the environment is popped
    whatever the outcome.
*/

noway :-
    getenv(alts,Subtechalts),
    popenv,
    !,              % here comes the test
    not(exists_better_alternative(lowr,Subtechalts)),
    not(getenv(alts,[])), !.


/* setaltrisk/0
Tlist, Blist, & CPrule can be safely overwritten since either the chosen
technique has been discussed ($alts), or the environment has been pushed
($genalts). If there are no risks/benefits, this test should fail.
*/

setaltrisk :-
    getenv(alts,[[Tech:R:Prose|Sub]|Alts]),
    btlist(Risks,Benefits,CP,Prose),
    setenv(tlist,Risks),
    setenv(blist,Benefits),
    setenv(cprule,CP),
    ( null(Risks), null(Benefits), !, fail
    ; true ), !.



ifsredund :-    /* check whether technique already discussed */
    getenv(chostech,[Tech|Sub]),
    redundant(global,Tech).



/* Nothing to comment on re chosen technique */                 

nochoscomment :-
    getenv(subplan,[intubation]),
    getenv(chostechval,V),
    !,
    ( V = [good]
    ; V = [ok] ).
nochoscomment :-
    not(getenv(blist,[])),
    getenv(tlist,[]).


setsubrisk.     % setbtlist on chosensubtech, has been done already


nextrisk :-     /* cycle through list of risks, updating counter */
    getenv(risknum,[N]),
    getenv(tlist,[R:F|Tail]),
    M is N+1,
    setenv(tlist,Tail),
    setenv(risknum,[M]), !.


/* The next risk is because of an operation rather than a disease.
Therefore text will read "for ..." rather than "In a patient with ..."                     
*/

ifopcntxt :-
    getenv(tlist,[R:Features|More]),
    member(context(C),Features), !,
    member(C,[appendectomy, abdop, aortic_aneurysmectomy, cea,
              cholecystectomy, colectomy, craniotomy, headop,
              leop, tendon_repair, thr, ueop, eyeop]).



/* ------------ actions for last section (fin) ---------------- */




/* flasttech - set up next of the good_techs, fail if not last.
The last one must be left for the initial action routine of the
minors net
*/

flasttech :-
    getenv(good_techs,[Done|Next]),
    ( null(Next)
    ; setenv(good_techs,Next),
      !, fail ).


/* The (set of) techniques that are 'wellchosen', and deal
with a particular context, are put into gtechlst.
*/

tsettechs :-  /* set up for descprob (wc) */
    getenv(good_techs,[prob(C):X|Others]),
    setenv(gtechlst,X).


nextminor :-
    getenv(minors,[Done|Next]),
    setenv(minors,Next), !.
