ParticleClassifyEngine.cpp 17 KB

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