LocalFlowEnvironment文件丢失,需要重写

This commit is contained in:
fengjiayi
2025-06-22 21:53:37 +08:00
parent 999060b67a
commit 97df2a04b2
58 changed files with 4285 additions and 354 deletions

View File

@@ -0,0 +1,170 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.NodeGenerator;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Mime;
using System.Threading;
namespace Serein.NodeFlow.Model
{
/// <summary>
/// 节点基类(数据)
/// </summary>
[NodeProperty(ValuePath = NodeValuePath.Node)]
public abstract partial class NodeModelBase : IFlowNode
{
/// <summary>
/// 节点运行环境
/// </summary>
[PropertyInfo(IsProtection = true)]
private IFlowEnvironment _env;
/// <summary>
/// 标识节点对象全局唯一
/// </summary>
[PropertyInfo(IsProtection = true)]
private string _guid;
/// <summary>
/// 描述节点对应的控件类型
/// </summary>
[PropertyInfo(IsProtection = true)]
private NodeControlType _controlType;
/// <summary>
/// 所属画布
/// </summary>
[PropertyInfo(IsProtection = true)]
private FlowCanvasDetails _canvasDetails ;
/// <summary>
/// 在画布中的位置
/// </summary>
[PropertyInfo(IsProtection = true)]
private PositionOfUI _position ;
/// <summary>
/// 显示名称
/// </summary>
[PropertyInfo]
private string _displayName;
/// <summary>
/// 是否公开
/// </summary>
[PropertyInfo(IsNotification = true)]
private bool _isPublic;
/* /// <summary>
/// 是否保护参数
/// </summary>
[PropertyInfo(IsNotification = true)]
private bool _isProtectionParameter;*/
/// <summary>
/// 附加的调试功能
/// </summary>
[PropertyInfo(IsProtection = true)]
private NodeDebugSetting _debugSetting ;
/// <summary>
/// 方法描述。包含参数信息。不包含Method与委托如若需要调用对应的方法需要通过MethodName从环境中获取委托进行调用。
/// </summary>
[PropertyInfo]
private MethodDetails _methodDetails ;
}
public abstract partial class NodeModelBase : IDynamicFlowNode
{
/// <summary>
/// 是否为基础节点
/// </summary>
public virtual bool IsBase { get; } = false;
/// <summary>
/// 可以放置多少个节点
/// </summary>
public virtual int MaxChildrenCount { get; } = 0;
public NodeModelBase(IFlowEnvironment environment)
{
PreviousNodes = new Dictionary<ConnectionInvokeType, List<IFlowNode>>();
SuccessorNodes = new Dictionary<ConnectionInvokeType, List<IFlowNode>>();
NeedResultNodes = new Dictionary<ConnectionArgSourceType, List<IFlowNode>>();
foreach (ConnectionInvokeType ctType in NodeStaticConfig.ConnectionTypes)
{
PreviousNodes[ctType] = new List<IFlowNode>();
SuccessorNodes[ctType] = new List<IFlowNode>();
}
foreach (ConnectionArgSourceType ctType in NodeStaticConfig.ConnectionArgSourceTypes)
{
NeedResultNodes[ctType] = new List<IFlowNode>();
}
ChildrenNode = new List<IFlowNode>();
DebugSetting = new NodeDebugSetting(this);
this.Env = environment;
}
/// <summary>
/// 不同分支的父节点(流程调用)
/// </summary>
public Dictionary<ConnectionInvokeType, List<IFlowNode>> PreviousNodes { get; }
/// <summary>
/// 不同分支的子节点(流程调用)
/// </summary>
public Dictionary<ConnectionInvokeType, List<IFlowNode>> SuccessorNodes { get; set; }
/// <summary>
/// 需要该节点返回值作为入参参数的节点集合
/// </summary>
public Dictionary<ConnectionArgSourceType, List<IFlowNode>> NeedResultNodes { get;}
/// <summary>
/// 该节点的容器节点
/// </summary>
public IFlowNode ContainerNode { get; set; } = null;
/// <summary>
/// 该节点的子项节点(如果该节点是容器节点,那就会有这个参数)
/// </summary>
public List<IFlowNode> ChildrenNode { get; }
/// <summary>
/// 节点公开状态发生改变
/// </summary>
partial void OnIsPublicChanged(bool oldValue, bool newValue)
{
if (newValue)
{
// 公开节点
if (!CanvasDetails.PublicNodes.Contains(this))
{
CanvasDetails.PublicNodes.Add(this);
}
}
else
{
// 取消公开
if (CanvasDetails.PublicNodes.Contains(this))
{
CanvasDetails.PublicNodes.Remove(this);
}
}
}
}
}

View File

