ParticleClassifyEngine.cpp 15 KB

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