mod OCCS2JSON is
  inc MAUDE2JSON-AUX .
  inc PL-PRINT .
  inc ESET .

***(
A simple occurrence specification (socc)  is an object satisyfing 
{ "stype":"socc",
  "name":"String",          //computed from ename,mods,loc
  "shortname":"String",     // computed from ename,mods,loc
  "ename":String,           // the entity name
  "mods":JArray[String],    // modification names
  "loc":String              // the location name
}

A complex occurrence specification (cocc) is an object satisyfing
{ "stype" :"cocc",
  "name" : "String",          //computed from component specs
  "shortname" : String,       //computed from component specs
  "components" : Components,
  "loc" : String                         
}

Components = JObject[SimpleOccSpec] |  JArray[String],        
  where elements of String should be simpleOcc names in the relevant KB

)

op soccTemplate : -> String .
eq soccTemplate = 
"\n %name:{\"stype\" : \"socc\",\n\
\t \"name\" : %name,\n\
\t \"shortname\" : %shortname,\n\
\t \"ename\" : %ename,\n\
\t \"mods\" : %mods,\n\
\t \"loc\" : %loc\n}"
.


op coccTemplate : -> String .
eq coccTemplate = 
"\n %name:{\"stype\" : \"cocc\",\n\
\t \"name\" : %name,\n\
\t \"shortname\" : %shortname,\n\
\t \"components\" : %components,\n\
\t \"loc\" : %loc\n}"
.

  vars kbname modname qid opid : Qid .
  var qidl : QidList .
  vars str oname uname : String .
  var bindings : TBinds .
  var es : ESet .
  vars onotes  : Notes .
  vars  t occsT occT thT locT tsoupT  pT msT : Term .
  vars tl occTL : TermList .


***(
addEntry(es,'tval,'tkb kbname, pnotes, tm(modname,pntlT))
addEntry(es,'qval,'occmap kbname, onotes, ql(nil))
)
 
**** to provide info to make a mod with site as needed.

  op modSpecTemplate : -> String .
  eq modSpecTemplate = 
"{\"stype\" : \"sitemod\",\
 \"name\" : %name,\
 \"op\" : %op,\
 \"site\" : %site }" .


  op soccT2mspecs : Term -> String .
  eq soccT2mspecs('`[_-_`][pT,msT]) =
      fillTemplate(jarrTemplate, 
                   ("%jvals" :~ modTL2mspecs(modsT2modTL(msT,empty),""))) .
  eq soccT2mspecs(thT) = "[ ]"  [owise] .

  op modTL2mspecs : TermList StringList -> String .
  eq modTL2mspecs(empty,str) = str .
  eq modTL2mspecs((t,tl),str) =
        modTL2mspecs(tl,str + sep?(str,", ") + modT2mspec(t,modT2label(t))) .

  op modT2mspec : Term String  -> String .
  eq modT2mspec(m:Constant,name:String) = squote(name:String) .
**** sitemods
  ceq modT2mspec(opid[site:Constant],name:String) =
     fillTemplate(modSpecTemplate,bindings)
   if opid =/= 'not 
	 /\ bindings := (("%name" :~ squote(name:String)),
                    ("%op" :~ squote(string(opid))),
                    ("%site" :~ squote(string(getName(site:Constant)))) ) .
  ceq modT2mspec(opid['__[aa:Constant,nT:Term]],name:String) =
     fillTemplate(modSpecTemplate,bindings)
   if opid =/= 'not 
	 /\  bindings := (("%name" :~ squote(name:String)),
                    ("%op" :~ squote(string(opid))),
                    ("%site" :~ squote(string(getName(aa:Constant))
                                       + string(downTerm(nT:Term,0),10))) ) .
  eq modT2mspec('not[m:Constant],name:String) = squote(name:String) .

****16apr09  negated mod
***(
  op nmodSpecTemplate : -> String .
  eq nmodSpecTemplate = 
"{\"stype\" : \"sitemod\",\
 \"name\" : %name,\
 \"op\" : %op,\
 \"site\" : %site,\
 \"neg\" : \"true\"  }" .
 )
****   replace "}"  by ", \"neg\" : "true"  }" in str
  ceq modT2mspec('not[t],name:String) =
    substr(str,0,find(str,"}",0)) + ", \"neg\" : \"true\"  }" 
  if str := modT2mspec(t,name:String) [owise] .

  eq modT2mspec(t,name:String) = squote(name:String) [owise] .
	 
  op occs2json :  ESet Qid  -> String .
  op occs2json1 : Notes String -> String .
  op occ2json : String Term String -> String .
  op socc2json : String Term Term String -> String .
  op cocc2json : String Term Term String -> String .

  eq occs2json(es,kbname) = occs2json1(getNotes(es,'qval, 'occmap kbname),"") .
  eq occs2json1(mt,str) = fillTemplate(jobjTemplate, ("%jelts" :~ str)) .
  eq occs2json1(((oname := tm(modname,occT)), onotes), str) = 
      occs2json1(onotes, occ2json(oname,occT,str))  .
 eq occs2json1(((uname := ql(qidl)), onotes), str) = occs2json1(onotes,str)  .

  eq occ2json(oname,occT,str) =
        (if isComplexThing(occT2thT(occT))
         then cocc2json(oname,occT2thT(occT),occT2locT(occT),str)
         else socc2json(oname,occT2thT(occT),occT2locT(occT),str)
         fi) .


 eq socc2json(oname,thT,locT,str) =
     str + sep?(str,",\n") 
     + fillTemplate(soccTemplate, 
                  (("%name" :~ squote(oname)),
                   ("%shortname" :~ squote(thingT2label(thT))),
                   ("%ename" :~ squote(soccT2ename(thT))),
                   ("%mods" :~ soccT2mspecs(thT)),
                   ("%loc" :~ squote(locT2label(locT))) ) ).        

  ceq cocc2json(oname,thT,locT,str) =
     str + sep?(str,",\n") 
     +  fillTemplate(coccTemplate,bindings) 
    if 
      bindings := (("%name" :~ squote(oname)),
                   ("%shortname" :~ squote(thingT2label(thT))),
                   ("%components" :~ cocc2soccs(thT,locT)),
                   ("%loc" :~ squote(locT2label(locT))) ) .        


  op cocc2soccs : Term Term -> String .
  op cocc2soccs1 : TermList Term String -> String .

****!!!! components as full socc specs
  eq cocc2soccs('_:_[tl],locT)   = cocc2soccs1(flattenThingTL(tl,'_:_),locT,"") .
  eq cocc2soccs('<_>[tsoupT],locT) = 
         cocc2soccs1(flattenThingTL(tsoupT,'__),locT,"") .
  eq cocc2soccs(thT,locT) = "" [owise] .
  eq cocc2soccs1(empty,locT,str) = fillTemplate(jobjTemplate, ("%jelts" :~ str)) .
  eq cocc2soccs1((t,tl),locT,str) = 
     cocc2soccs1(tl,locT,socc2json(occT2chatty(t,locT),t,locT,str)) .


endm