% --------------------------------------------------------------------------
%   FILE:       PROSE.PL
%   AUTHOR:     Tim Duncan
%   UPDATED:    Wed Aug  5 13:09:35 BST 1987
%   PURPOSE:    Prosenet network for ATTENDING
% --------------------------------------------------------------------------



:- op(100,fx,*).
:- op(100,xfy,:).
:- op(100,fx,&).        % action routines


/* prosenet(Node_name,Arcs)
   arc(Type,Destination,Proc)
   Arc types:

            push(node)
            pop
            jump
            text([blah])
            func(Function)
            opt(Id,Arclist)
            sequence(Id,Arclist)
            endsequence(Id)

   Where action routines (Proc) are complex, ie: involve
   several conditions, they are combined using &. For a
   description of text meta functions see runoff/1
*/




/* ---------- INTRODUCTION: GENERAL PRINCIPLES -------------- */


prosenet(pr1,[
    arc(text([*para,one]),pr4,prinnum(1)),
    arc(text([*para,there,are]),pr3,t) ]).
prosenet(pr3,[
    arc(text([several]),pr4,prinnum(2)),
    arc(text([a,number,of]),pr4,t) ]).          % was: prinnum > 2
prosenet(pr4,[
    arc(text([basic]),pr5,t) ]).
prosenet(pr5,[
    arc(text([principle]),pr6,prinnum(1)),
    arc(text([principles]),pr6,t) ]).
prosenet(pr6,[
    arc(text([involved,in,this,patient,*apost_s,management]),pr7,t) ]).
prosenet(pr7,[
    arc(text([is,that]),pr8,prinnum(1)),
    arc(text([*period]),pr8,t) ]).
prosenet(pr8,[
    arc(pop,t,t) ]).


prosenet(p1,[
    arc(push(pr1),p2,t) ]).
prosenet(p2,[
    arc(jump,p3,prinnum(1)),
    arc(jump,p2a,t) ]).             % this arc replaces p2a
prosenet(p2a,[
    arc(sequence(seq1,[
       arc(text([first,*comma]),p3,t),
       arc(text([second,*comma]),p3,t),
       arc(text([third,*comma]),p3,t),
       arc(text([fourth,*comma]),p3,t),
       arc(text([also,*comma]),p3,t),
       arc(text([in,addition,*comma]),p3,t) ]),t,t) ]).
prosenet(p3,[
    arc(push(genp),p4,t) ]).
prosenet(p4,[
    arc(jump,p2a,remprin),          % cycle back to p2a (was p2)
    arc(endsequence(seq1),p5,t) ]).
prosenet(p5,[
    arc(pop,t,t) ]).



prosenet(genp,[
    arc(opt(opt1,[
        arc(text([in,a,patient,with]),gp2,setenv(flag,[a])),
        arc(text([in,the,presence,of]),gp2,setenv(flag,[b])),
        arc(text([a,patient,with]),gp2,setenv(flag,[c])) ]),t,t) ]).
prosenet(gp2,[
    arc(func(probdesc),gp3,t) ]).
prosenet(gp3,[
    arc(text([must,be,protected,from]),gp4,getenv(flag,[c])),
    arc(text([*comma,one,must,consider,the,problem,of]),gp4,
            getenv(flag,[a])),
    arc(text([*comma,one,must,attempt,to,prevent]),gp4,
            getenv(flag,[b])) ]).
prosenet(gp4,[
    arc(func(compldesc),gp5,t) ]).
prosenet(gp5,[
    arc(text([*period]),gp6,t) ]).
prosenet(gp6,[
    arc(pop,t,t) ]).



/* ---------------------------- INTUBATION ---------------------- */

prosenet(int,[
    arc(text([*para]),int1,t) ]).
prosenet(int1,[                                     % intubation is important
    arc(text([intubation,of,this,patient,is]),int1a,recorded(patient,full_stom,_)),
    arc(jump,int4,t) ]).
prosenet(int1a,[
    arc(opt(opt6,[
        arc(text([certainly,necessary]),int1b,t),
        arc(text([definitely,appropriate]),int1b,t),
        arc(text([clearly,important]),int1b,t) ]),t,t) ]).
prosenet(int1b,[
    arc(text([because,of]),int2,t) ]).
