| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using IwbZero.IwbBase;
- using IwbZero.ToolCommon.LogHelpers;
- using IwbZero.ToolCommon.StringModel;
- using WeEngine.CommonDto;
- using WeEngine.Enum;
- namespace WeEngine.Packages
- {
- public abstract class NodeBase : IIwbId, IComparer<NodeBase>, ICloneable
- {
- #region 字段
- ///// <summary>
- ///// 实际权重得分
- ///// </summary>
- //private decimal _actualWeightScore;
- /// <summary>
- /// 节点状态
- /// </summary>
- private NodeState _nodeState;
- #endregion 字段
- #region 属性
- /// <summary>
- /// 父节点
- /// </summary>
- public NodeBase Parent { get; set; }
- /// <summary>
- /// 子节点
- /// </summary>
- public List<NodeBase> Children { get; set; }
- /// <summary>
- /// 唯一Id
- /// </summary>
- public string Id { get; set; }
- /// <summary>
- /// 方案包内部编号
- /// </summary>
- public string InternalNo { get; set; }
- /// <summary>
- /// 名称
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 节点类型
- /// </summary>
- public NodeType NodeType { get; set; }
- /// <summary>
- /// 扩展数据
- /// </summary>
- public dynamic ExtendData { get; set; }
- /// <summary>
- /// 节点状态
- /// </summary>
- public NodeState NodeState
- {
- get => _nodeState;
- set
- {
- if ( _nodeState != value)
- {
- var oldState = _nodeState;
- _nodeState = value;
- if (CurPackageInfo != null && !CurPackageInfo.EventSwitch)
- {
- return;
- }
- if (_nodeState != NodeState.New)
- {
- EvNodeStateChanged?.Invoke(this, oldState);
- }
- }
-
- }
- }
- /// <summary>
- /// 矫正评分
- /// </summary>
- public decimal CorrectionScore { get; set; }
- /// <summary>
- /// 权重(在父节点中的占比)
- /// </summary>
- public virtual decimal Weights { get; set; }
- /// <summary>
- /// 运行前事务(Id|Mapping)
- /// </summary>
- public string PreComponent { get; set; }
- /// <summary>
- /// 运行时事务(Id|Mapping)
- /// </summary>
- public string RunningComponent { get; set; }
- /// <summary>
- /// 运行结束时事务(Id|Mapping)
- /// </summary>
- public string RunnedComponent { get; set; }
- /// <summary>
- /// 运行后事务(Id|Mapping)
- /// </summary>
- public string PostComponent { get; set; }
- public List<GuideNode> GuideInfos { get; set; }
- public string GuideNos { get; set; }
- /// <summary>
- /// 实际节点评分
- /// </summary>
- public decimal ActualScore => GetActualScore();
- /// <summary>
- /// 节点满分
- /// </summary>
- public decimal NodeFullScore => GetNodeFullScore();
- /// <summary>
- /// 实际节点得分权重
- /// </summary>
- public decimal ActualWeights => GetActualWeights();
- /// <summary>
- /// 子权重和
- /// </summary>
- public decimal ChildFullWeights => GetChildFullWeights();
- /// <summary>
- /// 当前方案包
- /// </summary>
- public PackageNode CurPackageInfo => GetCurrentPackage();
- /// <summary>
- /// 节点深度
- /// </summary>
- public int NodeDepth => GetNodeDepth();
- /// <summary>
- /// 节点路径
- /// </summary>
- public string NodePath => GetNodePath();
- /// <summary>
- /// 是否有父节点
- /// </summary>
- public bool HasParent => IsHasParent();
- /// <summary>
- /// 是否有子节点
- /// </summary>
- public bool HasChild => IsHasChild();
- /// <summary>
- /// XML对象
- /// </summary>
- public IwbXmlNode XmlNode { get; set; }
- ///// <summary>
- ///// 是否等待其他节点
- ///// </summary>
- //public bool IsWait { get; set; }
- /// <summary>
- /// 局部变量
- /// </summary>
- public Hashtable Variables { get; set; }
- /// <summary>
- /// 验证合法性
- /// </summary>
- public virtual bool IsValid => Id != "" && Name != "";
- /// <summary>
- /// 是否等待
- /// </summary>
- public bool IsWait => WaitPath?.Any() ?? false;
- public List<string> WaitPath { get; set; }
- public bool ChildIsRunning => CheckChildIsRunning();
- public int AllChildrenCount { get; set; }
- #endregion 属性
- #region 构造函数
- protected NodeBase()
- {
- AllChildrenCount = 0;
- WaitPath = new List<string>();
- Children = new List<NodeBase>();
- GuideInfos= new List<GuideNode>();
- Variables = new Hashtable();
- NodeState = NodeState.New;
- EvNodeStateChanged += EventStateChange;
- // ReSharper disable once VirtualMemberCallInConstructor
- BindEvents();
- }
- protected NodeBase(string id) : this()
- {
- Id = id;
- }
- protected NodeBase(string id, string name) : this(id)
- {
- Name = name;
- }
- protected NodeBase(IwbXmlNode xmlNode) : this()
- {
- XmlNode = xmlNode;
- CreateNodeByXmlNode(xmlNode);
- }
- protected NodeBase(RunningBase run)
- {
- }
- #endregion 构造函数
- #region Event
- /// <summary>
- /// 绑定事件
- /// </summary>
- public virtual void BindEvents()
- {
- if (EvPreCondition == null)
- {
- EvPreCondition += EventRunPre;
- }
- if (EvRunningCondition == null)
- {
- EvRunningCondition += EventRunning;
- }
- if (EvRunnedCondition == null)
- {
- EvRunnedCondition += EventRunned;
- }
- if (EvPostCondition == null)
- {
- EvPostCondition += EventRunPost;
- }
- }
-
- #region 运行前事件
- /// <summary>
- /// 运行前条件验证
- /// </summary>
- public bool PreConditionValidated { get; set; }
- /// <summary>
- /// 无需验证(随机选的情景块不需要验证)
- /// </summary>
- public bool NotValidated { get; set; }
- /// <summary>
- /// 运行前事件
- /// </summary>
- public event EvPreCondition EvPreCondition;
- /// <summary>
- /// 执行运行前事件
- /// </summary>
- /// <param name="node"></param>
- protected virtual void ExecutePreCondition(NodeBase node)
- {
- PreConditionValidated = true;
- EvPreCondition?.Invoke(node);
- }
- protected virtual void EventRunPre(NodeBase node)
- {
- node.CurPackageInfo?.RunnerManager?.Instance?.Run(this, OperationType.PreEvent);
- }
- #endregion 运行前
- #region 运行时事件
- /// <summary>
- /// 运行时事件
- /// </summary>
- public event EvRunningCondition EvRunningCondition;
- /// <summary>
- /// 执行运行时事件
- /// </summary>
- /// <param name="node"></param>
- protected virtual void ExecuteRunningCondition(NodeBase node)
- {
- EvRunningCondition?.Invoke(node);
- }
- protected virtual void EventRunning(NodeBase node)
- {
- node.CurPackageInfo?.RunnerManager?.Instance?.Run(this, OperationType.RunningEvent);
- }
- #endregion 运行时事件
- #region 运行结束时事件
- /// <summary>
- /// 运行结束时事件
- /// </summary>
- public event EvRunnedCondition EvRunnedCondition;
- public bool RunnedConditionValidated { get; set; }
- /// <summary>
- /// 执行运行结束时事件
- /// </summary>
- /// <param name="node"></param>
- protected virtual void ExecuteRunnedCondition(NodeBase node)
- {
- RunnedConditionValidated = true;
- EvRunnedCondition?.Invoke(node);
-
-
- }
- protected virtual void EventRunned(NodeBase node)
- {
- node.CurPackageInfo?.RunnerManager?.Instance?.Run(this, OperationType.RunnedEvent);
- }
- #endregion 运行结束时事件
- #region 运行后事件
- /// <summary>
- /// 运行后条件验证
- /// </summary>
- public bool PostConditionValidated { get; set; }
- /// <summary>
- /// 运行后事件
- /// </summary>
- public event EvPostCondition EvPostCondition;
- /// <summary>
- /// 执行运行后事件
- /// </summary>
- /// <param name="node"></param>
- protected virtual void ExecutePostCondition(NodeBase node)
- {
- PostConditionValidated = true;
- EvPostCondition?.Invoke(node);
- }
- protected virtual void EventRunPost(NodeBase node)
- {
- node.CurPackageInfo?.RunnerManager?.Instance?.Run(this, OperationType.PostEvent);
- //if (HasChild)
- //{
- // foreach (var nodeBase in Children)
- // {
- // if (nodeBase.NodeState != NodeState.New)
- // {
- // nodeBase.NodeState = NodeState.Complete;
- // }
- // }
- //}
- }
- #endregion 运行后
- #region 节点变化
- public event EvNodeStateChanged EvNodeStateChanged;
- /// <summary>
- /// 节点状态变化触发事件
- /// </summary>
- /// <param name="node"></param>
- /// <param name="oldValue"></param>
- protected virtual void EventStateChange(NodeBase node, NodeState oldValue)
- {
-
- if (node.NodeState == NodeState.New)
- {
- return;
- }
- if (node.NodeState == NodeState.ReStart)
- {
- //重启后自动变更为运行状态
- node.NodeState = NodeState.Running;
- return;
- }
-
- switch (oldValue)
- {
- case NodeState.New:
- ExecutePreCondition(node);
- if (!node.NotValidated && !PreConditionValidated)
- {
- node.NodeState = NodeState.New;
- if (node.NodeType == NodeType.FlowNode || node.NodeType == NodeType.SceneFlowBlock)
- {
- node.CurPackageInfo?.RunnerManager?.Instance?.Run(this, OperationType.SelectNextNode);
- }
- return;
- }
- if (IsWait)
- {
- node.NodeState = NodeState.Wait;
- return;
- }
- NotValidated = false;
- if (node.NodeState == NodeState.Running)
- {
- typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【已启动】");
- ExecuteRunningCondition(node);
- }
- break;
- case NodeState.Wait:
- {
- if (node.NodeState == NodeState.Running)
- {
- typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【已启动】");
- ExecuteRunningCondition(node);
- }
- break;
- }
- case NodeState.Running:
- if (node.NodeState == NodeState.RunEnd)
- {
- ExecuteRunnedCondition(node);
- if (!RunnedConditionValidated)
- {
- node.NodeState = oldValue;
- return;
- }
- typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【已结束】");
- }
- else if (node.NodeState == NodeState.Complete)
- {
- typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【【已完成】】");
- ExecutePostCondition(node);
- }
- break;
- case NodeState.RunEnd:
- if (node.NodeState == NodeState.Complete)
- {
- ExecutePostCondition(node);
- typeof(NodeBase).LogDebug($"节点- [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] - 【【 已完成 】】");
- }
- break;
- case NodeState.ReStart:
-
- break;
- }
- }
- #endregion 节点变化
- #endregion Event
- #region Score
- /// <summary>
- /// 获取当前节点评分
- /// </summary>
- /// <returns></returns>
- protected virtual decimal GetActualScore()
- {
- var score= CurPackageInfo?.EvalManager?.Instance?.GetActualScore(this)??0;
- return score;
- }
- /// <summary>
- /// 获取当前节点评分
- /// </summary>
- /// <returns></returns>
- protected virtual decimal GetActualWeights()
- {
- var score = CurPackageInfo?.EvalManager?.Instance?.GetActualWeights(this)??0;
- return score;
- }
- /// <summary>
- /// 获取当前节点满分
- /// </summary>
- /// <returns></returns>
- protected virtual decimal GetNodeFullScore()
- {
- var score = CurPackageInfo?.EvalManager?.Instance?.GetNodeFullScore(this)??0;
- return score;
- }
- /// <summary>
- /// 获取子节点权重之和
- /// </summary>
- /// <returns></returns>
- protected virtual decimal GetChildFullWeights()
- {
- var score = CurPackageInfo?.EvalManager?.Instance?.GetChildFullWeights(this)??0;
- return score;
- }
- #endregion Score
-
- #region XML
- /// <summary>
- /// 转成XML
- /// </summary>
- /// <returns></returns>
- public virtual string ToXmlString()
- {
- string str = "";
- str += $"<Id>{Id}</Id>\r\n";
- str += $"<InternalNo>{InternalNo}</InternalNo>\r\n";
- str += $"<Name>{Name}</Name>\r\n";
- //str += $"<NodeType>{NodeType}</NodeType>\r\n";
- str += $"<NodeState>{NodeState}</NodeState>\r\n";
- str += Weights == 0 ? "" : $"<Weights>{Weights}</Weights>\r\n";
- str += ActualScore == 0 ? "" : $"<CurrentScore>{ActualScore}</CurrentScore>\r\n";
- str += CorrectionScore == 0 ? "" : $"<CorrectionScore>{CorrectionScore}</CorrectionScore>\r\n";
- if (Variables != null && Variables.Count > 0)
- {
- string vStr = "";
- foreach (DictionaryEntry entry in Variables)
- {
- var v = (IwbRtVariable) entry.Value;
- vStr += (vStr == "" ? "" : ",") + $"[{v.VarName}:{v.DataType.TypeString}:{v.GetStringValue()}]";
- }
- if (vStr.IsNotEmpty())
- {
- str += $"<Variables>{vStr}</Variables>";
- }
- }
- str += GuideNos.IsEmpty() ? "" : $"<GuideNos>{GuideNos}</GuideNos>\r\n";
- var str1 = "";
- str1 += PreComponent.IsEmpty() ? "" : $"<PreComponent>{PreComponent}</PreComponent>\r\n";
- str1 += RunningComponent.IsEmpty() ? "" : $"<RunningComponent>{RunningComponent}</RunningComponent>\r\n";
- str1 += RunnedComponent.IsEmpty() ? "" : $"<RunnedComponent>{RunnedComponent}</RunnedComponent>\r\n";
- str1 += PostComponent.IsEmpty() ? "" : $"<PostComponent>{PostComponent}</PostComponent>\r\n";
- if (str1.IsNotEmpty())
- {
- str += "<Components>\r\n";
- str += str1;
- str += "</Components>\r\n";
- }
-
- return str;
- }
- /// <summary>
- /// 从XML对象中创建Node对象的值
- /// </summary>
- public NodeBase CreateNodeByXmlNode(IwbXmlNode xmlNode)
- {
- XmlNode = xmlNode;
- if (xmlNode != null)
- {
- Id = xmlNode.GetChildValue("Id");
- InternalNo = xmlNode.GetChildValue("InternalNo");
- Name = xmlNode.GetChildValue("Name");
- NodeState = xmlNode.GetChildValue("NodeState")?.GetEnumByName<NodeState>() ?? NodeState.New;
- Weights = xmlNode.GetChildValue("Weights").ValD();
- Weights = Weights <= 0 ? 100 : Weights;
- CorrectionScore = xmlNode.GetChildValue("CorrectionScore").ValD();
- GuideNos = xmlNode.GetChildValue("GuideNos");
- GuideInfos = CreateGuideByNos(GuideNos);
- var variables = xmlNode.GetChildValue("Variables").StrToArray();
- if (variables.Length > 0)
- {
- foreach (var v in variables)
- {
- var arr = v.StrToArray(":");
- string id = "";
- if (arr.Length > 0)
- {
- id = arr[0].UAndT();
- }
- if (id.IsNotEmpty())
- {
- Variables[id] = IwbRtVariable.Str2Variable(v);
- }
- }
- }
- var componentNode = xmlNode.GetChildNode("Components");
- if (componentNode != null)
- {
- PreComponent = componentNode.GetChildValue("PreComponent");
- RunningComponent = componentNode.GetChildValue("RunningComponent");
- RunnedComponent = componentNode.GetChildValue("RunnedComponent");
- PostComponent = componentNode.GetChildValue("PostComponent");
- }
- CreateSelfNode(xmlNode);
- }
- return this;
- }
- private List<GuideNode> CreateGuideByNos(string guideNos)
- {
- var list = new List<GuideNode>();
- var xmlNode = PackageHelper.GetGuidesXmlFromFile(CurPackageInfo?.Id, guideNos);
- if (xmlNode != null && xmlNode.Nodes.Count>0)
- {
- foreach (IwbXmlNode child in xmlNode.Nodes)
- {
- list.Add(new GuideNode().CreateNodeByXmlNode(child));
- }
- }
- return list;
- }
- //private GuideInfo CreateGuideByXml(XmlNode xmlNode)
- //{
- // var guide= new GuideInfo();
- // if (xmlNode != null)
- // {
- // guide.Id = xmlNode.Attributes?["path"] != null ? xmlNode.Attributes["path"].Value : "";
- // if (xmlNode.HasChildNodes)
- // {
- // foreach (XmlNode node in xmlNode.ChildNodes)
- // {
- // switch (node.Name.Trim())
- // {
- // case "Name":
- // guide.Name = node.FirstChild?.Value;
- // break;
- // case "Description":
- // guide.Description = node.FirstChild?.Value;
- // break;
- // case "IsPush":
- // guide.IsPush = node.FirstChild.Value.ValB();
- // break;
- // }
- // }
- // }
- // }
-
- // return guide;
- //}
- /// <summary>
- /// 创建自己属性值的时候必须继承,获取属性值的节点对象
- /// </summary>
- /// <param name="xmlNode"></param>
- protected virtual void CreateSelfNode(IwbXmlNode xmlNode)
- {
- }
- protected string ConvertXml(string str,bool isEscape=false)
- {
- if (isEscape)
- {
- str = str.Replace("&","&");
- str = str.Replace("<","<");
- str = str.Replace(">",">");
- str = str.Replace("\"",""");
- str = str.Replace("'","'");
- }
- else
- {
- str = str.Replace("&", "&");
- str = str.Replace("<", "<");
- str = str.Replace(">", ">");
- str = str.Replace(""", "\"");
- str = str.Replace("'", "'");
- }
-
- return str;
- }
- #endregion XML
- #region Virtual Method
- protected NodeState GetNodeState(RunningBase running)
- {
- return running.IsEnd? NodeState.RunEnd: running.IsStart? NodeState.Running: NodeState.New;
- }
- /// <summary>
- /// 获取变量
- /// </summary>
- /// <returns></returns>
- public virtual Hashtable GetVariables()
- {
- var variables = (Hashtable)Variables.Clone();
- if (HasParent)
- {
- variables = Parent.GetVariables().MergeHashtable(variables);
- }
- return variables;
- }
- /// <summary>
- /// 获取变量
- /// </summary>
- /// <returns></returns>
- public virtual Hashtable GetSelfVariables()
- {
- var variables = GetVariables();
- var newVariables = new Hashtable();
- foreach (DictionaryEntry entry in variables)
- {
- if (!DefaultVariable.VariableNames.Contains(entry.Key))
- {
- newVariables.Add(entry.Key, entry.Value);
- }
- }
- return newVariables;
- }
- public virtual void SetPreVariables()
- {
- SetVariable(DefaultVariable.FullNodeScore, NodeFullScore);
- SetVariable(DefaultVariable.Path, NodePath);
- SetVariable(DefaultVariable.RunValidate, "true");
- }
- public virtual void SetRunnedVariables()
- {
- //SetVariable(DefaultVariable.PrevNodeScore, ActualScore);
- }
-
- /// <summary>
- /// 添加变量
- /// </summary>
- /// <returns></returns>
- public void SetVariable(string key,string value)
- {
- SetVariable(key, new IwbRtVariable(key).SetValue(value));
- }
- /// <summary>
- /// 添加变量
- /// </summary>
- /// <returns></returns>
- public void SetVariable(string key,int? value)
- {
- SetVariable(key, new IwbRtVariable(key, "int").SetValue(value));
- }
- /// <summary>
- /// 添加变量
- /// </summary>
- /// <returns></returns>
- public void SetVariable(string key,decimal? value)
- {
- SetVariable(key, new IwbRtVariable(key, "decimal").SetValue(value));
- }
- /// <summary>
- /// 添加变量
- /// </summary>
- /// <returns></returns>
- public void SetVariable(string key,IwbRtVariable variable)
- {
- if (Variables.ContainsKey(key))
- {
- Variables[key] = variable;
- }
- else
- {
- Variables.Add(key,variable);
- }
- }
- ///// <summary>
- ///// 添加变量
- ///// </summary>
- ///// <returns></returns>
- //public void SetVariable( Dictionary<string,object> dic)
- //{
- // foreach (var d in dic)
- // {
- // SetVariable(d.Key,d.Value);
- // }
- //}
- /// <summary>
- /// 获取百分制得分
- /// </summary>
- /// <returns></returns>
- public decimal ConvertScoreTo100()
- {
- return ConvertScoreByPer(100);
- }
- /// <summary>
- /// 获取指定分制的得分
- /// </summary>
- /// <returns></returns>
- public decimal ConvertScoreByPer(int maxScore)
- {
- if (maxScore <= 0)
- {
- throw new Exception("分制不能小于0!");
- }
- return NodeFullScore == 0 ? 0 : Math.Round(maxScore * ActualScore / NodeFullScore);
- }
- /// <summary>
- /// 获取当前Package
- /// </summary>
- /// <returns></returns>
- protected PackageNode GetCurrentPackage()
- {
- PackageNode curPackage = null;
- if (HasParent)
- {
- curPackage = Parent.GetCurrentPackage();
- }
- if (NodeType == NodeType.ScenePackage)
- {
- curPackage = (PackageNode) this;
- }
- return curPackage;
- }
- /// <summary>
- /// 根据Id查询节点
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public virtual NodeBase GetNodeByPath(string path)
- {
- return CurPackageInfo?.GetChildNodeByPath(path);
- }
- /// <summary>
- /// 查询字节的下的节点
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- protected NodeBase GetChildNodeByPath(string path)
- {
- if (path.IsEmpty())
- {
- return null;
- }
- if (NodePath.UAndT() == path.UAndT())
- {
- return this;
- }
- if (HasChild)
- {
- var child = Children.FirstOrDefault(a => a.NodePath.UAndT() == path.UAndT());
- if (child != null)
- {
- return child;
- }
- if (NodeType== NodeType.FlowNode)
- {
- var node1 = (FlowNode) this;
- foreach (var node in node1.SceneInfos)
- {
- child = node.GetChildNodeByPath(path);
- if (child != null)
- {
- return child;
- }
- }
- }
- foreach (var node in Children)
- {
- child = node.GetChildNodeByPath(path);
- if (child != null)
- {
- return child;
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 获取节点深度
- /// </summary>
- /// <returns></returns>
- protected virtual int GetNodeDepth()
- {
- var depth = 1;
- if (HasParent)
- {
- depth = Parent.NodeDepth + 1;
- }
- return depth;
- }
- protected virtual string GetNodePath()
- {
- return HasParent ? $"{Parent.NodePath}_{InternalNo}" : InternalNo;
- }
- protected virtual bool IsHasParent()
- {
- return Parent != null;
- }
- protected virtual bool IsHasChild()
- {
- return Children?.Count > 0;
- }
- protected virtual bool CheckChildIsRunning()
- {
- if (!HasChild)
- {
- return NodeState==NodeState.Running;
- }
- return Children.Any(a => a.ChildIsRunning || a.NodeState==NodeState.Running );
- }
- protected virtual void SetAllChildrenCount()
- {
- if (HasParent)
- {
- Parent.SetAllChildrenCount();
- }
- AllChildrenCount++;
- }
- #endregion Virtual Method
- #region IComparer<CimNode> 成员
- int IComparer<NodeBase>.Compare(NodeBase x, NodeBase y)
- {
- return String.CompareOrdinal(x?.Id, y?.Id);
- }
- #endregion IComparer<CimNode> 成员
- #region ICloneable 成员
- public object Clone()
- {
- var obj = (NodeBase)Assembly.GetExecutingAssembly().CreateInstance(GetType().FullName ?? throw new InvalidOperationException());
- NodeBase node = obj?.CreateNodeByXmlNode(XmlNode);
- return node ?? throw new InvalidOperationException();
- }
- #endregion ICloneable 成员
- public override string ToString()
- {
- return $"[{NodeState}][{GetType().Name}]【{Name}-{Id}】";
- }
- }
- /// <summary>
- /// 运行前委托
- /// </summary>
- /// <param name="node"></param>
- [Serializable]
- public delegate void EvPreCondition(NodeBase node);
- /// <summary>
- /// 运行时委托
- /// </summary>
- /// <param name="node"></param>
- [Serializable]
- public delegate void EvRunningCondition(NodeBase node);
- /// <summary>
- /// 运行结束时委托
- /// </summary>
- /// <param name="node"></param>
- [Serializable]
- public delegate void EvRunnedCondition(NodeBase node);
- /// <summary>
- /// 节点状态发生改变时触发的事件
- /// </summary>
- /// <param name="node">当前的节点</param>
- /// <param name="oldValue">改变前的值</param>
- [Serializable]
- public delegate void EvNodeStateChanged(NodeBase node, NodeState oldValue);
- /// <summary>
- /// 节点评分改变触发的事件
- /// </summary>
- /// <param name="node">当前的节点</param>
- /// <param name="oldValue">改变前的值</param>
- [Serializable]
- public delegate void EvNodeScoreChanged(NodeBase node, decimal oldValue);
- /// <summary>
- /// 运行后委托
- /// </summary>
- /// <param name="node"></param>
- [Serializable]
- public delegate void EvPostCondition(NodeBase node);
- }
|