@@ -0,0 +1,151 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serein.Library;
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.NodeFlow.Model
{
/// <summary>
/// 节点基类
/// </summary>
public abstract partial class NodeModelBase : IDynamicFlowNode
{
/// <summary>
/// 实体节点创建完成后调用的方法,调用时间早于 LoadInfo() 方法
/// </summary>
public virtual void OnCreating()
{
}
/// <summary>
/// 保存自定义信息
/// </summary>
/// <returns></returns>
public virtual NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
return nodeInfo;
}
/// <summary>
/// 加载自定义数据
/// </summary>
/// <param name="nodeInfo"></param>
public virtual void LoadCustomData(NodeInfo nodeInfo)
{
return;
}
/* /// <summary>
/// 移除该节点
/// </summary>
public virtual void Remove()
{
if (this.DebugSetting.CancelInterrupt != null)
{
this.DebugSetting.CancelInterrupt?.Invoke();
}
if (this.IsPublic)
{
this.CanvasDetails.PublicNodes.Remove(this);
}
this.DebugSetting.NodeModel = null;
this.DebugSetting = null;
if(this.MethodDetails is not null)
{
if (this.MethodDetails.ParameterDetailss != 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.InputType = ParameterValueInputType.Input;
}
}
this.MethodDetails.ParameterDetailss = null;
this.MethodDetails.NodeModel = null;
this.MethodDetails.ReturnType = null;
this.MethodDetails.ActingInstanceType = null;
this.MethodDetails = null;
}
this.Position = null;
this.DisplayName = null;
this.Env = null;
}*/
/// <summary>
/// 执行节点对应的方法
/// </summary>
/// <param name="context">流程上下文</param>
/// <param name="token"></param>
/// <param name="args">自定义参数</param>
/// <returns>节点传回数据对象</returns>
public virtual async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
// 执行触发检查是否需要中断
if (DebugSetting.IsInterrupt)
{
context.Env.TriggerInterrupt(Guid, "", InterruptTriggerEventArgs.InterruptTriggerType.Monitor); // 通知运行环境该节点中断了
await DebugSetting.GetInterruptTask.Invoke();
SereinEnv.WriteLine(InfoType.INFO, $"[{this.MethodDetails?.MethodName}]中断已取消,开始执行后继分支");
if (token.IsCancellationRequested) { return null; }
}
MethodDetails md = MethodDetails;
if (md is null)
{
throw new Exception($"节点{this.Guid}不存在方法信息请检查是否需要重写节点的ExecutingAsync");
}
if (!context.Env.TryGetDelegateDetails(md.AssemblyName, md.MethodName, out var dd)) // 流程运行到某个节点
{
throw new Exception($"节点{this.Guid}不存在对应委托");
}
var instance = Env.IOC.Get(md.ActingInstanceType);
if (instance is null)
{
Env.IOC.Register(md.ActingInstanceType).Build();
instance = Env.IOC.Get(md.ActingInstanceType);
}
object[] args = await this.GetParametersAsync(context, token);
var result = await dd.InvokeAsync(instance, args);
var flowReslt = new FlowResult(this, context, result);
return flowReslt;
}
}
}

View File

@@ -0,0 +1,18 @@
using Serein.Library.Api;
using Serein.Library;
using System.Security.AccessControl;
namespace Serein.NodeFlow.Model.Node
{
/// <summary>
/// 单动作节点(用于动作控件)
/// </summary>
public class SingleActionNode : NodeModelBase
{
public SingleActionNode(IFlowEnvironment environment):base(environment)
{
}
}
}

View File

