SNAP Library , Developer Reference  2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
exp.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00004 // Expression-Value-Type
00005 typedef enum {evtUndef, evtFlt, evtStr, evtVec, evtLst} TExpValType;
00006 
00008 // Expression-Value
00009 ClassTPVL(TExpVal, PExpVal, TExpValV, TExpValL, TExpValLN)//{
00010 private:
00011   static PExpVal UndefExpVal;
00012   static PExpVal ZeroExpVal;
00013   TInt ValType;
00014   TAscFlt Flt;
00015   TStr Str;
00016   TExpValV ValV;
00017   TExpValL ValL;
00018 public:
00019   TExpVal(const TExpValType& _ValType=evtUndef): ValType(_ValType){}
00020   TExpVal(const TFlt& _Flt): ValType(evtFlt), Flt(_Flt){}
00021   TExpVal(const double& _Flt): ValType(evtFlt), Flt(TFlt(_Flt)){}
00022   TExpVal(const TStr& _Str): ValType(evtStr), Str(_Str){}
00023   static PExpVal New(const TExpValType& ValType=evtUndef){
00024     return PExpVal(new TExpVal(ValType));}
00025   static PExpVal New(const TFlt& Flt){return PExpVal(new TExpVal(Flt));}
00026   static PExpVal New(const double& Flt){return PExpVal(new TExpVal(Flt));}
00027   static PExpVal New(const TStr& Str){return PExpVal(new TExpVal(Str));}
00028   ~TExpVal(){}
00029   TExpVal(TSIn& SIn):
00030     ValType(SIn), Flt(SIn), Str(SIn), ValV(SIn), ValL(SIn){}
00031   static PExpVal Load(TSIn& SIn){return PExpVal(new TExpVal(SIn));}
00032   void Save(TSOut& SOut) const {
00033     ValType.Save(SOut);
00034     Flt.Save(SOut); Str.Save(SOut); ValV.Save(SOut); ValL.Save(SOut);}
00035 
00036   TExpVal& operator=(const TExpVal&){Fail; return *this;}
00037   bool operator==(const TExpVal& ExpVal) const;
00038   bool operator<(const TExpVal& ExpVal) const;
00039 
00040   void AddToVec(const PExpVal& ExpVal){
00041     IAssert(TExpValType(static_cast<int>(ValType))==evtVec); ValV.Add(ExpVal);}
00042   void AddToLst(const PExpVal& ExpVal){
00043     IAssert(TExpValType(static_cast<int>(ValType))==evtLst); ValL.AddBack(ExpVal);}
00044 
00045   TExpValType GetValType() const {return TExpValType(int(ValType));}
00046   bool IsFltVal(TFlt& _Flt) const {
00047     if (GetValType()==evtFlt){_Flt=Flt; return true;} else {return false;}}
00048   bool IsFltVal(double& _Flt) const {
00049     if (GetValType()==evtFlt){_Flt=Flt; return true;} else {return false;}}
00050   double GetFltVal() const {
00051     if (GetValType()==evtFlt){return Flt;} else {return 0;}}
00052   int GetFltValAsInt(const bool& ThrowExceptP=true) const;
00053   TStr GetStrVal() const {IAssert(GetValType()==evtStr); return Str;}
00054 
00055   void SaveTxt(TOLx& Lx) const;
00056   void SaveTxt(const PSOut& SOut) const;
00057   TStr GetStr() const;
00058 
00059   static PExpVal MkClone(const PExpVal& ExpVal);
00060   static PExpVal GetUndefExpVal();
00061   static PExpVal GetZeroExpVal();
00062 };
00063 
00065 // Expression-Environment
00066 typedef enum {
00067   efatUndef, efatVoid,
00068   efatFlt, efatFltFlt, efatFltStr,
00069   efatStr, efatStrFlt, efatStrStr,
00070   efatStrAny
00071 } TExpFuncArgType;
00072 
00073 ClassTP(TExpEnv, PExpEnv)//{
00074 private:
00075   TRnd Rnd;
00076 public:
00077   TExpEnv(): Rnd(){}
00078   virtual ~TExpEnv(){}
00079   TExpEnv(TSIn& SIn): Rnd(SIn){}
00080   static PExpEnv Load(TSIn& SIn){return new TExpEnv(SIn);}
00081   virtual void Save(TSOut& SOut) const {Rnd.Save(SOut);}
00082 
00083   TExpEnv& operator=(const TExpEnv&){Fail; return *this;}
00084   bool operator==(const TExpEnv&) const {Fail; return false;}
00085 
00086   virtual void PutVarVal(const TStr& /*VarNm*/, const PExpVal& /*ExpVal*/){Fail;}
00087   virtual PExpVal GetVarVal(const TStr& /*VarNm*/, bool& IsVar){
00088     IsVar=false; return TExpVal::GetUndefExpVal();}
00089   virtual PExpVal GetVarVal(const TStr& VarNm){
00090     bool IsVar; return GetVarVal(VarNm, IsVar);}
00091   virtual int GetVars() const {return 0;}
00092   virtual void GetVarNmVal(
00093    const int& /*VarN*/, TStr& /*VarNm*/, PExpVal& /*VarVal*/) const {Fail;}
00094 
00095   virtual PExpVal GetFuncVal(
00096    const TStr& /*FuncNm*/, const TExpValV& /*ArgValV*/, bool& IsFunc){
00097     IsFunc=false; return TExpVal::GetUndefExpVal();}
00098 
00099   virtual TRnd& GetRnd(){return Rnd;}
00100 
00101   static bool IsFuncOk(
00102    const TStr& RqFuncNm, const TExpFuncArgType& RqFuncArgType,
00103    const TStr& FuncNm, const TExpValV& ArgValV);
00104 
00105   static PExpEnv DfExpEnv;
00106 };
00107 
00109 // Expression-BuiltIn-Ids
00110 typedef enum {
00111   // constants
00112   ebi_Undef, ebi_True, ebi_False, ebi_E, ebi_Pi,
00113 
00114   // trigonometric funcions
00115   ebi_Sin, ebi_Cos, ebi_Tan,
00116   ebi_ASin, ebi_ACos, ebi_ATan,
00117   ebi_SinH, ebi_CosH, ebi_TanH,
00118 
00119   // exponential functions
00120   ebi_Pow, ebi_Exp, ebi_Sqr, ebi_Sqrt,
00121   ebi_Log, ebi_Log10,
00122 
00123   // number manipulation functions
00124   ebi_Ceil, ebi_Floor, ebi_Int, ebi_Frac, ebi_Abs,
00125 
00126   // random deviates
00127   ebi_UniDev, ebi_NrmDev, ebi_ExpDev,
00128   ebi_GamDev, ebi_PoiDev, ebi_BinDev,
00129   ebi_UniDevStep, ebi_NrmDevStep, ebi_ExpDevStep,
00130 } TExpBiId;
00131 
00133 // Expression-BuiltIn-Argument-Type
00134 typedef enum {
00135   ebatUndef, ebatVoid, ebatFlt, ebatFltFlt
00136 } TExpBiArgType;
00137 
00139 // Expression-BuiltIn
00140 class TExpBi{
00141 private:
00142   TStrIntH ExpBiNmToIdH;
00143   TIntIntH ExpBiIdToArgTypeH;
00144   PExpVal Val_Undef, Val_True, Val_False, Val_E, Val_Pi;
00145   void AddBi(const TStr& ExpBiNm, const TExpBiId& ExpBiId,
00146    const TExpBiArgType& ExpBiArgType=ebatUndef);
00147 public:
00148   TExpBi();
00149 
00150   TExpBi& operator=(const TExpBi&){Fail; return *this;}
00151   bool operator==(const TExpBi&) const {Fail; return false;}
00152 
00153   bool IsExpBiId(const TStr& ExpBiNm, TExpBiId& ExpBiId);
00154   bool IsExpBiId(const TStr& ExpBiNm){
00155     TExpBiId ExpBiId; return IsExpBiId(ExpBiNm, ExpBiId);}
00156   TExpBiArgType GetExpBiArgType(const TExpBiId& ExpBiId);
00157 
00158   void AssertArgs(const int& RqArgs, const int& ActArgs);
00159   void AssertArgValType(const TExpValType& ExpValType, const PExpVal& ExpVal);
00160 
00161   PExpVal GetBiConstVal(const TExpBiId& ExpBiId);
00162   PExpVal GetBiFuncVal(
00163    const TExpBiId& ExpBiId, const TExpValV& ArgValV, const PExpEnv& ExpEnv);
00164 };
00165 
00167 // Expression-Type
00168 typedef enum {
00169   etUndef,
00170   etVal, etVec, etLst,
00171   etOp, etVar, etBiConst, etFunc, etBiFunc} TExpType;
00172 
00174 // Expression-Operator
00175 typedef enum {
00176   eoUndef,
00177   eoUPlus, eoUMinus,
00178   eoPlus, eoMinus, eoMul, eoDiv, eoIDiv, eoMod,
00179   eoNot, eoAnd, eoOr,
00180   eoEq, eoNEq, eoLss, eoGtr, eoLEq, eoGEq,
00181   eoIf
00182 } TExpOp;
00183 
00185 // Expression
00186 ClassTPV(TExp, PExp, TExpV)//{
00187 private:
00188   static TExpBi ExpBi;
00189   TInt ExpType;
00190   TBool IsParen;
00191   PExpVal ExpVal;
00192   TStr ExpNm;
00193   TInt ExpOp;
00194   TInt ExpBiId;
00195   TExpV ArgExpV;
00196 private:
00197   static const TFSet MulOpSymSet, UAddOpSymSet, AddOpSymSet, RelOpSymSet;
00198   static const TFSet FactExpExpect, MulExpExpect, AddExpExpect;
00199   static const TFSet RelExpExpect, ExpExpect;
00200   static TExpOp GetExpOpFromLxSym(const TLxSym& LxSym);
00201   static TLxSym GetLxSymFromExpOp(const TExpOp& ExpOp);
00202   static PExp LoadTxtFact(TILx& Lx, const TFSet& Expect);
00203   static PExp LoadTxtMulExp(TILx& Lx, const TFSet& Expect);
00204   static PExp LoadTxtAddExp(TILx& Lx, const TFSet& Expect);
00205   static PExp LoadTxtRelExp(TILx& Lx, const TFSet& Expect);
00206   static PExp LoadTxtExp(TILx& Lx, const TFSet& Expect);
00207   void SaveTxtOp(TOLx& Lx) const;
00208   PExpVal EvalExpOp(
00209    const PExpEnv& ExpEnv, const bool& DbgP, TChA& DbgChA);
00210   PExpVal EvalExp(
00211    const PExpEnv& ExpEnv, const bool& DbgP, TChA& DbgChA);
00212 public:
00213   TExp(const TExpType& _ExpType=etUndef):
00214     ExpType(_ExpType), IsParen(false),
00215     ExpVal(), ExpNm(), ExpOp(), ExpBiId(), ArgExpV(){}
00216   TExp(const TExpOp& _ExpOp,
00217    const PExp& Exp1, const PExp& Exp2=NULL, const PExp& Exp3=NULL);
00218   TExp(const PExpVal& _ExpVal);
00219   TExp(const TStr& _VarNm);
00220   TExp(const TStr& _FuncNm, const TExpV& _ArgExpV);
00221   ~TExp(){}
00222   TExp(TSIn& SIn):
00223     ExpType(SIn), IsParen(SIn),
00224     ExpVal(SIn), ExpNm(SIn), ExpOp(SIn), ExpBiId(SIn), ArgExpV(SIn){}
00225   static PExp Load(TSIn& SIn){return PExp(new TExp(SIn));}
00226   void Save(TSOut& SOut) const {
00227     ExpType.Save(SOut); IsParen.Save(SOut);
00228     ExpVal.Save(SOut); ExpNm.Save(SOut); ExpOp.Save(SOut);
00229     ExpBiId.Save(SOut); ArgExpV.Save(SOut);}
00230 
00231   TExp& operator=(const TExp&){Fail; return *this;}
00232   bool operator==(const TExp&) const {Fail; return false;}
00233 
00234   static bool IsExpStrOk(const TStr& ExpStr, TStr& MsgStr){
00235     bool Ok; LoadTxt(ExpStr, Ok, MsgStr); return Ok;}
00236   static PExp LoadTxt(const PSIn& SIn, bool& Ok, TStr& MsgStr,
00237    const TFSet& Expect=TFSet()|syEof);
00238   static PExp LoadTxt(const TStr& ExpStr, bool& Ok, TStr& MsgStr){
00239     PSIn SIn=TStrIn::New(ExpStr); return LoadTxt(SIn, Ok, MsgStr);}
00240   static PExp LoadTxt(const TStr& ExpStr){
00241     bool Ok; TStr MsgStr; return LoadTxt(ExpStr, Ok, MsgStr);}
00242 
00243   void SaveTxt(TOLx& Lx) const;
00244   void SaveTxt(const PSOut& SOut) const {
00245     TOLx Lx(SOut, TFSet()|oloCmtAlw|oloSigNum|oloCsSens); SaveTxt(Lx);}
00246   TStr GetStr() const;
00247   TStr GetTopObjNm() const;
00248   int GetArgExps() const;
00249   TStr GetArgExpStr(const int& ArgExpN) const;
00250 
00251   PExpVal Eval(bool& Ok, TStr& MsgStr, const bool& DbgP, TStr& DbgStr,
00252    const PExpEnv& ExpEnv=TExpEnv::DfExpEnv);
00253   PExpVal Eval(const PExpEnv& ExpEnv=TExpEnv::DfExpEnv){
00254     bool Ok; TStr MsgStr; TStr DbgStr;
00255     return Eval(Ok, MsgStr, false, DbgStr, ExpEnv);}
00256   PExpVal Eval(bool& Ok, TStr& MsgStr, const PExpEnv& ExpEnv=TExpEnv::DfExpEnv){
00257     TStr DbgStr; return Eval(Ok, MsgStr, false, DbgStr, ExpEnv);}
00258   PExpVal Eval(const PExpEnv& ExpEnv, const bool& DbgP, TStr& DbgStr){
00259     bool Ok; TStr MsgStr; return Eval(Ok, MsgStr, DbgP, DbgStr, ExpEnv);}
00260 
00261   static PExpVal LoadAndEvalExpL(
00262    const TStr& ExpLStr, bool& Ok, TStr& MsgStr,
00263    const PExpEnv& ExpEnv=TExpEnv::DfExpEnv);
00264 
00265   static TStr GetExpTypeStr(const TExpType& ExpType);
00266   static TStr GetExpOpStr(const TExpOp& ExpOp){
00267     return TLxSymStr::GetSymStr(TExp::GetLxSymFromExpOp(ExpOp));}
00268   static void GetBiDescV(TStrPrV& BiDescV);
00269 };
00270 
00272 // Expression-Help-Item
00273 ClassTPV(TExpHelpItem, PExpHelpItem, TExpHelpItemV)//{
00274 private:
00275   TStr Nm;
00276   TStr TypeStr;
00277   TStr DescStr;
00278   TStr DfValStr;
00279 public:
00280   TExpHelpItem(): Nm(), TypeStr(), DescStr(), DfValStr(){}
00281   TExpHelpItem(const TStr& _Nm, const TStr& _TypeStr,
00282    const TStr& _DescStr, const TStr& _DfValStr):
00283     Nm(_Nm), TypeStr(_TypeStr), DescStr(_DescStr), DfValStr(_DfValStr){}
00284   static PExpHelpItem New(const TStr& Nm, const TStr& TypeStr,
00285    const TStr& DescStr, const TStr& DfValStr){
00286     return PExpHelpItem(new TExpHelpItem(Nm, TypeStr, DescStr, DfValStr));}
00287   ~TExpHelpItem(){}
00288   TExpHelpItem(TSIn& SIn):
00289     Nm(SIn), TypeStr(SIn), DescStr(SIn), DfValStr(SIn){}
00290   static PExpHelpItem Load(TSIn& SIn){
00291     return PExpHelpItem(new TExpHelpItem(SIn));}
00292   void Save(TSOut& SOut) const {
00293     Nm.Save(SOut); TypeStr.Save(SOut); DescStr.Save(SOut); DfValStr.Save(SOut);}
00294 
00295   TExpHelpItem& operator=(const TExpHelpItem&){Fail; return *this;}
00296   bool operator==(const TExpHelpItem&) const {Fail; return false;}
00297 
00298   // component retrieval
00299   TStr GetNm() const {return Nm;}
00300   TStr GetTypeStr() const {return TypeStr;}
00301   TStr GetDescStr() const {return DescStr;}
00302   TStr GetDfValStr() const {return DfValStr;}
00303 };
00304 
00306 // Expression-Help-Object
00307 typedef enum {ehotUndef, ehotOp, ehotVar, ehotFunc, ehotTempl} TExpHelpObjType;
00308 
00309 ClassTPV(TExpHelpObj, PExpHelpObj, TExpHelpObjV)//{
00310 private:
00311   TInt Type; // TExpHelpObjType
00312   TStr CatNm;
00313   PExpHelpItem HdItem;
00314   TExpHelpItemV ArgItemV;
00315 public:
00316   TExpHelpObj(){}
00317   TExpHelpObj(const TExpHelpObjType& _Type, const TStr& _CatNm,
00318    const PExpHelpItem& _HdItem, const TExpHelpItemV& _ArgItemV):
00319     Type(_Type), CatNm(_CatNm), HdItem(_HdItem), ArgItemV(_ArgItemV){}
00320   static PExpHelpObj New(const TExpHelpObjType& Type, const TStr& CatNm,
00321    const PExpHelpItem& HdItem, const TExpHelpItemV& ArgItemV=TExpHelpItemV()){
00322     return PExpHelpObj(new TExpHelpObj(Type, CatNm, HdItem, ArgItemV));}
00323   ~TExpHelpObj(){}
00324   TExpHelpObj(TSIn& SIn):
00325     Type(SIn), CatNm(SIn), HdItem(SIn), ArgItemV(SIn){}
00326   static PExpHelpObj Load(TSIn& SIn){
00327     return PExpHelpObj(new TExpHelpObj(SIn));}
00328   void Save(TSOut& SOut) const {
00329     Type.Save(SOut); CatNm.Save(SOut); HdItem.Save(SOut); ArgItemV.Save(SOut);}
00330 
00331   TExpHelpObj& operator=(const TExpHelpObj&){Fail; return *this;}
00332   bool operator==(const TExpHelpObj&) const {Fail; return false;}
00333 
00334   // component retrieval
00335   TExpHelpObjType GetType() const {return TExpHelpObjType(int(Type));}
00336   TStr GetCatNm() const {return CatNm;}
00337   PExpHelpItem GetHdItem() const {return HdItem;}
00338   int GetArgItems() const {return ArgItemV.Len();}
00339   PExpHelpItem GetArgItem(const int& ArgItemN) const {
00340     return ArgItemV[ArgItemN];}
00341 
00342   // strings
00343   TStr GetHdArgNmStr() const;
00344 
00345   // object type
00346   static TExpHelpObjType GetObjTypeFromStr(const TStr& TypeStr);
00347 };
00348 
00350 // Expression-Help
00351 ClassTP(TExpHelp, PExpHelp)//{
00352 private:
00353   TExpHelpObjV ObjV;
00354 public:
00355   TExpHelp(): ObjV(){}
00356   static PExpHelp New(){return PExpHelp(new TExpHelp());}
00357   ~TExpHelp(){}
00358   TExpHelp(TSIn& SIn): ObjV(SIn){}
00359   static PExpHelp Load(TSIn& SIn){return PExpHelp(new TExpHelp(SIn));}
00360   void Save(TSOut& SOut) const {ObjV.Save(SOut);}
00361 
00362   TExpHelp& operator=(const TExpHelp&){Fail; return *this;}
00363   bool operator==(const TExpHelp&) const {Fail; return false;}
00364 
00365   static PExpHelp LoadXml(const PSIn& SIn);
00366   static PExpHelp LoadXml(const TStr& FNm){
00367     PSIn SIn=TFIn::New(FNm); return LoadXml(SIn);}
00368 
00369   void AddObj(const PExpHelpObj& Obj){ObjV.Add(Obj);}
00370 
00371   void GetCatNmV(TStrV& CatNmV) const;
00372   void GetObjHdNmV(const TStr& CatNm, TStrV& ObjHdNmV) const;
00373 
00374   PExpHelpObj GetObj(const TStr& ObjNm) const;
00375 };
00376