ParticleClassifyEngine.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "ParticleClassifyEngine.h"
  4. #include "ParticleEngine/LogicExp.h"
  5. #include "XMLSerialization.h"
  6. #include <map>
  7. #include <Element.h>
  8. #include "OTSSTDLibFileMgr.h"
  9. #include "COTSUtilityDllFunExport.h"
  10. namespace OTSClassifyEngine
  11. {
  12. using namespace expInterpreter;
  13. bool ParticleClassifyEngine::Init()
  14. {
  15. CSTDLibFileMgrPtr pLibFileMgr = CSTDLibFileMgrPtr(new CSTDLibFileMgr(m_StrName));
  16. m_std = ParticleSTDPtr(new ParticleSTD());
  17. if (!pLibFileMgr->LoadPartSTD(m_std))
  18. {
  19. return FALSE;
  20. }
  21. pLibFileMgr->LoadMaxEDSRulesData(m_std);
  22. pLibFileMgr->LoadZeroElementRulesData(m_std);
  23. string constantStr = pLibFileMgr->LoadConstantsData();
  24. std::map<std::string, double> m_mapConstants;
  25. m_mapConstants.clear();
  26. std::vector<std::string> strs;
  27. xmls::SplitString(constantStr, strs, ",");
  28. for (std::string s : strs)
  29. {
  30. std::vector<std::string> oneExp;
  31. xmls::SplitString(s, oneExp, "=");
  32. m_mapConstants[oneExp[0]] = std::atof(oneExp[1].c_str());
  33. }
  34. m_std->setConstantsMap(m_mapConstants);
  35. return true;
  36. }
  37. bool ParticleClassifyEngine::Classify(COTSParticlePtr particle, CPosXrayPtr xray)
  38. {
  39. if (particle != nullptr && xray != nullptr)
  40. {
  41. auto& originalPartEles = xray->GetElementQuantifyData();//find all the elements containing in the particle xray.
  42. //以下为调试用代码段(在log中打出颗粒元素),不要删除----------
  43. /*std::string allele=std::to_string(particle->GetTagId()) + " ";
  44. for (auto che : originalPartEles)
  45. {
  46. allele += che->GetName().GetBuffer() ;
  47. allele += ":";
  48. allele += std::to_string(che->GetPercentage()).c_str();
  49. allele += " ";
  50. }
  51. LogTrace(__FILE__, __LINE__, allele.c_str());*/
  52. //-----------------------------
  53. //zero element process,if satisfied the condition than erase the element from the list.
  54. auto partEles = ZeroElementProcess(particle, xray);//auto partEles = xray->GetElementQuantifyData();
  55. std::map<std::string, CElementChemistryPtr> mapChemistrys;
  56. for (auto ch : partEles)
  57. {
  58. mapChemistrys[ch->GetName().GetBuffer()] = ch;
  59. }
  60. PartSTDItemList stdItems = m_std->GetSTDItems();
  61. //以下为调试用代码段(在log中打出颗粒元素),不要删除----------
  62. /*CString stdnum = _T("STDNum ")+ CString(std::to_string(stdItems.size()).c_str());
  63. LogTrace(__FILE__, __LINE__, stdnum);*/
  64. for (auto itm : stdItems)
  65. {
  66. std::string exp = itm->GetExpressionStr();
  67. //LogTrace(__FILE__, __LINE__, exp.c_str());
  68. //if the element quantity is not match the std item's keyelement num than is unsatisfied.
  69. if (partEles.size() < itm->GetKeyElementList().size())
  70. {
  71. particle->SetClassifyId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);
  72. particle->TypeName("Not Identified");
  73. continue;
  74. }
  75. auto& mapStdEles = itm->GetMapElements();
  76. bool bMatch = true;
  77. // if the particle does not contain all the key elements of the item than is unsatisfied.
  78. for (auto che : mapStdEles)
  79. {
  80. auto chemical = mapChemistrys.find(che.second->GetName().GetBuffer());
  81. if (chemical == mapChemistrys.end())
  82. {
  83. particle->SetClassifyId((int)OTS_PARTCLE_TYPE::NOT_IDENTIFIED);
  84. particle->TypeName("Not Identified");
  85. bMatch = false;
  86. break;
  87. }
  88. }
  89. if (!bMatch) continue;
  90. // process the special property name such as "first_elem" and "Element#1" etc.
  91. for (std::string s : itm->GetUsingOtherpropertyList())
  92. {
  93. if (s.find("_elem") != std::string::npos)
  94. {
  95. auto val = GetAtomicNoBySortingPercentage(s.c_str(), xray);
  96. xmls::ReplaceAll(exp, s, std::to_string(val));
  97. }
  98. if (s.find("Element#") != std::string::npos)
  99. {
  100. auto val = GetEleNameBySortingPercentage(s.c_str(), xray);//find the "Element#1" and replace it with the real element name.
  101. auto elelist = itm->GetKeyElementList();
  102. elelist.push_back(CElementPtr(new CElement(val)));// then replace it in the next step.
  103. itm->SetKeyElementList(elelist);
  104. xmls::ReplaceAll(exp, s, val.GetBuffer());
  105. }
  106. }
  107. for (auto eleChemistry : itm->GetAllSortedEleList())
  108. {
  109. auto e = mapChemistrys.find(eleChemistry->GetName().GetBuffer());
  110. if (e != mapChemistrys.end())
  111. {
  112. std::string name = eleChemistry->GetName();
  113. xmls::ReplaceAll(exp, name, std::to_string(e->second->GetPercentage()));
  114. }
  115. else
  116. {
  117. std::string name = eleChemistry->GetName();
  118. xmls::ReplaceAll(exp, name, "0");
  119. }
  120. }
  121. //process the image property
  122. for (std::string s : itm->GetUsingImgPropertyNameList())
  123. {
  124. auto val = particle->GetImgPropertyValueByName(s.c_str());
  125. xmls::ReplaceAll(exp, s, std::to_string(val));
  126. }
  127. //process the "true" keyword.
  128. if (exp.find("true") != std::string::npos)
  129. {
  130. xmls::ReplaceAll(exp, "true", "(1=1)");
  131. }
  132. //process the "false" keyword.
  133. if (exp.find("false") != std::string::npos)
  134. {
  135. xmls::ReplaceAll(exp, "false", "(1=0)");
  136. }
  137. for (int i = 0; i < 10; i++)
  138. {
  139. std::string macStr = "MAC#" + std::to_string(i);
  140. if (exp.find(macStr) != std::string::npos)
  141. {
  142. auto val = GetMacValue(macStr.c_str());//find the "Mac#1" and replace it with the real element name.
  143. xmls::ReplaceAll(exp, macStr, std::to_string(val));
  144. }
  145. }
  146. //calculate the expression which has been processed.
  147. bool rst = CalcuExp(exp);
  148. //以下为调试用代码段(在log中打出颗粒元素),不要删除----------
  149. /*LogTrace(__FILE__, __LINE__, exp.c_str());
  150. if (rst)
  151. {
  152. LogTrace(__FILE__, __LINE__, CString("true"));
  153. }
  154. else
  155. {
  156. LogTrace(__FILE__, __LINE__, CString("false"));
  157. }*/
  158. if (rst)
  159. {
  160. //int id = itm->GetID();
  161. particle->SetType(OTS_PARTICLE_TYPE::IDENTIFIED);
  162. particle->SetClassifyId(itm->GetID());
  163. particle->TypeColor(itm->GetColor());
  164. particle->TypeName(itm->GetName());
  165. particle->SetHardness(itm->GetHardness());
  166. particle->SetDensity(itm->GetDensity());
  167. particle->SetConductivity(itm->GetElectrical_conductivity());
  168. particle->SetGroupId(itm->GetGrpID());
  169. particle->SetGroupColor(itm->GetGrpColor());
  170. particle->SetGroupName(itm->GetGrpName());
  171. return true;
  172. }
  173. else
  174. {
  175. continue;
  176. }
  177. }
  178. particle->SetType(OTS_PARTICLE_TYPE::NOT_IDENTIFIED);
  179. particle->TypeName("Not Identified");
  180. return true;
  181. }
  182. else if(particle != nullptr && xray == nullptr)
  183. {
  184. PartSTDItemList stdItems = m_std->GetSTDItems();
  185. for (auto itm : stdItems)
  186. {
  187. std::string exp = itm->GetExpressionStr();
  188. if (itm->GetKeyElementList().size() > 0 || itm->GetSubElementList().size() > 0)
  189. {
  190. continue;
  191. }
  192. //process the image property
  193. for (std::string s : itm->GetUsingImgPropertyNameList())
  194. {
  195. auto val = particle->GetImgPropertyValueByName(s.c_str());
  196. xmls::ReplaceAll(exp, s, std::to_string(val));
  197. }
  198. //process the "true" keyword.
  199. if (exp.find("true") != std::string::npos)
  200. {
  201. xmls::ReplaceAll(exp, "true", "(1=1)");
  202. }
  203. //process the "false" keyword.
  204. if (exp.find("false") != std::string::npos)
  205. {
  206. xmls::ReplaceAll(exp, "false", "(1=0)");
  207. }
  208. for (int i = 0; i < 10; i++)
  209. {
  210. std::string macStr = "MAC#" + std::to_string(i);
  211. if (exp.find(macStr) != std::string::npos)
  212. {
  213. auto val = GetMacValue(macStr.c_str());//find the "Mac#1" and replace it with the real element name.
  214. xmls::ReplaceAll(exp, macStr, std::to_string(val));
  215. }
  216. }
  217. //calculate the expression which has been processed.
  218. bool rst = CalcuExp(exp);
  219. if (rst)
  220. {
  221. //int id = itm->GetID();
  222. particle->SetType(OTS_PARTICLE_TYPE::IDENTIFIED);
  223. particle->SetClassifyId(itm->GetID());
  224. particle->TypeColor(itm->GetColor());
  225. particle->TypeName(itm->GetName());
  226. particle->SetHardness(itm->GetHardness());
  227. particle->SetDensity(itm->GetDensity());
  228. particle->SetConductivity(itm->GetElectrical_conductivity());
  229. particle->SetGroupId(itm->GetGrpID());
  230. particle->SetGroupColor(itm->GetGrpColor());
  231. particle->SetGroupName(itm->GetGrpName());
  232. return true;
  233. }
  234. else
  235. {
  236. continue;
  237. }
  238. }
  239. particle->SetType(OTS_PARTICLE_TYPE::NOT_IDENTIFIED);
  240. particle->TypeName("Not Identified");
  241. particle->TypeColor("#000000");
  242. particle->SetGroupId((int)OTS_PARTICLE_TYPE::NOT_IDENTIFIED);
  243. particle->SetGroupColor("#E6E6FA");
  244. particle->SetGroupName("Not Identified");
  245. return true;
  246. }
  247. }
  248. bool ParticleClassifyEngine::Classify(COTSParticlePtr particle, int SteelTech, CPosXrayPtr xray)
  249. {
  250. throw std::logic_error("The method or operation is not implemented.");
  251. }
  252. bool ParticleClassifyEngine::IfNeedMaxEDS(COTSParticlePtr particle, CPosXrayPtr xray, double& MaxEDSTime)
  253. {
  254. if (particle == nullptr || xray == nullptr) return false;
  255. MaxEDSRuleList Rules = m_std->GetMaxEDSRules();
  256. for (auto rule : Rules)
  257. {
  258. std::string exp = rule->m_expressionStr;
  259. auto& partEles = xray->GetElementQuantifyData();//find all the elements containing in the particle xray.
  260. if (partEles.size() < rule->m_elementList.size())
  261. {
  262. continue;// if the size not match then continue.
  263. }
  264. std::map<CString, CElementChemistryPtr> mapChe;
  265. for (auto ch : partEles)
  266. {
  267. mapChe[ch->GetName()] = ch;
  268. }
  269. auto& usingEles = rule->m_elementList;
  270. bool bMatch=true;
  271. for (auto che : usingEles)
  272. {
  273. auto chemical = mapChe.find(che->GetName());
  274. if (chemical == mapChe.end())
  275. {
  276. bMatch = false;
  277. break;
  278. }
  279. }
  280. if (!bMatch) continue;//if cann't find the element in the particle's element,then continue.
  281. // all the rule's using element are contained in the particle then we replace the element with the percentage value
  282. for (std::string s : rule->m_OtherpropertyList)
  283. {
  284. if (s.find("_elem")!=std::string::npos)
  285. {
  286. auto val = GetAtomicNoBySortingPercentage(s.c_str(), xray);
  287. xmls::ReplaceAll(exp, s, std::to_string(val));
  288. }
  289. if (s.find("Element#")!=std::string::npos)
  290. {
  291. auto val = GetEleNameBySortingPercentage(s.c_str(), xray);//find the "Element#1" and replace it with the real element name.
  292. auto& elelist = rule->m_elementList;
  293. elelist.push_back(CElementPtr(new CElement(val)));// then replace it in the next step.
  294. xmls::ReplaceAll(exp, s, val.GetBuffer());
  295. }
  296. }
  297. for (auto eleChemistry : rule->m_elementList)
  298. {
  299. auto e = mapChe[eleChemistry->GetName()];
  300. std::string name = eleChemistry->GetName();
  301. xmls::ReplaceAll(exp, name, std::to_string(e->GetPercentage()));
  302. }
  303. for (std::string s : rule->m_ImgPropertyList)
  304. {
  305. auto val = particle->GetImgPropertyValueByName(s.c_str());
  306. xmls::ReplaceAll(exp, s, std::to_string(val));
  307. }
  308. bool rst = CalcuExp(exp);
  309. if (rst)
  310. {
  311. MaxEDSTime = rule->m_MaxEDSTime;
  312. return true;
  313. }
  314. else
  315. {
  316. continue;
  317. }
  318. }
  319. MaxEDSTime = 0;
  320. return false;
  321. }
  322. CString ParticleClassifyEngine::GetEleNameBySortingPercentage(CString sortingNostr, CPosXrayInfoPtr xrayInfo)
  323. {
  324. std::map<double, CElementChemistryPtr> mapPercent;
  325. std::map<int, CElementChemistryPtr> mapSorted;
  326. auto eles = xrayInfo->GetElementQuantifyData();
  327. for (auto e : eles)
  328. {
  329. mapPercent[e->GetPercentage()] = e;
  330. }
  331. auto itr = --mapPercent.end();
  332. for (int i=0;itr != mapPercent.begin();)
  333. {
  334. mapSorted.insert(std::pair<int, CElementChemistryPtr>(i, itr->second));
  335. itr--;
  336. i++;
  337. }
  338. std::string NoStr = sortingNostr;
  339. std::vector<string> strs;
  340. xmls::SplitString(NoStr, strs, "#");
  341. int No = std::stoi(strs[1]);
  342. if (mapSorted.size() > No)
  343. {
  344. return mapSorted[No]->GetName();
  345. }
  346. else
  347. {
  348. return _T("");
  349. }
  350. }
  351. double ParticleClassifyEngine::GetMacValue(CString MacStr)
  352. {
  353. auto mapconstant = m_std->GetMapConstants();
  354. if (mapconstant.find(MacStr.GetBuffer()) != mapconstant.end())
  355. {
  356. return mapconstant[MacStr.GetBuffer()];
  357. }
  358. else
  359. {
  360. return 0.0;
  361. }
  362. }
  363. int ParticleClassifyEngine::GetAtomicNoBySortingPercentage(CString sortingNostr, CPosXrayInfoPtr xrayInfo)
  364. {
  365. std::map<double, CElementChemistryPtr> mapPercent;
  366. std::map<int, CElementChemistryPtr> mapSorted;
  367. auto eles = xrayInfo->GetElementQuantifyData();
  368. if (eles.size() == 0)
  369. {
  370. return 0;
  371. }
  372. for (auto e : eles)
  373. {
  374. mapPercent[e->GetPercentage()] = e;
  375. }
  376. auto itr = --mapPercent.end();
  377. for (int i = 0; itr != mapPercent.begin(); )
  378. {
  379. mapSorted.insert(std::pair<int, CElementChemistryPtr>(i, itr->second));
  380. itr--;
  381. i++;
  382. }
  383. std::string NoStr = sortingNostr;
  384. int No;
  385. if (sortingNostr == "first_elem") No = 0;
  386. if (sortingNostr == "second_elem") No = 1;
  387. if (sortingNostr == "third_elem") No = 2;
  388. if (sortingNostr == "fourth_elem") No = 3;
  389. if (sortingNostr == "fifth_elem") No = 4;
  390. if (sortingNostr == "sixth_elem") No = 5;
  391. if (sortingNostr == "seventh_elem") No =6;
  392. if (sortingNostr == "eighth_elem") No = 7;
  393. if (sortingNostr == "ninth_elem") No = 8;
  394. if (sortingNostr == "tenth_elem") No =9;
  395. if (mapSorted.size() > No)
  396. {
  397. std::string elename = mapSorted[No]->GetName();
  398. CElementPtr ele = CElementPtr(new CElement(elename.c_str()));
  399. return ele->GetAtomNum();
  400. }
  401. else
  402. {
  403. return 0;
  404. }
  405. }
  406. CElementChemistriesList ParticleClassifyEngine::ZeroElementProcess(COTSParticlePtr particle, CPosXrayPtr xray)
  407. {
  408. auto& originalPartEles = xray->GetElementQuantifyData();//find all the elements containing in the particle xray.
  409. CElementChemistriesList partEles;
  410. for (auto che : originalPartEles)
  411. {
  412. auto newChe = CElementChemistryPtr(new CElementChemistry(che.get()));
  413. partEles.push_back(newChe);
  414. }
  415. std::map<CString, CElementChemistryPtr> mapChe;
  416. for (auto ch : partEles)
  417. {
  418. mapChe[ch->GetName()] = ch;
  419. }
  420. ZeroElementRuleList Rules = m_std->GetZeroRules();
  421. for (auto rule : Rules)
  422. {
  423. std::string exp = rule->GetExpressionStr();
  424. if (partEles.size() < rule->GetUsingElementList().size())
  425. {
  426. continue;// if the size not match then continue.
  427. }
  428. auto& usingEles = rule->GetUsingElementList();
  429. bool bMatch = true;
  430. for (auto che : usingEles)
  431. {
  432. auto chemical = mapChe.find(che->GetName());
  433. if (chemical == mapChe.end())
  434. {
  435. bMatch = false;
  436. break;//if cann't find the element in the particle's element,then continue.
  437. }
  438. }
  439. if (bMatch == false) continue;
  440. // all the rule's using element are contained in the particle then we replace the element with the percentage value
  441. for (std::string s : rule->GetOtherpropertyList())
  442. {
  443. if (s.find("_elem")!=std::string::npos)
  444. {
  445. auto val = GetAtomicNoBySortingPercentage(s.c_str(), xray);
  446. xmls::ReplaceAll(exp, s, std::to_string(val));
  447. }
  448. if (s.find("Element#")!=std::string::npos)
  449. {
  450. auto val = GetEleNameBySortingPercentage(s.c_str(), xray);//find the "Element#1" and replace it with the real element name.
  451. auto elelist = rule->GetUsingElementList();
  452. elelist.push_back(CElementPtr(new CElement(val)));// then replace it in the next step.
  453. rule->SetUsingElementList(elelist);
  454. xmls::ReplaceAll(exp, s, val.GetBuffer());
  455. }
  456. }
  457. for (auto eleChemistry : rule->GetUsingElementList())
  458. {
  459. auto e = mapChe.find(eleChemistry->GetName());
  460. if (e != mapChe.end())
  461. {
  462. std::string name = eleChemistry->GetName();
  463. xmls::ReplaceAll(exp, name, std::to_string((*e).second->GetPercentage()));
  464. }
  465. }
  466. for (std::string s : rule->GetImgPropertyList())
  467. {
  468. auto val = particle->GetImgPropertyValueByName(s.c_str());
  469. xmls::ReplaceAll(exp, s, std::to_string(val));
  470. }
  471. for (int i = 0; i < 10; i++)
  472. {
  473. std::string macStr = "MAC#" + std::to_string(i);
  474. if (exp.find(macStr) != std::string::npos)
  475. {
  476. auto val = GetMacValue(macStr.c_str());//find the "Mac#1" and replace it with the real element name.
  477. xmls::ReplaceAll(exp, macStr, std::to_string(val));
  478. }
  479. }
  480. bool rst = CalcuExp(exp);
  481. if (rst)
  482. {
  483. auto itr = std::find_if(partEles.begin(), partEles.end(), [rule](CElementChemistryPtr ele) {return ele->GetName() == CString(rule->GetZeroElementName().c_str()); });
  484. if (itr != partEles.end())
  485. {
  486. //partEles.erase(itr);//if satisfied the condition then erase the element(zero the element).
  487. (*itr)->SetPercentage(0);//if satisfied the condition then set element percenttage to 0(zero the element).
  488. double sumPercentage=0;
  489. for (auto ele : partEles)
  490. {
  491. sumPercentage += ele->GetPercentage();
  492. }
  493. for (auto ele : partEles)
  494. {
  495. ele->SetPercentage(ele->GetPercentage() / sumPercentage*100);
  496. }
  497. }
  498. }
  499. else
  500. {
  501. continue;
  502. }
  503. }
  504. return partEles;
  505. }
  506. OTSClassifyEngine::CLEEnginePtr GetParticleEngine(std::string libName)
  507. {
  508. static CLEEnginePtr engine;
  509. if (engine == NULL)
  510. {
  511. engine = CLEEnginePtr(new ParticleClassifyEngine(libName));
  512. engine->Init();
  513. }
  514. return engine;
  515. }
  516. }