@@ -0,0 +1,195 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using System;
using System.ComponentModel;
using System.Dynamic;
using System.Linq.Expressions;
namespace Serein.NodeFlow.Model
{
/// <summary>
/// 条件节点(用于条件控件)
/// </summary>
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleConditionNode : NodeModelBase
{
/// <summary>
/// 是否为自定义参数
/// </summary>
[PropertyInfo(IsNotification = true)]
private bool _isExplicitData;
/// <summary>
/// 自定义参数值
/// </summary>
[PropertyInfo(IsNotification = true)]
private string? _explicitData;
/// <summary>
/// 条件表达式
/// </summary>
[PropertyInfo(IsNotification = true)]
private string _expression;
}
public partial class SingleConditionNode : NodeModelBase
{
/// <summary>
/// 条件表达式节点是基础节点
/// </summary>
public override bool IsBase => true;
/// <summary>
/// 表达式参数索引
/// </summary>
private const int INDEX_EXPRESSION = 0;
public SingleConditionNode(IFlowEnvironment environment):base(environment)
{
this.IsExplicitData = false;
this.ExplicitData = string.Empty;
this.Expression = "PASS";
}
public override void OnCreating()
{
// 这里的这个参数是为了方便使用入参控制点,参数无意义
var pd = new ParameterDetails[1];
pd[INDEX_EXPRESSION] = new ParameterDetails
{
Index = INDEX_EXPRESSION,
Name = nameof(Expression),
IsExplicitData = false,
DataValue = string.Empty,
DataType = typeof(string),
ExplicitType = typeof(string),
ArgDataSourceNodeGuid = string.Empty,
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
NodeModel = this,
//Convertor = null,
InputType = ParameterValueInputType.Input,
Items = null,
Description = "条件节点入参控制点"
};
this.MethodDetails.ParameterDetailss = [..pd];
}
/// <summary>
/// 导出方法信息
/// </summary>
/// <param name="nodeInfo"></param>
/// <returns></returns>
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.Expression = Expression ?? "";
data.ExplicitData = ExplicitData ?? "";
data.IsExplicitData = IsExplicitData;
nodeInfo.CustomData = data;
return nodeInfo;
}
/// <summary>
/// 加载自定义数据
/// </summary>
/// <param name="nodeInfo"></param>
public override void LoadCustomData(NodeInfo nodeInfo)
{
this.Expression = nodeInfo.CustomData?.Expression ?? "";
this.ExplicitData = nodeInfo.CustomData?.ExplicitData ?? "";
this.IsExplicitData = nodeInfo.CustomData?.IsExplicitData ?? false;
}
/// <summary>
/// 重写节点的方法执行
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested)
{
return new FlowResult(this, context);
}
// 接收上一节点参数or自定义参数内容
object? parameter;
object? result = null;
if (!IsExplicitData)
{
// 使用自动取参
var pd = MethodDetails.ParameterDetailss[INDEX_EXPRESSION];
var hasNode = context.Env.TryGetNodeModel(pd.ArgDataSourceNodeGuid, out var argSourceNode);
if (hasNode)
{
context.NextOrientation = ConnectionInvokeType.IsError;
return new FlowResult(this, context);
}
if (hasNode)
{
return new FlowResult(this, context);
}
if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeData)
{
result = context.GetFlowData(argSourceNode).Value; // 使用自定义节点的参数
}
else if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeDataOfInvoke)
{
CancellationTokenSource cts = new CancellationTokenSource();
var nodeResult = await argSourceNode.ExecutingAsync(context, cts.Token);
result = nodeResult.Value;
cts?.Cancel();
cts?.Dispose();
}
else
{
result = context.TransmissionData(this).Value; // 条件节点透传上一节点的数据
}
parameter = result; // 使用上一节点的参数
}
else
{
var exp = ExplicitData?.ToString();
if (!string.IsNullOrEmpty(exp) && exp.StartsWith('@'))
{
parameter = result; // 表达式获取上一节点数据
if (parameter is not null)
{
parameter = SerinExpressionEvaluator.Evaluate(exp, parameter, out _);
}
}
else
{
parameter = ExplicitData; // 使用自定义的参数
}
}
bool judgmentResult = false;
try
{
judgmentResult = SereinConditionParser.To(parameter, Expression);
context.NextOrientation = judgmentResult ? ConnectionInvokeType.IsSucceed : ConnectionInvokeType.IsFail;
}
catch (Exception ex)
{
context.NextOrientation = ConnectionInvokeType.IsError;
context.ExceptionOfRuning = ex;
}
SereinEnv.WriteLine(InfoType.INFO, $"{result} {Expression} -> " + context.NextOrientation);
//return result;
return new FlowResult(this, context, judgmentResult);
}
}
}

View File

