******************************************************************************
*** PLA based on IMaude
******************************************************************************
mod PLA-GRAPH is

  inc RFE .
  inc PLA-AUX .
  inc STATUS-AUX .

  inc META-PETRI .
  inc PNTL2GRAPH .
****  inc GRAPH2G2D .
  inc NEW-GRAPH2G2D .
  inc GRAPH2LOLA .


******************************************************************************
*** Variables
******************************************************************************
var ctl : Control .
var wait4s : Wait4Set .
var reqQ reqQ' reqQ0 reqQ1 reqQ2 : RequestQ .
vars es es' es0 : ESet .
var ee : Entry .
var ee? : Entry? .
var log : Log .

vars b b0 avoid? : Bool .
var n nodeId : Nat .
vars mname g2dname g2dvname kbname modname modname' : Qid .
vars type ptype tkbFname occsFname graphFname nid flag qid opname parent : Qid .
vars gname pname newgname newpname pathpname dname newdname user-dname : Qid .
vars outQ toks toks' rids qidl : QidList .
var ?graph : Graph .
vars pntlT pntlT' pnetT occsT ioccsT ioccsT' uoccsT roccsT nT occT : Term .
vars goccsT aoccsT ridsT ugoccsT : Term .
var tl occsTL : TermList .
vars dnotes gnotes pnotes notes : Notes .
vars res0?? res1?? res?? res2?? res3?? : [ResultPair?] .
vars id ?string status str gstr astr rstr title subtitle : String .
vars ctr  ctr1 ctr2 netStr taskStr : String .
vars ndl ndl' nds nodes pndl : NodeList .
var edl : EdgeList .
 
*** 12feb04
var requestor : Qid .