prosenet(int2,[
    arc(text([a,full,stomach]),int3,t) ]).   % was done by: func(impdesc)
prosenet(int3,[
    arc(text([*period]),int4,t) ]).
prosenet(int4,[                     % there is a risk
    arc(text([intubation]),int5,not(getenv(intub_risk,[]))),
    arc(jump,int9,t) ]).
prosenet(int5,[                              % and intubation is important
    arc(text([*comma,however,*comma]),int6,recorded(patient,full_stom,_)),
    arc(text([of,this,patient]),int6,t) ]).
prosenet(int6,[
    arc(text([does,entail,some,risk,due,to,the,presence,of]),int7,t) ]).
prosenet(int7,[
    arc(func(intriskdesc),int8,t) ]).   % Sources say FUNC(INTRISKDESC) but
prosenet(int8,[                         % mean RISCDESC which uses INTVAL.
    arc(text([*period]),int9,t) ]).
prosenet(int9,[                      % choice was not "none"
    arc(func(descchostech),int10,not(getenv(chostech,[none|_]))),
    arc(jump,int15,t) ]).
prosenet(int10,[
    arc(text([is,a,good,approach,*period]),int11,
        getenv(chostechval,[good])),
    arc(text([is,a,reasonable,approach,*period]),int11,
        getenv(chostechval,[ok])),
    arc(text([may,not,be,the,best,approach,*period]),int11,
        getenv(chostechval,[soso])) ]).
prosenet(int11,[
    arc(text([this]),int12,
        &([getenv(chostechval,[soso]), exist_risks_or_benefits])),
    arc(text([this]),int11a,exist_risks_or_benefits),
    arc(jump,int14,t) ]).       % no risks or benefits to chosen technique
prosenet(int11a,[
    arc(text([*comma,however,*comma]),int12,not(getenv(tlist,[]))),
    arc(jump,int12,t) ]).
prosenet(int12,[
    arc(push(risk_pred),int14,t) ]).
prosenet(int14,[
    arc(push(subalts),int14a,subtechalts),  % alternates in subplan? pushenv
    arc(jump,int15,t) ]).
prosenet(int14a,[
    arc(text([thus,there,is,no,ideal,way,to,perform]),int14b,noway),
    arc(jump,int15,t) ]).
prosenet(int14b,[       % environment was restored by "noway"
    arc(func(descchostech),int14c,t) ]).
prosenet(int14c,[
    arc(text([*period]),int15,t) ]).
prosenet(int15,[             % there are alts to chosen technique
    arc(push(alts),int16,not(getenv(alts,[]))),
    arc(pop,t,t) ]).
prosenet(int16,[
    arc(pop,t,t) ]).


/* ---------------- NOT INTUBATING THE PATIENT --------------- */


prosenet(n,[
    arc(text([*para,although,intubation,of,this,patient,was,not,
        proposed,*comma,it,is]),n10,t) ]).
prosenet(n10,[
    arc(text([clearly,desirable]),n20,recorded(patient,full_stom,_)),
    arc(text([probably,desirable]),n20,t) ]).
prosenet(n20,[
    arc(text([*comma,even,though,it]),n30,tifaltrisk),
    arc(text([*period]),n40,t) ]).
prosenet(n30,[
    arc(push(risk_pred),n40,tsetaltriskonly) ]).
prosenet(n40,[
    arc(text([not,intubating,this,patient]),n50,t) ]).
prosenet(n50,[
    arc(text([*comma,on,the,other,hand,*comma]),n60,tifaltrisk),
    arc(jump,n60,t) ]).
prosenet(n60,[
    arc(push(risk_pred),n80,setenv(blist,[])) ]).
prosenet(n80,[
    arc(pop,t,t) ]).




/* ---------- ALTERNATIVES TO CHOSEN TECHNIQUE ------------- */


prosenet(alts,[                              % only one alternative
    arc(text([an,alternative,approach]),alt1,getenv(alts,[Only_one])),
    arc(text([there,are,several,possible,alternatives]),alt1,t) ]).
prosenet(alt1,[
    arc(text([*comma,perhaps,*comma]),alt2,     % same risk as chosen tech
        &([getenv(chostechrisk,[R]), getenv(alts,[[_:R:_|_]|_])])),
    arc(jump,alt2,t) ]).
