mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-13 03:16:35 +08:00
将部分节点基类与表达式工具类从nodeflow迁移到library,重写了环境与工作台的交互,解耦节点的获取,下一部分将尝试远程登录环境编辑流程。
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -20,3 +20,5 @@ obj/
|
|||||||
# 排除发布文件夹
|
# 排除发布文件夹
|
||||||
.Output/
|
.Output/
|
||||||
/.git1
|
/.git1
|
||||||
|
WorkBench.ControlLibrary.Core
|
||||||
|
WorkBench.Remote
|
||||||
@@ -67,7 +67,7 @@ namespace SereinFlowRemoteManagement
|
|||||||
await Console.Out.WriteLineAsync("启动远程管理模块");
|
await Console.Out.WriteLineAsync("启动远程管理模块");
|
||||||
await socketServer.StartAsync($"http://*:{ServerPort}/");
|
await socketServer.StartAsync($"http://*:{ServerPort}/");
|
||||||
});
|
});
|
||||||
SereinProjectData projectData = environment.SaveProject();
|
SereinProjectData projectData = environment.GetProjectInfo();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -83,32 +83,10 @@ namespace SereinFlowRemoteManagement
|
|||||||
{
|
{
|
||||||
await Send("尝试获取");
|
await Send("尝试获取");
|
||||||
|
|
||||||
Dictionary<NodeLibrary, List<MethodDetailsInfo>> LibraryMds = [];
|
|
||||||
|
|
||||||
foreach (var mdskv in environment.MethodDetailss)
|
|
||||||
{
|
|
||||||
var library = mdskv.Key;
|
|
||||||
var mds = mdskv.Value;
|
|
||||||
foreach (var md in mds)
|
|
||||||
{
|
|
||||||
if (!LibraryMds.TryGetValue(library, out var t_mds))
|
|
||||||
{
|
|
||||||
t_mds = new List<MethodDetailsInfo>();
|
|
||||||
LibraryMds[library] = t_mds;
|
|
||||||
}
|
|
||||||
var mdInfo = md.ToInfo();
|
|
||||||
mdInfo.LibraryName = library.Assembly.GetName().FullName;
|
|
||||||
t_mds.Add(mdInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var project = await GetProjectInfo();
|
var envInfo = this.environment.GetEnvInfo();
|
||||||
return new
|
return envInfo;
|
||||||
{
|
|
||||||
project = project,
|
|
||||||
envNode = LibraryMds.Values,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -128,9 +106,9 @@ namespace SereinFlowRemoteManagement
|
|||||||
throw new InvalidOperationException("类型错误");
|
throw new InvalidOperationException("类型错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.environment.TryGetMethodDetails(methodName,out var md))
|
if (this.environment.TryGetMethodDetailsInfo(methodName,out var mdInfo))
|
||||||
{
|
{
|
||||||
this.environment.CreateNode(connectionType, new Position(x, y), md); ;
|
this.environment.CreateNode(connectionType, new Position(x, y), mdInfo); ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -191,7 +169,7 @@ namespace SereinFlowRemoteManagement
|
|||||||
public async Task<SereinProjectData> GetProjectInfo()
|
public async Task<SereinProjectData> GetProjectInfo()
|
||||||
{
|
{
|
||||||
await Task.Delay(0);
|
await Task.Delay(0);
|
||||||
return environment.SaveProject();
|
return environment.GetProjectInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
FlowEdit/FlowEdit可视化流程编辑器beta.zip
Normal file
BIN
FlowEdit/FlowEdit可视化流程编辑器beta.zip
Normal file
Binary file not shown.
@@ -2,6 +2,7 @@
|
|||||||
using Serein.Library.Enums;
|
using Serein.Library.Enums;
|
||||||
using Serein.Library.Utils;
|
using Serein.Library.Utils;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -35,17 +36,13 @@ namespace Serein.Library.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 运行环境节点连接发生了改变
|
/// 运行环境节点连接发生了改变
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fromNodeGuid"></param>
|
/// <param name="eventArgs"></param>
|
||||||
/// <param name="toNodeGuid"></param>
|
|
||||||
/// <param name="connectionType"></param>
|
|
||||||
public delegate void NodeConnectChangeHandler(NodeConnectChangeEventArgs eventArgs);
|
public delegate void NodeConnectChangeHandler(NodeConnectChangeEventArgs eventArgs);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 环境中加载了一个节点
|
/// 环境中加载了一个节点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fromNodeGuid"></param>
|
/// <param name="eventArgs"></param>
|
||||||
/// <param name="toNodeGuid"></param>
|
|
||||||
/// <param name="connectionType"></param>
|
|
||||||
public delegate void NodeCreateHandler(NodeCreateEventArgs eventArgs);
|
public delegate void NodeCreateHandler(NodeCreateEventArgs eventArgs);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -72,6 +69,16 @@ namespace Serein.Library.Api
|
|||||||
/// <param name="eventArgs"></param>
|
/// <param name="eventArgs"></param>
|
||||||
public delegate void ExpInterruptTriggerHandler(InterruptTriggerEventArgs eventArgs);
|
public delegate void ExpInterruptTriggerHandler(InterruptTriggerEventArgs eventArgs);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// IOC容器发生变化
|
||||||
|
/// </summary>
|
||||||
|
public delegate void IOCMembersChangedHandler(IOCMembersChangedEventArgs eventArgs);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点需要定位
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="eventArgs"></param>
|
||||||
|
public delegate void NodeLocatedHandler(NodeLocatedEventArgs eventArgs);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -118,7 +125,7 @@ namespace Serein.Library.Api
|
|||||||
|
|
||||||
public class LoadDllEventArgs : FlowEventArgs
|
public class LoadDllEventArgs : FlowEventArgs
|
||||||
{
|
{
|
||||||
public LoadDllEventArgs(NodeLibrary nodeLibrary, List<MethodDetails> MethodDetailss)
|
public LoadDllEventArgs(NodeLibrary nodeLibrary, List<MethodDetailsInfo> MethodDetailss)
|
||||||
{
|
{
|
||||||
this.NodeLibrary = nodeLibrary;
|
this.NodeLibrary = nodeLibrary;
|
||||||
this.MethodDetailss = MethodDetailss;
|
this.MethodDetailss = MethodDetailss;
|
||||||
@@ -130,7 +137,7 @@ namespace Serein.Library.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// dll文件中有效的流程方法描述
|
/// dll文件中有效的流程方法描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<MethodDetails> MethodDetailss { get; protected set; }
|
public List<MethodDetailsInfo> MethodDetailss { get; protected set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RemoteDllEventArgs : FlowEventArgs
|
public class RemoteDllEventArgs : FlowEventArgs
|
||||||
@@ -251,11 +258,27 @@ namespace Serein.Library.Api
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class MonitorObjectEventArgs : FlowEventArgs
|
public class MonitorObjectEventArgs : FlowEventArgs
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变化的数据类别
|
||||||
|
/// </summary>
|
||||||
public enum ObjSourceType
|
public enum ObjSourceType
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 流程节点的数据
|
||||||
|
/// </summary>
|
||||||
NodeFlowData,
|
NodeFlowData,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// IOC容器对象
|
||||||
|
/// </summary>
|
||||||
IOCObj,
|
IOCObj,
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 在某个节点运行时,监听的数据发生了改变
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="nodeGuid"></param>
|
||||||
|
/// <param name="monitorData"></param>
|
||||||
|
/// <param name="objSourceType"></param>
|
||||||
public MonitorObjectEventArgs(string nodeGuid, object monitorData, ObjSourceType objSourceType)
|
public MonitorObjectEventArgs(string nodeGuid, object monitorData, ObjSourceType objSourceType)
|
||||||
{
|
{
|
||||||
NodeGuid = nodeGuid;
|
NodeGuid = nodeGuid;
|
||||||
@@ -267,6 +290,10 @@ namespace Serein.Library.Api
|
|||||||
/// 中断的节点Guid
|
/// 中断的节点Guid
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string NodeGuid { get; protected set; }
|
public string NodeGuid { get; protected set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 监听对象类别
|
||||||
|
/// </summary>
|
||||||
public ObjSourceType ObjSource { get; protected set; }
|
public ObjSourceType ObjSource { get; protected set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 新的数据
|
/// 新的数据
|
||||||
@@ -326,14 +353,8 @@ namespace Serein.Library.Api
|
|||||||
public string Expression { get; protected set; }
|
public string Expression { get; protected set; }
|
||||||
public InterruptTriggerType Type { get; protected set; }
|
public InterruptTriggerType Type { get; protected set; }
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// IOC容器发生变化
|
|
||||||
/// </summary>
|
|
||||||
public delegate void IOCMembersChangedHandler(IOCMembersChangedEventArgs eventArgs);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 流程事件签名基类
|
/// 流程事件签名基类
|
||||||
@@ -360,11 +381,6 @@ namespace Serein.Library.Api
|
|||||||
public object Instance { get; private set; }
|
public object Instance { get; private set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 节点需要定位
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="eventArgs"></param>
|
|
||||||
public delegate void NodeLocatedHandler(NodeLocatedEventArgs eventArgs);
|
|
||||||
|
|
||||||
public class NodeLocatedEventArgs : FlowEventArgs
|
public class NodeLocatedEventArgs : FlowEventArgs
|
||||||
{
|
{
|
||||||
@@ -375,6 +391,14 @@ namespace Serein.Library.Api
|
|||||||
public string NodeGuid { get; private set; }
|
public string NodeGuid { get; private set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 运行环境
|
||||||
|
/// </summary>
|
||||||
public interface IFlowEnvironment
|
public interface IFlowEnvironment
|
||||||
{
|
{
|
||||||
#region 属性
|
#region 属性
|
||||||
@@ -397,7 +421,7 @@ namespace Serein.Library.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// DLL中NodeAction特性的方法描述的所有原始副本
|
/// DLL中NodeAction特性的方法描述的所有原始副本
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Dictionary<NodeLibrary, List<MethodDetails>> MethodDetailss { get; }
|
// ConcurrentDictionary<string, MethodDetails> MethodDetailss { get; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -478,14 +502,19 @@ namespace Serein.Library.Api
|
|||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取方法描述
|
/// 获取方法描述信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name"></param>
|
/// <param name="methodName">方法描述</param>
|
||||||
/// <param name="md"></param>
|
/// <param name="mdInfo">方法信息</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool TryGetMethodDetails(string methodName, out MethodDetails md);
|
bool TryGetMethodDetailsInfo(string methodName, out MethodDetailsInfo mdInfo);
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定方法的Emit委托
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="methodName"></param>
|
||||||
|
/// <param name="del"></param>
|
||||||
|
/// <returns></returns>
|
||||||
bool TryGetDelegateDetails(string methodName, out DelegateDetails del);
|
bool TryGetDelegateDetails(string methodName, out DelegateDetails del);
|
||||||
|
|
||||||
//bool TryGetNodeData(string methodName, out NodeData node);
|
//bool TryGetNodeData(string methodName, out NodeData node);
|
||||||
@@ -496,13 +525,22 @@ namespace Serein.Library.Api
|
|||||||
/// 保存当前项目
|
/// 保存当前项目
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
SereinProjectData SaveProject();
|
SereinProjectData GetProjectInfo();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加载项目文件
|
/// 加载项目文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="projectFile"></param>
|
/// <param name="projectFile"></param>
|
||||||
/// <param name="filePath"></param>
|
/// <param name="filePath"></param>
|
||||||
void LoadProject(SereinProjectData projectFile, string filePath);
|
void LoadProject(SereinProjectData projectFile, string filePath);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载远程项目
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addres">远程项目地址</param>
|
||||||
|
/// <param name="port">远程项目端口</param>
|
||||||
|
/// <param name="token">密码</param>
|
||||||
|
void LoadRemoteProject(string addres,int port, string token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从文件中加载Dll
|
/// 从文件中加载Dll
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -511,7 +549,7 @@ namespace Serein.Library.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 移除DLL
|
/// 移除DLL
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dllPath"></param>
|
/// <param name="assemblyFullName">程序集的名称</param>
|
||||||
bool RemoteDll(string assemblyFullName);
|
bool RemoteDll(string assemblyFullName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -552,8 +590,10 @@ namespace Serein.Library.Api
|
|||||||
/// 创建节点/区域/基础控件
|
/// 创建节点/区域/基础控件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeBase">节点/区域/基础控件</param>
|
/// <param name="nodeBase">节点/区域/基础控件</param>
|
||||||
/// <param name="methodDetails">节点绑定的方法说明(</param>
|
/// <param name="position">节点在画布上的位置(</param>
|
||||||
void CreateNode(NodeControlType nodeBase, Position position, MethodDetails methodDetails = null);
|
/// <param name="methodDetailsInfo">节点绑定的方法说明(</param>
|
||||||
|
void CreateNode(NodeControlType nodeBase, Position position, MethodDetailsInfo methodDetailsInfo = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 移除两个节点之间的连接关系
|
/// 移除两个节点之间的连接关系
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -590,15 +630,15 @@ namespace Serein.Library.Api
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加作用于某个对象的中断表达式
|
/// 添加作用于某个对象的中断表达式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeGuid"></param>
|
/// <param name="key"></param>
|
||||||
/// <param name="expression"></param>
|
/// <param name="expression"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool AddInterruptExpression(string key, string expression);
|
bool AddInterruptExpression(string key, string expression);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 监视指定对象
|
/// 监视指定对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">需要监视的对象</param>
|
/// <param name="key">需要监视的对象</param>
|
||||||
/// <param name="isMonitor">是否启用监视</param>
|
/// <param name="isMonitor">是否启用监视</param>
|
||||||
void SetMonitorObjState(string key,bool isMonitor);
|
void SetMonitorObjState(string key,bool isMonitor);
|
||||||
|
|
||||||
@@ -620,6 +660,14 @@ namespace Serein.Library.Api
|
|||||||
Task<CancelType> GetOrCreateGlobalInterruptAsync();
|
Task<CancelType> GetOrCreateGlobalInterruptAsync();
|
||||||
|
|
||||||
|
|
||||||
|
#region 远程相关
|
||||||
|
/// <summary>
|
||||||
|
/// (适用于远程连接后获取环境的运行状态)获取当前环境的信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
object GetEnvInfo();
|
||||||
|
#endregion
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 启动器调用
|
#region 启动器调用
|
||||||
@@ -628,7 +676,8 @@ namespace Serein.Library.Api
|
|||||||
/// 流程启动器调用,监视数据更新通知
|
/// 流程启动器调用,监视数据更新通知
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeGuid">更新了数据的节点Guid</param>
|
/// <param name="nodeGuid">更新了数据的节点Guid</param>
|
||||||
/// <param name="flowData">更新的数据</param>
|
/// <param name="monitorData">更新的数据</param>
|
||||||
|
/// <param name="sourceType">更新的数据</param>
|
||||||
void MonitorObjectNotification(string nodeGuid, object monitorData, MonitorObjectEventArgs.ObjSourceType sourceType);
|
void MonitorObjectNotification(string nodeGuid, object monitorData, MonitorObjectEventArgs.ObjSourceType sourceType);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
225
Library/Entity/Base/NodeModelBaseData.cs
Normal file
225
Library/Entity/Base/NodeModelBaseData.cs
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
using Serein.Library.Api;
|
||||||
|
using Serein.Library.Entity;
|
||||||
|
using Serein.Library.Enums;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Base
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||||
|
{
|
||||||
|
|
||||||
|
public NodeModelBase()
|
||||||
|
{
|
||||||
|
PreviousNodes = [];
|
||||||
|
SuccessorNodes = [];
|
||||||
|
foreach (ConnectionType ctType in NodeStaticConfig.ConnectionTypes)
|
||||||
|
{
|
||||||
|
PreviousNodes[ctType] = new List<NodeModelBase>();
|
||||||
|
SuccessorNodes[ctType] = new List<NodeModelBase>();
|
||||||
|
}
|
||||||
|
DebugSetting = new NodeDebugSetting();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调试功能
|
||||||
|
/// </summary>
|
||||||
|
public NodeDebugSetting DebugSetting { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点对应的控件类型
|
||||||
|
/// </summary>
|
||||||
|
public NodeControlType ControlType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 方法描述,对应DLL的方法
|
||||||
|
/// </summary>
|
||||||
|
public MethodDetails MethodDetails { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点guid
|
||||||
|
/// </summary>
|
||||||
|
public string Guid { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 显示名称
|
||||||
|
/// </summary>
|
||||||
|
public string DisplayName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为起点控件
|
||||||
|
/// </summary>
|
||||||
|
public bool IsStart { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 运行时的上一节点
|
||||||
|
/// </summary>
|
||||||
|
public NodeModelBase PreviousNode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 不同分支的父节点
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<ConnectionType,List<NodeModelBase>> PreviousNodes { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 不同分支的子节点
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<ConnectionType,List<NodeModelBase>> SuccessorNodes { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前节点执行完毕后需要执行的下一个分支的类别
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionType NextOrientation { get; set; } = ConnectionType.None;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 运行时的异常信息(仅在 FlowState 为 Error 时存在对应值)
|
||||||
|
/// </summary>
|
||||||
|
public Exception RuningException { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 控制FlowData在同一时间只会被同一个线程更改。
|
||||||
|
/// </summary>
|
||||||
|
private readonly ReaderWriterLockSlim _flowDataLock = new ReaderWriterLockSlim();
|
||||||
|
private object _flowData;
|
||||||
|
/// <summary>
|
||||||
|
/// 当前传递数据(执行了节点对应的方法,才会存在值)。
|
||||||
|
/// </summary>
|
||||||
|
protected object FlowData
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
_flowDataLock.EnterReadLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _flowData;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_flowDataLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_flowDataLock.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_flowData = value;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_flowDataLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||||
|
/// </summary>
|
||||||
|
//public class NodeModelBaseBuilder
|
||||||
|
//{
|
||||||
|
// public NodeModelBaseBuilder(NodeModelBase builder)
|
||||||
|
// {
|
||||||
|
// this.ControlType = builder.ControlType;
|
||||||
|
// this.MethodDetails = builder.MethodDetails;
|
||||||
|
// this.Guid = builder.Guid;
|
||||||
|
// this.DisplayName = builder.DisplayName;
|
||||||
|
// this.IsStart = builder.IsStart;
|
||||||
|
// this.PreviousNode = builder.PreviousNode;
|
||||||
|
// this.PreviousNodes = builder.PreviousNodes;
|
||||||
|
// this.SucceedBranch = builder.SucceedBranch;
|
||||||
|
// this.FailBranch = builder.FailBranch;
|
||||||
|
// this.ErrorBranch = builder.ErrorBranch;
|
||||||
|
// this.UpstreamBranch = builder.UpstreamBranch;
|
||||||
|
// this.FlowState = builder.FlowState;
|
||||||
|
// this.RuningException = builder.RuningException;
|
||||||
|
// this.FlowData = builder.FlowData;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 节点对应的控件类型
|
||||||
|
// /// </summary>
|
||||||
|
// public NodeControlType ControlType { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 方法描述,对应DLL的方法
|
||||||
|
// /// </summary>
|
||||||
|
// public MethodDetails MethodDetails { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 节点guid
|
||||||
|
// /// </summary>
|
||||||
|
// public string Guid { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 显示名称
|
||||||
|
// /// </summary>
|
||||||
|
// public string DisplayName { get;}
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 是否为起点控件
|
||||||
|
// /// </summary>
|
||||||
|
// public bool IsStart { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 运行时的上一节点
|
||||||
|
// /// </summary>
|
||||||
|
// public NodeModelBase? PreviousNode { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 上一节点集合
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> PreviousNodes { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 下一节点集合(真分支)
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> SucceedBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 下一节点集合(假分支)
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> FailBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 异常分支
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> ErrorBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 上游分支
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> UpstreamBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 当前执行状态(进入真分支还是假分支,异常分支在异常中确定)
|
||||||
|
// /// </summary>
|
||||||
|
// public FlowStateType FlowState { get; set; } = FlowStateType.None;
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 运行时的异常信息(仅在 FlowState 为 Error 时存在对应值)
|
||||||
|
// /// </summary>
|
||||||
|
// public Exception RuningException { get; set; } = null;
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 当前传递数据(执行了节点对应的方法,才会存在值)
|
||||||
|
// /// </summary>
|
||||||
|
// public object? FlowData { get; set; } = null;
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
457
Library/Entity/Base/NodeModelBaseFunc.cs
Normal file
457
Library/Entity/Base/NodeModelBaseFunc.cs
Normal file
@@ -0,0 +1,457 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Serein.Library.Api;
|
||||||
|
using Serein.Library.Attributes;
|
||||||
|
using Serein.Library.Entity;
|
||||||
|
using Serein.Library.Enums;
|
||||||
|
using Serein.Library.Ex;
|
||||||
|
using Serein.Library.Utils;
|
||||||
|
using Serein.NodeFlow.Tool;
|
||||||
|
using Serein.NodeFlow.Tool.SereinExpression;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Base
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
#region 调试中断
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 不再中断
|
||||||
|
/// </summary>
|
||||||
|
public void CancelInterrupt()
|
||||||
|
{
|
||||||
|
this.DebugSetting.InterruptClass = InterruptClass.None;
|
||||||
|
DebugSetting.CancelInterruptCallback?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 导出/导入项目文件节点信息
|
||||||
|
|
||||||
|
internal abstract Parameterdata[] GetParameterdatas();
|
||||||
|
public virtual NodeInfo ToInfo()
|
||||||
|
{
|
||||||
|
// if (MethodDetails == null) return null;
|
||||||
|
|
||||||
|
var trueNodes = SuccessorNodes[ConnectionType.IsSucceed].Select(item => item.Guid); // 真分支
|
||||||
|
var falseNodes = SuccessorNodes[ConnectionType.IsFail].Select(item => item.Guid);// 假分支
|
||||||
|
var errorNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 异常分支
|
||||||
|
var upstreamNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 上游分支
|
||||||
|
|
||||||
|
// 生成参数列表
|
||||||
|
Parameterdata[] parameterData = GetParameterdatas();
|
||||||
|
|
||||||
|
return new NodeInfo
|
||||||
|
{
|
||||||
|
Guid = Guid,
|
||||||
|
MethodName = MethodDetails?.MethodName,
|
||||||
|
Label = DisplayName ?? "",
|
||||||
|
Type = this.GetType().ToString(),
|
||||||
|
TrueNodes = trueNodes.ToArray(),
|
||||||
|
FalseNodes = falseNodes.ToArray(),
|
||||||
|
UpstreamNodes = upstreamNodes.ToArray(),
|
||||||
|
ParameterData = parameterData.ToArray(),
|
||||||
|
ErrorNodes = errorNodes.ToArray(),
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||||
|
{
|
||||||
|
this.Guid = nodeInfo.Guid;
|
||||||
|
if (this.MethodDetails is not null)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
|
||||||
|
{
|
||||||
|
Parameterdata? pd = nodeInfo.ParameterData[i];
|
||||||
|
this.MethodDetails.ParameterDetailss[i].IsExplicitData = pd.State;
|
||||||
|
this.MethodDetails.ParameterDetailss[i].DataValue = pd.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 节点方法的执行
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否应该退出执行
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="flowCts"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsBradk(IDynamicContext context, CancellationTokenSource? flowCts)
|
||||||
|
{
|
||||||
|
// 上下文不再执行
|
||||||
|
if(context.RunState == RunState.Completion)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 不存在全局触发器时,流程运行状态被设置为完成,退出执行,用于打断无限循环分支。
|
||||||
|
if (flowCts is null && context.Env.FlowState == RunState.Completion)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// 如果存在全局触发器,且触发器的执行任务已经被取消时,退出执行。
|
||||||
|
if (flowCts is not null)
|
||||||
|
{
|
||||||
|
if (flowCts.IsCancellationRequested)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始执行
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task StartFlowAsync(IDynamicContext context)
|
||||||
|
{
|
||||||
|
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
|
||||||
|
stack.Push(this);
|
||||||
|
var flowCts = context.Env.IOC.Get<CancellationTokenSource>(FlowStarter.FlipFlopCtsName);
|
||||||
|
bool hasFlipflow = flowCts != null;
|
||||||
|
while (stack.Count > 0) // 循环中直到栈为空才会退出循环
|
||||||
|
{
|
||||||
|
await Task.Delay(0);
|
||||||
|
// 从栈中弹出一个节点作为当前节点进行处理
|
||||||
|
var currentNode = stack.Pop();
|
||||||
|
|
||||||
|
#region 执行相关
|
||||||
|
|
||||||
|
// 筛选出上游分支
|
||||||
|
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream].ToArray();
|
||||||
|
for (int index = 0; index < upstreamNodes.Length; index++)
|
||||||
|
{
|
||||||
|
NodeModelBase? upstreamNode = upstreamNodes[index];
|
||||||
|
if (upstreamNode is not null && upstreamNode.DebugSetting.IsEnable)
|
||||||
|
{
|
||||||
|
if (upstreamNode.DebugSetting.InterruptClass != InterruptClass.None) // 执行触发前
|
||||||
|
{
|
||||||
|
var cancelType = await upstreamNode.DebugSetting.GetInterruptTask();
|
||||||
|
await Console.Out.WriteLineAsync($"[{upstreamNode.MethodDetails?.MethodName}]中断已{cancelType},开始执行后继分支");
|
||||||
|
}
|
||||||
|
upstreamNode.PreviousNode = currentNode;
|
||||||
|
await upstreamNode.StartFlowAsync(context); // 执行流程节点的上游分支
|
||||||
|
if (upstreamNode.NextOrientation == ConnectionType.IsError)
|
||||||
|
{
|
||||||
|
// 如果上游分支执行失败,不再继续执行
|
||||||
|
// 使上游节点(仅上游节点本身,不包含上游节点的后继节点)
|
||||||
|
// 具备通过抛出异常中断流程的能力
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (IsBradk(context, flowCts)) break; // 退出执行
|
||||||
|
// 上游分支执行完成,才执行当前节点
|
||||||
|
object? newFlowData = await currentNode.ExecutingAsync(context);
|
||||||
|
if (IsBradk(context, flowCts)) break; // 退出执行
|
||||||
|
|
||||||
|
await RefreshFlowDataAndExpInterrupt(context, currentNode, newFlowData); // 执行当前节点后刷新数据
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region 执行完成
|
||||||
|
|
||||||
|
// 选择后继分支
|
||||||
|
var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation];
|
||||||
|
|
||||||
|
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||||
|
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
// 筛选出启用的节点的节点
|
||||||
|
if (nextNodes[i].DebugSetting.IsEnable)
|
||||||
|
{
|
||||||
|
nextNodes[i].PreviousNode = currentNode;
|
||||||
|
stack.Push(nextNodes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行节点对应的方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">流程上下文</param>
|
||||||
|
/// <returns>节点传回数据对象</returns>
|
||||||
|
public virtual async Task<object?> ExecutingAsync(IDynamicContext context)
|
||||||
|
{
|
||||||
|
#region 调试中断
|
||||||
|
|
||||||
|
if (DebugSetting.InterruptClass != InterruptClass.None) // 执行触发检查是否需要中断
|
||||||
|
{
|
||||||
|
var cancelType = await this.DebugSetting.GetInterruptTask(); // 等待中断结束
|
||||||
|
await Console.Out.WriteLineAsync($"[{this.MethodDetails?.MethodName}]中断已{cancelType},开始执行后继分支");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
MethodDetails? md = MethodDetails;
|
||||||
|
//var del = md.MethodDelegate.Clone();
|
||||||
|
if (md is null)
|
||||||
|
{
|
||||||
|
throw new Exception($"节点{this.Guid}不存在方法信息,请检查是否需要重写节点的ExecutingAsync");
|
||||||
|
}
|
||||||
|
if (!context.Env.TryGetDelegateDetails(md.MethodName, out var dd))
|
||||||
|
{
|
||||||
|
throw new Exception($"节点{this.Guid}不存在对应委托");
|
||||||
|
}
|
||||||
|
md.ActingInstance ??= context.Env.IOC.Get(md.ActingInstanceType);
|
||||||
|
object instance = md.ActingInstance;
|
||||||
|
|
||||||
|
|
||||||
|
object? result = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
object?[]? args = GetParameters(context, this, md);
|
||||||
|
result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||||
|
NextOrientation = ConnectionType.IsSucceed;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await Console.Out.WriteLineAsync($"节点[{this.MethodDetails?.MethodName}]异常:" + ex);
|
||||||
|
NextOrientation = ConnectionType.IsError;
|
||||||
|
RuningException = ex;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取对应的参数数组
|
||||||
|
/// </summary>
|
||||||
|
public static object?[]? GetParameters(IDynamicContext context, NodeModelBase nodeModel, MethodDetails md)
|
||||||
|
{
|
||||||
|
// 用正确的大小初始化参数数组
|
||||||
|
if (md.ParameterDetailss.Length == 0)
|
||||||
|
{
|
||||||
|
return null;// md.ActingInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
object?[]? parameters = new object[md.ParameterDetailss.Length];
|
||||||
|
var flowData = nodeModel.PreviousNode?.FlowData; // 当前传递的数据
|
||||||
|
var previousDataType = flowData?.GetType();
|
||||||
|
|
||||||
|
for (int i = 0; i < parameters.Length; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
object? inputParameter; // 存放解析的临时参数
|
||||||
|
var ed = md.ParameterDetailss[i]; // 方法入参描述
|
||||||
|
|
||||||
|
|
||||||
|
if (ed.IsExplicitData) // 判断是否使用显示的输入参数
|
||||||
|
{
|
||||||
|
if (ed.DataValue.StartsWith("@get", StringComparison.OrdinalIgnoreCase) && flowData is not null)
|
||||||
|
{
|
||||||
|
// 执行表达式从上一节点获取对象
|
||||||
|
inputParameter = SerinExpressionEvaluator.Evaluate(ed.DataValue, flowData, out _);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 使用输入的固定值
|
||||||
|
inputParameter = ed.DataValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inputParameter = flowData; // 使用上一节点的对象
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入参存在取值转换器
|
||||||
|
if (ed.ExplicitType.IsEnum && ed.Convertor is not null)
|
||||||
|
{
|
||||||
|
if (Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum))
|
||||||
|
{
|
||||||
|
var value = ed.Convertor(resultEnum);
|
||||||
|
if (value is not null)
|
||||||
|
{
|
||||||
|
parameters[i] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("转换器调用失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入参存在类型转换器,获取枚举转换器中记录的枚举
|
||||||
|
if (ed.ExplicitType.IsEnum && ed.DataType != ed.ExplicitType)
|
||||||
|
{
|
||||||
|
if (Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum)) // 获取对应的枚举项
|
||||||
|
{
|
||||||
|
// 获取绑定的类型
|
||||||
|
var type = EnumHelper.GetBoundValue(ed.ExplicitType, resultEnum, attr => attr.Value);
|
||||||
|
if (type is Type enumBindType && enumBindType is not null)
|
||||||
|
{
|
||||||
|
var value = context.Env.IOC.Instantiate(enumBindType);
|
||||||
|
if (value is not null)
|
||||||
|
{
|
||||||
|
parameters[i] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (ed.DataType.IsValueType)
|
||||||
|
{
|
||||||
|
var valueStr = inputParameter?.ToString();
|
||||||
|
parameters[i] = valueStr.ToValueData(ed.DataType);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var valueStr = inputParameter?.ToString();
|
||||||
|
parameters[i] = ed.DataType switch
|
||||||
|
{
|
||||||
|
Type t when t == typeof(string) => valueStr,
|
||||||
|
Type t when t == typeof(IDynamicContext) => context, // 上下文
|
||||||
|
Type t when t == typeof(DateTime) => string.IsNullOrEmpty(valueStr) ? 0 : DateTime.Parse(valueStr),
|
||||||
|
|
||||||
|
Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||||
|
Type t when t == typeof(NodeModelBase) => nodeModel, // 节点实体类
|
||||||
|
|
||||||
|
Type t when t.IsArray => (inputParameter as Array)?.Cast<object>().ToList(),
|
||||||
|
Type t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => inputParameter,
|
||||||
|
_ => inputParameter,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新节点数据,并检查监视表达式是否生效
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">上下文</param>
|
||||||
|
/// <param name="nodeModel">节点Moel</param>
|
||||||
|
/// <param name="newData">新的数据</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task RefreshFlowDataAndExpInterrupt(IDynamicContext context, NodeModelBase nodeModel, object? newData = null)
|
||||||
|
{
|
||||||
|
string guid = nodeModel.Guid;
|
||||||
|
if (newData is not null)
|
||||||
|
{
|
||||||
|
await MonitorObjExpInterrupt(context, nodeModel, newData, 0); // 首先监视对象
|
||||||
|
await MonitorObjExpInterrupt(context, nodeModel, newData, 1); // 然后监视节点
|
||||||
|
nodeModel.FlowData = newData; // 替换数据
|
||||||
|
context.AddOrUpdate(guid, nodeModel); // 上下文中更新数据
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task MonitorObjExpInterrupt(IDynamicContext context, NodeModelBase nodeModel, object? data, int monitorType)
|
||||||
|
{
|
||||||
|
MonitorObjectEventArgs.ObjSourceType sourceType;
|
||||||
|
string? key;
|
||||||
|
if (monitorType == 0)
|
||||||
|
{
|
||||||
|
key = data?.GetType()?.FullName;
|
||||||
|
sourceType = MonitorObjectEventArgs.ObjSourceType.IOCObj;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
key = nodeModel.Guid;
|
||||||
|
sourceType = MonitorObjectEventArgs.ObjSourceType.IOCObj;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.Env.CheckObjMonitorState(key, out List<string> exps)) // 如果新的数据处于查看状态,通知UI进行更新?交给运行环境判断?
|
||||||
|
{
|
||||||
|
context.Env.MonitorObjectNotification(nodeModel.Guid, data, sourceType); // 对象处于监视状态,通知UI更新数据显示
|
||||||
|
if (exps.Count > 0)
|
||||||
|
{
|
||||||
|
// 表达式环境下判断是否需要执行中断
|
||||||
|
bool isExpInterrupt = false;
|
||||||
|
string? exp = "";
|
||||||
|
// 判断执行监视表达式,直到为 true 时退出
|
||||||
|
for (int i = 0; i < exps.Count && !isExpInterrupt; i++)
|
||||||
|
{
|
||||||
|
exp = exps[i];
|
||||||
|
if (string.IsNullOrEmpty(exp)) continue;
|
||||||
|
isExpInterrupt = SereinConditionParser.To(data, exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isExpInterrupt) // 触发中断
|
||||||
|
{
|
||||||
|
InterruptClass interruptClass = InterruptClass.Branch; // 分支中断
|
||||||
|
if (context.Env.SetNodeInterrupt(nodeModel.Guid, interruptClass))
|
||||||
|
{
|
||||||
|
context.Env.TriggerInterrupt(nodeModel.Guid, exp, InterruptTriggerEventArgs.InterruptTriggerType.Exp);
|
||||||
|
var cancelType = await nodeModel.DebugSetting.GetInterruptTask();
|
||||||
|
await Console.Out.WriteLineAsync($"[{data}]中断已{cancelType},开始执行后继分支");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 释放对象
|
||||||
|
/// </summary>
|
||||||
|
public void ReleaseFlowData()
|
||||||
|
{
|
||||||
|
if (typeof(IDisposable).IsAssignableFrom(FlowData?.GetType()) && FlowData is IDisposable disposable)
|
||||||
|
{
|
||||||
|
disposable?.Dispose();
|
||||||
|
}
|
||||||
|
this.FlowData = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取节点数据
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public object? GetFlowData()
|
||||||
|
{
|
||||||
|
return this.FlowData;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,11 +14,21 @@ namespace Serein.Library.Entity
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DelegateDetails
|
public class DelegateDetails
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 记录Emit委托
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="EmitMethodType"></param>
|
||||||
|
/// <param name="EmitDelegate"></param>
|
||||||
public DelegateDetails(EmitMethodType EmitMethodType, Delegate EmitDelegate)
|
public DelegateDetails(EmitMethodType EmitMethodType, Delegate EmitDelegate)
|
||||||
{
|
{
|
||||||
this._emitMethodType = EmitMethodType;
|
this._emitMethodType = EmitMethodType;
|
||||||
this._emitDelegate = EmitDelegate;
|
this._emitDelegate = EmitDelegate;
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 更新委托方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="EmitMethodType"></param>
|
||||||
|
/// <param name="EmitDelegate"></param>
|
||||||
public void Upload(EmitMethodType EmitMethodType, Delegate EmitDelegate)
|
public void Upload(EmitMethodType EmitMethodType, Delegate EmitDelegate)
|
||||||
{
|
{
|
||||||
_emitMethodType = EmitMethodType;
|
_emitMethodType = EmitMethodType;
|
||||||
@@ -26,18 +36,30 @@ namespace Serein.Library.Entity
|
|||||||
}
|
}
|
||||||
private Delegate _emitDelegate;
|
private Delegate _emitDelegate;
|
||||||
private EmitMethodType _emitMethodType;
|
private EmitMethodType _emitMethodType;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <para>普通方法:Func<object,object[],object></para>
|
||||||
|
/// <para>异步方法:Func<object,object[],Task></para>
|
||||||
|
/// <para>异步有返回值方法:Func<object,object[],Task<object>></para>
|
||||||
|
/// </summary>
|
||||||
public Delegate EmitDelegate { get => _emitDelegate; }
|
public Delegate EmitDelegate { get => _emitDelegate; }
|
||||||
|
/// <summary>
|
||||||
|
/// 表示Emit构造的委托类型
|
||||||
|
/// </summary>
|
||||||
public EmitMethodType EmitMethodType { get => _emitMethodType; }
|
public EmitMethodType EmitMethodType { get => _emitMethodType; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步等待Emit创建的委托。
|
/// <para>使用的实例必须能够正确调用该委托,传入的参数也必须符合方法入参信息。</para>
|
||||||
/// 需要注意的是,传入的实例必须包含创建委托的方法信息,传入的参数也必须符合方法入参信息。
|
/// </summary>
|
||||||
/// </summary>
|
|
||||||
/// <param name="instance">实例</param>
|
/// <param name="instance">实例</param>
|
||||||
/// <param name="args">入参</param>
|
/// <param name="args">入参</param>
|
||||||
/// <returns>返回值</returns>
|
/// <returns>void方法自动返回null</returns>
|
||||||
public async Task<object> Invoke(object instance, object[] args)
|
public async Task<object> InvokeAsync(object instance, object[] args)
|
||||||
{
|
{
|
||||||
|
if(args is null)
|
||||||
|
{
|
||||||
|
args = new object[0];
|
||||||
|
}
|
||||||
object result = null;
|
object result = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ namespace Serein.Library.Entity
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 节点类型
|
/// 节点类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string NodeType { get; set; }
|
public NodeType NodeType { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 方法说明
|
/// 方法说明
|
||||||
@@ -39,7 +39,6 @@ namespace Serein.Library.Entity
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 出参类型
|
/// 出参类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
public string ReturnTypeFullName { get; set; }
|
public string ReturnTypeFullName { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +59,7 @@ namespace Serein.Library.Entity
|
|||||||
{
|
{
|
||||||
MethodName = MethodName,
|
MethodName = MethodName,
|
||||||
MethodTips = MethodTips,
|
MethodTips = MethodTips,
|
||||||
NodeType = MethodDynamicType.ToString(),
|
NodeType = MethodDynamicType,
|
||||||
ParameterDetailsInfos = this.ParameterDetailss.Select(p => p.ToInfo()).ToArray(),
|
ParameterDetailsInfos = this.ParameterDetailss.Select(p => p.ToInfo()).ToArray(),
|
||||||
ReturnTypeFullName = ReturnType.FullName,
|
ReturnTypeFullName = ReturnType.FullName,
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ namespace Serein.Library.Entity
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Path { get; set; }
|
public string Path { get; set; }
|
||||||
|
|
||||||
|
public string Name{ get; set; }
|
||||||
public Assembly Assembly { get; set; }
|
public Assembly Assembly { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
225
Library/Entity/NodeModelBaseData.cs
Normal file
225
Library/Entity/NodeModelBaseData.cs
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
using Serein.Library.Api;
|
||||||
|
using Serein.Library.Entity;
|
||||||
|
using Serein.Library.Enums;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Base
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||||
|
{
|
||||||
|
|
||||||
|
public NodeModelBase()
|
||||||
|
{
|
||||||
|
PreviousNodes = new Dictionary<ConnectionType, List<NodeModelBase>>();
|
||||||
|
SuccessorNodes = new Dictionary<ConnectionType, List<NodeModelBase>>();
|
||||||
|
foreach (ConnectionType ctType in NodeStaticConfig.ConnectionTypes)
|
||||||
|
{
|
||||||
|
PreviousNodes[ctType] = new List<NodeModelBase>();
|
||||||
|
SuccessorNodes[ctType] = new List<NodeModelBase>();
|
||||||
|
}
|
||||||
|
DebugSetting = new NodeDebugSetting();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调试功能
|
||||||
|
/// </summary>
|
||||||
|
public NodeDebugSetting DebugSetting { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点对应的控件类型
|
||||||
|
/// </summary>
|
||||||
|
public NodeControlType ControlType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 方法描述,对应DLL的方法
|
||||||
|
/// </summary>
|
||||||
|
public MethodDetails MethodDetails { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点guid
|
||||||
|
/// </summary>
|
||||||
|
public string Guid { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 显示名称
|
||||||
|
/// </summary>
|
||||||
|
public string DisplayName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为起点控件
|
||||||
|
/// </summary>
|
||||||
|
public bool IsStart { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 运行时的上一节点
|
||||||
|
/// </summary>
|
||||||
|
public NodeModelBase PreviousNode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 不同分支的父节点
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<ConnectionType,List<NodeModelBase>> PreviousNodes { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 不同分支的子节点
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<ConnectionType,List<NodeModelBase>> SuccessorNodes { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前节点执行完毕后需要执行的下一个分支的类别
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionType NextOrientation { get; set; } = ConnectionType.None;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 运行时的异常信息(仅在 FlowState 为 Error 时存在对应值)
|
||||||
|
/// </summary>
|
||||||
|
public Exception RuningException { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 控制FlowData在同一时间只会被同一个线程更改。
|
||||||
|
/// </summary>
|
||||||
|
private readonly ReaderWriterLockSlim _flowDataLock = new ReaderWriterLockSlim();
|
||||||
|
private object _flowData;
|
||||||
|
/// <summary>
|
||||||
|
/// 当前传递数据(执行了节点对应的方法,才会存在值)。
|
||||||
|
/// </summary>
|
||||||
|
protected object FlowData
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
_flowDataLock.EnterReadLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _flowData;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_flowDataLock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_flowDataLock.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_flowData = value;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_flowDataLock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||||
|
/// </summary>
|
||||||
|
//public class NodeModelBaseBuilder
|
||||||
|
//{
|
||||||
|
// public NodeModelBaseBuilder(NodeModelBase builder)
|
||||||
|
// {
|
||||||
|
// this.ControlType = builder.ControlType;
|
||||||
|
// this.MethodDetails = builder.MethodDetails;
|
||||||
|
// this.Guid = builder.Guid;
|
||||||
|
// this.DisplayName = builder.DisplayName;
|
||||||
|
// this.IsStart = builder.IsStart;
|
||||||
|
// this.PreviousNode = builder.PreviousNode;
|
||||||
|
// this.PreviousNodes = builder.PreviousNodes;
|
||||||
|
// this.SucceedBranch = builder.SucceedBranch;
|
||||||
|
// this.FailBranch = builder.FailBranch;
|
||||||
|
// this.ErrorBranch = builder.ErrorBranch;
|
||||||
|
// this.UpstreamBranch = builder.UpstreamBranch;
|
||||||
|
// this.FlowState = builder.FlowState;
|
||||||
|
// this.RuningException = builder.RuningException;
|
||||||
|
// this.FlowData = builder.FlowData;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 节点对应的控件类型
|
||||||
|
// /// </summary>
|
||||||
|
// public NodeControlType ControlType { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 方法描述,对应DLL的方法
|
||||||
|
// /// </summary>
|
||||||
|
// public MethodDetails MethodDetails { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 节点guid
|
||||||
|
// /// </summary>
|
||||||
|
// public string Guid { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 显示名称
|
||||||
|
// /// </summary>
|
||||||
|
// public string DisplayName { get;}
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 是否为起点控件
|
||||||
|
// /// </summary>
|
||||||
|
// public bool IsStart { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 运行时的上一节点
|
||||||
|
// /// </summary>
|
||||||
|
// public NodeModelBase? PreviousNode { get; }
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 上一节点集合
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> PreviousNodes { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 下一节点集合(真分支)
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> SucceedBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 下一节点集合(假分支)
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> FailBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 异常分支
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> ErrorBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 上游分支
|
||||||
|
// /// </summary>
|
||||||
|
// public List<NodeModelBase> UpstreamBranch { get; } = [];
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 当前执行状态(进入真分支还是假分支,异常分支在异常中确定)
|
||||||
|
// /// </summary>
|
||||||
|
// public FlowStateType FlowState { get; set; } = FlowStateType.None;
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 运行时的异常信息(仅在 FlowState 为 Error 时存在对应值)
|
||||||
|
// /// </summary>
|
||||||
|
// public Exception RuningException { get; set; } = null;
|
||||||
|
|
||||||
|
// /// <summary>
|
||||||
|
// /// 当前传递数据(执行了节点对应的方法,才会存在值)
|
||||||
|
// /// </summary>
|
||||||
|
// public object? FlowData { get; set; } = null;
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
487
Library/Entity/NodeModelBaseFunc.cs
Normal file
487
Library/Entity/NodeModelBaseFunc.cs
Normal file
@@ -0,0 +1,487 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Serein.Library.Api;
|
||||||
|
using Serein.Library.Attributes;
|
||||||
|
using Serein.Library.Entity;
|
||||||
|
using Serein.Library.Enums;
|
||||||
|
using Serein.Library.Ex;
|
||||||
|
using Serein.Library.Utils;
|
||||||
|
using Serein.NodeFlow.Tool.SereinExpression;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Base
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||||
|
/// </summary>
|
||||||
|
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
#region 调试中断
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 不再中断
|
||||||
|
/// </summary>
|
||||||
|
public void CancelInterrupt()
|
||||||
|
{
|
||||||
|
this.DebugSetting.InterruptClass = InterruptClass.None;
|
||||||
|
DebugSetting.CancelInterruptCallback?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 导出/导入项目文件节点信息
|
||||||
|
|
||||||
|
public abstract Parameterdata[] GetParameterdatas();
|
||||||
|
public virtual NodeInfo ToInfo()
|
||||||
|
{
|
||||||
|
// if (MethodDetails == null) return null;
|
||||||
|
|
||||||
|
var trueNodes = SuccessorNodes[ConnectionType.IsSucceed].Select(item => item.Guid); // 真分支
|
||||||
|
var falseNodes = SuccessorNodes[ConnectionType.IsFail].Select(item => item.Guid);// 假分支
|
||||||
|
var errorNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 异常分支
|
||||||
|
var upstreamNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 上游分支
|
||||||
|
|
||||||
|
// 生成参数列表
|
||||||
|
Parameterdata[] parameterData = GetParameterdatas();
|
||||||
|
|
||||||
|
return new NodeInfo
|
||||||
|
{
|
||||||
|
Guid = Guid,
|
||||||
|
MethodName = MethodDetails?.MethodName,
|
||||||
|
Label = DisplayName ?? "",
|
||||||
|
Type = this.GetType().ToString(),
|
||||||
|
TrueNodes = trueNodes.ToArray(),
|
||||||
|
FalseNodes = falseNodes.ToArray(),
|
||||||
|
UpstreamNodes = upstreamNodes.ToArray(),
|
||||||
|
ParameterData = parameterData.ToArray(),
|
||||||
|
ErrorNodes = errorNodes.ToArray(),
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||||
|
{
|
||||||
|
this.Guid = nodeInfo.Guid;
|
||||||
|
if (this.MethodDetails != null)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
|
||||||
|
{
|
||||||
|
Parameterdata pd = nodeInfo.ParameterData[i];
|
||||||
|
this.MethodDetails.ParameterDetailss[i].IsExplicitData = pd.State;
|
||||||
|
this.MethodDetails.ParameterDetailss[i].DataValue = pd.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 节点方法的执行
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否应该退出执行
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="flowCts"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsBradk(IDynamicContext context, CancellationTokenSource flowCts)
|
||||||
|
{
|
||||||
|
// 上下文不再执行
|
||||||
|
if(context.RunState == RunState.Completion)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 不存在全局触发器时,流程运行状态被设置为完成,退出执行,用于打断无限循环分支。
|
||||||
|
if (flowCts is null && context.Env.FlowState == RunState.Completion)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// 如果存在全局触发器,且触发器的执行任务已经被取消时,退出执行。
|
||||||
|
if (flowCts != null)
|
||||||
|
{
|
||||||
|
if (flowCts.IsCancellationRequested)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始执行
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task StartFlowAsync(IDynamicContext context)
|
||||||
|
{
|
||||||
|
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
|
||||||
|
stack.Push(this);
|
||||||
|
var flowCts = context.Env.IOC.Get<CancellationTokenSource>(NodeStaticConfig.FlipFlopCtsName);
|
||||||
|
bool hasFlipflow = flowCts != null;
|
||||||
|
while (stack.Count > 0) // 循环中直到栈为空才会退出循环
|
||||||
|
{
|
||||||
|
await Task.Delay(0);
|
||||||
|
// 从栈中弹出一个节点作为当前节点进行处理
|
||||||
|
var currentNode = stack.Pop();
|
||||||
|
|
||||||
|
#region 执行相关
|
||||||
|
|
||||||
|
// 筛选出上游分支
|
||||||
|
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream].ToArray();
|
||||||
|
for (int index = 0; index < upstreamNodes.Length; index++)
|
||||||
|
{
|
||||||
|
NodeModelBase upstreamNode = upstreamNodes[index];
|
||||||
|
if (!(upstreamNode is null) && upstreamNode.DebugSetting.IsEnable)
|
||||||
|
{
|
||||||
|
if (upstreamNode.DebugSetting.InterruptClass != InterruptClass.None) // 执行触发前
|
||||||
|
{
|
||||||
|
var cancelType = await upstreamNode.DebugSetting.GetInterruptTask();
|
||||||
|
await Console.Out.WriteLineAsync($"[{upstreamNode.MethodDetails?.MethodName}]中断已{cancelType},开始执行后继分支");
|
||||||
|
}
|
||||||
|
upstreamNode.PreviousNode = currentNode;
|
||||||
|
await upstreamNode.StartFlowAsync(context); // 执行流程节点的上游分支
|
||||||
|
if (upstreamNode.NextOrientation == ConnectionType.IsError)
|
||||||
|
{
|
||||||
|
// 如果上游分支执行失败,不再继续执行
|
||||||
|
// 使上游节点(仅上游节点本身,不包含上游节点的后继节点)
|
||||||
|
// 具备通过抛出异常中断流程的能力
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (IsBradk(context, flowCts)) break; // 退出执行
|
||||||
|
// 上游分支执行完成,才执行当前节点
|
||||||
|
object newFlowData = await currentNode.ExecutingAsync(context);
|
||||||
|
if (IsBradk(context, flowCts)) break; // 退出执行
|
||||||
|
|
||||||
|
await RefreshFlowDataAndExpInterrupt(context, currentNode, newFlowData); // 执行当前节点后刷新数据
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region 执行完成
|
||||||
|
|
||||||
|
// 选择后继分支
|
||||||
|
var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation];
|
||||||
|
|
||||||
|
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||||
|
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
// 筛选出启用的节点的节点
|
||||||
|
if (nextNodes[i].DebugSetting.IsEnable)
|
||||||
|
{
|
||||||
|
nextNodes[i].PreviousNode = currentNode;
|
||||||
|
stack.Push(nextNodes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行节点对应的方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">流程上下文</param>
|
||||||
|
/// <returns>节点传回数据对象</returns>
|
||||||
|
public virtual async Task<object> ExecutingAsync(IDynamicContext context)
|
||||||
|
{
|
||||||
|
#region 调试中断
|
||||||
|
|
||||||
|
if (DebugSetting.InterruptClass != InterruptClass.None) // 执行触发检查是否需要中断
|
||||||
|
{
|
||||||
|
var cancelType = await this.DebugSetting.GetInterruptTask(); // 等待中断结束
|
||||||
|
await Console.Out.WriteLineAsync($"[{this.MethodDetails?.MethodName}]中断已{cancelType},开始执行后继分支");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
MethodDetails md = MethodDetails;
|
||||||
|
//var del = md.MethodDelegate.Clone();
|
||||||
|
if (md is null)
|
||||||
|
{
|
||||||
|
throw new Exception($"节点{this.Guid}不存在方法信息,请检查是否需要重写节点的ExecutingAsync");
|
||||||
|
}
|
||||||
|
if (!context.Env.TryGetDelegateDetails(md.MethodName, out var dd))
|
||||||
|
{
|
||||||
|
throw new Exception($"节点{this.Guid}不存在对应委托");
|
||||||
|
}
|
||||||
|
if(md.ActingInstance is null)
|
||||||
|
{
|
||||||
|
md.ActingInstance = context.Env.IOC.Get(md.ActingInstanceType);
|
||||||
|
}
|
||||||
|
// md.ActingInstance ??= context.Env.IOC.Get(md.ActingInstanceType);
|
||||||
|
object instance = md.ActingInstance;
|
||||||
|
|
||||||
|
|
||||||
|
object result = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
object[] args = GetParameters(context, this, md);
|
||||||
|
result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||||
|
NextOrientation = ConnectionType.IsSucceed;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await Console.Out.WriteLineAsync($"节点[{this.MethodDetails?.MethodName}]异常:" + ex);
|
||||||
|
NextOrientation = ConnectionType.IsError;
|
||||||
|
RuningException = ex;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取对应的参数数组
|
||||||
|
/// </summary>
|
||||||
|
public static object[] GetParameters(IDynamicContext context, NodeModelBase nodeModel, MethodDetails md)
|
||||||
|
{
|
||||||
|
// 用正确的大小初始化参数数组
|
||||||
|
if (md.ParameterDetailss.Length == 0)
|
||||||
|
{
|
||||||
|
return null;// md.ActingInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
object[] parameters = new object[md.ParameterDetailss.Length];
|
||||||
|
var flowData = nodeModel.PreviousNode?.FlowData; // 当前传递的数据
|
||||||
|
var previousDataType = flowData?.GetType();
|
||||||
|
|
||||||
|
for (int i = 0; i < parameters.Length; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
object inputParameter; // 存放解析的临时参数
|
||||||
|
var ed = md.ParameterDetailss[i]; // 方法入参描述
|
||||||
|
|
||||||
|
|
||||||
|
if (ed.IsExplicitData) // 判断是否使用显示的输入参数
|
||||||
|
{
|
||||||
|
if (ed.DataValue.StartsWith("@get", StringComparison.OrdinalIgnoreCase) && !(flowData is null))
|
||||||
|
{
|
||||||
|
// 执行表达式从上一节点获取对象
|
||||||
|
inputParameter = SerinExpressionEvaluator.Evaluate(ed.DataValue, flowData, out _);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 使用输入的固定值
|
||||||
|
inputParameter = ed.DataValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inputParameter = flowData; // 使用上一节点的对象
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入参存在取值转换器
|
||||||
|
if (ed.ExplicitType.IsEnum && !(ed.Convertor is null))
|
||||||
|
{
|
||||||
|
//var resultEnum = Enum.ToObject(ed.ExplicitType, ed.DataValue);
|
||||||
|
var resultEnum = Enum.Parse(ed.ExplicitType, ed.DataValue);
|
||||||
|
var value = ed.Convertor(resultEnum);
|
||||||
|
if (value is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("转换器调用失败");
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parameters[i] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//if (Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum))
|
||||||
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入参存在类型转换器,获取枚举转换器中记录的枚举
|
||||||
|
if (ed.ExplicitType.IsEnum && ed.DataType != ed.ExplicitType)
|
||||||
|
{
|
||||||
|
var resultEnum = Enum.Parse(ed.ExplicitType, ed.DataValue);
|
||||||
|
// 获取绑定的类型
|
||||||
|
var type = EnumHelper.GetBoundValue(ed.ExplicitType, resultEnum, attr => attr.Value);
|
||||||
|
if (type is Type enumBindType && !(enumBindType is null))
|
||||||
|
{
|
||||||
|
var value = context.Env.IOC.Instantiate(enumBindType);
|
||||||
|
if (value is null)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parameters[i] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (ed.DataType.IsValueType)
|
||||||
|
{
|
||||||
|
var valueStr = inputParameter?.ToString();
|
||||||
|
parameters[i] = valueStr.ToValueData(ed.DataType);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var valueStr = inputParameter?.ToString();
|
||||||
|
if(ed.DataType == typeof(string))
|
||||||
|
{
|
||||||
|
parameters[i] = valueStr;
|
||||||
|
}
|
||||||
|
else if(ed.DataType == typeof(IDynamicContext))
|
||||||
|
{
|
||||||
|
parameters[i] = context;
|
||||||
|
}
|
||||||
|
else if(ed.DataType == typeof(MethodDetails))
|
||||||
|
{
|
||||||
|
parameters[i] = md;
|
||||||
|
}
|
||||||
|
else if(ed.DataType == typeof(NodeModelBase))
|
||||||
|
{
|
||||||
|
parameters[i] = nodeModel;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parameters[i] = inputParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
//parameters[i] = ed.DataType switch
|
||||||
|
//{
|
||||||
|
// Type t when t == typeof(string) => valueStr,
|
||||||
|
// Type t when t == typeof(IDynamicContext) => context, // 上下文
|
||||||
|
// Type t when t == typeof(DateTime) => string.IsNullOrEmpty(valueStr) ? null : DateTime.Parse(valueStr),
|
||||||
|
|
||||||
|
// Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||||
|
// Type t when t == typeof(NodeModelBase) => nodeModel, // 节点实体类
|
||||||
|
|
||||||
|
// Type t when t.IsArray => (inputParameter as Array)?.Cast<object>().ToList(),
|
||||||
|
// Type t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => inputParameter,
|
||||||
|
// _ => inputParameter,
|
||||||
|
//};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新节点数据,并检查监视表达式是否生效
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">上下文</param>
|
||||||
|
/// <param name="nodeModel">节点Moel</param>
|
||||||
|
/// <param name="newData">新的数据</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task RefreshFlowDataAndExpInterrupt(IDynamicContext context, NodeModelBase nodeModel, object newData = null)
|
||||||
|
{
|
||||||
|
string guid = nodeModel.Guid;
|
||||||
|
if (newData is null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await MonitorObjExpInterrupt(context, nodeModel, newData, 0); // 首先监视对象
|
||||||
|
await MonitorObjExpInterrupt(context, nodeModel, newData, 1); // 然后监视节点
|
||||||
|
nodeModel.FlowData = newData; // 替换数据
|
||||||
|
context.AddOrUpdate(guid, nodeModel); // 上下文中更新数据
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task MonitorObjExpInterrupt(IDynamicContext context, NodeModelBase nodeModel, object data, int monitorType)
|
||||||
|
{
|
||||||
|
MonitorObjectEventArgs.ObjSourceType sourceType;
|
||||||
|
string key;
|
||||||
|
if (monitorType == 0)
|
||||||
|
{
|
||||||
|
key = data?.GetType()?.FullName;
|
||||||
|
sourceType = MonitorObjectEventArgs.ObjSourceType.IOCObj;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
key = nodeModel.Guid;
|
||||||
|
sourceType = MonitorObjectEventArgs.ObjSourceType.IOCObj;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.Env.CheckObjMonitorState(key, out List<string> exps)) // 如果新的数据处于查看状态,通知UI进行更新?交给运行环境判断?
|
||||||
|
{
|
||||||
|
context.Env.MonitorObjectNotification(nodeModel.Guid, data, sourceType); // 对象处于监视状态,通知UI更新数据显示
|
||||||
|
if (exps.Count > 0)
|
||||||
|
{
|
||||||
|
// 表达式环境下判断是否需要执行中断
|
||||||
|
bool isExpInterrupt = false;
|
||||||
|
string exp = "";
|
||||||
|
// 判断执行监视表达式,直到为 true 时退出
|
||||||
|
for (int i = 0; i < exps.Count && !isExpInterrupt; i++)
|
||||||
|
{
|
||||||
|
exp = exps[i];
|
||||||
|
if (string.IsNullOrEmpty(exp)) continue;
|
||||||
|
// isExpInterrupt = SereinConditionParser.To(data, exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isExpInterrupt) // 触发中断
|
||||||
|
{
|
||||||
|
InterruptClass interruptClass = InterruptClass.Branch; // 分支中断
|
||||||
|
if (context.Env.SetNodeInterrupt(nodeModel.Guid, interruptClass))
|
||||||
|
{
|
||||||
|
context.Env.TriggerInterrupt(nodeModel.Guid, exp, InterruptTriggerEventArgs.InterruptTriggerType.Exp);
|
||||||
|
var cancelType = await nodeModel.DebugSetting.GetInterruptTask();
|
||||||
|
await Console.Out.WriteLineAsync($"[{data}]中断已{cancelType},开始执行后继分支");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 释放对象
|
||||||
|
/// </summary>
|
||||||
|
public void ReleaseFlowData()
|
||||||
|
{
|
||||||
|
if (typeof(IDisposable).IsAssignableFrom(FlowData?.GetType()) && FlowData is IDisposable disposable)
|
||||||
|
{
|
||||||
|
disposable?.Dispose();
|
||||||
|
}
|
||||||
|
this.FlowData = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取节点数据
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public object GetFlowData()
|
||||||
|
{
|
||||||
|
return this.FlowData;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,14 +15,17 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||||
{
|
{
|
||||||
public class WebSocketHandleConfig
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public class JsonMsgHandleConfig
|
||||||
{
|
{
|
||||||
private readonly Delegate EmitDelegate;
|
private readonly Delegate EmitDelegate;
|
||||||
private readonly EmitHelper.EmitMethodType EmitMethodType;
|
private readonly EmitHelper.EmitMethodType EmitMethodType;
|
||||||
|
|
||||||
private Action<Exception, Action<object>> OnExceptionTracking;
|
private Action<Exception, Action<object>> OnExceptionTracking;
|
||||||
|
|
||||||
internal WebSocketHandleConfig(SocketHandleModel model,ISocketHandleModule instance, MethodInfo methodInfo, Action<Exception, Action<object>> onExceptionTracking)
|
internal JsonMsgHandleConfig(SocketHandleModel model,ISocketHandleModule instance, MethodInfo methodInfo, Action<Exception, Action<object>> onExceptionTracking)
|
||||||
{
|
{
|
||||||
EmitMethodType = EmitHelper.CreateDynamicMethod(methodInfo,out EmitDelegate);
|
EmitMethodType = EmitHelper.CreateDynamicMethod(methodInfo,out EmitDelegate);
|
||||||
this.Model = model;
|
this.Model = model;
|
||||||
@@ -8,30 +8,51 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Json消息处理模块
|
||||||
|
/// </summary>
|
||||||
public class WebSocketHandleModule
|
public class WebSocketHandleModule
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Json消息处理模块
|
||||||
|
/// </summary>
|
||||||
public WebSocketHandleModule(string ThemeJsonKey, string DataJsonKey)
|
public WebSocketHandleModule(string ThemeJsonKey, string DataJsonKey)
|
||||||
{
|
{
|
||||||
this.ThemeJsonKey = ThemeJsonKey;
|
this.ThemeJsonKey = ThemeJsonKey;
|
||||||
this.DataJsonKey = DataJsonKey;
|
this.DataJsonKey = DataJsonKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 指示处理模块该使用json中的哪个key作为业务区别字段
|
||||||
|
/// </summary>
|
||||||
public string ThemeJsonKey { get; }
|
public string ThemeJsonKey { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 指示处理模块该使用json中的哪个key作为业务数据字段
|
||||||
|
/// </summary>
|
||||||
public string DataJsonKey { get; }
|
public string DataJsonKey { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 存储处理数据的配置
|
||||||
|
/// </summary>
|
||||||
|
public ConcurrentDictionary<string, JsonMsgHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, JsonMsgHandleConfig>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public ConcurrentDictionary<string, WebSocketHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, WebSocketHandleConfig>();
|
|
||||||
internal void AddHandleConfigs(SocketHandleModel model, ISocketHandleModule instance, MethodInfo methodInfo
|
internal void AddHandleConfigs(SocketHandleModel model, ISocketHandleModule instance, MethodInfo methodInfo
|
||||||
, Action<Exception, Action<object>> onExceptionTracking)
|
, Action<Exception, Action<object>> onExceptionTracking)
|
||||||
{
|
{
|
||||||
if (!MyHandleConfigs.ContainsKey(model.ThemeValue))
|
if (!MyHandleConfigs.ContainsKey(model.ThemeValue))
|
||||||
{
|
{
|
||||||
var myHandleConfig = new WebSocketHandleConfig(model,instance, methodInfo, onExceptionTracking);
|
var jsonMsgHandleConfig = new JsonMsgHandleConfig(model,instance, methodInfo, onExceptionTracking);
|
||||||
MyHandleConfigs[model.ThemeValue] = myHandleConfig;
|
MyHandleConfigs[model.ThemeValue] = jsonMsgHandleConfig;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public bool ResetConfig(ISocketHandleModule socketControlBase)
|
|
||||||
|
/// <summary>
|
||||||
|
/// 移除某个处理模块
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="socketControlBase"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool RemoveConfig(ISocketHandleModule socketControlBase)
|
||||||
{
|
{
|
||||||
foreach (var kv in MyHandleConfigs.ToArray())
|
foreach (var kv in MyHandleConfigs.ToArray())
|
||||||
{
|
{
|
||||||
@@ -44,7 +65,10 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
|||||||
return MyHandleConfigs.Count == 0;
|
return MyHandleConfigs.Count == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetConfig()
|
/// <summary>
|
||||||
|
/// 卸载当前模块的所有配置
|
||||||
|
/// </summary>
|
||||||
|
public void UnloadConfig()
|
||||||
{
|
{
|
||||||
var temp = MyHandleConfigs.Values;
|
var temp = MyHandleConfigs.Values;
|
||||||
MyHandleConfigs.Clear();
|
MyHandleConfigs.Clear();
|
||||||
@@ -54,7 +78,11 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理JSON数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="RecoverAsync"></param>
|
||||||
|
/// <param name="jsonObject"></param>
|
||||||
public void HandleSocketMsg(Func<string, Task> RecoverAsync, JObject jsonObject)
|
public void HandleSocketMsg(Func<string, Task> RecoverAsync, JObject jsonObject)
|
||||||
{
|
{
|
||||||
// 获取到消息
|
// 获取到消息
|
||||||
|
|||||||
@@ -19,7 +19,9 @@ using System.Security.Cryptography;
|
|||||||
|
|
||||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 适用于Json数据格式的WebSocket消息处理类
|
||||||
|
/// </summary>
|
||||||
public class WebSocketMsgHandleHelper
|
public class WebSocketMsgHandleHelper
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -68,7 +70,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
|||||||
var key = (themeKeyName, dataKeyName);
|
var key = (themeKeyName, dataKeyName);
|
||||||
if (MyHandleModuleDict.TryGetValue(key, out var myHandleModules))
|
if (MyHandleModuleDict.TryGetValue(key, out var myHandleModules))
|
||||||
{
|
{
|
||||||
var isRemote = myHandleModules.ResetConfig(socketControlBase);
|
var isRemote = myHandleModules.RemoveConfig(socketControlBase);
|
||||||
if (isRemote) MyHandleModuleDict.TryGetValue(key, out _);
|
if (isRemote) MyHandleModuleDict.TryGetValue(key, out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,6 +134,12 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步处理消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="RecoverAsync"></param>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public async Task HandleMsgAsync(Func<string, Task> RecoverAsync, string message)
|
public async Task HandleMsgAsync(Func<string, Task> RecoverAsync, string message)
|
||||||
{
|
{
|
||||||
JObject json = JObject.Parse(message);
|
JObject json = JObject.Parse(message);
|
||||||
|
|||||||
@@ -14,12 +14,25 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Serein.Library.Network.WebSocketCommunication
|
namespace Serein.Library.Network.WebSocketCommunication
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WebSocket服务类
|
||||||
|
/// </summary>
|
||||||
[AutoRegister]
|
[AutoRegister]
|
||||||
public class WebSocketServer
|
public class WebSocketServer
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 消息处理
|
||||||
|
/// </summary>
|
||||||
public WebSocketMsgHandleHelper MsgHandleHelper { get; } = new WebSocketMsgHandleHelper();
|
public WebSocketMsgHandleHelper MsgHandleHelper { get; } = new WebSocketMsgHandleHelper();
|
||||||
|
|
||||||
private HttpListener listener;
|
private HttpListener listener;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 进行监听服务
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public async Task StartAsync(string url)
|
public async Task StartAsync(string url)
|
||||||
{
|
{
|
||||||
listener = new HttpListener();
|
listener = new HttpListener();
|
||||||
@@ -58,6 +71,9 @@ namespace Serein.Library.Network.WebSocketCommunication
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 停止监听服务
|
||||||
|
/// </summary>
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
listener?.Stop();
|
listener?.Stop();
|
||||||
@@ -96,6 +112,12 @@ namespace Serein.Library.Network.WebSocketCommunication
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="webSocket"></param>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public static async Task SendAsync(WebSocket webSocket, string message)
|
public static async Task SendAsync(WebSocket webSocket, string message)
|
||||||
{
|
{
|
||||||
var buffer = Encoding.UTF8.GetBytes(message);
|
var buffer = Encoding.UTF8.GetBytes(message);
|
||||||
|
|||||||
41
Library/NodeStaticConfig.cs
Normal file
41
Library/NodeStaticConfig.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using Serein.Library.Api;
|
||||||
|
using Serein.Library.Enums;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow
|
||||||
|
{
|
||||||
|
public static class NodeStaticConfig
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 全局触发器CTS
|
||||||
|
/// </summary>
|
||||||
|
public const string FlipFlopCtsName = "<>.FlowFlipFlopCts";
|
||||||
|
/// <summary>
|
||||||
|
/// 流程运行CTS
|
||||||
|
/// </summary>
|
||||||
|
public const string FlowRungCtsName = "<>.FlowRungCtsName";
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点的命名空间
|
||||||
|
/// </summary>
|
||||||
|
//public const string NodeSpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
|
||||||
|
public const string NodeSpaceName = "Serein.NodeFlow.Model";
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点连接关系种类
|
||||||
|
/// </summary>
|
||||||
|
public static readonly ConnectionType[] ConnectionTypes = new ConnectionType[]
|
||||||
|
{
|
||||||
|
ConnectionType.Upstream,
|
||||||
|
ConnectionType.IsSucceed,
|
||||||
|
ConnectionType.IsFail,
|
||||||
|
ConnectionType.IsError,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,10 +14,13 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Remove="Entity\Base\**" />
|
||||||
<Compile Remove="Http\**" />
|
<Compile Remove="Http\**" />
|
||||||
<Compile Remove="Utils\SerinExpression\**" />
|
<Compile Remove="Utils\SerinExpression\**" />
|
||||||
|
<EmbeddedResource Remove="Entity\Base\**" />
|
||||||
<EmbeddedResource Remove="Http\**" />
|
<EmbeddedResource Remove="Http\**" />
|
||||||
<EmbeddedResource Remove="Utils\SerinExpression\**" />
|
<EmbeddedResource Remove="Utils\SerinExpression\**" />
|
||||||
|
<None Remove="Entity\Base\**" />
|
||||||
<None Remove="Http\**" />
|
<None Remove="Http\**" />
|
||||||
<None Remove="Utils\SerinExpression\**" />
|
<None Remove="Utils\SerinExpression\**" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ namespace Serein.Library.Utils
|
|||||||
result = default;
|
result = default;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从枚举值的 BindValueAttribute 特性中 获取绑定的参数(用于绑定了某些内容的枚举值)
|
/// 从枚举值的 BindValueAttribute 特性中 获取绑定的参数(用于绑定了某些内容的枚举值)
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||||
|
{
|
||||||
|
public class BoolConditionResolver : SereinConditionResolver
|
||||||
|
{
|
||||||
|
public enum Operator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是
|
||||||
|
/// </summary>
|
||||||
|
Is
|
||||||
|
}
|
||||||
|
|
||||||
|
public Operator Op { get; set; }
|
||||||
|
public bool Value { get; set; }
|
||||||
|
|
||||||
|
public override bool Evaluate(object obj)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (obj is bool boolObj)
|
||||||
|
{
|
||||||
|
return boolObj == Value;
|
||||||
|
/*switch (Op)
|
||||||
|
{
|
||||||
|
case Operator.Is:
|
||||||
|
return boolObj == Value;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||||
|
{
|
||||||
|
public class MemberConditionResolver<T> : SereinConditionResolver where T : struct, IComparable<T>
|
||||||
|
{
|
||||||
|
//public string MemberPath { get; set; }
|
||||||
|
public ValueTypeConditionResolver<T>.Operator Op { get; set; }
|
||||||
|
public object TargetObj { get; set; }
|
||||||
|
public T Value { get; set; }
|
||||||
|
|
||||||
|
public string ArithmeticExpression { get; set; }
|
||||||
|
public T RangeEnd { get; internal set; }
|
||||||
|
public T RangeStart { get; internal set; }
|
||||||
|
|
||||||
|
public override bool Evaluate(object obj)
|
||||||
|
{
|
||||||
|
//object? memberValue = GetMemberValue(obj, MemberPath);
|
||||||
|
|
||||||
|
|
||||||
|
if (TargetObj is T typedObj)
|
||||||
|
{
|
||||||
|
return new ValueTypeConditionResolver<T>
|
||||||
|
{
|
||||||
|
RangeStart = RangeStart,
|
||||||
|
RangeEnd = RangeEnd,
|
||||||
|
Op = Op,
|
||||||
|
Value = Value,
|
||||||
|
ArithmeticExpression = ArithmeticExpression,
|
||||||
|
}.Evaluate(typedObj);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//private object? GetMemberValue(object? obj, string memberPath)
|
||||||
|
//{
|
||||||
|
// string[] members = memberPath[1..].Split('.');
|
||||||
|
// foreach (var member in members)
|
||||||
|
// {
|
||||||
|
// if (obj == null) return null;
|
||||||
|
// Type type = obj.GetType();
|
||||||
|
// PropertyInfo? propertyInfo = type.GetProperty(member);
|
||||||
|
// FieldInfo? fieldInfo = type.GetField(member);
|
||||||
|
// if (propertyInfo != null)
|
||||||
|
// obj = propertyInfo.GetValue(obj);
|
||||||
|
// else if (fieldInfo != null)
|
||||||
|
// obj = fieldInfo.GetValue(obj);
|
||||||
|
// else
|
||||||
|
// throw new ArgumentException($"Member {member} not found in type {type.FullName}");
|
||||||
|
// }
|
||||||
|
// return obj;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||||
|
{
|
||||||
|
public class MemberStringConditionResolver : SereinConditionResolver
|
||||||
|
{
|
||||||
|
public string MemberPath { get; set; }
|
||||||
|
|
||||||
|
public StringConditionResolver.Operator Op { get; set; }
|
||||||
|
|
||||||
|
public string Value { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public override bool Evaluate(object obj)
|
||||||
|
{
|
||||||
|
object memberValue;
|
||||||
|
if (!string.IsNullOrWhiteSpace(MemberPath))
|
||||||
|
{
|
||||||
|
memberValue = GetMemberValue(obj, MemberPath);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memberValue = obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (memberValue is string strObj)
|
||||||
|
{
|
||||||
|
return new StringConditionResolver
|
||||||
|
{
|
||||||
|
Op = Op,
|
||||||
|
Value = Value
|
||||||
|
}.Evaluate(strObj);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private object GetMemberValue(object obj, string memberPath)
|
||||||
|
{
|
||||||
|
//string[] members = memberPath[1..].Split('.');
|
||||||
|
string[] members = memberPath.Substring(1).Split('.');
|
||||||
|
foreach (var member in members)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (obj is null) return null;
|
||||||
|
|
||||||
|
Type type = obj.GetType();
|
||||||
|
PropertyInfo propertyInfo = type.GetProperty(member);
|
||||||
|
FieldInfo fieldInfo = type.GetField(member);
|
||||||
|
if (propertyInfo != null)
|
||||||
|
obj = propertyInfo.GetValue(obj);
|
||||||
|
else if (fieldInfo != null)
|
||||||
|
obj = fieldInfo.GetValue(obj);
|
||||||
|
else
|
||||||
|
throw new ArgumentException($"Member {member} not found in type {type.FullName}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static string GetArithmeticExpression(string part)
|
||||||
|
{
|
||||||
|
int startIndex = part.IndexOf('[');
|
||||||
|
int endIndex = part.IndexOf(']');
|
||||||
|
if (startIndex >= 0 && endIndex > startIndex)
|
||||||
|
{
|
||||||
|
return part.Substring(startIndex + 1, endIndex - startIndex - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||||
|
{
|
||||||
|
public class PassConditionResolver : SereinConditionResolver
|
||||||
|
{
|
||||||
|
public Operator Op { get; set; }
|
||||||
|
public override bool Evaluate(object obj)
|
||||||
|
{
|
||||||
|
/*return Op switch
|
||||||
|
{
|
||||||
|
Operator.Pass => true,
|
||||||
|
Operator.NotPass => false,
|
||||||
|
_ => throw new NotSupportedException("不支持的条件类型")
|
||||||
|
};*/
|
||||||
|
switch (Op)
|
||||||
|
{
|
||||||
|
case Operator.Pass:
|
||||||
|
return true;
|
||||||
|
case Operator.NotPass:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException("不支持的条件类型");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Operator
|
||||||
|
{
|
||||||
|
Pass,
|
||||||
|
NotPass,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||||
|
{
|
||||||
|
public class StringConditionResolver : SereinConditionResolver
|
||||||
|
{
|
||||||
|
public enum Operator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 出现过
|
||||||
|
/// </summary>
|
||||||
|
Contains,
|
||||||
|
/// <summary>
|
||||||
|
/// 没有出现过
|
||||||
|
/// </summary>
|
||||||
|
DoesNotContain,
|
||||||
|
/// <summary>
|
||||||
|
/// 相等
|
||||||
|
/// </summary>
|
||||||
|
Equal,
|
||||||
|
/// <summary>
|
||||||
|
/// 不相等
|
||||||
|
/// </summary>
|
||||||
|
NotEqual,
|
||||||
|
/// <summary>
|
||||||
|
/// 起始字符串等于
|
||||||
|
/// </summary>
|
||||||
|
StartsWith,
|
||||||
|
/// <summary>
|
||||||
|
/// 结束字符串等于
|
||||||
|
/// </summary>
|
||||||
|
EndsWith
|
||||||
|
}
|
||||||
|
|
||||||
|
public Operator Op { get; set; }
|
||||||
|
|
||||||
|
public string Value { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public override bool Evaluate(object obj)
|
||||||
|
{
|
||||||
|
if (obj is string strObj)
|
||||||
|
{
|
||||||
|
/*return Op switch
|
||||||
|
{
|
||||||
|
Operator.Contains => strObj.Contains(Value),
|
||||||
|
Operator.DoesNotContain => !strObj.Contains(Value),
|
||||||
|
Operator.Equal => strObj == Value,
|
||||||
|
Operator.NotEqual => strObj != Value,
|
||||||
|
Operator.StartsWith => strObj.StartsWith(Value),
|
||||||
|
Operator.EndsWith => strObj.EndsWith(Value),
|
||||||
|
_ => throw new NotSupportedException("不支持的条件类型"),
|
||||||
|
};*/
|
||||||
|
|
||||||
|
switch (Op)
|
||||||
|
{
|
||||||
|
case Operator.Contains:
|
||||||
|
return strObj.Contains(Value);
|
||||||
|
case Operator.DoesNotContain:
|
||||||
|
return !strObj.Contains(Value);
|
||||||
|
case Operator.Equal:
|
||||||
|
return strObj == Value;
|
||||||
|
case Operator.NotEqual:
|
||||||
|
return strObj != Value;
|
||||||
|
case Operator.StartsWith:
|
||||||
|
return strObj.StartsWith(Value);
|
||||||
|
case Operator.EndsWith:
|
||||||
|
return strObj.EndsWith(Value);
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException("不支持的条件类型");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
using Serein.Library.Utils;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||||
|
{
|
||||||
|
public class ValueTypeConditionResolver<T> : SereinConditionResolver where T : struct, IComparable<T>
|
||||||
|
{
|
||||||
|
public enum Operator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 不进行任何操作
|
||||||
|
/// </summary>
|
||||||
|
Node,
|
||||||
|
/// <summary>
|
||||||
|
/// 大于
|
||||||
|
/// </summary>
|
||||||
|
GreaterThan,
|
||||||
|
/// <summary>
|
||||||
|
/// 小于
|
||||||
|
/// </summary>
|
||||||
|
LessThan,
|
||||||
|
/// <summary>
|
||||||
|
/// 等于
|
||||||
|
/// </summary>
|
||||||
|
Equal,
|
||||||
|
/// <summary>
|
||||||
|
/// 大于或等于
|
||||||
|
/// </summary>
|
||||||
|
GreaterThanOrEqual,
|
||||||
|
/// <summary>
|
||||||
|
/// 小于或等于
|
||||||
|
/// </summary>
|
||||||
|
LessThanOrEqual,
|
||||||
|
/// <summary>
|
||||||
|
/// 在两者之间
|
||||||
|
/// </summary>
|
||||||
|
InRange,
|
||||||
|
/// <summary>
|
||||||
|
/// 不在两者之间
|
||||||
|
/// </summary>
|
||||||
|
OutOfRange
|
||||||
|
}
|
||||||
|
|
||||||
|
public Operator Op { get; set; }
|
||||||
|
public T Value { get; set; }
|
||||||
|
public T RangeStart { get; set; }
|
||||||
|
public T RangeEnd { get; set; }
|
||||||
|
|
||||||
|
public string ArithmeticExpression { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public override bool Evaluate(object obj)
|
||||||
|
{
|
||||||
|
|
||||||
|
var evaluatedValue = obj.ToConvert<T>();
|
||||||
|
if (!string.IsNullOrEmpty(ArithmeticExpression))
|
||||||
|
{
|
||||||
|
evaluatedValue = SerinArithmeticExpressionEvaluator<T>.Evaluate(ArithmeticExpression, evaluatedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (Op)
|
||||||
|
{
|
||||||
|
case Operator.GreaterThan:
|
||||||
|
return evaluatedValue.CompareTo(Value) > 0;
|
||||||
|
case Operator.LessThan:
|
||||||
|
return evaluatedValue.CompareTo(Value) < 0;
|
||||||
|
case Operator.Equal:
|
||||||
|
return evaluatedValue.CompareTo(Value) == 0;
|
||||||
|
case Operator.GreaterThanOrEqual:
|
||||||
|
return evaluatedValue.CompareTo(Value) >= 0;
|
||||||
|
case Operator.LessThanOrEqual:
|
||||||
|
return evaluatedValue.CompareTo(Value) <= 0;
|
||||||
|
case Operator.InRange:
|
||||||
|
return evaluatedValue.CompareTo(RangeStart) >= 0 && evaluatedValue.CompareTo(RangeEnd) <= 0;
|
||||||
|
case Operator.OutOfRange:
|
||||||
|
return evaluatedValue.CompareTo(RangeStart) < 0 || evaluatedValue.CompareTo(RangeEnd) > 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
//if (obj is T typedObj)
|
||||||
|
//{
|
||||||
|
// numericValue = Convert.ToDouble(typedObj);
|
||||||
|
// numericValue = Convert.ToDouble(obj);
|
||||||
|
// if (!string.IsNullOrEmpty(ArithmeticExpression))
|
||||||
|
// {
|
||||||
|
// numericValue = SerinArithmeticExpressionEvaluator.Evaluate(ArithmeticExpression, numericValue);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// T evaluatedValue = (T)Convert.ChangeType(numericValue, typeof(T));
|
||||||
|
|
||||||
|
// /*return Op switch
|
||||||
|
// {
|
||||||
|
// Operator.GreaterThan => evaluatedValue.CompareTo(Value) > 0,
|
||||||
|
// Operator.LessThan => evaluatedValue.CompareTo(Value) < 0,
|
||||||
|
// Operator.Equal => evaluatedValue.CompareTo(Value) == 0,
|
||||||
|
// Operator.GreaterThanOrEqual => evaluatedValue.CompareTo(Value) >= 0,
|
||||||
|
// Operator.LessThanOrEqual => evaluatedValue.CompareTo(Value) <= 0,
|
||||||
|
// Operator.InRange => evaluatedValue.CompareTo(RangeStart) >= 0 && evaluatedValue.CompareTo(RangeEnd) <= 0,
|
||||||
|
// Operator.OutOfRange => evaluatedValue.CompareTo(RangeStart) < 0 || evaluatedValue.CompareTo(RangeEnd) > 0,
|
||||||
|
// _ => throw new NotSupportedException("不支持的条件类型")
|
||||||
|
// };*/
|
||||||
|
// switch (Op)
|
||||||
|
// {
|
||||||
|
// case Operator.GreaterThan:
|
||||||
|
// return evaluatedValue.CompareTo(Value) > 0;
|
||||||
|
// case Operator.LessThan:
|
||||||
|
// return evaluatedValue.CompareTo(Value) < 0;
|
||||||
|
// case Operator.Equal:
|
||||||
|
// return evaluatedValue.CompareTo(Value) == 0;
|
||||||
|
// case Operator.GreaterThanOrEqual:
|
||||||
|
// return evaluatedValue.CompareTo(Value) >= 0;
|
||||||
|
// case Operator.LessThanOrEqual:
|
||||||
|
// return evaluatedValue.CompareTo(Value) <= 0;
|
||||||
|
// case Operator.InRange:
|
||||||
|
// return evaluatedValue.CompareTo(RangeStart) >= 0 && evaluatedValue.CompareTo(RangeEnd) <= 0;
|
||||||
|
// case Operator.OutOfRange:
|
||||||
|
// return evaluatedValue.CompareTo(RangeStart) < 0 || evaluatedValue.CompareTo(RangeEnd) > 0;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
775
Library/Utils/SereinExpression/SereinConditionParser.cs
Normal file
775
Library/Utils/SereinExpression/SereinConditionParser.cs
Normal file
@@ -0,0 +1,775 @@
|
|||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Serein.Library.Utils;
|
||||||
|
using Serein.NodeFlow.Tool.SereinExpression.Resolver;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.Design;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 字符串工具类
|
||||||
|
/// </summary>
|
||||||
|
public static class StringHelper
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Net低版本无法引入Skip函数,所以使用扩展方法代替
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="count"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentNullException"></exception>
|
||||||
|
public static IEnumerable<T> MySkip<T>(this IEnumerable<T> source, int count)
|
||||||
|
{
|
||||||
|
if (source == null)
|
||||||
|
throw new ArgumentNullException(nameof(source));
|
||||||
|
|
||||||
|
int skipped = 0;
|
||||||
|
foreach (var item in source)
|
||||||
|
{
|
||||||
|
if (skipped++ >= count)
|
||||||
|
yield return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string JoinStrings(string[] parts, int startIndex, int count, char separator)
|
||||||
|
{
|
||||||
|
if (parts == null)
|
||||||
|
throw new ArgumentNullException(nameof(parts));
|
||||||
|
|
||||||
|
if (startIndex < 0 || startIndex >= parts.Length || count < 0 || startIndex + count > parts.Length)
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
|
||||||
|
// 复制需要的部分到新的数组
|
||||||
|
string[] subArray = new string[count];
|
||||||
|
Array.Copy(parts, startIndex, subArray, 0, count);
|
||||||
|
|
||||||
|
// 使用 string.Join 连接
|
||||||
|
return string.Join(separator.ToString(), subArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class SereinConditionParser
|
||||||
|
{
|
||||||
|
public static bool To<T>(T data, string expression)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(expression))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var parse = ConditionParse(data, expression);
|
||||||
|
var result = parse.Evaluate(data);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SereinConditionResolver ConditionParse(object data, string expression)
|
||||||
|
{
|
||||||
|
if (expression.StartsWith(".")) // 表达式前缀属于从上一个节点数据对象获取成员值
|
||||||
|
{
|
||||||
|
return ParseObjectExpression(data, expression);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ParseSimpleExpression(data, expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//bool ContainsArithmeticOperators(string expression)
|
||||||
|
//{
|
||||||
|
// return expression.Contains('+') || expression.Contains('-') || expression.Contains('*') || expression.Contains('/');
|
||||||
|
//}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取计算表达式的部分
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="part"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static string GetArithmeticExpression(string part)
|
||||||
|
{
|
||||||
|
int startIndex = part.IndexOf('[');
|
||||||
|
int endIndex = part.IndexOf(']');
|
||||||
|
if (startIndex >= 0 && endIndex > startIndex)
|
||||||
|
{
|
||||||
|
return part.Substring(startIndex + 1, endIndex - startIndex - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取对象指定名称的成员
|
||||||
|
/// </summary>
|
||||||
|
private static object GetMemberValue(object obj, string memberPath)
|
||||||
|
{
|
||||||
|
//string[] members = memberPath[1..].Split('.');
|
||||||
|
string[] members = memberPath.Substring(1).Split('.');
|
||||||
|
|
||||||
|
foreach (var member in members)
|
||||||
|
{
|
||||||
|
if (obj is null) return null;
|
||||||
|
Type type = obj.GetType();
|
||||||
|
PropertyInfo propertyInfo = type.GetProperty(member);
|
||||||
|
FieldInfo fieldInfo = type.GetField(member);
|
||||||
|
if (propertyInfo != null)
|
||||||
|
obj = propertyInfo.GetValue(obj);
|
||||||
|
else if (fieldInfo != null)
|
||||||
|
obj = fieldInfo.GetValue(obj);
|
||||||
|
else
|
||||||
|
throw new ArgumentException($"Member {member} not found in type {type.FullName}");
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析对象表达式
|
||||||
|
/// </summary>
|
||||||
|
private static SereinConditionResolver ParseObjectExpression(object data, string expression)
|
||||||
|
{
|
||||||
|
var parts = expression.Split(' ');
|
||||||
|
string operatorStr = parts[0]; // 获取操作类型
|
||||||
|
string valueStr; //= string.Join(' ', parts, 1, parts.Length - 1);
|
||||||
|
string memberPath;
|
||||||
|
Type type;
|
||||||
|
object targetObj;
|
||||||
|
|
||||||
|
// 尝试获取指定类型
|
||||||
|
int typeStartIndex = expression.IndexOf('<');
|
||||||
|
int typeEndIndex = expression.IndexOf('>');
|
||||||
|
if (typeStartIndex + typeStartIndex == -2)
|
||||||
|
{
|
||||||
|
// 如果不需要转为指定类型
|
||||||
|
memberPath = operatorStr;
|
||||||
|
targetObj = SerinExpressionEvaluator.Evaluate("@get " + operatorStr, data, out _);
|
||||||
|
//targetObj = GetMemberValue(data, operatorStr);
|
||||||
|
type = targetObj.GetType();
|
||||||
|
operatorStr = parts[1].ToLower(); //
|
||||||
|
valueStr = string.Join(" ", parts.Skip(2));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 类型语法不正确
|
||||||
|
if (typeStartIndex >= typeEndIndex)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("无效的表达式格式");
|
||||||
|
}
|
||||||
|
memberPath = expression.Substring(0, typeStartIndex).Trim();
|
||||||
|
string typeStr = expression.Substring(typeStartIndex + 1, typeEndIndex - typeStartIndex - 1)
|
||||||
|
.Trim().ToLower(); // 手动置顶的类型
|
||||||
|
|
||||||
|
// 对象取值表达式
|
||||||
|
parts = expression.Substring(typeEndIndex + 1).Trim().Split(' ');
|
||||||
|
if (parts.Length == 3)
|
||||||
|
{
|
||||||
|
operatorStr = parts[1].ToLower(); // 操作类型
|
||||||
|
valueStr = string.Join(" ", parts.Skip(2)); // 表达式值
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
operatorStr = parts[0].ToLower(); // 操作类型
|
||||||
|
valueStr = string.Join(" ", parts.Skip(1)); // 表达式值
|
||||||
|
}
|
||||||
|
Type tempType;
|
||||||
|
|
||||||
|
switch (typeStr)
|
||||||
|
{
|
||||||
|
case "bool":
|
||||||
|
tempType = typeof(bool);
|
||||||
|
break;
|
||||||
|
case "float":
|
||||||
|
tempType = typeof(float);
|
||||||
|
break;
|
||||||
|
case "decimal":
|
||||||
|
tempType = typeof(decimal);
|
||||||
|
break;
|
||||||
|
case "double":
|
||||||
|
tempType = typeof(double);
|
||||||
|
break;
|
||||||
|
case "sbyte":
|
||||||
|
tempType = typeof(sbyte);
|
||||||
|
break;
|
||||||
|
case "byte":
|
||||||
|
tempType = typeof(byte);
|
||||||
|
break;
|
||||||
|
case "short":
|
||||||
|
tempType = typeof(short);
|
||||||
|
break;
|
||||||
|
case "ushort":
|
||||||
|
tempType = typeof(ushort);
|
||||||
|
break;
|
||||||
|
case "int":
|
||||||
|
tempType = typeof(int);
|
||||||
|
break;
|
||||||
|
case "uint":
|
||||||
|
tempType = typeof(uint);
|
||||||
|
break;
|
||||||
|
case "long":
|
||||||
|
tempType = typeof(long);
|
||||||
|
break;
|
||||||
|
case "ulong":
|
||||||
|
tempType = typeof(ulong);
|
||||||
|
break;
|
||||||
|
// 如果需要支持 nint 和 nuint
|
||||||
|
// case "nint":
|
||||||
|
// tempType = typeof(nint);
|
||||||
|
// break;
|
||||||
|
// case "nuint":
|
||||||
|
// tempType = typeof(nuint);
|
||||||
|
// break;
|
||||||
|
case "string":
|
||||||
|
tempType = typeof(string);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
tempType = Type.GetType(typeStr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
type = tempType ?? throw new ArgumentException("对象表达式无效的类型声明");
|
||||||
|
if (string.IsNullOrWhiteSpace(memberPath))
|
||||||
|
{
|
||||||
|
targetObj = Convert.ChangeType(data, type);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
targetObj = GetMemberValue(data, memberPath);// 获取对象成员,作为表达式的目标对象
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 解析类型 int
|
||||||
|
if (type.IsValueType)
|
||||||
|
{
|
||||||
|
//return GetValueResolver(type, valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
if (type == typeof(int))
|
||||||
|
{
|
||||||
|
var op = ParseValueTypeOperator<int>(operatorStr);
|
||||||
|
if (op == ValueTypeConditionResolver<int>.Operator.InRange || op == ValueTypeConditionResolver<int>.Operator.OutOfRange)
|
||||||
|
{
|
||||||
|
var temp = valueStr.Split('-');
|
||||||
|
if (temp.Length < 2)
|
||||||
|
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||||
|
int rangeStart = int.Parse(temp[0], CultureInfo.InvariantCulture);
|
||||||
|
int rangeEnd = int.Parse(temp[1], CultureInfo.InvariantCulture);
|
||||||
|
return new MemberConditionResolver<int>
|
||||||
|
{
|
||||||
|
Op = op,
|
||||||
|
RangeStart = rangeStart,
|
||||||
|
RangeEnd = rangeEnd,
|
||||||
|
TargetObj = targetObj,
|
||||||
|
ArithmeticExpression = GetArithmeticExpression(parts[0]),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
int value = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
|
||||||
|
return new MemberConditionResolver<int>
|
||||||
|
{
|
||||||
|
TargetObj = targetObj,
|
||||||
|
//MemberPath = memberPath,
|
||||||
|
Op = ParseValueTypeOperator<int>(operatorStr),
|
||||||
|
Value = value,
|
||||||
|
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#region 解析类型 double
|
||||||
|
else if (type == typeof(double))
|
||||||
|
{
|
||||||
|
double value = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||||
|
return new MemberConditionResolver<double>
|
||||||
|
{
|
||||||
|
//MemberPath = memberPath,
|
||||||
|
TargetObj = targetObj,
|
||||||
|
Op = ParseValueTypeOperator<double>(operatorStr),
|
||||||
|
Value = value,
|
||||||
|
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#region 解析类型 bool
|
||||||
|
else if (type == typeof(bool))
|
||||||
|
{
|
||||||
|
return new MemberConditionResolver<bool>
|
||||||
|
{
|
||||||
|
//MemberPath = memberPath,
|
||||||
|
TargetObj = targetObj,
|
||||||
|
Op = (ValueTypeConditionResolver<bool>.Operator)ParseBoolOperator(operatorStr)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#region 解析类型 string
|
||||||
|
else if (type == typeof(string))
|
||||||
|
{
|
||||||
|
return new MemberStringConditionResolver
|
||||||
|
{
|
||||||
|
MemberPath = memberPath,
|
||||||
|
Op = ParseStringOperator(operatorStr),
|
||||||
|
Value = valueStr
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
throw new NotSupportedException($"Type {type} is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 条件表达式解析
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <param name="expression"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
/// <exception cref="NotSupportedException"></exception>
|
||||||
|
private static SereinConditionResolver ParseSimpleExpression(object data, string expression)
|
||||||
|
{
|
||||||
|
if ("pass".Equals(expression.ToLower()))
|
||||||
|
{
|
||||||
|
return new PassConditionResolver
|
||||||
|
{
|
||||||
|
Op = PassConditionResolver.Operator.Pass,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ("not pass".Equals(expression.ToLower()))
|
||||||
|
{
|
||||||
|
return new PassConditionResolver
|
||||||
|
{
|
||||||
|
Op = PassConditionResolver.Operator.NotPass,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if ("!pass".Equals(expression.ToLower()))
|
||||||
|
{
|
||||||
|
return new PassConditionResolver
|
||||||
|
{
|
||||||
|
Op = PassConditionResolver.Operator.NotPass,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var parts = expression.Split(' ');
|
||||||
|
|
||||||
|
if (parts.Length < 2)
|
||||||
|
throw new ArgumentException("无效的表达式格式。");
|
||||||
|
|
||||||
|
string operatorStr;
|
||||||
|
string valueStr;
|
||||||
|
Type type = null;
|
||||||
|
// 尝试获取指定类型
|
||||||
|
int typeStartIndex = expression.IndexOf('<');
|
||||||
|
int typeEndIndex = expression.IndexOf('>');
|
||||||
|
if (typeStartIndex + typeStartIndex == -2)
|
||||||
|
{
|
||||||
|
// 如果不需要转为指定类型
|
||||||
|
operatorStr = parts[0];
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
valueStr = string.Join(' ', parts, 1, parts.Length - 1);
|
||||||
|
|
||||||
|
#elif NET462_OR_GREATER
|
||||||
|
|
||||||
|
valueStr = StringHelper.JoinStrings(parts, 1, parts.Length - 1, ' ');
|
||||||
|
#endif
|
||||||
|
|
||||||
|
type = data.GetType();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{//string typeStr = parts[0];
|
||||||
|
string typeStr = expression.Substring(typeStartIndex + 1, typeEndIndex - typeStartIndex - 1)
|
||||||
|
.Trim().ToLower(); // 手动置顶的类型
|
||||||
|
parts = expression.Substring(typeEndIndex + 1).Trim().Split(' ');
|
||||||
|
operatorStr = parts[0].ToLower(); // 操作类型
|
||||||
|
valueStr = string.Join(" ", parts.Skip(1)); // 表达式值
|
||||||
|
|
||||||
|
|
||||||
|
Type tempType;
|
||||||
|
|
||||||
|
switch (typeStr)
|
||||||
|
{
|
||||||
|
case "bool":
|
||||||
|
tempType = typeof(bool);
|
||||||
|
break;
|
||||||
|
case "float":
|
||||||
|
tempType = typeof(float);
|
||||||
|
break;
|
||||||
|
case "decimal":
|
||||||
|
tempType = typeof(decimal);
|
||||||
|
break;
|
||||||
|
case "double":
|
||||||
|
tempType = typeof(double);
|
||||||
|
break;
|
||||||
|
case "sbyte":
|
||||||
|
tempType = typeof(sbyte);
|
||||||
|
break;
|
||||||
|
case "byte":
|
||||||
|
tempType = typeof(byte);
|
||||||
|
break;
|
||||||
|
case "short":
|
||||||
|
tempType = typeof(short);
|
||||||
|
break;
|
||||||
|
case "ushort":
|
||||||
|
tempType = typeof(ushort);
|
||||||
|
break;
|
||||||
|
case "int":
|
||||||
|
tempType = typeof(int);
|
||||||
|
break;
|
||||||
|
case "uint":
|
||||||
|
tempType = typeof(uint);
|
||||||
|
break;
|
||||||
|
case "long":
|
||||||
|
tempType = typeof(long);
|
||||||
|
break;
|
||||||
|
case "ulong":
|
||||||
|
tempType = typeof(ulong);
|
||||||
|
break;
|
||||||
|
// 如果需要支持 nint 和 nuint
|
||||||
|
// case "nint":
|
||||||
|
// tempType = typeof(nint);
|
||||||
|
// break;
|
||||||
|
// case "nuint":
|
||||||
|
// tempType = typeof(nuint);
|
||||||
|
// break;
|
||||||
|
case "string":
|
||||||
|
tempType = typeof(string);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
tempType = Type.GetType(typeStr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (type == typeof(bool))
|
||||||
|
{
|
||||||
|
bool value = bool.Parse(valueStr);
|
||||||
|
return new BoolConditionResolver
|
||||||
|
{
|
||||||
|
Op = ParseBoolOperator(operatorStr),
|
||||||
|
Value = value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (type.IsValueType)
|
||||||
|
{
|
||||||
|
return GetValueResolver(type, valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (type == typeof(string))
|
||||||
|
{
|
||||||
|
return new StringConditionResolver
|
||||||
|
{
|
||||||
|
Op = ParseStringOperator(operatorStr),
|
||||||
|
Value = valueStr
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotSupportedException($"Type {type} is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SereinConditionResolver GetValueResolver(Type valueType, string valueStr, string operatorStr, string[] parts)// where T : struct, IComparable<T>
|
||||||
|
{
|
||||||
|
SereinConditionResolver resolver;
|
||||||
|
|
||||||
|
if (valueType == typeof(float))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<float>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(decimal))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<decimal>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(double))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<double>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(sbyte))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<sbyte>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(byte))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<byte>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(short))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<short>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(ushort))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<ushort>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(int))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<int>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(uint))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<uint>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(long))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<long>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(ulong))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<ulong>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
else if (valueType == typeof(nint))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<nint>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
else if (valueType == typeof(nuint))
|
||||||
|
{
|
||||||
|
resolver = GetValueResolver<nuint>(valueStr, operatorStr, parts);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("非预期值类型");
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolver;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static ValueTypeConditionResolver<T> GetValueResolver<T>(string valueStr, string operatorStr, string[] parts)
|
||||||
|
where T :struct, IComparable<T>
|
||||||
|
{
|
||||||
|
var op = ParseValueTypeOperator<T>(operatorStr);
|
||||||
|
if (op == ValueTypeConditionResolver<T>.Operator.InRange || op == ValueTypeConditionResolver<T>.Operator.OutOfRange)
|
||||||
|
{
|
||||||
|
var temp = valueStr.Split('-');
|
||||||
|
var leftNum = string.Empty;
|
||||||
|
var rightNum = string.Empty;
|
||||||
|
if (temp.Length < 2 || temp.Length > 4)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||||
|
}
|
||||||
|
else if (temp.Length == 2)
|
||||||
|
{
|
||||||
|
leftNum = temp[0];
|
||||||
|
rightNum = temp[1];
|
||||||
|
}
|
||||||
|
else if (temp.Length == 3)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(temp[0])
|
||||||
|
&& !string.IsNullOrEmpty(temp[1])
|
||||||
|
&& !string.IsNullOrEmpty(temp[2]))
|
||||||
|
{
|
||||||
|
leftNum = "-" + temp[1];
|
||||||
|
rightNum = temp[2];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (temp.Length == 4)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(temp[0])
|
||||||
|
&& !string.IsNullOrEmpty(temp[1])
|
||||||
|
&& string.IsNullOrEmpty(temp[2])
|
||||||
|
&& !string.IsNullOrEmpty(temp[3]))
|
||||||
|
{
|
||||||
|
leftNum = "-" + temp[1];
|
||||||
|
rightNum = temp[3];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return new ValueTypeConditionResolver<T>
|
||||||
|
{
|
||||||
|
Op = op,
|
||||||
|
RangeStart = leftNum.ToValueData<T>(),
|
||||||
|
RangeEnd = rightNum.ToValueData<T>(),
|
||||||
|
ArithmeticExpression = GetArithmeticExpression(parts[0]),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new ValueTypeConditionResolver<T>
|
||||||
|
{
|
||||||
|
Op = op,
|
||||||
|
Value = valueStr.ToValueData<T>(),
|
||||||
|
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
||||||
|
//{
|
||||||
|
// return (T)ValueParse(typeof(T), value);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//public static object ValueParse(Type type, object value)
|
||||||
|
//{
|
||||||
|
|
||||||
|
// string? valueStr = value.ToString();
|
||||||
|
// if (string.IsNullOrEmpty(valueStr))
|
||||||
|
// {
|
||||||
|
// throw new ArgumentException("value is null");
|
||||||
|
// }
|
||||||
|
// object result = type switch
|
||||||
|
// {
|
||||||
|
// Type t when t.IsEnum => Enum.Parse(type, valueStr),
|
||||||
|
// Type t when t == typeof(bool) => bool.Parse(valueStr),
|
||||||
|
// Type t when t == typeof(float) => float.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(decimal) => decimal.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(double) => double.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(sbyte) => sbyte.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(byte) => byte.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(short) => short.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(ushort) => ushort.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(int) => int.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(uint) => uint.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(long) => long.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(ulong) => ulong.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(nint) => nint.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// Type t when t == typeof(nuint) => nuint.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||||
|
// _ => throw new ArgumentException("非预期值类型")
|
||||||
|
// };
|
||||||
|
// return result;
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 数值操作类型
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="operatorStr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
private static ValueTypeConditionResolver<T>.Operator ParseValueTypeOperator<T>(string operatorStr) where T : struct, IComparable<T>
|
||||||
|
{
|
||||||
|
if (operatorStr == ">")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.GreaterThan;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "<")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.LessThan;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "==")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.Equal;
|
||||||
|
}
|
||||||
|
else if (operatorStr == ">=" || operatorStr == "≥")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.GreaterThanOrEqual;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "<=" || operatorStr == "≤")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.LessThanOrEqual;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "in")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.InRange;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "!in")
|
||||||
|
{
|
||||||
|
return ValueTypeConditionResolver<T>.Operator.OutOfRange;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid operator {operatorStr} for value type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 布尔操作类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="operatorStr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
private static BoolConditionResolver.Operator ParseBoolOperator(string operatorStr)
|
||||||
|
{
|
||||||
|
if (operatorStr == "is" || operatorStr == "==" || operatorStr == "equals")
|
||||||
|
{
|
||||||
|
return BoolConditionResolver.Operator.Is;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid operator {operatorStr} for bool type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 字符串操作类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="operatorStr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
private static StringConditionResolver.Operator ParseStringOperator(string operatorStr)
|
||||||
|
{
|
||||||
|
operatorStr = operatorStr.ToLower();
|
||||||
|
|
||||||
|
if (operatorStr == "c" || operatorStr == "contains")
|
||||||
|
{
|
||||||
|
return StringConditionResolver.Operator.Contains;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "nc" || operatorStr == "doesnotcontain")
|
||||||
|
{
|
||||||
|
return StringConditionResolver.Operator.DoesNotContain;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "sw" || operatorStr == "startswith")
|
||||||
|
{
|
||||||
|
return StringConditionResolver.Operator.StartsWith;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "ew" || operatorStr == "endswith")
|
||||||
|
{
|
||||||
|
return StringConditionResolver.Operator.EndsWith;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "==" || operatorStr == "equals")
|
||||||
|
{
|
||||||
|
return StringConditionResolver.Operator.Equal;
|
||||||
|
}
|
||||||
|
else if (operatorStr == "!=" || operatorStr == "notequals")
|
||||||
|
{
|
||||||
|
return StringConditionResolver.Operator.NotEqual;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid operator {operatorStr} for string type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Library/Utils/SereinExpression/SereinConditionResolver.cs
Normal file
12
Library/Utils/SereinExpression/SereinConditionResolver.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 条件解析抽象类
|
||||||
|
/// </summary>
|
||||||
|
public abstract class SereinConditionResolver
|
||||||
|
{
|
||||||
|
public abstract bool Evaluate(object obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
378
Library/Utils/SereinExpression/SerinExpressionEvaluator.cs
Normal file
378
Library/Utils/SereinExpression/SerinExpressionEvaluator.cs
Normal file
@@ -0,0 +1,378 @@
|
|||||||
|
using Serein.Library.Utils;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Serein.NodeFlow.Tool.SereinExpression
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 使用表达式操作/获取 对象的值
|
||||||
|
/// 获取值 @get .xx.xxx
|
||||||
|
/// 设置值 @set .xx.xxx = [data]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">操作的对象</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public class SerinArithmeticExpressionEvaluator<T> where T : struct, IComparable<T>
|
||||||
|
{
|
||||||
|
private static readonly DataTable table = new DataTable();
|
||||||
|
|
||||||
|
public static T Evaluate(string expression, T inputValue)
|
||||||
|
{
|
||||||
|
|
||||||
|
// 替换占位符@为输入值
|
||||||
|
expression = expression.Replace("@", inputValue.ToString());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 使用 DataTable.Compute 方法计算表达式
|
||||||
|
var result = table.Compute(expression, string.Empty);
|
||||||
|
return (T)result;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid arithmetic expression.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SerinExpressionEvaluator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="expression">表达式</param>
|
||||||
|
/// <param name="targetObJ">操作对象</param>
|
||||||
|
/// <param name="isChange">是否改变了对象(Set语法)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
/// <exception cref="NotSupportedException"></exception>
|
||||||
|
public static object Evaluate(string expression, object targetObJ, out bool isChange)
|
||||||
|
{
|
||||||
|
//var parts = expression.Split([' '], 2);
|
||||||
|
|
||||||
|
var parts = expression.Split(new[] { ' ' }, 2, StringSplitOptions.None);
|
||||||
|
if (parts.Length != 2)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid expression format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var operation = parts[0].ToLower();
|
||||||
|
var operand = parts[1][0] == '.' ? parts[1].Substring(1) : parts[1];
|
||||||
|
object result;
|
||||||
|
if (operation == "@num")
|
||||||
|
{
|
||||||
|
result = ComputedNumber(targetObJ, operand);
|
||||||
|
}
|
||||||
|
else if (operation == "@call")
|
||||||
|
{
|
||||||
|
result = InvokeMethod(targetObJ, operand);
|
||||||
|
}
|
||||||
|
else if (operation == "@get")
|
||||||
|
{
|
||||||
|
result = GetMember(targetObJ, operand);
|
||||||
|
}
|
||||||
|
else if (operation == "@set")
|
||||||
|
{
|
||||||
|
result = SetMember(targetObJ, operand);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new NotSupportedException($"Operation {operation} is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(operation == "@set")
|
||||||
|
{
|
||||||
|
isChange = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isChange = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static readonly char[] separator = new char[] { '(', ')' };
|
||||||
|
private static readonly char[] separatorArray = new char[] { ',' };
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用目标方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标实例</param>
|
||||||
|
/// <param name="methodCall">方法名称</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
private static object InvokeMethod(object target, string methodCall)
|
||||||
|
{
|
||||||
|
if (target is null) return null;
|
||||||
|
var methodParts = methodCall.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (methodParts.Length != 2)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid method call format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var methodName = methodParts[0];
|
||||||
|
var parameterList = methodParts[1];
|
||||||
|
var parameters = parameterList.Split(separatorArray, StringSplitOptions.RemoveEmptyEntries)
|
||||||
|
.Select(p => p.Trim())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
var method = target.GetType().GetMethod(methodName) ?? throw new ArgumentException($"Method {methodName} not found on target.");
|
||||||
|
var parameterValues = method.GetParameters()
|
||||||
|
.Select((p, index) => Convert.ChangeType(parameters[index], p.ParameterType))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
|
||||||
|
return method.Invoke(target, parameterValues);
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标实例</param>
|
||||||
|
/// <param name="memberPath">属性路径</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
private static object GetMember(object target, string memberPath)
|
||||||
|
{
|
||||||
|
if (target is null) return null;
|
||||||
|
// 分割成员路径,按 '.' 处理多级访问
|
||||||
|
var members = memberPath.Split('.');
|
||||||
|
|
||||||
|
foreach (var member in members)
|
||||||
|
{
|
||||||
|
// 检查成员是否包含数组索引,例如 "cars[0]"
|
||||||
|
var arrayIndexStart = member.IndexOf('[');
|
||||||
|
if (arrayIndexStart != -1)
|
||||||
|
{
|
||||||
|
// 解析数组/集合名与索引部分
|
||||||
|
var arrayName = member.Substring(0, arrayIndexStart);
|
||||||
|
var arrayIndexEnd = member.IndexOf(']');
|
||||||
|
if (arrayIndexEnd == -1 || arrayIndexEnd <= arrayIndexStart + 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid array syntax for member {member}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取数组索引
|
||||||
|
var indexStr = member.Substring(arrayIndexStart + 1, arrayIndexEnd - arrayIndexStart - 1);
|
||||||
|
if (!int.TryParse(indexStr, out int index))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid array index '{indexStr}' for member {member}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取数组或集合对象
|
||||||
|
var arrayProperty = target?.GetType().GetProperty(arrayName);
|
||||||
|
if (arrayProperty is null)
|
||||||
|
{
|
||||||
|
var arrayField = target?.GetType().GetField(arrayName);
|
||||||
|
if (arrayField is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {arrayName} not found on target.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = arrayField.GetValue(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = arrayProperty.GetValue(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 访问数组或集合中的指定索引
|
||||||
|
if (target is Array array)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= array.Length)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Index {index} out of bounds for array {arrayName}");
|
||||||
|
}
|
||||||
|
target = array.GetValue(index);
|
||||||
|
}
|
||||||
|
else if (target is IList<object> list)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= list.Count)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Index {index} out of bounds for list {arrayName}");
|
||||||
|
}
|
||||||
|
target = list[index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {arrayName} is not an array or list.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 处理非数组情况的属性或字段
|
||||||
|
var property = target?.GetType().GetProperty(member);
|
||||||
|
if (property is null)
|
||||||
|
{
|
||||||
|
var field = target?.GetType().GetField(member);
|
||||||
|
if (field is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {member} not found on target.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = field.GetValue(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = property.GetValue(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置目标的值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标实例</param>
|
||||||
|
/// <param name="assignment">属性路径 </param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
|
private static object SetMember(object target, string assignment)
|
||||||
|
{
|
||||||
|
var parts = assignment.Split(new[] { '=' }, 2);
|
||||||
|
if (parts.Length != 2)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid assignment format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var memberPath = parts[0].Trim();
|
||||||
|
var value = parts[1].Trim();
|
||||||
|
|
||||||
|
var members = memberPath.Split('.');
|
||||||
|
for (int i = 0; i < members.Length - 1; i++)
|
||||||
|
{
|
||||||
|
var member = members[i];
|
||||||
|
|
||||||
|
// 检查是否包含数组索引
|
||||||
|
var arrayIndexStart = member.IndexOf('[');
|
||||||
|
if (arrayIndexStart != -1)
|
||||||
|
{
|
||||||
|
// 解析数组名和索引
|
||||||
|
var arrayName = member.Substring(0, arrayIndexStart);
|
||||||
|
var arrayIndexEnd = member.IndexOf(']');
|
||||||
|
if (arrayIndexEnd == -1 || arrayIndexEnd <= arrayIndexStart + 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid array syntax for member {member}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var indexStr = member.Substring(arrayIndexStart + 1, arrayIndexEnd - arrayIndexStart - 1);
|
||||||
|
if (!int.TryParse(indexStr, out int index))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid array index '{indexStr}' for member {member}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取数组或集合
|
||||||
|
var arrayProperty = target?.GetType().GetProperty(arrayName);
|
||||||
|
if (arrayProperty is null)
|
||||||
|
{
|
||||||
|
var arrayField = target?.GetType().GetField(arrayName);
|
||||||
|
if (arrayField is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {arrayName} not found on target.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = arrayField.GetValue(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = arrayProperty.GetValue(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取目标数组或集合中的指定元素
|
||||||
|
if (target is Array array)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= array.Length)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Index {index} out of bounds for array {arrayName}");
|
||||||
|
}
|
||||||
|
target = array.GetValue(index);
|
||||||
|
}
|
||||||
|
else if (target is IList<object> list)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= list.Count)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Index {index} out of bounds for list {arrayName}");
|
||||||
|
}
|
||||||
|
target = list[index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {arrayName} is not an array or list.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 处理非数组情况的属性或字段
|
||||||
|
var property = target?.GetType().GetProperty(member);
|
||||||
|
if (property is null)
|
||||||
|
{
|
||||||
|
var field = target?.GetType().GetField(member);
|
||||||
|
if (field is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {member} not found on target.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = field.GetValue(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target = property.GetValue(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置值
|
||||||
|
var lastMember = members.Last();
|
||||||
|
|
||||||
|
var lastProperty = target?.GetType().GetProperty(lastMember);
|
||||||
|
if (lastProperty is null)
|
||||||
|
{
|
||||||
|
var lastField = target?.GetType().GetField(lastMember);
|
||||||
|
if (lastField is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Member {lastMember} not found on target.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var convertedValue = Convert.ChangeType(value, lastField.FieldType);
|
||||||
|
lastField.SetValue(target, convertedValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var convertedValue = Convert.ChangeType(value, lastProperty.PropertyType);
|
||||||
|
lastProperty.SetValue(target, convertedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 计算数学简单表达式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="expression"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static decimal ComputedNumber(object value, string expression)
|
||||||
|
{
|
||||||
|
return ComputedNumber<decimal>(value, expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static T ComputedNumber<T>(object value, string expression) where T : struct, IComparable<T>
|
||||||
|
{
|
||||||
|
T result = value.ToConvert<T>();
|
||||||
|
return SerinArithmeticExpressionEvaluator<T>.Evaluate(expression, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -87,8 +87,7 @@ namespace Serein.Library.Utils
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用于临时实例的创建,不登记到IOC容器中,依赖项注入失败时也不记录。
|
/// 用于临时实例的创建,不登记到IOC容器中,依赖项注入失败时也不记录。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="controllerType"></param>
|
/// <param name="type"></param>
|
||||||
/// <param name="parameters"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public object Instantiate(Type type)
|
public object Instantiate(Type type)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ namespace Serein.NodeFlow.Base
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
object?[]? args = GetParameters(context, this, md);
|
object?[]? args = GetParameters(context, this, md);
|
||||||
result = await dd.Invoke(md.ActingInstance, args);
|
result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||||
NextOrientation = ConnectionType.IsSucceed;
|
NextOrientation = ConnectionType.IsSucceed;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,13 @@ using Serein.Library.Utils;
|
|||||||
using Serein.NodeFlow.Base;
|
using Serein.NodeFlow.Base;
|
||||||
using Serein.NodeFlow.Model;
|
using Serein.NodeFlow.Model;
|
||||||
using Serein.NodeFlow.Tool;
|
using Serein.NodeFlow.Tool;
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||||
using static Serein.NodeFlow.FlowStarter;
|
using static Serein.NodeFlow.FlowStarter;
|
||||||
@@ -180,11 +184,20 @@ namespace Serein.NodeFlow
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ISereinIOC IOC { get => this; }
|
public ISereinIOC IOC { get => this; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Library 与 MethodDetailss的依赖关系
|
||||||
|
/// </summary>
|
||||||
|
public ConcurrentDictionary<NodeLibrary, List<MethodDetails>> MethodDetailsOfLibrarys { get; } = [];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 描述所有DLL中NodeAction特性的方法的原始副本
|
/// 存储已加载的程序集
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Dictionary<NodeLibrary, List<MethodDetails>> MethodDetailss { get; } = [];
|
public ConcurrentDictionary<string, NodeLibrary> Librarys { get; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 存储已加载的方法信息。描述所有DLL中NodeAction特性的方法的原始副本
|
||||||
|
/// </summary>
|
||||||
|
public ConcurrentDictionary<string, MethodDetails> MethodDetailss { get; } = [];
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -194,17 +207,6 @@ namespace Serein.NodeFlow
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly SereinIOC sereinIOC;
|
public readonly SereinIOC sereinIOC;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 存储加载的程序集路径
|
|
||||||
/// </summary>
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 存储加载的程序集
|
|
||||||
/// </summary>
|
|
||||||
public List<NodeLibrary> NodeLibrarys { get; } = [];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 环境加载的节点集合
|
/// 环境加载的节点集合
|
||||||
/// Node Guid - Node Model
|
/// Node Guid - Node Model
|
||||||
@@ -266,7 +268,9 @@ namespace Serein.NodeFlow
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 对外暴露的接口
|
#region 基础接口
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步运行
|
/// 异步运行
|
||||||
@@ -281,15 +285,22 @@ namespace Serein.NodeFlow
|
|||||||
List<MethodDetails> initMethods = [];
|
List<MethodDetails> initMethods = [];
|
||||||
List<MethodDetails> loadMethods = [];
|
List<MethodDetails> loadMethods = [];
|
||||||
List<MethodDetails> exitMethods = [];
|
List<MethodDetails> exitMethods = [];
|
||||||
foreach(var mds in MethodDetailss.Values)
|
//foreach(var mds in MethodDetailss.Values)
|
||||||
{
|
//{
|
||||||
var initMds = mds.Where(it => it.MethodDynamicType == NodeType.Init);
|
// var initMds = mds.Where(it => it.MethodDynamicType == NodeType.Init);
|
||||||
var loadMds = mds.Where(it => it.MethodDynamicType == NodeType.Loading);
|
// var loadMds = mds.Where(it => it.MethodDynamicType == NodeType.Loading);
|
||||||
var exitMds = mds.Where(it => it.MethodDynamicType == NodeType.Exit);
|
// var exitMds = mds.Where(it => it.MethodDynamicType == NodeType.Exit);
|
||||||
initMethods.AddRange(initMds);
|
// initMethods.AddRange(initMds);
|
||||||
loadMethods.AddRange(loadMds);
|
// loadMethods.AddRange(loadMds);
|
||||||
exitMethods.AddRange(exitMds);
|
// exitMethods.AddRange(exitMds);
|
||||||
}
|
//}
|
||||||
|
var initMds = MethodDetailss.Values.Where(it => it.MethodDynamicType == NodeType.Init);
|
||||||
|
var loadMds = MethodDetailss.Values.Where(it => it.MethodDynamicType == NodeType.Loading);
|
||||||
|
var exitMds = MethodDetailss.Values.Where(it => it.MethodDynamicType == NodeType.Exit);
|
||||||
|
initMethods.AddRange(initMds);
|
||||||
|
loadMethods.AddRange(loadMds);
|
||||||
|
exitMethods.AddRange(exitMds);
|
||||||
|
|
||||||
this.IOC.Reset(); // 开始运行时清空ioc中注册的实例
|
this.IOC.Reset(); // 开始运行时清空ioc中注册的实例
|
||||||
this.IOC.CustomRegisterInstance(typeof(IFlowEnvironment).FullName,this);
|
this.IOC.CustomRegisterInstance(typeof(IFlowEnvironment).FullName,this);
|
||||||
|
|
||||||
@@ -354,8 +365,8 @@ namespace Serein.NodeFlow
|
|||||||
public void ClearAll()
|
public void ClearAll()
|
||||||
{
|
{
|
||||||
//LoadedAssemblyPaths.Clear();
|
//LoadedAssemblyPaths.Clear();
|
||||||
NodeLibrarys.Clear();
|
//NodeLibrarys.Clear();
|
||||||
MethodDetailss.Clear();
|
//MethodDetailss.Clear();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,8 +407,10 @@ namespace Serein.NodeFlow
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
TryGetMethodDetails(nodeInfo.MethodName, out MethodDetails? methodDetails); // 加载项目时尝试获取方法信息
|
//TryGetMethodDetails(nodeInfo.MethodName, out MethodDetailsInfo? methodDetailsInfo);
|
||||||
if(controlType == NodeControlType.ExpOp || controlType == NodeControlType.ExpOp)
|
|
||||||
|
MethodDetailss.TryGetValue(nodeInfo.MethodName, out var methodDetails);// 加载项目时尝试获取方法信息
|
||||||
|
if (controlType == NodeControlType.ExpOp || controlType == NodeControlType.ExpOp)
|
||||||
{
|
{
|
||||||
methodDetails ??= new MethodDetails();
|
methodDetails ??= new MethodDetails();
|
||||||
}
|
}
|
||||||
@@ -495,22 +508,53 @@ namespace Serein.NodeFlow
|
|||||||
SetStartNode(project.StartNode);
|
SetStartNode(project.StartNode);
|
||||||
OnProjectLoaded?.Invoke(new ProjectLoadedEventArgs());
|
OnProjectLoaded?.Invoke(new ProjectLoadedEventArgs());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存项目为项目文件
|
/// 加载远程项目
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addres">远程项目地址</param>
|
||||||
|
/// <param name="port">远程项目端口</param>
|
||||||
|
/// <param name="token">密码</param>
|
||||||
|
public void LoadRemoteProject(string addres, int port, string token)
|
||||||
|
{
|
||||||
|
// -- 第1种,直接从远程环境复制所有dll信息,项目信息,在本地打开?(安全问题)
|
||||||
|
// 第2种,WebSocket连接到远程环境,实时接收远程环境的响应?
|
||||||
|
Console.WriteLine($"准备连接:{addres}:{port},{token}");
|
||||||
|
|
||||||
|
// bool success = false;
|
||||||
|
//try
|
||||||
|
//{
|
||||||
|
// TcpClient tcpClient = new TcpClient();
|
||||||
|
// var result = tcpClient.BeginConnect(addres, port, null, null);
|
||||||
|
// success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));
|
||||||
|
//}
|
||||||
|
//catch (Exception ex)
|
||||||
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
//if (!success)
|
||||||
|
//{
|
||||||
|
// Console.WriteLine($"无法连接远程:{addres}:{port}");
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 序列化当前项目的依赖信息、节点信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public SereinProjectData SaveProject()
|
public SereinProjectData GetProjectInfo()
|
||||||
{
|
{
|
||||||
var projectData = new SereinProjectData()
|
var projectData = new SereinProjectData()
|
||||||
{
|
{
|
||||||
Librarys = NodeLibrarys.Select(assemblies => assemblies.Assembly.ToLibrary()).ToArray(),
|
Librarys = Librarys.Values.Select(assemblies => assemblies.Assembly.ToLibrary()).ToArray(),
|
||||||
Nodes = Nodes.Values.Select(node => node.ToInfo()).Where(info => info is not null).ToArray(),
|
Nodes = Nodes.Values.Select(node => node.ToInfo()).Where(info => info is not null).ToArray(),
|
||||||
StartNode = Nodes.Values.FirstOrDefault(it => it.IsStart)?.Guid,
|
StartNode = Nodes.Values.FirstOrDefault(it => it.IsStart)?.Guid,
|
||||||
};
|
};
|
||||||
return projectData;
|
return projectData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从文件路径中加载DLL
|
/// 从文件路径中加载DLL
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -521,9 +565,14 @@ namespace Serein.NodeFlow
|
|||||||
LoadDllNodeInfo(dllPath);
|
LoadDllNodeInfo(dllPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 移除DLL
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assemblyFullName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public bool RemoteDll(string assemblyFullName)
|
public bool RemoteDll(string assemblyFullName)
|
||||||
{
|
{
|
||||||
var library = NodeLibrarys.FirstOrDefault(nl => assemblyFullName.Equals(nl.Assembly.FullName));
|
var library = Librarys.Values.FirstOrDefault(nl => assemblyFullName.Equals(nl.Assembly.FullName));
|
||||||
if(library is null)
|
if(library is null)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -535,12 +584,14 @@ namespace Serein.NodeFlow
|
|||||||
.ToDictionary(
|
.ToDictionary(
|
||||||
key => key.Key,
|
key => key.Key,
|
||||||
group => group.Count());
|
group => group.Count());
|
||||||
|
|
||||||
|
|
||||||
if(Nodes.Count == 0)
|
if(Nodes.Count == 0)
|
||||||
{
|
{
|
||||||
return true; // 当前无节点,可以直接删除
|
return true; // 当前无节点,可以直接删除
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MethodDetailss.TryGetValue(library,out var mds)) // 存在方法
|
if (MethodDetailsOfLibrarys.TryGetValue(library,out var mds)) // 存在方法
|
||||||
{
|
{
|
||||||
foreach(var md in mds)
|
foreach(var md in mds)
|
||||||
{
|
{
|
||||||
@@ -548,12 +599,17 @@ namespace Serein.NodeFlow
|
|||||||
{
|
{
|
||||||
if (count > 0)
|
if (count > 0)
|
||||||
{
|
{
|
||||||
return false; // 创建过相关的节点
|
return false; // 创建过相关的节点,无法移除
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MethodDetailss.Remove(library);
|
// 开始移除相关信息
|
||||||
return true; // 没有创建相关的节点
|
foreach (var md in mds)
|
||||||
|
{
|
||||||
|
MethodDetailss.TryRemove(md.MethodName, out _);
|
||||||
|
}
|
||||||
|
MethodDetailsOfLibrarys.TryRemove(library, out _);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -567,9 +623,14 @@ namespace Serein.NodeFlow
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeControlType"></param>
|
/// <param name="nodeControlType"></param>
|
||||||
/// <param name="position"></param>
|
/// <param name="position"></param>
|
||||||
/// <param name="methodDetails"></param>
|
/// <param name="methodDetailsInfo">如果是表达式节点条件节点,该项为null</param>
|
||||||
public void CreateNode(NodeControlType nodeControlType, Position position, MethodDetails? methodDetails = null)
|
public void CreateNode(NodeControlType nodeControlType, Position position, MethodDetailsInfo? methodDetailsInfo = null)
|
||||||
{
|
{
|
||||||
|
MethodDetails? methodDetails = null;
|
||||||
|
if (methodDetailsInfo != null &&!MethodDetailss.TryGetValue(methodDetailsInfo.MethodName, out methodDetails))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
var nodeModel = CreateNode(nodeControlType, methodDetails);
|
var nodeModel = CreateNode(nodeControlType, methodDetails);
|
||||||
TryAddNode(nodeModel);
|
TryAddNode(nodeModel);
|
||||||
|
|
||||||
@@ -646,8 +707,8 @@ namespace Serein.NodeFlow
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 连接节点
|
/// 连接节点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fromNode">起始节点</param>
|
/// <param name="fromNodeGuid">起始节点</param>
|
||||||
/// <param name="toNode">目标节点</param>
|
/// <param name="toNodeGuid">目标节点</param>
|
||||||
/// <param name="connectionType">连接关系</param>
|
/// <param name="connectionType">连接关系</param>
|
||||||
public void ConnectNode(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
|
public void ConnectNode(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
|
||||||
{
|
{
|
||||||
@@ -684,13 +745,13 @@ namespace Serein.NodeFlow
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取方法描述
|
/// 获取方法描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool TryGetMethodDetails(string name, out MethodDetails? md)
|
public bool TryGetMethodDetailsInfo(string name, out MethodDetailsInfo? md)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(name))
|
if (!string.IsNullOrEmpty(name))
|
||||||
{
|
{
|
||||||
foreach(var mds in MethodDetailss.Values)
|
foreach(var t_md in MethodDetailss.Values)
|
||||||
{
|
{
|
||||||
md = mds.FirstOrDefault(it => it.MethodName == name);
|
md = t_md.ToInfo();
|
||||||
if(md != null)
|
if(md != null)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -706,6 +767,16 @@ namespace Serein.NodeFlow
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <para>通过方法名称获取对应的Emit委托</para>
|
||||||
|
/// <para>方法无入参时需要传入空数组,void方法自动返回null</para>
|
||||||
|
/// <para>普通方法:Func<object,object[],object></para>
|
||||||
|
/// <para>异步方法:Func<object,object[],Task></para>
|
||||||
|
/// <para>异步有返回值方法:Func<object,object[],Task<object>></para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="methodName"></param>
|
||||||
|
/// <param name="delegateDetails"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public bool TryGetDelegateDetails(string methodName, out DelegateDetails? delegateDetails)
|
public bool TryGetDelegateDetails(string methodName, out DelegateDetails? delegateDetails)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -875,7 +946,8 @@ namespace Serein.NodeFlow
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} /// <summary>
|
}
|
||||||
|
/// <summary>
|
||||||
/// 关闭全局触发器
|
/// 关闭全局触发器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeGuid"></param>
|
/// <param name="nodeGuid"></param>
|
||||||
@@ -888,13 +960,48 @@ namespace Serein.NodeFlow
|
|||||||
flowStarter.TerminateGlobalFlipflopRuning(flipflopNode);
|
flowStarter.TerminateGlobalFlipflopRuning(flipflopNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 环境执行中断
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public Task<CancelType> GetOrCreateGlobalInterruptAsync()
|
public Task<CancelType> GetOrCreateGlobalInterruptAsync()
|
||||||
{
|
{
|
||||||
IsGlobalInterrupt = true;
|
IsGlobalInterrupt = true;
|
||||||
return ChannelFlowInterrupt.GetOrCreateChannelAsync(this.EnvName);
|
return ChannelFlowInterrupt.GetOrCreateChannelAsync(this.EnvName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前环境信息(远程连接)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public object GetEnvInfo()
|
||||||
|
{
|
||||||
|
Dictionary<NodeLibrary, List<MethodDetailsInfo>> LibraryMds = [];
|
||||||
|
|
||||||
|
foreach (var mdskv in this.MethodDetailsOfLibrarys)
|
||||||
|
{
|
||||||
|
var library = mdskv.Key;
|
||||||
|
var mds = mdskv.Value;
|
||||||
|
foreach (var md in mds)
|
||||||
|
{
|
||||||
|
if (!LibraryMds.TryGetValue(library, out var t_mds))
|
||||||
|
{
|
||||||
|
t_mds = new List<MethodDetailsInfo>();
|
||||||
|
LibraryMds[library] = t_mds;
|
||||||
|
}
|
||||||
|
var mdInfo = md.ToInfo();
|
||||||
|
mdInfo.LibraryName = library.Assembly.GetName().FullName;
|
||||||
|
t_mds.Add(mdInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var project = this.GetProjectInfo();
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
project = project,
|
||||||
|
envNode = LibraryMds.Values,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Guid 转 NodeModel
|
/// Guid 转 NodeModel
|
||||||
@@ -902,7 +1009,7 @@ namespace Serein.NodeFlow
|
|||||||
/// <param name="nodeGuid">节点Guid</param>
|
/// <param name="nodeGuid">节点Guid</param>
|
||||||
/// <returns>节点Model</returns>
|
/// <returns>节点Model</returns>
|
||||||
/// <exception cref="ArgumentNullException">无法获取节点、Guid/节点为null时报错</exception>
|
/// <exception cref="ArgumentNullException">无法获取节点、Guid/节点为null时报错</exception>
|
||||||
public NodeModelBase? GuidToModel(string nodeGuid)
|
private NodeModelBase? GuidToModel(string nodeGuid)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(nodeGuid))
|
if (string.IsNullOrEmpty(nodeGuid))
|
||||||
{
|
{
|
||||||
@@ -931,10 +1038,15 @@ namespace Serein.NodeFlow
|
|||||||
(var nodeLibrary, var registerTypes, var mdlist) = LoadAssembly(dllPath);
|
(var nodeLibrary, var registerTypes, var mdlist) = LoadAssembly(dllPath);
|
||||||
if (nodeLibrary is not null && mdlist.Count > 0)
|
if (nodeLibrary is not null && mdlist.Count > 0)
|
||||||
{
|
{
|
||||||
MethodDetailss.Add(nodeLibrary, mdlist);
|
Librarys.TryAdd(nodeLibrary.Name, nodeLibrary);
|
||||||
NodeLibrarys.Add(nodeLibrary);
|
MethodDetailsOfLibrarys.TryAdd(nodeLibrary, mdlist);
|
||||||
|
|
||||||
|
foreach(var md in mdlist)
|
||||||
|
{
|
||||||
|
MethodDetailss.TryAdd(md.MethodName, md);
|
||||||
|
}
|
||||||
|
|
||||||
foreach(var kv in registerTypes)
|
foreach (var kv in registerTypes)
|
||||||
{
|
{
|
||||||
if (!AutoRegisterTypes.TryGetValue(kv.Key, out var types))
|
if (!AutoRegisterTypes.TryGetValue(kv.Key, out var types))
|
||||||
{
|
{
|
||||||
@@ -943,8 +1055,8 @@ namespace Serein.NodeFlow
|
|||||||
}
|
}
|
||||||
types.AddRange(kv.Value);
|
types.AddRange(kv.Value);
|
||||||
}
|
}
|
||||||
|
var mdInfo = mdlist.Select(md => md.ToInfo()).ToList(); // 转换成方法信息
|
||||||
OnDllLoad?.Invoke(new LoadDllEventArgs(nodeLibrary, mdlist)); // 通知UI创建dll面板显示
|
OnDllLoad?.Invoke(new LoadDllEventArgs(nodeLibrary, mdInfo)); // 通知UI创建dll面板显示
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1046,6 +1158,7 @@ namespace Serein.NodeFlow
|
|||||||
|
|
||||||
var nodeLibrary = new NodeLibrary
|
var nodeLibrary = new NodeLibrary
|
||||||
{
|
{
|
||||||
|
Name = assembly.GetName().FullName,
|
||||||
Assembly = assembly,
|
Assembly = assembly,
|
||||||
Path = dllPath,
|
Path = dllPath,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -24,15 +24,7 @@ namespace Serein.NodeFlow
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class FlowStarter
|
public class FlowStarter
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 全局触发器CTS
|
|
||||||
/// </summary>
|
|
||||||
public const string FlipFlopCtsName = "<>.FlowFlipFlopCts";
|
|
||||||
/// <summary>
|
|
||||||
/// 流程运行CTS
|
|
||||||
/// </summary>
|
|
||||||
public const string FlowRungCtsName = "<>.FlowRungCtsName";
|
|
||||||
|
|
||||||
public FlowStarter()
|
public FlowStarter()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -209,7 +201,7 @@ namespace Serein.NodeFlow
|
|||||||
{
|
{
|
||||||
throw new Exception("不存在对应委托");
|
throw new Exception("不存在对应委托");
|
||||||
}
|
}
|
||||||
await dd.Invoke(md.ActingInstance, [Context]);
|
await dd.InvokeAsync(md.ActingInstance, [Context]);
|
||||||
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||||
}
|
}
|
||||||
Context.Env.IOC.Build(); // 绑定初始化时注册的类型
|
Context.Env.IOC.Build(); // 绑定初始化时注册的类型
|
||||||
@@ -230,7 +222,7 @@ namespace Serein.NodeFlow
|
|||||||
{
|
{
|
||||||
throw new Exception("不存在对应委托");
|
throw new Exception("不存在对应委托");
|
||||||
}
|
}
|
||||||
await dd.Invoke(md.ActingInstance, [Context]);
|
await dd.InvokeAsync(md.ActingInstance, [Context]);
|
||||||
//((Action<object, object?[]?>)del).Invoke(md.ActingInstance, [Context]);
|
//((Action<object, object?[]?>)del).Invoke(md.ActingInstance, [Context]);
|
||||||
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||||
}
|
}
|
||||||
@@ -250,7 +242,7 @@ namespace Serein.NodeFlow
|
|||||||
{
|
{
|
||||||
throw new Exception("不存在对应委托");
|
throw new Exception("不存在对应委托");
|
||||||
}
|
}
|
||||||
await dd.Invoke(md.ActingInstance, [Context]);
|
await dd.InvokeAsync(md.ActingInstance, [Context]);
|
||||||
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,7 +269,7 @@ namespace Serein.NodeFlow
|
|||||||
env.FlipFlopState = RunState.Running;
|
env.FlipFlopState = RunState.Running;
|
||||||
// 如果存在需要启动的触发器,则开始启动
|
// 如果存在需要启动的触发器,则开始启动
|
||||||
_flipFlopCts = new CancellationTokenSource();
|
_flipFlopCts = new CancellationTokenSource();
|
||||||
env.IOC.CustomRegisterInstance(FlipFlopCtsName, _flipFlopCts,false);
|
env.IOC.CustomRegisterInstance(NodeStaticConfig.FlipFlopCtsName, _flipFlopCts,false);
|
||||||
|
|
||||||
// 使用 TaskCompletionSource 创建未启动的触发器任务
|
// 使用 TaskCompletionSource 创建未启动的触发器任务
|
||||||
var tasks = flipflopNodes.Select(async node =>
|
var tasks = flipflopNodes.Select(async node =>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
throw new NotImplementedException("动作区域暂未实现");
|
throw new NotImplementedException("动作区域暂未实现");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override Parameterdata[] GetParameterdatas()
|
public override Parameterdata[] GetParameterdatas()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Task.FromResult(PreviousNode?.GetFlowData()); // 条件区域透传上一节点的数据
|
return Task.FromResult( PreviousNode?.GetFlowData()); // 条件区域透传上一节点的数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override Parameterdata[] GetParameterdatas()
|
public override Parameterdata[] GetParameterdatas()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
public class SingleActionNode : NodeModelBase
|
public class SingleActionNode : NodeModelBase
|
||||||
{
|
{
|
||||||
|
|
||||||
internal override Parameterdata[] GetParameterdatas()
|
public override Parameterdata[] GetParameterdatas()
|
||||||
{
|
{
|
||||||
if (base.MethodDetails.ParameterDetailss.Length > 0)
|
if (base.MethodDetails.ParameterDetailss.Length > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
return Task.FromResult(result);
|
return Task.FromResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override Parameterdata[] GetParameterdatas()
|
public override Parameterdata[] GetParameterdatas()
|
||||||
{
|
{
|
||||||
var value = CustomData switch
|
var value = CustomData switch
|
||||||
{
|
{
|
||||||
@@ -90,8 +90,6 @@ namespace Serein.NodeFlow.Model
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public override NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
public override NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||||
{
|
{
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override Parameterdata[] GetParameterdatas()
|
public override Parameterdata[] GetParameterdatas()
|
||||||
{
|
{
|
||||||
return [new Parameterdata{ Expression = Expression}];
|
return [new Parameterdata{ Expression = Expression}];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var args = GetParameters(context, this, md);
|
var args = GetParameters(context, this, md);
|
||||||
var result = await dd.Invoke(md.ActingInstance, args);
|
var result = await dd.InvokeAsync(md.ActingInstance, args);
|
||||||
dynamic flipflopContext = result;
|
dynamic flipflopContext = result;
|
||||||
FlipflopStateType flipflopStateType = flipflopContext.State;
|
FlipflopStateType flipflopStateType = flipflopContext.State;
|
||||||
NextOrientation = flipflopStateType.ToContentType();
|
NextOrientation = flipflopStateType.ToContentType();
|
||||||
@@ -80,7 +80,7 @@ namespace Serein.NodeFlow.Model
|
|||||||
{
|
{
|
||||||
return context.Value; // dynamic 会在运行时处理类型
|
return context.Value; // dynamic 会在运行时处理类型
|
||||||
}
|
}
|
||||||
internal override Parameterdata[] GetParameterdatas()
|
public override Parameterdata[] GetParameterdatas()
|
||||||
{
|
{
|
||||||
if (base.MethodDetails.ParameterDetailss.Length > 0)
|
if (base.MethodDetails.ParameterDetailss.Length > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Serein.NodeFlow
|
|||||||
public class MoveNodeData
|
public class MoveNodeData
|
||||||
{
|
{
|
||||||
public NodeControlType NodeControlType { get; set; }
|
public NodeControlType NodeControlType { get; set; }
|
||||||
public MethodDetails MethodDetails { get; set; }
|
// public MethodDetails MethodDetails { get; set; }
|
||||||
|
public MethodDetailsInfo MethodDetailsInfo { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Serein.Library.Api;
|
using Serein.Library.Enums;
|
||||||
using Serein.Library.Enums;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|||||||
@@ -19,18 +19,25 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Remove="Base\**" />
|
||||||
<Compile Remove="bin\**" />
|
<Compile Remove="bin\**" />
|
||||||
<Compile Remove="SerinExpression\**" />
|
<Compile Remove="SerinExpression\**" />
|
||||||
|
<Compile Remove="Tool\SereinExpression\**" />
|
||||||
|
<EmbeddedResource Remove="Base\**" />
|
||||||
<EmbeddedResource Remove="bin\**" />
|
<EmbeddedResource Remove="bin\**" />
|
||||||
<EmbeddedResource Remove="SerinExpression\**" />
|
<EmbeddedResource Remove="SerinExpression\**" />
|
||||||
|
<EmbeddedResource Remove="Tool\SereinExpression\**" />
|
||||||
|
<None Remove="Base\**" />
|
||||||
<None Remove="bin\**" />
|
<None Remove="bin\**" />
|
||||||
<None Remove="SerinExpression\**" />
|
<None Remove="SerinExpression\**" />
|
||||||
|
<None Remove="Tool\SereinExpression\**" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="ConnectionType.cs" />
|
<Compile Remove="ConnectionType.cs" />
|
||||||
<Compile Remove="DynamicContext.cs" />
|
<Compile Remove="DynamicContext.cs" />
|
||||||
<Compile Remove="MethodDetails.cs" />
|
<Compile Remove="MethodDetails.cs" />
|
||||||
|
<Compile Remove="NodeStaticConfig.cs" />
|
||||||
<Compile Remove="Tool\Attribute.cs" />
|
<Compile Remove="Tool\Attribute.cs" />
|
||||||
<Compile Remove="Tool\DynamicTool.cs" />
|
<Compile Remove="Tool\DynamicTool.cs" />
|
||||||
<Compile Remove="Tool\ExpressionHelper.cs" />
|
<Compile Remove="Tool\ExpressionHelper.cs" />
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
Loaded="Window_Loaded"
|
Loaded="Window_Loaded"
|
||||||
ContentRendered="Window_ContentRendered"
|
ContentRendered="Window_ContentRendered"
|
||||||
PreviewKeyDown="Window_PreviewKeyDown"
|
PreviewKeyDown="Window_PreviewKeyDown"
|
||||||
PreviewTextInput="Window_PreviewTextInput"
|
|
||||||
Closing="Window_Closing">
|
Closing="Window_Closing">
|
||||||
|
|
||||||
<Window.Resources>
|
<Window.Resources>
|
||||||
@@ -36,40 +35,56 @@
|
|||||||
<ColumnDefinition Width="*"/>
|
<ColumnDefinition Width="*"/>
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<StackPanel Grid.Row="0" Grid.ColumnSpan="5" Background="#F5F5F5" Orientation="Horizontal" >
|
<Menu DockPanel.Dock="Top" Grid.Row="0" Height="20">
|
||||||
<Button x:Name="ButtonDebugRun" Content="运行" Width="100" Margin="10" Click="ButtonDebugRun_Click"></Button>
|
<MenuItem Header="文件">
|
||||||
<Button x:Name="ButtonDebugFlipflopNode" Content="结束" Width="100" Margin="10" Click="ButtonDebugFlipflopNode_Click"></Button>
|
<!--菜单项为MenuItem,文字使用属性 Header-->
|
||||||
<Button x:Name="ButtonStartFlowInSelectNode" Content="从选定节点开始" Width="100" Margin="10" Click="ButtonStartFlowInSelectNode_Click"></Button>
|
<MenuItem Header="保存项目" Click="ButtonSaveFile_Click" ></MenuItem>
|
||||||
<Button x:Name="ButtonResetCanvas" Content="重置画布" Width="100" Margin="10" Click="ButtonResetCanvas_Click"></Button>
|
<MenuItem Header="打开本地文件" Click="OpenLocalProject_Click"></MenuItem>
|
||||||
<Button x:Name="ButtonTestExpObj" Content="测试对象表达式" Width="100" Margin="10" Click="ButtonTestExpObj_Click"></Button>
|
<MenuItem Header="连接远程环境" Click="OpenRemoteProject_Click"></MenuItem>
|
||||||
</StackPanel>
|
</MenuItem>
|
||||||
|
<MenuItem Header="调试">
|
||||||
|
<MenuItem Header="运行(从起始节点)" Click="ButtonDebugRun_Click"></MenuItem>
|
||||||
|
<MenuItem Header="运行(从选定节点)" Click="ButtonDebugFlipflopNode_Click"></MenuItem>
|
||||||
|
<MenuItem Header="结束流程" Click="ButtonDebugFlipflopNode_Click"></MenuItem>
|
||||||
|
</MenuItem>
|
||||||
|
<MenuItem Header="视图">
|
||||||
|
<MenuItem Header="输出窗口" Click="ButtonOpenConsoleOutWindow_Click"></MenuItem>
|
||||||
|
<MenuItem Header="重置画布" Click="ButtonResetCanvas_Click"></MenuItem>
|
||||||
|
</MenuItem>
|
||||||
|
<!--<MenuItem Header="说明"></MenuItem>-->
|
||||||
|
</Menu>
|
||||||
|
|
||||||
<DockPanel Grid.Row="1" Grid.Column="0" Background="#F5F5F5">
|
<DockPanel Grid.Row="1" Grid.Column="0" Background="#F5F5F5">
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="*"/>
|
<ColumnDefinition Width="*"/>
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<RowDefinition Height="40"></RowDefinition>
|
<RowDefinition Height="auto"></RowDefinition>
|
||||||
<RowDefinition Height="Auto"></RowDefinition>
|
<RowDefinition Height="*"></RowDefinition>
|
||||||
<RowDefinition Height="2*"></RowDefinition>
|
|
||||||
<!--<RowDefinition Height="3"></RowDefinition>-->
|
<!--<RowDefinition Height="3"></RowDefinition>-->
|
||||||
<!--<RowDefinition Height="3*"></RowDefinition>-->
|
<!--<RowDefinition Height="3*"></RowDefinition>-->
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
<Grid Margin="2,2,1,5" Grid.Row="0" >
|
<!--<Grid Margin="2,2,1,5" Grid.Row="0" >
|
||||||
<Button Grid.Row="0" Content="保存项目" Click="ButtonSaveFile_Click" HorizontalAlignment="Left" Margin="5,5,5,5"/>
|
<Grid.ColumnDefinitions>
|
||||||
<!--<Button Grid.Row="0" Content="卸载清空" Click="UnloadAllButton_Click" HorizontalAlignment="Right" Margin="5,5,5,5"/>-->
|
<ColumnDefinition Width="auto"/>
|
||||||
</Grid>
|
<ColumnDefinition Width="auto"/>
|
||||||
|
<ColumnDefinition Width="*"/>
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
<Button Grid.Column="0" Content="保存项目" Click="ButtonSaveFile_Click" HorizontalAlignment="Left" Margin="5,5,5,5"/>
|
||||||
|
--><!--<Button Grid.Column="1" Content="加载项目" Click="ButtonLoadProjectFile_Click" HorizontalAlignment="Left" Margin="5,5,5,5"/>-->
|
||||||
|
<!--<Button Grid.Row="0" Content="卸载清空" Click="UnloadAllButton_Click" HorizontalAlignment="Right" Margin="5,5,5,5"/>--><!--
|
||||||
|
</Grid>-->
|
||||||
|
|
||||||
<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto">
|
<ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto">
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal">
|
||||||
<nodeView:ExpOpNodeControl x:Name="ExpOpNodeControl" Margin="10" AllowDrop="True" PreviewMouseMove="BaseNodeControl_PreviewMouseMove"/>
|
<nodeView:ExpOpNodeControl x:Name="ExpOpNodeControl" Margin="10" AllowDrop="True" PreviewMouseMove="BaseNodeControl_PreviewMouseMove"/>
|
||||||
<nodeView:ConditionNodeControl x:Name="ConditionNodeControl" Margin="10" AllowDrop="True" PreviewMouseMove="BaseNodeControl_PreviewMouseMove"/>
|
<nodeView:ConditionNodeControl x:Name="ConditionNodeControl" Margin="10" AllowDrop="True" PreviewMouseMove="BaseNodeControl_PreviewMouseMove"/>
|
||||||
<nodeView:ConditionRegionControl x:Name="ConditionRegionControl" Margin="10" AllowDrop="True" PreviewMouseMove="BaseNodeControl_PreviewMouseMove"/>
|
<nodeView:ConditionRegionControl x:Name="ConditionRegionControl" Margin="10" AllowDrop="True" PreviewMouseMove="BaseNodeControl_PreviewMouseMove"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto" MaxHeight="400" Grid.RowSpan="2">
|
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" MaxHeight="400" Grid.RowSpan="2">
|
||||||
<StackPanel x:Name="DllStackPanel" Margin="5"/>
|
<StackPanel x:Name="DllStackPanel" Margin="5"/>
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
<!--<GridSplitter Grid.Row="3" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Center" ResizeBehavior="PreviousAndNext" Background="Gray"/>-->
|
<!--<GridSplitter Grid.Row="3" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Center" ResizeBehavior="PreviousAndNext" Background="Gray"/>-->
|
||||||
|
|||||||
@@ -255,6 +255,7 @@ namespace Serein.WorkBench
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 运行环境事件
|
#region 运行环境事件
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加载完成
|
/// 加载完成
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -281,19 +282,19 @@ namespace Serein.WorkBench
|
|||||||
{
|
{
|
||||||
this.Dispatcher.Invoke(() => {
|
this.Dispatcher.Invoke(() => {
|
||||||
NodeLibrary nodeLibrary = eventArgs.NodeLibrary;
|
NodeLibrary nodeLibrary = eventArgs.NodeLibrary;
|
||||||
List<MethodDetails> methodDetailss = eventArgs.MethodDetailss;
|
List<MethodDetailsInfo> methodDetailss = eventArgs.MethodDetailss;
|
||||||
|
|
||||||
var dllControl = new DllControl(nodeLibrary);
|
var dllControl = new DllControl(nodeLibrary);
|
||||||
|
|
||||||
foreach (var methodDetails in methodDetailss)
|
foreach (var methodDetailsInfo in methodDetailss)
|
||||||
{
|
{
|
||||||
switch (methodDetails.MethodDynamicType)
|
switch (methodDetailsInfo.NodeType)
|
||||||
{
|
{
|
||||||
case Library.Enums.NodeType.Action:
|
case Library.Enums.NodeType.Action:
|
||||||
dllControl.AddAction(methodDetails.Clone()); // 添加动作类型到控件
|
dllControl.AddAction(methodDetailsInfo); // 添加动作类型到控件
|
||||||
break;
|
break;
|
||||||
case Library.Enums.NodeType.Flipflop:
|
case Library.Enums.NodeType.Flipflop:
|
||||||
dllControl.AddFlipflop(methodDetails.Clone()); // 添加触发器方法到控件
|
dllControl.AddFlipflop(methodDetailsInfo); // 添加触发器方法到控件
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -320,20 +321,13 @@ namespace Serein.WorkBench
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 节点连接关系变更
|
/// 节点连接关系变更
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fromNodeGuid"></param>
|
/// <param name="eventArgs"></param>
|
||||||
/// <param name="toNodeGuid"></param>
|
|
||||||
/// <param name="connectionType"></param>
|
|
||||||
private void FlowEnvironment_NodeConnectChangeEvemt(NodeConnectChangeEventArgs eventArgs)
|
private void FlowEnvironment_NodeConnectChangeEvemt(NodeConnectChangeEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
string fromNodeGuid = eventArgs.FromNodeGuid;
|
string fromNodeGuid = eventArgs.FromNodeGuid;
|
||||||
string toNodeGuid = eventArgs.ToNodeGuid;
|
string toNodeGuid = eventArgs.ToNodeGuid;
|
||||||
NodeControlBase? fromNode = GuidToControl(fromNodeGuid);
|
if (!TryGetControl(fromNodeGuid, out var fromNode)
|
||||||
NodeControlBase? toNode = GuidToControl(toNodeGuid);
|
|| !TryGetControl(toNodeGuid, out var toNode))
|
||||||
if(fromNode != null && toNode != null)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -394,8 +388,6 @@ namespace Serein.WorkBench
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 节点移除事件
|
/// 节点移除事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -403,7 +395,11 @@ namespace Serein.WorkBench
|
|||||||
private void FlowEnvironment_NodeRemoteEvent(NodeRemoteEventArgs eventArgs)
|
private void FlowEnvironment_NodeRemoteEvent(NodeRemoteEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
var nodeGuid = eventArgs.NodeGuid;
|
var nodeGuid = eventArgs.NodeGuid;
|
||||||
NodeControlBase? nodeControl = GuidToControl(nodeGuid);
|
if (!TryGetControl(nodeGuid, out var nodeControl))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (nodeControl is null) return;
|
if (nodeControl is null) return;
|
||||||
if (selectNodeControls.Count > 0)
|
if (selectNodeControls.Count > 0)
|
||||||
{
|
{
|
||||||
@@ -494,7 +490,6 @@ namespace Serein.WorkBench
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置了流程起始控件
|
/// 设置了流程起始控件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -506,12 +501,10 @@ namespace Serein.WorkBench
|
|||||||
{
|
{
|
||||||
string oldNodeGuid = eventArgs.OldNodeGuid;
|
string oldNodeGuid = eventArgs.OldNodeGuid;
|
||||||
string newNodeGuid = eventArgs.NewNodeGuid;
|
string newNodeGuid = eventArgs.NewNodeGuid;
|
||||||
NodeControlBase? newStartNodeControl = GuidToControl(newNodeGuid);
|
if (!TryGetControl(newNodeGuid, out var newStartNodeControl)) return;
|
||||||
if (newStartNodeControl is null) return;
|
|
||||||
if (!string.IsNullOrEmpty(oldNodeGuid))
|
if (!string.IsNullOrEmpty(oldNodeGuid))
|
||||||
{
|
{
|
||||||
NodeControlBase? oldStartNodeControl = GuidToControl(oldNodeGuid);
|
if (!TryGetControl(oldNodeGuid, out var oldStartNodeControl)) return;
|
||||||
if (oldStartNodeControl is null) return;
|
|
||||||
oldStartNodeControl.BorderBrush = Brushes.Black;
|
oldStartNodeControl.BorderBrush = Brushes.Black;
|
||||||
oldStartNodeControl.BorderThickness = new Thickness(0);
|
oldStartNodeControl.BorderThickness = new Thickness(0);
|
||||||
}
|
}
|
||||||
@@ -527,7 +520,6 @@ namespace Serein.WorkBench
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 被监视的对象发生改变
|
/// 被监视的对象发生改变
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -564,7 +556,6 @@ namespace Serein.WorkBench
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 节点中断状态改变。
|
/// 节点中断状态改变。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -572,8 +563,8 @@ namespace Serein.WorkBench
|
|||||||
private void FlowEnvironment_OnNodeInterruptStateChange(NodeInterruptStateChangeEventArgs eventArgs)
|
private void FlowEnvironment_OnNodeInterruptStateChange(NodeInterruptStateChangeEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
string nodeGuid = eventArgs.NodeGuid;
|
string nodeGuid = eventArgs.NodeGuid;
|
||||||
NodeControlBase? nodeControl = GuidToControl(nodeGuid);
|
if (!TryGetControl(nodeGuid, out var nodeControl)) return;
|
||||||
if (nodeControl is null) return;
|
|
||||||
if (eventArgs.Class == InterruptClass.None)
|
if (eventArgs.Class == InterruptClass.None)
|
||||||
{
|
{
|
||||||
nodeControl.ViewModel.IsInterrupt = false;
|
nodeControl.ViewModel.IsInterrupt = false;
|
||||||
@@ -610,8 +601,7 @@ namespace Serein.WorkBench
|
|||||||
private void FlowEnvironment_OnInterruptTrigger(InterruptTriggerEventArgs eventArgs)
|
private void FlowEnvironment_OnInterruptTrigger(InterruptTriggerEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
string nodeGuid = eventArgs.NodeGuid;
|
string nodeGuid = eventArgs.NodeGuid;
|
||||||
NodeControlBase? nodeControl = GuidToControl(nodeGuid);
|
if (!TryGetControl(nodeGuid, out var nodeControl)) return;
|
||||||
if (nodeControl is null) return;
|
|
||||||
if(eventArgs.Type == InterruptTriggerEventArgs.InterruptTriggerType.Exp)
|
if(eventArgs.Type == InterruptTriggerEventArgs.InterruptTriggerType.Exp)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"表达式触发了中断:{eventArgs.Expression}");
|
Console.WriteLine($"表达式触发了中断:{eventArgs.Expression}");
|
||||||
@@ -639,8 +629,7 @@ namespace Serein.WorkBench
|
|||||||
/// <exception cref="NotImplementedException"></exception>
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
private void FlowEnvironment_OnNodeLocate(NodeLocatedEventArgs eventArgs)
|
private void FlowEnvironment_OnNodeLocate(NodeLocatedEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
NodeControlBase? nodeControl = GuidToControl(eventArgs.NodeGuid);
|
if (!TryGetControl(eventArgs.NodeGuid, out var nodeControl)) return;
|
||||||
if (nodeControl is null) return;
|
|
||||||
//scaleTransform.ScaleX = 1;
|
//scaleTransform.ScaleX = 1;
|
||||||
//scaleTransform.ScaleY = 1;
|
//scaleTransform.ScaleY = 1;
|
||||||
// 获取控件在 FlowChartCanvas 上的相对位置
|
// 获取控件在 FlowChartCanvas 上的相对位置
|
||||||
@@ -669,7 +658,6 @@ namespace Serein.WorkBench
|
|||||||
|
|
||||||
//if (!isInView)
|
//if (!isInView)
|
||||||
//{
|
//{
|
||||||
|
|
||||||
//}
|
//}
|
||||||
// 计算平移偏移量,使得控件在可视区域的中心
|
// 计算平移偏移量,使得控件在可视区域的中心
|
||||||
double translateX = scaledCenterX - FlowChartStackGrid.ActualWidth / 2;
|
double translateX = scaledCenterX - FlowChartStackGrid.ActualWidth / 2;
|
||||||
@@ -688,6 +676,7 @@ namespace Serein.WorkBench
|
|||||||
ElasticAnimation(nodeControl, translateTransform, 4,1,0.5);
|
ElasticAnimation(nodeControl, translateTransform, 4,1,0.5);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 控件抖动
|
/// 控件抖动
|
||||||
/// 来源:https://www.cnblogs.com/RedSky/p/17705411.html
|
/// 来源:https://www.cnblogs.com/RedSky/p/17705411.html
|
||||||
@@ -695,6 +684,7 @@ namespace Serein.WorkBench
|
|||||||
/// (……太好用了)
|
/// (……太好用了)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="translate"></param>
|
/// <param name="translate"></param>
|
||||||
|
/// <param name="nodeControl">需要抖动的控件</param>
|
||||||
/// <param name="power">抖动第一下偏移量</param>
|
/// <param name="power">抖动第一下偏移量</param>
|
||||||
/// <param name="range">减弱幅度(小于等于power,大于0)</param>
|
/// <param name="range">减弱幅度(小于等于power,大于0)</param>
|
||||||
/// <param name="speed">持续系数(大于0),越大时间越长,</param>
|
/// <param name="speed">持续系数(大于0),越大时间越长,</param>
|
||||||
@@ -724,26 +714,28 @@ namespace Serein.WorkBench
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Guid 转 NodeControl
|
/// Guid 转 NodeControl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeGuid">节点Guid</param>
|
/// <param name="nodeGuid"></param>
|
||||||
/// <returns>节点Model</returns>
|
/// <param name="nodeControl"></param>
|
||||||
/// <exception cref="ArgumentNullException">无法获取节点、Guid/节点为null时报错</exception>
|
/// <returns></returns>
|
||||||
private NodeControlBase? GuidToControl(string nodeGuid)
|
private bool TryGetControl(string nodeGuid,out NodeControlBase nodeControl)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(nodeGuid))
|
if (string.IsNullOrEmpty(nodeGuid))
|
||||||
{
|
{
|
||||||
return null;
|
nodeControl = null;
|
||||||
throw new ArgumentNullException("not contains - Guid没有对应节点:" + (nodeGuid));
|
return false;
|
||||||
}
|
}
|
||||||
if (!NodeControls.TryGetValue(nodeGuid, out NodeControlBase? nodeControl) || nodeControl is null)
|
if (!NodeControls.TryGetValue(nodeGuid, out nodeControl))
|
||||||
{
|
{
|
||||||
return null;
|
nodeControl = null;
|
||||||
|
return false;
|
||||||
throw new ArgumentNullException("null - Guid存在对应节点,但节点为null:" + (nodeGuid));
|
|
||||||
}
|
}
|
||||||
return nodeControl;
|
if(nodeControl is null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 加载项目文件后触发事件相关方法
|
#region 加载项目文件后触发事件相关方法
|
||||||
@@ -906,7 +898,7 @@ namespace Serein.WorkBench
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
if (nodeControl?.ViewModel?.Node?.MethodDetails?.ReturnType is Type returnType && returnType != typeof(void))
|
if (nodeControl.ViewModel?.Node?.MethodDetails?.ReturnType is Type returnType && returnType != typeof(void))
|
||||||
{
|
{
|
||||||
contextMenu.Items.Add(CreateMenuItem("查看返回类型", (s, e) =>
|
contextMenu.Items.Add(CreateMenuItem("查看返回类型", (s, e) =>
|
||||||
{
|
{
|
||||||
@@ -1180,7 +1172,7 @@ namespace Serein.WorkBench
|
|||||||
if (e.Data.GetData(MouseNodeType.CreateDllNodeInCanvas) is MoveNodeData nodeData)
|
if (e.Data.GetData(MouseNodeType.CreateDllNodeInCanvas) is MoveNodeData nodeData)
|
||||||
{
|
{
|
||||||
// 创建DLL文件的节点对象
|
// 创建DLL文件的节点对象
|
||||||
FlowEnvironment.CreateNode(nodeData.NodeControlType, position, nodeData.MethodDetails);
|
FlowEnvironment.CreateNode(nodeData.NodeControlType, position, nodeData.MethodDetailsInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (e.Data.GetDataPresent(MouseNodeType.CreateBaseNodeInCanvas))
|
else if (e.Data.GetDataPresent(MouseNodeType.CreateBaseNodeInCanvas))
|
||||||
@@ -1208,8 +1200,7 @@ namespace Serein.WorkBench
|
|||||||
/// 尝试将节点放置在区域中
|
/// 尝试将节点放置在区域中
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nodeControl"></param>
|
/// <param name="nodeControl"></param>
|
||||||
/// <param name="dropPosition"></param>
|
/// <param name="position"></param>
|
||||||
/// <param name="e"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private bool TryPlaceNodeInRegion(NodeControlBase nodeControl, Position position)
|
private bool TryPlaceNodeInRegion(NodeControlBase nodeControl, Position position)
|
||||||
{
|
{
|
||||||
@@ -1764,6 +1755,7 @@ namespace Serein.WorkBench
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void FlowChartCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
private void FlowChartCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine(1);
|
||||||
if (!IsSelectControl)
|
if (!IsSelectControl)
|
||||||
{
|
{
|
||||||
// 进入选取状态
|
// 进入选取状态
|
||||||
@@ -2335,32 +2327,8 @@ namespace Serein.WorkBench
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 卸载DLL文件,清空当前项目
|
#region 顶部菜单栏 - 调试功能区
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender"></param>
|
|
||||||
/// <param name="e"></param>
|
|
||||||
private void UnloadAllButton_Click(object sender, RoutedEventArgs e)
|
|
||||||
{
|
|
||||||
FlowEnvironment.ClearAll();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 卸载DLL文件,清空当前项目
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender"></param>
|
|
||||||
/// <param name="e"></param>
|
|
||||||
private void UnloadAllAssemblies()
|
|
||||||
{
|
|
||||||
DllStackPanel.Children.Clear();
|
|
||||||
FlowChartCanvas.Children.Clear();
|
|
||||||
Connections.Clear();
|
|
||||||
NodeControls.Clear();
|
|
||||||
currentLine = null;
|
|
||||||
startConnectNodeControl = null;
|
|
||||||
MessageBox.Show("所有DLL已卸载。", "信息", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 运行测试
|
/// 运行测试
|
||||||
@@ -2394,7 +2362,7 @@ namespace Serein.WorkBench
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private async void ButtonStartFlowInSelectNode_Click(object sender, RoutedEventArgs e)
|
private async void ButtonStartFlowInSelectNode_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if(selectNodeControls.Count == 0)
|
if (selectNodeControls.Count == 0)
|
||||||
{
|
{
|
||||||
Console.WriteLine("请至少选择一个节点");
|
Console.WriteLine("请至少选择一个节点");
|
||||||
}
|
}
|
||||||
@@ -2409,6 +2377,14 @@ namespace Serein.WorkBench
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 顶部菜单栏 - 项目文件菜单
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存为项目文件
|
/// 保存为项目文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -2416,7 +2392,7 @@ namespace Serein.WorkBench
|
|||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonSaveFile_Click(object sender, RoutedEventArgs e)
|
private void ButtonSaveFile_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var projectData = FlowEnvironment.SaveProject();
|
var projectData = FlowEnvironment.GetProjectInfo();
|
||||||
|
|
||||||
projectData.Basic = new Basic
|
projectData.Basic = new Basic
|
||||||
{
|
{
|
||||||
@@ -2432,10 +2408,10 @@ namespace Serein.WorkBench
|
|||||||
Versions = "1",
|
Versions = "1",
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach(var node in projectData.Nodes)
|
foreach (var node in projectData.Nodes)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(NodeControls.TryGetValue(node.Guid,out var nodeControl))
|
if (NodeControls.TryGetValue(node.Guid, out var nodeControl))
|
||||||
{
|
{
|
||||||
Point positionRelativeToParent = nodeControl.TranslatePoint(new Point(0, 0), FlowChartCanvas);
|
Point positionRelativeToParent = nodeControl.TranslatePoint(new Point(0, 0), FlowChartCanvas);
|
||||||
node.Position = new Position(positionRelativeToParent.X, positionRelativeToParent.Y);
|
node.Position = new Position(positionRelativeToParent.X, positionRelativeToParent.Y);
|
||||||
@@ -2481,7 +2457,7 @@ namespace Serein.WorkBench
|
|||||||
}
|
}
|
||||||
|
|
||||||
JObject projectJsonData = JObject.FromObject(projectData);
|
JObject projectJsonData = JObject.FromObject(projectData);
|
||||||
savaProjectFile?.Invoke(savePath, projectJsonData.ToString());
|
savaProjectFile?.Invoke(savePath, projectJsonData.ToString());
|
||||||
}
|
}
|
||||||
public static bool SaveContentToFile(out string savePath, out Action<string, string>? savaProjectFile)
|
public static bool SaveContentToFile(out string savePath, out Action<string, string>? savaProjectFile)
|
||||||
{
|
{
|
||||||
@@ -2499,7 +2475,7 @@ namespace Serein.WorkBench
|
|||||||
// 如果用户选择了文件并点击了保存按钮
|
// 如果用户选择了文件并点击了保存按钮
|
||||||
if (result == true)
|
if (result == true)
|
||||||
{
|
{
|
||||||
savePath = saveFileDialog.FileName;
|
savePath = saveFileDialog.FileName;
|
||||||
savaProjectFile = File.WriteAllText;
|
savaProjectFile = File.WriteAllText;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -2507,18 +2483,57 @@ namespace Serein.WorkBench
|
|||||||
savaProjectFile = null;
|
savaProjectFile = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public static string GetRelativePath(string baseDirectory, string fullPath)
|
|
||||||
{
|
|
||||||
Uri baseUri = new(baseDirectory + System.IO.Path.DirectorySeparatorChar);
|
|
||||||
Uri fullUri = new(fullPath);
|
|
||||||
Uri relativeUri = baseUri.MakeRelativeUri(fullUri);
|
|
||||||
return Uri.UnescapeDataString(relativeUri.ToString().Replace('/', System.IO.Path.DirectorySeparatorChar));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
|
/// <summary>
|
||||||
|
/// 打开本地项目文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void OpenLocalProject_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 连接远程运行环境
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void OpenRemoteProject_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
var windowEnvRemoteLoginView = new WindowEnvRemoteLoginView((addres,port,token) =>
|
||||||
|
{
|
||||||
|
this.FlowEnvironment.LoadRemoteProject(addres, port, token);
|
||||||
|
});
|
||||||
|
windowEnvRemoteLoginView.Show();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 顶部菜单栏 - 视图管理
|
||||||
|
/// <summary>
|
||||||
|
/// 重置画布
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void ButtonResetCanvas_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
translateTransform.X = 0;
|
||||||
|
translateTransform.Y = 0;
|
||||||
|
scaleTransform.ScaleX = 1;
|
||||||
|
scaleTransform.ScaleY = 1;
|
||||||
|
}
|
||||||
|
private void ButtonOpenConsoleOutWindow_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
logWindow?.Show();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 按键监听。esc取消操作
|
/// 按键监听。esc取消操作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -2596,16 +2611,29 @@ namespace Serein.WorkBench
|
|||||||
data = SerinExpressionEvaluator.Evaluate(exp,result!, out isChange);
|
data = SerinExpressionEvaluator.Evaluate(exp,result!, out isChange);
|
||||||
Console.WriteLine($"{exp} => {data}");
|
Console.WriteLine($"{exp} => {data}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 重置画布
|
/// 卸载DLL文件,清空当前项目
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void ButtonResetCanvas_Click(object sender, RoutedEventArgs e)
|
private void UnloadAllButton_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
translateTransform.X = 0;
|
FlowEnvironment.ClearAll();
|
||||||
translateTransform.Y = 0;
|
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 卸载DLL文件,清空当前项目
|
||||||
|
/// </summary>
|
||||||
|
private void UnloadAllAssemblies()
|
||||||
|
{
|
||||||
|
DllStackPanel.Children.Clear();
|
||||||
|
FlowChartCanvas.Children.Clear();
|
||||||
|
Connections.Clear();
|
||||||
|
NodeControls.Clear();
|
||||||
|
currentLine = null;
|
||||||
|
startConnectNodeControl = null;
|
||||||
|
MessageBox.Show("所有DLL已卸载。", "信息", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ namespace Serein.WorkBench.Node.View
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加条件控件
|
/// 添加条件控件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="condition"></param>
|
/// <param name="node"></param>
|
||||||
public void AddCondition(NodeControlBase node)
|
public void AddCondition(NodeControlBase node)
|
||||||
{
|
{
|
||||||
((CompositeConditionNode)ViewModel.Node).AddNode((SingleConditionNode)node.ViewModel.Node);
|
((CompositeConditionNode)ViewModel.Node).AddNode((SingleConditionNode)node.ViewModel.Node);
|
||||||
|
|||||||
@@ -14,22 +14,17 @@
|
|||||||
<DockPanel>
|
<DockPanel>
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<!--<RowDefinition Height="*"/>-->
|
|
||||||
<RowDefinition Height="*"/>
|
<RowDefinition Height="*"/>
|
||||||
<RowDefinition Height="*"/>
|
<RowDefinition Height="*"/>
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="*" />
|
<ColumnDefinition Width="*" />
|
||||||
<!--<ColumnDefinition Width="*" />-->
|
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<!--<GroupBox Grid.Row="0" Header="条件" Margin="5">
|
<GroupBox x:Name="ActionNodes" Grid.Row="0" Header="动作" Margin="5">
|
||||||
<ListBox x:Name="ConditionsListBox" Background="#A8D8EA"/>
|
|
||||||
</GroupBox>-->
|
|
||||||
<GroupBox Grid.Row="0" Header="动作" Margin="5">
|
|
||||||
<ListBox x:Name="ActionsListBox" Background="#D0F1F9"/>
|
<ListBox x:Name="ActionsListBox" Background="#D0F1F9"/>
|
||||||
</GroupBox>
|
</GroupBox>
|
||||||
<GroupBox Grid.Row="1" Header="触发器" Margin="5">
|
<GroupBox x:Name="FlipflopNodes" Grid.Row="1" Header="触发器" Margin="5" >
|
||||||
<ListBox x:Name="FlipflopsListBox" Background="#FACFC1"/>
|
<ListBox x:Name="FlipflopsListBox" Background="#FACFC1"/>
|
||||||
</GroupBox>
|
</GroupBox>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -53,18 +53,18 @@ namespace Serein.WorkBench.Node.View
|
|||||||
/// 向动作面板添加类型的文本块
|
/// 向动作面板添加类型的文本块
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">要添加的类型</param>
|
/// <param name="type">要添加的类型</param>
|
||||||
public void AddAction(MethodDetails md)
|
public void AddAction(MethodDetailsInfo mdInfo)
|
||||||
{
|
{
|
||||||
AddTypeToListBox(md, ActionsListBox);
|
AddTypeToListBox(mdInfo, ActionsListBox);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 向触发器面板添加类型的文本块
|
/// 向触发器面板添加类型的文本块
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">要添加的类型</param>
|
/// <param name="type">要添加的类型</param>
|
||||||
public void AddFlipflop(MethodDetails md)
|
public void AddFlipflop(MethodDetailsInfo mdInfo)
|
||||||
{
|
{
|
||||||
AddTypeToListBox(md, FlipflopsListBox);
|
AddTypeToListBox(mdInfo, FlipflopsListBox);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -72,14 +72,14 @@ namespace Serein.WorkBench.Node.View
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">要添加的类型</param>
|
/// <param name="type">要添加的类型</param>
|
||||||
/// <param name="panel">要添加到的面板</param>
|
/// <param name="panel">要添加到的面板</param>
|
||||||
private void AddTypeToListBox(MethodDetails md, ListBox listBox)
|
private void AddTypeToListBox(MethodDetailsInfo mdInfo, ListBox listBox)
|
||||||
{
|
{
|
||||||
// 创建一个新的 TextBlock 并设置其属性
|
// 创建一个新的 TextBlock 并设置其属性
|
||||||
TextBlock typeText = new TextBlock
|
TextBlock typeText = new TextBlock
|
||||||
{
|
{
|
||||||
Text = $"{md.MethodTips}",
|
Text = $"{mdInfo.MethodTips}",
|
||||||
Margin = new Thickness(10, 2, 0, 0),
|
Margin = new Thickness(10, 2, 0, 0),
|
||||||
Tag = md
|
Tag = mdInfo
|
||||||
};
|
};
|
||||||
// 为 TextBlock 添加鼠标左键按下事件处理程序
|
// 为 TextBlock 添加鼠标左键按下事件处理程序
|
||||||
typeText.MouseLeftButtonDown += TypeText_MouseLeftButtonDown;
|
typeText.MouseLeftButtonDown += TypeText_MouseLeftButtonDown;
|
||||||
@@ -125,17 +125,17 @@ namespace Serein.WorkBench.Node.View
|
|||||||
// 获取触发事件的 TextBlock
|
// 获取触发事件的 TextBlock
|
||||||
|
|
||||||
|
|
||||||
if (sender is TextBlock typeText && typeText.Tag is MethodDetails md)
|
if (sender is TextBlock typeText && typeText.Tag is MethodDetailsInfo mdInfo)
|
||||||
{
|
{
|
||||||
MoveNodeData moveNodeData = new MoveNodeData
|
MoveNodeData moveNodeData = new MoveNodeData
|
||||||
{
|
{
|
||||||
NodeControlType = md.MethodDynamicType switch
|
NodeControlType = mdInfo.NodeType switch
|
||||||
{
|
{
|
||||||
NodeType.Action => NodeControlType.Action,
|
NodeType.Action => NodeControlType.Action,
|
||||||
NodeType.Flipflop => NodeControlType.Flipflop,
|
NodeType.Flipflop => NodeControlType.Flipflop,
|
||||||
_ => NodeControlType.None,
|
_ => NodeControlType.None,
|
||||||
},
|
},
|
||||||
MethodDetails = md,
|
MethodDetailsInfo = mdInfo,
|
||||||
};
|
};
|
||||||
if(moveNodeData.NodeControlType == NodeControlType.None)
|
if(moveNodeData.NodeControlType == NodeControlType.None)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -91,7 +91,11 @@ namespace Serein.WorkBench.Themes
|
|||||||
|
|
||||||
public void ClearObjItem()
|
public void ClearObjItem()
|
||||||
{
|
{
|
||||||
DependenciesListBox.Items.Clear();
|
DependenciesListBox.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
DependenciesListBox.Items.Clear();
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SortLisbox(ListBox listBox)
|
private static void SortLisbox(ListBox listBox)
|
||||||
|
|||||||
30
WorkBench/Themes/WindowDialogInput.xaml
Normal file
30
WorkBench/Themes/WindowDialogInput.xaml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<Window x:Class="Serein.WorkBench.Themes.WindowEnvRemoteLoginView"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:local="clr-namespace:Serein.WorkBench.Themes"
|
||||||
|
Title="登录远程环境" Height="150" Width="200">
|
||||||
|
<Grid Margin="0,10,0,0">
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="auto"/>
|
||||||
|
<RowDefinition Height="auto"/>
|
||||||
|
<RowDefinition Height="auto"/>
|
||||||
|
<RowDefinition Height="auto"/>
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="40"/>
|
||||||
|
<ColumnDefinition Width="auto"/>
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
<TextBlock Grid.Row="0" Grid.Column="0" Text="地址" HorizontalAlignment="Center"></TextBlock>
|
||||||
|
<TextBox x:Name="TextBlockAddres" Grid.Row="0" Grid.Column="1" Text="192.168.1.100"></TextBox>
|
||||||
|
<TextBlock Grid.Row="1" Grid.Column="0" Text="端口" HorizontalAlignment="Center"></TextBlock>
|
||||||
|
<TextBox x:Name="TextBlockPort" Grid.Row="1" Grid.Column="1" Text="7525"></TextBox>
|
||||||
|
<TextBlock Grid.Row="2" Grid.Column="0" Text="密码" HorizontalAlignment="Center"></TextBlock>
|
||||||
|
<TextBox x:Name="TextBlockToken" Grid.Row="2" Grid.Column="1" Text="123456"></TextBox>
|
||||||
|
<StackPanel Grid.Row="3" Grid.Column="1" HorizontalAlignment="Center" Orientation="Horizontal" Margin="4">
|
||||||
|
<Button Content="测试连接" Margin="2" Click="ButtonTestConnect_Client"></Button>
|
||||||
|
<Button Content="登录环境" Margin="2" Click="ButtonTestLoginEnv_Client"></Button>
|
||||||
|
</StackPanel>
|
||||||
|
</Grid>
|
||||||
|
</Window>
|
||||||
68
WorkBench/Themes/WindowDialogInput.xaml.cs
Normal file
68
WorkBench/Themes/WindowDialogInput.xaml.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
using System.Windows.Shapes;
|
||||||
|
|
||||||
|
namespace Serein.WorkBench.Themes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// WindowDialogInput.xaml 的交互逻辑
|
||||||
|
/// </summary>
|
||||||
|
public partial class WindowEnvRemoteLoginView : Window
|
||||||
|
{
|
||||||
|
private Action<string, int, string> ConnectRemoteFlowEnv;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 弹窗输入
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="connectRemoteFlowEnv"></param>
|
||||||
|
public WindowEnvRemoteLoginView(Action<string, int, string> connectRemoteFlowEnv)
|
||||||
|
{
|
||||||
|
WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
||||||
|
InitializeComponent();
|
||||||
|
ConnectRemoteFlowEnv = connectRemoteFlowEnv;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ButtonTestConnect_Client(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
var addres = this.TextBlockAddres.Text;
|
||||||
|
_ = int.TryParse(this.TextBlockPort.Text, out var port);
|
||||||
|
_ = Task.Run(() => {
|
||||||
|
bool success = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TcpClient tcpClient = new TcpClient();
|
||||||
|
var result = tcpClient.BeginConnect(addres, port, null, null);
|
||||||
|
success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!success)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"无法连接远程:{addres}:{port}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ButtonTestLoginEnv_Client(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
var addres = this.TextBlockAddres.Text;
|
||||||
|
_ = int.TryParse(this.TextBlockPort.Text, out var port);
|
||||||
|
var token = this.TextBlockToken.Text;
|
||||||
|
ConnectRemoteFlowEnv?.Invoke(addres, port, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user