#include "stdafx.h" #include "ConditionEvent.h" #include //******************条件结构体************************** 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 valueList = stringSplit(m_Value, ConfItem_split); result = (std::find(valueList.begin(), valueList.end(), strValue) != valueList.end()); } else if (m_OperatorType == OperatorType_Relational_NotIN) //"" { vector valueList = stringSplit(m_Value, ConfItem_split); result = (std::find(valueList.begin(), valueList.end(), strValue) == valueList.end()); } else if (m_OperatorType == OperatorType_Relational_Between) //"[]" { vector 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 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* 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 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(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 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 ConditionEvent::GetFrom() { return m_FromList; } string ConditionEvent::GetTo() { return m_strTo; } map* ConditionEvent::GetCoditionsMap() { if (!m_CoditionsMap.empty()) { return &m_CoditionsMap; } else return nullptr; } void ConditionEvent::ShowExpression(vector>& 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; }