@@ -0,0 +1,154 @@
using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using System.Dynamic;
using System.Reactive;
using System.Reflection.Metadata;
namespace Serein.NodeFlow.Model
{
/// <summary>
/// Expression Operation - 表达式操作
/// </summary>
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleExpOpNode : NodeModelBase
{
/// <summary>
/// 表达式
/// </summary>
[PropertyInfo(IsNotification = true)]
private string _expression;
}
public partial class SingleExpOpNode : NodeModelBase
{
/// <summary>
/// 表达式节点是基础节点
/// </summary>
public override bool IsBase => true;
/// <summary>
/// 表达式参数索引
/// </summary>
private const int INDEX_EXPRESSION = 0;
public SingleExpOpNode(IFlowEnvironment environment) : base(environment)
{
}
/// <summary>
/// 加载完成后调用的方法
/// </summary>
public override void OnCreating()
{
// 这里的这个参数是为了方便使用入参控制点,参数无意义
var pd = new ParameterDetails[1];
pd[INDEX_EXPRESSION] = new ParameterDetails
{
Index = INDEX_EXPRESSION,
Name = nameof(Expression),
IsExplicitData = false,
DataValue = string.Empty,
DataType = typeof(string),
ExplicitType = typeof(string),
ArgDataSourceNodeGuid = string.Empty,
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
NodeModel = this,
//Convertor = null,
InputType = ParameterValueInputType.Input,
Items = null,
Description = "表达式节点入参控制点"
};
this.MethodDetails.ParameterDetailss = [.. pd];
}
/// <summary>
/// 导出方法信息
/// </summary>
/// <param name="nodeInfo"></param>
/// <returns></returns>
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.Expression = Expression ?? "";
nodeInfo.CustomData = data;
return nodeInfo;
}
/// <summary>
/// 加载自定义数据
/// </summary>
/// <param name="nodeInfo"></param>
public override void LoadCustomData(NodeInfo nodeInfo)
{
this.Expression = nodeInfo.CustomData?.Expression ?? "";
}
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if(token.IsCancellationRequested) return new FlowResult(this, context);
object? parameter = null;// context.TransmissionData(this); // 表达式节点使用上一节点数据
var pd = MethodDetails.ParameterDetailss[0];
var hasNode = context.Env.TryGetNodeModel(pd.ArgDataSourceNodeGuid, out var argSourceNode);
if (hasNode)
{
context.NextOrientation = ConnectionInvokeType.IsError;
return new FlowResult(this, context);
}
if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeData)
{
// 使用自定义节点的参数
parameter = context.GetFlowData(argSourceNode).Value;
}
else if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeDataOfInvoke)
{
// 立刻调用目标节点,然后使用其返回值
var cts = new CancellationTokenSource();
var result = await argSourceNode.ExecutingAsync(context, cts.Token);
cts?.Cancel();
cts?.Dispose();
parameter = result.Value;
}
else
{
// 条件节点透传上一节点的数据
parameter = context.TransmissionData(this);
}
try
{
var newData = SerinExpressionEvaluator.Evaluate(Expression, parameter, out bool isChange);
object? result = null;
if (isChange)
{
result = newData;
}
else
{
result = parameter;
}
context.NextOrientation = ConnectionInvokeType.IsSucceed;
return new FlowResult(this,context, result);
}
catch (Exception ex)
{
context.NextOrientation = ConnectionInvokeType.IsError;
context.ExceptionOfRuning = ex;
return new FlowResult(this, context);
}
}
}
}

View File

@@ -0,0 +1,72 @@
using Serein.Library.Api;
using Serein.Library;
using Serein.Library.Utils;
using System;
namespace Serein.NodeFlow.Model.Node
{
/// <summary>
/// 触发器节点
/// </summary>
public class SingleFlipflopNode : NodeModelBase
{
public SingleFlipflopNode(IFlowEnvironment environment) : base(environment)
{
}
/// <summary>
/// 执行触发器进行等待触发
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
#region
if (DebugSetting.IsInterrupt) // 执行触发前
{
string guid = Guid.ToString();
await DebugSetting.GetInterruptTask.Invoke();
await Console.Out.WriteLineAsync($"[{MethodDetails.MethodName}]中断已取消,开始执行后继分支");
}
#endregion
MethodDetails md = MethodDetails;
if (!context.Env.TryGetDelegateDetails(md.AssemblyName, md.MethodName, out var dd)) // 流程运行到某个节点
{
throw new Exception("不存在对应委托");
}
var instance = context.Env.IOC.Get(md.ActingInstanceType);
if (instance is null)
{
Env.IOC.Register(md.ActingInstanceType).Build();
instance = Env.IOC.Get(md.ActingInstanceType);
}
await dd.InvokeAsync(instance, [context]);
var args = await this.GetParametersAsync(context, token);
// 因为这里会返回不确定的泛型 IFlipflopContext<TRsult>
// 而我们只需要获取到 State 和 Value返回的数据
// 所以使用 dynamic 类型接收
if (token.IsCancellationRequested)
{
return null;
}
dynamic dynamicFlipflopContext = await dd.InvokeAsync(instance, args);
FlipflopStateType flipflopStateType = dynamicFlipflopContext.State;
context.NextOrientation = flipflopStateType.ToContentType();
if (dynamicFlipflopContext.Type == TriggerDescription.Overtime)
{
throw new FlipflopException(MethodDetails.MethodName + "触发器超时触发。Guid" + Guid);
}
object result = dynamicFlipflopContext.Value;
var flowReslt = new FlowResult(this, context, result);
return flowReslt;
}
}
}

View File

