using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Serein.Library
{
///
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
///
public abstract partial class NodeModelBase : IDynamicFlowNode
{
#region 节点相关事件
///
/// 实体节点创建完成后调用的方法,调用时间早于 LoadInfo() 方法
///
public virtual void OnCreating()
{
}
///
/// 保存自定义信息
///
///
public virtual NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
return nodeInfo;
}
///
/// 加载自定义数据
///
///
public virtual void LoadCustomData(NodeInfo nodeInfo)
{
return;
}
///
/// 移除该节点
///
public virtual void Remove()
{
if (this.DebugSetting.CancelInterrupt != null)
{
this.DebugSetting.CancelInterrupt?.Invoke();
}
this.DebugSetting.NodeModel = null;
this.DebugSetting = null;
foreach (var pd in this.MethodDetails.ParameterDetailss)
{
pd.DataValue = null;
pd.Items = null;
pd.NodeModel = null;
pd.ExplicitType = null;
pd.DataType = null;
pd.Name = null;
pd.ArgDataSourceNodeGuid = null;
pd.ExplicitTypeName = null;
}
this.MethodDetails.ParameterDetailss = null;
this.MethodDetails.ActingInstance = null;
this.MethodDetails.NodeModel = null;
this.MethodDetails.ReturnType = null;
this.MethodDetails.AssemblyName = null;
this.MethodDetails.MethodAnotherName = null;
this.MethodDetails.MethodLockName = null;
this.MethodDetails.MethodName = null;
this.MethodDetails.ActingInstanceType = null;
this.MethodDetails = null;
this.Position = null;
this.DisplayName = null;
this.Env = null;
}
///
/// 输出方法参数信息
///
///
public ParameterData[] SaveParameterInfo()
{
if(MethodDetails.ParameterDetailss == null)
{
return new ParameterData[0];
}
if (MethodDetails.ParameterDetailss.Length > 0)
{
return MethodDetails.ParameterDetailss
.Select(it => new ParameterData
{
SourceNodeGuid = it.ArgDataSourceNodeGuid,
SourceType = it.ArgDataSourceType.ToString(),
State = it.IsExplicitData,
Value = it.DataValue,
})
.ToArray();
}
else
{
return new ParameterData[0];
}
}
///
/// 导出为节点信息
///
///
public NodeInfo ToInfo()
{
// if (MethodDetails == null) return null;
var trueNodes = SuccessorNodes[ConnectionInvokeType.IsSucceed].Select(item => item.Guid); // 真分支
var falseNodes = SuccessorNodes[ConnectionInvokeType.IsFail].Select(item => item.Guid);// 假分支
var errorNodes = SuccessorNodes[ConnectionInvokeType.IsError].Select(item => item.Guid);// 异常分支
var upstreamNodes = SuccessorNodes[ConnectionInvokeType.Upstream].Select(item => item.Guid);// 上游分支
// 生成参数列表
ParameterData[] parameterData = SaveParameterInfo();
NodeInfo nodeInfo = new NodeInfo
{
Guid = Guid,
AssemblyName = MethodDetails.AssemblyName,
MethodName = MethodDetails?.MethodName,
Label = MethodDetails?.MethodAnotherName,
Type = ControlType.ToString() , //this.GetType().ToString(),
TrueNodes = trueNodes.ToArray(),
FalseNodes = falseNodes.ToArray(),
UpstreamNodes = upstreamNodes.ToArray(),
ParameterData = parameterData.ToArray(),
ErrorNodes = errorNodes.ToArray(),
Position = Position,
IsProtectionParameter = this.MethodDetails.IsProtectionParameter,
IsInterrupt = this.DebugSetting.IsInterrupt,
IsEnable = this.DebugSetting.IsEnable,
ParentNodeGuid = ContainerNode?.Guid,
ChildNodeGuids = ChildrenNode.Select(item => item.Guid).ToArray(),
};
nodeInfo.Position.X = Math.Round(nodeInfo.Position.X, 1);
nodeInfo.Position.Y = Math.Round(nodeInfo.Position.Y, 1);
nodeInfo = SaveCustomData(nodeInfo);
return nodeInfo;
}
///
/// 从节点信息加载节点
///
///
///
public void LoadInfo(NodeInfo nodeInfo)
{
this.Guid = nodeInfo.Guid;
this.Position = nodeInfo.Position ?? new PositionOfUI(0, 0);// 加载位置信息
var md = this.MethodDetails; // 当前节点的方法说明
this.MethodDetails.IsProtectionParameter = nodeInfo.IsProtectionParameter; // 保护参数
this.DebugSetting.IsInterrupt = nodeInfo.IsInterrupt; // 是否中断
this.DebugSetting.IsEnable = nodeInfo.IsEnable; // 是否使能
if (md != null)
{
if(md.ParameterDetailss == null)
{
md.ParameterDetailss = new ParameterDetails[0];
}
LoadCustomData(nodeInfo); // 加载自定义数据
var pds = md.ParameterDetailss; // 当前节点的入参描述数组
#region 类库方法型节点加载参数
if (nodeInfo.ParameterData.Length > pds.Length && md.HasParamsArg)
{
// 保存的参数信息项数量大于方法本身的方法入参数量(可能存在可变入参)
var length = nodeInfo.ParameterData.Length - pds.Length; // 需要扩容的长度
this.MethodDetails.ParameterDetailss = ArrayHelper.Expansion(pds, length); // 扩容入参描述数组
pds = md.ParameterDetailss; // 当前节点的入参描述数组
var startParmsPd = pds[md.ParamsArgIndex]; // 获取可变入参参数描述
for (int i = md.ParamsArgIndex + 1; i <= md.ParamsArgIndex + length; i++)
{
pds[i] = startParmsPd.CloneOfModel(this);
pds[i].Index = pds[i - 1].Index + 1;
pds[i].IsParams = true;
}
}
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
{
if (i >= pds.Length)
{
Env.WriteLine(InfoType.ERROR, $"保存的参数数量大于方法此时的入参参数数量:[{nodeInfo.Guid}][{nodeInfo.MethodName}]");
break;
}
var pd = pds[i];
ParameterData pdInfo = nodeInfo.ParameterData[i];
pd.IsExplicitData = pdInfo.State;
pd.DataValue = pdInfo.Value;
pd.ArgDataSourceType = EnumHelper.ConvertEnum(pdInfo.SourceType);
pd.ArgDataSourceNodeGuid = pdInfo.SourceNodeGuid;
}
#endregion
}
}
#endregion
#region 调试中断
///
/// 不再中断
///
public void CancelInterrupt()
{
this.DebugSetting.IsInterrupt = false;
DebugSetting.CancelInterrupt?.Invoke();
}
#endregion
#region 节点方法的执行
///
/// 是否应该退出执行
///
///
///
///
public static bool IsBradk(IDynamicContext context, CancellationTokenSource flowCts)
{
// 上下文不再执行
if (context.RunState == RunState.Completion)
{
return true;
}
// 不存在全局触发器时,流程运行状态被设置为完成,退出执行,用于打断无限循环分支。
if (flowCts is null && context.Env.FlowState == RunState.Completion)
{
return true;
}
// 如果存在全局触发器,且触发器的执行任务已经被取消时,退出执行。
if (flowCts != null)
{
if (flowCts.IsCancellationRequested)
return true;
}
return false;
}
///
/// 开始执行
///
///
///
public async Task StartFlowAsync(IDynamicContext context)
{
Stack stack = new Stack();
HashSet processedNodes = new HashSet(); // 用于记录已处理上游节点的节点
stack.Push(this);
var flowCts = context.Env.IOC.Get(NodeStaticConfig.FlipFlopCtsName);
bool hasFlipflow = flowCts != null;
while (stack.Count > 0) // 循环中直到栈为空才会退出循环
{
#if DEBUG
await Task.Delay(1);
#endif
#region 执行相关
// 从栈中弹出一个节点作为当前节点进行处理
var currentNode = stack.Pop();
context.NextOrientation = ConnectionInvokeType.None; // 重置上下文状态
object newFlowData;
try
{
if (IsBradk(context, flowCts)) break; // 退出执行
newFlowData = await currentNode.ExecutingAsync(context);
if (IsBradk(context, flowCts)) break; // 退出执行
if (context.NextOrientation == ConnectionInvokeType.None) // 没有手动设置时,进行自动设置
{
context.NextOrientation = ConnectionInvokeType.IsSucceed;
}
}
catch (Exception ex)
{
newFlowData = null;
context.Env.WriteLine(InfoType.ERROR, $"节点[{currentNode.Guid}]异常:" + ex);
context.NextOrientation = ConnectionInvokeType.IsError;
context.ExceptionOfRuning = ex;
}
await RefreshFlowDataAndExpInterrupt(context, currentNode, newFlowData); // 执行当前节点后刷新数据
#endregion
#region 执行完成
// 首先将指定类别后继分支的所有节点逆序推入栈中
var nextNodes = currentNode.SuccessorNodes[context.NextOrientation];
for (int index = nextNodes.Count - 1; index >= 0; index--)
{
// 筛选出启用的节点的节点
if (nextNodes[index].DebugSetting.IsEnable)
{
context.SetPreviousNode(nextNodes[index], currentNode);
stack.Push(nextNodes[index]);
}
}
// 然后将指上游分支的所有节点逆序推入栈中
var upstreamNodes = currentNode.SuccessorNodes[ConnectionInvokeType.Upstream];
for (int index = upstreamNodes.Count - 1; index >= 0; index--)
{
// 筛选出启用的节点的节点
if (upstreamNodes[index].DebugSetting.IsEnable)
{
context.SetPreviousNode(upstreamNodes[index], currentNode);
stack.Push(upstreamNodes[index]);
}
}
#endregion
}
}
///
/// 执行节点对应的方法
///
/// 流程上下文
/// 节点传回数据对象
public virtual async Task