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