prosenet(alt2,[         % chosen technique
    arc(text([to]),alt3,not(getenv(chostech,[]))),
    arc(jump,alt4,t) ]).
prosenet(alt3,[
    arc(func(descchostech),alt4,t) ]).
prosenet(alt4,[                 % only one alternative
    arc(text([would,be]),alt7,getenv(alts,[Only_one])),
    arc(text([*period]),alt5,t) ]).
prosenet(alt5,[
    arc(push(genalts),alt6,t) ]).
prosenet(alt6,[
    arc(pop,t,t) ]).
prosenet(alt7,[
    arc(func(descalttech),alt8,t) ]).
prosenet(alt8,[
    arc(text([*period]),alt9,t) ]).
prosenet(alt9,[             % setup btlist for first alternative, fail if none
    arc(push(genrisk),alt9a,setaltrisk),
    arc(push(cprule1),alt10,not(getenv(cprule,[]))),  % only if no risk/bene ??
    arc(jump,alt10,t) ]).
prosenet(alt9a,[           % then check first for CP rule
    arc(push(cprule),alt10,not(getenv(cprule,[]))),
    arc(jump,alt10,t) ]).
prosenet(alt10,[
    arc(pop,t,t) ]).


prosenet(genalts,[
    arc(text([an]),gaa,        % first and only alternative
        &([getenv(altcount,[0]),getenv(alts,[Only_one])])),
    arc(text([one]),gaa,getenv(altcount,[0])),          % 1st (of several)
    arc(text([another]),gaa,not(getenv(altcount,[2]))), % 2nd, 4th etc ...
    arc(text([a,third]),gaa,getenv(altcount,[2])) ]).   % 3rd
prosenet(gaa,[
    arc(text([alternative,would,be]),ga1,t) ]).
prosenet(ga1,[
    arc(func(descalttech),ga2,t) ]).
prosenet(ga2,[
    arc(text([*period]),ga2a,t) ]).
prosenet(ga2a,[
    arc(push(alt_risk),ga2b,setaltrisk),          % set risk/benes of this alt
    arc(push(cprule1),ga3,not(getenv(cprule,[]))), % no risk/bene, but CP rule
    arc(jump,ga3,t) ]).
prosenet(ga2b,[
    arc(push(cprule),ga3,not(getenv(cprule,[]))),   % CP rule
    arc(jump,ga3,t) ]).
prosenet(ga3,[
    arc(pop,t,getenv(alts,[Only_one])),  % last alternative to discuss
    arc(jump,genalts,nextalt) ]).        % get next alt, and update altcount




/* ----------- ALTERNATIVES IN THE SUBPLAN --------------------- */


% NB: environment has been pushed before subalts net

prosenet(subalts,[                % there is some risk to subtech chosen
    arc(text([the,choice,of]),sa1,not(getenv(tlist,[]))),
    arc(jump,sa4,t) ]).
prosenet(sa1,[
    arc(func(descsubtech),sa1a,t) ]).    % = descchostech
prosenet(sa1a,[
    arc(text([has,certain,considerations,which,have,already,
            been,discussed,*period]),sa5,ifsredund),
    arc(jump,sa1b,t) ]).
prosenet(sa1b,[
    arc(text([*comma,however,*comma]),sa2,nochoscomment),
    arc(jump,sa2,t) ]).
prosenet(sa2,[
    arc(push(risk_pred),sa4,setsubrisk) ]). % = t
prosenet(sa4,[
    arc(push(genalts),sa5,not(getenv(alts,[]))), % was tifsubalts
    arc(jump,sa5,t) ]).
prosenet(sa5,[
    arc(pop,t,t) ]).



/* ------------------ DESCRIPTION OF RISK ---------------- */

prosenet(risk_np,[
    arc(text([at,least,the,theoretical]),rnp20,
        &([getenv(tlist,[R:F|_]), member(questrisk,F)])),
    arc(text([the]),rnp10,t) ]).
prosenet(rnp10,[
    arc(text([possible]),rnp20,
        &([getenv(tlist,[R:F|_]), member(possrisk,F)])),
    arc(text([conceivable]),rnp20,
        &([getenv(tlist,[R:F|_]), member(whensevere,F)])),
    arc(jump,rnp20,t) ]).