@@ -0,0 +1,285 @@
using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library.Api;
using Serein.Script;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Serein.NodeFlow.Model
{
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleFlowCallNode
{
/// <summary>
/// 目标公开节点
/// </summary>
[PropertyInfo(IsNotification = true)]
private string targetNodeGuid;
/// <summary>
/// 使用目标节点的参数如果为true则使用目标节点的入参如果为false则使用节点自定义入参
/// </summary>
[PropertyInfo(IsNotification = true)]
private bool _isShareParam ;
}
/// <summary>
/// 流程调用节点
/// </summary>
public partial class SingleFlowCallNode : NodeModelBase
{
/// <summary>
/// 接口节点
/// </summary>
private IFlowNode targetNode;
/// <summary>
/// 缓存的方法信息
/// </summary>
public MethodDetails CacheMethodDetails { get; private set; }
/// <summary>
/// 接口节点Guid
/// </summary>
//public string? TargetNodeGuid => targetNode?.Guid;
public SingleFlowCallNode(IFlowEnvironment environment) : base(environment)
{
}
/// <summary>
/// 重置接口节点
/// </summary>
public void ResetTargetNode()
{
if (targetNode is not null)
{
// 取消接口
TargetNodeGuid = string.Empty;
}
}
/// <summary>
/// 设置接口节点
/// </summary>
/// <param name="nodeGuid"></param>
public void SetTargetNode(string? nodeGuid)
{
if (nodeGuid is null || !Env.TryGetNodeModel(nodeGuid, out _))
{
return;
}
TargetNodeGuid = nodeGuid;
}
partial void OnTargetNodeGuidChanged(string value)
{
if (string.IsNullOrEmpty(value) || !Env.TryGetNodeModel(value, out targetNode))
{
// 取消设置接口节点
targetNode.PropertyChanged -= TargetNode_PropertyChanged;
this.MethodDetails = new MethodDetails();
}
else
{
//if (this.MethodDetails.ActingInstanceType.FullName.Equals())
if(!this.IsShareParam
&& CacheMethodDetails is not null
&& targetNode.MethodDetails is not null
&& targetNode.MethodDetails.AssemblyName == CacheMethodDetails.AssemblyName
&& targetNode.MethodDetails.MethodName == CacheMethodDetails.MethodName)
{
this.MethodDetails = CacheMethodDetails;
}
else
{
if (targetNode.MethodDetails is not null)
{
CacheMethodDetails = targetNode.MethodDetails.CloneOfNode(this); // 从目标节点复制一份
targetNode.PropertyChanged += TargetNode_PropertyChanged;
this.MethodDetails = CacheMethodDetails;
}
}
}
OnPropertyChanged(nameof(MethodDetails));
}
partial void OnIsShareParamChanged(bool value)
{
if (targetNode is null || targetNode.MethodDetails is null)
{
return;
}
if (value)
{
CacheMethodDetails = targetNode.MethodDetails.CloneOfNode(this);
this.MethodDetails = CacheMethodDetails;
}
else
{
if(targetNode.ControlType == NodeControlType.Script)
{
// 脚本节点入参需不可编辑入参数量、入参名称
foreach (var item in CacheMethodDetails.ParameterDetailss)
{
item.IsParams = false;
}
}
this.MethodDetails = CacheMethodDetails;
}
OnPropertyChanged(nameof(MethodDetails));
}
private void TargetNode_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// 如果不再公开
if (sender is NodeModelBase node && !node.IsPublic)
{
foreach (ConnectionInvokeType ctType in NodeStaticConfig.ConnectionTypes)
{
this.SuccessorNodes[ctType] = [];
}
targetNode.PropertyChanged -= TargetNode_PropertyChanged;
}
}
/// <summary>
/// 从节点Guid刷新实体
/// </summary>
/// <returns></returns>
private bool UploadTargetNode()
{
if (targetNode is null)
{
if (string.IsNullOrWhiteSpace(TargetNodeGuid))
{
return false;
}
if (!Env.TryGetNodeModel(TargetNodeGuid, out var targetNode) || targetNode is null)
{
return false;
}
this.targetNode = targetNode;
}
return true;
}
/// <summary>
/// 保存全局变量的数据
/// </summary>
/// <param name="nodeInfo"></param>
/// <returns></returns>
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.TargetNodeGuid = targetNode?.Guid; // 变量名称
data.IsShareParam = IsShareParam;
nodeInfo.CustomData = data;
return nodeInfo;
}
/// <summary>
/// 加载全局变量的数据
/// </summary>
/// <param name="nodeInfo"></param>
public override void LoadCustomData(NodeInfo nodeInfo)
{
CacheMethodDetails = this.MethodDetails; // 缓存
string targetNodeGuid = nodeInfo.CustomData?.TargetNodeGuid ?? "";
this.IsShareParam = nodeInfo.CustomData?.IsShareParam;
if (Env.TryGetNodeModel(targetNodeGuid, out var targetNode))
{
TargetNodeGuid = targetNode.Guid;
this.targetNode = targetNode;
}
else
{
SereinEnv.WriteLine(InfoType.ERROR, $"流程接口节点[{this.Guid}]无法找到对应的节点:{targetNodeGuid}");
}
}
/*public override void Remove()
{
var tmp = this;
targetNode = null;
CacheMethodDetails = null;
}
*/
/// <summary>
/// 需要调用其它流程图中的某个节点
/// </summary>
/// <param name="context"></param>
/// <param name="token"></param>
/// <returns></returns>
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (!UploadTargetNode())
{
throw new ArgumentNullException();
}
if (IsShareParam)
{
this.MethodDetails = targetNode.MethodDetails;
}
this.SuccessorNodes = targetNode.SuccessorNodes;
FlowResult flowData = await (targetNode.ControlType switch
{
NodeControlType.Script => ((SingleScriptNode)targetNode).ExecutingAsync(this, context, token),
_ => base.ExecutingAsync(context, token)
});
if (IsShareParam)
{
// 设置运行时上一节点
// 此处代码与SereinFlow.Library.FlowNode.ParameterDetails
// ToMethodArgData()方法中判断流程接口节点分支逻辑耦合
// 不要轻易修改
context.AddOrUpdate(targetNode, flowData);
foreach (ConnectionInvokeType ctType in NodeStaticConfig.ConnectionTypes)
{
if (this.SuccessorNodes[ctType] == null) continue;
foreach (var node in this.SuccessorNodes[ctType])
{
if (node.DebugSetting.IsEnable)
{
context.SetPreviousNode(node, this);
}
}
}
}
return flowData;
}
}
}

