***(
Converting ground thing terms GThT to array exp representation

GThT ~ Unit + Complex
Unit ~ Constant + ModT
ModT ~ '`[_-_`][GThT,MsT]
Complex ~ '_:_[GThTList] + '<_>[GThTSoupT]

MsT ~ Constant + 'num[NumeralT] + modQ[SiteT]
SiteT ~ '__[AAT,NumeralT]

)

fmod PL-THING2AEXP is
  inc MY-META-ALL .
  inc STRING .
  inc CONVERSION .
  inc META-PETRI .
  inc TEMPLATES .

  vars mop : Qid .
  vars name type base modstr aestr : String .
  vars strT thC modC aaC : Constant .
  vars thT  pT msT msT' t thT0 thT1 thT2 tsoupT : Term .
  var  tl tl' : TermList .

**** flattening complex bodies to Unit lists
  op csoupT2tl : Term -> TermList .
  op csoupTl2tl : TermList -> TermList .
  
  eq csoupT2tl('empty.Soup) = empty .
  eq csoupT2tl('__[tl]) = csoupTl2tl(tl) .
  eq csoupT2tl('_:_[tl]) = csoupTl2tl(tl) .
  eq csoupT2tl('<_>[tsoupT]) = csoupT2tl(tsoupT) .
  eq csoupT2tl(t) = t  [owise] .

  eq csoupTl2tl(empty) = empty .
  eq csoupTl2tl((t,tl)) = (csoupT2tl(t) , csoupTl2tl(tl)) .

  op mkComplexAexp : String -> String .
  eq mkComplexAexp(aestr) = 
       "(array java.lang.Object " + squote("complex") + " " + aestr + ")" .

  op mkBaseAexp : String String -> String .
  op mkModAexp : String  String -> String .

  eq mkBaseAexp(name,type) =
      "(array java.lang.Object " + squote("base") 
              + " " + squote(name) + " " + squote(type) + ")" .
****           base  modifiers
  eq mkModAexp(aestr,modstr) =
      "(array java.lang.Object " + squote("modified") 
              + " " + aestr + " " + modstr + ")" .

**** to handle malformed "occ" node "occ" attributes
  op thingTl2aexp  :  TermList -> String .
  eq thingTl2aexp(t) = thing2aexp(t) .
  eq thingTl2aexp(tl) = "(object null)"  [owise] .
  
  op thing2aexp  :  Term -> String .
  op unitT2aexp : Term -> String .
  op unitTl2aexp : TermList String -> String .

  eq thing2aexp('_:_[tl]) = mkComplexAexp(unitTl2aexp(csoupTl2tl(tl),"")) .
  eq thing2aexp('<_>[tsoupT]) = 
       mkComplexAexp(unitTl2aexp(csoupT2tl(tsoupT),"")) .
  eq thing2aexp(thT) = unitT2aexp(thT) [owise] .

  eq unitT2aexp(thC) = mkBaseAexp(string(getName(thC)),string(getType(thC))) .
  eq unitT2aexp('`[_-_`][pT,msT]) = mthing2aexp(pT,msT) .
  eq unitT2aexp(thT) = "" [owise] .

  eq unitTl2aexp(empty,aestr) = aestr .
  eq unitTl2aexp((t,tl),aestr) =  unitTl2aexp(tl,aestr + " " + unitT2aexp(t)) .  

**** normalizing nested modifier -- rare
  op mthing2aexp : Term Term -> String .
  eq mthing2aexp(thC,msT) = 
     mkModAexp( mkBaseAexp(string(getName(thC)),string(getType(thC))),
                modTl2aexp(modsT2tl(msT,empty), ""))  .
  eq mthing2aexp('`[_-_`][pT,msT'],msT) = mthing2aexp(pT, '__[msT',msT]) .
  eq mthing2aexp(thT,msT) = "" [owise] .


**** flatten modification set term to list of modifier terms
  op modsT2tl : Term TermList -> TermList .
  op modsTl2tl : TermList TermList -> TermList .

  eq modsT2tl('none.ModSet,tl) = tl .
  eq modsT2tl('__[tl'],tl) = modsTl2tl(tl',tl) .
  eq modsT2tl(t,tl) = (tl, t) [owise] .

  eq modsTl2tl((t,tl'), tl) = modsTl2tl(tl', modsT2tl(t,tl)) .
  eq modsTl2tl(empty, tl) = tl .


**** modification to aexp representation
  op modT2aexp : Term -> String .
  op modTl2aexp : TermList String -> String .

  eq  modTl2aexp(empty,modstr) = modstr .
  eq  modTl2aexp((t,tl),modstr) = modTl2aexp(tl, modstr + " " + modT2aexp(t)) .

  eq  modT2aexp(modC) =
          "(array java.lang.Object " + squote("mod") 
                   + " " + squote(string(getName(modC))) + ")" .
  eq  modT2aexp(mop[t]) =
        (if (mop == 'num)
         then "(array java.lang.Object " + squote("mod")
                     + " " + squote("num")
                     + " " + squote(string(downTerm(t,0),10)) + ")" 
         else   "(array java.lang.Object " + squote("mod")
                       + " " + squote(string(mop)) + " " + site2aexp(t) + ")" 
         fi) .
  eq  modT2aexp(t)  = "" [owise] .

  op site2aexp : Term -> String .
  eq site2aexp(aaC) = squote(string(getName(aaC))) .
  eq site2aexp('__[aaC,t]) =
       squote(string(getName(aaC))) + " " + squote(string(downTerm(t,0),10)) .
  eq site2aexp(t) = "" [owise] .

endfm


fmod TEST-PL-THING2AEXP is
  inc PL-THING2AEXP .

  ops srcB rasB pipB pipD srcP srcPP rasG : -> Term .
  ops gMod pMod sMod yMod nMod :  -> Term .
  ops cplx1 cplx2 cplx3 :  -> Term .
  ops msT csT0 csT1 :  -> Term .

  eq srcB =   'Src.Protein .
  eq rasB =   'Ras.Protein .
  eq pipB = 'PIP2.Chemical .

  eq gMod = 'GTP.Modification .
  eq pMod = 'phos.Modification .
  eq sMod =  'phos['__['S.AminoAcid, upTerm(245)]] .
  eq yMod =  'phos['__['Y.AminoAcid, upTerm(429)]] .
  eq nMod = 'num[upTerm(2)] .
  eq msT = '__[gMod, '__[yMod,'__[nMod,sMod]], '__[pMod,'none.ModSet]] .

  eq srcP = '`[_-_`][srcB,pMod] .
  eq pipD = '`[_-_`][pipB,nMod] .
  eq rasG = '`[_-_`][rasB,gMod] .

  eq srcPP = '`[_-_`][srcB,'__[yMod,sMod]] .

  eq cplx1 = '<_>['__[rasG,srcP]] .
  eq cplx2 = '_:_[rasG,srcP] .
  eq cplx3 = '_:_[cplx1,cplx2] .
  
  eq csT0 = '__[srcPP, cplx3, '_:_[cplx1,pipD]] .
  
endfm

***(

red csoupTl2tl((cplx1,cplx2)) .
red csoupT2tl(csT0)  .
red modsT2tl(yMod,empty) .
red modsT2tl(msT,empty) .
red unitT2aexp(srcB) .
red unitT2aexp(srcP) .
red unitT2aexp(srcPP) .
red unitTl2aexp((rasG, srcP), "") .

red srcB .
red thing2aexp(srcB) .
red srcP .
red thing2aexp(srcP) .
red srcPP .
red thing2aexp(srcPP) .
red pipD .
red thing2aexp(pipD) .
red cplx1 .
red thing2aexp(cplx1) .
red cplx2 .
red thing2aexp(cplx2) .
red cplx3 .
red thing2aexp(cplx3) .
q
)