prosenet(rnp20,[
    arc(text([risk,of]),rnp50,getenv(tlist,[Only_one])),
    arc(opt(opt8,[
        arc(text([risk,that]),rnp30,t),
        arc(text([risk,of]),rnp50,t)]), t,t) ]).
prosenet(rnp30,[
    arc(text([this]),rnp40,t) ]).
prosenet(rnp40,[
    arc(text([could,cause]),rnp50,t) ]).
prosenet(rnp50,[
    arc(func(desconerisk),rnp60,t) ]).
prosenet(rnp60,[
    arc(pop,t,t) ]).




prosenet(risk_s,[
    arc(push(cond),rs10,
        &([getenv(tlist,[R:F|_]), member(cond(_),F)])),
    arc(jump,rs5,t) ]).
prosenet(rs5,[
    arc(opt(opt9,[
        arc(jump,rs10,t),
        arc(push(cntxt),rs10,t)]),t,t) ]).
prosenet(rs10,[
    arc(text([this]),rs40,        % RISKSPEC only occurs in HBP
        &([getenv(tlist,[R:F|_]), member(riskspec,F)])),
    arc(text([there,is]),rs20,t) ]).
prosenet(rs20,[
    arc(push(risk_np),rs30,t) ]).
prosenet(rs30,[
    arc(pop,t,t) ]).
prosenet(rs40,[
    arc(func(specrisk),rs30,t) ]).



prosenet(cond,[
    arc(text([if]),c10,t) ]).
prosenet(c10,[
    arc(func(desccond),c20,t) ]).
prosenet(c20,[
    arc(text([*comma]),c30,t) ]).
prosenet(c30,[
    arc(pop,t,t) ]).



prosenet(cntxt,[
    arc(text([for]),cnt10,ifopcntxt),   % if the next risk is because of an op
    arc(text([in,a,patient,with]),cnt10,t) ]).
prosenet(cnt10,[
    arc(func(desccontext),cnt20,t) ]).
prosenet(cnt20,[
    arc(text([*comma]),cnt30,t) ]).
prosenet(cnt30,[
    arc(pop,t,t) ]).



prosenet(mult_risk,[
    arc(jump,mr5,setenv(risknum,[1])) ]).   % initialise risk counter
prosenet(mr5,[
    arc(text([first,*comma]),mr10,getenv(risknum,[1])),
    arc(text([second,*comma]),mr10,getenv(risknum,[2])),
    arc(text([third,*comma]),mr10,getenv(risknum,[3])),
    arc(text([also,*comma]),mr10,t) ]).
prosenet(mr10,[
    arc(push(risk_s),mr20,t) ]).
prosenet(mr20,[
    arc(text([*period]),mr30,t) ]).
prosenet(mr30,[
    arc(pop,t,getenv(tlist,[Done])),        % done all risks
    arc(jump,mr5,nextrisk) ]).          % next risk



prosenet(alt_risk,[
    arc(text([this,has]),gr90,getenv(tlist,[])),
    arc(jump,ar5,setenv(risknum,[1])) ]).
prosenet(ar5,[
    arc(text([here,*comma,however,*comma]),ar10,getenv(risknum,[1])),
    arc(text([also,*comma]),ar10,getenv(risknum,[2])),
    arc(text([in,addition,*comma]),ar10,t) ]).
prosenet(ar10,[
    arc(push(risk_s),ar20,t) ]).
prosenet(ar20,[
    arc(text([*period]),ar30,t) ]).
prosenet(ar30,[
    arc(jump,gr60,getenv(tlist,[Done_it])),
    arc(jump,ar5,nextrisk) ]).



prosenet(bene_np,[
    arc(text([the,advantage,of]),bn10,t) ]).
prosenet(bn10,[
    arc(func(descbenes),bn20,t) ]).
prosenet(bn20,[
    arc(pop,t,t) ]).



prosenet(genrisk,[
    arc(text([this]),gr10,not(getenv(tlist,[]))),   % there are risks
    arc(text([this,has]),gr90,t) ]).