View File

@@ -0,0 +1,178 @@
using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Model
{
/// <summary>
/// Expression Operation - 表达式操作
/// </summary>
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleGlobalDataNode : NodeModelBase
{
/// <summary>
/// 表达式
/// </summary>
[PropertyInfo(IsNotification = true)]
private string _keyName;
}
/// <summary>
/// 全局数据节点
/// </summary>
public partial class SingleGlobalDataNode : NodeModelBase, INodeContainer
{
/// <summary>
/// 全局数据节点是基础节点
/// </summary>
public override bool IsBase => true;
/// <summary>
/// 数据源只允许放置1个节点。
/// </summary>
public override int MaxChildrenCount => 1;
public SingleGlobalDataNode(IFlowEnvironment environment) : base(environment)
{
}
/// <summary>
/// 数据来源的节点
/// </summary>
private IFlowNode? DataNode;
/// <summary>
/// 有节点被放置
/// </summary>
/// <param name="nodeModel"></param>
/// <returns></returns>
public bool PlaceNode(IFlowNode nodeModel)
{
if(DataNode is null)
{
// 放置节点
nodeModel.ContainerNode = this;
ChildrenNode.Add(nodeModel);
DataNode = nodeModel;
return true;
}
else
{
// 全局数据节点只有一个子控件
return false;
}
}
public bool TakeOutNode(IFlowNode nodeModel)
{
if (ChildrenNode.Contains(nodeModel))
{
ChildrenNode.Remove(nodeModel);
nodeModel.ContainerNode = null;
DataNode = null;
return true;
}
else
{
return false;
}
}
public async void TakeOutAll()
{
foreach (var nodeModel in ChildrenNode)
{
nodeModel.Env.TakeOutNodeToContainer(nodeModel.CanvasDetails.Guid, nodeModel.Guid);
}
DataNode = null;
}
/// <summary>
/// 设置全局数据
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested) return new FlowResult(this, context);
if (string.IsNullOrEmpty(KeyName))
{
context.NextOrientation = ConnectionInvokeType.IsError;
SereinEnv.WriteLine(InfoType.ERROR, $"全局数据的KeyName不能为空[{this.Guid}]");
return new FlowResult(this, context);
}
if (DataNode is null)
{
context.NextOrientation = ConnectionInvokeType.IsError;
SereinEnv.WriteLine(InfoType.ERROR, $"全局数据节点没有设置数据来源[{this.Guid}]");
return new FlowResult(this, context);
}
try
{
var result = await DataNode.ExecutingAsync(context, token);
SereinEnv.AddOrUpdateFlowGlobalData(KeyName, result.Value);
return result;
}
catch (Exception ex)
{
context.NextOrientation = ConnectionInvokeType.IsError;
context.ExceptionOfRuning = ex;
return new FlowResult(this, context);
}
}
/// <summary>
/// 保存全局变量的数据
/// </summary>
/// <param name="nodeInfo"></param>
/// <returns></returns>
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.KeyName = KeyName; // 变量名称
nodeInfo.CustomData = data;
return nodeInfo;
}
/// <summary>
/// 加载全局变量的数据
/// </summary>
/// <param name="nodeInfo"></param>
public override void LoadCustomData(NodeInfo nodeInfo)
{
KeyName = nodeInfo.CustomData?.KeyName;
}
/* /// <summary>
/// 需要移除数据节点
/// </summary>
public override void Remove()
{
if (DataNode is null) {
return;
}
// 移除数据节点
_ = this.Env.RemoveNodeAsync(DataNode.CanvasDetails.Guid, DataNode.Guid);
}*/
}
}

View File

