ConditionEvent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include "stdafx.h"
  2. #include "ConditionEvent.h"
  3. #include <iostream>
  4. //******************条件结构体**************************
  5. stru_Condition::stru_Condition(string key, string value, string operatorType)
  6. {
  7. m_Key = key;
  8. m_Value = value;
  9. m_OperatorType = operatorType;
  10. }
  11. stru_Condition::stru_Condition(const stru_Condition& cd)
  12. {
  13. m_Key = cd.m_Key;
  14. m_Value = cd.m_Value;
  15. m_OperatorType = cd.m_OperatorType;
  16. m_Result.store(cd.m_Result);
  17. }
  18. stru_Condition& stru_Condition::operator=(const stru_Condition& cd)
  19. {
  20. m_Key = cd.m_Key;
  21. m_Value = cd.m_Value;
  22. m_OperatorType = cd.m_OperatorType;
  23. m_Result.store(cd.m_Result);
  24. return *this;
  25. }
  26. bool stru_Condition::CheckCondition(string& strValue)
  27. {
  28. bool result = m_Result;
  29. try {
  30. if (m_OperatorType == OperatorType_Relational_Big) //">"
  31. {
  32. if (IsDigit(strValue) && IsDigit(m_Value))
  33. {
  34. float getfValue = atof(strValue.c_str());
  35. float conffValue = atof(m_Value.c_str());
  36. result = getfValue > conffValue;
  37. }
  38. else
  39. result = strValue > m_Value;
  40. }
  41. else if (m_OperatorType == OperatorType_Relational_BigAnd) //">="
  42. {
  43. if (IsDigit(strValue) && IsDigit(m_Value))
  44. {
  45. float getfValue = atof(strValue.c_str());
  46. float conffValue = atof(m_Value.c_str());
  47. result = getfValue >= conffValue;
  48. }
  49. else
  50. result = strValue >= m_Value;
  51. }
  52. else if (m_OperatorType == OperatorType_Relational_Small) //"<"
  53. {
  54. if (IsDigit(strValue) && IsDigit(m_Value))
  55. {
  56. float getfValue = atof(strValue.c_str());
  57. float conffValue = atof(m_Value.c_str());
  58. result = getfValue < conffValue;
  59. }
  60. else
  61. result = strValue < m_Value;
  62. }
  63. else if (m_OperatorType == OperatorType_Relational_SmallAnd) //"<="
  64. {
  65. if (IsDigit(strValue) && IsDigit(m_Value))
  66. {
  67. float getfValue = atof(strValue.c_str());
  68. float conffValue = atof(m_Value.c_str());
  69. result = getfValue <= conffValue;
  70. }
  71. else
  72. result = strValue <= m_Value;
  73. }
  74. else if (m_OperatorType == OperatorType_Relational_Equal) //"="
  75. {
  76. if (IsDigit(strValue) && IsDigit(m_Value))
  77. {
  78. float getfValue = atof(strValue.c_str());
  79. float conffValue = atof(m_Value.c_str());
  80. result = getfValue == conffValue;
  81. }
  82. else
  83. result = strValue == m_Value;
  84. }
  85. else if (m_OperatorType == OperatorType_Relational_NotEqual) //"!="
  86. {
  87. if (IsDigit(strValue) && IsDigit(m_Value))
  88. {
  89. float getfValue = atof(strValue.c_str());
  90. float conffValue = atof(m_Value.c_str());
  91. result = getfValue != conffValue;
  92. }
  93. else
  94. result = strValue != m_Value;
  95. }
  96. else if (m_OperatorType == OperatorType_Relational_IN) //"<>"
  97. {
  98. vector<string> valueList = stringSplit(m_Value, ConfItem_split);
  99. result = (std::find(valueList.begin(), valueList.end(), strValue) != valueList.end());
  100. }
  101. else if (m_OperatorType == OperatorType_Relational_NotIN) //"<!>"
  102. {
  103. vector<string> valueList = stringSplit(m_Value, ConfItem_split);
  104. result = (std::find(valueList.begin(), valueList.end(), strValue) == valueList.end());
  105. }
  106. else if (m_OperatorType == OperatorType_Relational_Between) //"[]"
  107. {
  108. vector<string> valueList = stringSplit(m_Value, ConfItem_split);
  109. if (valueList.size() == 2)
  110. {
  111. float fLeftValue = atof(valueList[0].c_str());
  112. float fRightValue = atof(valueList[1].c_str());
  113. float getfValue = atof(strValue.c_str());
  114. result = getfValue >= fLeftValue && getfValue <= fRightValue;
  115. }
  116. }
  117. else if (m_OperatorType == OperatorType_Relational_NotBetween) //"[!]"
  118. {
  119. vector<string> valueList = stringSplit(m_Value, ConfItem_split);
  120. if (valueList.size() == 2)
  121. {
  122. float fLeftValue = atof(valueList[0].c_str());
  123. float fRightValue = atof(valueList[1].c_str());
  124. float getfValue = atof(strValue.c_str());
  125. result = getfValue < fLeftValue || getfValue > fRightValue;
  126. }
  127. }
  128. else
  129. {
  130. //mLog::FERROR("unknow OperatorType[{$}]", m_OperatorType.c_str());
  131. }
  132. if (m_Result != result)
  133. {
  134. m_Result = result;
  135. return true;
  136. }
  137. }
  138. catch (...)
  139. {
  140. //mLog::FERROR("checkCondition crash");
  141. }
  142. return false;
  143. }
  144. //******************事件定义**************************
  145. ConditionEvent::ConditionEvent()
  146. {
  147. m_CoditionsMap.clear();
  148. }
  149. ConditionEvent::ConditionEvent(string& name, ResDataObject* event, map<string, int>* attrCountMap)
  150. {
  151. try{
  152. m_strName = name;
  153. //Type字段
  154. if (event->GetKeyCount(ConfItem_Type) > 0)
  155. {
  156. m_strType = (string)(*event)[ConfItem_Type];
  157. }
  158. else
  159. {
  160. //mLog::FWARN("ConditionEvent:[{$}] type is null", m_strName.c_str());
  161. }
  162. //From字段
  163. if (event->GetKeyCount(ConfItem_From) > 0)
  164. {
  165. string strFrom = (string)(*event)[ConfItem_From];
  166. m_FromList = stringSplit(strFrom, ConfItem_split);
  167. }
  168. else
  169. {
  170. //mLog::FERROR("ConditionEvent:[{$}] From is null", m_strName.c_str());
  171. return;
  172. }
  173. //To字段
  174. if (event->GetKeyCount(ConfItem_To) > 0)
  175. {
  176. m_strTo = (string)(*event)[ConfItem_To];
  177. }
  178. else
  179. {
  180. //mLog::FERROR("ConditionEvent:[{$}] to is null", m_strName.c_str());
  181. return;
  182. }
  183. //Value字段
  184. if (event->GetKeyCount(ConfItem_Value) > 0)
  185. {
  186. m_Value = (string)(*event)[ConfItem_Value];
  187. if (m_strType.empty())
  188. {
  189. if (m_Value.empty())//无值默认为直接跳转
  190. {
  191. m_strType = EventType_External;
  192. }
  193. else
  194. {
  195. if (IsDigit(m_strType))//纯数字默认为超时
  196. {
  197. m_strType = EventType_Timeout;
  198. }
  199. else//其余默认为条件
  200. {
  201. m_strType = EventType_Conditions;
  202. }
  203. }
  204. }
  205. if(m_strType == EventType_Conditions)
  206. {
  207. RemoveSomeChar(m_Value, ' ');
  208. m_pExpression.reset(new CcosExpression(CcosExpression::TT_bool));
  209. list<string> nodeValueList;
  210. if (m_pExpression->CreateTree(m_Value, nodeValueList))
  211. {
  212. for (auto& item : nodeValueList)
  213. {
  214. string key, value, opr;
  215. SplitKeyValueOprString(item, key, value, opr);
  216. m_CoditionsMap[key] = stru_Condition(key, value, opr);
  217. if (attrCountMap)
  218. {
  219. auto finder = attrCountMap->find(key);
  220. if (finder != attrCountMap->end())
  221. {
  222. finder->second += 1;
  223. }
  224. else
  225. {
  226. attrCountMap->insert(pair<string, int>(key,1));
  227. }
  228. }
  229. }
  230. }
  231. else
  232. {
  233. //mLog::FERROR("ConditionEvent:[{$}] CreateTree failed", m_strName.c_str());
  234. return;
  235. }
  236. }
  237. else if (m_strType == EventType_Timeout)
  238. {
  239. m_Value = atoi(m_Value.c_str());//超时定义则强制按数值判断
  240. }
  241. else if(m_strType == EventType_External)
  242. {
  243. //直接跳转则不在意值为何
  244. }
  245. }
  246. else
  247. {
  248. //mLog::FERROR("ConditionEvent:[{$}] value is null", m_strName.c_str());
  249. }
  250. //自动设置TriggerHandle
  251. CreatTriggerHandle();
  252. }
  253. catch (...)
  254. {
  255. //mLog::FERROR("create ConditionEvent crash");
  256. }
  257. }
  258. ConditionEvent::ConditionEvent(const ConditionEvent& evt)
  259. {
  260. m_strName = evt.m_strName;
  261. m_strType = evt.m_strType;
  262. m_FromList = evt.m_FromList;
  263. m_strTo = evt.m_strTo;
  264. m_CoditionsMap = evt.m_CoditionsMap;
  265. m_pExpression = evt.m_pExpression;
  266. CreatTriggerHandle();
  267. }
  268. ConditionEvent::~ConditionEvent()
  269. {
  270. m_pExpression.reset();
  271. m_CoditionsMap.clear();
  272. m_pExpression.reset();
  273. }
  274. bool ConditionEvent::CreatTriggerHandle(bool isManual)
  275. {
  276. if (m_hTriggered == NULL)
  277. {
  278. if (isManual)
  279. m_hTriggered = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET,false); //一旦触发,需要手动Reset
  280. else
  281. m_hTriggered = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false); //触发后自动Reset,用于状态机外部手动触发
  282. }
  283. if (m_hTriggered != NULL)
  284. {
  285. std::cout << "event[" << m_strName.c_str() << "] Triggerhandle[" << m_hTriggered << "]" << std::endl;
  286. return true;
  287. }
  288. return false;
  289. }
  290. bool ConditionEvent::IsTriggered()
  291. {
  292. return m_bTriggered;
  293. }
  294. std::shared_ptr<LinuxEvent> ConditionEvent::GetTriggerHandle()
  295. {
  296. return m_hTriggered;
  297. }
  298. void ConditionEvent::ResetTrigger()
  299. {
  300. m_hTriggered->ResetEvent();
  301. m_bTriggered = false;
  302. for (auto& item : m_CoditionsMap)//条件判断结果全部还原
  303. {
  304. item.second.m_Result = false;
  305. }
  306. }
  307. void ConditionEvent::SetTrigger()
  308. {
  309. m_bTriggered = true;
  310. m_hTriggered->SetEvent();
  311. }
  312. string ConditionEvent::GetName()
  313. {
  314. return m_strName;
  315. }
  316. string ConditionEvent::GetType()
  317. {
  318. return m_strType;
  319. }
  320. vector<string> ConditionEvent::GetFrom()
  321. {
  322. return m_FromList;
  323. }
  324. string ConditionEvent::GetTo()
  325. {
  326. return m_strTo;
  327. }
  328. map<string, stru_Condition>* ConditionEvent::GetCoditionsMap()
  329. {
  330. if (!m_CoditionsMap.empty())
  331. {
  332. return &m_CoditionsMap;
  333. }
  334. else
  335. return nullptr;
  336. }
  337. void ConditionEvent::ShowExpression(vector<vector<string>>& strPrint)
  338. {
  339. if (m_pExpression)
  340. {
  341. m_pExpression->Show(strPrint);
  342. for (auto item = strPrint.rbegin(); item != strPrint.rend(); ++item)
  343. {
  344. for (auto& printing : *item)
  345. {
  346. std::cout << printing;
  347. }
  348. std::cout << std::endl;
  349. }
  350. }
  351. }
  352. bool ConditionEvent::CheckConditions(string& strKey, string& strValue, string& strFrom, string& strTo)
  353. {
  354. try {
  355. if (m_strType == EventType_Conditions)
  356. {
  357. if (m_FromList.empty() || std::find(m_FromList.begin(), m_FromList.end(), strFrom) != m_FromList.end())
  358. {
  359. if (m_CoditionsMap.empty())
  360. {
  361. return false;
  362. }
  363. //匹配条件查找表
  364. auto finder = m_CoditionsMap.find(strKey);
  365. if (finder != m_CoditionsMap.end())
  366. {
  367. //Action的Key和Notify的Key对应的value
  368. if (finder->second.CheckCondition(strValue))
  369. {
  370. if (CaculateCondition())
  371. {
  372. strTo = m_strTo;
  373. return true;
  374. }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. catch (...)
  381. {
  382. //mLog::FERROR("CheckConditions crash");
  383. }
  384. return false;
  385. }
  386. bool ConditionEvent::CheckTimeout(string& strValue, string& strFrom, string& strTo)
  387. {
  388. if (m_strType == EventType_Timeout)
  389. {
  390. if (m_FromList.empty() || std::find(m_FromList.begin(), m_FromList.end(), strFrom) != m_FromList.end())
  391. {
  392. strValue = m_Value;
  393. strTo = m_strTo;
  394. return true;
  395. }
  396. }
  397. return false;
  398. }
  399. bool ConditionEvent::CheckExternal(string& strFrom, string& strTo)
  400. {
  401. if (m_strType == EventType_External)
  402. {
  403. if (strFrom.empty())
  404. {
  405. if (!m_FromList.empty())
  406. {
  407. strTo = m_strTo;
  408. SetTrigger();
  409. return true;
  410. }
  411. }
  412. else if (std::find(m_FromList.begin(), m_FromList.end(), strFrom) != m_FromList.end())
  413. {
  414. strTo = m_strTo;
  415. SetTrigger();
  416. return true;
  417. }
  418. }
  419. return false;
  420. }
  421. bool ConditionEvent::CaculateCondition()
  422. {
  423. try {
  424. //所有条件
  425. bool wholeRes = false;
  426. if (m_pExpression)
  427. {
  428. auto FUN_getValue = [this](string key) -> bool
  429. {
  430. string temp = GetKeyValueString(key);
  431. auto finder = m_CoditionsMap.find(temp);
  432. if (finder != m_CoditionsMap.end())
  433. {
  434. return finder->second.m_Result;
  435. }
  436. return false;
  437. };
  438. m_pExpression->CalculateExpression(wholeRes, FUN_getValue);
  439. }
  440. if (wholeRes != m_bTriggered)
  441. {
  442. m_bTriggered = wholeRes;
  443. if (m_bTriggered)
  444. {
  445. m_hTriggered->SetEvent();
  446. }
  447. else
  448. {
  449. m_hTriggered->ResetEvent();
  450. }
  451. return true;
  452. }
  453. }
  454. catch (...)
  455. {
  456. //mLog::FERROR("CaculateCondition crash");
  457. }
  458. return false;
  459. }