ConditionEvent.cpp 11 KB

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