@@ -0,0 +1,122 @@
using Serein.Library;
using Serein.Library.Api;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Model
{
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleNetScriptNode : NodeModelBase
{
/// <summary>
/// 脚本代码
/// </summary>
[PropertyInfo(IsNotification = true)]
private string _script;
/// <summary>
/// 功能提示
/// </summary>
[PropertyInfo(IsNotification = true)]
private string _tips = "写一下提示吧";
/// <summary>
/// 依赖路径
/// </summary>
[PropertyInfo(IsNotification = true)]
private List<string> _libraryFilePaths;
}
public partial class SingleNetScriptNode
{
/// <summary>
/// 表达式节点是基础节点
/// </summary>
public override bool IsBase => true;
public SingleNetScriptNode(IFlowEnvironment environment) : base(environment)
{
this.Env = environment;
}
public override void OnCreating()
{
//MethodInfo? method = this.GetType().GetMethod(nameof(GetFlowApi));
//if (method != null)
//{
// ScriptInterpreter.AddFunction(nameof(GetFlowApi), method, () => this); // 挂载获取流程接口
//}
//var md = MethodDetails;
//var pd = md.ParameterDetailss ??= new ParameterDetails[1];
//md.ParamsArgIndex = 0;
//pd[0] = new ParameterDetails
//{
// Index = 0,
// Name = "object",
// IsExplicitData = true,
// DataValue = string.Empty,
// DataType = typeof(object),
// ExplicitType = typeof(object),
// ArgDataSourceNodeGuid = string.Empty,
// ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
// NodeModel = this,
// InputType = ParameterValueInputType.Input,
// Items = null,
// IsParams = true,
// Description = "脚本节点入参"
//};
}
/// <summary>
/// 导出脚本代码
/// </summary>
/// <param name="nodeInfo"></param>
/// <returns></returns>
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.Script = this.Script ?? "";
nodeInfo.CustomData = data;
return nodeInfo;
}
/// <summary>
/// 加载自定义数据
/// </summary>
/// <param name="nodeInfo"></param>
public override void LoadCustomData(NodeInfo nodeInfo)
{
this.Script = nodeInfo.CustomData?.Script ?? "";
// 更新变量名
//for (int i = 0; i < Math.Min(this.MethodDetails.ParameterDetailss.Length, nodeInfo.ParameterData.Length); i++)
//{
// this.MethodDetails.ParameterDetailss[i].Name = nodeInfo.ParameterData[i].ArgName;
//}
}
}
}

View File

