2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-22 14:10:13 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2024-09-17 14:20:27 +08:00
|
|
|
|
using Serein.Library.Api;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
using Serein.Library.Attributes;
|
|
|
|
|
|
using Serein.Library.Entity;
|
|
|
|
|
|
using Serein.Library.Enums;
|
|
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using Serein.NodeFlow.Base;
|
|
|
|
|
|
using Serein.NodeFlow.Model;
|
|
|
|
|
|
using Serein.NodeFlow.Tool;
|
2024-09-24 22:39:43 +08:00
|
|
|
|
using System.Collections;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
using System.Reflection;
|
2024-09-21 10:06:44 +08:00
|
|
|
|
using System.Xml.Linq;
|
2024-09-22 17:37:32 +08:00
|
|
|
|
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
2024-09-16 21:38:34 +08:00
|
|
|
|
using static Serein.NodeFlow.FlowStarter;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow
|
|
|
|
|
|
{
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
脱离wpf平台独立运行。
|
|
|
|
|
|
加载文件。
|
|
|
|
|
|
创建节点对象,设置节点属性,确定连接关系,设置起点。
|
|
|
|
|
|
|
|
|
|
|
|
↓抽象↓
|
|
|
|
|
|
|
|
|
|
|
|
wpf依赖于运行环境,而不是运行环境依赖于wpf。
|
|
|
|
|
|
|
|
|
|
|
|
运行环境实现以下功能:
|
|
|
|
|
|
①从项目文件加载数据,生成项目文件对象。
|
|
|
|
|
|
②运行项目,调试项目,中止项目,终止项目。
|
|
|
|
|
|
③自动包装数据类型,在上下文中传递数据。
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行环境
|
|
|
|
|
|
/// </summary>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public class FlowEnvironment : IFlowEnvironment, ISereinIOC
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-20 10:50:32 +08:00
|
|
|
|
public FlowEnvironment()
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
sereinIOC = new SereinIOC();
|
2024-09-30 22:20:02 +08:00
|
|
|
|
ChannelFlowInterrupt = new ChannelFlowInterrupt();
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//LoadedAssemblyPaths = new List<string>();
|
|
|
|
|
|
//LoadedAssemblies = new List<Assembly>();
|
|
|
|
|
|
//MethodDetailss = new List<MethodDetails>();
|
|
|
|
|
|
//Nodes = new Dictionary<string, NodeModelBase>();
|
|
|
|
|
|
//FlipflopNodes = new List<SingleFlipflopNode>();
|
2024-09-20 10:50:32 +08:00
|
|
|
|
IsGlobalInterrupt = false;
|
|
|
|
|
|
flowStarter = null;
|
2024-09-26 21:00:17 +08:00
|
|
|
|
|
|
|
|
|
|
sereinIOC.OnIOCMembersChanged += e => this?.OnIOCMembersChanged?.Invoke(e) ; // 监听IOC容器的注册
|
2024-09-20 10:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-26 21:00:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点的命名空间
|
|
|
|
|
|
/// </summary>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
public const string SpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-20 10:50:32 +08:00
|
|
|
|
#region 环境接口事件
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载Dll
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event LoadDllHandler? OnDllLoad;
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除DLL
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event RemoteDllHandler? OnDllRemote;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// 项目加载完成
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event ProjectLoadedHandler? OnProjectLoaded;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// 节点连接属性改变事件
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event NodeConnectChangeHandler? OnNodeConnectChange;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// 节点创建事件
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event NodeCreateHandler? OnNodeCreate;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除节点事件
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event NodeRemoteHandler? OnNodeRemote;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 起始节点变化事件
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event StartNodeChangeHandler? OnStartNodeChange;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
2024-09-22 14:10:13 +08:00
|
|
|
|
/// 流程运行完成事件
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event FlowRunCompleteHandler? OnFlowRunComplete;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-22 14:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 被监视的对象改变事件
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event MonitorObjectChangeHandler? OnMonitorObjectChange;
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点中断状态改变事件
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event NodeInterruptStateChangeHandler? OnNodeInterruptStateChange;
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点触发了中断
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event ExpInterruptTriggerHandler? OnInterruptTrigger;
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
2024-09-26 21:00:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 容器改变
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event IOCMembersChangedHandler? OnIOCMembersChanged;
|
2024-09-26 21:00:17 +08:00
|
|
|
|
|
2024-09-27 23:47:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点需要定位
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
public event NodeLocatedHandler? OnNodeLocate;
|
2024-09-20 10:50:32 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
#region 属性
|
|
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
/// 环境名称
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
public string EnvName { get; set; } = SpaceName;
|
|
|
|
|
|
|
2024-09-20 10:50:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否全局中断
|
|
|
|
|
|
/// </summary>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public bool IsGlobalInterrupt { get; set; }
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-22 17:37:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 流程中断器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ChannelFlowInterrupt ChannelFlowInterrupt { get; set; }
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
public ISereinIOC IOC { get => this; }
|
2024-09-26 21:00:17 +08:00
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 私有变量
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 容器管理
|
|
|
|
|
|
/// </summary>
|
2024-09-26 21:00:17 +08:00
|
|
|
|
private readonly SereinIOC sereinIOC;
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储加载的程序集路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储加载的程序集
|
|
|
|
|
|
/// </summary>
|
2024-09-30 02:45:49 +08:00
|
|
|
|
private List<NodeLibrary> NodeLibrarys { get; } = [];
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储所有方法信息
|
|
|
|
|
|
/// </summary>
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//private MethodDetailss { get; } = [];
|
|
|
|
|
|
private Dictionary<NodeLibrary, List<MethodDetails>> MethodDetailss { get; } = [];
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-20 10:50:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 环境加载的节点集合
|
2024-09-30 02:45:49 +08:00
|
|
|
|
/// Node Guid - Node Model
|
2024-09-20 10:50:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-30 02:45:49 +08:00
|
|
|
|
private Dictionary<string, NodeModelBase> Nodes { get; } = [];
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存放触发器节点(运行时全部调用)
|
|
|
|
|
|
/// </summary>
|
2024-09-30 02:45:49 +08:00
|
|
|
|
private List<SingleFlipflopNode> FlipflopNodes { get; } = [];
|
2024-10-10 10:45:53 +08:00
|
|
|
|
private Dictionary<RegisterSequence,List<Type>> AutoRegisterTypes { get; } = [];
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存放委托
|
|
|
|
|
|
///
|
|
|
|
|
|
/// md.Methodname - delegate
|
|
|
|
|
|
/// </summary>
|
2024-10-10 10:45:53 +08:00
|
|
|
|
|
|
|
|
|
|
private ConcurrentDictionary<string, DelegateDetails> MethodDelegates { get; } = [];
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
2024-09-20 10:50:32 +08:00
|
|
|
|
/// 起始节点私有属性
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
private NodeModelBase? _startNode = null;
|
2024-09-17 21:43:49 +08:00
|
|
|
|
|
2024-09-15 19:48:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 起始节点
|
|
|
|
|
|
/// </summary>
|
2024-09-30 22:20:02 +08:00
|
|
|
|
private NodeModelBase? StartNode
|
2024-09-16 19:53:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2024-09-15 12:15:32 +08:00
|
|
|
|
return _startNode;
|
|
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2024-09-30 22:20:02 +08:00
|
|
|
|
if (value is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
if (_startNode is not null)
|
2024-09-24 22:39:43 +08:00
|
|
|
|
{
|
2024-09-15 12:15:32 +08:00
|
|
|
|
_startNode.IsStart = false;
|
|
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
value.IsStart = true;
|
|
|
|
|
|
_startNode = value;
|
2024-09-16 19:53:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-20 10:50:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 流程启动器(每次运行时都会重新new一个)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private FlowStarter? flowStarter;
|
|
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 对外暴露的接口
|
|
|
|
|
|
|
2024-09-15 19:48:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步运行
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-09-15 12:15:32 +08:00
|
|
|
|
public async Task StartAsync()
|
|
|
|
|
|
{
|
2024-09-20 10:50:32 +08:00
|
|
|
|
ChannelFlowInterrupt?.CancelAllTasks();
|
2024-09-17 21:43:49 +08:00
|
|
|
|
flowStarter = new FlowStarter();
|
2024-09-22 14:10:13 +08:00
|
|
|
|
var nodes = Nodes.Values.ToList();
|
|
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
List<MethodDetails> initMethods = [];
|
|
|
|
|
|
List<MethodDetails> loadMethods = [];
|
|
|
|
|
|
List<MethodDetails> exitMethods = [];
|
|
|
|
|
|
foreach(var mds in MethodDetailss.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
var initMds = mds.Where(it => it.MethodDynamicType == NodeType.Init);
|
|
|
|
|
|
var loadMds = mds.Where(it => it.MethodDynamicType == NodeType.Loading);
|
|
|
|
|
|
var exitMds = mds.Where(it => it.MethodDynamicType == NodeType.Exit);
|
|
|
|
|
|
initMethods.AddRange(initMds);
|
|
|
|
|
|
loadMethods.AddRange(loadMds);
|
|
|
|
|
|
exitMethods.AddRange(exitMds);
|
|
|
|
|
|
}
|
|
|
|
|
|
this.IOC.Reset(); // 开始运行时清空ioc中注册的实例
|
|
|
|
|
|
this.IOC.CustomRegisterInstance(typeof(IFlowEnvironment).FullName,this);
|
2024-10-10 10:45:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await flowStarter.RunAsync(this, nodes, AutoRegisterTypes, initMethods, loadMethods, exitMethods);
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (flowStarter?.FlipFlopState == RunState.NoStart)
|
2024-09-16 21:38:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
this.Exit(); // 未运行触发器时,才会调用结束方法
|
|
|
|
|
|
}
|
2024-09-17 21:43:49 +08:00
|
|
|
|
flowStarter = null;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public async Task StartFlowInSelectNodeAsync(string startNodeGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (flowStarter is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (flowStarter.FlowState == RunState.Running || flowStarter.FlipFlopState == RunState.Running)
|
|
|
|
|
|
{
|
|
|
|
|
|
NodeModelBase? nodeModel = GuidToModel(startNodeGuid);
|
|
|
|
|
|
if (nodeModel is null || nodeModel is SingleFlipflopNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
await flowStarter.StartFlowInSelectNodeAsync(nodeModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-22 14:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 退出
|
|
|
|
|
|
/// </summary>
|
2024-09-15 12:15:32 +08:00
|
|
|
|
public void Exit()
|
|
|
|
|
|
{
|
2024-09-22 14:10:13 +08:00
|
|
|
|
ChannelFlowInterrupt?.CancelAllTasks();
|
|
|
|
|
|
flowStarter?.Exit();
|
|
|
|
|
|
|
2024-09-21 10:06:44 +08:00
|
|
|
|
foreach (var node in Nodes.Values)
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (node is not null)
|
2024-09-21 10:06:44 +08:00
|
|
|
|
{
|
2024-09-22 14:10:13 +08:00
|
|
|
|
node.ReleaseFlowData(); // 退出时释放对象计数
|
2024-09-21 10:06:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
OnFlowRunComplete?.Invoke(new FlowEventArgs());
|
2024-09-21 10:06:44 +08:00
|
|
|
|
|
|
|
|
|
|
GC.Collect();
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清除所有
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ClearAll()
|
|
|
|
|
|
{
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//LoadedAssemblyPaths.Clear();
|
|
|
|
|
|
NodeLibrarys.Clear();
|
2024-09-15 12:15:32 +08:00
|
|
|
|
MethodDetailss.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载项目文件
|
|
|
|
|
|
/// </summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// <param name="project"></param>
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <param name="filePath"></param>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
public void LoadProject(SereinProjectData project, string filePath)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 加载项目配置文件
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var dllPaths = project.Librarys.Select(it => it.Path).ToList();
|
2024-09-15 12:15:32 +08:00
|
|
|
|
List<MethodDetails> methodDetailss = [];
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历依赖项中的特性注解,生成方法详情
|
|
|
|
|
|
foreach (var dll in dllPaths)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dllFilePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(filePath, dll));
|
2024-09-30 02:45:49 +08:00
|
|
|
|
LoadDllNodeInfo(dllFilePath);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 方法加载完成,缓存到运行环境中。
|
2024-09-17 14:20:27 +08:00
|
|
|
|
//MethodDetailss.AddRange(methodDetailss);
|
|
|
|
|
|
//methodDetailss.Clear();
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
List<(NodeModelBase, string[])> regionChildNodes = new List<(NodeModelBase, string[])>();
|
|
|
|
|
|
List<(NodeModelBase, Position)> ordinaryNodes = new List<(NodeModelBase, Position)>();
|
2024-09-15 12:15:32 +08:00
|
|
|
|
// 加载节点
|
2024-09-17 14:20:27 +08:00
|
|
|
|
foreach (var nodeInfo in project.Nodes)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var controlType = GetNodeControlType(nodeInfo);
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (controlType == NodeControlType.None)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
TryGetMethodDetails(nodeInfo.MethodName, out MethodDetails? methodDetails); // 加载项目时尝试获取方法信息
|
2024-09-25 22:20:23 +08:00
|
|
|
|
if(controlType == NodeControlType.ExpOp || controlType == NodeControlType.ExpOp)
|
|
|
|
|
|
{
|
|
|
|
|
|
methodDetails ??= new MethodDetails();
|
|
|
|
|
|
}
|
|
|
|
|
|
if(methodDetails is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue; // 节点对应的方法不存在于DLL中
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var nodeModel = CreateNode(controlType, methodDetails);
|
|
|
|
|
|
nodeModel.LoadInfo(nodeInfo); // 创建节点model
|
|
|
|
|
|
if (nodeModel is null)
|
|
|
|
|
|
{
|
2024-09-25 22:20:23 +08:00
|
|
|
|
nodeInfo.Guid = string.Empty;
|
2024-09-17 14:20:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
TryAddNode(nodeModel);
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (nodeInfo.ChildNodeGuids?.Length > 0)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
regionChildNodes.Add((nodeModel, nodeInfo.ChildNodeGuids));
|
2024-09-17 14:20:27 +08:00
|
|
|
|
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, nodeInfo.Position));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ordinaryNodes.Add((nodeModel, nodeInfo.Position));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 加载区域的子项
|
2024-09-24 22:39:43 +08:00
|
|
|
|
foreach ((NodeModelBase region, string[] childNodeGuids) item in regionChildNodes)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var childNodeGuid in item.childNodeGuids)
|
|
|
|
|
|
{
|
|
|
|
|
|
Nodes.TryGetValue(childNodeGuid, out NodeModelBase? childNode);
|
|
|
|
|
|
if (childNode is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 节点尚未加载
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 存在节点
|
|
|
|
|
|
OnNodeCreate?.Invoke(new NodeCreateEventArgs(childNode, true, item.region.Guid));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 加载节点
|
|
|
|
|
|
foreach ((NodeModelBase nodeModel, Position position) item in ordinaryNodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool IsContinue = false;
|
|
|
|
|
|
foreach ((NodeModelBase region, string[] childNodeGuids) item2 in regionChildNodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var childNodeGuid in item2.childNodeGuids)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.nodeModel.Guid.Equals(childNodeGuid))
|
|
|
|
|
|
{
|
|
|
|
|
|
IsContinue = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-17 14:20:27 +08:00
|
|
|
|
if (IsContinue) continue;
|
|
|
|
|
|
OnNodeCreate?.Invoke(new NodeCreateEventArgs(item.nodeModel, item.position));
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
// 确定节点之间的连接关系
|
2024-09-17 14:20:27 +08:00
|
|
|
|
foreach (var nodeInfo in project.Nodes)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
if (!Nodes.TryGetValue(nodeInfo.Guid, out NodeModelBase? fromNode))
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 不存在对应的起始节点
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
List<(ConnectionType connectionType, string[] guids)> allToNodes = [(ConnectionType.IsSucceed,nodeInfo.TrueNodes),
|
|
|
|
|
|
(ConnectionType.IsFail, nodeInfo.FalseNodes),
|
|
|
|
|
|
(ConnectionType.IsError, nodeInfo.ErrorNodes),
|
|
|
|
|
|
(ConnectionType.Upstream, nodeInfo.UpstreamNodes)];
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
List<(ConnectionType, NodeModelBase[])> fromNodes = allToNodes.Where(info => info.guids.Length > 0)
|
|
|
|
|
|
.Select(info => (info.connectionType,
|
2024-09-25 22:20:23 +08:00
|
|
|
|
info.guids.Where(guid => Nodes.ContainsKey(guid)).Select(guid => Nodes[guid])
|
|
|
|
|
|
.ToArray()))
|
2024-09-15 12:15:32 +08:00
|
|
|
|
.ToList();
|
|
|
|
|
|
// 遍历每种类型的节点分支(四种)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
foreach ((ConnectionType connectionType, NodeModelBase[] toNodes) item in fromNodes)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 遍历当前类型分支的节点(确认连接关系)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
foreach (var toNode in item.toNodes)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
ConnectNode(fromNode, toNode, item.connectionType); // 加载时确定节点间的连接关系
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
SetStartNode(project.StartNode);
|
|
|
|
|
|
OnProjectLoaded?.Invoke(new ProjectLoadedEventArgs());
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// 保存项目为项目文件
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <returns></returns>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
public SereinProjectData SaveProject()
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var projectData = new SereinProjectData()
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-30 02:45:49 +08:00
|
|
|
|
Librarys = NodeLibrarys.Select(assemblies => assemblies.Assembly.ToLibrary()).ToArray(),
|
2024-09-16 21:38:34 +08:00
|
|
|
|
Nodes = Nodes.Values.Select(node => node.ToInfo()).Where(info => info is not null).ToArray(),
|
|
|
|
|
|
StartNode = Nodes.Values.FirstOrDefault(it => it.IsStart)?.Guid,
|
|
|
|
|
|
};
|
|
|
|
|
|
return projectData;
|
|
|
|
|
|
}
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从文件路径中加载DLL
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dllPath"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public void LoadDll(string dllPath)
|
|
|
|
|
|
{
|
2024-09-30 02:45:49 +08:00
|
|
|
|
LoadDllNodeInfo(dllPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool RemoteDll(string assemblyFullName)
|
|
|
|
|
|
{
|
2024-09-30 22:20:02 +08:00
|
|
|
|
var library = NodeLibrarys.FirstOrDefault(nl => assemblyFullName.Equals(nl.Assembly.FullName));
|
2024-09-30 02:45:49 +08:00
|
|
|
|
if(library is null)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-30 02:45:49 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-09-30 22:20:02 +08:00
|
|
|
|
var groupedNodes = Nodes.Values
|
|
|
|
|
|
.Where(node => node.MethodDetails is not null)
|
|
|
|
|
|
.ToArray()
|
|
|
|
|
|
.GroupBy(node => node.MethodDetails!.MethodName)
|
|
|
|
|
|
.ToDictionary(
|
|
|
|
|
|
key => key.Key,
|
|
|
|
|
|
group => group.Count());
|
|
|
|
|
|
if(Nodes.Count == 0)
|
2024-09-30 02:45:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
return true; // 当前无节点,可以直接删除
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (MethodDetailss.TryGetValue(library,out var mds)) // 存在方法
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach(var md in mds)
|
|
|
|
|
|
{
|
2024-09-30 22:20:02 +08:00
|
|
|
|
if(groupedNodes.TryGetValue(md.MethodName,out int count))
|
2024-09-30 02:45:49 +08:00
|
|
|
|
{
|
2024-09-30 22:20:02 +08:00
|
|
|
|
if (count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false; // 创建过相关的节点
|
|
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
MethodDetailss.Remove(library);
|
|
|
|
|
|
return true; // 没有创建相关的节点
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-09-30 22:20:02 +08:00
|
|
|
|
return true;
|
2024-09-16 19:53:36 +08:00
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-28 23:55:19 +08:00
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
2024-10-10 16:49:37 +08:00
|
|
|
|
/// 流程正在运行时创建节点
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// </summary>
|
2024-09-28 23:55:19 +08:00
|
|
|
|
/// <param name="nodeControlType"></param>
|
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
|
/// <param name="methodDetails"></param>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
public void CreateNode(NodeControlType nodeControlType, Position position, MethodDetails? methodDetails = null)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var nodeModel = CreateNode(nodeControlType, methodDetails);
|
|
|
|
|
|
TryAddNode(nodeModel);
|
2024-09-17 21:43:49 +08:00
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
//if (flowStarter?.FlowState != RunState.Completion
|
|
|
|
|
|
// && nodeControlType == NodeControlType.Flipflop
|
|
|
|
|
|
// && nodeModel is SingleFlipflopNode flipflopNode)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// _ = flowStarter?.RunGlobalFlipflopAsync(this, flipflopNode); // 当前添加节点属于触发器,且当前正在运行,则加载到运行环境中
|
|
|
|
|
|
//}
|
2024-09-17 21:43:49 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
// 通知UI更改
|
2024-09-17 14:20:27 +08:00
|
|
|
|
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, position));
|
2024-09-15 12:15:32 +08:00
|
|
|
|
// 因为需要UI先布置了元素,才能通知UI变更特效
|
|
|
|
|
|
// 如果不存在流程起始控件,默认设置为流程起始控件
|
|
|
|
|
|
if (StartNode is null)
|
|
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
SetStartNode(nodeModel);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// 移除节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
|
public void RemoteNode(string nodeGuid)
|
|
|
|
|
|
{
|
2024-09-22 17:37:32 +08:00
|
|
|
|
var remoteNode = GuidToModel(nodeGuid);
|
|
|
|
|
|
if (remoteNode is null) return;
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//if (remoteNode.IsStart)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return;
|
|
|
|
|
|
//}
|
2024-09-28 23:55:19 +08:00
|
|
|
|
if (remoteNode is SingleFlipflopNode flipflopNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
flowStarter?.TerminateGlobalFlipflopRuning(flipflopNode); // 假设被移除的是全局触发器,尝试从启动器移除
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
// 遍历所有父节点,从那些父节点中的子节点集合移除该节点
|
2024-09-16 19:53:36 +08:00
|
|
|
|
foreach (var pnc in remoteNode.PreviousNodes)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
var pCType = pnc.Key; // 连接类型
|
|
|
|
|
|
for (int i = 0; i < pnc.Value.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
NodeModelBase? pNode = pnc.Value[i];
|
2024-09-29 12:01:26 +08:00
|
|
|
|
pNode.SuccessorNodes[pCType].Remove(remoteNode);
|
2024-09-21 10:06:44 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(pNode.Guid,
|
|
|
|
|
|
remoteNode.Guid,
|
|
|
|
|
|
pCType,
|
2024-09-16 21:38:34 +08:00
|
|
|
|
NodeConnectChangeEventArgs.ConnectChangeType.Remote)); // 通知UI
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历所有子节点,从那些子节点中的父节点集合移除该节点
|
|
|
|
|
|
foreach (var snc in remoteNode.SuccessorNodes)
|
|
|
|
|
|
{
|
2024-09-17 21:43:49 +08:00
|
|
|
|
var connectionType = snc.Key; // 连接类型
|
2024-09-15 12:15:32 +08:00
|
|
|
|
for (int i = 0; i < snc.Value.Count; i++)
|
|
|
|
|
|
{
|
2024-09-17 21:43:49 +08:00
|
|
|
|
NodeModelBase? toNode = snc.Value[i];
|
|
|
|
|
|
|
|
|
|
|
|
RemoteConnect(remoteNode, toNode, connectionType);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从集合中移除节点
|
|
|
|
|
|
Nodes.Remove(nodeGuid);
|
|
|
|
|
|
OnNodeRemote?.Invoke(new NodeRemoteEventArgs(nodeGuid));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fromNode">起始节点</param>
|
|
|
|
|
|
/// <param name="toNode">目标节点</param>
|
|
|
|
|
|
/// <param name="connectionType">连接关系</param>
|
|
|
|
|
|
public void ConnectNode(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取起始节点与目标节点
|
2024-09-22 17:37:32 +08:00
|
|
|
|
var fromNode = GuidToModel(fromNodeGuid);
|
|
|
|
|
|
var toNode = GuidToModel(toNodeGuid);
|
|
|
|
|
|
if (fromNode is null) return;
|
|
|
|
|
|
if (toNode is null) return;
|
2024-09-16 21:38:34 +08:00
|
|
|
|
// 开始连接
|
|
|
|
|
|
ConnectNode(fromNode, toNode, connectionType); // 外部调用连接方法
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除连接关系
|
|
|
|
|
|
/// </summary>
|
2024-09-17 21:43:49 +08:00
|
|
|
|
/// <param name="fromNodeGuid">起始节点Guid</param>
|
|
|
|
|
|
/// <param name="toNodeGuid">目标节点Guid</param>
|
|
|
|
|
|
/// <param name="connectionType">连接关系</param>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
|
public void RemoteConnect(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取起始节点与目标节点
|
2024-09-22 17:37:32 +08:00
|
|
|
|
var fromNode = GuidToModel(fromNodeGuid);
|
|
|
|
|
|
var toNode = GuidToModel(toNodeGuid);
|
|
|
|
|
|
if (fromNode is null) return;
|
|
|
|
|
|
if (toNode is null) return;
|
2024-09-17 21:43:49 +08:00
|
|
|
|
RemoteConnect(fromNode, toNode, connectionType);
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
2024-09-17 21:43:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取方法描述
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool TryGetMethodDetails(string name, out MethodDetails? md)
|
|
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(name))
|
|
|
|
|
|
{
|
2024-09-30 02:45:49 +08:00
|
|
|
|
foreach(var mds in MethodDetailss.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
md = mds.FirstOrDefault(it => it.MethodName == name);
|
|
|
|
|
|
if(md != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
md = null;
|
|
|
|
|
|
return false;
|
2024-09-17 14:20:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2024-09-16 21:38:34 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
md = null;
|
2024-09-16 21:38:34 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
public bool TryGetDelegateDetails(string methodName, out DelegateDetails? delegateDetails)
|
2024-09-30 02:45:49 +08:00
|
|
|
|
{
|
2024-09-30 22:20:02 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(methodName) && MethodDelegates.TryGetValue(methodName, out delegateDetails))
|
2024-09-30 02:45:49 +08:00
|
|
|
|
{
|
2024-10-10 10:45:53 +08:00
|
|
|
|
return delegateDetails != null;
|
2024-09-30 02:45:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-10-10 10:45:53 +08:00
|
|
|
|
delegateDetails = null;
|
2024-09-30 02:45:49 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置起点控件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="newNodeGuid"></param>
|
|
|
|
|
|
public void SetStartNode(string newNodeGuid)
|
|
|
|
|
|
{
|
2024-09-22 17:37:32 +08:00
|
|
|
|
var newStartNodeModel = GuidToModel(newNodeGuid);
|
|
|
|
|
|
if (newStartNodeModel is null) return;
|
2024-09-22 14:10:13 +08:00
|
|
|
|
SetStartNode(newStartNodeModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 中断指定节点,并指定中断等级。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid">被中断的目标节点Guid</param>
|
|
|
|
|
|
/// <param name="interruptClass">中断级别</param>
|
|
|
|
|
|
/// <returns>操作是否成功</returns>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
public bool SetNodeInterrupt(string nodeGuid, InterruptClass interruptClass)
|
2024-09-22 14:10:13 +08:00
|
|
|
|
{
|
2024-09-22 17:37:32 +08:00
|
|
|
|
var nodeModel = GuidToModel(nodeGuid);
|
|
|
|
|
|
if (nodeModel is null) return false;
|
|
|
|
|
|
if (interruptClass == InterruptClass.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
nodeModel.CancelInterrupt();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (interruptClass == InterruptClass.Branch)
|
|
|
|
|
|
{
|
|
|
|
|
|
nodeModel.DebugSetting.CancelInterruptCallback?.Invoke();
|
2024-09-24 22:39:43 +08:00
|
|
|
|
nodeModel.DebugSetting.GetInterruptTask = () =>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
TriggerInterrupt(nodeGuid, "", InterruptTriggerEventArgs.InterruptTriggerType.Monitor);
|
|
|
|
|
|
return ChannelFlowInterrupt.GetOrCreateChannelAsync(nodeGuid);
|
|
|
|
|
|
};
|
|
|
|
|
|
nodeModel.DebugSetting.CancelInterruptCallback = () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ChannelFlowInterrupt.TriggerSignal(nodeGuid);
|
|
|
|
|
|
};
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
2024-09-22 17:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (interruptClass == InterruptClass.Global) // 全局……做不了omg
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-09-22 14:10:13 +08:00
|
|
|
|
nodeModel.DebugSetting.InterruptClass = interruptClass;
|
2024-09-30 22:20:02 +08:00
|
|
|
|
OnNodeInterruptStateChange?.Invoke(new NodeInterruptStateChangeEventArgs(nodeGuid, interruptClass));
|
2024-09-22 14:10:13 +08:00
|
|
|
|
return true;
|
2024-09-22 17:37:32 +08:00
|
|
|
|
}
|
2024-09-22 14:10:13 +08:00
|
|
|
|
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加表达式中断
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
/// <param name="expression"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-09-26 21:00:17 +08:00
|
|
|
|
public bool AddInterruptExpression(string key, string expression)
|
2024-09-22 17:37:32 +08:00
|
|
|
|
{
|
2024-09-30 10:02:06 +08:00
|
|
|
|
if(string.IsNullOrEmpty(expression)) return false;
|
2024-09-26 21:00:17 +08:00
|
|
|
|
if (dictMonitorObjExpInterrupt.TryGetValue(key, out var condition))
|
2024-09-22 17:37:32 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
condition.Clear(); // 暂时
|
|
|
|
|
|
condition.Add(expression);// 暂时
|
2024-09-22 17:37:32 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
var exps = new List<string>();
|
|
|
|
|
|
exps.Add(expression);
|
2024-09-26 21:00:17 +08:00
|
|
|
|
dictMonitorObjExpInterrupt.TryAdd(key, exps);
|
2024-09-22 17:37:32 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-09-22 14:10:13 +08:00
|
|
|
|
}
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 要监视的对象,以及与其关联的表达式
|
|
|
|
|
|
/// </summary>
|
2024-09-26 21:00:17 +08:00
|
|
|
|
private ConcurrentDictionary<string, List<string>> dictMonitorObjExpInterrupt = [];
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置对象的监视状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
|
/// <param name="isMonitor"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-09-26 21:00:17 +08:00
|
|
|
|
public void SetMonitorObjState(string key, bool isMonitor)
|
2024-09-22 14:10:13 +08:00
|
|
|
|
{
|
2024-09-26 21:00:17 +08:00
|
|
|
|
if (string.IsNullOrEmpty(key)) { return; }
|
|
|
|
|
|
var isExist = dictMonitorObjExpInterrupt.ContainsKey(key);
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (isExist)
|
2024-09-22 17:37:32 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (!isMonitor) // 对象存在且需要不监视
|
2024-09-22 17:37:32 +08:00
|
|
|
|
{
|
2024-09-26 21:00:17 +08:00
|
|
|
|
dictMonitorObjExpInterrupt.Remove(key, out _);
|
2024-09-22 17:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-24 22:39:43 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isMonitor) // 对象不存在且需要监视,添加在集合中。
|
|
|
|
|
|
{
|
2024-09-26 21:00:17 +08:00
|
|
|
|
dictMonitorObjExpInterrupt.TryAdd(key, new List<string>()); ;
|
2024-09-24 22:39:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检查一个对象是否处于监听状态,如果是,则传出与该对象相关的表达式(用于中断),如果不是,则返回false。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-09-26 21:00:17 +08:00
|
|
|
|
public bool CheckObjMonitorState(string key, out List<string>? exps)
|
2024-09-24 22:39:43 +08:00
|
|
|
|
{
|
2024-09-26 21:00:17 +08:00
|
|
|
|
if (string.IsNullOrEmpty(key)) { exps = null; return false; }
|
|
|
|
|
|
return dictMonitorObjExpInterrupt.TryGetValue(key, out exps);
|
2024-09-22 14:10:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
/// 启动器调用,节点数据更新通知
|
2024-09-22 14:10:13 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public void MonitorObjectNotification(string nodeGuid, object monitorData, MonitorObjectEventArgs.ObjSourceType sourceType)
|
2024-09-22 14:10:13 +08:00
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
OnMonitorObjectChange?.Invoke(new MonitorObjectEventArgs(nodeGuid, monitorData, sourceType));
|
2024-09-22 14:10:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-22 17:37:32 +08:00
|
|
|
|
/// <summary>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
/// 启动器调用,节点触发了中断。
|
2024-09-22 17:37:32 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid">节点</param>
|
|
|
|
|
|
/// <param name="expression">表达式</param>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
/// <param name="type">类型,0用户主动的中断,1表达式中断</param>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
public void TriggerInterrupt(string nodeGuid, string expression, InterruptTriggerEventArgs.InterruptTriggerType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnInterruptTrigger?.Invoke(new InterruptTriggerEventArgs(nodeGuid, expression, type));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 激活全局触发器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
public void ActivateFlipflopNode(string nodeGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var nodeModel = GuidToModel(nodeGuid);
|
|
|
|
|
|
if (nodeModel is null) return;
|
|
|
|
|
|
if (flowStarter is not null && nodeModel is SingleFlipflopNode flipflopNode) // 子节点为触发器
|
|
|
|
|
|
{
|
|
|
|
|
|
if (flowStarter.FlowState != RunState.Completion
|
|
|
|
|
|
&& flipflopNode.NotExitPreviousNode()) // 正在运行,且该触发器没有上游节点
|
|
|
|
|
|
{
|
|
|
|
|
|
_ = flowStarter.RunGlobalFlipflopAsync(this, flipflopNode);// 被父节点移除连接关系的子节点若为触发器,且无上级节点,则当前流程正在运行,则加载到运行环境中
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} /// <summary>
|
|
|
|
|
|
/// 关闭全局触发器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
public void TerminateFlipflopNode(string nodeGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var nodeModel = GuidToModel(nodeGuid);
|
|
|
|
|
|
if (nodeModel is null) return;
|
|
|
|
|
|
if (flowStarter is not null && nodeModel is SingleFlipflopNode flipflopNode) // 子节点为触发器
|
|
|
|
|
|
{
|
|
|
|
|
|
flowStarter.TerminateGlobalFlipflopRuning(flipflopNode);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
|
|
|
|
|
public Task<CancelType> GetOrCreateGlobalInterruptAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsGlobalInterrupt = true;
|
|
|
|
|
|
return ChannelFlowInterrupt.GetOrCreateChannelAsync(this.EnvName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-22 14:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Guid 转 NodeModel
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid">节点Guid</param>
|
|
|
|
|
|
/// <returns>节点Model</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentNullException">无法获取节点、Guid/节点为null时报错</exception>
|
2024-09-22 17:37:32 +08:00
|
|
|
|
private NodeModelBase? GuidToModel(string nodeGuid)
|
2024-09-22 14:10:13 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(nodeGuid))
|
2024-09-17 14:20:27 +08:00
|
|
|
|
{
|
2024-09-22 17:37:32 +08:00
|
|
|
|
//throw new ArgumentNullException("not contains - Guid没有对应节点:" + (nodeGuid));
|
|
|
|
|
|
return null;
|
2024-09-17 14:20:27 +08:00
|
|
|
|
}
|
2024-09-22 14:10:13 +08:00
|
|
|
|
if (!Nodes.TryGetValue(nodeGuid, out NodeModelBase? nodeModel) || nodeModel is null)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-22 17:37:32 +08:00
|
|
|
|
//throw new ArgumentNullException("null - Guid存在对应节点,但节点为null:" + (nodeGuid));
|
|
|
|
|
|
return null;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-22 14:10:13 +08:00
|
|
|
|
return nodeModel;
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-22 17:37:32 +08:00
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 私有方法
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载指定路径的DLL文件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dllPath"></param>
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
|
|
|
|
|
private void LoadDllNodeInfo(string dllPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
(var nodeLibrary, var registerTypes, var mdlist) = LoadAssembly(dllPath);
|
|
|
|
|
|
if (nodeLibrary is not null && mdlist.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
MethodDetailss.Add(nodeLibrary, mdlist);
|
|
|
|
|
|
NodeLibrarys.Add(nodeLibrary);
|
2024-10-10 10:45:53 +08:00
|
|
|
|
|
|
|
|
|
|
foreach(var kv in registerTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!AutoRegisterTypes.TryGetValue(kv.Key, out var types))
|
|
|
|
|
|
{
|
|
|
|
|
|
types = new List<Type>();
|
|
|
|
|
|
AutoRegisterTypes.Add(kv.Key, types);
|
|
|
|
|
|
}
|
|
|
|
|
|
types.AddRange(kv.Value);
|
|
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
|
|
|
|
|
OnDllLoad?.Invoke(new LoadDllEventArgs(nodeLibrary, mdlist)); // 通知UI创建dll面板显示
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-10 16:49:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除连接关系
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fromNodeGuid">起始节点Model</param>
|
|
|
|
|
|
/// <param name="toNodeGuid">目标节点Model</param>
|
|
|
|
|
|
/// <param name="connectionType">连接关系</param>
|
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
|
private void RemoteConnect(NodeModelBase fromNode, NodeModelBase toNode, ConnectionType connectionType)
|
|
|
|
|
|
{
|
|
|
|
|
|
fromNode.SuccessorNodes[connectionType].Remove(toNode);
|
|
|
|
|
|
toNode.PreviousNodes[connectionType].Remove(fromNode);
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
2024-10-10 16:49:37 +08:00
|
|
|
|
// 通知UI
|
|
|
|
|
|
OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(fromNode.Guid,
|
|
|
|
|
|
toNode.Guid,
|
|
|
|
|
|
connectionType,
|
|
|
|
|
|
NodeConnectChangeEventArgs.ConnectChangeType.Remote));
|
|
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
private (NodeLibrary?, Dictionary<RegisterSequence, List<Type>>, List<MethodDetails>) LoadAssembly(string dllPath)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Assembly assembly = Assembly.LoadFrom(dllPath); // 加载DLL文件
|
|
|
|
|
|
Type[] types = assembly.GetTypes(); // 获取程序集中的所有类型
|
|
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
Dictionary<RegisterSequence, List<Type>> autoRegisterTypes = new Dictionary<RegisterSequence, List<Type>>();
|
|
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
|
{
|
|
|
|
|
|
var autoRegisterAttribute = type.GetCustomAttribute<AutoRegisterAttribute>();
|
|
|
|
|
|
if (autoRegisterAttribute is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!autoRegisterTypes.TryGetValue(autoRegisterAttribute.Class,out var valus))
|
|
|
|
|
|
{
|
|
|
|
|
|
valus = new List<Type>();
|
|
|
|
|
|
autoRegisterTypes.Add(autoRegisterAttribute.Class, valus);
|
|
|
|
|
|
}
|
|
|
|
|
|
valus.Add(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Dictionary<Sequence, Type> autoRegisterTypes = assembly.GetTypes().Where(t => t.GetCustomAttribute<AutoRegisterAttribute>() is not null).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-28 23:55:19 +08:00
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
List<(Type, string)> scanTypes = types.Select(t => {
|
2024-10-07 15:15:18 +08:00
|
|
|
|
if (t.GetCustomAttribute<DynamicFlowAttribute>() is DynamicFlowAttribute dynamicFlowAttribute
|
|
|
|
|
|
&& dynamicFlowAttribute.Scan == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (t, dynamicFlowAttribute.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return (null, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).Where(it => it.t is not null) .ToList();
|
2024-09-15 12:15:32 +08:00
|
|
|
|
if (scanTypes.Count == 0)
|
|
|
|
|
|
{
|
2024-09-28 23:55:19 +08:00
|
|
|
|
return (null, [], []);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<MethodDetails> methodDetails = new List<MethodDetails>();
|
|
|
|
|
|
// 遍历扫描的类型
|
2024-10-07 15:15:18 +08:00
|
|
|
|
foreach ((var type,var flowName ) in scanTypes)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
2024-09-15 19:48:27 +08:00
|
|
|
|
// 加载DLL,创建 MethodDetails、实例作用对象、委托方法
|
2024-09-30 02:45:49 +08:00
|
|
|
|
var assemblyName = type.Assembly.GetName().Name;
|
2024-09-30 22:20:02 +08:00
|
|
|
|
if (string.IsNullOrEmpty(assemblyName))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2024-10-10 20:52:19 +08:00
|
|
|
|
var methods = NodeMethodDetailsHelper.GetMethodsToProcess(type);
|
2024-09-30 02:45:49 +08:00
|
|
|
|
foreach(var method in methods)
|
|
|
|
|
|
{
|
2024-10-10 20:52:19 +08:00
|
|
|
|
(var md, var del) = NodeMethodDetailsHelper.CreateMethodDetails(type, method, assemblyName);
|
2024-09-30 22:20:02 +08:00
|
|
|
|
if(md is null || del is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"无法加载方法信息:{assemblyName}-{type}-{method}");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2024-10-07 15:15:18 +08:00
|
|
|
|
md.MethodTips = flowName + md.MethodTips;
|
2024-09-30 02:45:49 +08:00
|
|
|
|
if (MethodDelegates.TryAdd(md.MethodName, del))
|
|
|
|
|
|
{
|
|
|
|
|
|
methodDetails.Add(md);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"节点委托创建失败:{md.MethodName}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//methodDetails.AddRange(itemMethodDetails);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
|
|
|
|
|
var nodeLibrary = new NodeLibrary
|
|
|
|
|
|
{
|
|
|
|
|
|
Assembly = assembly,
|
|
|
|
|
|
Path = dllPath,
|
|
|
|
|
|
};
|
|
|
|
|
|
//LoadedAssemblies.Add(assembly); // 将加载的程序集添加到列表中
|
|
|
|
|
|
//LoadedAssemblyPaths.Add(dllPath); // 记录加载的DLL路径
|
|
|
|
|
|
return (nodeLibrary, autoRegisterTypes , methodDetails);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(ex.ToString());
|
2024-09-28 23:55:19 +08:00
|
|
|
|
return (null, [],[]);
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-28 23:55:19 +08:00
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 运行环节加载了项目文件,需要创建节点控件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
/// <param name="methodDetailss"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
|
private NodeControlType GetNodeControlType(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 创建控件实例
|
|
|
|
|
|
NodeControlType controlType = nodeInfo.Type switch
|
|
|
|
|
|
{
|
|
|
|
|
|
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleActionNode)}" => NodeControlType.Action,// 动作节点控件
|
|
|
|
|
|
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleFlipflopNode)}" => NodeControlType.Flipflop, // 触发器节点控件
|
|
|
|
|
|
|
|
|
|
|
|
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleConditionNode)}" => NodeControlType.ExpCondition,// 条件表达式控件
|
|
|
|
|
|
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleExpOpNode)}" => NodeControlType.ExpOp, // 操作表达式控件
|
|
|
|
|
|
|
|
|
|
|
|
$"{NodeStaticConfig.NodeSpaceName}.{nameof(CompositeConditionNode)}" => NodeControlType.ConditionRegion, // 条件区域控件
|
|
|
|
|
|
_ => NodeControlType.None,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return controlType;
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeBase"></param>
|
2024-09-24 22:39:43 +08:00
|
|
|
|
private NodeModelBase CreateNode(NodeControlType nodeControlType, MethodDetails? methodDetails = null)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 确定创建的节点类型
|
|
|
|
|
|
Type? nodeType = nodeControlType switch
|
|
|
|
|
|
{
|
|
|
|
|
|
NodeControlType.Action => typeof(SingleActionNode),
|
|
|
|
|
|
NodeControlType.Flipflop => typeof(SingleFlipflopNode),
|
|
|
|
|
|
|
|
|
|
|
|
NodeControlType.ExpOp => typeof(SingleExpOpNode),
|
|
|
|
|
|
NodeControlType.ExpCondition => typeof(SingleConditionNode),
|
|
|
|
|
|
NodeControlType.ConditionRegion => typeof(CompositeConditionNode),
|
|
|
|
|
|
_ => null
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-09-25 22:20:23 +08:00
|
|
|
|
if (nodeType is null)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"节点类型错误[{nodeControlType}]");
|
|
|
|
|
|
}
|
|
|
|
|
|
// 生成实例
|
|
|
|
|
|
var nodeObj = Activator.CreateInstance(nodeType);
|
|
|
|
|
|
if (nodeObj is not NodeModelBase nodeBase)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"无法创建目标节点类型的实例[{nodeControlType}]");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 配置基础的属性
|
|
|
|
|
|
nodeBase.ControlType = nodeControlType;
|
|
|
|
|
|
if (methodDetails != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var md = methodDetails.Clone();
|
|
|
|
|
|
nodeBase.DisplayName = md.MethodTips;
|
|
|
|
|
|
nodeBase.MethodDetails = md;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是触发器,则需要添加到专属集合中
|
|
|
|
|
|
if (nodeControlType == NodeControlType.Flipflop && nodeBase is SingleFlipflopNode flipflopNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
var guid = flipflopNode.Guid;
|
|
|
|
|
|
if (!FlipflopNodes.Exists(it => it.Guid.Equals(guid)))
|
|
|
|
|
|
{
|
|
|
|
|
|
FlipflopNodes.Add(flipflopNode);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nodeBase;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool TryAddNode(NodeModelBase nodeModel)
|
|
|
|
|
|
{
|
|
|
|
|
|
nodeModel.Guid ??= Guid.NewGuid().ToString();
|
|
|
|
|
|
Nodes[nodeModel.Guid] = nodeModel;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fromNode">起始节点</param>
|
|
|
|
|
|
/// <param name="toNode">目标节点</param>
|
|
|
|
|
|
/// <param name="connectionType">连接关系</param>
|
|
|
|
|
|
private void ConnectNode(NodeModelBase fromNode, NodeModelBase toNode, ConnectionType connectionType)
|
|
|
|
|
|
{
|
2024-09-25 22:20:23 +08:00
|
|
|
|
if (fromNode is null || toNode is null || fromNode == toNode)
|
2024-09-15 12:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var ToExistOnFrom = true;
|
|
|
|
|
|
var FromExistInTo = true;
|
|
|
|
|
|
ConnectionType[] ct = [ConnectionType.IsSucceed,
|
|
|
|
|
|
ConnectionType.IsFail,
|
|
|
|
|
|
ConnectionType.IsError,
|
|
|
|
|
|
ConnectionType.Upstream];
|
2024-09-28 23:55:19 +08:00
|
|
|
|
|
|
|
|
|
|
if (toNode is SingleFlipflopNode flipflopNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
flowStarter?.TerminateGlobalFlipflopRuning(flipflopNode); // 假设被连接的是全局触发器,尝试移除
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
foreach (ConnectionType ctType in ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
var FToTo = fromNode.SuccessorNodes[ctType].Where(it => it.Guid.Equals(toNode.Guid)).ToArray();
|
|
|
|
|
|
var ToOnF = toNode.PreviousNodes[ctType].Where(it => it.Guid.Equals(fromNode.Guid)).ToArray();
|
|
|
|
|
|
ToExistOnFrom = FToTo.Length > 0;
|
|
|
|
|
|
FromExistInTo = ToOnF.Length > 0;
|
|
|
|
|
|
if (ToExistOnFrom && FromExistInTo)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("起始节点已与目标节点存在连接");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查是否可能存在异常
|
|
|
|
|
|
if (!ToExistOnFrom && FromExistInTo)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("目标节点不是起始节点的子节点,起始节点却是目标节点的父节点");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (ToExistOnFrom && !FromExistInTo)
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
Console.WriteLine(" 起始节点不是目标节点的父节点,目标节点却是起始节点的子节点");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else // if (!ToExistOnFrom && !FromExistInTo)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 可以正常连接
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fromNode.SuccessorNodes[connectionType].Add(toNode); // 添加到起始节点的子分支
|
|
|
|
|
|
toNode.PreviousNodes[connectionType].Add(fromNode); // 添加到目标节点的父分支
|
|
|
|
|
|
OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(fromNode.Guid,
|
|
|
|
|
|
toNode.Guid,
|
|
|
|
|
|
connectionType,
|
2024-09-16 21:38:34 +08:00
|
|
|
|
NodeConnectChangeEventArgs.ConnectChangeType.Create)); // 通知UI
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更改起点节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="newStartNode"></param>
|
|
|
|
|
|
/// <param name="oldStartNode"></param>
|
|
|
|
|
|
private void SetStartNode(NodeModelBase newStartNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldNodeGuid = StartNode?.Guid;
|
|
|
|
|
|
StartNode = newStartNode;
|
|
|
|
|
|
OnStartNodeChange?.Invoke(new StartNodeChangeEventArgs(oldNodeGuid, StartNode.Guid));
|
|
|
|
|
|
}
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 12:15:32 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-09-27 23:47:25 +08:00
|
|
|
|
#region 视觉效果
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 定位节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeGuid"></param>
|
|
|
|
|
|
public void NodeLocated(string nodeGuid)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnNodeLocate?.Invoke(new NodeLocatedEventArgs(nodeGuid));
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
#region IOC容器相关
|
|
|
|
|
|
ISereinIOC ISereinIOC.Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Reset();
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Register(Type type, params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Register(type, parameters);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Register<T>(params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Register<T>(parameters);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Register<TService, TImplementation>(params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Register<TService, TImplementation>(parameters);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//T ISereinIOC.GetOrRegisterInstantiate<T>()
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return sereinIOC.GetOrRegisterInstantiate<T>();
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//}
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
//object ISereinIOC.GetOrRegisterInstantiate(Type type)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return sereinIOC.GetOrRegisterInstantiate(type);
|
|
|
|
|
|
//}
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
object ISereinIOC.Get(Type type)
|
|
|
|
|
|
{
|
|
|
|
|
|
return sereinIOC.Get(type);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
T ISereinIOC.Get<T>()
|
|
|
|
|
|
{
|
|
|
|
|
|
return (T)sereinIOC.Get(typeof(T));
|
|
|
|
|
|
}
|
2024-09-24 22:39:43 +08:00
|
|
|
|
T ISereinIOC.Get<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return sereinIOC.Get<T>(key);
|
|
|
|
|
|
}
|
2024-09-30 02:45:49 +08:00
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
|
|
|
|
|
|
void ISereinIOC.CustomRegisterInstance(string key, object instance, bool needInjectProperty)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.CustomRegisterInstance(key, instance, needInjectProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-30 02:45:49 +08:00
|
|
|
|
object ISereinIOC.Instantiate(Type type)
|
2024-09-24 22:39:43 +08:00
|
|
|
|
{
|
2024-09-30 02:45:49 +08:00
|
|
|
|
return sereinIOC.Instantiate(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
T ISereinIOC.Instantiate<T>()
|
|
|
|
|
|
{
|
|
|
|
|
|
return sereinIOC.Instantiate<T>();
|
2024-09-24 22:39:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
ISereinIOC ISereinIOC.Build()
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Build();
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T>(Action<T> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2>(Action<T1, T2> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2, T3>(Action<T1, T2, T3> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5, T6>(Action<T1, T2, T3, T4, T5, T6> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5, T6, T7>(Action<T1, T2, T3, T4, T5, T6, T7> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5, T6, T7, T8>(Action<T1, T2, T3, T4, T5, T6, T7, T8> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
sereinIOC.Run(action);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
public static class FlowFunc
|
|
|
|
|
|
{
|
|
|
|
|
|
public static Library.Entity.Library ToLibrary(this Assembly assembly)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Library.Entity.Library
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = assembly.GetName().Name,
|
|
|
|
|
|
Path = assembly.Location,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2024-09-15 22:07:10 +08:00
|
|
|
|
|
|
|
|
|
|
public static ConnectionType ToContentType(this FlipflopStateType flowStateType)
|
|
|
|
|
|
{
|
|
|
|
|
|
return flowStateType switch
|
|
|
|
|
|
{
|
|
|
|
|
|
FlipflopStateType.Succeed => ConnectionType.IsSucceed,
|
|
|
|
|
|
FlipflopStateType.Fail => ConnectionType.IsFail,
|
|
|
|
|
|
FlipflopStateType.Error => ConnectionType.IsError,
|
|
|
|
|
|
FlipflopStateType.Cancel => ConnectionType.None,
|
|
|
|
|
|
_ => throw new NotImplementedException("未定义的流程状态")
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-24 22:39:43 +08:00
|
|
|
|
public static Type? ControlTypeToModel(this NodeControlType nodeControlType)
|
2024-09-17 14:20:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 确定创建的节点类型
|
|
|
|
|
|
Type? nodeType = nodeControlType switch
|
|
|
|
|
|
{
|
|
|
|
|
|
NodeControlType.Action => typeof(SingleActionNode),
|
|
|
|
|
|
NodeControlType.Flipflop => typeof(SingleFlipflopNode),
|
|
|
|
|
|
|
|
|
|
|
|
NodeControlType.ExpOp => typeof(SingleExpOpNode),
|
|
|
|
|
|
NodeControlType.ExpCondition => typeof(SingleConditionNode),
|
|
|
|
|
|
NodeControlType.ConditionRegion => typeof(CompositeConditionNode),
|
|
|
|
|
|
_ => null
|
|
|
|
|
|
};
|
|
|
|
|
|
return nodeType;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static NodeControlType ModelToControlType(this NodeControlType nodeControlType)
|
|
|
|
|
|
{
|
|
|
|
|
|
var type = nodeControlType.GetType();
|
|
|
|
|
|
NodeControlType controlType = type switch
|
|
|
|
|
|
{
|
|
|
|
|
|
Type when type == typeof(SingleActionNode) => NodeControlType.Action,
|
|
|
|
|
|
Type when type == typeof(SingleFlipflopNode) => NodeControlType.Flipflop,
|
|
|
|
|
|
|
|
|
|
|
|
Type when type == typeof(SingleExpOpNode) => NodeControlType.ExpOp,
|
|
|
|
|
|
Type when type == typeof(SingleConditionNode) => NodeControlType.ExpCondition,
|
|
|
|
|
|
Type when type == typeof(CompositeConditionNode) => NodeControlType.ConditionRegion,
|
|
|
|
|
|
_ => NodeControlType.None,
|
|
|
|
|
|
};
|
|
|
|
|
|
return controlType;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public static bool NotExitPreviousNode(this SingleFlipflopNode node)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConnectionType[] ct = [ConnectionType.IsSucceed,
|
|
|
|
|
|
ConnectionType.IsFail,
|
|
|
|
|
|
ConnectionType.IsError,
|
|
|
|
|
|
ConnectionType.Upstream];
|
|
|
|
|
|
foreach (ConnectionType ctType in ct)
|
|
|
|
|
|
{
|
2024-09-24 22:39:43 +08:00
|
|
|
|
if (node.PreviousNodes[ctType].Count > 0)
|
2024-09-16 21:38:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|