2024-09-12 20:32:54 +08:00
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.Utils;
|
2025-01-22 21:09:52 +08:00
|
|
|
|
using System;
|
2024-10-14 17:29:28 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2025-01-22 21:09:52 +08:00
|
|
|
|
using System.Collections.Generic;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
|
2025-01-22 21:09:52 +08:00
|
|
|
|
namespace Serein.Library
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 动态流程上下文
|
|
|
|
|
|
/// </summary>
|
2024-11-02 16:48:40 +08:00
|
|
|
|
public class DynamicContext : IDynamicContext
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 动态流程上下文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="flowEnvironment"></param>
|
|
|
|
|
|
public DynamicContext(IFlowEnvironment flowEnvironment)
|
2024-09-12 20:32:54 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
Env = flowEnvironment;
|
2024-10-14 17:29:28 +08:00
|
|
|
|
RunState = RunState.Running;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-20 22:54:10 +08:00
|
|
|
|
private string _guid = global::System.Guid.NewGuid().ToString();
|
2024-12-21 20:47:31 +08:00
|
|
|
|
string IDynamicContext.Guid => _guid;
|
|
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行环境
|
|
|
|
|
|
/// </summary>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public IFlowEnvironment Env { get; }
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public RunState RunState { get; set; } = RunState.NoStart;
|
|
|
|
|
|
|
2024-11-02 16:48:40 +08:00
|
|
|
|
|
2024-10-24 23:32:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前节点执行完成后,设置该属性,让运行环境判断接下来要执行哪个分支的节点。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ConnectionInvokeType NextOrientation { get; set; }
|
|
|
|
|
|
|
2024-11-04 23:30:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行时异常信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Exception ExceptionOfRuning { get; set; }
|
|
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
2024-10-28 21:52:45 +08:00
|
|
|
|
/// 每个流程上下文分别存放节点的当前数据
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// </summary>
|
2025-03-21 18:26:01 +08:00
|
|
|
|
private readonly ConcurrentDictionary<NodeModelBase, FlowResult> dictNodeFlowData = new ConcurrentDictionary<NodeModelBase, FlowResult>();
|
2024-10-14 17:29:28 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-28 21:52:45 +08:00
|
|
|
|
/// 每个流程上下文存储运行时节点的调用关系
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly ConcurrentDictionary<NodeModelBase, NodeModelBase> dictPreviousNodes = new ConcurrentDictionary<NodeModelBase, NodeModelBase>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置运行时上一节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="currentNodeModel">当前节点</param>
|
|
|
|
|
|
/// <param name="PreviousNode">上一节点</param>
|
|
|
|
|
|
public void SetPreviousNode(NodeModelBase currentNodeModel, NodeModelBase PreviousNode)
|
|
|
|
|
|
{
|
2025-01-22 21:09:52 +08:00
|
|
|
|
dictPreviousNodes.AddOrUpdate(currentNodeModel, (_) => PreviousNode, (o, n) => PreviousNode);
|
2024-10-28 21:52:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前节点的运行时上一节点
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// </summary>
|
2024-10-28 21:52:45 +08:00
|
|
|
|
/// <param name="currentNodeModel"></param>
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <returns></returns>
|
2024-10-28 21:52:45 +08:00
|
|
|
|
public NodeModelBase GetPreviousNode(NodeModelBase currentNodeModel)
|
2024-10-14 17:29:28 +08:00
|
|
|
|
{
|
2024-10-28 21:52:45 +08:00
|
|
|
|
if (dictPreviousNodes.TryGetValue(currentNodeModel, out var node))
|
|
|
|
|
|
{
|
|
|
|
|
|
return node;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-10-28 21:52:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取节点当前数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid">节点</param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-03-21 18:26:01 +08:00
|
|
|
|
public FlowResult GetFlowData(NodeModelBase nodeGuid)
|
2024-10-28 21:52:45 +08:00
|
|
|
|
{
|
2024-11-02 16:48:40 +08:00
|
|
|
|
if (dictNodeFlowData.TryGetValue(nodeGuid, out var data))
|
2024-10-14 17:29:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
2024-10-28 21:52:45 +08:00
|
|
|
|
else
|
2024-10-14 17:29:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-12 20:32:54 +08:00
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加或更新当前节点数据
|
|
|
|
|
|
/// </summary>
|
2025-03-21 18:26:01 +08:00
|
|
|
|
/// <param name="nodeModel">节点</param>
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <param name="flowData">新的数据</param>
|
2025-03-21 18:26:01 +08:00
|
|
|
|
public void AddOrUpdate(NodeModelBase nodeModel, FlowResult flowData)
|
2024-10-14 17:29:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
// this.dictNodeFlowData.TryGetValue(nodeGuid, out var oldFlowData);
|
2025-03-21 18:26:01 +08:00
|
|
|
|
dictNodeFlowData.AddOrUpdate(nodeModel, _ => flowData, (o,n ) => flowData);
|
2024-10-14 17:29:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-28 21:52:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 上一节点数据透传到下一节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeModel"></param>
|
2025-03-21 18:26:01 +08:00
|
|
|
|
public FlowResult TransmissionData(NodeModelBase nodeModel)
|
2024-10-28 21:52:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (dictPreviousNodes.TryGetValue(nodeModel, out var previousNode)) // 首先获取当前节点的上一节点
|
2024-11-02 16:48:40 +08:00
|
|
|
|
{
|
2025-03-21 18:26:01 +08:00
|
|
|
|
if (dictNodeFlowData.TryGetValue(previousNode, out var data)) // 其次获取上一节点的数据
|
2024-11-02 16:48:40 +08:00
|
|
|
|
{
|
2024-10-28 21:52:45 +08:00
|
|
|
|
return data;
|
|
|
|
|
|
//AddOrUpdate(nodeModel.Guid, data); // 然后作为当前节点的数据记录在上下文中
|
2024-11-02 16:48:40 +08:00
|
|
|
|
}
|
2024-10-28 21:52:45 +08:00
|
|
|
|
}
|
2025-03-21 18:26:01 +08:00
|
|
|
|
throw new InvalidOperationException($"透传{nodeModel.Guid}节点数据时发生异常:上一节点不存在数据");
|
2024-10-28 21:52:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-20 22:54:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
//foreach (var nodeObj in dictNodeFlowData.Values)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (nodeObj is null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// disposable?.Dispose();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
//if (Tag != null && typeof(IDisposable).IsAssignableFrom(Tag?.GetType()) && Tag is IDisposable tagDisposable)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// tagDisposable?.Dispose();
|
|
|
|
|
|
//}
|
|
|
|
|
|
this.dictNodeFlowData?.Clear();
|
|
|
|
|
|
ExceptionOfRuning = null;
|
|
|
|
|
|
NextOrientation = ConnectionInvokeType.None;
|
|
|
|
|
|
RunState = RunState.Running;
|
|
|
|
|
|
_guid = global::System.Guid.NewGuid().ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// <summary>
|
2025-03-17 11:57:06 +08:00
|
|
|
|
/// 结束当前流程上下文
|
2024-10-14 17:29:28 +08:00
|
|
|
|
/// </summary>
|
2024-10-27 00:54:10 +08:00
|
|
|
|
public void Exit()
|
2024-10-14 17:29:28 +08:00
|
|
|
|
{
|
2025-03-17 11:57:06 +08:00
|
|
|
|
//foreach (var nodeObj in dictNodeFlowData.Values)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (nodeObj is null)
|
|
|
|
|
|
// {
|
2025-01-22 21:09:52 +08:00
|
|
|
|
|
2025-03-17 11:57:06 +08:00
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// disposable?.Dispose();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
//if (Tag != null && typeof(IDisposable).IsAssignableFrom(Tag?.GetType()) && Tag is IDisposable tagDisposable)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// tagDisposable?.Dispose();
|
|
|
|
|
|
//}
|
2024-10-14 17:29:28 +08:00
|
|
|
|
this.dictNodeFlowData?.Clear();
|
2025-03-20 22:54:10 +08:00
|
|
|
|
ExceptionOfRuning = null;
|
|
|
|
|
|
NextOrientation = ConnectionInvokeType.None;
|
2024-10-14 17:29:28 +08:00
|
|
|
|
RunState = RunState.Completion;
|
2025-03-20 22:54:10 +08:00
|
|
|
|
_guid = global::System.Guid.NewGuid().ToString();
|
2024-10-14 17:29:28 +08:00
|
|
|
|
}
|
2024-09-12 20:32:54 +08:00
|
|
|
|
|
2024-11-04 23:30:52 +08:00
|
|
|
|
private void Dispose(ref IDictionary<string, object> keyValuePairs)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var nodeObj in keyValuePairs.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nodeObj is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (nodeObj is IDisposable disposable) /* typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) &&*/
|
|
|
|
|
|
{
|
|
|
|
|
|
disposable?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is IDictionary<string, object> tmpDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpDict);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is ICollection<object> tmpList)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpList);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is IList<object> tmpList2)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpList2);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
keyValuePairs.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void Dispose(ref ICollection<object> list)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var nodeObj in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nodeObj is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (nodeObj is IDisposable disposable) /* typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) &&*/
|
|
|
|
|
|
{
|
|
|
|
|
|
disposable?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is IDictionary<string, object> tmpDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpDict);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is ICollection<object> tmpList)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpList);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is IList<object> tmpList2)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpList2);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void Dispose(ref IList<object> list)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var nodeObj in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nodeObj is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (nodeObj is IDisposable disposable) /* typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) &&*/
|
|
|
|
|
|
{
|
|
|
|
|
|
disposable?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is IDictionary<string, object> tmpDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpDict);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is ICollection<object> tmpList)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpList);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (nodeObj is IList<object> tmpList2)
|
|
|
|
|
|
{
|
|
|
|
|
|
Dispose(ref tmpList2);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list.Clear();
|
|
|
|
|
|
}
|
2024-10-14 17:29:28 +08:00
|
|
|
|
}
|
2024-09-12 20:32:54 +08:00
|
|
|
|
}
|