PackageNode.cs 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using IwbZero.Expr;
  5. using IwbZero.IwbBase;
  6. using IwbZero.ToolCommon.StringModel;
  7. using WeEngine.CommonDto;
  8. using WeEngine.Enum;
  9. using WeEngine.Message;
  10. using WeEngine.Strategy.Eval;
  11. using WeEngine.Strategy.Runner;
  12. namespace WeEngine.Packages
  13. {
  14. /// <summary>
  15. /// 方案包
  16. /// </summary>
  17. public class PackageNode : NodeBase
  18. {
  19. public PackageNode()
  20. {
  21. EventSwitch = true;
  22. NodeType = NodeType.ScenePackage;
  23. WaitForRunNode= new Dictionary<string, List<string>>();
  24. Variables = DefaultVariable.Variables;
  25. }
  26. public bool EventSwitch { get; set; }
  27. /// <summary>
  28. /// 目标客户端
  29. /// </summary>
  30. public string TargetClientId { get; set; }
  31. /// <summary>
  32. /// 模型类型
  33. /// </summary>
  34. public string EngineType { get; set; }
  35. /// <summary>
  36. /// 评估模块
  37. /// </summary>
  38. public IEvalManager EvalManager { get; set; }
  39. /// <summary>
  40. /// 运行模块
  41. /// </summary>
  42. public IRunnerManager RunnerManager { get; set; }
  43. /// <summary>
  44. /// 消息发送模块
  45. /// </summary>
  46. public IMessageSender MessageSender { get; set; }
  47. /// <summary>
  48. /// 运行ID
  49. /// </summary>
  50. public string RunningId { get; set; }
  51. /// <summary>
  52. /// 考核角色
  53. /// </summary>
  54. public List<string> AssessRoles { get; set; }
  55. /// <summary>
  56. /// 未考核分数自动运行(满分)
  57. /// </summary>
  58. public bool AssessAuto { get; set; }
  59. /// <summary>
  60. /// 自动下一轮
  61. /// </summary>
  62. public bool AutoNextRound { get; set; }
  63. /// <summary>
  64. /// 每轮总分
  65. /// </summary>
  66. public decimal RoundScore { get; set; }
  67. /// <summary>
  68. /// 等待运行的节点
  69. /// </summary>
  70. public Dictionary<string,List<string>> WaitForRunNode { get; set; }
  71. /// <summary>
  72. /// 模块已加载
  73. /// </summary>
  74. public bool ModuleHasLoad => EvalManager != null && RunnerManager != null && MessageSender != null;
  75. /// <summary>
  76. /// 从运行数据恢复到运行态
  77. /// </summary>
  78. /// <param name="running"></param>
  79. public void ConvertFromRun(RunningInfo running)
  80. {
  81. EventSwitch = false;
  82. RunningId = running.Id;
  83. NodeState = GetNodeState(running);
  84. AssessAuto = running.AssessAuto;
  85. AssessRoles = running.AssessRoles;
  86. RoundScore = running.RoundScore;
  87. AutoNextRound = running.AutoNextRound;
  88. Variables = running.VariableTable;
  89. WaitPath = running.WaitPath;
  90. if (HasChild)
  91. {
  92. foreach (var nodeBase in Children)
  93. {
  94. var child = (SceneRoundNode)nodeBase;
  95. var runChild = running.RoundInfo.FirstOrDefault(a => a.Path == child.NodePath);
  96. if (runChild != null)
  97. {
  98. child.ConvertFromRun(runChild);
  99. }
  100. }
  101. }
  102. EventSwitch = true;
  103. }
  104. #region CurrentInfo
  105. public RunningInfo RunningInfo => GetRunningInfo();
  106. /// <summary>
  107. /// 当前轮次
  108. /// </summary>
  109. public int CurrentRound => CurRoundInfo?.RoundIndex ?? 0;
  110. /// <summary>
  111. /// 当前轮次信息
  112. /// </summary>
  113. public SceneRoundNode CurRoundInfo => GetCurrentRoundInfo();
  114. /// <summary>
  115. /// 当前情景流块
  116. /// </summary>
  117. public List<SceneFlowBlockNode> CurSceneFlowBlocks => GetCurrentFlowBlocks();
  118. /// <summary>
  119. /// 当前情景流
  120. /// </summary>
  121. public List<SceneFlowNode> CurSceneFlows => GetCurrentFlows();
  122. /// <summary>
  123. /// 当前流节点
  124. /// </summary>
  125. public List<FlowNode> CurFlowNodes => GetCurrentFlowNodes();
  126. /// <summary>
  127. /// 当前情景
  128. /// </summary>
  129. public List<SceneNode> CurSceneInfos => GetCurrentSceneInfos();
  130. /// <summary>
  131. /// 当前行为
  132. /// </summary>
  133. public List<BehaviorNode> CurBehaviorInfos => GetCurrentBehaviors();
  134. /// <summary>
  135. /// 已运行的行为
  136. /// </summary>
  137. public List<BehaviorNode> AllRunnedBehaviorInfos => GetAllRunnedBehaviors();
  138. /// <summary>
  139. /// 当前轮次各标签总得分
  140. /// </summary>
  141. public Dictionary<string, decimal> BehaviorTagScoreDic => GetBehaviorTagScoreDic();
  142. /// <summary>
  143. /// 当前轮次所运行的行为各标签总分
  144. /// </summary>
  145. public Dictionary<string, decimal> BehaviorTagFullScoreDic => GetBehaviorTagScoreDic(true);
  146. /// <summary>
  147. /// 当前关键字
  148. /// </summary>
  149. public List<KeyWord> CurKeyWords => GetCurrentKeyWords();
  150. /// <summary>
  151. /// 未匹配的关键字
  152. /// </summary>
  153. public List<KeyWord> NotMatchKeyWords => GetNotMatchKeyWords();
  154. /// <summary>
  155. /// 获取当前轮次
  156. /// </summary>
  157. /// <returns></returns>
  158. private SceneRoundNode GetCurrentRoundInfo()
  159. {
  160. var round = (SceneRoundNode) Children.FirstOrDefault(a => a.NodeState == NodeState.Running) ?? Children
  161. .Select(a => (SceneRoundNode) a).OrderByDescending(a => a.RoundIndex)
  162. .FirstOrDefault(a => a.NodeState == NodeState.Complete || a.NodeState == NodeState.RunEnd);
  163. return round;
  164. }
  165. /// <summary>
  166. /// 获取当前情景流块
  167. /// </summary>
  168. /// <returns></returns>
  169. private List<SceneFlowBlockNode> GetCurrentFlowBlocks()
  170. {
  171. var list = new List<SceneFlowBlockNode>();
  172. var children = CurRoundInfo?.HasChild == true ? CurRoundInfo.Children.Where(a => a.NodeState == NodeState.Running).Select(a => (SceneFlowBlockNode)a).ToList() : new List<SceneFlowBlockNode>();
  173. list.AddRange(children);
  174. return list;
  175. }
  176. /// <summary>
  177. /// 获取当前情景流
  178. /// </summary>
  179. /// <returns></returns>
  180. private List<SceneFlowNode> GetCurrentFlows()
  181. {
  182. var list = new List<SceneFlowNode>();
  183. foreach (var cur in CurSceneFlowBlocks)
  184. {
  185. var children = cur?.HasChild == true ? cur.Children.Where(a => a.NodeState == NodeState.Running).Select(a => (SceneFlowNode)a).ToList() : new List<SceneFlowNode>();
  186. list.AddRange(children);
  187. }
  188. return list;
  189. }
  190. /// <summary>
  191. /// 获取当前流节点
  192. /// </summary>
  193. /// <returns></returns>
  194. private List<FlowNode> GetCurrentFlowNodes()
  195. {
  196. var list = new List<FlowNode>();
  197. foreach (var cur in CurSceneFlows)
  198. {
  199. var flowNode = cur.RunningNode?.NodeState == NodeState.Running ? cur.RunningNode : null;
  200. if (flowNode != null)
  201. {
  202. list.Add(flowNode);
  203. }
  204. }
  205. return list;
  206. }
  207. /// <summary>
  208. /// 获取当前情景信息
  209. /// </summary>
  210. /// <returns></returns>
  211. private List<SceneNode> GetCurrentSceneInfos()
  212. {
  213. var list = new List<SceneNode>();
  214. foreach (var cur in CurFlowNodes)
  215. {
  216. var children = cur?.SceneInfos.Count >0
  217. ? cur.SceneInfos.Where(a => a.NodeState == NodeState.Running).Select(a => a)
  218. .ToList()
  219. : new List<SceneNode>();
  220. list.AddRange(children);
  221. }
  222. return list;
  223. }
  224. /// <summary>
  225. /// 获取当前行为信息
  226. /// </summary>
  227. /// <returns></returns>
  228. private List<BehaviorNode> GetCurrentBehaviors()
  229. {
  230. var list = new List<BehaviorNode>();
  231. foreach (var cur in CurSceneInfos)
  232. {
  233. var children = cur?.HasChild == true ? cur.Children.Select(a => (BehaviorNode)a).ToList() : new List<BehaviorNode>();
  234. list.AddRange(children);
  235. }
  236. return list;
  237. }
  238. /// <summary>
  239. /// 获取已运行的行为信息
  240. /// </summary>
  241. /// <returns></returns>
  242. private List<BehaviorNode> GetAllRunnedBehaviors()
  243. {
  244. var list = new List<BehaviorNode>();
  245. var rounds = HasChild ? Children.Where(a => a.NodeState != NodeState.New).ToList() : null;
  246. if (rounds==null || !rounds.Any())
  247. {
  248. return list;
  249. }
  250. List<NodeBase> blocks = new List<NodeBase>();
  251. foreach (var node in rounds)
  252. {
  253. blocks.AddRange(node.HasChild ? node.Children.Where(a => a.NodeState != NodeState.New).ToList() : new List<NodeBase>());
  254. }
  255. if (!blocks.Any())
  256. {
  257. return list;
  258. }
  259. List<NodeBase> flows = new List<NodeBase>();
  260. foreach (var node in blocks)
  261. {
  262. flows.AddRange(node.HasChild ? node.Children.Where(a => a.NodeState != NodeState.New).ToList() : new List<NodeBase>());
  263. }
  264. if (!flows.Any())
  265. {
  266. return list;
  267. }
  268. List<NodeBase> flowNodes = new List<NodeBase>();
  269. foreach (var node in flows)
  270. {
  271. flowNodes.AddRange(node.HasChild ? GetAllRunFlowNodes(node.Children.FirstOrDefault(a => a.NodeState != NodeState.New)) : new List<NodeBase>());
  272. }
  273. if (!flowNodes.Any())
  274. {
  275. return list;
  276. }
  277. List<SceneNode> scenes = new List<SceneNode>();
  278. foreach (var nodeBase in flowNodes)
  279. {
  280. var node = (FlowNode) nodeBase;
  281. scenes.AddRange(node.HasChild ? node.SceneInfos.Where(a => a.NodeState != NodeState.New).ToList() : new List<SceneNode>());
  282. }
  283. if (!scenes.Any())
  284. {
  285. return list;
  286. }
  287. foreach (var node in scenes)
  288. {
  289. list.AddRange(node.HasChild ? node.Children.Where(a => a.NodeState != NodeState.New).Select(a=>(BehaviorNode)a).ToList() : new List<BehaviorNode>());
  290. }
  291. return list;
  292. }
  293. /// <summary>
  294. /// 递归获取已运行的流节点
  295. /// </summary>
  296. /// <param name="node"></param>
  297. /// <returns></returns>
  298. private List<NodeBase> GetAllRunFlowNodes(NodeBase node)
  299. {
  300. List<NodeBase> flowNodes = new List<NodeBase>();
  301. if (node == null)
  302. {
  303. return flowNodes;
  304. }
  305. if (node.NodeType == NodeType.FlowNode)
  306. {
  307. flowNodes.Add(node);
  308. }
  309. if (node.HasChild)
  310. {
  311. flowNodes.AddRange(GetAllRunFlowNodes(node.Children.FirstOrDefault(a => a.NodeState != NodeState.New)));
  312. }
  313. return flowNodes;
  314. }
  315. /// <summary>
  316. /// 计算标签的总分
  317. /// </summary>
  318. /// <param name="isAll"></param>
  319. /// <returns></returns>
  320. private Dictionary<string, decimal> GetBehaviorTagScoreDic(bool isAll = false)
  321. {
  322. var dic= new Dictionary<string, decimal>();
  323. var behaviorInfos = AllRunnedBehaviorInfos?.Where(a => a.BehaviorScoreType != BehaviorScoreType.ImportantNegative).ToList();
  324. if (behaviorInfos != null&& behaviorInfos.Any())
  325. {
  326. foreach(var be in behaviorInfos)
  327. {
  328. if(be.BehaviorTagArray!=null&& be.BehaviorTagArray.Any())
  329. {
  330. foreach (var tag in be.BehaviorTagArray)
  331. {
  332. var score = isAll
  333. ? be.BehaviorScoreType == BehaviorScoreType.Normal ? be.NodeFullScore : 0
  334. : be.ActualScore;
  335. if (dic.ContainsKey(tag))
  336. {
  337. var temp = dic[tag];
  338. dic[tag] = temp + score;
  339. }
  340. else
  341. {
  342. dic.Add(tag, score);
  343. }
  344. }
  345. }
  346. }
  347. }
  348. return dic;
  349. }
  350. /// <summary>
  351. /// 获取当前关键字
  352. /// </summary>
  353. /// <returns></returns>
  354. private List<KeyWord> GetCurrentKeyWords()
  355. {
  356. var list = new List<KeyWord>();
  357. foreach (var cur in CurBehaviorInfos)
  358. {
  359. var children = cur.HasChild
  360. ? cur.Children.Select(a => (KeyWord)a).ToList()
  361. : new List<KeyWord>();
  362. list.AddRange(children);
  363. }
  364. return list;
  365. }
  366. /// <summary>
  367. /// 获取未匹配的当前关键字
  368. /// </summary>
  369. /// <returns></returns>
  370. private List<KeyWord> GetNotMatchKeyWords()
  371. {
  372. var list = new List<KeyWord>();
  373. foreach (var cur in CurBehaviorInfos)
  374. {
  375. var children = cur.HasChild
  376. ? cur.Children.Select(a => (KeyWord)a).Where(a => !a.HasMatched).ToList()
  377. : new List<KeyWord>();
  378. list.AddRange(children);
  379. }
  380. return list;
  381. }
  382. #endregion
  383. #region RUNNING
  384. public RunningInfo GetRunningInfo()
  385. {
  386. var running = new RunningInfo(this)
  387. {
  388. Id = RunningId,
  389. CurrentRoundIndex = CurrentRound,
  390. PackageNo = Id,
  391. AssessAuto = AssessAuto,
  392. AssessRoles = AssessRoles,
  393. RoundScore = RoundScore,
  394. AutoNextRound= AutoNextRound,
  395. RoundInfo = new List<RoundRunningInfo>()
  396. };
  397. if (Children != null && Children.Any())
  398. {
  399. foreach (var child in Children)
  400. {
  401. GetRoundRunningInfo((SceneRoundNode)child, running);
  402. }
  403. }
  404. return running;
  405. }
  406. public RoundRunningInfo GetRoundRunningInfo(SceneRoundNode node, RunningInfo parent)
  407. {
  408. var running = new RoundRunningInfo(node)
  409. {
  410. RoundIndex = node.RoundIndex,
  411. BlockInfo = new List<BlockRunningInfo>()
  412. };
  413. if (running.IsStart)
  414. {
  415. parent.CurrentRoundIndex = running.RoundIndex;
  416. }
  417. if (node.Children != null && node.Children.Any())
  418. {
  419. foreach (var child in node.Children)
  420. {
  421. GetBlockRunningInfo((SceneFlowBlockNode)child, running);
  422. }
  423. }
  424. parent.RoundInfo.Add(running);
  425. return running;
  426. }
  427. public BlockRunningInfo GetBlockRunningInfo(SceneFlowBlockNode node, RoundRunningInfo parent)
  428. {
  429. var running = new BlockRunningInfo(node)
  430. {
  431. FlowInfo = new List<FlowRunningInfo>()
  432. };
  433. if (node.Children != null && node.Children.Any())
  434. {
  435. foreach (var child in node.Children)
  436. {
  437. GetFlowRunningInfo((SceneFlowNode)child, running);
  438. }
  439. }
  440. parent.BlockInfo.Add(running);
  441. return running;
  442. }
  443. public FlowRunningInfo GetFlowRunningInfo(SceneFlowNode node, BlockRunningInfo parent)
  444. {
  445. var running = new FlowRunningInfo(node)
  446. {
  447. NodeInfo = new List<FlowNodeRunningInfo>()
  448. };
  449. if (node.Children != null && node.Children.Any())
  450. {
  451. foreach (var child in node.Children)
  452. {
  453. GetFlowNodeRunningInfo((FlowNode)child, running);
  454. }
  455. }
  456. parent.FlowInfo.Add(running);
  457. return running;
  458. }
  459. public FlowNodeRunningInfo GetFlowNodeRunningInfo(FlowNode node, FlowRunningInfo parent)
  460. {
  461. var running = new FlowNodeRunningInfo(node)
  462. {
  463. NodeInfo = new List<FlowNodeRunningInfo>(),
  464. SceneInfo = new List<SceneRunningInfo>()
  465. };
  466. if (node.Children != null && node.Children.Any())
  467. {
  468. foreach (var child in node.Children)
  469. {
  470. GetFlowNodeRunningInfo((FlowNode)child, running);
  471. }
  472. }
  473. if (node.SceneInfos != null && node.SceneInfos.Any())
  474. {
  475. foreach (var sceneInfo in node.SceneInfos)
  476. {
  477. GetSceneRunningInfo(sceneInfo, running);
  478. }
  479. }
  480. parent.NodeInfo.Add(running);
  481. return running;
  482. }
  483. public FlowNodeRunningInfo GetFlowNodeRunningInfo(FlowNode node, FlowNodeRunningInfo parent)
  484. {
  485. var running = new FlowNodeRunningInfo(node)
  486. {
  487. NodeInfo = new List<FlowNodeRunningInfo>(),
  488. SceneInfo = new List<SceneRunningInfo>()
  489. };
  490. if (node.Children != null && node.Children.Any())
  491. {
  492. foreach (var child in node.Children)
  493. {
  494. GetFlowNodeRunningInfo((FlowNode)child, running);
  495. }
  496. }
  497. if (node.SceneInfos != null && node.SceneInfos.Any())
  498. {
  499. foreach (var sceneInfo in node.SceneInfos)
  500. {
  501. GetSceneRunningInfo(sceneInfo, running);
  502. }
  503. }
  504. parent.NodeInfo.Add(running);
  505. return running;
  506. }
  507. public SceneRunningInfo GetSceneRunningInfo(SceneNode node, FlowNodeRunningInfo parent)
  508. {
  509. var running = new SceneRunningInfo(node)
  510. {
  511. BehaviorInfo = new List<BehaviorRunningInfo>()
  512. };
  513. if (node.Children != null && node.Children.Any())
  514. {
  515. foreach (var child in node.Children)
  516. {
  517. GetBehaviorRunningInfo((BehaviorNode)child, running);
  518. }
  519. }
  520. parent.SceneInfo.Add(running);
  521. return running;
  522. }
  523. public BehaviorRunningInfo GetBehaviorRunningInfo(BehaviorNode node, SceneRunningInfo parent)
  524. {
  525. var running = new BehaviorRunningInfo(node) {KeyWords = node.GetKeyWords()};
  526. parent.BehaviorInfo.Add(running);
  527. return running;
  528. }
  529. #endregion
  530. #region override
  531. public override void SetPreVariables()
  532. {
  533. SetVariable(DefaultVariable.RunningId, RunningId);
  534. base.SetPreVariables();
  535. }
  536. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  537. {
  538. EventSwitch = true;
  539. if (xmlNode != null)
  540. {
  541. RunningId = xmlNode.GetChildValue("RunningId");
  542. EngineType = xmlNode.GetChildValue("EngineType");
  543. AutoNextRound = xmlNode.GetChildValue("AutoNextRound").ValB();
  544. AssessRoles = xmlNode.GetChildValue("AssessRoles")?.StrToArray()?.ToList() ?? new List<string>();
  545. AssessAuto = xmlNode.GetChildValue("AssessAuto").ValB();
  546. RoundScore = xmlNode.GetChildValue("RoundScore").ValD();
  547. var childNodes = xmlNode.GetChildNode("RoundInfos")?.Nodes;
  548. if (childNodes != null && childNodes.Count > 0)
  549. {
  550. foreach (IwbXmlNode childNode in childNodes)
  551. {
  552. var node = (SceneRoundNode)new SceneRoundNode() { Parent = this }.CreateNodeByXmlNode(childNode);
  553. Children.Add(node);
  554. }
  555. }
  556. }
  557. }
  558. public override string ToXmlString()
  559. {
  560. return ToXmlStringSelf();
  561. }
  562. public string ToXmlStringSelf(bool withHeader = true)
  563. {
  564. string str = withHeader ? "<?xml version=\"1.0\" encoding=\"utf-8\"?>" : "";
  565. str += $"<PackageInfo id=\"{Id}\">\r\n";
  566. str += RunningId.IsEmpty() ? "" : $"<RunningId>{RunningId}</RunningId>";
  567. str += base.ToXmlString();
  568. str += $"<CurrentRound>{CurrentRound}</CurrentRound>\r\n";
  569. str += $"<AutoNextRound>{AutoNextRound}</AutoNextRound>\r\n";
  570. str += EngineType.IsEmpty() ? "" : $"<EngineType>{EngineType}</EngineType>\r\n";
  571. str += AssessRoles == null ? "" : $"<AssessRoles>{AssessRoles?.ToArray().ArrayToStr()}</AssessRoles>\r\n";
  572. str += $"<AssessAuto>{AssessAuto.ToString().UAndT()}</AssessAuto>\r\n";
  573. if (Children?.Count > 0)
  574. {
  575. str += "<RoundInfos>\r\n";
  576. foreach (var sceneRound in Children)
  577. {
  578. str += sceneRound.ToXmlString();
  579. }
  580. str += "</RoundInfos>\r\n";
  581. }
  582. str += "</PackageInfo>\r\n";
  583. return str;
  584. }
  585. #endregion override
  586. }
  587. /// <summary>
  588. /// 演练轮次
  589. /// </summary>
  590. public class SceneRoundNode : NodeBase
  591. {
  592. public SceneRoundNode()
  593. {
  594. NodeType = NodeType.SceneRound;
  595. }
  596. /// <summary>
  597. /// 轮次
  598. /// </summary>
  599. public int RoundIndex { get; set; }
  600. private decimal _roundFullScore;
  601. /// <summary>
  602. /// 轮次总分
  603. /// </summary>
  604. public decimal RoundFullScore
  605. {
  606. get => (HasParent && ((PackageNode) Parent).RoundScore > 0)
  607. ? ((PackageNode) Parent).RoundScore
  608. : _roundFullScore;
  609. set => _roundFullScore = value;
  610. }
  611. /// <summary>
  612. /// 最大情景流数量
  613. /// </summary>
  614. public int SceneCount { get; set; }
  615. private int _controlRate;
  616. /// <summary>
  617. /// 随机几率
  618. /// </summary>
  619. public int ControlRate
  620. {
  621. get => _controlRate;
  622. set => _controlRate = value > 100 ? 100 : value < 0 ? 0 : value;
  623. }
  624. /// <summary>
  625. /// 获取当前所在的轮次
  626. /// </summary>
  627. /// <returns></returns>
  628. public SceneRoundNode GetSelfRound()
  629. {
  630. return this;
  631. }
  632. /// <summary>
  633. /// 是否已选中演化情景流块(一个轮次只能选一个演化情景流块)
  634. /// </summary>
  635. public bool EvolutionBlockHasSelected { get; set; }
  636. /// <summary>
  637. /// 从运行数据恢复到运行态
  638. /// </summary>
  639. /// <param name="running"></param>
  640. public void ConvertFromRun(RoundRunningInfo running)
  641. {
  642. Variables = running.VariableTable;
  643. WaitPath = running.WaitPath;
  644. NodeState = GetNodeState(running);
  645. if (HasChild)
  646. {
  647. foreach (var nodeBase in Children)
  648. {
  649. var child = (SceneFlowBlockNode)nodeBase;
  650. var runChild = running.BlockInfo.FirstOrDefault(a => a.Path == child.NodePath);
  651. if (runChild != null)
  652. {
  653. child.ConvertFromRun(runChild);
  654. }
  655. }
  656. }
  657. }
  658. #region override
  659. public override void SetPreVariables()
  660. {
  661. SetVariable(DefaultVariable.FullRoundScore, RoundFullScore);
  662. var score = RoundIndex == 1
  663. ? new Random(0).Next(0, Convert.ToInt32(NodeFullScore))
  664. : Parent.Children.Select(a => (SceneRoundNode) a).FirstOrDefault(a => a.RoundIndex == RoundIndex - 1)?
  665. .ActualScore ?? 0;
  666. SetVariable(DefaultVariable.PrevNodeScore, score);
  667. SetVariable(DefaultVariable.RoundScore, score);
  668. base.SetPreVariables();
  669. }
  670. public override void SetRunnedVariables()
  671. {
  672. SetVariable(DefaultVariable.RoundScore,ActualScore);
  673. base.SetRunnedVariables();
  674. }
  675. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  676. {
  677. if (xmlNode != null)
  678. {
  679. RoundIndex = xmlNode.GetChildValue("RoundIndex").ValI();
  680. SceneCount = xmlNode.GetChildValue("SceneCount").ValI();
  681. RoundFullScore = xmlNode.GetChildValue("RoundFullScore").ValD();
  682. ControlRate = xmlNode.GetChildValue("ControlRate").ValI();
  683. var childNodes = xmlNode.GetChildNode("BlockInfos")?.Nodes;
  684. if (childNodes != null && childNodes.Count > 0)
  685. {
  686. foreach (IwbXmlNode childNode in childNodes)
  687. {
  688. var node = (SceneFlowBlockNode)new SceneFlowBlockNode() { Parent = this }.CreateNodeByXmlNode(childNode);
  689. Children.Add(node);
  690. }
  691. }
  692. }
  693. }
  694. public override string ToXmlString()
  695. {
  696. string str = "";
  697. str += $"<RoundInfo id=\"{Id}\">\r\n";
  698. str += base.ToXmlString();
  699. str += $"<RoundIndex>{RoundIndex}</RoundIndex>\r\n";
  700. str += $"<RoundFullScore>{RoundFullScore}</RoundFullScore>\r\n";
  701. str += $"<SceneCount>{SceneCount}</SceneCount>\r\n";
  702. str += $"<ControlRate>{ControlRate}</ControlRate>\r\n";
  703. if (Children?.Count > 0)
  704. {
  705. str += "<BlockInfos>\r\n";
  706. foreach (var flowBlock in Children)
  707. {
  708. str += flowBlock.ToXmlString();
  709. }
  710. str += "</BlockInfos>\r\n";
  711. }
  712. str += "</RoundInfo>\r\n";
  713. return str;
  714. }
  715. #endregion override
  716. }
  717. /// <summary>
  718. /// 情景流块
  719. /// </summary>
  720. public class SceneFlowBlockNode : NodeBase
  721. {
  722. public SceneFlowBlockNode()
  723. {
  724. NodeType = NodeType.SceneFlowBlock;
  725. }
  726. /// <summary>
  727. /// 情景流块类型(客观|演化)
  728. /// </summary>
  729. public SceneFlowBlockType BlockType { get; set; }
  730. /// <summary>
  731. /// 当前轮次
  732. /// </summary>
  733. public SceneRoundNode SelfRound => GetSelfRound();
  734. /// <summary>
  735. /// 获取当前所在的轮次
  736. /// </summary>
  737. /// <returns></returns>
  738. public SceneRoundNode GetSelfRound()
  739. {
  740. return HasParent ? ((SceneRoundNode)Parent).GetSelfRound() : null;
  741. }
  742. /// <summary>
  743. /// 从运行数据恢复到运行态
  744. /// </summary>
  745. /// <param name="running"></param>
  746. public void ConvertFromRun(BlockRunningInfo running)
  747. {
  748. Variables = running.VariableTable;
  749. WaitPath = running.WaitPath;
  750. NodeState = GetNodeState(running);
  751. if (HasChild)
  752. {
  753. foreach (var nodeBase in Children)
  754. {
  755. var child = (SceneFlowNode)nodeBase;
  756. var runChild = running.FlowInfo.FirstOrDefault(a => a.Path == child.NodePath);
  757. if (runChild != null)
  758. {
  759. child.ConvertFromRun(runChild);
  760. }
  761. }
  762. }
  763. }
  764. #region override
  765. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  766. {
  767. if (xmlNode != null)
  768. {
  769. BlockType = xmlNode.GetChildValue("FlowBlockType").GetEnumByName<SceneFlowBlockType>();
  770. var flowNos = xmlNode.GetChildValue("SceneFlowNos")?.StrToArray();
  771. if (flowNos != null && flowNos.Length > 0)
  772. {
  773. foreach (var no in flowNos)
  774. {
  775. var id = no;
  776. var child = new SceneFlowNode() { Parent = this }.CreateFlowById(id);
  777. child.InternalNo = no;
  778. Children.Add(child);
  779. }
  780. }
  781. }
  782. }
  783. public override string ToXmlString()
  784. {
  785. string str = "";
  786. str += $"<SceneFlowBlock id=\"{Id}\">\r\n";
  787. str += base.ToXmlString();
  788. str += $"<FlowBlockType>{BlockType}</FlowBlockType>\r\n";
  789. if (Children?.Count > 0)
  790. {
  791. //str += "<SceneFlows>\r\n";
  792. //foreach (var sceneFlow in Children)
  793. //{
  794. // str += sceneFlow.ToXmlString();
  795. //}
  796. //str += "</SceneFlows>\r\n";
  797. str += $"<SceneFlowNos>{Children.Select(a => $"{a.Id}|{a.InternalNo}").ToArray().ArrayToStr()}</SceneFlowNos>";
  798. }
  799. str += "</SceneFlowBlock>\r\n";
  800. return str;
  801. }
  802. #endregion override
  803. }
  804. /// <summary>
  805. /// 情景流
  806. /// </summary>
  807. public class SceneFlowNode : NodeBase
  808. {
  809. public SceneFlowNode()
  810. {
  811. NodeType = NodeType.SceneFlow;
  812. }
  813. public FlowNode RunningNode { get; set; }
  814. /// <summary>
  815. /// 当前轮次
  816. /// </summary>
  817. public SceneRoundNode SelfRound => GetSelfRound();
  818. /// <summary>
  819. /// 获取当前所在的轮次
  820. /// </summary>
  821. /// <returns></returns>
  822. public SceneRoundNode GetSelfRound()
  823. {
  824. return HasParent ? ((SceneFlowBlockNode)Parent).GetSelfRound() : null;
  825. }
  826. /// <summary>
  827. /// 根据flowId创建情景流
  828. /// </summary>
  829. /// <param name="flowId"></param>
  830. /// <returns></returns>
  831. public SceneFlowNode CreateFlowById(string flowId)
  832. {
  833. var xmlNode= PackageHelper.GetSceneFlowXmlFromFile(CurPackageInfo?.Id ?? "", flowId);
  834. if (xmlNode != null)
  835. {
  836. return (SceneFlowNode)CreateNodeByXmlNode(xmlNode);
  837. }
  838. return null;
  839. }
  840. /// <summary>
  841. /// 从运行数据恢复到运行态
  842. /// </summary>
  843. /// <param name="running"></param>
  844. public void ConvertFromRun(FlowRunningInfo running)
  845. {
  846. Variables = running.VariableTable;
  847. NodeState = GetNodeState(running);
  848. WaitPath = running.WaitPath;
  849. if (HasChild)
  850. {
  851. foreach (var nodeBase in Children)
  852. {
  853. var child = (FlowNode)nodeBase;
  854. var runChild = running.NodeInfo.FirstOrDefault(a => a.Path == child.NodePath);
  855. if (runChild != null)
  856. {
  857. child.ConvertFromRun(runChild);
  858. }
  859. }
  860. }
  861. }
  862. #region override
  863. public override void SetPreVariables()
  864. {
  865. SetVariable(DefaultVariable.FullFlowScore, NodeFullScore);
  866. SetVariable(DefaultVariable.FlowScore, ActualScore);
  867. base.SetPreVariables();
  868. }
  869. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  870. {
  871. var childNodes = xmlNode?.GetChildNode("FlowNodes")?.Nodes;
  872. if (childNodes != null && childNodes.Count > 0)
  873. {
  874. foreach (IwbXmlNode childNode in childNodes)
  875. {
  876. var node = (FlowNode)new FlowNode() { Parent = this }.CreateNodeByXmlNode(childNode);
  877. Children.Add(node);
  878. }
  879. }
  880. }
  881. public override string ToXmlString()
  882. {
  883. string str = "";
  884. str += $"<SceneFlow id=\"{Id}\">\r\n";
  885. str += base.ToXmlString();
  886. if (Children?.Count > 0)
  887. {
  888. str += "<FlowNodes>\r\n";
  889. foreach (var node in Children)
  890. {
  891. str += node.ToXmlString();
  892. }
  893. str += "</FlowNodes>\r\n";
  894. }
  895. str += "</SceneFlow>\r\n";
  896. return str;
  897. }
  898. #endregion override
  899. }
  900. /// <summary>
  901. /// 情景流节点
  902. /// </summary>
  903. public class FlowNode:NodeBase
  904. {
  905. public FlowNode()
  906. {
  907. NodeType = NodeType.FlowNode;
  908. SceneInfos= new List<SceneNode>();
  909. }
  910. /// <summary>
  911. /// 节点深度
  912. /// </summary>
  913. public int FlowDepth { get; set; }
  914. /// <summary>
  915. /// 是否是留根节点
  916. /// </summary>
  917. public bool IsFlowRoot { get; set; }
  918. public string SceneNos { get; set; }
  919. public string SceneNames { get; set; }
  920. /// <summary>
  921. /// 当前轮次
  922. /// </summary>
  923. public SceneRoundNode SelfRound => SelfFlow?.SelfRound;
  924. /// <summary>
  925. /// 当前情景流
  926. /// </summary>
  927. public SceneFlowNode SelfFlow => GetSelfFlow();
  928. /// <summary>
  929. /// 情景集合
  930. /// </summary>
  931. public List<SceneNode> SceneInfos { get; set; }
  932. /// <summary>
  933. /// 情景总权重
  934. /// </summary>
  935. public decimal SceneFullWeights => SceneInfos?.Where(a=>a.ChildFullWeights>0).Sum(a => a.Weights) ?? 0;
  936. /// <summary>
  937. /// 获取当前所在的情景流
  938. /// </summary>
  939. /// <returns></returns>
  940. public SceneFlowNode GetSelfFlow()
  941. {
  942. return Parent.NodeType != NodeType.SceneFlow ? ((FlowNode) Parent).GetSelfFlow() : (SceneFlowNode) Parent;
  943. }
  944. /// <summary>
  945. /// 从运行数据恢复到运行态
  946. /// </summary>
  947. /// <param name="running"></param>
  948. public void ConvertFromRun(FlowNodeRunningInfo running)
  949. {
  950. Variables = running.VariableTable;
  951. NodeState = GetNodeState(running);
  952. WaitPath = running.WaitPath;
  953. if (SceneInfos != null && SceneInfos.Any())
  954. {
  955. foreach (var sceneInfo in SceneInfos)
  956. {
  957. var runChild = running.SceneInfo.FirstOrDefault(a => a.Path == sceneInfo.NodePath);
  958. if (runChild != null)
  959. {
  960. sceneInfo.ConvertFromRun(runChild);
  961. }
  962. }
  963. }
  964. if (NodeState == NodeState.Running)
  965. {
  966. SelfFlow.RunningNode = this;
  967. }
  968. else if (HasChild)
  969. {
  970. foreach (var nodeBase in Children)
  971. {
  972. var child = (FlowNode)nodeBase;
  973. var runChild = running.NodeInfo.FirstOrDefault(a => a.Path == child.NodePath);
  974. if (runChild != null)
  975. {
  976. child.ConvertFromRun(runChild);
  977. }
  978. }
  979. }
  980. }
  981. #region override
  982. public override void SetPreVariables()
  983. {
  984. SetVariable(DefaultVariable.FlowNodeScore, ActualScore);
  985. SetVariable(DefaultVariable.FullFlowScore, SelfFlow.NodeFullScore);
  986. SetVariable(DefaultVariable.PrevNodeScore, SelfFlow.ActualScore);
  987. base.SetPreVariables();
  988. }
  989. public override void SetRunnedVariables()
  990. {
  991. SetVariable(DefaultVariable.FlowNodeScore, ActualScore);
  992. Parent.SetVariable(DefaultVariable.FlowScore, SelfFlow.ActualScore);
  993. Parent.SetVariable($"{DefaultVariable.FlowScore}{FlowDepth}", SelfFlow.ActualScore);
  994. }
  995. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  996. {
  997. IsFlowRoot = Parent.NodeType == NodeType.FlowNode;
  998. if (xmlNode != null)
  999. {
  1000. FlowDepth = xmlNode.GetChildValue("").ValI();
  1001. SceneNos = xmlNode.GetChildValue("SceneInfoNos");
  1002. var sceneNos = SceneNos?.StrToArray();
  1003. if (sceneNos != null && sceneNos.Length > 0)
  1004. {
  1005. foreach (var no in sceneNos)
  1006. {
  1007. var nos = no.StrToArray("|");
  1008. var id = nos[0];
  1009. var child = new SceneNode() { Parent = this }.CreateSceneById(id);
  1010. if (nos.Length >1)
  1011. {
  1012. child.InternalNo = nos[1];
  1013. }
  1014. SceneNames = (SceneNames.IsEmpty() ? "" : ",") + child.Name;
  1015. SceneInfos.Add(child);
  1016. }
  1017. }
  1018. var childNodes = xmlNode.GetChildNode("ChildNodes")?.Nodes;
  1019. if (childNodes != null && childNodes.Count > 0)
  1020. {
  1021. foreach (IwbXmlNode childNode in childNodes)
  1022. {
  1023. var node = (FlowNode)new FlowNode() { Parent = this }.CreateNodeByXmlNode(childNode);
  1024. Children.Add(node);
  1025. }
  1026. }
  1027. }
  1028. }
  1029. public override string ToXmlString()
  1030. {
  1031. string str = "";
  1032. str += $"<FlowNode id=\"{Id}\">\r\n";
  1033. str += base.ToXmlString();
  1034. str += $"<FlowDepth>{FlowDepth}</FlowDepth>\r\n";
  1035. if (Children?.Count > 0)
  1036. {
  1037. str += "<ChildNodes>";
  1038. foreach (var node in Children)
  1039. {
  1040. str += node.ToXmlString();
  1041. }
  1042. str += "</ChildNodes>\r\n";
  1043. }
  1044. if (SceneInfos?.Count > 0)
  1045. {
  1046. str += $"<SceneInfoNos>{SceneInfos.Select(a => $"{a.Id}|{a.InternalNo}").ToArray().ArrayToStr()}</SceneInfoNos>";
  1047. str += $"<SceneInfoNames>{SceneInfos.Select(a => $"{a.Name}").ToArray().ArrayToStr()}</SceneInfoNames>";
  1048. }
  1049. else
  1050. {
  1051. str += $"<SceneInfoNos>{SceneNos}</SceneInfoNos>";
  1052. str += $"<SceneInfoNames>{SceneNames}</SceneInfoNames>";
  1053. }
  1054. str += "</FlowNode>\r\n";
  1055. return str;
  1056. }
  1057. #endregion override
  1058. }
  1059. /// <summary>
  1060. /// 情景信息
  1061. /// </summary>
  1062. public class SceneNode : NodeBase
  1063. {
  1064. public SceneNode()
  1065. {
  1066. NodeType = NodeType.SceneInfo;
  1067. }
  1068. public SceneType SceneType { get; set; }
  1069. /// <summary>
  1070. /// 当前轮次
  1071. /// </summary>
  1072. public SceneRoundNode SelfRound => SelfFlow?.SelfRound;
  1073. /// <summary>
  1074. /// 情景标签
  1075. /// </summary>
  1076. public string SceneTag { get; set; }
  1077. private string _desc;
  1078. /// <summary>
  1079. /// 情景信息
  1080. /// </summary>
  1081. public string Description
  1082. {
  1083. get => _desc?.IndexOf(IwbVariableType.Local, StringComparison.Ordinal) >= 0 ? EvalExpr.TransVariable(_desc, GetVariables()) : _desc;
  1084. set => _desc = value;
  1085. }
  1086. /// <summary>
  1087. /// 当前情景流
  1088. /// </summary>
  1089. public SceneFlowNode SelfFlow => GetSelfFlow();
  1090. /// <summary>
  1091. /// 环境渲染码
  1092. /// </summary>
  1093. public List<EnvironmentRender> EnvironResources { get; set; }
  1094. /// <summary>
  1095. /// 触发关键负向行为(true 为0分)
  1096. /// </summary>
  1097. public bool ImportantNegative => CheckImportantNegative();
  1098. /// <summary>
  1099. /// 获取当前所在的情景流
  1100. /// </summary>
  1101. /// <returns></returns>
  1102. public SceneFlowNode GetSelfFlow()
  1103. {
  1104. return HasParent ? ((FlowNode)Parent).GetSelfFlow() : null;
  1105. }
  1106. /// <summary>
  1107. /// 检查是否触发关键性负向行为
  1108. /// </summary>
  1109. /// <returns></returns>
  1110. private bool CheckImportantNegative()
  1111. {
  1112. return HasChild && Children.Select(a=>(BehaviorNode)a).Any(a=>a.ImportantNegative);
  1113. }
  1114. /// <summary>
  1115. /// 从运行数据恢复到运行态
  1116. /// </summary>
  1117. /// <param name="running"></param>
  1118. public void ConvertFromRun(SceneRunningInfo running)
  1119. {
  1120. Variables = running.VariableTable;
  1121. NodeState = GetNodeState(running);
  1122. WaitPath = running.WaitPath;
  1123. if (HasChild)
  1124. {
  1125. foreach (var nodeBase in Children)
  1126. {
  1127. var child = (BehaviorNode)nodeBase;
  1128. var runChild = running.BehaviorInfo.FirstOrDefault(a => a.Path == child.NodePath);
  1129. if (runChild != null)
  1130. {
  1131. child.ConvertFromRun(runChild);
  1132. }
  1133. }
  1134. }
  1135. }
  1136. #region override
  1137. public override void SetRunnedVariables()
  1138. {
  1139. SetVariable(DefaultVariable.SceneScore, ActualScore);
  1140. base.SetRunnedVariables();
  1141. }
  1142. protected override bool CheckChildIsRunning()
  1143. {
  1144. return NodeState == NodeState.Running;
  1145. }
  1146. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  1147. {
  1148. if (xmlNode != null)
  1149. {
  1150. SceneType = xmlNode.GetChildValue("SceneType").GetEnumByName<SceneType>();
  1151. SceneTag = xmlNode.GetChildValue("SceneTag");
  1152. Description = xmlNode.GetChildValue("Description");
  1153. var behaviorNos = xmlNode.GetChildValue("BehaviorNos")?.StrToArray();
  1154. if (behaviorNos != null && behaviorNos.Length > 0)
  1155. {
  1156. foreach (var no in behaviorNos)
  1157. {
  1158. var nos = no.StrToArray("|");
  1159. var id = nos[0];
  1160. var child = new BehaviorNode() { Parent = this }.CreateBehaviorById(id);
  1161. if (nos.Length >= 2)
  1162. {
  1163. child.InternalNo = nos[1];
  1164. }
  1165. Children.Add(child);
  1166. }
  1167. }
  1168. var resourceNos = xmlNode.GetChildValue("EnvironResourceNos")?.StrToArray();
  1169. if (resourceNos != null && resourceNos.Length > 0)
  1170. {
  1171. foreach (var no in resourceNos)
  1172. {
  1173. var nos = no.StrToArray("|");
  1174. var id = nos[0];
  1175. var child = new EnvironmentRender().CreateEnvironmentById(CurPackageInfo?.Id ?? "", id);
  1176. EnvironResources = EnvironResources ?? new List<EnvironmentRender>();
  1177. EnvironResources.Add(child);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. public SceneNode CreateSceneById( string id)
  1183. {
  1184. var xmlNode = PackageHelper.GetSceneXmlFromFile(CurPackageInfo?.Id ?? "", id);
  1185. return (SceneNode) CreateNodeByXmlNode(xmlNode);
  1186. }
  1187. public override string ToXmlString()
  1188. {
  1189. string str = "";
  1190. str += $"<SceneInfo id=\"{Id}\">\r\n";
  1191. str += base.ToXmlString();
  1192. str += $"<SceneType>{SceneType}</SceneType>\r\n";
  1193. str += SceneTag.IsEmpty() ? "" : $"<SceneTag>{SceneTag}</SceneTag>\r\n";
  1194. str += $"<ImportantNegative>{ImportantNegative}</ImportantNegative>\r\n";
  1195. str += $"<Description>{Description}</Description>\r\n";
  1196. if (EnvironResources?.Count > 0)
  1197. {
  1198. str += "<EnvironResourceNos>";
  1199. str += string.Join(",", EnvironResources.Select(a => Id).ToArray());
  1200. //foreach (var render in EnvironResources)
  1201. //{
  1202. // str += render.ToXmlString();
  1203. //}
  1204. str += "</EnvironResourceNos>\r\n";
  1205. }
  1206. if (Children?.Count > 0)
  1207. {
  1208. //str += "<Behaviors>";
  1209. //foreach (var behaviorNode in Children)
  1210. //{
  1211. // str += behaviorNode.ToXmlString();
  1212. //}
  1213. //str += "</Behaviors>\r\n";
  1214. str += $"<BehaviorNos>{Children.Select(a => $"{a.Id}|{a.InternalNo}").ToArray().ArrayToStr()}</BehaviorNos>";
  1215. }
  1216. str += "</SceneInfo>\r\n";
  1217. return str;
  1218. }
  1219. #endregion override
  1220. }
  1221. /// <summary>
  1222. /// 行为节点
  1223. /// </summary>
  1224. public class BehaviorNode : NodeBase
  1225. {
  1226. public BehaviorNode()
  1227. {
  1228. NodeType = NodeType.Behavior;
  1229. }
  1230. public BehaviorNode(string word):this()
  1231. {
  1232. KeyWords = word;
  1233. CreateKeyWord();
  1234. }
  1235. ///// <summary>
  1236. ///// 行为类型
  1237. ///// </summary>
  1238. //public BehaviorType BehaviorType { get; set; }
  1239. /// <summary>
  1240. /// 行为得分类型
  1241. /// </summary>
  1242. public BehaviorScoreType BehaviorScoreType { get; set; }
  1243. /// <summary>
  1244. /// 关键字字符串
  1245. /// </summary>
  1246. public string KeyWords { get; set; }
  1247. /// <summary>
  1248. /// 行为规则
  1249. /// </summary>
  1250. public string Description { get; set; }
  1251. /// <summary>
  1252. /// 行为标签
  1253. /// </summary>
  1254. public string BehaviorTag { get; set; }
  1255. /// <summary>
  1256. /// 行为标签
  1257. /// </summary>
  1258. public string[] BehaviorTagArray => BehaviorTag.StrToArray();
  1259. /// <summary>
  1260. /// 行为角色
  1261. /// </summary>
  1262. public string[] BehaviorRole { get; set; }
  1263. /// <summary>
  1264. /// 行为角色关系
  1265. /// </summary>
  1266. public BehaviorRoleLogicType BehaviorRoleLogic { get; set; }
  1267. /// <summary>
  1268. /// 触发关键负向行为(true 情景为0分)
  1269. /// </summary>
  1270. public bool ImportantNegative => CheckImportantNegative();
  1271. /// <summary>
  1272. /// 环境渲染码
  1273. /// </summary>
  1274. public List<EnvironmentRender> EnvironResources { get; set; }
  1275. /// <summary>
  1276. /// 检查是否触发关键性负向行为
  1277. /// </summary>
  1278. /// <returns></returns>
  1279. private bool CheckImportantNegative()
  1280. {
  1281. return HasChild && BehaviorScoreType==BehaviorScoreType.ImportantNegative && Children.Select(a=>(KeyWord)a).Any(a=>a.HasMatched);
  1282. }
  1283. /// <summary>
  1284. /// 从运行数据恢复到运行态
  1285. /// </summary>
  1286. /// <param name="running"></param>
  1287. public void ConvertFromRun(BehaviorRunningInfo running)
  1288. {
  1289. NodeState = GetNodeState(running);
  1290. WaitPath = running.WaitPath;
  1291. KeyWords = running.KeyWords;
  1292. CreateKeyWord();
  1293. }
  1294. #region override
  1295. protected override bool CheckChildIsRunning()
  1296. {
  1297. return false;
  1298. }
  1299. protected override void CreateSelfNode(IwbXmlNode xmlNode)
  1300. {
  1301. if (xmlNode != null)
  1302. {
  1303. BehaviorScoreType = xmlNode.GetChildValue("ScoreType").GetEnumByName<BehaviorScoreType>();
  1304. BehaviorRoleLogic = xmlNode.GetChildValue("RoleLogic").GetEnumByName<BehaviorRoleLogicType>();
  1305. BehaviorTag = xmlNode.GetChildValue("BehaviorTag");
  1306. Description = xmlNode.GetChildValue("Description");
  1307. KeyWords = xmlNode.GetChildValue("KeyWord");
  1308. CreateKeyWord();
  1309. BehaviorRole = xmlNode.GetChildValue("Role")?.StrToArray() ?? new string[0];
  1310. var resourceNos = xmlNode.GetChildValue("EnvironResourceNos")?.StrToArray();
  1311. if (resourceNos != null && resourceNos.Length > 0)
  1312. {
  1313. foreach (var no in resourceNos)
  1314. {
  1315. var nos = no.StrToArray("|");
  1316. var id = nos[0];
  1317. var child = new EnvironmentRender().CreateEnvironmentById(CurPackageInfo?.Id ?? "", id);
  1318. EnvironResources = EnvironResources ?? new List<EnvironmentRender>();
  1319. EnvironResources.Add(child);
  1320. }
  1321. }
  1322. }
  1323. }
  1324. public BehaviorNode CreateBehaviorById(string id)
  1325. {
  1326. var xmlNode = PackageHelper.GetBehaviorXmlFromFile(CurPackageInfo?.Id ?? "", id);
  1327. return (BehaviorNode)CreateNodeByXmlNode(xmlNode);
  1328. }
  1329. private void CreateKeyWord()
  1330. {
  1331. string wordStr = KeyWords;
  1332. if (KeyWords.StartsWith("[") && KeyWords.EndsWith("]"))
  1333. {
  1334. wordStr = KeyWords.StrToArray()[0];
  1335. }
  1336. var wordArray = wordStr?.StrToArray();
  1337. if (wordArray != null && wordArray.Length > 0)
  1338. {
  1339. Children= new List<NodeBase>();
  1340. foreach (string word in wordArray)
  1341. {
  1342. var kc = new KeyWord(word, this);
  1343. Children.Add(kc);
  1344. }
  1345. }
  1346. }
  1347. public string GetKeyWords()
  1348. {
  1349. var str = "";
  1350. if (HasChild)
  1351. {
  1352. foreach (var keyWord in Children)
  1353. {
  1354. str += (str == "" ? "" : ",") + keyWord.ToXmlString();
  1355. }
  1356. }
  1357. return str;
  1358. }
  1359. public override string ToXmlString()
  1360. {
  1361. string str = "";
  1362. str += $"<Behavior id=\"{Id}\">\r\n";
  1363. str += base.ToXmlString();
  1364. //str += $"<BehaviorType>{BehaviorType}</BehaviorType>\r\n";
  1365. str += $"<ScoreType>{BehaviorScoreType}</ScoreType>\r\n";
  1366. str += BehaviorRole?.Length > 0 ? $"<Role>{BehaviorRole.ArrayToStr()}</Role>\r\n" : "";
  1367. str += $"<RoleLogic>{BehaviorRoleLogic}</RoleLogic>\r\n";
  1368. str += $"<BehaviorTag>{BehaviorTag}</BehaviorTag>\r\n";
  1369. str += $"<Description>{Description}</Description>\r\n";
  1370. if (Children?.Count > 0)
  1371. {
  1372. string str1 = "",str2="";
  1373. foreach (var keyWord in Children)
  1374. {
  1375. str1 += (str1 == "" ? "" : ",") + keyWord.ToXmlString();
  1376. str2 += ((KeyWord)keyWord).MatchWordXmlString();
  1377. }
  1378. str += $"<KeyWord>[{str1}]</KeyWord>\r\n";
  1379. if (str2.IsNotEmpty())
  1380. {
  1381. str += "<MatchWords>";
  1382. str += str2;
  1383. str += "</MatchWords>";
  1384. }
  1385. }
  1386. else
  1387. {
  1388. str += $"<KeyWord>{KeyWords}</KeyWord>\r\n";
  1389. }
  1390. str += "</Behavior>\r\n";
  1391. return str;
  1392. }
  1393. #endregion override
  1394. }
  1395. /// <summary>
  1396. /// 关键字分组
  1397. /// </summary>
  1398. public sealed class KeyWord : NodeBase
  1399. {
  1400. public KeyWord()
  1401. {
  1402. NodeType = NodeType.KeyWord;
  1403. }
  1404. /// <summary>
  1405. /// [aa,bb,ccc]:8:0【关键字:比重:是否匹配】
  1406. /// </summary>
  1407. /// <param name="words"></param>
  1408. public KeyWord(string words) : this()
  1409. {
  1410. Word = words;
  1411. string w = words;
  1412. if (words.StartsWith("[") && words.EndsWith("]"))
  1413. {
  1414. w = words.StrToArray()[0];
  1415. }
  1416. var arr = w.StrToArray(":");
  1417. if (arr.Length == 2)
  1418. {
  1419. Words = arr[0].StrToArray();
  1420. Weights = arr[1].ValD();
  1421. HasMatched = false;
  1422. }
  1423. else if (arr.Length == 3)
  1424. {
  1425. Words = arr[0].StrToArray();
  1426. Weights = arr[1].ValD();
  1427. HasMatched = arr[2].ValB();
  1428. }
  1429. }
  1430. public KeyWord(string words, BehaviorNode parent) : this(words)
  1431. {
  1432. Parent = parent;
  1433. }
  1434. /// <summary>
  1435. /// 行为角色
  1436. /// </summary>
  1437. public string[] Roles => HasParent ? ((BehaviorNode) Parent).BehaviorRole : "".StrToArray();
  1438. /// <summary>
  1439. /// 匹配此关键字的内容
  1440. /// </summary>
  1441. public string MatchWord { get; set; }
  1442. /// <summary>
  1443. /// 是否匹配
  1444. /// </summary>
  1445. public bool HasMatched { get; set; }
  1446. /// <summary>
  1447. /// 关键字信息
  1448. /// </summary>
  1449. public string Word { get; set; }
  1450. /// <summary>
  1451. /// 关键字数组
  1452. /// </summary>
  1453. public string[] Words { get; set; }
  1454. #region override
  1455. protected override bool CheckChildIsRunning()
  1456. {
  1457. return false;
  1458. }
  1459. public override string ToString()
  1460. {
  1461. return $"[{GetType().Name}]【{Word}】";
  1462. }
  1463. public override string ToXmlString()
  1464. {
  1465. string str = Words?.Length > 0 ? $"[{Words.ArrayToStr()}]:{Weights}:{(HasMatched ? "1" : "0")}" : "";
  1466. return str;
  1467. }
  1468. public string MatchWordXmlString()
  1469. {
  1470. string str = MatchWord.IsEmpty()? "":$"<MatchWord>{ToXmlString()}|{MatchWord}</MatchWord>\r\n";
  1471. return str;
  1472. }
  1473. #endregion override
  1474. }
  1475. /// <summary>
  1476. /// 环境渲染
  1477. /// </summary>
  1478. public class EnvironmentRender
  1479. {
  1480. public string Id { get; set; }
  1481. public string NodePath { get; set; }
  1482. public string ResourceName { get; set; }
  1483. public int ResourceType { get; set; }
  1484. public string ResourcePath { get; set; }
  1485. public string Description { get; set; }
  1486. public string MessageCode { get; set; }
  1487. public EnvironmentRender CreateEnvironmentById(string packageId,string id)
  1488. {
  1489. var xmlNode = PackageHelper.GetEnvironResourceXmlFromFile(packageId, id);
  1490. return CreateNodeByXmlNode(xmlNode);
  1491. }
  1492. public EnvironmentRender CreateNodeByXmlNode(IwbXmlNode xmlNode)
  1493. {
  1494. if (xmlNode != null )
  1495. {
  1496. Id = xmlNode.GetChildValue("Id");
  1497. ResourceName = xmlNode.GetChildValue("ResourceName");
  1498. ResourceType = xmlNode.GetChildValue("ResourceType").ValI();
  1499. ResourcePath = xmlNode.GetChildValue("ResourcePath");
  1500. MessageCode = xmlNode.GetChildValue("MessageCode");
  1501. Description = xmlNode.GetChildValue("Description");
  1502. }
  1503. return this;
  1504. }
  1505. public string ToXmlString()
  1506. {
  1507. string str = "";
  1508. str += $"<Environment id=\"{Id}\">\r\n";
  1509. str += $"<ResourceName>{ResourceName}</ResourceName>\r\n";
  1510. str += $"<ResourceType>{ResourceType}</ResourceType>\r\n";
  1511. str += ResourcePath.IsEmpty() ? "" : $"<ResourcePath>{ResourcePath}</ResourcePath>\r\n";
  1512. str += MessageCode.IsEmpty() ? "" : $"<MessageCode>{MessageCode}</MessageCode>\r\n";
  1513. str += Description.IsEmpty() ? "" : $"<Description>{Description}</Description>\r\n";
  1514. str += $"</Environment>\r\n";
  1515. return str;
  1516. }
  1517. }
  1518. /// <summary>
  1519. /// 引导信息
  1520. /// </summary>
  1521. public class GuideNode
  1522. {
  1523. public string Id { get; set; }
  1524. public string Name { get; set; }
  1525. public string Description { get; set; }
  1526. public bool IsPush { get; set; }
  1527. public int GuideType { get; set; }
  1528. public GuideNode CreateNodeByXmlNode(IwbXmlNode xmlNode)
  1529. {
  1530. if (xmlNode != null)
  1531. {
  1532. Id = xmlNode.GetChildValue("Id");
  1533. Name = xmlNode.GetChildValue("Name");
  1534. Description = xmlNode.GetChildValue("Description");
  1535. GuideType = xmlNode.GetChildValue("GuideType").ValI();
  1536. IsPush = xmlNode.GetChildValue("IsPush").ValB();
  1537. }
  1538. return this;
  1539. }
  1540. public string ToXmlString()
  1541. {
  1542. string str = "";
  1543. str += $"<GuideInfo id=\"{Id}\">\r\n";
  1544. str += $"<Name>{Name}</Name>\r\n";
  1545. str += $"<Desc>{Description}</Desc>\r\n";
  1546. str += $"<IsPush>{IsPush}</IsPush>\r\n";
  1547. str += "</GuideInfo>\r\n";
  1548. return str;
  1549. }
  1550. }
  1551. }