PackageTest.cs 18 KB

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