ModuleTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Xml;
  7. using IwbZero.IwbBase;
  8. using IwbZero.ToolCommon.StringModel;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. using WeEngine.Enum;
  11. using WeEngine.Packages;
  12. namespace WeEngine.Tests.Module
  13. {
  14. [TestClass]
  15. public class ModuleTest
  16. {
  17. [TestMethod]
  18. public void Test()
  19. {
  20. for (int i = 0; i < 20; i++)
  21. {
  22. var code = GenerateCode();
  23. Debug.WriteLine($"========================> {code} <========================");
  24. Thread.Sleep(new Random().Next(6000,10000));
  25. }
  26. }
  27. private string GenerateCode()
  28. {
  29. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
  30. string str = Convert.ToInt64(ts.TotalSeconds).ToString();
  31. // Debug.WriteLine($"========================> {str} ");
  32. var arr = str.ToCharArray();
  33. char temp1 = arr[0], temp2 = arr[1], temp3 = arr[2], temp4 = arr[4];
  34. arr[0] = arr[arr.Length - 2];
  35. arr[1] = arr[arr.Length - 4];
  36. arr[2] = arr[arr.Length - 5];
  37. arr[4] = arr[arr.Length - 3];
  38. arr[arr.Length - 3] = temp4;
  39. arr[arr.Length - 2] = temp3;
  40. arr[arr.Length - 4] = temp2;
  41. arr[arr.Length - 5] = temp1;
  42. //var code = $"{str.Substring(5)}{str.Substring(0, 5)}";
  43. var code = new string(arr);
  44. return code;
  45. }
  46. [TestMethod]
  47. public void Test1()
  48. {
  49. //ScoreEvalModuleContextBase scoreEvalModule = new ScoreEvalModuleContextBase("EvolutionScoreEvalModule");
  50. //ScoreEvalModuleContextBase scoreEvalModule2 = new ScoreEvalModuleContextBase("QuestionScoreEvalModule");
  51. //scoreEvalModule.MatchKeyWord("11",);
  52. //scoreEvalModule2.MatchKeyWord("22");
  53. }
  54. private static string PackageNo = "P1";
  55. private readonly string _baseFilePath = $"C:/WisdomExercise/WeEngine/Data/Packages/{PackageNo}/";
  56. [TestMethod]
  57. public void Test2()
  58. {
  59. var package = new PackageNode
  60. {
  61. InternalNo = PackageNo,
  62. Name = $"方案包{PackageNo}",
  63. Weights = 100,
  64. Children = new List<NodeBase>(),
  65. };
  66. for (int i = 1; i < 4; i++)
  67. {
  68. package.Children.Add(GetRound(i , package));
  69. }
  70. SavePackage(package);
  71. }
  72. [TestMethod]
  73. public void Test3()
  74. {
  75. var xmlDoc = new XmlDocument();
  76. xmlDoc.Load(GetFilePath());
  77. var xmlNode = IwbXmlNode.ParseGenericXml(xmlDoc.LastChild.InnerXml);
  78. var package = new PackageNode().BuildNodeByXmlNode(xmlNode);
  79. var str = package.ToXmlString();
  80. var xmlDoc2 = new XmlDocument { InnerXml = str };
  81. xmlDoc2.Save($"{_baseFilePath}package.iwbx");
  82. //xmlDoc2.Save(GetFilePath("package"));
  83. //xmlDoc2.Save(GetFilePath("package2"));
  84. }
  85. [TestMethod]
  86. public void Test4()
  87. {
  88. var xmlDoc = new XmlDocument();
  89. xmlDoc.Load(GetFilePath());
  90. var xmlNode = IwbXmlNode.ParseGenericXml(xmlDoc.LastChild.InnerXml);
  91. var package = new PackageNode().BuildNodeByXmlNode(xmlNode);
  92. var package2 = (PackageNode)package.Clone();
  93. package2.RunningId = "G101";
  94. //PackageCache.Instance.SetCache(package2);
  95. //package2.RunnerManager.Instance.Run(package2, OperationType.Start);
  96. //string word = "lmtp73dj";
  97. //package2.RunnerModule.Run($"{package2.Id}|{word}|民政局", OperationType.MatchKey);
  98. //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
  99. //word = "pqja5";
  100. //var round = package2.CurRoundInfo.ActualScore;
  101. //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
  102. //word = "6";
  103. //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
  104. //word = "8";
  105. //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
  106. var xmlDoc2 = new XmlDocument { InnerXml = package2.ToXmlString() };
  107. xmlDoc2.Save(GetFilePath("package3"));
  108. }
  109. public static string GetFilePath(string fileName = "package")
  110. {
  111. var path = AppDomain.CurrentDomain.BaseDirectory;
  112. path = path.Substring(0, path.IndexOf("WeEngine.Tests", StringComparison.Ordinal));
  113. string filePath = $"{path}WeEngine.Tests/Module/{fileName}.xml";
  114. return filePath;
  115. }
  116. private Random _random = new Random(unchecked((int)DateTime.Now.Ticks));
  117. private void SavePackage(PackageNode package)
  118. {
  119. var guides = new List<GuideNode>();
  120. var flows= new List<SceneFlowNode>();
  121. var scenes = new List<SceneNode>();
  122. var behaviors = new List<BehaviorNode>();
  123. foreach (var round in package.Children)
  124. {
  125. AddGuide(round, ref guides);
  126. foreach (var block in round.Children)
  127. {
  128. AddGuide(block, ref guides);
  129. flows.AddRange(block.Children.Select(a => (SceneFlowNode)a));
  130. }
  131. }
  132. flows = flows.IwbDistinct(a => a.Id).ToList();
  133. if (flows.Any())
  134. {
  135. var str = "";
  136. foreach (var flow in flows)
  137. {
  138. AddGuide(flow, ref guides);
  139. if (flow.HasChild)
  140. {
  141. foreach (var nodeBase in flow.Children)
  142. {
  143. var child = (FlowNode) nodeBase;
  144. AddScene(child, ref scenes, ref guides);
  145. }
  146. }
  147. str += flow.ToXmlString();
  148. }
  149. if (str.IsNotEmpty())
  150. {
  151. str = $"<SceneFlows>{str}</SceneFlows>";
  152. var xDoc = new XmlDocument {InnerXml = str};
  153. xDoc.Save($"{_baseFilePath}flow.iwbx");
  154. }
  155. }
  156. scenes = scenes.IwbDistinct(a => a.Id).ToList();
  157. if (scenes.Any())
  158. {
  159. var str = "";
  160. foreach (var scene in scenes)
  161. {
  162. AddGuide(scene, ref guides);
  163. if (scene.HasChild)
  164. {
  165. foreach (var nodeBase in scene.Children)
  166. {
  167. var child = (BehaviorNode)nodeBase;
  168. AddGuide(child, ref guides);
  169. behaviors.Add(child);
  170. }
  171. }
  172. str += scene.ToXmlString();
  173. }
  174. if (str.IsNotEmpty())
  175. {
  176. str = $"<SceneInfos>{str}</SceneInfos>";
  177. var xDoc = new XmlDocument { InnerXml = str };
  178. xDoc.Save($"{_baseFilePath}scene.iwbx");
  179. }
  180. }
  181. behaviors = behaviors.IwbDistinct(a => a.Id).ToList();
  182. if (behaviors.Any())
  183. {
  184. var str = "";
  185. foreach (var behavior in behaviors)
  186. {
  187. str += behavior.ToXmlString();
  188. }
  189. if (str.IsNotEmpty())
  190. {
  191. str = $"<Behaviors>{str}</Behaviors>";
  192. var xDoc = new XmlDocument { InnerXml = str };
  193. xDoc.Save($"{_baseFilePath}behavior.iwbx");
  194. }
  195. }
  196. guides = guides.IwbDistinct(a => a.Id).ToList();
  197. if (guides.Any())
  198. {
  199. var str = "";
  200. foreach (var guide in guides)
  201. {
  202. str += guide.ToXmlString();
  203. }
  204. if (str.IsNotEmpty())
  205. {
  206. str = $"<GuideInfos>{str}</GuideInfos>";
  207. var xDoc = new XmlDocument { InnerXml = str };
  208. xDoc.Save($"{_baseFilePath}guide.iwbx");
  209. }
  210. }
  211. var result = package.ToXmlString();
  212. var xmlDoc2 = new XmlDocument { InnerXml = result };
  213. xmlDoc2.Save($"{_baseFilePath}package.iwbx");
  214. }
  215. private void AddGuide(NodeBase node, ref List<GuideNode> guides)
  216. {
  217. if (node.GuideInfos != null && node.GuideInfos.Any())
  218. {
  219. guides.AddRange(node.GuideInfos);
  220. }
  221. }
  222. private void AddScene(FlowNode node, ref List<SceneNode> scenes, ref List<GuideNode> guides)
  223. {
  224. AddGuide(node, ref guides);
  225. if (node.SceneInfos != null && node.SceneInfos.Any())
  226. {
  227. scenes.AddRange(node.SceneInfos);
  228. }
  229. if (node.HasChild)
  230. {
  231. foreach (var nodeBase in node.Children)
  232. {
  233. var child = (FlowNode) nodeBase;
  234. AddScene(child,ref scenes,ref guides);
  235. }
  236. }
  237. }
  238. #region www
  239. private SceneRoundNode GetRound(int id, PackageNode parent = null)
  240. {
  241. _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
  242. var node = new SceneRoundNode()
  243. {
  244. InternalNo = $"R{id}",
  245. Parent = parent,
  246. Id = $"R{id}",
  247. Name = $"轮次{id}",
  248. SceneCount = 2,
  249. ControlRate = 80,
  250. RoundFullScore = 500,
  251. RoundIndex = id,
  252. Children = new List<SceneFlowBlockNode>(),
  253. };
  254. node.Children.Add(GetBlock(1, node));
  255. for (int i = 2; i <= _random.Next(3, 4); i++)
  256. {
  257. node.Children.Add(GetBlock(i, node, 1));
  258. }
  259. return node;
  260. }
  261. private SceneFlowBlockNode GetBlock(int id, SceneRoundNode parent = null, int t = 0)
  262. {
  263. var node = new SceneFlowBlockNode()
  264. {
  265. Id = $"SFB{id}",
  266. Parent = parent,
  267. InternalNo = $"SFB{id}",
  268. Name = $"情景流块{id}",
  269. BlockType = t == 0 ? SceneFlowBlockType.Objective : SceneFlowBlockType.Evolutionary,
  270. Weights = _random.Next(0, 1) == 0 ? 50 : 100,
  271. Children = new List<SceneFlowNode>(),
  272. };
  273. for (int i = 1; i <= (t == 0 ? 2 : _random.Next(2, 3)); i++)
  274. {
  275. Thread.Sleep(50);
  276. node.Children.Add(GetFlow(i, node));
  277. }
  278. return node;
  279. }
  280. private SceneFlowNode GetFlow(int id, SceneFlowBlockNode parent = null)
  281. {
  282. _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
  283. var no = $"{DateTime.Now:mmssffff}";
  284. var node = new SceneFlowNode()
  285. {
  286. Id = $"SF{no}",
  287. Parent = parent,
  288. InternalNo = $"SF{id}",
  289. Name = $"情景流_{_random.Next(2, 5).GetRandomChinese()}",
  290. Weights = _random.Next(0, 1) == 0 ? 50 : 100,
  291. Children = new List<FlowNode>(),
  292. };
  293. var count = _random.Next(2, 3);
  294. node.Children.Add(GetFlowNode(1, ref count, node));
  295. //for (int i = 1; i <= _random.Next(1, 3); i++)
  296. //{
  297. // var depth = _random.Next(2, 3);
  298. // node.Children.Add(GetFlowNode($"{no}_{i}", ref depth, node));
  299. //}
  300. return node;
  301. }
  302. private FlowNode GetFlowNode(int id, ref int depth, NodeBase parent = null,int childCount=1,int index=0)
  303. {
  304. _random = new Random(depth * unchecked((int) DateTime.Now.Ticks) + id);
  305. var no = $"{DateTime.Now:mmssffff}";
  306. var flowDepth = parent != null && parent.NodeType != NodeType.SceneFlow
  307. ? (((FlowNode) parent).FlowDepth + 1)
  308. : 1;
  309. int min = 0, max = 0;
  310. if (flowDepth>1)
  311. {
  312. var score = 100 / childCount;
  313. min = score * (index - 1); max = childCount == index ? 100 : score * index;
  314. }
  315. var node = new FlowNode()
  316. {
  317. Id = $"FN{no}",
  318. Parent = parent,
  319. InternalNo = $"FN{id}",
  320. Name = $"节点{no}",
  321. PreComponent = parent?.NodeType == NodeType.SceneFlow
  322. ? ""
  323. : $"@FlowScore >{min} && @FlowScore<={max}",
  324. FlowDepth = flowDepth,
  325. Children = new List<NodeBase>(),
  326. SceneInfos = new List<SceneNode>()
  327. };
  328. if (depth == 3&&node.InternalNo == "FN1")
  329. {
  330. node.PostComponent = "RunFunSetVariableWithRp('FnScore','@NodeScore','N44')";
  331. }
  332. if (depth == 3&&node.InternalNo == "FN2")
  333. {
  334. node.PreComponent = "RunFunWaitNodeWithRp('N5','@FnScore > 20')";
  335. }
  336. for (int i = 1; i <= _random.Next(2, 3); i++)
  337. {
  338. Thread.Sleep(20);
  339. node.SceneInfos.Add(GetSceneInfo(i, node));
  340. }
  341. if (depth > 0)
  342. {
  343. depth--;
  344. var count = _random.Next(2, 3);
  345. for (int i = 1; i <= count; i++)
  346. {
  347. Thread.Sleep(25);
  348. node.Children.Add(GetFlowNode(i, ref depth, node, count,i));
  349. }
  350. }
  351. return node;
  352. }
  353. private SceneNode GetSceneInfo(int id, NodeBase parent = null)
  354. {
  355. _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
  356. var no = $"{DateTime.Now:mmssffff}";
  357. var scene = new SceneNode()
  358. {
  359. Id = $"SN{no}",
  360. Parent = parent,
  361. InternalNo = $"SN{id}",
  362. Name = $"情景_{_random.Next(2, 5).GetRandomChinese()}",
  363. Description = _random.Next(50, 150).GetRandomChinese(),
  364. Weights = _random.Next(0, 1) == 0 ? 50 : 100,
  365. Children = new List<NodeBase>()
  366. };
  367. for (int i = 1; i <= _random.Next(2, 4); i++)
  368. {
  369. Thread.Sleep(15);
  370. scene.Children.Add(GetBehavior(i, scene));
  371. }
  372. return scene;
  373. }
  374. private BehaviorNode GetBehavior(int id, NodeBase parent = null)
  375. {
  376. _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
  377. var no = $"{DateTime.Now:mmssffff}";
  378. string[] allRoles = "消防局,公安局,民政局,交通局,应急指挥中心,防台防汛办,水利局,电力公司".StrToArray();
  379. string[] allTag1 = "对上,对下,对相关,对媒体".StrToArray();
  380. string[] allTag2 = "掌控力,研判力,决策力,协调力,舆论引导力".StrToArray();
  381. var str = "";
  382. {
  383. var role = allRoles[_random.Next(0, allRoles.Length - 1)];
  384. str += role;
  385. allRoles.RemoveFromArray(role);
  386. }
  387. if (_random.Next(0, 1)>0)
  388. {
  389. var role1 = allRoles[_random.Next(0, allRoles.Length - 1)];
  390. str += $",{role1}";
  391. allRoles.RemoveFromArray(role1);
  392. if (_random.Next(0, 2) > 1)
  393. {
  394. var role2 = allRoles[_random.Next(0, allRoles.Length - 1)];
  395. str += $",{role2}";
  396. allRoles.RemoveFromArray(role2);
  397. if (_random.Next(0, 3) > 2)
  398. {
  399. var role3 = allRoles[_random.Next(0, allRoles.Length - 1)];
  400. str += $",{role3}";
  401. allRoles.RemoveFromArray(role3);
  402. }
  403. }
  404. }
  405. _random = new Random(unchecked((int)DateTime.Now.Ticks) * allTag1.Length +id);
  406. var tag = $"{allTag1[_random.Next(0, allTag1.Length - 1)]},{allTag2[_random.Next(0, allTag2.Length - 1)]}";
  407. _random = new Random(unchecked((int) DateTime.Now.Ticks) * allTag2.Length + id);
  408. var node = new BehaviorNode(GetKeyWords())
  409. {
  410. Id = $"B{no}",
  411. Parent = parent,
  412. InternalNo = $"B{id}",
  413. Name = $"行为_{_random.Next(2, 5).GetRandomChinese()}",
  414. Description = _random.Next(50, 150).GetRandomChinese(),
  415. Weights = _random.Next(0, 1) == 0 ? 50 : 100,
  416. BehaviorTag = tag,
  417. BehaviorRole = str.StrToArray(),
  418. BehaviorRoleLogic = BehaviorRoleLogicType.Or,
  419. BehaviorScoreType = BehaviorScoreType.Normal,
  420. };
  421. var ran = _random.Next(0, 10);
  422. if (ran > 9)
  423. {
  424. node.BehaviorScoreType = BehaviorScoreType.ImportantNegative;
  425. }
  426. else if (ran > 8)
  427. {
  428. node.BehaviorScoreType = BehaviorScoreType.Negative;
  429. }
  430. return node;
  431. }
  432. private string GetKeyWords()
  433. {
  434. _random = new Random(unchecked((int) DateTime.Now.Ticks));
  435. var ran = _random.Next(0, 10);
  436. string str;
  437. if (ran >= 8)
  438. {
  439. str = $"[{RandomKeyWord(_random.Next(4, 9))}:30],[{RandomKeyWord(_random.Next(4, 9))}:70]";
  440. }
  441. else if (ran >= 6)
  442. {
  443. str = $"[{RandomKeyWord(_random.Next(4, 9))}:40],[{RandomKeyWord(_random.Next(4, 9))}:60]";
  444. }
  445. else if (ran >= 4)
  446. {
  447. str = $"[{RandomKeyWord(_random.Next(4, 9))}:60],[[{RandomKeyWord(_random.Next(4, 9))},{RandomKeyWord(_random.Next(4, 9))}]:40]";
  448. }
  449. else if (ran >= 2)
  450. {
  451. str = $"[{RandomKeyWord(_random.Next(4, 9))}:70],[[{RandomKeyWord(_random.Next(4, 9))},{RandomKeyWord(_random.Next(4, 9))},{RandomKeyWord(_random.Next(4, 9))}]:30]";
  452. }
  453. else
  454. {
  455. str = $"[{RandomKeyWord(_random.Next(4, 9))}:100]";
  456. }
  457. return $"[{str}]";
  458. }
  459. private string RandomKeyWord(int len)
  460. {
  461. _random = new Random(unchecked((int) DateTime.Now.Ticks)+len);
  462. char[] arr = "qwertyuiopasdfghjklzxcvbnm1234567890".ToCharArray();
  463. var str = "";
  464. while (str.Length < len)
  465. {
  466. str += arr[_random.Next(0, arr.Length)];
  467. }
  468. return str;
  469. }
  470. #endregion
  471. }
  472. }