mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
更改了dll参数类型,更改了流程执行,添加了异常分支处理
This commit is contained in:
@@ -65,9 +65,9 @@ namespace Serein.NodeFlow
|
||||
/// <summary>
|
||||
/// 是否为显式参数
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
public class ExplicitAttribute : Attribute // where TEnum : Enum
|
||||
{
|
||||
}
|
||||
//[AttributeUsage(AttributeTargets.Parameter)]
|
||||
//public class ExplicitAttribute : Attribute // where TEnum : Enum
|
||||
//{
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Serein.Library.IOC;
|
||||
using Serein.NodeFlow.Model;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
@@ -12,23 +13,24 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
|
||||
public enum FfState
|
||||
{
|
||||
Succeed,
|
||||
Cancel,
|
||||
}
|
||||
//public enum FfState
|
||||
//{
|
||||
// Succeed,
|
||||
// Cancel,
|
||||
// Error,
|
||||
//}
|
||||
/// <summary>
|
||||
/// 触发器上下文
|
||||
/// </summary>
|
||||
public class FlipflopContext
|
||||
{
|
||||
public FfState State { get; set; }
|
||||
public FlowStateType State { get; set; }
|
||||
public object? Data { get; set; }
|
||||
/*public FlipflopContext()
|
||||
{
|
||||
State = FfState.Cancel;
|
||||
}*/
|
||||
public FlipflopContext(FfState ffState, object? data = null)
|
||||
public FlipflopContext(FlowStateType ffState, object? data = null)
|
||||
{
|
||||
State = ffState;
|
||||
Data = data;
|
||||
@@ -144,7 +146,7 @@ namespace Serein.NodeFlow
|
||||
public NodeRunTcs NodeRunCts { get; set; }
|
||||
public Task CreateTimingTask(Action action, int time = 100, int count = -1)
|
||||
{
|
||||
NodeRunCts ??= ServiceContainer.Get<NodeRunTcs>();
|
||||
NodeRunCts ??= ServiceContainer.GetOrInstantiate<NodeRunTcs>();
|
||||
return Task.Factory.StartNew(async () =>
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
|
||||
@@ -14,16 +14,22 @@
|
||||
MethodDetails ??= node.MethodDetails;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 条件节点重写执行方法
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public override object? Execute(DynamicContext context)
|
||||
{
|
||||
// bool allTrue = ConditionNodes.All(condition => Judge(context,condition.MethodDetails));
|
||||
// bool IsAllTrue = true; // 初始化为 true
|
||||
FlowState = true;
|
||||
FlowState = FlowStateType.Succeed;
|
||||
foreach (SingleConditionNode? node in ConditionNodes)
|
||||
{
|
||||
if (!Judge(context, node))
|
||||
var state = Judge(context, node);
|
||||
if (state == FlowStateType.Fail || FlowStateType.Fail == FlowStateType.Error)
|
||||
{
|
||||
FlowState = false;
|
||||
FlowState = state;
|
||||
break;// 一旦发现条件为假,立即退出循环
|
||||
}
|
||||
}
|
||||
@@ -44,7 +50,7 @@
|
||||
// }
|
||||
//}
|
||||
}
|
||||
private bool Judge(DynamicContext context, SingleConditionNode node)
|
||||
private FlowStateType Judge(DynamicContext context, SingleConditionNode node)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -54,8 +60,8 @@
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
return FlowStateType.Error;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,22 @@ namespace Serein.NodeFlow.Model
|
||||
Upstream,
|
||||
}
|
||||
|
||||
public enum FlowStateType
|
||||
{
|
||||
/// <summary>
|
||||
/// 成功(方法成功执行)
|
||||
/// </summary>
|
||||
Succeed,
|
||||
/// <summary>
|
||||
/// 失败(方法没有成功执行,不过执行时没有发生非预期的错误)
|
||||
/// </summary>
|
||||
Fail,
|
||||
/// <summary>
|
||||
/// 异常(节点没有
|
||||
/// </summary>
|
||||
Error,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||
/// </summary>
|
||||
@@ -73,21 +89,30 @@ namespace Serein.NodeFlow.Model
|
||||
/// <summary>
|
||||
/// 当前状态(进入真分支还是假分支,异常分支在异常中确定)
|
||||
/// </summary>
|
||||
public bool FlowState { get; set; } = true;
|
||||
//public ConnectionType NextType { get; set; } = ConnectionType.IsTrue;
|
||||
public FlowStateType FlowState { get; set; } = FlowStateType.Succeed;
|
||||
public Exception Exception { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 当前传递数据
|
||||
/// </summary>
|
||||
public object? FlowData { get; set; } = null;
|
||||
|
||||
|
||||
// 正常流程节点调用
|
||||
/// <summary>
|
||||
/// 执行节点对应的方法
|
||||
/// </summary>
|
||||
/// <param name="context">流程上下文</param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
public virtual object? Execute(DynamicContext context)
|
||||
{
|
||||
MethodDetails md = MethodDetails;
|
||||
object? result = null;
|
||||
if (!DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
|
||||
try
|
||||
{
|
||||
if (md.ExplicitDatas.Length == 0)
|
||||
{
|
||||
@@ -110,89 +135,76 @@ namespace Serein.NodeFlow.Model
|
||||
else
|
||||
{
|
||||
result = ((Func<object, object[], object>)del).Invoke(md.ActingInstance, parameters);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
// context.SetFlowData(result);
|
||||
// CurrentData = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FlowState = FlowStateType.Error;
|
||||
Exception = ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 触发器调用
|
||||
/// <summary>
|
||||
/// 执行等待触发器的方法
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public virtual async Task<object?> ExecuteAsync(DynamicContext context)
|
||||
{
|
||||
MethodDetails md = MethodDetails;
|
||||
object? result = null;
|
||||
|
||||
if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
|
||||
if (!DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
FlipflopContext flipflopContext = null;
|
||||
try
|
||||
{
|
||||
// 调用委托并获取结果
|
||||
if (md.ExplicitDatas.Length == 0)
|
||||
{
|
||||
// 调用委托并获取结果
|
||||
FlipflopContext flipflopContext = await ((Func<object, Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance);
|
||||
|
||||
if (flipflopContext != null)
|
||||
{
|
||||
if (flipflopContext.State == FfState.Cancel)
|
||||
{
|
||||
throw new Exception("this async task is cancel.");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (flipflopContext.State == FfState.Succeed)
|
||||
{
|
||||
FlowState = true;
|
||||
result = flipflopContext.Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
FlowState = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
flipflopContext = await ((Func<object, Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
object?[]? parameters = GetParameters(context, MethodDetails);
|
||||
// 调用委托并获取结果
|
||||
flipflopContext = await ((Func<object, object[], Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance, parameters);
|
||||
}
|
||||
|
||||
|
||||
FlipflopContext flipflopContext = await ((Func<object, object[], Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance, parameters);
|
||||
|
||||
|
||||
|
||||
if (flipflopContext != null)
|
||||
if (flipflopContext != null)
|
||||
{
|
||||
FlowState = flipflopContext.State;
|
||||
if (flipflopContext.State == FlowStateType.Succeed)
|
||||
{
|
||||
if (flipflopContext.State == FfState.Cancel)
|
||||
{
|
||||
throw new Exception("取消此异步");
|
||||
}
|
||||
else
|
||||
{
|
||||
FlowState = flipflopContext.State == FfState.Succeed;
|
||||
result = flipflopContext.Data;
|
||||
}
|
||||
result = flipflopContext.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FlowState = FlowStateType.Error;
|
||||
Exception = ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task ExecuteStack(DynamicContext context)
|
||||
/// <summary>
|
||||
/// 开始执行
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public async Task StartExecution(DynamicContext context)
|
||||
{
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
await ExecuteStackTmp(context);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task ExecuteStackTmp(DynamicContext context)
|
||||
{
|
||||
var cts = context.ServiceContainer.Get<CancellationTokenSource>();
|
||||
var cts = context.ServiceContainer.GetOrInstantiate<CancellationTokenSource>();
|
||||
|
||||
Stack<NodeBase> stack = [];
|
||||
stack.Push(this);
|
||||
@@ -202,32 +214,38 @@ namespace Serein.NodeFlow.Model
|
||||
// 从栈中弹出一个节点作为当前节点进行处理
|
||||
var currentNode = stack.Pop();
|
||||
|
||||
// 设置方法执行的对象
|
||||
if (currentNode.MethodDetails != null)
|
||||
{
|
||||
currentNode.MethodDetails.ActingInstance ??= context.ServiceContainer.Get(MethodDetails.ActingInstanceType);
|
||||
} // 设置方法执行的对象
|
||||
currentNode.MethodDetails.ActingInstance ??= context.ServiceContainer.GetOrInstantiate(MethodDetails.ActingInstanceType);
|
||||
}
|
||||
|
||||
// 获取上游分支,首先执行一次
|
||||
var upstreamNodes = currentNode.UpstreamBranch;
|
||||
for (int i = upstreamNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
upstreamNodes[i].PreviousNode = currentNode;
|
||||
await upstreamNodes[i].ExecuteStack(context);
|
||||
await upstreamNodes[i].StartExecution(context);
|
||||
}
|
||||
|
||||
|
||||
if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == DynamicNodeType.Flipflop)
|
||||
{
|
||||
// 触发器节点
|
||||
currentNode.FlowData = await currentNode.ExecuteAsync(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 动作节点
|
||||
currentNode.FlowData = currentNode.Execute(context);
|
||||
}
|
||||
|
||||
|
||||
var nextNodes = currentNode.FlowState ? currentNode.SucceedBranch
|
||||
: currentNode.FailBranch;
|
||||
var nextNodes = currentNode.FlowState switch
|
||||
{
|
||||
FlowStateType.Succeed => currentNode.SucceedBranch,
|
||||
FlowStateType.Fail => currentNode.FailBranch,
|
||||
FlowStateType.Error => currentNode.ErrorBranch,
|
||||
_ => throw new Exception("非预期的枚举值")
|
||||
};
|
||||
|
||||
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||
@@ -238,7 +256,9 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取对应的参数数组
|
||||
/// </summary>
|
||||
public object[]? GetParameters(DynamicContext context, MethodDetails md)
|
||||
{
|
||||
// 用正确的大小初始化参数数组
|
||||
@@ -355,7 +375,12 @@ namespace Serein.NodeFlow.Model
|
||||
return parameters;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// json文本反序列化为对象
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="targetType"></param>
|
||||
/// <returns></returns>
|
||||
private dynamic? ConvertValue(string value, Type targetType)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -36,7 +36,17 @@ namespace Serein.NodeFlow.Model
|
||||
{
|
||||
result = PreviousNode?.FlowData;
|
||||
}
|
||||
FlowState = SerinConditionParser.To(result, Expression);
|
||||
try
|
||||
{
|
||||
var isPass = SerinConditionParser.To(result, Expression);
|
||||
FlowState = isPass ? FlowStateType.Succeed : FlowStateType.Fail;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FlowState = FlowStateType.Error;
|
||||
Exception = ex;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{result} {Expression} -> " + FlowState);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Serein.NodeFlow.Model
|
||||
|
||||
var newData = SerinExpressionEvaluator.Evaluate(Expression, data, out bool isChange);
|
||||
|
||||
FlowState = true;
|
||||
FlowState = FlowStateType.Succeed;
|
||||
Console.WriteLine(newData);
|
||||
if (isChange)
|
||||
{
|
||||
|
||||
@@ -102,7 +102,10 @@ namespace Serein.NodeFlow
|
||||
|
||||
try
|
||||
{
|
||||
await Task.WhenAll([startNode.ExecuteStack(context), .. tasks]);
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
await Task.WhenAll([startNode.StartExecution(context), .. tasks]);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -133,24 +136,27 @@ namespace Serein.NodeFlow
|
||||
|
||||
FlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);
|
||||
|
||||
|
||||
if (flipflopContext == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (flipflopContext.State == FfState.Cancel)
|
||||
else if (flipflopContext.State == FlowStateType.Error)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (flipflopContext.State == FfState.Succeed)
|
||||
else if (flipflopContext.State == FlowStateType.Fail)
|
||||
{
|
||||
singleFlipFlopNode.FlowState = true;
|
||||
break;
|
||||
}
|
||||
else if (flipflopContext.State == FlowStateType.Succeed)
|
||||
{
|
||||
singleFlipFlopNode.FlowState = FlowStateType.Succeed;
|
||||
singleFlipFlopNode.FlowData = flipflopContext.Data;
|
||||
var tasks = singleFlipFlopNode.SucceedBranch.Select(nextNode =>
|
||||
{
|
||||
var context = new DynamicContext(ServiceContainer);
|
||||
nextNode.PreviousNode = singleFlipFlopNode;
|
||||
return nextNode.ExecuteStack(context);
|
||||
return nextNode.StartExecution(context);
|
||||
}).ToArray();
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ public static class DelegateGenerator
|
||||
|
||||
return new ExplicitData
|
||||
{
|
||||
IsExplicitData = it.GetCustomAttribute(typeof(ExplicitAttribute)) is ExplicitAttribute,
|
||||
IsExplicitData = it.HasDefaultValue,
|
||||
Index = index,
|
||||
ExplicitType = it.ParameterType,
|
||||
ExplicitTypeName = explicitTypeName,
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Serein.NodeFlow;
|
||||
using Serein.NodeFlow.Model;
|
||||
|
||||
namespace Serein.NodeFlow.Tool
|
||||
{
|
||||
public class TcsSignalException : Exception
|
||||
{
|
||||
public FfState FfState { get; set; }
|
||||
public FlowStateType FsState { get; set; }
|
||||
public TcsSignalException(string? message) : base(message)
|
||||
{
|
||||
FfState = FfState.Cancel;
|
||||
FsState = FlowStateType.Error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user