using Serein.Library.Api;
using System;
using System.Collections.Concurrent;
namespace Serein.Library.Framework.NodeFlow
{
///
/// 动态流程上下文
///
public class DynamicContext : IDynamicContext
{
public DynamicContext(/*ISereinIOC sereinIoc,*/ IFlowEnvironment flowEnvironment)
{
// SereinIoc = sereinIoc;
Env = flowEnvironment;
RunState = RunState.Running;
}
///
/// 运行环境
///
public IFlowEnvironment Env { get; }
///
/// 运行状态
///
public RunState RunState { get; set; } = RunState.NoStart;
///
/// 当前节点执行完成后,设置该属性,让运行环境判断接下来要执行哪个分支的节点。
///
public ConnectionInvokeType NextOrientation { get; set; }
///
/// 每个上下文分别存放节点的当前数据
///
private readonly ConcurrentDictionary dictNodeFlowData = new ConcurrentDictionary();
private readonly ConcurrentDictionary dictPreviousNodes = new ConcurrentDictionary();
///
/// 设置运行时上一节点
///
/// 当前节点
/// 上一节点
public void SetPreviousNode(NodeModelBase currentNodeModel, NodeModelBase PreviousNode)
{
dictPreviousNodes.AddOrUpdate(currentNodeModel, (n1) => PreviousNode, (n1, n2) => PreviousNode);
}
///
/// 获取当前节点的运行时上一节点
///
///
///
public NodeModelBase GetPreviousNode(NodeModelBase currentNodeModel)
{
if (dictPreviousNodes.TryGetValue(currentNodeModel, out var node))
{
return node;
}
else
{
return null;
}
}
///
/// 获取节点当前数据
///
///
public object GetFlowData(string nodeGuid)
{
if (dictNodeFlowData.TryGetValue(nodeGuid, out var data))
{
return data;
}
else
{
return null;
}
}
///
/// 添加或更新当前节点数据
///
/// 节点
/// 新的数据
public void AddOrUpdate(string nodeGuid, object flowData)
{
// this.dictNodeFlowData.TryGetValue(nodeGuid, out var oldFlowData);
this.dictNodeFlowData.AddOrUpdate(nodeGuid, n1 => flowData, (n1, n2)=> flowData);
}
///
/// 上一节点数据透传到下一节点
///
///
public object TransmissionData(NodeModelBase nodeModel)
{
if (dictPreviousNodes.TryGetValue(nodeModel, out var previousNode)) // 首先获取当前节点的上一节点
{
if (dictNodeFlowData.TryGetValue(previousNode.Guid, out var data)) // 其次获取上一节点的数据
{
return data;
//AddOrUpdate(nodeModel.Guid, data); // 然后作为当前节点的数据记录在上下文中
}
}
return null;
}
///
/// 结束流程
///
public void Exit()
{
foreach (var nodeObj in dictNodeFlowData.Values)
{
if (nodeObj is null)
{
continue;
}
else
{
if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
{
disposable?.Dispose();
}
}
}
this.dictNodeFlowData?.Clear();
RunState = RunState.Completion;
}
// public NodeRunCts NodeRunCts { get; set; }
// public ISereinIOC SereinIoc { get; }
//public Task CreateTimingTask(Action action, int time = 100, int count = -1)
//{
// if(NodeRunCts == null)
// {
// NodeRunCts = Env.IOC.Get();
// }
// // 使用局部变量,避免捕获外部的 `action`
// Action localAction = action;
// 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();
// }
// // 清理引用,避免闭包导致的内存泄漏
// localAction = null;
// });
//}
}
}