| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml;
- using IwbZero.ToolCommon.StringModel;
- using WeEngine.Enum;
- using WeEngine.Functions;
- using WeEngine.Packages;
- namespace WeEngine.Module.Runner
- {
- public sealed class EvolutionRunnerModule: PackageRunnerModuleBase
- {
-
- public EvolutionRunnerModule(string runningId)
- {
- try
- {
- PackageInfo = PackageCache.Instance.GetCache(runningId);
- }
- catch (Exception e)
- {
- Message = e.Message;
- }
- }
- public EvolutionRunnerModule(XmlNode xmlNode)
- {
- PackageInfo = (PackageInfo) new PackageInfo().CreateNodeByXmlNode(xmlNode);
- //PackageInfo.RunningId = PackageInfo.RunningId.IsEmpty() ? GenerateRunningId() : PackageInfo.RunningId;
- //PackageCache.Instance.SetCache(PackageInfo);
- }
- public EvolutionRunnerModule(PackageInfo packageInfo)
- {
- PackageInfo = packageInfo;
- //PackageInfo.RunningId = PackageInfo.RunningId.IsEmpty() ? GenerateRunningId() : PackageInfo.RunningId;
- //PackageCache.Instance.SetCache(PackageInfo);
- }
- ///// <summary>
- ///// 产生随机运行Id
- ///// </summary>
- ///// <returns></returns>
- //private string GenerateRunningId()
- //{
- // return $"{DateTime.Now:yyyyMMddHHmmss}{new Random(1000).Next(1000, 9999)}";
- //}
- public string Message { get; set; }
- public override PackageInfo PackageInfo { get; set; }
- public NodeBase CurrentNode { get; set; }
- private NodeBase GetNodeByPath(string path)
- {
- var node = PackageInfo.GetNodeByPath(path);
- return node;
- }
- /// <summary>
- /// 运行方案包
- /// </summary>
- /// <param name="node"></param>
- /// <param name="operationType"></param>
- /// <returns></returns>
- public override bool Run(NodeBase node, OperationType operationType)
- {
- CurrentNode = node;
- return Run(operationType);
- }
- /// <summary>
- /// 运行
- /// </summary>
- /// <param name="path">匹配关键字时('path|word|role')</param>
- /// <param name="operationType"></param>
- /// <returns></returns>
- public override bool Run(string path, OperationType operationType)
- {
- if (operationType == OperationType.MatchKey)
- {
- string[] arr = path.Split('|');
- if (arr.Length == 3)
- {
- CurrentNode = GetNodeByPath(arr[0]);
- CurrentNode.ExtendData = new { BehaviorWord = arr[1], BehaviorRole = arr[2] };
- //CurrentNode.ExtendData.BehaviorWord = arr[1];
- //CurrentNode.ExtendData.BehaviorRole = arr[2];
- return Run(OperationType.MatchKey);
- }
- return false;
- }
- CurrentNode = GetNodeByPath(path);
- return Run(operationType);
- }
- /// <summary>
- /// 运行
- /// </summary>
- /// <param name="operationType"></param>
- /// <returns></returns>
- private bool Run(OperationType operationType)
- {
- if (CurrentNode != null)
- {
- return RunNode(CurrentNode, operationType);
- }
- return false;
- }
- /// <summary>
- /// 运行节点
- /// </summary>
- /// <param name="curPackageInfo"></param>
- /// <param name="id"></param>
- /// <param name="operationType"></param>
- /// <returns></returns>
- public static bool RunNode(PackageInfo curPackageInfo, string id, OperationType operationType)
- {
- if (operationType == OperationType.MatchKey)
- {
- string[] arr = id.Split('|');
- if (arr.Length == 3)
- {
- var curNode = curPackageInfo.GetNodeByPath(arr[0]);
- curNode.ExtendData = new { BehaviorWord= arr[1], BehaviorRole= arr[2] };
- //curNode.ExtendData.BehaviorWord = arr[1];
- //curNode.ExtendData.BehaviorRole = arr[2];
- return RunNode(curNode, OperationType.MatchKey);
- }
- return false;
- }
- {
- var curNode = curPackageInfo.GetNodeByPath(id);
- return RunNode(curNode, operationType);
- }
- }
-
- /// <summary>
- /// 运行节点
- /// </summary>
- /// <param name="node"></param>
- /// <param name="operationType"></param>
- /// <returns></returns>
- private static bool RunNode(NodeBase node, OperationType operationType)
- {
- switch (operationType)
- {
- case OperationType.Start:
- return Start(node);
- case OperationType.Stop:
- return Stop(node);
- case OperationType.NextRound:
- return NextRound(node);
- case OperationType.NextFlowNodes:
- return NextFlowNodes(node);
- case OperationType.NextFlowNode:
- return NextFlowNode(node);
- case OperationType.PreEvent:
- return RunnerPre(node);
- case OperationType.RunningEvent:
- return RunnerRunning(node);
- case OperationType.RunnedEvent:
- return RunnerRunned(node);
- case OperationType.PostEvent:
- return RunnerPost(node);
- case OperationType.MatchKey:
- return RunnerMatchKey(node);
- case OperationType.MatchSuccess:
- return RunnerMatchSuccess(node);
- }
- return false;
- }
- private static bool Start(NodeBase node)
- {
- if (node.NodeState == NodeState.Running)
- {
- return false;
- }
- node.NodeState = NodeState.Running;
- return true;
- }
- private static bool Stop(NodeBase node)
- {
- if (node.NodeState == NodeState.Running)
- {
- node.NodeState = NodeState.RunEnd;
- }
- return true;
- }
- /// <summary>
- /// 前置验证
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool RunnerPre(NodeBase node)
- {
- node.SetPreVariables();
- if (node.PreConditionExpr.IsNotEmpty())
- {
- var expr = ConvertExpr(node.PreConditionExpr);
- var vars = node.GetVariables();
- node.PreConditionValidated = ExprModule
- .Evaluate($"RunCheckCondition('@RunningId','@Id',{expr})", vars).ValB();
- }
- if (node.PreConditionValidated)
- {
- //if (node.IsWait)
- //{
- // node.PreConditionValidated = false;
- // return;
- //}
- SelectNextRun(node);
- }
- return true;
- }
- /// <summary>
- /// 通知下一节点运行
- /// </summary>
- /// <param name="node"></param>
- private static void SelectNextRun(NodeBase node)
- {
- switch (node.NodeType)
- {
- case NodeType.ScenePackage:
- {
- var n = ((PackageInfo)node);
- var roundIndex = n.CurrentRound + 1;
- var roundInfo = n.Children.Select(a => (SceneRound)a).FirstOrDefault(a => a.RoundIndex == roundIndex);
- if (roundInfo != null)
- {
- roundInfo.NodeState = NodeState.Running;
- }
- break;
- }
- case NodeType.SceneRound:
- {
- SelectChildren2Run(node);
- break;
- }
- case NodeType.SceneFlowBlock:
- {
- var n = ((SceneFlowBlock)node);
- if (n.BlockType == SceneFlowBlockType.Objective)
- {
- SelectChildren2Run(node);
- }
- else if (n.BlockType == SceneFlowBlockType.Evolutionary &&
- !((SceneRound)n.Parent).EvolutionBlockHasSelected)
- {
- ((SceneRound)n.Parent).EvolutionBlockHasSelected = true;
- }
- else
- {
- n.PreConditionValidated = false;
- SelectRandomFlowBlock(node);
- }
- break;
- }
- case NodeType.SceneFlow:
- {
- var n = ((SceneFlow)node);
- var runChild = n.RunningNode;
- if (runChild == null)
- {
- runChild = (FlowNode)n.Children.FirstOrDefault();
- if (runChild != null) runChild.NodeState = NodeState.Running;
- }
- else if (runChild.HasChild)
- {
- foreach (var child in runChild.Children)
- {
- child.NodeState = NodeState.Running;
- }
- }
- break;
- }
- case NodeType.FlowNode:
- {
- var n = ((FlowNode)node);
- n.SelfFlow.RunningNode = n;
- foreach (var sceneInfo in n.SceneInfos)
- {
- sceneInfo.NodeState = NodeState.Running;
- }
- break;
- }
- case NodeType.SceneInfo:
- {
- SelectChildren2Run(node);
- break;
- }
- default:
- SelectChildren2Run(node);
- break;
- }
- }
- /// <summary>
- /// 通知子节点运行
- /// </summary>
- /// <param name="node"></param>
- private static void SelectChildren2Run(NodeBase node)
- {
- foreach (var child in node.Children)
- {
- child.NodeState = NodeState.Running;
- }
- }
- /// <summary>
- /// 选取演化情景流块(带权重)
- /// </summary>
- /// <param name="node"></param>
- private static void SelectRandomFlowBlock(NodeBase node)
- {
- var curRoundInfo = node.CurPackageInfo.CurRoundInfo;
- var list = curRoundInfo.Children.Select(a => (SceneFlowBlock)a).Where(a => a.BlockType == SceneFlowBlockType.Evolutionary).ToList();
- SceneFlowBlock selectBlock = list.FirstOrDefault();
- if (list.Count > 1)
- {
- //生成随机值,判断是否在权重范围内
- var randomResult = ExprModule.Evaluate($"IFunRandomSelection({curRoundInfo.ControlRate})");
- if (randomResult.ValB())
- {
- selectBlock =
- list.FirstOrDefault(a => a.NodeState == NodeState.Running);
- }
- else
- {
- var noRunBlock = list.Where(a => a.NodeState == NodeState.New).ToList();
- if (noRunBlock.Any())
- {
- var random = new Random().Next(0, noRunBlock.Count - 1);
- selectBlock = noRunBlock[random];
- }
- }
- }
- //选取情景流
- if (selectBlock != null)
- {
- var sceneCount = curRoundInfo.SceneCount;
- var flows = new List<NodeBase>();
- flows.AddRange(selectBlock.Children);
- sceneCount = sceneCount > flows.Count ? flows.Count : sceneCount;
- while (sceneCount > 0)
- {
- var id = SelectRandomFlows(ref flows);
- var flow = selectBlock.Children.FirstOrDefault(a => a.Id == id);
- if (flow != null) flow.NodeState = NodeState.Running;
- sceneCount--;
- }
- }
- }
- /// <summary>
- /// 演化情景流块选取情景流
- /// </summary>
- /// <param name="flows"></param>
- /// <returns></returns>
- private static string SelectRandomFlows(ref List<NodeBase> flows)
- {
- var random = new Random().Next(0, flows.Count - 1);
- var flow = flows[random];
- flows.Remove(flow);
- return flow.Id;
- }
- /// <summary>
- /// 运行时事件
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool RunnerRunning(NodeBase node)
- {
- if (node.RunningConditionExpr.IsNotEmpty())
- {
- var expr = ConvertExpr(node.RunningConditionExpr);
- var vars = node.GetVariables();
- ExprModule.Evaluate(expr, vars);
- }
- var guideInfos = node.GuideInfos?.Where(a => a.IsPush).ToList();
- if (guideInfos != null && guideInfos.Any())
- {
- foreach (var guideInfo in guideInfos)
- {
- node.CurPackageInfo.MessageSender.SendMsg(guideInfo.Obj2String(),MessageType.GuideInfo);
- }
- }
- if (node.NodeType==NodeType.SceneInfo)
- {
- var scene = (SceneInfo) node;
- var pushSceneInfo = new PushSceneInfo(scene);
- node.CurPackageInfo.MessageSender.SendMsg(pushSceneInfo.Obj2String(), MessageType.SceneInfo);
- if(scene.EnvironmentRenders!=null&& scene.EnvironmentRenders.Any())
- {
- foreach (var render in scene.EnvironmentRenders)
- {
- node.CurPackageInfo.MessageSender.SendMsg(render.Obj2String(),MessageType.Environment);
- }
- }
- }
- return true;
- }
-
- /// <summary>
- /// 运行结束时事件
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool RunnerRunned(NodeBase node)
- {
- node.SetRunnedVariables();
- if (node.RunnedConditionExpr.IsNotEmpty())
- {
- var expr = ConvertExpr(node.RunnedConditionExpr);
- var vars = node.GetVariables();
- node.RunnedConditionValidated = ExprModule
- .Evaluate($"RunCheckCondition('@RunningId','@Id',{expr})", vars).ValB();
- }
- if (node.RunnedConditionValidated)
- {
- SelectPrevEnd(node);
- node.NodeState = NodeState.Complete;
- }
- return true;
- }
- /// <summary>
- /// 通知父节点结束
- /// </summary>
- /// <param name="node"></param>
- private static void SelectPrevEnd(NodeBase node)
- {
- switch (node.NodeType)
- {
- case NodeType.ScenePackage:
- {
- var n = ((PackageInfo)node);
- if (n.AutoNextRound)
- {
- }
- break;
- }
- case NodeType.SceneRound:
- {
- var n = ((SceneRound)node);
- if (n.HasParent && !n.Parent.ChildIsRunning)
- {
- var nextRound = n.Parent.Children.Select(a => (SceneRound)a)
- .FirstOrDefault(a => a.RoundIndex == n.RoundIndex + 1);
- if (nextRound == null)
- {
- n.Parent.NodeState = NodeState.RunEnd;
- }
- }
- break;
- }
- case NodeType.SceneFlowBlock:
- {
- SelectParent2Stop(node);
- break;
- }
- case NodeType.SceneFlow:
- SelectParent2Stop(node);
- break;
- case NodeType.FlowNode:
- SelectParent2Stop(node);
- break;
- case NodeType.SceneInfo:
- SelectParent2Stop(node);
- break;
- default:
- SelectParent2Stop(node);
- break;
- }
- }
- /// <summary>
- /// 通知父节点结束
- /// </summary>
- /// <param name="node"></param>
- private static void SelectParent2Stop(NodeBase node)
- {
- if (node.HasParent && !node.Parent.ChildIsRunning)
- {
- node.Parent.NodeState = NodeState.RunEnd;
- }
- }
- /// <summary>
- /// 后置事件
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool RunnerPost(NodeBase node)
- {
- if (node.PostConditionExpr.IsNotEmpty())
- {
- var expr = ConvertExpr(node.PostConditionExpr);
- var vars = node.GetVariables();
- ExprModule.Evaluate(expr, vars);
- }
- CallNodesToRun(node);
- NextNode(node);
- return true;
- }
- /// <summary>
- /// 通知待运行节点
- /// </summary>
- /// <param name="node"></param>
- private static void CallNodesToRun(NodeBase node)
- {
- var waitNodes = node.CurPackageInfo.WaitForRunNode;
- if (waitNodes.Count > 0 && waitNodes.ContainsKey(node.Id))
- {
- var toRunIds = waitNodes[node.Id];
- if (toRunIds != null && toRunIds.Any())
- {
- foreach (var nodeId in toRunIds)
- {
- var runNode = node.CurPackageInfo.GetNodeByPath(nodeId);
- if (runNode.Parent.Children.All(a => a.NodeState == NodeState.New))
- {
- runNode.NodeState = NodeState.Running;
- }
- }
- }
- }
- }
- /// <summary>
- /// 关键字匹配
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool RunnerMatchKey(NodeBase node)
- {
- //string word = node.ExtendData?.BehaviorWord ?? "";
- //string role = node.ExtendData?.BehaviorRole ?? "";
- node.CurPackageInfo.ScoreEvalModule.MatchKeyWord(node);
- return true;
- }
- /// <summary>
- /// 关键字匹配成功
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool RunnerMatchSuccess(NodeBase node)
- {
- if (node.NodeType != NodeType.KeyWord)
- {
- return false;
- }
- var n = (KeyWord) node;
- string word = node.ExtendData?.MatchWord ?? "";
- n.HasMatched = true;
- n.MatchWord = word;
- return true;
- }
- /// <summary>
- /// 下一节点
- /// </summary>
- /// <param name="node"></param>
- private static void NextNode(NodeBase node)
- {
- switch (node.NodeType)
- {
- case NodeType.SceneRound:
- {
- var np = ((PackageInfo)node.Parent);
- if (np.AutoNextRound)
- {
- SelectNextRun(np);
- }
- break;
- }
- case NodeType.FlowNode:
- {
- var n = ((FlowNode)node);
- if (n.HasChild&&n.Children.Count>0)
- {
- SelectNextRun(n.SelfFlow);
- }
-
- }
- break;
-
- }
- }
- /// <summary>
- /// 下一轮次
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool NextRound(NodeBase node)
- {
- if (node.CurPackageInfo.CurRoundInfo.NodeState != NodeState.Complete)
- {
- node.CurPackageInfo.CurRoundInfo.NodeState = NodeState.Complete;
- return true;
- }
- return false;
- }
- /// <summary>
- /// 下一情景流
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- public static bool NextFlowNode(NodeBase node)
- {
- FlowNode flowNode = null;
- if (node.NodeType == NodeType.SceneFlow)
- {
- flowNode = ((SceneFlow) node).RunningNode;
- //flowNode.NodeState = NodeState.Running;
- }
- else if (node.NodeType == NodeType.FlowNode)
- {
- flowNode = (FlowNode) node;
- }
- if (flowNode != null && flowNode.NodeState != NodeState.New)
- {
- if (flowNode.NodeState == NodeState.Running)
- {
- flowNode.NodeState = NodeState.RunEnd;
- }else if (flowNode.NodeState == NodeState.RunEnd)
- {
- flowNode.NodeState = NodeState.Complete;
- }
- else
- {
- return false;
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// 下一情景流(当前运行的所有情景流)
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private static bool NextFlowNodes(NodeBase node)
- {
- if (node.CurPackageInfo != null && node.CurPackageInfo.CurFlowNodes.Any())
- {
- foreach (var child in node.CurPackageInfo.CurFlowNodes)
- {
- if (child.NodeState == NodeState.New)
- {
- continue;
- }
- if (child.NodeState == NodeState.Running|| child.NodeState==NodeState.RunEnd)
- {
- child.NodeState = NodeState.Complete;
- }
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// 特殊表达式转换(添加上RunningId和节点Id)
- /// </summary>
- /// <param name="expr"></param>
- /// <returns></returns>
- private static string ConvertExpr(string expr)
- {
- if (expr.IndexOf("WithRp(", StringComparison.Ordinal) > 0)
- {
- expr = expr.Replace("WithRp(", "WithRp('@RunningId','@Id',");
- }
- return expr;
- }
- //private string C(string expr)
- //{
- // string str = "";
- // return str;
- //}
- }
- }
|