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); } ///// ///// 产生随机运行Id ///// ///// //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; } /// /// 运行方案包 /// /// /// /// public override bool Run(NodeBase node, OperationType operationType) { CurrentNode = node; return Run(operationType); } /// /// 运行 /// /// 匹配关键字时('path|word|role') /// /// 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); } /// /// 运行 /// /// /// private bool Run(OperationType operationType) { if (CurrentNode != null) { return RunNode(CurrentNode, operationType); } return false; } /// /// 运行节点 /// /// /// /// /// 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); } } /// /// 运行节点 /// /// /// /// 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; } /// /// 前置验证 /// /// /// 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; } /// /// 通知下一节点运行 /// /// 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; } } /// /// 通知子节点运行 /// /// private static void SelectChildren2Run(NodeBase node) { foreach (var child in node.Children) { child.NodeState = NodeState.Running; } } /// /// 选取演化情景流块(带权重) /// /// 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(); 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--; } } } /// /// 演化情景流块选取情景流 /// /// /// private static string SelectRandomFlows(ref List flows) { var random = new Random().Next(0, flows.Count - 1); var flow = flows[random]; flows.Remove(flow); return flow.Id; } /// /// 运行时事件 /// /// /// 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; } /// /// 运行结束时事件 /// /// /// 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; } /// /// 通知父节点结束 /// /// 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; } } /// /// 通知父节点结束 /// /// private static void SelectParent2Stop(NodeBase node) { if (node.HasParent && !node.Parent.ChildIsRunning) { node.Parent.NodeState = NodeState.RunEnd; } } /// /// 后置事件 /// /// /// 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; } /// /// 通知待运行节点 /// /// 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; } } } } } /// /// 关键字匹配 /// /// /// private static bool RunnerMatchKey(NodeBase node) { //string word = node.ExtendData?.BehaviorWord ?? ""; //string role = node.ExtendData?.BehaviorRole ?? ""; node.CurPackageInfo.ScoreEvalModule.MatchKeyWord(node); return true; } /// /// 关键字匹配成功 /// /// /// 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; } /// /// 下一节点 /// /// 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; } } /// /// 下一轮次 /// /// /// private static bool NextRound(NodeBase node) { if (node.CurPackageInfo.CurRoundInfo.NodeState != NodeState.Complete) { node.CurPackageInfo.CurRoundInfo.NodeState = NodeState.Complete; return true; } return false; } /// /// 下一情景流 /// /// /// 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; } /// /// 下一情景流(当前运行的所有情景流) /// /// /// 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; } /// /// 特殊表达式转换(添加上RunningId和节点Id) /// /// /// 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; //} } }