2024-09-12 20:32:54 +08:00
|
|
|
|
using Serein.Library.Api;
|
2024-10-14 17:29:28 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library.Framework.NodeFlow
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 动态流程上下文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class DynamicContext : IDynamicContext
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public DynamicContext(/*ISereinIOC sereinIoc,*/ IFlowEnvironment flowEnvironment)
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
// SereinIoc = sereinIoc;
|
|
|
|
|
|
Env = flowEnvironment;
|
2024-10-14 17:29:28 +08:00
|
|
|
|
RunState = RunState.Running;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行环境
|
|
|
|
|
|
/// </summary>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public IFlowEnvironment Env { get; }
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public RunState RunState { get; set; } = RunState.NoStart;
|
2024-10-24 23:32:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前节点执行完成后,设置该属性,让运行环境判断接下来要执行哪个分支的节点。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ConnectionInvokeType NextOrientation { get; set; }
|
|
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 每个上下文分别存放节点的当前数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, object> dictNodeFlowData = new ConcurrentDictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取节点当前数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public object GetFlowData(string nodeGuid)
|
|
|
|
|
|
{
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
if (dictNodeFlowData.TryGetValue(nodeGuid, out var data))
|
|
|
|
|
|
{
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加或更新当前节点数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid">节点Guid</param>
|
|
|
|
|
|
/// <param name="flowData">新的数据</param>
|
|
|
|
|
|
public void AddOrUpdate(string nodeGuid, object flowData)
|
|
|
|
|
|
{
|
|
|
|
|
|
// this.dictNodeFlowData.TryGetValue(nodeGuid, out var oldFlowData);
|
|
|
|
|
|
this.dictNodeFlowData[nodeGuid] = flowData;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 结束流程
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void EndCurrentBranch()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.dictNodeFlowData?.Clear();
|
|
|
|
|
|
RunState = RunState.Completion;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public NodeRunCts NodeRunCts { get; set; }
|
|
|
|
|
|
// public ISereinIOC SereinIoc { get; }
|
2024-10-10 10:45:53 +08:00
|
|
|
|
//public Task CreateTimingTask(Action action, int time = 100, int count = -1)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if(NodeRunCts == null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// NodeRunCts = Env.IOC.Get<NodeRunCts>();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// // 使用局部变量,避免捕获外部的 `action`
|
|
|
|
|
|
// Action localAction = action;
|
2024-09-21 10:06:44 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
// return Task.Run(async () =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// for (int i = 0; i < count && !NodeRunCts.IsCancellationRequested; i++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// await Task.Delay(time);
|
|
|
|
|
|
// if (NodeRunCts.IsCancellationRequested) { break; }
|
|
|
|
|
|
// //if (FlowEnvironment.IsGlobalInterrupt)
|
|
|
|
|
|
// //{
|
|
|
|
|
|
// // await FlowEnvironment.GetOrCreateGlobalInterruptAsync();
|
|
|
|
|
|
// //}
|
|
|
|
|
|
// // 确保对局部变量的引用
|
|
|
|
|
|
// localAction?.Invoke();
|
|
|
|
|
|
// }
|
2024-09-21 10:06:44 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
// // 清理引用,避免闭包导致的内存泄漏
|
|
|
|
|
|
// localAction = null;
|
|
|
|
|
|
// });
|
|
|
|
|
|
//}
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|