ParticleClassifyEngine.cpp 14 KB

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