mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
实现了拖拽式设置方法调用顺序、方法入参参数来源
This commit is contained in:
@@ -6,6 +6,7 @@ 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;
|
||||
@@ -24,22 +25,6 @@ namespace Serein.Library
|
||||
/// </summary>
|
||||
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||
{
|
||||
|
||||
|
||||
#region 调试中断
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 不再中断
|
||||
/// </summary>
|
||||
public void CancelInterrupt()
|
||||
{
|
||||
this.DebugSetting.InterruptClass = InterruptClass.None;
|
||||
DebugSetting.CancelInterruptCallback?.Invoke();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 导出/导入项目文件节点信息
|
||||
|
||||
/// <summary>
|
||||
@@ -56,10 +41,10 @@ namespace Serein.Library
|
||||
{
|
||||
// if (MethodDetails == null) return null;
|
||||
|
||||
var trueNodes = SuccessorNodes[ConnectionType.IsSucceed].Select(item => item.Guid); // 真分支
|
||||
var falseNodes = SuccessorNodes[ConnectionType.IsFail].Select(item => item.Guid);// 假分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 异常分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 上游分支
|
||||
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 = GetParameterdatas();
|
||||
@@ -86,7 +71,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public virtual NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||
{
|
||||
this.Guid = nodeInfo.Guid;
|
||||
this.Guid = nodeInfo.Guid;
|
||||
|
||||
if (nodeInfo.Position is null)
|
||||
{
|
||||
@@ -104,6 +89,19 @@ namespace Serein.Library
|
||||
}
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 调试中断
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 不再中断
|
||||
/// </summary>
|
||||
public void CancelInterrupt()
|
||||
{
|
||||
this.DebugSetting.InterruptClass = InterruptClass.None;
|
||||
DebugSetting.CancelInterruptCallback?.Invoke();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -137,8 +135,6 @@ namespace Serein.Library
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 开始执行
|
||||
/// </summary>
|
||||
@@ -163,7 +159,7 @@ namespace Serein.Library
|
||||
var currentNode = stack.Pop();
|
||||
|
||||
// 筛选出上游分支
|
||||
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream].ToArray();
|
||||
var upstreamNodes = currentNode.SuccessorNodes[ConnectionInvokeType.Upstream].ToArray();
|
||||
for (int index = 0; index < upstreamNodes.Length; index++)
|
||||
{
|
||||
NodeModelBase upstreamNode = upstreamNodes[index];
|
||||
@@ -176,7 +172,7 @@ namespace Serein.Library
|
||||
}
|
||||
upstreamNode.PreviousNode = currentNode;
|
||||
await upstreamNode.StartFlowAsync(context); // 执行流程节点的上游分支
|
||||
if (upstreamNode.NextOrientation == ConnectionType.IsError)
|
||||
if (context.NextOrientation == ConnectionInvokeType.IsError)
|
||||
{
|
||||
// 如果上游分支执行失败,不再继续执行
|
||||
// 使上游节点(仅上游节点本身,不包含上游节点的后继节点)
|
||||
@@ -197,7 +193,7 @@ namespace Serein.Library
|
||||
#region 执行完成
|
||||
|
||||
// 选择后继分支
|
||||
var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation];
|
||||
var nextNodes = currentNode.SuccessorNodes[context.NextOrientation];
|
||||
|
||||
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||
@@ -215,7 +211,6 @@ namespace Serein.Library
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行节点对应的方法
|
||||
/// </summary>
|
||||
@@ -234,7 +229,6 @@ namespace Serein.Library
|
||||
#endregion
|
||||
|
||||
MethodDetails md = MethodDetails;
|
||||
//var del = md.MethodDelegate.Clone();
|
||||
if (md is null)
|
||||
{
|
||||
throw new Exception($"节点{this.Guid}不存在方法信息,请检查是否需要重写节点的ExecutingAsync");
|
||||
@@ -247,35 +241,70 @@ namespace Serein.Library
|
||||
{
|
||||
md.ActingInstance = context.Env.IOC.Get(md.ActingInstanceType);
|
||||
}
|
||||
// md.ActingInstance ??= context.Env.IOC.Get(md.ActingInstanceType);
|
||||
object instance = md.ActingInstance;
|
||||
|
||||
|
||||
object result = null;
|
||||
|
||||
try
|
||||
{
|
||||
object[] args = GetParameters(context, this, md);
|
||||
result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||
NextOrientation = ConnectionType.IsSucceed;
|
||||
object[] args = await GetParametersAsync(context, this, md);
|
||||
var result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||
context.NextOrientation = ConnectionInvokeType.IsSucceed;
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync($"节点[{this.MethodDetails?.MethodName}]异常:" + ex);
|
||||
NextOrientation = ConnectionType.IsError;
|
||||
context.NextOrientation = ConnectionInvokeType.IsError;
|
||||
RuningException = ex;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行单个节点对应的方法,并不做状态检查
|
||||
/// </summary>
|
||||
/// <param name="env"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<object> InvokeAsync(IFlowEnvironment env)
|
||||
{
|
||||
try
|
||||
{
|
||||
MethodDetails md = MethodDetails;
|
||||
if (md is null)
|
||||
{
|
||||
throw new Exception($"不存在方法信息{md.MethodName}");
|
||||
}
|
||||
if (!env.TryGetDelegateDetails(md.MethodName, out var dd))
|
||||
{
|
||||
throw new Exception($"不存在对应委托{md.MethodName}");
|
||||
}
|
||||
if (md.ActingInstance is null)
|
||||
{
|
||||
md.ActingInstance = env.IOC.Get(md.ActingInstanceType);
|
||||
if (md.ActingInstance is null)
|
||||
{
|
||||
md.ActingInstance = env.IOC.Instantiate(md.ActingInstanceType);
|
||||
if (md.ActingInstance is null)
|
||||
{
|
||||
throw new Exception($"无法创建相应的实例{md.ActingInstanceType.FullName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object[] args = await GetParametersAsync(null, this, md);
|
||||
var result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync($"节点[{this.MethodDetails?.MethodName}]异常:" + ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对应的参数数组
|
||||
/// </summary>
|
||||
public static object[] GetParameters(IDynamicContext context, NodeModelBase nodeModel, MethodDetails md)
|
||||
public static async Task<object[]> GetParametersAsync(IDynamicContext context, NodeModelBase nodeModel, MethodDetails md)
|
||||
{
|
||||
await Task.Delay(0);
|
||||
// 用正确的大小初始化参数数组
|
||||
if (md.ParameterDetailss.Length == 0)
|
||||
{
|
||||
@@ -283,22 +312,34 @@ namespace Serein.Library
|
||||
}
|
||||
|
||||
object[] parameters = new object[md.ParameterDetailss.Length];
|
||||
var flowData = nodeModel.PreviousNode?.FlowData; // 当前传递的数据
|
||||
var previousDataType = flowData?.GetType();
|
||||
var previousFlowData = nodeModel.PreviousNode?.FlowData; // 当前传递的数据
|
||||
var previousDataType = previousFlowData?.GetType(); // 当前传递数据的类型
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
|
||||
object inputParameter; // 存放解析的临时参数
|
||||
var ed = md.ParameterDetailss[i]; // 方法入参描述
|
||||
|
||||
#region 获取基础的上下文数据
|
||||
if (ed.DataType == typeof(IFlowEnvironment)) // 获取流程上下文
|
||||
{
|
||||
parameters[i] = nodeModel.Env;
|
||||
continue;
|
||||
}
|
||||
if (ed.DataType == typeof(IDynamicContext)) // 获取流程上下文
|
||||
{
|
||||
parameters[i] = context;
|
||||
continue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 确定[预入参]数据
|
||||
object inputParameter; // 存放解析的临时参数
|
||||
if (ed.IsExplicitData) // 判断是否使用显示的输入参数
|
||||
{
|
||||
if (ed.DataValue.StartsWith("@get", StringComparison.OrdinalIgnoreCase) && !(flowData is null))
|
||||
if (ed.DataValue.StartsWith("@get", StringComparison.OrdinalIgnoreCase) && !(previousFlowData is null))
|
||||
{
|
||||
// 执行表达式从上一节点获取对象
|
||||
inputParameter = SerinExpressionEvaluator.Evaluate(ed.DataValue, flowData, out _);
|
||||
inputParameter = SerinExpressionEvaluator.Evaluate(ed.DataValue, previousFlowData, out _);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -308,9 +349,31 @@ namespace Serein.Library
|
||||
}
|
||||
else
|
||||
{
|
||||
inputParameter = flowData; // 使用上一节点的对象
|
||||
}
|
||||
if (ed.ArgDataSourceType == ConnectionArgSourceType.GetPreviousNodeData)
|
||||
{
|
||||
inputParameter = previousFlowData; // 使用运行时上一节点的返回值
|
||||
}
|
||||
else if (ed.ArgDataSourceType == ConnectionArgSourceType.GetPreviousNodeData)
|
||||
{
|
||||
// 获取指定节点的数据
|
||||
// 如果指定节点没有被执行,会返回null
|
||||
// 如果执行过,会获取上一次执行结果作为预入参数据
|
||||
inputParameter = ed.ArgDataSourceNodeMoels[i].FlowData;
|
||||
}
|
||||
else if (ed.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeDataOfInvoke)
|
||||
{
|
||||
// 立刻调用对应节点获取数据。
|
||||
var result = await ed.ArgDataSourceNodeMoels[i].InvokeAsync(nodeModel.Env);
|
||||
inputParameter = result;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("节点执行方法获取入参参数时,ConnectionArgSourceType枚举是意外的枚举值");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 入参存在取值转换器,调用对应的转换器获取入参数据
|
||||
// 入参存在取值转换器
|
||||
if (ed.ExplicitType.IsEnum && !(ed.Convertor is null))
|
||||
{
|
||||
@@ -327,13 +390,11 @@ namespace Serein.Library
|
||||
parameters[i] = value;
|
||||
continue;
|
||||
}
|
||||
//if (Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum))
|
||||
//{
|
||||
|
||||
//}
|
||||
}
|
||||
#endregion
|
||||
|
||||
// 入参存在类型转换器,获取枚举转换器中记录的枚举
|
||||
#region 入参存在基于BinValue的类型转换器,获取枚举转换器中记录的类型
|
||||
// 入参存在基于BinValue的类型转换器,获取枚举转换器中记录的类型
|
||||
if (ed.ExplicitType.IsEnum && ed.DataType != ed.ExplicitType)
|
||||
{
|
||||
var resultEnum = Enum.Parse(ed.ExplicitType, ed.DataValue);
|
||||
@@ -341,7 +402,7 @@ namespace Serein.Library
|
||||
var type = EnumHelper.GetBoundValue(ed.ExplicitType, resultEnum, attr => attr.Value);
|
||||
if (type is Type enumBindType && !(enumBindType is null))
|
||||
{
|
||||
var value = context.Env.IOC.Instantiate(enumBindType);
|
||||
var value = nodeModel.Env.IOC.Instantiate(enumBindType);
|
||||
if (value is null)
|
||||
{
|
||||
|
||||
@@ -351,62 +412,83 @@ namespace Serein.Library
|
||||
parameters[i] = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 对入参数据尝试进行转换
|
||||
|
||||
if (ed.DataType.IsValueType)
|
||||
if (inputParameter.GetType() == ed.DataType)
|
||||
{
|
||||
var valueStr = inputParameter?.ToString();
|
||||
parameters[i] = valueStr.ToValueData(ed.DataType);
|
||||
parameters[i] = inputParameter; // 类型一致无需转换,直接装入入参数组
|
||||
}
|
||||
else
|
||||
else if (ed.DataType.IsValueType)
|
||||
{
|
||||
// 值类型
|
||||
var valueStr = inputParameter?.ToString();
|
||||
if (ed.DataType == typeof(string))
|
||||
parameters[i] = valueStr.ToValueData(ed.DataType); // 类型不一致,尝试进行转换,如果转换失败返回类型对应的默认值
|
||||
}
|
||||
else
|
||||
{
|
||||
// 引用类型
|
||||
if (ed.DataType == typeof(string)) // 转为字符串
|
||||
{
|
||||
var valueStr = inputParameter?.ToString();
|
||||
parameters[i] = valueStr;
|
||||
}
|
||||
else if (ed.DataType == typeof(IDynamicContext))
|
||||
else if(ed.DataType.IsSubclassOf(inputParameter.GetType())) // 入参类型 是 预入参数据类型 的 子类/实现类
|
||||
{
|
||||
parameters[i] = context;
|
||||
// 方法入参中,父类不能隐式转为子类,这里需要进行强制转换
|
||||
parameters[i] = ObjectConvertHelper.ConvertParentToChild(inputParameter, ed.DataType);
|
||||
}
|
||||
else if (ed.DataType == typeof(MethodDetails))
|
||||
{
|
||||
parameters[i] = md;
|
||||
}
|
||||
else if (ed.DataType == typeof(NodeModelBase))
|
||||
{
|
||||
parameters[i] = nodeModel;
|
||||
}
|
||||
else
|
||||
else if(ed.DataType.IsAssignableFrom(inputParameter.GetType())) // 入参类型 是 预入参数据类型 的 父类/接口
|
||||
{
|
||||
parameters[i] = inputParameter;
|
||||
}
|
||||
// 集合类型
|
||||
else if(inputParameter is IEnumerable collection)
|
||||
{
|
||||
var enumerableMethods = typeof(Enumerable).GetMethods(); // 获取所有的 Enumerable 扩展方法
|
||||
MethodInfo conversionMethod;
|
||||
if (ed.DataType.IsArray) // 转为数组
|
||||
{
|
||||
parameters[i] = inputParameter;
|
||||
conversionMethod = enumerableMethods.FirstOrDefault(m => m.Name == "ToArray" && m.IsGenericMethodDefinition);
|
||||
}
|
||||
else if (ed.DataType.GetGenericTypeDefinition() == typeof(List<>)) // 转为集合
|
||||
{
|
||||
conversionMethod = enumerableMethods.FirstOrDefault(m => m.Name == "ToList" && m.IsGenericMethodDefinition);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("输入对象不是集合或目标类型不支持(目前仅支持Array、List的自动转换)");
|
||||
}
|
||||
var genericMethod = conversionMethod.MakeGenericMethod(ed.DataType);
|
||||
var result = genericMethod.Invoke(null, new object[] { collection });
|
||||
parameters[i] = result;
|
||||
}
|
||||
|
||||
|
||||
//parameters[i] = ed.DataType switch
|
||||
|
||||
//else if (ed.DataType == typeof(MethodDetails)) // 希望获取节点对应的方法描述,好像没啥用
|
||||
//{
|
||||
// Type t when t == typeof(string) => valueStr,
|
||||
// Type t when t == typeof(IDynamicContext) => context, // 上下文
|
||||
// Type t when t == typeof(DateTime) => string.IsNullOrEmpty(valueStr) ? null : DateTime.Parse(valueStr),
|
||||
|
||||
// Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||
// Type t when t == typeof(NodeModelBase) => nodeModel, // 节点实体类
|
||||
|
||||
// Type t when t.IsArray => (inputParameter as Array)?.Cast<object>().ToList(),
|
||||
// Type t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => inputParameter,
|
||||
// _ => inputParameter,
|
||||
//};
|
||||
}
|
||||
// parameters[i] = md;
|
||||
//}
|
||||
//else if (ed.DataType == typeof(NodeModelBase)) // 希望获取方法生成的节点,好像没啥用
|
||||
//{
|
||||
// parameters[i] = nodeModel;
|
||||
//}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新节点数据,并检查监视表达式是否生效
|
||||
/// </summary>
|
||||
@@ -479,7 +561,6 @@ namespace Serein.Library
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 释放对象
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user