@@ -0,0 +1,310 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Script;
using Serein.Script.Node;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Serein.NodeFlow.Model
{
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleScriptNode : NodeModelBase
{
[PropertyInfo(IsNotification = true)]
private string _script;
}
/// <summary>
/// 流程脚本节点
/// </summary>
public partial class SingleScriptNode : NodeModelBase
{
/// <summary>
/// 脚本节点是基础节点
/// </summary>
public override bool IsBase => true;
private IScriptFlowApi ScriptFlowApi;
private ASTNode mainNode;
private SereinScriptInterpreter ScriptInterpreter;
private bool IsScriptChanged = false;
/// <summary>
/// 构建流程脚本节点
/// </summary>
/// <param name="environment"></param>
public SingleScriptNode(IFlowEnvironment environment):base(environment)
{
ScriptFlowApi = new ScriptFlowApi(environment, this);
ScriptInterpreter = new SereinScriptInterpreter();
}
static SingleScriptNode()
{
// 挂载静态方法
var tempMethods = typeof(BaseFunc).GetMethods().Where(method =>
!(method.Name.Equals("GetHashCode")
|| method.Name.Equals("Equals")
|| method.Name.Equals("ToString")
|| method.Name.Equals("GetType")
)).Select(method => (method.Name, method)).ToArray();
// 加载基础方法
foreach ((string name, MethodInfo method) item in tempMethods)
{
SereinScriptInterpreter.AddStaticFunction(item.name, item.method);
}
}
/// <summary>
/// 代码改变后
/// </summary>
/// <param name="value"></param>
/// <exception cref="NotImplementedException"></exception>
partial void OnScriptChanged(string value)
{
IsScriptChanged = true;
}
/// <summary>
/// 节点创建时
/// </summary>
public override void OnCreating()
{
MethodInfo? method = this.GetType().GetMethod(nameof(GetFlowApi));
if (method != null)
{
ScriptInterpreter.AddFunction(nameof(GetFlowApi), method, () => this); // 挂载获取流程接口
}
var md = MethodDetails;
var pd = md.ParameterDetailss ??= new ParameterDetails[1];
md.ParamsArgIndex = 0;
pd[0] = new ParameterDetails
{
Index = 0,
Name = "object",
IsExplicitData = true,
DataValue = string.Empty,
DataType = typeof(object),
ExplicitType = typeof(object),
ArgDataSourceNodeGuid = string.Empty,
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
NodeModel = this,
InputType = ParameterValueInputType.Input,
Items = null,
IsParams = true,
//Description = "脚本节点入参"
};
}
/// <summary>
/// 导出脚本代码
/// </summary>
/// <param name="nodeInfo"></param>
/// <returns></returns>
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.Script = Script ?? "";
nodeInfo.CustomData = data;
return nodeInfo;
}
/// <summary>
/// 加载自定义数据
/// </summary>
/// <param name="nodeInfo"></param>
public override void LoadCustomData(NodeInfo nodeInfo)
{
this.Script = nodeInfo.CustomData?.Script ?? "";
// 更新变量名
for (int i = 0; i < Math.Min(this.MethodDetails.ParameterDetailss.Length, nodeInfo.ParameterData.Length); i++)
{
this.MethodDetails.ParameterDetailss[i].Name = nodeInfo.ParameterData[i].ArgName;
}
}
/// <summary>
/// 重新加载脚本代码
/// </summary>
public void ReloadScript()
{
try
{
HashSet<string> varNames = new HashSet<string>();
foreach (var pd in MethodDetails.ParameterDetailss)
{
if (varNames.Contains(pd.Name))
{
throw new Exception($"脚本节点重复的变量名称:{pd.Name} - {Guid}");
}
varNames.Add(pd.Name);
}
StringBuilder sb = new StringBuilder();
foreach (var pd in MethodDetails.ParameterDetailss)
{
sb.AppendLine($"let {pd.Name};"); // 提前声明这些变量
}
sb.Append(Script);
var p = new SereinScriptParser(sb.ToString());
//var p = new SereinScriptParser(Script);
mainNode = p.Parse(); // 开始解析
}
catch (Exception ex)
{
SereinEnv.WriteLine(InfoType.ERROR, ex.ToString());
}
}
/// <summary>
/// 执行脚本
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
return await ExecutingAsync(this, context, token);
}
/// <summary>
/// 流程接口提供参数进行调用脚本节点
/// </summary>
/// <param name="flowCallNode"></param>
/// <param name="context"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<FlowResult> ExecutingAsync(NodeModelBase flowCallNode, IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested) return new FlowResult(this, context);
var @params = await flowCallNode.GetParametersAsync(context, token);
if (token.IsCancellationRequested) return new FlowResult(this, context);
//context.AddOrUpdate($"{context.Guid}_{this.Guid}_Params", @params[0]); // 后面再改
if (IsScriptChanged)
{
lock (@params) {
if (IsScriptChanged)
{
ReloadScript();// 每次都重新解析
IsScriptChanged = false;
}
}
}
IScriptInvokeContext scriptContext = new ScriptInvokeContext(context);
if (@params[0] is object[] agrDatas)
{
for (int i = 0; i < agrDatas.Length; i++)
{
var argName = flowCallNode.MethodDetails.ParameterDetailss[i].Name;
var argData = agrDatas[i];
scriptContext.SetVarValue(argName, argData);
}
}
FlowRunCompleteHandler onFlowStop = (e) =>
{
scriptContext.OnExit();
};
var envEvent = context.Env.Event;
envEvent.FlowRunComplete += onFlowStop; // 防止运行后台流程
if (token.IsCancellationRequested) return null;
var result = await ScriptInterpreter.InterpretAsync(scriptContext, mainNode); // 从入口节点执行
envEvent.FlowRunComplete -= onFlowStop;
return new FlowResult(this, context, result);
}
#region
public IScriptFlowApi GetFlowApi()
{
return ScriptFlowApi;
}
private static class BaseFunc
{
public static DateTime GetNow() => DateTime.Now;
#region
public static bool BoolOf(object value)
{
return ConvertHelper.ValueParse<bool>(value);
}
public static int IntOf(object value)
{
return ConvertHelper.ValueParse<int>(value);
}
public static int LongOf(object value)
{
return ConvertHelper.ValueParse<int>(value);
}
#endregion
public static Type TypeOf(object type)
{
return type.GetType();
}
public static void Print(object value)
{
SereinEnv.WriteLine(InfoType.INFO, value?.ToString());
}
#region
public static int ToInt(object value)
{
return int.Parse(value.ToString());
}
public static double ToDouble(object value)
{
return double.Parse(value.ToString());
}
public static bool ToBool(object value)
{
return bool.Parse(value.ToString());
}
#endregion
public static async Task Delay(object value)
{
if (value is int @int)
{
Console.WriteLine($"等待{@int}ms");
await Task.Delay(@int);
}
else if (value is TimeSpan timeSpan)
{
Console.WriteLine($"等待{timeSpan}");
await Task.Delay(timeSpan);
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,46 @@
using Serein.Library;
using Serein.Library.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Model.Node
{
public class SingleUINode : NodeModelBase
{
public IEmbeddedContent Adapter { get; private set; }
public SingleUINode(IFlowEnvironment environment) : base(environment)
{
}
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested) return new FlowResult(this,context);
if(Adapter is null)
{
var result = await base.ExecutingAsync(context, token);
if (result.Value is IEmbeddedContent adapter)
{
Adapter = adapter;
context.NextOrientation = ConnectionInvokeType.IsSucceed;
}
else
{
context.NextOrientation = ConnectionInvokeType.IsError;
}
}
else
{
var p = context.GetPreviousNode(this);
var data = context.GetFlowData(p).Value;
var iflowContorl = Adapter.GetFlowControl();
iflowContorl.OnExecuting(data);
}
return new FlowResult(this, context);
}
}
}