prosenet(gr10,[
    arc(func(specrisk),gr50,    % SPECRISK and last risk
        &([getenv(tlist,[R:F]),member(specrisk,F)])),
    arc(opt(opt4,[
        arc(text([involves]),gr20,t),
        arc(text([would,have]),gr20,t),
        arc(text([could,have]),gr20,t),
        arc(text([might,have]),gr20,t)]),t,t) ]).
prosenet(gr20,[
    arc(push(risk_np),gr50,getenv(tlist,[R:P])),    % only one risk left
    arc(func(descrisknumb),gr30,t) ]).
prosenet(gr30,[
    arc(text([possible,risks,*period]),gr40,t) ]).
prosenet(gr40,[
    arc(push(mult_risk),gr60,t) ]).
prosenet(gr50,[         % risk involves conditional
    arc(push(cond),gr55,&([getenv(tlist,[R:P|_]), member(cond(X),P)])),
    arc(jump,gr55,t) ]).
prosenet(gr55,[
    arc(text([*period]),gr60,t) ]).
prosenet(gr60,[
    arc(pop,t,getenv(blist,[])),    % no benefits
    arc(text([it,does]),gr70,t) ]).
prosenet(gr70,[                             % there are risks
    arc(text([*comma,however,*comma]),gr80,not(getenv(tlist,[]))),
    arc(jump,gr80,t) ]).            % NB this arc added in case of failure
prosenet(gr80,[
    arc(text([have]),gr90,t) ]).
prosenet(gr90,[
    arc(push(bene_np),gr100,t) ]).
prosenet(gr100,[
    arc(text([*period]),gr110,t) ]).
prosenet(gr110,[
    arc(pop,t,t) ]).


prosenet(risk_pred,[
    arc(jump,gr10,not(getenv(tlist,[]))),       % there are risks
    arc(text([has]),gr90,t) ]).



prosenet(cprule1,[
    arc(text([this]),cp10,t) ]).
prosenet(cprule,[
    arc(func(descalttech),cp10,t) ]).
prosenet(cp10,[
    arc(text([may,be,preferable,for,a,patient]),cp20,t) ]).
prosenet(cp20,[
    arc(func(desccpcontext),cp30,t) ]).
prosenet(cp30,[
    arc(func(descwhy),cp40,t) ]).
prosenet(cp40,[
    arc(text([*period]),cp50,t) ]).
prosenet(cp50,[
    arc(pop,t,t) ]).




/* -------- FINAL SECTION: MINOR RISKS/WELL CHOSEN TECHNIQUES ------ */


prosenet(fin,[
    arc(text([*para,looking,at,other,aspects,of,the,proposed,plan,
        *comma]),f10,recorded(major,discussed,_)),
    arc(jump,f10,t) ]).
prosenet(f10,[
    arc(push(wellchos),f20,not(getenv(good_techs,[]))),
    arc(jump,f20,t) ]).
prosenet(f20,[
    arc(push(minors),f30,not(getenv(minors,[]))),
    arc(jump,f30,t) ]).
prosenet(f30,[
    arc(pop,t,t) ]).


prosenet(wellchos,[
    arc(push(wc),w10,t) ]).
prosenet(w10,[
    arc(pop,t,flasttech),
    arc(text([also,*comma]),wellchos,t) ]).


prosenet(wc,[
    arc(opt(opt2,[
        arc(text([for,a,patient,with]),wc10,t),
        arc(text([in,the,presence,of]),wc10,t)]),t,t) ]).
prosenet(wc10,[
    arc(func(descprob),wc20,tsettechs) ]).
prosenet(wc20,[
    arc(text([*comma]),wc30,t) ]).
prosenet(wc30,[
    arc(func(descwctech),wc40,t) ]).
prosenet(wc40,[
    arc(text([has,the,advantage,of]),wc50,
        &([getenv(gtechlst,[T:R:D|_]), member(wellchos,D)])),
    arc(opt(opt3,[
        arc(text([is,a,good,choice,since]),wc50,t),
        arc(text([is,appropriate,since]),wc50,t),
        arc(text([is,a,reasonable,selection,since]),wc50,t)]),t,t) ]).
prosenet(wc50,[
    arc(func(descbenewc),wc60,t) ]).
prosenet(wc60,[
    arc(func(comma_and),wc30,
        &([getenv(gtechlst,[_|More]),setenv(gtechlst,More),not(null(More))])),
    arc(text([*period]),wc70,t) ]).
