123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- #include "stdafx.h"
- #include "ConditionEvent.h"
- #include <iostream>
- //******************条件结构体**************************
- stru_Condition::stru_Condition(string key, string value, string operatorType)
- {
- m_Key = key;
- m_Value = value;
- m_OperatorType = operatorType;
- }
- stru_Condition::stru_Condition(const stru_Condition& cd)
- {
- m_Key = cd.m_Key;
- m_Value = cd.m_Value;
- m_OperatorType = cd.m_OperatorType;
- m_Result.store(cd.m_Result);
- }
- stru_Condition& stru_Condition::operator=(const stru_Condition& cd)
- {
- m_Key = cd.m_Key;
- m_Value = cd.m_Value;
- m_OperatorType = cd.m_OperatorType;
- m_Result.store(cd.m_Result);
- return *this;
- }
- bool stru_Condition::CheckCondition(string& strValue)
- {
- bool result = m_Result;
- try {
- if (m_OperatorType == OperatorType_Relational_Big) //">"
- {
- if (IsDigit(strValue) && IsDigit(m_Value))
- {
- float getfValue = atof(strValue.c_str());
- float conffValue = atof(m_Value.c_str());
- result = getfValue > conffValue;
- }
- else
- result = strValue > m_Value;
- }
- else if (m_OperatorType == OperatorType_Relational_BigAnd) //">="
- {
- if (IsDigit(strValue) && IsDigit(m_Value))
- {
- float getfValue = atof(strValue.c_str());
- float conffValue = atof(m_Value.c_str());
- result = getfValue >= conffValue;
- }
- else
- result = strValue >= m_Value;
- }
- else if (m_OperatorType == OperatorType_Relational_Small) //"<"
- {
- if (IsDigit(strValue) && IsDigit(m_Value))
- {
- float getfValue = atof(strValue.c_str());
- float conffValue = atof(m_Value.c_str());
- result = getfValue < conffValue;
- }
- else
- result = strValue < m_Value;
- }
- else if (m_OperatorType == OperatorType_Relational_SmallAnd) //"<="
- {
- if (IsDigit(strValue) && IsDigit(m_Value))
- {
- float getfValue = atof(strValue.c_str());
- float conffValue = atof(m_Value.c_str());
- result = getfValue <= conffValue;
- }
- else
- result = strValue <= m_Value;
- }
- else if (m_OperatorType == OperatorType_Relational_Equal) //"="
- {
- if (IsDigit(strValue) && IsDigit(m_Value))
- {
- float getfValue = atof(strValue.c_str());
- float conffValue = atof(m_Value.c_str());
- result = getfValue == conffValue;
- }
- else
- result = strValue == m_Value;
- }
- else if (m_OperatorType == OperatorType_Relational_NotEqual) //"!="
- {
- if (IsDigit(strValue) && IsDigit(m_Value))
- {
- float getfValue = atof(strValue.c_str());
- float conffValue = atof(m_Value.c_str());
- result = getfValue != conffValue;
- }
- else
- result = strValue != m_Value;
- }
- else if (m_OperatorType == OperatorType_Relational_IN) //"<>"
- {
- vector<string> valueList = stringSplit(m_Value, ConfItem_split);
- result = (std::find(valueList.begin(), valueList.end(), strValue) != valueList.end());
- }
- else if (m_OperatorType == OperatorType_Relational_NotIN) //"<!>"
- {
- vector<string> valueList = stringSplit(m_Value, ConfItem_split);
- result = (std::find(valueList.begin(), valueList.end(), strValue) == valueList.end());
- }
- else if (m_OperatorType == OperatorType_Relational_Between) //"[]"
- {
- vector<string> valueList = stringSplit(m_Value, ConfItem_split);
- if (valueList.size() == 2)
- {
- float fLeftValue = atof(valueList[0].c_str());
- float fRightValue = atof(valueList[1].c_str());
- float getfValue = atof(strValue.c_str());
- result = getfValue >= fLeftValue && getfValue <= fRightValue;
- }
- }
- else if (m_OperatorType == OperatorType_Relational_NotBetween) //"[!]"
- {
- vector<string> valueList = stringSplit(m_Value, ConfItem_split);
- if (valueList.size() == 2)
- {
- float fLeftValue = atof(valueList[0].c_str());
- float fRightValue = atof(valueList[1].c_str());
- float getfValue = atof(strValue.c_str());
- result = getfValue < fLeftValue || getfValue > fRightValue;
- }
- }
- else
- {
- //mLog::FERROR("unknow OperatorType[{$}]", m_OperatorType.c_str());
- }
- if (m_Result != result)
- {
- m_Result = result;
- return true;
- }
- }
- catch (...)
- {
- //mLog::FERROR("checkCondition crash");
- }
- return false;
- }
- //******************事件定义**************************
- ConditionEvent::ConditionEvent()
- {
- m_CoditionsMap.clear();
- }
- ConditionEvent::ConditionEvent(string& name, ResDataObject* event, map<string, int>* attrCountMap)
- {
- try{
- m_strName = name;
- //Type字段
- if (event->GetKeyCount(ConfItem_Type) > 0)
- {
- m_strType = (string)(*event)[ConfItem_Type];
- }
- else
- {
- //mLog::FWARN("ConditionEvent:[{$}] type is null", m_strName.c_str());
- }
- //From字段
- if (event->GetKeyCount(ConfItem_From) > 0)
- {
- string strFrom = (string)(*event)[ConfItem_From];
- m_FromList = stringSplit(strFrom, ConfItem_split);
- }
- else
- {
- //mLog::FERROR("ConditionEvent:[{$}] From is null", m_strName.c_str());
- return;
- }
- //To字段
- if (event->GetKeyCount(ConfItem_To) > 0)
- {
- m_strTo = (string)(*event)[ConfItem_To];
- }
- else
- {
- //mLog::FERROR("ConditionEvent:[{$}] to is null", m_strName.c_str());
- return;
- }
- //Value字段
- if (event->GetKeyCount(ConfItem_Value) > 0)
- {
- m_Value = (string)(*event)[ConfItem_Value];
- if (m_strType.empty())
- {
- if (m_Value.empty())//无值默认为直接跳转
- {
- m_strType = EventType_External;
- }
- else
- {
- if (IsDigit(m_strType))//纯数字默认为超时
- {
- m_strType = EventType_Timeout;
- }
- else//其余默认为条件
- {
- m_strType = EventType_Conditions;
- }
- }
- }
- if(m_strType == EventType_Conditions)
- {
- RemoveSomeChar(m_Value, ' ');
- m_pExpression.reset(new CcosExpression(CcosExpression::TT_bool));
- list<string> nodeValueList;
- if (m_pExpression->CreateTree(m_Value, nodeValueList))
- {
- for (auto& item : nodeValueList)
- {
- string key, value, opr;
- SplitKeyValueOprString(item, key, value, opr);
- m_CoditionsMap[key] = stru_Condition(key, value, opr);
- if (attrCountMap)
- {
- auto finder = attrCountMap->find(key);
- if (finder != attrCountMap->end())
- {
- finder->second += 1;
- }
- else
- {
- attrCountMap->insert(pair<string, int>(key,1));
- }
- }
- }
- }
- else
- {
- //mLog::FERROR("ConditionEvent:[{$}] CreateTree failed", m_strName.c_str());
- return;
- }
- }
- else if (m_strType == EventType_Timeout)
- {
- m_Value = atoi(m_Value.c_str());//超时定义则强制按数值判断
- }
- else if(m_strType == EventType_External)
- {
- //直接跳转则不在意值为何
- }
- }
- else
- {
- //mLog::FERROR("ConditionEvent:[{$}] value is null", m_strName.c_str());
- }
- //自动设置TriggerHandle
- CreatTriggerHandle();
- }
- catch (...)
- {
- //mLog::FERROR("create ConditionEvent crash");
- }
- }
- ConditionEvent::ConditionEvent(const ConditionEvent& evt)
- {
- m_strName = evt.m_strName;
- m_strType = evt.m_strType;
- m_FromList = evt.m_FromList;
- m_strTo = evt.m_strTo;
- m_CoditionsMap = evt.m_CoditionsMap;
- m_pExpression = evt.m_pExpression;
- CreatTriggerHandle();
- }
- ConditionEvent::~ConditionEvent()
- {
- m_pExpression.reset();
- m_CoditionsMap.clear();
- m_pExpression.reset();
- }
- bool ConditionEvent::CreatTriggerHandle(bool isManual)
- {
- if (m_hTriggered == NULL)
- {
- if (isManual)
- m_hTriggered = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET,false); //一旦触发,需要手动Reset
- else
- m_hTriggered = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false); //触发后自动Reset,用于状态机外部手动触发
- }
- if (m_hTriggered != NULL)
- {
- std::cout << "event[" << m_strName.c_str() << "] Triggerhandle[" << m_hTriggered << "]" << std::endl;
- return true;
- }
- return false;
- }
- bool ConditionEvent::IsTriggered()
- {
- return m_bTriggered;
- }
- std::shared_ptr<LinuxEvent> ConditionEvent::GetTriggerHandle()
- {
- return m_hTriggered;
- }
- void ConditionEvent::ResetTrigger()
- {
- m_hTriggered->ResetEvent();
- m_bTriggered = false;
- for (auto& item : m_CoditionsMap)//条件判断结果全部还原
- {
- item.second.m_Result = false;
- }
- }
- void ConditionEvent::SetTrigger()
- {
- m_bTriggered = true;
- m_hTriggered->SetEvent();
- }
- string ConditionEvent::GetName()
- {
- return m_strName;
- }
- string ConditionEvent::GetType()
- {
- return m_strType;
- }
- vector<string> ConditionEvent::GetFrom()
- {
- return m_FromList;
- }
- string ConditionEvent::GetTo()
- {
- return m_strTo;
- }
- map<string, stru_Condition>* ConditionEvent::GetCoditionsMap()
- {
- if (!m_CoditionsMap.empty())
- {
- return &m_CoditionsMap;
- }
- else
- return nullptr;
- }
- void ConditionEvent::ShowExpression(vector<vector<string>>& strPrint)
- {
- if (m_pExpression)
- {
- m_pExpression->Show(strPrint);
- for (auto item = strPrint.rbegin(); item != strPrint.rend(); ++item)
- {
- for (auto& printing : *item)
- {
- std::cout << printing;
- }
- std::cout << std::endl;
- }
- }
- }
- bool ConditionEvent::CheckConditions(string& strKey, string& strValue, string& strFrom, string& strTo)
- {
- try {
- if (m_strType == EventType_Conditions)
- {
- if (m_FromList.empty() || std::find(m_FromList.begin(), m_FromList.end(), strFrom) != m_FromList.end())
- {
- if (m_CoditionsMap.empty())
- {
- return false;
- }
- //匹配条件查找表
- auto finder = m_CoditionsMap.find(strKey);
- if (finder != m_CoditionsMap.end())
- {
- //Action的Key和Notify的Key对应的value
- if (finder->second.CheckCondition(strValue))
- {
- if (CaculateCondition())
- {
- strTo = m_strTo;
- return true;
- }
- }
- }
- }
- }
- }
- catch (...)
- {
- //mLog::FERROR("CheckConditions crash");
- }
- return false;
- }
- bool ConditionEvent::CheckTimeout(string& strValue, string& strFrom, string& strTo)
- {
- if (m_strType == EventType_Timeout)
- {
- if (m_FromList.empty() || std::find(m_FromList.begin(), m_FromList.end(), strFrom) != m_FromList.end())
- {
- strValue = m_Value;
- strTo = m_strTo;
- return true;
- }
- }
- return false;
- }
- bool ConditionEvent::CheckExternal(string& strFrom, string& strTo)
- {
- if (m_strType == EventType_External)
- {
- if (strFrom.empty())
- {
- if (!m_FromList.empty())
- {
- strTo = m_strTo;
- SetTrigger();
- return true;
- }
- }
- else if (std::find(m_FromList.begin(), m_FromList.end(), strFrom) != m_FromList.end())
- {
- strTo = m_strTo;
- SetTrigger();
- return true;
- }
- }
- return false;
- }
- bool ConditionEvent::CaculateCondition()
- {
- try {
- //所有条件
- bool wholeRes = false;
- if (m_pExpression)
- {
- auto FUN_getValue = [this](string key) -> bool
- {
- string temp = GetKeyValueString(key);
- auto finder = m_CoditionsMap.find(temp);
- if (finder != m_CoditionsMap.end())
- {
- return finder->second.m_Result;
- }
- return false;
- };
- m_pExpression->CalculateExpression(wholeRes, FUN_getValue);
- }
- if (wholeRes != m_bTriggered)
- {
- m_bTriggered = wholeRes;
- if (m_bTriggered)
- {
- m_hTriggered->SetEvent();
- }
- else
- {
- m_hTriggered->ResetEvent();
- }
- return true;
- }
- }
- catch (...)
- {
- //mLog::FERROR("CaculateCondition crash");
- }
- return false;
- }
|