**** saving and restoring kbgraph 
***(

(entry gval

(qidlist PLTest)

toks2
(notes
 (note kbname :=(qval PLTest))
 (note source :=(qval tkb PLTest))
 (note title :=(sval  "Graph for KB PLTest" )))

toks3
(gval(graph PLTest(notes)(
    nodelist(node 0 (notes(note chattylabel :=(sval  "Cbl-CLc" )) ...)) ..)
))
)



  ceq parseEntry('`( 'entry etype toks '`)) = 
       (if b and val? :: Val then e(etype,ids,notes,val?) else mt fi)
   if {b,'`( 'qidlist ids '`), toks1} := splitParen(toks)
   /\ {b0,toks2,toks3} := splitParen(toks1)
   /\ notes := (if b0 then parseNotes(toks2) else mt fi)
   /\ val? := (if b then parseVal(toks3) else undefined fi) .

saving KB should produce
(kbgval 
  (kbgraph kbname modname (kbnodes 
     (kbnd nid (occ occql ) )
     (kbnd nid (rule ridT) (pre ... ) (post ...)) )
)
 )


)


fmod KBGVAL-IO is
  inc GVAL-IO .
  inc PNTL2GRAPH .

  vars edl iedl oedl bedl : EdgeList .
  vars ndl nds : NodeList .
  vars nid rid kbname modname : Qid .
  vars qidl qidl0 qidl1 toks0 toks1 : QidList .
  var notes : Notes .
  var nodeId : Nat .
  vars inl onl bnl : NatList .
  var ridT t : Term .
  var graph : Graph .
  var id prestr poststr : String .
  var b : Bool .
  vars res?? res1?? : [ResultPair?] .

  op showKBGVal : Qid GVal -> QidList  .
  eq showKBGVal(modname,gv(graph)) = 
       '`( 'kbgval '\n showKBGraph(modname,graph) '`) .
  eq parseVal( '`( 'kbgval qidl '`) ) = gv(parseKBGraph(qidl)) .

  op showKBGraph : Qid Graph -> QidList  .
  eq showKBGraph(modname, graph(id,notes,ndl,edl)) =
      '`( 'kbgraph qid(id) modname '`( 'kbnodes '\n showKBNodes(ndl) '`) '`) .

  op showKBNodes : NodeList -> QidList .
  eq showKBNodes(nil) = nil .
  eq showKBNodes(nd(nodeId,notes) ndl) = 
      '`( 'kbnd qid(string(nodeId,10)) showKBNotes(notes) '`) '\n
       showKBNodes(ndl) .

  op showKBNotes : Notes -> QidList .
  ceq showKBNotes((notes, "type" := sv("occ") )) =  '`( 'occ  qidl '`)
  if tm(modname,t) := lookupd(notes,"occ",tm('BOOL,'true.Bool)) 
  /\ qidl := (if modname == 'BOOL 
              then nil 
              else metaPrettyPrint([modname],t) 
              fi) .

  ceq showKBNotes((notes, "type" := sv("rule") )) = 
        '`( 'rule qidl '`) 
       '`( 'pre str2qidl(prestr) '`) '`( 'post str2qidl(poststr)  '`)  
  if tm(modname,t) := lookupd(notes,"rule",tm('BOOL,'true.Bool))  
  /\ qidl := (if modname == 'BOOL 
              then nil 
              else metaPrettyPrint([modname],t) 
              fi) 
  /\ sv(prestr) := lookupd(notes, "pre", sv(""))
  /\ sv(poststr) := lookupd(notes, "post", sv("")) .

  eq showKBNotes(notes) = nil [owise] .

  op parseKBGraph : QidList -> Graph .
  eq parseKBGraph('`( 'kbgraph kbname modname '`( 'kbnodes qidl '`) '`)) =
     parseKBNodes(kbname,modname,mt,nil,nil,qidl) .

   eq parseKBGraph(qidl) = mtGraph [owise] .

  op parseKBNodes : Qid Qid Notes NodeList EdgeList QidList -> Graph .
  op parseKBNode : Qid Qid Notes NodeList EdgeList QidList QidList -> Graph .
  
  ceq parseKBNodes(kbname,modname,notes,ndl,edl,qidl) =
        (if b 
         then parseKBNode(kbname,modname,notes,ndl,edl,toks1,toks0) 
         else  graph(string(kbname), notes, ndl, edl)
         fi) 
      if {b,toks0,toks1} := splitParen(qidl) .

  ceq parseKBNode(kbname,modname,notes,ndl,edl,toks1,
             '`( 'kbnd nid '`( 'occ qidl '`) '`) ) =
       parseKBNodes(kbname,modname,notes,ndl nds,edl,toks1)
  if res?? := metaParse([modname],qidl,'Occ)
  /\ res1?? := (if res?? :: ResultPair 
                then metaReduce([modname],getTerm(res??))
                else res?? fi)
  /\ nodeId := rat(string(nid),10) 
  /\ nds := (if res1?? :: ResultPair and nodeId :: Nat
             then mkONode(modname,getTerm(res1??),nodeId)
             else nil
             fi) .


  ceq parseKBNode(kbname,modname,notes,ndl,edl,toks1,
  '`( 'kbnd nid '`( 'rule rid '`) '`( 'pre qidl0 '`) '`( 'post qidl1 '`) '`) ) =
       parseKBNodes(kbname,modname,notes,ndl nds,bedl,toks1)
  if  res?? := metaParse([modname],rid,'Qid)
**** ridT := mkQidConst(rid)
  /\ nodeId := rat(string(nid),10) 
  /\ nds := (if res?? :: ResultPair and nodeId :: Nat
             then mkRNode(modname,getTerm(res??),nodeId,
                          qidl2str(qidl0),qidl2str(qidl1))
             else nil
             fi) 
  /\ inl := qidl2natl(lDiff(qidl0, qidl1))
  /\ onl := qidl2natl(lDiff(qidl1, qidl0))
  /\ bnl := qidl2natl(lDiff(qidl0, lDiff(qidl0, qidl1)))
  /\ iedl := mkIedges(inl,nodeId,edl)
  /\ oedl := mkOedges(onl,nodeId,iedl)
  /\ bedl := mkBedges(bnl,nodeId,oedl)
.
  
endfm


***(
in PLTest
maude load-pl
load ../PLA.ibgraph-io.maude
load test-pla.maude
)