prosenet(wc70,[
    arc(pop,t,t) ]).



prosenet(minors,[                          % wellchos already discussed?
    arc(text([*para,in,addition,*comma]),m5,not(getenv(good_techs,[]))),
    arc(jump,m5,t) ]).
prosenet(m5,[
    arc(text([one,consideration,worth,mentioning]),m30,
        getenv(minors,[Only_one])),
    arc(text([there]),m10,t) ]).
prosenet(m10,[
    arc(text([are]),m30,t) ]).
prosenet(m30,[
    arc(text([is,that]),m70,getenv(minors,[Only_one])),
    arc(text([two]),m35,getenv(minors,[Two,Minors])),
    arc(text([several]),m35,t) ]).
prosenet(m35,[
    arc(text([further,considerations]),m40,not(getenv(good_techs,[]))),
    arc(text([considerations]),m40,t) ]).
prosenet(m40,[
    arc(text([worth,mentioning]),m50,t) ]).
prosenet(m50,[
    arc(text([*period]),m60,t) ]).
prosenet(m60,[
    arc(func(linenum),m70,t) ]).
prosenet(m70,[
    arc(func(desctech),m80,t) ]).
prosenet(m80,[
    arc(push(risk_pred),m110,   % set tlist to first minor, blist to nil
        &([getenv(minors,[T:R:D|_]),setenv(tlist,D),delenv(blist,_)])) ]).
prosenet(m110,[
    arc(pop,t,getenv(minors,[Last_one])),
    arc(jump,m60,nextminor) ]).




/* --------- GENERAL PURPOSE: REGIONAL/INDUCTION/MAINTENANCE ------ */

prosenet(imx,[
    arc(text([*para]),im10,t) ]).
prosenet(im10,[
    arc(opt(opt5,[
        arc(text([in,regard,to]),im20,t),
        arc(text([from,the,standpoint,of]),im20,t)]),t,t) ]).
prosenet(im20,[
    arc(func(desccntxt),im30,t) ]).     % subplan
prosenet(im30,[
    arc(text([*comma]),im40,t) ]).
prosenet(im40,[
    arc(func(descchostech),im50,exist_risks_or_benefits),
    arc(jump,im120,t) ]).
prosenet(im50,[
    arc(text([has]),im60,not(getenv(blist,[]))),
    arc(jump,im90,t) ]).
prosenet(im60,[
    arc(push(bene_np),im70,not(getenv(blist,[]))),
    arc(jump,im90,t) ]).
prosenet(im70,[
    arc(text([*period]),im80,t) ]).
prosenet(im80,[
    arc(text([on,the,other,hand,*comma,it]),im90,
        &([setenv(blist,[]),not(getenv(tlist,[]))])),
    arc(jump,im120,t) ]).
prosenet(im90,[
    arc(push(risk_pred),im120,t) ]).
prosenet(im120,[
    arc(push(subalts),im130,subtechalts),  % alternates in subplan, pushenv
    arc(jump,im130,t) ]).
prosenet(im130,[
    arc(push(alts),im140,&([popenv,not(getenv(alts,[]))])),
    arc(jump,im140,t) ]).
prosenet(im140,[
    arc(pop,t,t) ]).






/* -------------------- VARIOUS COMMENTS -------------------- */


prosenet(ok,[
    arc(text([*para,the,proposed,plan,seems,to,be,generally,reasonable,
    *period]),ok10,t) ]).
prosenet(ok10,[
    arc(pop,t,t) ]).


prosenet(notga,[
    arc(text([*para,as,an,alternative,to,general,anesthesia,*comma]),ng10,t) ]).
prosenet(ng10,[
    arc(text([a,regional,technique]),ng20,t) ]).
prosenet(ng20,[
    arc(text([might,be,worth,considering,*period]),ng30,t) ]).
prosenet(ng30,[
    arc(pop,t,t) ]).



prosenet(notreg,[
    arc(text([in,view,of,this,problem,with,regional,anesthesia,
    *comma,general,anesthesia,might,be,worth,considering,*period]),nr20,t) ]).
prosenet(nr20,[
    arc(pop,t,t) ]).
