ParticleClassifyEngine.cpp 16 KB

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