fmod COMPONENT-INFO is
  inc MY-META-ALL .
  inc META-PETRI .
  inc NAT-QID .
  inc PARSE-SEXP-STRING .
  inc NEW-GRAPH2G2D .


  var next : FindResult .
  vars qid modname opname gname kbname opsort clab : Qid .
  vars qidl toks : QidList .
  vars str str' astr opstr field : String .
  var sexp : Sexp .  

  vars sl sl' sexpl : SexpL . 
  var sexp? : Sexp? . 
  var atm : SAtom .

  var typel : TypeList . var type : Type .
  var attrs : AttrSet .
  var opdecls : OpDeclSet .
  vars occT thT locT : Term .
  vars nodes nodes' : NodeList .


  op getField : Sexp String -> Sexp .  *** returns [field  vals] or [nil]
  op getFieldL : SexpL String -> Sexp .  *** returns [field  vals] or [nil]

  eq getField(atm,str) = [nil] .
  eq getField([sl],str) = getFieldL(sl,str) .

  eq getFieldL(nil,str) = [nil] .
  eq getFieldL([a(str') sl] sl',str) = 
       (if str == str' 
        then [a(str) sl] 
        else getFieldL(sl',str)
        fi) .

**** want string part of atomic string value
  op getFieldStr : SexpL -> String .
  eq getFieldStr(a(str) sexpl) = str .
  eq getFieldStr(sexpl) = "" [owise] .

  op getFieldVal : Sexp String -> String .
  eq getFieldVal(sexp,str) = getFieldStr(getFieldVals(sexp,str)) .

  op getFieldVals : Sexp String -> SexpL .
  eq getFieldVals(sexp,str) = cdrl(sexp2l(getField(sexp,str))) .

**** assumes SexpL is list of atoms
  op sexpl2arrayExp : SexpL String -> String .
  eq sexpl2arrayExp(nil,astr) = 
        fillTemplate("(array java.lang.String %strings )",("%strings" :~ astr)) .
  eq sexpl2arrayExp(a(str) sexpl,astr) = 
           sexpl2arrayExp(sexpl,astr + " " + squote(str)) . 
  eq sexpl2arrayExp(sexp sexpl,astr) = sexpl2arrayExp(sexpl,astr) [owise] .

  op proteinInfoTemplate : -> String .
  eq proteinInfoTemplate = 
    "(apply displayProteinInfo %gname %hugosym %spnum %synonymsArray)" .

****                  gname   metadata 
  op mkProteinInfoSExp : Qid   Sexp -> String .
  eq mkProteinInfoSExp(gname,sexp) =
       fillTemplate(proteinInfoTemplate,
       (("%gname" :~ squote(string(gname))),
        ("%hugosym" :~ squote(getFieldVal(sexp,"hugosym"))),
        ("%spnum" :~ squote(getFieldVal(sexp,"spnumber"))),
        ("%synonymsArray" :~ sexpl2arrayExp(getFieldVals(sexp,"synonyms"),""))
       )) . 

  op chemicalInfoTemplate : -> String .
  eq chemicalInfoTemplate = 
    "(apply displayChemicalInfo %gname %keggcpd %synonymsArray)" .

****                  gname modname opname
  op mkChemicalInfoSExp : Qid Sexp -> String .
  eq mkChemicalInfoSExp(gname,sexp) =
       fillTemplate(chemicalInfoTemplate,
       (("%gname" :~ squote(string(gname))),
        ("%keggcpd" :~ squote(getFieldVal(sexp,"keggcpd"))),
        ("%synonymsArray" :~ sexpl2arrayExp(getFieldVals(sexp,"synonyms"),""))
       )) . 


**** occT2ThT
**** what about the includes attribute?
****  op leastSort : Module Term ~> Type

**** reduce use of meta/graph to the mkComponent/Rule info call

  op getOpMetaData : Qid Qid -> String .
  op getOpMetaData1 : OpDeclSet Qid -> String .
  eq getOpMetaData(modname,opname) =
      getOpMetaData1(upOpDecls(modname,true),opname) .

  eq getOpMetaData1((op qid : typel -> type[attrs].) opdecls, 
                    opname) = 
      (if qid == opname 
       then getMetaData(attrs)
       else getOpMetaData1(opdecls,opname) 
       fi) .
  eq getOpMetaData1(none,opname) = "" .


****12apr28 clt no longer used, replaced by mkComponentInfoExpX
****                  gname modname thT
  op mkComponentInfoExp : Qid Qid Term -> String .
  ceq mkComponentInfoExp(gname,modname,thT) =
      (if hasSort([modname],thT,'Protein)
****       then mkProteinInfoExp(gname,modname,getBaseName(thT)) 
       then mkProteinInfoSExp(gname,sexp)
       else (if hasSort([modname],thT,'Chemical)
             then mkChemicalInfoSExp(gname,sexp)
             else mkOtherInfoExp(gname,modname,leastSort([modname],thT),
                             qidl2str(metaPrettyPrint([modname],thT)))
             fi)  fi) 
  if str := getOpMetaData(modname,getBaseName(thT)) 
  /\ ixs(next,sexp?) := readSexp(str,0,length(str)) 
  /\ sexp := (if sexp? :: Sexp
              then sexp?
              else [nil] fi) .


**** put in meta-petri
  op getBaseName : Term -> Qid .
  eq getBaseName(v:Variable) = getName(v:Variable) .
  eq getBaseName(c:Constant) = getName(c:Constant) .
  eq getBaseName('`[_-_`][(t:Term,tl:TermList)]) = getBaseName(t:Term) .
  eq getBaseName(t:Term) = 'false [owise] .

  op otherInfoTemplate : -> String .
  eq otherInfoTemplate = 
    "(apply displayOtherInfo %gname %sort %opstr)" .

****                  gname modname opsort opstr
  op mkOtherInfoExp : Qid   Qid    Qid     String -> String .
  eq mkOtherInfoExp(gname,modname,opsort,opstr) =
       fillTemplate(otherInfoTemplate,
       (("%gname" :~ squote(string(gname))),
        ("%sort" :~ squote(string(opsort))),
        ("%opstr" :~ squote(opstr))
       )) . 

**** from meta-petri
****  op occT2thT : Term -> TermList .
****  eq occT2thT('<_`,_>[thT,locT]) = thT .
****  eq occT2thT(occT) = empty [owise] .

  op ruleEvidenceTemplate : -> String .
  eq ruleEvidenceTemplate = 
    "(apply ruleEvidence %gname %clab %refArray)" .

  op ql2arrayExp : QidList String -> String .
  eq ql2arrayExp(nil,astr) = 
        fillTemplate("(array java.lang.String %strings )",("%strings" :~ astr)) .
  eq ql2arrayExp(qid qidl,astr) = 
          ql2arrayExp(qidl,astr + " " + squote(string(qid))) . 

****                gname clab pmids
  op mkRuleEvidenceExp : Qid Qid QidList -> String .
  eq mkRuleEvidenceExp(gname,clab,toks) =
       fillTemplate(ruleEvidenceTemplate,
       (("%gname" :~ squote(string(gname))),
        ("%clab" :~ squote(string(clab))),
        ("%refArray" :~ ql2arrayExp(toks,""))
       )) . 



endfm


