EvolutionRunnerModule.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using IwbZero.ToolCommon.StringModel;
  6. using WeEngine.Enum;
  7. using WeEngine.Functions;
  8. using WeEngine.Packages;
  9. namespace WeEngine.Module.Runner
  10. {
  11. public sealed class EvolutionRunnerModule: PackageRunnerModuleBase
  12. {
  13. public EvolutionRunnerModule(string runningId)
  14. {
  15. try
  16. {
  17. PackageInfo = PackageCache.Instance.GetCache(runningId);
  18. }
  19. catch (Exception e)
  20. {
  21. Message = e.Message;
  22. }
  23. }
  24. public EvolutionRunnerModule(XmlNode xmlNode)
  25. {
  26. PackageInfo = (PackageInfo) new PackageInfo().CreateNodeByXmlNode(xmlNode);
  27. //PackageInfo.RunningId = PackageInfo.RunningId.IsEmpty() ? GenerateRunningId() : PackageInfo.RunningId;
  28. //PackageCache.Instance.SetCache(PackageInfo);
  29. }
  30. public EvolutionRunnerModule(PackageInfo packageInfo)
  31. {
  32. PackageInfo = packageInfo;
  33. //PackageInfo.RunningId = PackageInfo.RunningId.IsEmpty() ? GenerateRunningId() : PackageInfo.RunningId;
  34. //PackageCache.Instance.SetCache(PackageInfo);
  35. }
  36. ///// <summary>
  37. ///// 产生随机运行Id
  38. ///// </summary>
  39. ///// <returns></returns>
  40. //private string GenerateRunningId()
  41. //{
  42. // return $"{DateTime.Now:yyyyMMddHHmmss}{new Random(1000).Next(1000, 9999)}";
  43. //}
  44. public string Message { get; set; }
  45. public override PackageInfo PackageInfo { get; set; }
  46. public NodeBase CurrentNode { get; set; }
  47. private NodeBase GetNodeByPath(string path)
  48. {
  49. var node = PackageInfo.GetNodeByPath(path);
  50. return node;
  51. }
  52. /// <summary>
  53. /// 运行方案包
  54. /// </summary>
  55. /// <param name="node"></param>
  56. /// <param name="operationType"></param>
  57. /// <returns></returns>
  58. public override bool Run(NodeBase node, OperationType operationType)
  59. {
  60. CurrentNode = node;
  61. return Run(operationType);
  62. }
  63. /// <summary>
  64. /// 运行
  65. /// </summary>
  66. /// <param name="path">匹配关键字时('path|word|role')</param>
  67. /// <param name="operationType"></param>
  68. /// <returns></returns>
  69. public override bool Run(string path, OperationType operationType)
  70. {
  71. if (operationType == OperationType.MatchKey)
  72. {
  73. string[] arr = path.Split('|');
  74. if (arr.Length == 3)
  75. {
  76. CurrentNode = GetNodeByPath(arr[0]);
  77. CurrentNode.ExtendData = new { BehaviorWord = arr[1], BehaviorRole = arr[2] };
  78. //CurrentNode.ExtendData.BehaviorWord = arr[1];
  79. //CurrentNode.ExtendData.BehaviorRole = arr[2];
  80. return Run(OperationType.MatchKey);
  81. }
  82. return false;
  83. }
  84. CurrentNode = GetNodeByPath(path);
  85. return Run(operationType);
  86. }
  87. /// <summary>
  88. /// 运行
  89. /// </summary>
  90. /// <param name="operationType"></param>
  91. /// <returns></returns>
  92. private bool Run(OperationType operationType)
  93. {
  94. if (CurrentNode != null)
  95. {
  96. return RunNode(CurrentNode, operationType);
  97. }
  98. return false;
  99. }
  100. /// <summary>
  101. /// 运行节点
  102. /// </summary>
  103. /// <param name="curPackageInfo"></param>
  104. /// <param name="id"></param>
  105. /// <param name="operationType"></param>
  106. /// <returns></returns>
  107. public static bool RunNode(PackageInfo curPackageInfo, string id, OperationType operationType)
  108. {
  109. if (operationType == OperationType.MatchKey)
  110. {
  111. string[] arr = id.Split('|');
  112. if (arr.Length == 3)
  113. {
  114. var curNode = curPackageInfo.GetNodeByPath(arr[0]);
  115. curNode.ExtendData = new { BehaviorWord= arr[1], BehaviorRole= arr[2] };
  116. //curNode.ExtendData.BehaviorWord = arr[1];
  117. //curNode.ExtendData.BehaviorRole = arr[2];
  118. return RunNode(curNode, OperationType.MatchKey);
  119. }
  120. return false;
  121. }
  122. {
  123. var curNode = curPackageInfo.GetNodeByPath(id);
  124. return RunNode(curNode, operationType);
  125. }
  126. }
  127. /// <summary>
  128. /// 运行节点
  129. /// </summary>
  130. /// <param name="node"></param>
  131. /// <param name="operationType"></param>
  132. /// <returns></returns>
  133. private static bool RunNode(NodeBase node, OperationType operationType)
  134. {
  135. switch (operationType)
  136. {
  137. case OperationType.Start:
  138. return Start(node);
  139. case OperationType.Stop:
  140. return Stop(node);
  141. case OperationType.NextRound:
  142. return NextRound(node);
  143. case OperationType.NextFlowNodes:
  144. return NextFlowNodes(node);
  145. case OperationType.NextFlowNode:
  146. return NextFlowNode(node);
  147. case OperationType.PreEvent:
  148. return RunnerPre(node);
  149. case OperationType.RunningEvent:
  150. return RunnerRunning(node);
  151. case OperationType.RunnedEvent:
  152. return RunnerRunned(node);
  153. case OperationType.PostEvent:
  154. return RunnerPost(node);
  155. case OperationType.MatchKey:
  156. return RunnerMatchKey(node);
  157. case OperationType.MatchSuccess:
  158. return RunnerMatchSuccess(node);
  159. }
  160. return false;
  161. }
  162. private static bool Start(NodeBase node)
  163. {
  164. if (node.NodeState == NodeState.Running)
  165. {
  166. return false;
  167. }
  168. node.NodeState = NodeState.Running;
  169. return true;
  170. }
  171. private static bool Stop(NodeBase node)
  172. {
  173. if (node.NodeState == NodeState.Running)
  174. {
  175. node.NodeState = NodeState.RunEnd;
  176. }
  177. return true;
  178. }
  179. /// <summary>
  180. /// 前置验证
  181. /// </summary>
  182. /// <param name="node"></param>
  183. /// <returns></returns>
  184. private static bool RunnerPre(NodeBase node)
  185. {
  186. node.SetPreVariables();
  187. if (node.PreConditionExpr.IsNotEmpty())
  188. {
  189. var expr = ConvertExpr(node.PreConditionExpr);
  190. var vars = node.GetVariables();
  191. node.PreConditionValidated = ExprModule
  192. .Evaluate($"RunCheckCondition('@RunningId','@Id',{expr})", vars).ValB();
  193. }
  194. if (node.PreConditionValidated)
  195. {
  196. //if (node.IsWait)
  197. //{
  198. // node.PreConditionValidated = false;
  199. // return;
  200. //}
  201. SelectNextRun(node);
  202. }
  203. return true;
  204. }
  205. /// <summary>
  206. /// 通知下一节点运行
  207. /// </summary>
  208. /// <param name="node"></param>
  209. private static void SelectNextRun(NodeBase node)
  210. {
  211. switch (node.NodeType)
  212. {
  213. case NodeType.ScenePackage:
  214. {
  215. var n = ((PackageInfo)node);
  216. var roundIndex = n.CurrentRound + 1;
  217. var roundInfo = n.Children.Select(a => (SceneRound)a).FirstOrDefault(a => a.RoundIndex == roundIndex);
  218. if (roundInfo != null)
  219. {
  220. roundInfo.NodeState = NodeState.Running;
  221. }
  222. break;
  223. }
  224. case NodeType.SceneRound:
  225. {
  226. SelectChildren2Run(node);
  227. break;
  228. }
  229. case NodeType.SceneFlowBlock:
  230. {
  231. var n = ((SceneFlowBlock)node);
  232. if (n.BlockType == SceneFlowBlockType.Objective)
  233. {
  234. SelectChildren2Run(node);
  235. }
  236. else if (n.BlockType == SceneFlowBlockType.Evolutionary &&
  237. !((SceneRound)n.Parent).EvolutionBlockHasSelected)
  238. {
  239. ((SceneRound)n.Parent).EvolutionBlockHasSelected = true;
  240. }
  241. else
  242. {
  243. n.PreConditionValidated = false;
  244. SelectRandomFlowBlock(node);
  245. }
  246. break;
  247. }
  248. case NodeType.SceneFlow:
  249. {
  250. var n = ((SceneFlow)node);
  251. var runChild = n.RunningNode;
  252. if (runChild == null)
  253. {
  254. runChild = (FlowNode)n.Children.FirstOrDefault();
  255. if (runChild != null) runChild.NodeState = NodeState.Running;
  256. }
  257. else if (runChild.HasChild)
  258. {
  259. foreach (var child in runChild.Children)
  260. {
  261. child.NodeState = NodeState.Running;
  262. }
  263. }
  264. break;
  265. }
  266. case NodeType.FlowNode:
  267. {
  268. var n = ((FlowNode)node);
  269. n.SelfFlow.RunningNode = n;
  270. foreach (var sceneInfo in n.SceneInfos)
  271. {
  272. sceneInfo.NodeState = NodeState.Running;
  273. }
  274. break;
  275. }
  276. case NodeType.SceneInfo:
  277. {
  278. SelectChildren2Run(node);
  279. break;
  280. }
  281. default:
  282. SelectChildren2Run(node);
  283. break;
  284. }
  285. }
  286. /// <summary>
  287. /// 通知子节点运行
  288. /// </summary>
  289. /// <param name="node"></param>
  290. private static void SelectChildren2Run(NodeBase node)
  291. {
  292. foreach (var child in node.Children)
  293. {
  294. child.NodeState = NodeState.Running;
  295. }
  296. }
  297. /// <summary>
  298. /// 选取演化情景流块(带权重)
  299. /// </summary>
  300. /// <param name="node"></param>
  301. private static void SelectRandomFlowBlock(NodeBase node)
  302. {
  303. var curRoundInfo = node.CurPackageInfo.CurRoundInfo;
  304. var list = curRoundInfo.Children.Select(a => (SceneFlowBlock)a).Where(a => a.BlockType == SceneFlowBlockType.Evolutionary).ToList();
  305. SceneFlowBlock selectBlock = list.FirstOrDefault();
  306. if (list.Count > 1)
  307. {
  308. //生成随机值,判断是否在权重范围内
  309. var randomResult = ExprModule.Evaluate($"IFunRandomSelection({curRoundInfo.ControlRate})");
  310. if (randomResult.ValB())
  311. {
  312. selectBlock =
  313. list.FirstOrDefault(a => a.NodeState == NodeState.Running);
  314. }
  315. else
  316. {
  317. var noRunBlock = list.Where(a => a.NodeState == NodeState.New).ToList();
  318. if (noRunBlock.Any())
  319. {
  320. var random = new Random().Next(0, noRunBlock.Count - 1);
  321. selectBlock = noRunBlock[random];
  322. }
  323. }
  324. }
  325. //选取情景流
  326. if (selectBlock != null)
  327. {
  328. var sceneCount = curRoundInfo.SceneCount;
  329. var flows = new List<NodeBase>();
  330. flows.AddRange(selectBlock.Children);
  331. sceneCount = sceneCount > flows.Count ? flows.Count : sceneCount;
  332. while (sceneCount > 0)
  333. {
  334. var id = SelectRandomFlows(ref flows);
  335. var flow = selectBlock.Children.FirstOrDefault(a => a.Id == id);
  336. if (flow != null) flow.NodeState = NodeState.Running;
  337. sceneCount--;
  338. }
  339. }
  340. }
  341. /// <summary>
  342. /// 演化情景流块选取情景流
  343. /// </summary>
  344. /// <param name="flows"></param>
  345. /// <returns></returns>
  346. private static string SelectRandomFlows(ref List<NodeBase> flows)
  347. {
  348. var random = new Random().Next(0, flows.Count - 1);
  349. var flow = flows[random];
  350. flows.Remove(flow);
  351. return flow.Id;
  352. }
  353. /// <summary>
  354. /// 运行时事件
  355. /// </summary>
  356. /// <param name="node"></param>
  357. /// <returns></returns>
  358. private static bool RunnerRunning(NodeBase node)
  359. {
  360. if (node.RunningConditionExpr.IsNotEmpty())
  361. {
  362. var expr = ConvertExpr(node.RunningConditionExpr);
  363. var vars = node.GetVariables();
  364. ExprModule.Evaluate(expr, vars);
  365. }
  366. var guideInfos = node.GuideInfos?.Where(a => a.IsPush).ToList();
  367. if (guideInfos != null && guideInfos.Any())
  368. {
  369. foreach (var guideInfo in guideInfos)
  370. {
  371. node.CurPackageInfo.MessageSender.SendMsg(guideInfo.Obj2String(),MessageType.GuideInfo);
  372. }
  373. }
  374. if (node.NodeType==NodeType.SceneInfo)
  375. {
  376. var scene = (SceneInfo) node;
  377. var pushSceneInfo = new PushSceneInfo(scene);
  378. node.CurPackageInfo.MessageSender.SendMsg(pushSceneInfo.Obj2String(), MessageType.SceneInfo);
  379. if(scene.EnvironmentRenders!=null&& scene.EnvironmentRenders.Any())
  380. {
  381. foreach (var render in scene.EnvironmentRenders)
  382. {
  383. node.CurPackageInfo.MessageSender.SendMsg(render.Obj2String(),MessageType.Environment);
  384. }
  385. }
  386. }
  387. return true;
  388. }
  389. /// <summary>
  390. /// 运行结束时事件
  391. /// </summary>
  392. /// <param name="node"></param>
  393. /// <returns></returns>
  394. private static bool RunnerRunned(NodeBase node)
  395. {
  396. node.SetRunnedVariables();
  397. if (node.RunnedConditionExpr.IsNotEmpty())
  398. {
  399. var expr = ConvertExpr(node.RunnedConditionExpr);
  400. var vars = node.GetVariables();
  401. node.RunnedConditionValidated = ExprModule
  402. .Evaluate($"RunCheckCondition('@RunningId','@Id',{expr})", vars).ValB();
  403. }
  404. if (node.RunnedConditionValidated)
  405. {
  406. SelectPrevEnd(node);
  407. node.NodeState = NodeState.Complete;
  408. }
  409. return true;
  410. }
  411. /// <summary>
  412. /// 通知父节点结束
  413. /// </summary>
  414. /// <param name="node"></param>
  415. private static void SelectPrevEnd(NodeBase node)
  416. {
  417. switch (node.NodeType)
  418. {
  419. case NodeType.ScenePackage:
  420. {
  421. var n = ((PackageInfo)node);
  422. if (n.AutoNextRound)
  423. {
  424. }
  425. break;
  426. }
  427. case NodeType.SceneRound:
  428. {
  429. var n = ((SceneRound)node);
  430. if (n.HasParent && !n.Parent.ChildIsRunning)
  431. {
  432. var nextRound = n.Parent.Children.Select(a => (SceneRound)a)
  433. .FirstOrDefault(a => a.RoundIndex == n.RoundIndex + 1);
  434. if (nextRound == null)
  435. {
  436. n.Parent.NodeState = NodeState.RunEnd;
  437. }
  438. }
  439. break;
  440. }
  441. case NodeType.SceneFlowBlock:
  442. {
  443. SelectParent2Stop(node);
  444. break;
  445. }
  446. case NodeType.SceneFlow:
  447. SelectParent2Stop(node);
  448. break;
  449. case NodeType.FlowNode:
  450. SelectParent2Stop(node);
  451. break;
  452. case NodeType.SceneInfo:
  453. SelectParent2Stop(node);
  454. break;
  455. default:
  456. SelectParent2Stop(node);
  457. break;
  458. }
  459. }
  460. /// <summary>
  461. /// 通知父节点结束
  462. /// </summary>
  463. /// <param name="node"></param>
  464. private static void SelectParent2Stop(NodeBase node)
  465. {
  466. if (node.HasParent && !node.Parent.ChildIsRunning)
  467. {
  468. node.Parent.NodeState = NodeState.RunEnd;
  469. }
  470. }
  471. /// <summary>
  472. /// 后置事件
  473. /// </summary>
  474. /// <param name="node"></param>
  475. /// <returns></returns>
  476. private static bool RunnerPost(NodeBase node)
  477. {
  478. if (node.PostConditionExpr.IsNotEmpty())
  479. {
  480. var expr = ConvertExpr(node.PostConditionExpr);
  481. var vars = node.GetVariables();
  482. ExprModule.Evaluate(expr, vars);
  483. }
  484. CallNodesToRun(node);
  485. NextNode(node);
  486. return true;
  487. }
  488. /// <summary>
  489. /// 通知待运行节点
  490. /// </summary>
  491. /// <param name="node"></param>
  492. private static void CallNodesToRun(NodeBase node)
  493. {
  494. var waitNodes = node.CurPackageInfo.WaitForRunNode;
  495. if (waitNodes.Count > 0 && waitNodes.ContainsKey(node.Id))
  496. {
  497. var toRunIds = waitNodes[node.Id];
  498. if (toRunIds != null && toRunIds.Any())
  499. {
  500. foreach (var nodeId in toRunIds)
  501. {
  502. var runNode = node.CurPackageInfo.GetNodeByPath(nodeId);
  503. if (runNode.Parent.Children.All(a => a.NodeState == NodeState.New))
  504. {
  505. runNode.NodeState = NodeState.Running;
  506. }
  507. }
  508. }
  509. }
  510. }
  511. /// <summary>
  512. /// 关键字匹配
  513. /// </summary>
  514. /// <param name="node"></param>
  515. /// <returns></returns>
  516. private static bool RunnerMatchKey(NodeBase node)
  517. {
  518. //string word = node.ExtendData?.BehaviorWord ?? "";
  519. //string role = node.ExtendData?.BehaviorRole ?? "";
  520. node.CurPackageInfo.ScoreEvalModule.MatchKeyWord(node);
  521. return true;
  522. }
  523. /// <summary>
  524. /// 关键字匹配成功
  525. /// </summary>
  526. /// <param name="node"></param>
  527. /// <returns></returns>
  528. private static bool RunnerMatchSuccess(NodeBase node)
  529. {
  530. if (node.NodeType != NodeType.KeyWord)
  531. {
  532. return false;
  533. }
  534. var n = (KeyWord) node;
  535. string word = node.ExtendData?.MatchWord ?? "";
  536. n.HasMatched = true;
  537. n.MatchWord = word;
  538. return true;
  539. }
  540. /// <summary>
  541. /// 下一节点
  542. /// </summary>
  543. /// <param name="node"></param>
  544. private static void NextNode(NodeBase node)
  545. {
  546. switch (node.NodeType)
  547. {
  548. case NodeType.SceneRound:
  549. {
  550. var np = ((PackageInfo)node.Parent);
  551. if (np.AutoNextRound)
  552. {
  553. SelectNextRun(np);
  554. }
  555. break;
  556. }
  557. case NodeType.FlowNode:
  558. {
  559. var n = ((FlowNode)node);
  560. if (n.HasChild&&n.Children.Count>0)
  561. {
  562. SelectNextRun(n.SelfFlow);
  563. }
  564. }
  565. break;
  566. }
  567. }
  568. /// <summary>
  569. /// 下一轮次
  570. /// </summary>
  571. /// <param name="node"></param>
  572. /// <returns></returns>
  573. private static bool NextRound(NodeBase node)
  574. {
  575. if (node.CurPackageInfo.CurRoundInfo.NodeState != NodeState.Complete)
  576. {
  577. node.CurPackageInfo.CurRoundInfo.NodeState = NodeState.Complete;
  578. return true;
  579. }
  580. return false;
  581. }
  582. /// <summary>
  583. /// 下一情景流
  584. /// </summary>
  585. /// <param name="node"></param>
  586. /// <returns></returns>
  587. public static bool NextFlowNode(NodeBase node)
  588. {
  589. FlowNode flowNode = null;
  590. if (node.NodeType == NodeType.SceneFlow)
  591. {
  592. flowNode = ((SceneFlow) node).RunningNode;
  593. //flowNode.NodeState = NodeState.Running;
  594. }
  595. else if (node.NodeType == NodeType.FlowNode)
  596. {
  597. flowNode = (FlowNode) node;
  598. }
  599. if (flowNode != null && flowNode.NodeState != NodeState.New)
  600. {
  601. if (flowNode.NodeState == NodeState.Running)
  602. {
  603. flowNode.NodeState = NodeState.RunEnd;
  604. }else if (flowNode.NodeState == NodeState.RunEnd)
  605. {
  606. flowNode.NodeState = NodeState.Complete;
  607. }
  608. else
  609. {
  610. return false;
  611. }
  612. return true;
  613. }
  614. return false;
  615. }
  616. /// <summary>
  617. /// 下一情景流(当前运行的所有情景流)
  618. /// </summary>
  619. /// <param name="node"></param>
  620. /// <returns></returns>
  621. private static bool NextFlowNodes(NodeBase node)
  622. {
  623. if (node.CurPackageInfo != null && node.CurPackageInfo.CurFlowNodes.Any())
  624. {
  625. foreach (var child in node.CurPackageInfo.CurFlowNodes)
  626. {
  627. if (child.NodeState == NodeState.New)
  628. {
  629. continue;
  630. }
  631. if (child.NodeState == NodeState.Running|| child.NodeState==NodeState.RunEnd)
  632. {
  633. child.NodeState = NodeState.Complete;
  634. }
  635. }
  636. return true;
  637. }
  638. return false;
  639. }
  640. /// <summary>
  641. /// 特殊表达式转换(添加上RunningId和节点Id)
  642. /// </summary>
  643. /// <param name="expr"></param>
  644. /// <returns></returns>
  645. private static string ConvertExpr(string expr)
  646. {
  647. if (expr.IndexOf("WithRp(", StringComparison.Ordinal) > 0)
  648. {
  649. expr = expr.Replace("WithRp(", "WithRp('@RunningId','@Id',");
  650. }
  651. return expr;
  652. }
  653. //private string C(string expr)
  654. //{
  655. // string str = "";
  656. // return str;
  657. //}
  658. }
  659. }