| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 | #pragma  once#include "stdafx.h"#include "ParticleClassifyEngine.h"#include "ParticleEngine/LogicExp.h"#include "XMLSerialization.h"#include <map>#include <Element.h>#include "OTSSTDLibFileMgr.h"#include "COTSUtilityDllFunExport.h"namespace OTSClassifyEngine{	using namespace expInterpreter;bool ParticleClassifyEngine::Init(){	CSTDLibFileMgrPtr pLibFileMgr = CSTDLibFileMgrPtr(new CSTDLibFileMgr(m_StrName));		m_std = ParticleSTDPtr(new ParticleSTD());	if (!pLibFileMgr->LoadPartSTD(m_std))	{		return FALSE;	}	 pLibFileMgr->LoadMaxEDSRulesData(m_std);	 pLibFileMgr->LoadZeroElementRulesData(m_std);		 string constantStr = pLibFileMgr->LoadConstantsData();	 std::map<std::string, double> m_mapConstants;	 m_mapConstants.clear();	 std::vector<std::string> strs;	 xmls::SplitString(constantStr, strs, ",");	 for (std::string s : strs)	 {		 std::vector<std::string> oneExp;		 xmls::SplitString(s, oneExp, "=");		 m_mapConstants[oneExp[0]] = std::atof(oneExp[1].c_str());	 }	 m_std->setConstantsMap(m_mapConstants);	return true;}bool ParticleClassifyEngine::Classify(COTSParticlePtr particle, CPosXrayPtr xray){	if (particle != nullptr && xray != nullptr)	{		auto& originalPartEles = xray->GetElementQuantifyData();//find all the elements containing in the particle xray.		//以下为调试用代码段(在log中打出颗粒元素),不要删除----------		/*std::string allele=std::to_string(particle->GetTagId()) + " ";		for (auto che : originalPartEles)		{			allele += che->GetName().GetBuffer() ;			allele += ":";			allele += std::to_string(che->GetPercentage()).c_str();			allele += "  ";		}		LogTrace(__FILE__, __LINE__, allele.c_str());*/		//-----------------------------		//zero element process,if satisfied the condition than erase the element from the list.		auto partEles = ZeroElementProcess(particle, xray);//auto partEles = xray->GetElementQuantifyData();		std::map<std::string, CElementChemistryPtr> mapChemistrys;		for (auto ch : partEles)		{			mapChemistrys[ch->GetName().GetBuffer()] = ch;		}		PartSTDItemList stdItems = m_std->GetSTDItems();		//以下为调试用代码段(在log中打出颗粒元素),不要删除----------		/*CString stdnum = _T("STDNum ")+ CString(std::to_string(stdItems.size()).c_str());		LogTrace(__FILE__, __LINE__, stdnum);*/		for (auto itm : stdItems)		{			std::string exp = itm->GetExpressionStr();			//LogTrace(__FILE__, __LINE__, exp.c_str());			//if the element quantity is not match the std item's keyelement num than  is unsatisfied.			if (partEles.size() < itm->GetKeyElementList().size())			{								particle->SetClassifyId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);				particle->TypeName("Not Identified");				particle->TypeColor("#000000");				particle->SetGroupId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);				particle->SetGroupName("Not Identified");				particle->SetGroupColor("#000000");				continue;			}			auto& mapStdEles = itm->GetMapElements();			bool bMatch = true;			// if the particle does not contain all the key elements of the item than is unsatisfied.			for (auto che : mapStdEles)			{				auto chemical = mapChemistrys.find(che.second->GetName().GetBuffer());				if (chemical == mapChemistrys.end())				{					particle->SetClassifyId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);					particle->TypeName("Not Identified");					particle->TypeColor("#000000");					particle->SetGroupId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);					particle->SetGroupName("Not Identified");					particle->SetGroupColor("#000000");					bMatch = false;					break;				}			}			if (!bMatch) continue;			// process the special property name such as "first_elem" and "Element#1" etc.			for (std::string s : itm->GetUsingOtherpropertyList())			{				if (s.find("_elem") != std::string::npos)				{					auto val = GetAtomicNoBySortingPercentage(s.c_str(), xray);					xmls::ReplaceAll(exp, s, std::to_string(val));				}				if (s.find("Element#") != std::string::npos)				{					auto val = GetEleNameBySortingPercentage(s.c_str(), xray);//find the "Element#1" and  replace it with the real element name. 					auto elelist = itm->GetKeyElementList();					elelist.push_back(CElementPtr(new CElement(val)));// then replace it in the next step.					itm->SetKeyElementList(elelist);					xmls::ReplaceAll(exp, s, val.GetBuffer());				}			}			for (auto eleChemistry : itm->GetAllSortedEleList())			{				auto e = mapChemistrys.find(eleChemistry->GetName().GetBuffer());				if (e != mapChemistrys.end())				{					std::string name = eleChemistry->GetName();					xmls::ReplaceAll(exp, name, std::to_string(e->second->GetPercentage()));				}				else				{					std::string name = eleChemistry->GetName();					xmls::ReplaceAll(exp, name, "0");				}			}			//process the image property			for (std::string s : itm->GetUsingImgPropertyNameList())			{				auto val = particle->GetImgPropertyValueByName(s.c_str());				xmls::ReplaceAll(exp, s, std::to_string(val));			}			//process the "true" keyword.			if (exp.find("true") != std::string::npos)			{				xmls::ReplaceAll(exp, "true", "(1=1)");			}			//process the "false" keyword.			if (exp.find("false") != std::string::npos)			{				xmls::ReplaceAll(exp, "false", "(1=0)");			}			for (int i = 0; i < 10; i++)			{				std::string macStr = "MAC#" + std::to_string(i);				if (exp.find(macStr) != std::string::npos)				{					auto val = GetMacValue(macStr.c_str());//find the "Mac#1" and  replace it with the real element name. 					xmls::ReplaceAll(exp, macStr, std::to_string(val));				}			}					//calculate the expression which has been processed.						bool rst = CalcuExp(exp);			//以下为调试用代码段(在log中打出颗粒元素),不要删除----------			/*LogTrace(__FILE__, __LINE__, exp.c_str());			if (rst)			{				LogTrace(__FILE__, __LINE__, CString("true"));			}			else			{				LogTrace(__FILE__, __LINE__, CString("false"));			}*/						if (rst)			{				//int id = itm->GetID();				particle->SetType(OTS_PARTICLE_TYPE::IDENTIFIED);				particle->SetClassifyId(itm->GetID());				particle->TypeColor(itm->GetColor());				particle->TypeName(itm->GetName());				particle->SetHardness(itm->GetHardness());				particle->SetDensity(itm->GetDensity());				particle->SetConductivity(itm->GetElectrical_conductivity());				particle->SetGroupId(itm->GetGrpID());				particle->SetGroupColor(itm->GetGrpColor());				particle->SetGroupName(itm->GetGrpName());				return true;			}			else			{				continue;			}		}		particle->SetType(OTS_PARTICLE_TYPE::NOT_IDENTIFIED);		particle->TypeName("Not Identified");		particle->TypeColor("#000000");		particle->SetGroupId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);		particle->SetGroupName("Not Identified");		particle->SetGroupColor("#000000");		return true;	}	else if(particle != nullptr && xray == nullptr)	{		PartSTDItemList stdItems = m_std->GetSTDItems();		for (auto itm : stdItems)		{			std::string exp = itm->GetExpressionStr();			if (itm->GetKeyElementList().size() > 0 || itm->GetSubElementList().size() > 0)			{				continue;			}			//process the image property			for (std::string s : itm->GetUsingImgPropertyNameList())			{				auto val = particle->GetImgPropertyValueByName(s.c_str());				xmls::ReplaceAll(exp, s, std::to_string(val));			}			//process the "true" keyword.			if (exp.find("true") != std::string::npos)			{				xmls::ReplaceAll(exp, "true", "(1=1)");			}			//process the "false" keyword.			if (exp.find("false") != std::string::npos)			{				xmls::ReplaceAll(exp, "false", "(1=0)");			}			for (int i = 0; i < 10; i++)			{				std::string macStr = "MAC#" + std::to_string(i);				if (exp.find(macStr) != std::string::npos)				{					auto val = GetMacValue(macStr.c_str());//find the "Mac#1" and  replace it with the real element name. 					xmls::ReplaceAll(exp, macStr, std::to_string(val));				}			}						//calculate the expression which has been processed.			bool rst = CalcuExp(exp);			if (rst)			{				//int id = itm->GetID();				particle->SetType(OTS_PARTICLE_TYPE::IDENTIFIED);				particle->SetClassifyId(itm->GetID());				particle->TypeColor(itm->GetColor());				particle->TypeName(itm->GetName());				particle->SetHardness(itm->GetHardness());				particle->SetDensity(itm->GetDensity());				particle->SetConductivity(itm->GetElectrical_conductivity());				particle->SetGroupId(itm->GetGrpID());				particle->SetGroupColor(itm->GetGrpColor());				particle->SetGroupName(itm->GetGrpName());				return true;			}			else			{				continue;			}		}		particle->SetType(OTS_PARTICLE_TYPE::NOT_IDENTIFIED);		particle->TypeName("Not Identified");		particle->TypeColor("#000000");		particle->SetGroupId((int)OTS_PARTICLE_TYPE::NOT_IDENTIFIED);		particle->SetGroupColor("#000000");		particle->SetGroupName("Not Identified");		return true;	}}bool ParticleClassifyEngine::Classify(COTSParticlePtr particle, int SteelTech, CPosXrayPtr xray){	throw std::logic_error("The method or operation is not implemented.");}bool ParticleClassifyEngine::IfNeedMaxEDS(COTSParticlePtr particle, CPosXrayPtr xray, double& MaxEDSTime){	if (particle == nullptr || xray == nullptr) return false;	MaxEDSRuleList Rules = m_std->GetMaxEDSRules();	for (auto rule : Rules)	{		std::string exp = rule->m_expressionStr;		auto& partEles = xray->GetElementQuantifyData();//find all the elements containing in the particle xray.		if (partEles.size() < rule->m_elementList.size())		{			continue;// if the size not match then continue.		}		std::map<CString, CElementChemistryPtr> mapChe;		for (auto ch : partEles)		{			mapChe[ch->GetName()] = ch;		}		auto& usingEles = rule->m_elementList;		bool bMatch=true;		for (auto che : usingEles)		{			auto chemical = mapChe.find(che->GetName());			if (chemical == mapChe.end())			{				bMatch = false;				break;							}		}		if (!bMatch) continue;//if cann't find the element in the particle's element,then continue.		// all the rule's using element are contained in the particle then we replace the element with the percentage value		for (std::string s : rule->m_OtherpropertyList)		{			if (s.find("_elem")!=std::string::npos)			{				auto val = GetAtomicNoBySortingPercentage(s.c_str(), xray);								xmls::ReplaceAll(exp, s, std::to_string(val));			}			if (s.find("Element#")!=std::string::npos)			{				auto val = GetEleNameBySortingPercentage(s.c_str(), xray);//find the "Element#1" and  replace it with the real element name. 				auto& elelist = rule->m_elementList;				elelist.push_back(CElementPtr(new CElement(val)));// then replace it in the next step.											xmls::ReplaceAll(exp, s, val.GetBuffer());			}		}		for (auto eleChemistry : rule->m_elementList)		{			auto e = mapChe[eleChemistry->GetName()];			std::string name = eleChemistry->GetName();						xmls::ReplaceAll(exp, name, std::to_string(e->GetPercentage()));		}		for (std::string s : rule->m_ImgPropertyList)		{			auto val = particle->GetImgPropertyValueByName(s.c_str());						xmls::ReplaceAll(exp, s, std::to_string(val));		}		bool rst = CalcuExp(exp);		if (rst)		{			MaxEDSTime = rule->m_MaxEDSTime;			return true;		}		else		{			continue;		}	}	MaxEDSTime = 0;	return false;}CString ParticleClassifyEngine::GetEleNameBySortingPercentage(CString sortingNostr, CPosXrayInfoPtr xrayInfo){	std::map<double, CElementChemistryPtr> mapPercent;	std::map<int, CElementChemistryPtr> mapSorted;	auto eles = xrayInfo->GetElementQuantifyData();	for (auto e : eles)	{		mapPercent[e->GetPercentage()] = e;	}	auto itr = --mapPercent.end();	for (int i=0;itr != mapPercent.begin();)	{				mapSorted.insert(std::pair<int, CElementChemistryPtr>(i, itr->second));			itr--;			i++;	}	std::string NoStr = sortingNostr;	std::vector<string> strs;	xmls::SplitString(NoStr, strs, "#");	int No = std::stoi(strs[1]);	if (mapSorted.size() > No)	{		return  mapSorted[No]->GetName();	}	else	{		return _T("");	}}double ParticleClassifyEngine::GetMacValue(CString MacStr){	auto mapconstant = m_std->GetMapConstants();		if (mapconstant.find(MacStr.GetBuffer()) != mapconstant.end())	{		return mapconstant[MacStr.GetBuffer()];	}	else	{		return 0.0;	}}int ParticleClassifyEngine::GetAtomicNoBySortingPercentage(CString sortingNostr, CPosXrayInfoPtr xrayInfo){	std::map<double, CElementChemistryPtr> mapPercent;	std::map<int, CElementChemistryPtr> mapSorted;	auto eles = xrayInfo->GetElementQuantifyData();	if (eles.size() == 0)	{		return 0;	}	for (auto e : eles)	{		mapPercent[e->GetPercentage()] = e;	}		auto itr = --mapPercent.end();	for (int i = 0; itr != mapPercent.begin(); )	{								mapSorted.insert(std::pair<int, CElementChemistryPtr>(i, itr->second));					itr--;		i++;	}	std::string NoStr = sortingNostr;	int No;	if (sortingNostr == "first_elem") No = 0;	if (sortingNostr == "second_elem") No = 1;	if (sortingNostr == "third_elem") No = 2;	if (sortingNostr == "fourth_elem") No = 3;	if (sortingNostr == "fifth_elem") No = 4;	if (sortingNostr == "sixth_elem") No = 5;	if (sortingNostr == "seventh_elem") No =6;	if (sortingNostr == "eighth_elem") No = 7;	if (sortingNostr == "ninth_elem") No = 8;	if (sortingNostr == "tenth_elem") No =9;	if (mapSorted.size() > No)	{		std::string elename = mapSorted[No]->GetName();		CElementPtr ele = CElementPtr(new CElement(elename.c_str()));		return ele->GetAtomNum();	}	else	{		return 0;	}	}CElementChemistriesList ParticleClassifyEngine::ZeroElementProcess(COTSParticlePtr particle, CPosXrayPtr xray){	auto& originalPartEles = xray->GetElementQuantifyData();//find all the elements containing in the particle xray.	CElementChemistriesList partEles;	for (auto che : originalPartEles)	{		auto newChe = CElementChemistryPtr(new CElementChemistry(che.get()));		partEles.push_back(newChe);	}	std::map<CString, CElementChemistryPtr> mapChe;	for (auto ch : partEles)	{		mapChe[ch->GetName()] = ch;	}	ZeroElementRuleList Rules = m_std->GetZeroRules();	for (auto rule : Rules)	{		std::string exp = rule->GetExpressionStr();		if (partEles.size() < rule->GetUsingElementList().size())		{			continue;// if the size not match then continue.		}		auto& usingEles = rule->GetUsingElementList();		bool bMatch = true;		for (auto che : usingEles)		{			auto chemical = mapChe.find(che->GetName());			if (chemical == mapChe.end())			{				bMatch = false;				break;//if cann't find the element in the particle's element,then continue.			}		}		if (bMatch == false) continue;		// all the rule's using element are contained in the particle then we replace the element with the percentage value		for (std::string s : rule->GetOtherpropertyList())		{			if (s.find("_elem")!=std::string::npos)			{				auto val = GetAtomicNoBySortingPercentage(s.c_str(), xray);								xmls::ReplaceAll(exp, s, std::to_string(val));			}			if (s.find("Element#")!=std::string::npos)			{				auto val = GetEleNameBySortingPercentage(s.c_str(), xray);//find the "Element#1" and  replace it with the real element name. 				auto elelist = rule->GetUsingElementList();				elelist.push_back(CElementPtr(new CElement(val)));// then replace it in the next step.				rule->SetUsingElementList(elelist);							xmls::ReplaceAll(exp, s, val.GetBuffer());			}								}		for (auto eleChemistry : rule->GetUsingElementList())		{			auto e = mapChe.find(eleChemistry->GetName());			if (e != mapChe.end())			{				std::string name = eleChemistry->GetName();						xmls::ReplaceAll(exp, name, std::to_string((*e).second->GetPercentage()));			}					}		for (std::string s : rule->GetImgPropertyList())		{			auto val = particle->GetImgPropertyValueByName(s.c_str());						xmls::ReplaceAll(exp, s, std::to_string(val));		}		for (int i = 0; i < 10; i++)		{			std::string macStr = "MAC#" + std::to_string(i);			if (exp.find(macStr) != std::string::npos)			{				auto val = GetMacValue(macStr.c_str());//find the "Mac#1" and  replace it with the real element name. 				xmls::ReplaceAll(exp, macStr, std::to_string(val));			}		}		bool rst = CalcuExp(exp);		if (rst)		{			auto itr = std::find_if(partEles.begin(), partEles.end(), [rule](CElementChemistryPtr ele) {return ele->GetName() == CString(rule->GetZeroElementName().c_str()); });				if (itr != partEles.end())				{					//partEles.erase(itr);//if satisfied the condition then erase the element(zero the element).					(*itr)->SetPercentage(0);//if satisfied the condition then set element percenttage to 0(zero the element).					double sumPercentage=0;					for (auto ele : partEles)					{						sumPercentage += ele->GetPercentage();					}					for (auto ele : partEles)					{						ele->SetPercentage(ele->GetPercentage() / sumPercentage*100);					}				}									}		else		{			continue;		}	}	return partEles;}OTSClassifyEngine::CLEEnginePtr GetParticleEngine(std::string libName){	static CLEEnginePtr engine;	if (engine == NULL)	{		engine = CLEEnginePtr(new ParticleClassifyEngine(libName));		engine->Init();	}	return engine;}}
 |