******************************************************************************
***(  
  op withAckTemplate : -> String .
  eq withAckTemplate = 
"(seq \n\
    %exp\n\
    (sinvoke \"g2d.util.ActorMsg\" \"send\" %replyto %g2dname \"OK\") )" .

**** (apply sendMessage %replyto  %g2dname \"OK\") 
 
 op withAck : String Qid Qid -> String .
 eq withAck(?string,mname,aname) =
     fillTemplate(withAckTemplate,
                  (("%exp" :~ ?string),
                   ("%replyto" :~ squote(string(mname))),
                   ("%g2dname" :~ squote(string(aname))))) .

)
******************************************************************************
**** defineGraph -- as iop graph, but don't display
******************************************************************************
  eq isReq('defineGraph) = true .
****                                    where   what
  eq enabled(wait4s,req('defineGraph,ql(g2dvname gname toks),reqQ)) = 
      not(waiting4(wait4s,g2dvname)) .

  crl[defineGraph]:
   [nil,
    st(processing(req('defineGraph, ql(g2dvname gname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready,
       (wait4s !
         wait4(g2dvname,gname,
               req('logreq,ql('defineGraph g2dvname gname),reqQ'))), 
       reqQ,
       es,
       log), outQ 
     g2dvname '\n mname '\n toks' maudePrompt ]
   if gv(?graph) := getVal(es,'gval,gname,gv(mtGraph)) 
   /\ mname :=  getMaudeName(es)
   /\ gnotes := getNotes(es,'gval,gname)
   /\ ?string :=
        withAck((if (graphNodes(?graph) == nil)
                 then "(apply mtGraphAlert)"
**** 09oct10 to stop the progress bar when no graph is shown
*****            then "(apply displayMessage \"Alert\" \"Empty Graph\")"
                 else mkDefineNewGraphExp(gname,?graph,gnotes)
                   fi), mname,g2dvname) 
   /\ toks' := str2toks(?string) .


******************************************************************************
**** showGraph -- display an already defined  graph
******************************************************************************
  eq isReq('showGraph) = true .
****                                   where   what
  eq enabled(wait4s,req('showGraph,ql(g2dvname gname toks),reqQ)) = 
      not(waiting4(wait4s,g2dvname)) .

  crl[showGraph]:
   [nil,
    st(processing(req('showGraph, ql(g2dvname gname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   (if notes == mt or graphNodes(?graph) == nil
    then [nil,  
          st(ready,wait4s,
             reqQ  req('logreq,
                       ql('showGraph 'mtgraph g2dvname gname toks),
                       reqQ'),
              es, log),
           outQ]
    else [nil,  
          st(ready,
             wait4s !
                   wait4(g2dvname,gname,
                         req('logreq,ql('showGraph g2dvname gname toks),reqQ')),
             reqQ,
              es,
              log), outQ g2dvname '\n mname '\n toks' maudePrompt ]
    fi)
   if notes := getNotes(es,'gval,gname) 
   /\ gv(?graph) := getVal(es,'gval,gname,gv(mtGraph)) 
   /\ mname :=  getMaudeName(es)
   /\ ?string := (if graphNodes(?graph) =/= nil
                  then withAck(mkShowNewGraphExp(gname,notes), mname,g2dvname) 
                  else "" fi)
   /\ toks' := str2toks(?string) .

******************************************************************************
**** kbFixup given dishnet and its graph
**** redefines kb net to be pntl of dishnet and sets kbgraph to be its graph
******************************************************************************
  eq isReq('kbFixup) = true .
  eq enabled(wait4s, req('kbFixup,ql(kbname pname gname toks),reqQ)) 
       = true .

  rl[kbFixup]:
   [nil,
    st(processing(req('kbFixup, ql(kbname pname gname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ', kbFixup(es,kbname,pname,gname), log), outQ] .

  op kbFixup : ESet Qid Qid Qid -> ESet .
  ceq kbFixup(es,kbname,pname,gname) =
      addEntry(addEntry(es,'tval,'tkb kbname, pnotes, tm(modname,pntlT)),
              'gval,kbname, gnotes, getVal(es,'gval, gname,gv(mtGraph)))
   if tm(modname,pnetT) := getVal(es,'tval, 'pnet pname, dumTv)
   /\ pntlT := (if modname == 'BOOL 
                then 'nil.PNTransList 
                else pnetT2pntlT(pnetT) fi)
   /\ pnotes :=  (("source" := ql('pnet pname)),("graph" := ql(kbname)))
   /\ gnotes := (("source" := ql('tkb kbname)), 
                 ("kbname" := ql(kbname))) .

******************************************************************************
**** dishShortCut  assumes kb is defined, but not graph
**** makes dishnet, redefined kb to be the dishnet Pntl
**** makes graph, stores it as kbgraph and as dishnet graph
**** then display the dishnet graph
******************************************************************************
  eq isReq('dishShortCut) = true .
  eq enabled(wait4s, req('dishShortCut,ql(kbname g2dvname dname toks),reqQ)) 
       = true .

  crl[dishShortCut]:
   [nil,
    st(processing(req('dishShortCut, ql(kbname g2dvname dname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, incGlobalCounter(es,3), log), outQ]
   if ctr := getGlobalCounter(es) 
   /\ newdname := qid("dish" + ctr)
   /\ newpname := qid("pnet" + stringInc(ctr) )
   /\ newgname := qid("graph" + stringInc(stringInc(ctr)) )
   /\ reqQ0 :=
       req('predefDish,ql(kbname dname newdname dname),
         req('dish2pnet, ql(kbname newdname newpname),
           req('pnet2graph, ql(kbname newpname newgname),
             req('kbFixup, ql(kbname newpname newgname),
               req('defineGraph, ql(g2dvname newgname),
                 req('startListener, ql(newgname 'graphreq g2dvname),
                   req('showGraph, ql(g2dvname newgname),
               reqQ'))))))) .

***(seq
  (predefDish PLTest graphics2d quiz dishq quiz)
  (dish2pnet PLTest dishq pnetq)
  (pnet2graph PLTest dishnet pnetq graphq)
  (kbFixup PLTest pnetq graphq)
  (defineGraph  graphics2d graphq)
  (showGraph  graphics2d graphq)
)

******************************************************************************
****  dish2pnet --- uses
****                              Init   
****  op fwdCollect : PNTransList Occs -> PNTL3 .
****                    I^Rch I-Rch Rch    *** I-Rch = unused
****       {PNTransList Occs Occs Occs}
******************************************************************************
  eq isReq('dish2pnet) = true .
  eq enabled(wait4s, req('dish2pnet,ql(kbname dname pname toks),reqQ)) 
       = true .
  rl[dish2pnet]:
   [nil,
    st(processing(req('dish2pnet, ql(kbname dname pname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready,
       wait4s,
       (reqQ reqQ'),
       dish2pnet(es,kbname,dname,pname),
       log), outQ] .

  op dish2pnet : ESet Qid Qid Qid -> ESet .
  ceq dish2pnet(es,kbname,dname,pname) =
       addEntry(updateNotes(es,'tval,'dish dname,
                            insert("pnet",ql('pnet pname), dnotes)),
               'tval,'pnet pname, pnotes, 
               tm(modname,'pnet[pntlT',ioccsT]))
   if tm(modname,occsT) := getVal(es,'tval,'dish dname,tm('BOOL,'true.Bool))
   /\ dnotes := getNotes(es,'tval,'dish dname)
   /\ ql(user-dname) := lookupd(dnotes,"user-dname",ql(dname))
   /\ tm(modname',pntlT) := getVal(es,'tval, 'tkb kbname,tm('BOOL,'true.Bool))
***(
   /\ res?? := metaReduce([modname],'fwdCollect[pntlT,occsT])
   /\ b := modname =/= 'BOOL and  modname' =/= 'BOOL and res?? :: ResultPair
   /\ '`{_`,_`,_`,_`}[pntlT',ioccsT,uoccsT,roccsT] :=
         (if b
          then getTerm(res??)
          else '`{_`,_`,_`,_`}['nil.PNTransList,'none.Occs,'none.Occs,'none.Occs]
          fi )
)          
**** 17feb05 adding ruleRanking to pnet notes
     /\ res?? := metaReduce([modname],'fwdCollectRanked[pntlT,occsT])
     /\ b := modname =/= 'BOOL and  modname' =/= 'BOOL and res?? :: ResultPair
     /\ '`{_`,_`,_`,_`,_`}[pntlT',ioccsT,uoccsT,roccsT,ranksT] :=
           (if b
            then getTerm(res??)
            else '`{_`,_`,_`,_`,_`}['nil.PNTransList, 
                    'none.Occs,'none.Occs,'none.Occs,'nil.QidListList]
            fi )
   /\ pnotes := (if b
                 then (("source" := ql('dishnet dname)),
							  ("requestor" := ql(kbname)),
							  ("rchOccs" := tm(modname, roccsT)),
							  ("unusedOccs" := tm(modname, uoccsT)),
          		  ("ruleRanks" := tm(modname, ranksT)),
							  ("dishname" := ql(dname user-dname)),
							  ("kbname" := ql(kbname)))
                 else mt
                 fi)   .

******************************************************************************
****  pnet2graph --- uses pnetT2graph which uses pntl2graph initNodeLStatus
******************************************************************************
  eq isReq('pnet2graph) = true .
  eq enabled(wait4s, req('pnet2graph,ql(kbname pname gname toks),reqQ)) 
       = true .
  rl[pnet2graph]:
   [nil,
    st(processing(req('pnet2graph, ql(kbname pname gname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready,
       wait4s,
       (reqQ reqQ'),
       pnet2graph(es,kbname,pname,gname),
       log), outQ] .

**** name of graph of parent -- better be visible
  op getParentG : ESet Qid -> Qid .
  ceq getParentG(es,qid) =
     (if gname == 'unknown
      then getParentG(es,
                      getNth(1,
                             qvalQidList(lookupd(getNotes(es,'tval,'pnet qid),
                                                 "source",ql('unknown 'unknown))),
                             'unknown) )
      else gname
      fi)
  if ql(gname) := lookupd(getNotes(es,'tval, 'pnet qid), "graph", ql('unknown) ) .


  op pnet2graph : ESet Qid Qid Qid -> ESet .
  ceq  pnet2graph(es,kbname,pname,gname) =
        addEntry(updateNotes(es,'tval,'pnet pname,
                             insert("graph",ql(gname), pnotes)),
                 'gval, gname, gnotes, 
                 gv(graph(string(gname),mt,ndl',edl)))
   if tm(modname,pnetT) := getVal(es,'tval,'pnet pname,tm('BOOL,'true.Bool))
   /\ pnotes := getNotes(es,'tval, 'pnet pname)
   /\ ql(ptype qid) := lookupd(pnotes,"source",ql('unknown 'unknown))
   /\ ql(requestor) := lookupd(pnotes,"requestor",ql('unknown))
   /\ gv(?graph) := getVal(es,'gval, kbname, gv(mtGraph))
   /\ b := modname =/= 'BOOL and ptype =/= 'unknown
   /\ graph(str,notes,ndl,edl) := (if b 
                                   then pnetT2graph(modname, pnetT, 
                                                    graphNodes(?graph))
                                   else mtGraph fi)
   /\ tm(modname',uoccsT) := 
           lookupd(pnotes,"unRchGoals",tm(modname,'none.Occs))
***** 17feb05 propagating ruleRanks
   /\ ranksT := 
     tvalTerm(lookupd(pnotes,"ruleRanks",tm(modname,'nil.QidListList)))
   /\ parent := (if ptype == 'dishnet 
                 then 'unknown
                 else getParentG(es,qid) fi)
   /\ gnotes :=
         (if b
          then (("source" := ql('pnet pname)),
			       ("kbname" := ql(kbname)),
		    		 ("unRchGoals" :=
					      sv(printOccTL([modname],occsT2occTL(uoccsT)))),
					 (if ptype == 'pathnet 
					  then ("lolapath" := lookupd(pnotes, "lolapath",ql(nil)))
					  else mt fi),
**** 09aug02 clt added subtitle to gatts					  
			       ("gatts" := ql('kbname 'unRchGoals 'source 'subtitle
   	                      'requestor 'requestop 'requestargs
                  			(if ptype == 'pathnet then 'lolapath else nil fi))),
			       ("oatts" := ql('init 'status)),      
             ("ratts" := ql('status 'pre 'post)),           
             ("colorFun" := sv("colorPnetNode")),   
             ("title"  := sv(mkPGraphTitle(gname,pnotes))), 
             ("subtitle" := 
                    sv(if ptype == 'subnet or ptype == 'pathnet
                       then mkGraphSubTitle(pnotes)
                       else "" fi)), 
             (if parent == 'unknown then mt else ("parent" := ql(parent)) fi),
             ("selections" := sv("true")),
***** 17feb05 propagating ruleRanks
        		 ("ruleRanks" := tm(modname, ranksT)),
             ("toolBarFun" := sv("toolBarFunPnet")),
         **** assume a pnet
             ("requestor" := ql(requestor)),
             ("requestop" := ql(ptype)),
             ("requestargs" :=
                 sv(if (ptype == 'dishnet) then getUserDName(pnotes)
                 		else mkReqArgString(modname,pnotes) fi))  
         )
          else mt
          fi) 
    /\ goccsT  := tvalTerm(lookupd(pnotes,"gOccs",tm(modname,'none.Occs)))
    /\ ndl' := (if b and  (ptype == 'subnet or ptype == 'pathnet)
                then setNodesGoalStatus(ndl,occsT2occTL(goccsT),nil)
                else ndl
                fi) .

  op getUserDName : Notes -> String .
  ceq getUserDName(notes) = string(user-dname)        
    if 
      ql(dname user-dname) := lookupd(notes,"dishname",ql('unknown 'unknown))
     .

  op mkReqArgString : Qid Notes -> String .
  ceq mkReqArgString(modname,pnotes) = 
      "goals " + printOccTL([modname],occsT2occTL(goccsT))
     + "avoids " + printOccTL([modname],occsT2occTL(aoccsT))
     + "hides " + printRidTL(qidlT2tl([modname],ridsT))
   if 
       goccsT  := tvalTerm(lookupd(pnotes,"gOccs",tm(modname,'none.Occs)))
    /\ aoccsT  := tvalTerm(lookupd(pnotes,"aOccs",tm(modname,'none.Occs)))
    /\ ridsT  := tvalTerm(lookupd(pnotes,"aRules",tm(modname,'nil.QidList)))
    .



  op pnetT2graph : Qid Term NodeList -> Graph .
  ceq pnetT2graph(modname,'pnet[pntlT,ioccsT],pndl) =
        graph(str,mt,initNodeLAtts(ndl,occsT2occTL(ioccsT),nil),edl)
  if graph(str,notes,ndl,edl) :=  
                  pntl2graph(modname,pntlT2pntTL(pntlT),pndl) .

  eq pnetT2graph(modname,pnetT,pndl) = mtGraph [owise] .  **** not a pnet

 
******************************************************************************
***** dgraph2subnet 
******************************************************************************

  eq isReq('dgraph2subnet) = true .
  eq enabled(wait4s, req('dgraph2subnet,ql(kbname gname pname toks),reqQ)) 
       = true .
  rl[dgraph2subnet]:
   [nil,
    st(processing(req('dgraph2subnet, ql(kbname gname pname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready,
       wait4s,
       reqQ reqQ',
       dgraph2subnet(es,kbname,gname,pname),
       log), outQ] .

  op dgraph2subnet : ESet Qid Qid Qid -> ESet .
  ceq dgraph2subnet(es,kbname,gname,pname) =
         (if res1?? :: ResultPair
          then addEntry(es,'tval,'pnet pname, pnotes, 
                            tm(modname,'pnet[pntlT',ioccsT]))
          else es fi)
**** for status annotations
   if ndl := graphNodes(gvalGraph(getVal(es,'gval,gname,gv(mtGraph))))
   /\ ql(type opname) :=
       lookupd(getNotes(es,'gval,gname),"source",ql('unknown 'unknown))
   /\ tm(modname,pnetT) := getVal(es,'tval, type opname, tm('BOOL,'true.Bool))
   /\ ql(toks') := lookupd(getNotes(es,'tval, type opname),"dishname",ql(nil))
   /\ b := modname == 'BOOL 
   /\ (goccsT, aoccsT, ridsT) := 
           (if b
            then ('none.Occs,'none.Occs, 'nil.QidList)
            else (getONodesStatus(ndl,"goal",empty),  *** goccsT 
                  getONodesStatus(ndl,"avoid",empty), *** aoccsT
                  getHiddenRuleIdsT(ndl,empty))               *** ridsT
            fi)
   /\ res?? := (if modname == 'BOOL
                then noParse(0)
                else metaReduce([modname], 
                                'relSubnet[pnetT,goccsT,aoccsT,ridsT]) 
                fi)
   /\ '`{_`,_`,_`,_`}[pntlT',ioccsT,uoccsT,roccsT] :=
        (if res?? :: ResultPair
         then getTerm(res??)
         else '`{_`,_`,_`,_`} 
                ['nil.PNTransList,'none.Occs,'none.Occs,'none.Occs]
         fi )
   /\ res1?? := (if res?? :: ResultPair
                 then metaReduce([modname],'Odiff[goccsT,roccsT])
                 else noParse(0) fi)
   /\ pnotes := (if res1?? :: ResultPair
****!!!! toks' is the dish source?
                 then (("source" := ql('subnet opname)),
                       ("requestor" := ql(gname)),
****!!!! use when displaying to alert user to problem
                       ("unRchGoals" := tm(modname,getTerm(res1??))),
                       ("gOccs" := tm(modname, goccsT) ),
                       ("aOccs" := tm(modname, aoccsT) ),
                       ("aRules" := tm(modname, ridsT) ),
                       ("rchOccs" := tm(modname, roccsT)), 
                       ("unusedOccs" := tm(modname, uoccsT)),
                       ("dishname" := ql(toks')),
                       ("kbname" := ql(kbname)))
                 else mt
                 fi) 
   .


  op getHiddenRuleIdsT : NodeList TermList -> Term .
  eq getHiddenRuleIdsT(nil,tl) = qidTL2qidlT(tl) .
  eq getHiddenRuleIdsT(nd(nodeId,notes) nds,tl) =
      getHiddenRuleIdsT(nds,
         (if lookupd(notes,"type",sv("")) == sv("rule")
             and lookupd(notes,"status",sv("none")) == sv("avoid")
          then (tl, 
                tvalTerm(lookupd(notes,"rule",tm('QID,''?.Qid)))
                )
          else tl fi) ) .
  
  op getONodesStatus :  NodeList String TermList -> Term .
  eq getONodesStatus(nil,status,occsTL) = occTL2occsT(occsTL) .
  eq getONodesStatus(nd(nodeId,notes) nds, status, occsTL) =
       getONodesStatus(nds,status,
         (occsTL, (if (lookupd(notes,"type",sv("")) == sv("occ")
                       and lookupd(notes,"status",sv("")) == sv(status)
                       and tvalTerm(lookupd(notes, "occ", 
                                    tm('BOOL,'true.Bool)))
                                 =/= 'true.Bool)
                     then tvalTerm(lookupd(notes,"occ",tm('BOOL,'true.Bool)))
                     else empty fi)) ) .

******************************************************************************
**** (dgraph2pathsubnet,ql(kbname pname gname newpname),
******************************************************************************
  eq isReq('dgraph2pathsubnet) = true .
  eq enabled(wait4s, req('dgraph2pathsubnet,
                         ql(kbname pname gname newpname toks),reqQ)) 
       = true .
  rl[dgraph2pathsubnet]:
   [nil,
    st(processing(req('dgraph2pathsubnet,
                      ql(kbname pname gname newpname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready,
       wait4s,
       reqQ reqQ',
       dgraph2pathsubnet(es,kbname,pname,gname,newpname),
       log), outQ] .

  op dgraph2pathsubnet : ESet Qid Qid Qid Qid -> ESet .
  ceq dgraph2pathsubnet(es,kbname,pname,gname,newpname) =
       (if res1?? :: ResultPair
        then addEntry(es,'tval,'pnet newpname, pnotes, 
                      tm(modname,'pnet[pntlT',ioccsT]))
        else es fi)
**** for status annotations
   if ndl := graphNodes(gvalGraph(getVal(es,'gval,gname,gv(mtGraph))))
   /\ goccsT := getONodesStatus(ndl,"goal",empty)
   /\ aoccsT := getONodesStatus(ndl,"avoid",empty)
   /\ ridsT :=  getHiddenRuleIdsT(ndl,empty)
   /\ tm(modname,pnetT) := 
          getVal(es,'tval,'pnet pname, tm('BOOL, 'true.Bool))
   /\ b := modname == 'BOOL
   /\ ql(toks') := lookupd(getNotes(es,'tval,'pnet pname),"dishname",ql(nil))
   /\ pntlT := (if b then 'nil.PNTransList else pnetT2pntlT(pnetT) fi)
   /\ ioccsT' := (if b then 'none.Occs else pnetT2occsT(pnetT) fi)
   /\ res?? := (if b
                then noParse(0)
                else metaReduce([modname], 
                                'omitRules['avoidOccs[pntlT,aoccsT],ridsT]) 
                fi)
   /\ pntlT' := (if res?? :: ResultPair
                 then getTerm(res??)
                 else 'nil.PNTransList
                 fi )
   /\ res0?? := (if res?? :: ResultPair
                 then metaReduce([modname], 'allOccs[pntlT'])
                 else noParse(0)
                 fi)
  /\ roccsT := (if res0?? :: ResultPair 
                 then getTerm(res0??)
                 else 'none.Occs 
                 fi)
   /\ ioccsT := getTerm(metaReduce([modname], 'Osame[ioccsT',roccsT]))
   /\ uoccsT := getTerm(metaReduce([modname], 'Odiff[ioccsT',roccsT]))
   /\ res1?? := (if res0?? :: ResultPair
                 then metaReduce([modname],'Odiff[goccsT, roccsT])
                 else noParse(0) fi)
   /\ ugoccsT := (if res1?? :: ResultPair 
                  then getTerm(res1??) 
                  else 'none.Occs 
                  fi)
   /\ pnotes :=  (("source" := ql('subnet pname)),
                  ("unRchGoals" := tm(modname, ugoccsT)),
                  ("gOccs" := tm(modname, goccsT)),
                  ("aOccs" := tm(modname, aoccsT)),
                  ("aRules" := tm(modname, ridsT)),
                  ("rchOccs" := tm(modname, roccsT)), 
                  ("unusedOccs" := tm(modname, uoccsT)),
                  (if toks' == nil then mt else ("dishname" := ql(toks')) fi),
                  ("kbname" := ql(kbname)))
      .

******************************************************************************
**** (askLola kbname pname gname pathpname)
**** assumes pname names pnet, gname has goals annotation
**** pnet may be that of graph or a reduced form with no graph
**** sends lola request and waits with lolaReply continuation
**** pathpname is to be used for pathgraph
******************************************************************************
  eq isReq('askLola) = true .
  eq enabled(wait4s,req('askLola,ql(kbname pname gname pathpname toks),reqQ)) = 
       not(waiting4(wait4s,'graphics2d)) .
****!!!! getG2dName(es)
 
crl[askLola]:
   [nil,
    st(processing(req('askLola, ql(kbname pname gname pathpname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, 
       (if ?string == ""
        then wait4s
        else (wait4s !  wait4(g2dname, 'askLola pname, 
                   creq('lolaReply,nil,ql(gname pname pathpname toks),reqQ')))
        fi), 
     (reqQ reqQ0), es, log), 
    outQ toks']
   if mname :=  getMaudeName(es)
   /\ g2dname := getG2dName(es) 
   /\  ?string := lolaReqString(es,kbname,pname,gname,mname)
   /\   toks' := (if ?string == "" 
                  then nil 
                  else g2dname '\n mname '\n str2toks(?string) maudePrompt
                  fi)
   /\ reqQ0 := (if ?string == ""
                then req('logreq,
                         ql('askLola 'failed kbname pname pathpname toks),
                     req('sendAlert,
                         ql(getG2dVName(es) 'askLola 'unable 'to 'ask), nil)) 
                else nil fi)   .


  op lolaReqString : ESet Qid Qid Qid Qid -> String .
  ceq lolaReqString(es,kbname,pname,gname,mname) =
          (if modname == 'BOOL or ?graph == mtGraph or goccsT == 'none.Occs
           then ""
           else mkLolaReq(setNodeLGOccs(ndl,occsT2occTL(goccsT),nil),
                           mname)
           fi) 
   if tm(modname,pnetT) := getVal(es,'tval,'pnet pname, tm('BOOL,'true.Bool))
   /\ gv(?graph) := getVal(es,'gval,gname,gv(mtGraph))
   /\ goccsT := getONodesStatus(graphNodes(?graph),"goal",empty)
**** reduce graph if not graph of pnet (pnet tmp subnet)
**** assume pnet is subnet of graph source
   /\ ndl := 
       graphNodes(if lookupd(getNotes(es,'gval,gname),"source", ql(nil)) 
                        == ql('pnet pname)
                  then ?graph
                  else pnetT2graph(modname,pnetT,graphNodes(?graph)) fi) .
    

  op setNodeLGOccs : NodeList TermList NodeList -> NodeList .
  eq setNodeLGOccs(nil,tl,ndl') = ndl' .
  eq setNodeLGOccs(nd(nodeId,notes) ndl,tl,ndl') = 
        setNodeLGOccs( ndl,tl,ndl' nd(nodeId,setNotesGOccs(notes,tl))) .
 
  op setNotesGOccs : Notes TermList -> Notes .
  ceq setNotesGOccs(notes,tl) =
     (if occT == 'true.Bool 
      then notes
      else (if eltOf(occT,tl)
            then insert("status",sv("goal"), notes)
            else notes
            fi)
      fi)
  if occT := (if lookupd(notes,"type",sv("")) == sv("occ")
              then tvalTerm(lookupd(notes, "occ", tm('BOOL,'true.Bool)))
              else 'true.Bool 
              fi)
  .

  op lolaReqTemplate : -> String .
  eq lolaReqTemplate = 
      "(apply lolaRequest %net %task %id %requestor)" .

  op mkLolaReq : NodeList Qid -> String .
  ceq mkLolaReq(ndl,mname) =
     fillTemplate(lolaReqTemplate,
                  (("%net" :~ squote(netStr)),
                   ("%task" :~ squote(taskStr)),
                   ("%id" :~ squote("0")),
                   ("%requestor" :~ squote(string(mname)))) ) 
  if netStr := mkLolaNetString(ndl)
  /\ taskStr := mkLolaTaskString(ndl) .
  


******************************************************************************
**** (lolaReply .... ) -- creq
**** if return code is not 0 or subnet pname does not exist then
****    just log a failure report
****    otherwise create a pathnet from the lola reply and store it
******************************************************************************
**** can not be invoked from outside!
  eq enabled(wait4s,
         creq('lolaReply,g2dname qid toks', 
              ql(gname pname pathpname toks),reqQ))
       = true .

**** qid is the lola return code
**** just the pathsubnet and store it as pathpname
crl[lolaReply]:
   [nil,
    st(processing(creq('lolaReply,g2dname qid toks',
                       ql(gname pname pathpname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, (reqQ reqQ0), es', log),
    outQ]
  if {b,es'} := processLolaReply(es,gname,pname,pathpname,qid,toks')
   /\ reqQ0 :=
         (if b
          then reqQ' 
          else req('logreq,ql('askLola 'nopath gname pname pathpname),
                 req('sendAlert,
                     ql(getG2dVName(es) 'LolaFailed 'return-code qid), nil))
          fi) .


  op processLolaReply : ESet Qid Qid Qid Qid QidList -> BoolESet . 
  ceq processLolaReply(es,gname,pname,pathpname,qid,toks) =
       processPath(es,gname,pname,pathpname,ndl,rids)
   if ndl := graphNodes(gvalGraph(getVal(es,'gval, gname,gv(mtGraph))))
   /\ rids :=  **** chatty rule labels as qids
       (if qid == '0 then readLolaResult(ndl,qid toks) else nil fi) .

op processPath : ESet Qid Qid Qid NodeList QidList -> BoolESet . 
ceq processPath(es,gname,pname,pathpname,ndl,rids) =
    {res3?? :: ResultPair,
     (if res3?? :: ResultPair
      then addEntry(es,'tval,'pnet pathpname, pnotes,
                tm(modname,'pnet[pntlT',ioccsT']))     
      else es fi)}
  if ndl := graphNodes(gvalGraph(getVal(es,'gval, gname,gv(mtGraph))))
   /\ goccsT := getONodesStatus(ndl,"goal",empty)
   /\ aoccsT := getONodesStatus(ndl,"avoid",empty)
   /\ ridsT :=  getHiddenRuleIdsT(ndl,empty)
   /\ tm(modname,pnetT) := getVal(es,'tval,'pnet pname, tm('BOOL,'true.Bool))
   /\ res?? := 
        (if modname == 'BOOL  or rids == nil
         then noParse(0)
         else metaReduce([modname],   
                         'getTrans[pnetT2pntlT(pnetT),qidl2qidlT(rids)])
         fi)
   /\ pntlT := (if res?? :: ResultPair 
                then getTerm(res??) 
                else 'nil.PNTransList fi)
**** eliminate junk
   /\ ioccsT := pnetT2occsT(pnetT)
   /\ res2?? := (if res?? :: ResultPair
                then metaReduce([modname],  
                                'relSubnet['pnet[pntlT,ioccsT],
                                           goccsT,'none.Occs,'nil.QidList]) 
                else  noParse(0) 
                fi)
   /\ '`{_`,_`,_`,_`}[pntlT',ioccsT',uoccsT,roccsT] :=
        (if res2?? :: ResultPair
         then getTerm(res2??)
         else '`{_`,_`,_`,_`}['nil.PNTransList,
                              'none.Occs,'none.Occs,'none.Occs]
         fi )
  /\ res3?? := (if res2?? :: ResultPair 
                then metaReduce([modname],'Odiff[goccsT,roccsT]) 
                else  noParse(0) fi)
  /\ ugoccsT := (if res3?? :: ResultPair 
                  then getTerm(res3??) 
                  else 'none.Occs fi)
  /\ ql(toks') := lookupd(getNotes(es,'tval,'pnet pname),"dishname",ql(nil))
  /\ ql(kbname) := 
         lookupd(getNotes(es,'tval,'pnet pname),"kbname",ql('unknown))
  /\ pnotes := 
      (if res3?? :: ResultPair 
       then (("source" := ql('pathnet pname)),
        	   ("requestor" := ql(gname)),
             ("unRchGoals" := tm(modname,ugoccsT)),
             ("gOccs" := tm(modname, goccsT)), 
             ("aOccs" := tm(modname, aoccsT)), 
             ("aRules" := tm(modname, ridsT)), 
             ("rchOccs" := tm(modname, roccsT)),
             ("unusedOccs" := tm(modname, uoccsT)), 
             (if toks' == nil then mt else ("dishname" := ql(toks')) fi),
             ("lolapath" := ql(rids)),
             ("kbname" := ql(kbname))  
            )
       else mt fi)  .


******************************************************************************
**** Lola-2 Lola-2 Lola-2 Lola-2 Lola-2 Lola-2 Lola-2 Lola-2 Lola-2
******************************************************************************
******************************************************************************
**** (askLola2 kbname pname gname pathpname)
**** assumes pname names pnet, gname has goals annotation
**** pnet may be that of graph or a reduced form with no graph
**** sends lola request and waits with lolaReply continuation
**** pathpname is to be used for pathgraph
******************************************************************************
  eq isReq('askLola2) = true .
  eq enabled(wait4s,req('askLola2,ql(kbname pname gname pathpname toks),reqQ)) = 
       not(waiting4(wait4s,'graphics2d)) .

 
crl[askLola2]:
   [nil,
    st(processing(req('askLola2, ql(kbname pname gname pathpname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, 
       (if ?string == ""
        then wait4s
        else (wait4s !  wait4(g2dname, 'askLola2 pname, 
                   creq('lola2Reply,nil,ql(gname pname pathpname toks),reqQ')))
        fi), 
     (reqQ reqQ0), es, log), 
    outQ toks']
   if mname :=  getMaudeName(es)
   /\ g2dname := getG2dName(es) 
   /\  ?string := lola2ReqString(es,kbname,pname,gname,mname)
   /\   toks' := (if ?string == "" 
                  then nil 
                  else g2dname '\n mname '\n str2toks(?string) maudePrompt
                  fi)
   /\ reqQ0 := (if ?string == ""
                then req('logreq,
                         ql('askLola2 'failed kbname pname pathpname toks),
                     req('sendAlert,
                         ql(getG2dVName(es) 'askLola2 'unable 'to 'ask), nil)) 
                else nil fi)   .


  op lola2ReqString : ESet Qid Qid Qid Qid -> String .
  ceq lola2ReqString(es,kbname,pname,gname,mname) =
          (if modname == 'BOOL or ?graph == mtGraph or goccsT == 'none.Occs
           then ""
           else mkLola2Req(setNodeLGOccs(ndl,occsT2occTL(goccsT),nil),
                           mname)
           fi) 
   if tm(modname,pnetT) := getVal(es,'tval,'pnet pname, tm('BOOL,'true.Bool))
   /\ gv(?graph) := getVal(es,'gval,gname,gv(mtGraph))
   /\ goccsT := getONodesStatus(graphNodes(?graph),"goal",empty)
**** reduce graph if not graph of pnet (pnet tmp subnet)
**** assume pnet is subnet of graph source
   /\ ndl := 
       graphNodes(if lookupd(getNotes(es,'gval,gname),"source", ql(nil)) 
                        == ql('pnet pname)
                  then ?graph
                  else pnetT2graph(modname,pnetT,graphNodes(?graph)) fi) .
    

  op lola2ReqTemplate : -> String .
  eq lola2ReqTemplate = 
      "(apply lola2Request %net %task %id %requestor)" .

  op mkLola2Req : NodeList Qid -> String .
  ceq mkLola2Req(ndl,mname) =
     fillTemplate(lola2ReqTemplate,
                  (("%net" :~ squote(netStr)),
                   ("%task" :~ squote(taskStr)),
                   ("%id" :~ squote("0")),
                   ("%requestor" :~ squote(string(mname)))) ) 
  if netStr := mkLola2NetString(ndl)
  /\ taskStr := mkLola2TaskString(ndl) .
  


******************************************************************************
**** (lola2Reply .... ) -- creq
**** if return code is not 0 or subnet pname does not exist then
****    just log a failure report
****    otherwise create a pathnet from the lola reply and store it
******************************************************************************
**** can not be invoked from outside!
  eq enabled(wait4s,
         creq('lola2Reply,g2dname qid toks', 
              ql(gname pname pathpname toks),reqQ))
       = true .

**** qid is the lola return code
**** just the pathsubnet and store it as pathpname
crl[lola2Reply]:
   [nil,
    st(processing(creq('lola2Reply,g2dname qid toks',
                       ql(gname pname pathpname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, (reqQ reqQ0), es', log),
    outQ]
  if {b,es'} := processLola2Reply(es,gname,pname,pathpname,qid,toks')
   /\ reqQ0 :=
         (if b
          then reqQ' 
          else req('logreq,ql('askLola2 'nopath gname pname pathpname),
                 req('sendAlert,
                     ql(getG2dVName(es) 'Lola2Failed 'return-code qid), nil))
          fi) .


  op processLola2Reply : ESet Qid Qid Qid Qid QidList -> BoolESet . 
  ceq processLola2Reply(es,gname,pname,pathpname,qid,toks) =
       {res3?? :: ResultPair,
        (if res3?? :: ResultPair
         then addEntry(es,'tval,'pnet pathpname, pnotes,
                       tm(modname,'pnet[pntlT',ioccsT']))     
         else es fi)}
  if ndl := graphNodes(gvalGraph(getVal(es,'gval, gname,gv(mtGraph))))
   /\ rids :=  **** chatty rule labels as qids
       (if qid == '0 then readLolaResult(ndl,qid 'PATH toks) else nil fi)
   /\ goccsT := getONodesStatus(ndl,"goal",empty)
   /\ aoccsT := getONodesStatus(ndl,"avoid",empty)
   /\ ridsT :=  getHiddenRuleIdsT(ndl,empty)
   /\ tm(modname,pnetT) := getVal(es,'tval,'pnet pname, tm('BOOL,'true.Bool))
   /\ res?? := 
        (if modname == 'BOOL  or qid =/= '0
         then noParse(0)
         else metaReduce([modname],   
                         'getTrans[pnetT2pntlT(pnetT),qidl2qidlT(rids)])
         fi)
   /\ pntlT := (if res?? :: ResultPair 
                then getTerm(res??) 
                else 'nil.PNTransList fi)
**** eliminate junk
   /\ ioccsT := pnetT2occsT(pnetT)
   /\ res2?? := (if res?? :: ResultPair
                then metaReduce([modname],  
                                'relSubnet['pnet[pntlT,ioccsT],
                                           goccsT,'none.Occs,'nil.QidList]) 
                else  noParse(0) 
                fi)
   /\ '`{_`,_`,_`,_`}[pntlT',ioccsT',uoccsT,roccsT] :=
        (if res2?? :: ResultPair
         then getTerm(res2??)
         else '`{_`,_`,_`,_`}['nil.PNTransList,
                              'none.Occs,'none.Occs,'none.Occs]
         fi )
  /\ res3?? := (if res2?? :: ResultPair 
                then metaReduce([modname],'Odiff[goccsT,roccsT]) 
                else  noParse(0) fi)
  /\ ugoccsT := (if res3?? :: ResultPair 
                  then getTerm(res3??) 
                  else 'none.Occs fi)
  /\ ql(toks') := lookupd(getNotes(es,'tval,'pnet pname),"dishname",ql(nil))
  /\ ql(kbname) := 
         lookupd(getNotes(es,'tval,'pnet pname),"kbname",ql('unknown))
  /\ pnotes := 
      (if res3?? :: ResultPair 
       then (("source" := ql('pathnet pname)),
        	   ("requestor" := ql(gname)),
             ("unRchGoals" := tm(modname,ugoccsT)),
             ("gOccs" := tm(modname, goccsT)), 
             ("aOccs" := tm(modname, aoccsT)), 
             ("aRules" := tm(modname, ridsT)), 
             ("rchOccs" := tm(modname, roccsT)),
             ("unusedOccs" := tm(modname, uoccsT)), 
             (if toks' == nil then mt else ("dishname" := ql(toks')) fi),
             ("lolapath" := ql(rids)),
             ("kbname" := ql(kbname))  
            )
       else mt fi)  .


******************************************************************************
******************************************************************************
**** Viewer requests
******************************************************************************
**** displayPetri  
**** makes predefined dish in kb
**** continues with displayDishnet
******************************************************************************
  eq isReq('displayPetri) = true .
  eq enabled(wait4s, req('displayPetri,ql(kbname g2dvname dname toks),reqQ)) 
       = true .

  crl[displayPetri]:
   [nil,
    st(processing(req('displayPetri, ql(kbname g2dvname dname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, incGlobalCounter(es,1), log), outQ]
   if ctr := getGlobalCounter(es) 
   /\ newdname := qid("dish" + ctr)
   /\ reqQ0 :=
       req('predefDish,ql(kbname  dname newdname dname),
            req('displayDishnet,ql(kbname g2dvname newdname toks),
               reqQ')) .


******************************************************************************
**** displayPetriF  
**** makes dish from chattylabels in file in kb
**** continues with displayDishnet
******************************************************************************
  eq isReq('displayPetriF) = true .
  eq enabled(wait4s, req('displayPetri,
                         ql(kbname g2dvname user-dname occsFname toks),reqQ)) 
       = true .

  crl[displayPetriF]:
   [nil,
    st(processing(req('displayPetriF, 
                      ql(kbname g2dvname user-dname occsFname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, incGlobalCounter(es,1), log), outQ]
   if ctr := getGlobalCounter(es) 
   /\ newdname := qid("dish" + ctr)
   /\ reqQ0 :=
       req('dishFromFile,ql(kbname user-dname newdname occsFname),
            req('displayDishnet,ql(kbname g2dvname newdname toks),
               reqQ')) .


******************************************************************************
**** displayDishNet
**** makes and stores pnet for dish in kb
**** makes and stores graph
**** sends graphdescr to gdvname and starts graph listener
**** toks has fname if provided
******************************************************************************
  eq isReq('displayDishnet) = true .
  eq enabled(wait4s, req('displayDishnet,ql(kbname g2dvname dname toks),reqQ)) 
       = true .

  crl[displayDishnet]:
   [nil,
    st(processing(req('displayDishnet, ql(kbname g2dvname dname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, incGlobalCounter(es,2), log), outQ]
   if ctr := getGlobalCounter(es) 
   /\ ctr1 := stringInc(ctr) 
   /\ pname := qid("pnet" + ctr)
   /\ gname := qid("graph" + ctr1)
   /\ reqQ0 :=
       req('dish2pnet,ql(kbname dname pname),
         req('pnet2graph,ql(kbname pname gname),
           req('defineGraph,ql(g2dvname gname),
             req('startListener, ql(gname 'graphreq g2dvname),
             req('showGraph,ql(g2dvname gname toks),
               reqQ'))))) .

******************************************************************************
**** displaySubnet1  combines status setting and subnet production
**** resets graph status, sets it according to toks, and 
**** invokes displaySubnet
******************************************************************************
  eq isReq('displaySubnet1) = true .
  eq enabled(wait4s, req('displaySubnet1,ql(gname g2dvname toks),reqQ)) 
       = true .

  crl[displaySubnet1]:
   [nil,
    st(processing(req('displaySubnet1, ql(gname g2dvname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, es, log),
    outQ]
   if reqQ0 :=
        req('resetStatus,ql(gname g2dvname),
          req('setStatus,ql(gname g2dvname toks),
            req('displaySubnet,ql(gname g2dvname),
                reqQ'))) .



******************************************************************************
**** displaySubnet
**** makes and stores the relevant subnet for the pnet underlying the graph
****  with goals and avoids from graph annotations
**** makes and stores the subnet's graph
**** sends graphdescr to gdvname and starts graph listener
**** toks has fname if provided
******************************************************************************
  eq isReq('displaySubnet) = true .
  eq enabled(wait4s, req('displaySubnet,ql(gname g2dvname toks),reqQ)) 
       = true .

  crl[displaySubnet]:
   [nil,
    st(processing(req('displaySubnet, ql(gname g2dvname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, incGlobalCounter(es,2), log),
    outQ]
   if ctr := getGlobalCounter(es) 
   /\ ctr1 := stringInc(ctr) 
   /\ pname := qid("pnet" + ctr)
   /\ newgname := qid("graph" + ctr1)
   /\ kbname := getGraphKBname(es,gname,'unknown)
   /\ reqQ0 :=
      (if kbname == 'unknown
       then req('logreq,ql('displaySubnet 'unknownKB gname g2dvname toks), 
                 reqQ')
       else req('dgraph2subnet,ql(kbname gname pname),
              req('pnet2graph,ql(kbname pname newgname),
                req('defineGraph,ql(g2dvname newgname),
                 req('startListener, ql(newgname 'graphreq g2dvname),
                  req('showGraph,ql(g2dvname newgname toks),
                      reqQ'))))) 
      fi) .


******************************************************************************
**** displayPath1  combines status setting and path production
**** resets graph status, sets it according to toks, and 
**** invokes displayPath
******************************************************************************
  eq isReq('displayPath1) = true .
  eq enabled(wait4s, req('displayPath1,ql(gname g2dvname toks),reqQ)) 
       = true .

  crl[displayPath1]:
   [nil,
    st(processing(req('displayPath1, ql(gname g2dvname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, es, log),
    outQ]
   if reqQ0 :=
        req('resetStatus,ql(gname g2dvname),
          req('setStatus,ql(gname g2dvname toks),
            req('displayPath,ql(gname g2dvname),
                reqQ'))) .


******************************************************************************
**** displayPath1  combines status setting and path production
**** resets graph status, sets it according to toks, and 
**** invokes displayPath
******************************************************************************
  eq isReq('displayPath2) = true .
  eq enabled(wait4s, req('displayPath2,ql(gname g2dvname toks),reqQ)) 
       = true .

  crl[displayPath2]:
   [nil,
    st(processing(req('displayPath2, ql(gname g2dvname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, wait4s, reqQ reqQ0, es, log),
    outQ]
   if reqQ0 :=
        req('resetStatus,ql(gname g2dvname),
          req('setStatus,ql(gname g2dvname toks),
            req('displayPath,ql(gname g2dvname '2),
                reqQ'))) .



******************************************************************************
**** displayPath
****  asks lola, converts result to pathnet and stores,  
****   if the graph is for a subnet and no new avoids/hides specified, 
****   use that subnet, ow generate an invisible subnet by just
****     removing occ and rule avoids, and possibly doing a forward collect. 
**** makes and stores the pathnet's graph
**** sends graphdescr to gdvname and starts graph listener
**** toks has fname if provided
******************************************************************************
  eq isReq('displayPath) = true .
  eq enabled(wait4s, req('displayPath,ql(gname g2dvname toks),reqQ)) 
       = true .

  crl[displayPath]:
   [nil,
    st(processing(req('displayPath, ql(gname g2dvname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready,
       wait4s,
       reqQ reqQ0,
       es',
       log), outQ]
   if gnotes := getNotes(es,'gval,gname)
   /\  ql(toks') := lookupd(gnotes,"source",ql(nil))
   /\ ql(kbname) := lookupd(gnotes,"kbname",ql('unknown))
   /\ ptype := getNth(0,toks','unknown)    
   /\ pname := getNth(1,toks','unknown) 
***   /\ pnotes := getNotes(es,'tval, ptype pname)
   /\ b := ptype =/= 'pnet or pname == 'unknown  or kbname == 'unknown
   /\ ndl := graphNodes(gvalGraph(getVal(es,'gval,gname,gv(mtGraph))))
   /\ aoccsT := getONodesStatus(ndl,"avoid",empty)
   /\ ridsT :=  getHiddenRuleIdsT(ndl,empty)
   /\ avoid? := (aoccsT =/= 'none.Occs or ridsT =/= 'nil.QidList)
   /\ ctr := getGlobalCounter(es) 
   /\ ctr1 := stringInc(ctr) 
   /\ ctr2 := stringInc(ctr1)
   /\ pathpname := qid("pnet" + ctr)
   /\ newgname := qid("graph" + ctr1)
   /\ newpname := (if avoid? then qid("pnet" + ctr2) else pname fi)
   /\ es' := incGlobalCounter(es,(if avoid? then 3 else 2 fi))
   /\ lola:Qid := (if toks == nil then 'askLola else 'askLola2 fi)
   /\ reqQ2 :=
        req('logreq, 
            ql('displayPath 'failed 'kbname kbname 'ptype ptype 'pname pname 
                gname g2dvname toks),
            reqQ')  
   /\ reqQ1 :=
          req(lola:Qid,ql(kbname newpname gname pathpname),
            req('pnet2graph,ql(kbname pathpname newgname),
              req('defineGraph,ql(g2dvname newgname),
                 req('startListener, ql(newgname 'graphreq g2dvname),
                  req('showGraph,ql(g2dvname newgname toks),
                      reqQ'))))) 
   /\ reqQ0 :=
       (if b 
        then reqQ2
        else (if avoid?
              then req('dgraph2pathsubnet,ql(kbname pname gname newpname), 
                       reqQ1)
              else reqQ1
              fi) 
        fi) .

******************************************************************************
**** manipulating status
******************************************************************************
****  toks are (nid status)*   sets status if allowed
  eq isReq('setStatus) = true .
  eq enabled(wait4s,req('setStatus,ql(gname g2dvname toks),reqQ)) = true .

 crl[setStatus]:
   [nil,
    st(processing(req('setStatus, ql(gname g2dvname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, 
       wait4s, 
       (reqQ req('logreq,ql('setStatus qid gname toks),reqQ')), 
       (if ?graph == mtGraph
        then es
        else addEntry(es, 'gval, gname,  getNotes(es,'gval,gname), 
                      gv(setGraphStatus(?graph, toks)) )
        fi),
       log), 
    outQ]
  if gv(?graph) := getVal(es,'gval,gname,gv(mtGraph)) 
  /\ qid := (if ?graph == mtGraph then 'mtGraph else 'Ok fi) .

******************************************************************************
  eq isReq('resetStatus) = true .
  eq enabled(wait4s,req('resetStatus,ql(gname g2dname toks),reqQ)) = true .

 crl[resetStatus]:
   [nil,
    st(processing(req('resetStatus, ql(gname g2dname toks),reqQ')), 
       wait4s,reqQ,es,log), 
    outQ]
     =>
   [nil,  
    st(ready, 
       wait4s, 
       (reqQ  req('logreq,ql('resetStatus gname toks),reqQ')), 
       (if ?graph == mtGraph
        then es
        else addEntry(es, 'gval, gname, getNotes(es,'gval,gname),
                      gv(resetGraphStatus(?graph)) )
        fi),
       log), 
    outQ]
  if gv(?graph) := getVal(es,'gval,gname,gv(mtGraph)) 
  /\ qid := (if ?graph == mtGraph then 'mtGraph else 'Ok fi) .



  ******************************************************************************
  **** displayPathRMP
  ****  
  ****  gname is name of a subnet
  ****  toks is list of rids in rmp  
  **** makes and stores the pathnet's graph
  **** sends graphdescr to gdvname and starts graph listener

  ******************************************************************************
    eq isReq('displayRMP) = true .
    eq enabled(wait4s, req('displayRMP,ql(gname g2dvname toks),reqQ)) 
         = true .

    crl[displayRMP]:
     [nil,
      st(processing(req('displayRMP, ql(gname g2dvname toks),reqQ')), 
         wait4s,reqQ,es,log), 
      outQ]
       =>
     [nil,  
      st(ready,
         wait4s,
         reqQ reqQ0,
         es',
         log), outQ]
      if gnotes := getNotes(es,'gval,gname)
      /\ ql(toks') := lookupd(gnotes,"source",ql(nil))
      /\ ql(kbname) := lookupd(gnotes,"kbname",ql('unknown))
      /\ ptype := getNth(0,toks','unknown)    
      /\ pname := getNth(1,toks','unknown) 
      /\ b := ptype =/= 'pnet or pname == 'unknown  or kbname == 'unknown
      /\ ndl := graphNodes(gvalGraph(getVal(es,'gval,gname,gv(mtGraph))))
      /\ ctr := getGlobalCounter(es) 
      /\ ctr1 := stringInc(ctr) 
      /\ newgname := qid("graph" + ctr1)
      /\ pathpname := qid("pnet" + ctr)
      /\ es0 := incGlobalCounter(es,2)
      /\ {b0, es'} :=
           (if b
            then {false,es0}
            else processRMP(es0,gname,pname,pathpname,toks) fi)
      /\ reqQ2 :=
           req('logreq, 
               ql('displayRMP 'failed 'kbname kbname 'ptype ptype 'pname pname 
                   gname g2dvname toks),
               reqQ')  
      /\ reqQ1 := req('pnet2graph,ql(kbname pathpname newgname),
                   req('defineGraph,ql(g2dvname newgname),
                    req('startListener, ql(newgname 'graphreq g2dvname),
                     req('showGraph,ql(g2dvname newgname toks),
                         reqQ'))))
     /\ reqQ0 := (if b0 then reqQ1 else reqQ2 fi) .
 

  op processRMP : ESet Qid Qid Qid QidList -> BoolESet . 
  ceq processRMP(es,gname,pname,pathpname,rids) =
           processPath(es,gname,pname,pathpname,ndl,rids)
  if ndl := graphNodes(gvalGraph(getVal(es,'gval, gname,gv(mtGraph)))) .


  ******************************************************************************
***** testing ranks  ******************************************************************************
    var ranksT : Term .
    var fname : Qid .
    
   eq isReq('dish2ranks) = true .
   eq enabled(wait4s, req('dish2ranks,ql(kbname dname fname toks),reqQ)) 
         = true .
   crl[dish2ranks]:
     [nil,
      st(processing(req('dish2ranks, ql(kbname dname fname toks),reqQ')), 
         wait4s,reqQ,es,log), 
      outQ]
       =>
     [nil,  
      st(ready,
         wait4s,
         (reqQ reqQ0),
         es,
         log), outQ] 
  if str := dish2ranks(es,kbname,dname)       
  /\ reqQ0 := req('filewrite, 
                  ql(fname 'C str2toks(str)), 
                  reqQ')  .

 
    op dish2ranks : ESet Qid Qid -> String .
    ceq dish2ranks(es,kbname,dname) = str
     if tm(modname,occsT) := getVal(es,'tval,'dish dname,tm('BOOL,'true.Bool))
     /\ tm(modname',pntlT) := 
             getVal(es,'tval, 'tkb kbname,tm('BOOL,'true.Bool))
     /\ res?? := metaReduce([modname],'fwdCollectRanked[pntlT,occsT])
     /\ b := modname =/= 'BOOL and  modname' =/= 'BOOL and res?? :: ResultPair
     /\ '`{_`,_`,_`,_`,_`}[pntlT',ioccsT,uoccsT,roccsT,ranksT] :=
           (if b
            then getTerm(res??)
            else '`{_`,_`,_`,_`,_`}['nil.PNTransList, 
                    'none.Occs,'none.Occs,'none.Occs,'nil.QidListList]
            fi )
    /\ res0?? := metaReduce([modname],'qidll2str[ranksT])
    /\ str := (if res0?? :: ResultPair 
               then downTerm(getTerm(res0??),"") 
               else "" fi)  .
  ******************************************************************************

endm
