mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
重构了底层,方便向Android、Web、Linux进行跨平台迁移
This commit is contained in:
@@ -6,8 +6,9 @@ namespace Serein.Library.Api
|
||||
{
|
||||
public interface IDynamicContext
|
||||
{
|
||||
NodeRunCts NodeRunCts { get; set; }
|
||||
IFlowEnvironment FlowEnvironment { get; }
|
||||
ISereinIoc SereinIoc { get; }
|
||||
NodeRunCts NodeRunCts { get; set; }
|
||||
Task CreateTimingTask(Action action, int time = 100, int count = -1);
|
||||
}
|
||||
}
|
||||
|
||||
13
Library/Api/IDynamicFlowNode.cs
Normal file
13
Library/Api/IDynamicFlowNode.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Api
|
||||
{
|
||||
public interface IDynamicFlowNode
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
206
Library/Api/IFlowEnvironment.cs
Normal file
206
Library/Api/IFlowEnvironment.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Api
|
||||
{
|
||||
|
||||
public class FlowEventArgs : EventArgs
|
||||
{
|
||||
public bool IsSucceed { get; protected set; } = true;
|
||||
public string ErrorTips { get; protected set; } = string.Empty;
|
||||
}
|
||||
|
||||
|
||||
public delegate void FlowRunCompleteHandler(FlowEventArgs eventArgs);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 加载节点
|
||||
/// </summary>
|
||||
public delegate void LoadNodeHandler(LoadNodeEventArgs eventArgs);
|
||||
public class LoadNodeEventArgs : FlowEventArgs
|
||||
{
|
||||
public LoadNodeEventArgs(NodeInfo NodeInfo, MethodDetails MethodDetailss)
|
||||
{
|
||||
this.NodeInfo = NodeInfo;
|
||||
this.MethodDetailss = MethodDetailss;
|
||||
}
|
||||
public NodeInfo NodeInfo { get; protected set; }
|
||||
public MethodDetails MethodDetailss { get; protected set; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 加载DLL
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
public delegate void LoadDLLHandler(LoadDLLEventArgs eventArgs);
|
||||
public class LoadDLLEventArgs : FlowEventArgs
|
||||
{
|
||||
public LoadDLLEventArgs(Assembly Assembly, List<MethodDetails> MethodDetailss)
|
||||
{
|
||||
this.Assembly = Assembly;
|
||||
this.MethodDetailss = MethodDetailss;
|
||||
}
|
||||
public Assembly Assembly { get; protected set; }
|
||||
public List<MethodDetails> MethodDetailss { get; protected set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 运行环境节点连接发生了改变
|
||||
/// </summary>
|
||||
/// <param name="fromNodeGuid"></param>
|
||||
/// <param name="toNodeGuid"></param>
|
||||
/// <param name="connectionType"></param>
|
||||
public delegate void NodeConnectChangeHandler(NodeConnectChangeEventArgs eventArgs);
|
||||
public class NodeConnectChangeEventArgs : FlowEventArgs
|
||||
{
|
||||
public enum ChangeTypeEnum
|
||||
{
|
||||
Create,
|
||||
Remote,
|
||||
}
|
||||
public NodeConnectChangeEventArgs(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType, ChangeTypeEnum changeType)
|
||||
{
|
||||
this.FromNodeGuid = fromNodeGuid;
|
||||
this.ToNodeGuid = toNodeGuid;
|
||||
this.ConnectionType = connectionType;
|
||||
this.ChangeType = changeType;
|
||||
}
|
||||
public string FromNodeGuid { get; protected set; }
|
||||
public string ToNodeGuid { get; protected set; }
|
||||
public ConnectionType ConnectionType { get; protected set; }
|
||||
public ChangeTypeEnum ChangeType { get; protected set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加了节点
|
||||
/// </summary>
|
||||
/// <param name="fromNodeGuid"></param>
|
||||
/// <param name="toNodeGuid"></param>
|
||||
/// <param name="connectionType"></param>
|
||||
public delegate void NodeCreateHandler(NodeCreateEventArgs eventArgs);
|
||||
public class NodeCreateEventArgs : FlowEventArgs
|
||||
{
|
||||
public NodeCreateEventArgs(object nodeModel)
|
||||
{
|
||||
this.NodeModel = nodeModel;
|
||||
}
|
||||
public object NodeModel { get; private set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public delegate void NodeRemoteHandler(NodeRemoteEventArgs eventArgs);
|
||||
public class NodeRemoteEventArgs : FlowEventArgs
|
||||
{
|
||||
public NodeRemoteEventArgs(string nodeGuid)
|
||||
{
|
||||
this.NodeGuid = nodeGuid;
|
||||
}
|
||||
public string NodeGuid { get; private set; }
|
||||
}
|
||||
|
||||
|
||||
public delegate void StartNodeChangeHandler(StartNodeChangeEventArgs eventArgs);
|
||||
|
||||
|
||||
public class StartNodeChangeEventArgs: FlowEventArgs
|
||||
{
|
||||
public StartNodeChangeEventArgs(string oldNodeGuid, string newNodeGuid)
|
||||
{
|
||||
this.OldNodeGuid = oldNodeGuid;
|
||||
this.NewNodeGuid = newNodeGuid; ;
|
||||
}
|
||||
public string OldNodeGuid { get; private set; }
|
||||
public string NewNodeGuid { get; private set; }
|
||||
}
|
||||
|
||||
|
||||
public interface IFlowEnvironment
|
||||
{
|
||||
event FlowRunCompleteHandler OnFlowRunComplete;
|
||||
event LoadNodeHandler OnLoadNode;
|
||||
event LoadDLLHandler OnDllLoad;
|
||||
event NodeConnectChangeHandler OnNodeConnectChange;
|
||||
event NodeCreateHandler OnNodeCreate;
|
||||
event NodeRemoteHandler OnNodeRemote;
|
||||
event StartNodeChangeHandler OnStartNodeChange;
|
||||
|
||||
/// <summary>
|
||||
/// 保存当前项目
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
SereinOutputFileData SaveProject();
|
||||
/// <summary>
|
||||
/// 加载项目文件
|
||||
/// </summary>
|
||||
/// <param name="projectFile"></param>
|
||||
/// <param name="filePath"></param>
|
||||
void LoadProject(SereinOutputFileData projectFile, string filePath);
|
||||
/// <summary>
|
||||
/// 从文件中加载Dll
|
||||
/// </summary>
|
||||
/// <param name="dllPath"></param>
|
||||
void LoadDll(string dllPath);
|
||||
/// <summary>
|
||||
/// 清理加载的DLL(待更改)
|
||||
/// </summary>
|
||||
void ClearAll();
|
||||
/// <summary>
|
||||
/// 获取方法描述
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="md"></param>
|
||||
/// <returns></returns>
|
||||
bool TryGetMethodDetails(string methodName,out MethodDetails md);
|
||||
|
||||
/// <summary>
|
||||
/// 开始运行
|
||||
/// </summary>
|
||||
Task StartAsync();
|
||||
/// <summary>
|
||||
/// 结束运行
|
||||
/// </summary>
|
||||
void Exit();
|
||||
|
||||
/// <summary>
|
||||
/// 设置流程起点节点
|
||||
/// </summary>
|
||||
/// <param name="nodeGuid"></param>
|
||||
void SetStartNode(string nodeGuid);
|
||||
/// <summary>
|
||||
/// 在两个节点之间创建连接关系
|
||||
/// </summary>
|
||||
/// <param name="fromNodeGuid">起始节点Guid</param>
|
||||
/// <param name="toNodeGuid">目标节点Guid</param>
|
||||
/// <param name="connectionType">连接类型</param>
|
||||
void ConnectNode(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType);
|
||||
/// <summary>
|
||||
/// 创建节点/区域/基础控件
|
||||
/// </summary>
|
||||
/// <param name="nodeBase">节点/区域/基础控件</param>
|
||||
/// <param name="methodDetails">节点绑定的方法说明(</param>
|
||||
void CreateNode(NodeControlType nodeBase, MethodDetails methodDetails = null);
|
||||
/// <summary>
|
||||
/// 移除两个节点之间的连接关系
|
||||
/// </summary>
|
||||
/// <param name="fromNodeGuid">起始节点</param>
|
||||
/// <param name="toNodeGuid">目标节点</param>
|
||||
/// <param name="connectionType">连接类型</param>
|
||||
void RemoteConnect(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType);
|
||||
/// <summary>
|
||||
/// 移除节点/区域/基础控件
|
||||
/// </summary>
|
||||
/// <param name="nodeGuid">待移除的节点Guid</param>
|
||||
void RemoteNode(string nodeGuid);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
83
Library/Base/NodeBase.cs
Normal file
83
Library/Base/NodeBase.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Serein.Library.Base
|
||||
{
|
||||
public abstract class NodeBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点类型
|
||||
/// </summary>
|
||||
public abstract NodeControlType ControlType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 方法描述,对应DLL的方法
|
||||
/// </summary>
|
||||
public abstract MethodDetails MethodDetails { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点guid
|
||||
/// </summary>
|
||||
public abstract string Guid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示名称
|
||||
/// </summary>
|
||||
public abstract string DisplayName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否为起点控件
|
||||
/// </summary>
|
||||
public abstract bool IsStart { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 运行时的上一节点
|
||||
/// </summary>
|
||||
public abstract NodeBase PreviousNode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上一节点集合
|
||||
/// </summary>
|
||||
public abstract List<NodeBase> PreviousNodes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 下一节点集合(真分支)
|
||||
/// </summary>
|
||||
public abstract List<NodeBase> SucceedBranch { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 下一节点集合(假分支)
|
||||
/// </summary>
|
||||
public abstract List<NodeBase> FailBranch { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 异常分支
|
||||
/// </summary>
|
||||
public abstract List<NodeBase> ErrorBranch { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上游分支
|
||||
/// </summary>
|
||||
public abstract List<NodeBase> UpstreamBranch { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前执行状态(进入真分支还是假分支,异常分支在异常中确定)
|
||||
/// </summary>
|
||||
public abstract FlowStateType FlowState { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 运行时的异常信息(仅在 FlowState 为 Error 时存在对应值)
|
||||
/// </summary>
|
||||
public abstract Exception RuningException { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前传递数据(执行了节点对应的方法,才会存在值)
|
||||
/// </summary>
|
||||
public abstract object FlowData { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
110
Library/Base/NodeModelBaseData.cs
Normal file
110
Library/Base/NodeModelBaseData.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Serein.Library.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||
/// </summary>
|
||||
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||
{
|
||||
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; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否为起点控件
|
||||
/// </summary>
|
||||
public bool IsStart { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 运行时的上一节点
|
||||
/// </summary>
|
||||
public NodeModelBase PreviousNode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上一节点集合
|
||||
/// </summary>
|
||||
public List<NodeModelBase> PreviousNodes { get; set; } = new List<NodeModelBase>();
|
||||
|
||||
/// <summary>
|
||||
/// 下一节点集合(真分支)
|
||||
/// </summary>
|
||||
public List<NodeModelBase> SucceedBranch { get; set; } = new List<NodeModelBase>();
|
||||
|
||||
/// <summary>
|
||||
/// 下一节点集合(假分支)
|
||||
/// </summary>
|
||||
public List<NodeModelBase> FailBranch { get; set; } = new List<NodeModelBase>();
|
||||
|
||||
/// <summary>
|
||||
/// 异常分支
|
||||
/// </summary>
|
||||
public List<NodeModelBase> ErrorBranch { get; set; } = new List<NodeModelBase>();
|
||||
|
||||
/// <summary>
|
||||
/// 上游分支
|
||||
/// </summary>
|
||||
public List<NodeModelBase> UpstreamBranch { get; set; } = new List<NodeModelBase>();
|
||||
|
||||
/// <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;
|
||||
|
||||
public abstract Parameterdata[] GetParameterdatas();
|
||||
public virtual NodeInfo ToInfo()
|
||||
{
|
||||
if (MethodDetails == null) return null;
|
||||
|
||||
var trueNodes = SucceedBranch.Select(item => item.Guid); // 真分支
|
||||
var falseNodes = FailBranch.Select(item => item.Guid);// 假分支
|
||||
var upstreamNodes = UpstreamBranch.Select(item => item.Guid);// 上游分支
|
||||
var errorNodes = ErrorBranch.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(),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
480
Library/Base/NodeModelBaseFunc.cs
Normal file
480
Library/Base/NodeModelBaseFunc.cs
Normal file
@@ -0,0 +1,480 @@
|
||||
using Newtonsoft.Json;
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||
/// </summary>
|
||||
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行节点对应的方法
|
||||
/// </summary>
|
||||
/// <param name="context">流程上下文</param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
public virtual object Execute(IDynamicContext context)
|
||||
{
|
||||
MethodDetails md = MethodDetails;
|
||||
object result = null;
|
||||
var del = md.MethodDelegate;
|
||||
try
|
||||
{
|
||||
if (md.ExplicitDatas.Length == 0)
|
||||
{
|
||||
if (md.ReturnType == typeof(void))
|
||||
{
|
||||
((Action<object>)del).Invoke(md.ActingInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = ((Func<object, object>)del).Invoke(md.ActingInstance);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
object[] parameters = GetParameters(context, MethodDetails);
|
||||
if (md.ReturnType == typeof(void))
|
||||
{
|
||||
((Action<object, object[]>)del).Invoke(md.ActingInstance, parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = ((Func<object, object[], object>)del).Invoke(md.ActingInstance, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FlowState = FlowStateType.Error;
|
||||
RuningException = ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行等待触发器的方法
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
/// <exception cref="RuningException"></exception>
|
||||
public virtual async Task<object> ExecuteAsync(IDynamicContext context)
|
||||
{
|
||||
MethodDetails md = MethodDetails;
|
||||
object result = null;
|
||||
|
||||
IFlipflopContext flipflopContext = null;
|
||||
try
|
||||
{
|
||||
// 调用委托并获取结果
|
||||
if (md.ExplicitDatas.Length == 0)
|
||||
{
|
||||
flipflopContext = await ((Func<object, Task<IFlipflopContext>>)md.MethodDelegate).Invoke(MethodDetails.ActingInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
object[] parameters = GetParameters(context, MethodDetails);
|
||||
flipflopContext = await ((Func<object, object[], Task<IFlipflopContext>>)md.MethodDelegate).Invoke(MethodDetails.ActingInstance, parameters);
|
||||
}
|
||||
|
||||
if (flipflopContext != null)
|
||||
{
|
||||
FlowState = flipflopContext.State;
|
||||
if (flipflopContext.State == FlowStateType.Succeed)
|
||||
{
|
||||
result = flipflopContext.Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FlowState = FlowStateType.Error;
|
||||
RuningException = ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始执行
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public async Task StartExecution(IDynamicContext context)
|
||||
{
|
||||
var cts = context.SereinIoc.GetOrInstantiate<CancellationTokenSource>();
|
||||
|
||||
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
|
||||
stack.Push(this);
|
||||
|
||||
while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环
|
||||
{
|
||||
// 从栈中弹出一个节点作为当前节点进行处理
|
||||
var currentNode = stack.Pop();
|
||||
|
||||
// 设置方法执行的对象
|
||||
if (currentNode.MethodDetails != null)
|
||||
{
|
||||
if(currentNode.MethodDetails.ActingInstance == null)
|
||||
{
|
||||
currentNode.MethodDetails.ActingInstance = context.SereinIoc.GetOrInstantiate(MethodDetails.ActingInstanceType);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 获取上游分支,首先执行一次
|
||||
var upstreamNodes = currentNode.UpstreamBranch;
|
||||
for (int i = upstreamNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
upstreamNodes[i].PreviousNode = currentNode;
|
||||
await upstreamNodes[i].StartExecution(context);
|
||||
}
|
||||
|
||||
if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == NodeType.Flipflop)
|
||||
{
|
||||
// 触发器节点
|
||||
currentNode.FlowData = await currentNode.ExecuteAsync(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 动作节点
|
||||
currentNode.FlowData = currentNode.Execute(context);
|
||||
}
|
||||
|
||||
List<NodeModelBase> nextNodes = null ;
|
||||
switch (currentNode.FlowState)
|
||||
{
|
||||
case FlowStateType.Succeed:
|
||||
nextNodes = currentNode.SucceedBranch;
|
||||
break;
|
||||
case FlowStateType.Fail :
|
||||
nextNodes = currentNode.FailBranch;
|
||||
break;
|
||||
case FlowStateType.Error :
|
||||
nextNodes = currentNode.ErrorBranch;
|
||||
break;
|
||||
}
|
||||
if(nextNodes != null)
|
||||
{
|
||||
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
nextNodes[i].PreviousNode = currentNode;
|
||||
stack.Push(nextNodes[i]);
|
||||
}
|
||||
}
|
||||
/*var nextNodes = currentNode.FlowState switch
|
||||
{
|
||||
FlowStateType.Succeed => currentNode.SucceedBranch,
|
||||
FlowStateType.Fail => currentNode.FailBranch,
|
||||
FlowStateType.Error => currentNode.ErrorBranch,
|
||||
_ => throw new Exception("非预期的枚举值")
|
||||
};*/
|
||||
|
||||
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对应的参数数组
|
||||
/// </summary>
|
||||
public object[] GetParameters(IDynamicContext context, MethodDetails md)
|
||||
{
|
||||
// 用正确的大小初始化参数数组
|
||||
var types = md.ExplicitDatas.Select(it => it.DataType).ToArray();
|
||||
if (types.Length == 0)
|
||||
{
|
||||
return new object[] { md.ActingInstance };
|
||||
}
|
||||
|
||||
object[] parameters = new object[types.Length];
|
||||
|
||||
for (int i = 0; i < types.Length; i++)
|
||||
{
|
||||
|
||||
var mdEd = md.ExplicitDatas[i];
|
||||
Type type = mdEd.DataType;
|
||||
|
||||
var f1 = PreviousNode?.FlowData?.GetType();
|
||||
var f2 = mdEd.DataType;
|
||||
if (type == typeof(IDynamicContext))
|
||||
{
|
||||
parameters[i] = context;
|
||||
}
|
||||
else if (type == typeof(MethodDetails))
|
||||
{
|
||||
parameters[i] = md;
|
||||
}
|
||||
else if (type == typeof(NodeModelBase))
|
||||
{
|
||||
parameters[i] = this;
|
||||
}
|
||||
else if (mdEd.IsExplicitData) // 显式参数
|
||||
{
|
||||
// 判断是否使用表达式解析
|
||||
if (mdEd.DataValue[0] == '@')
|
||||
{
|
||||
var expResult = SerinExpressionEvaluator.Evaluate(mdEd.DataValue, PreviousNode?.FlowData, out bool isChange);
|
||||
|
||||
|
||||
if (mdEd.DataType.IsEnum)
|
||||
{
|
||||
var enumValue = Enum.Parse(mdEd.DataType, mdEd.DataValue);
|
||||
parameters[i] = enumValue;
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(string))
|
||||
{
|
||||
parameters[i] = Convert.ChangeType(expResult, typeof(string));
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(bool))
|
||||
{
|
||||
parameters[i] = Convert.ChangeType(expResult, typeof(bool));
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(int))
|
||||
{
|
||||
parameters[i] = Convert.ChangeType(expResult, typeof(int));
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(double))
|
||||
{
|
||||
parameters[i] = Convert.ChangeType(expResult, typeof(double));
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters[i] = expResult;
|
||||
//parameters[i] = ConvertValue(mdEd.DataValue, mdEd.ExplicitType);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mdEd.DataType.IsEnum)
|
||||
{
|
||||
var enumValue = Enum.Parse(mdEd.DataType, mdEd.DataValue);
|
||||
parameters[i] = enumValue;
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(string))
|
||||
{
|
||||
parameters[i] = mdEd.DataValue;
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(bool))
|
||||
{
|
||||
parameters[i] = bool.Parse(mdEd.DataValue);
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(int))
|
||||
{
|
||||
parameters[i] = int.Parse(mdEd.DataValue);
|
||||
}
|
||||
else if (mdEd.ExplicitType == typeof(double))
|
||||
{
|
||||
parameters[i] = double.Parse(mdEd.DataValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters[i] = "";
|
||||
|
||||
//parameters[i] = ConvertValue(mdEd.DataValue, mdEd.ExplicitType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else if (f1 != null && f2 != null)
|
||||
{
|
||||
if (f2.IsAssignableFrom(f1) || f2.FullName.Equals(f1.FullName))
|
||||
{
|
||||
parameters[i] = PreviousNode?.FlowData;
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
var tmpParameter = PreviousNode?.FlowData?.ToString();
|
||||
if (mdEd.DataType.IsEnum)
|
||||
{
|
||||
|
||||
var enumValue = Enum.Parse(mdEd.DataType, tmpParameter);
|
||||
|
||||
parameters[i] = enumValue;
|
||||
}
|
||||
else if (mdEd.DataType == typeof(string))
|
||||
{
|
||||
|
||||
parameters[i] = tmpParameter;
|
||||
|
||||
}
|
||||
else if (mdEd.DataType == typeof(bool))
|
||||
{
|
||||
|
||||
parameters[i] = bool.Parse(tmpParameter);
|
||||
|
||||
}
|
||||
else if (mdEd.DataType == typeof(int))
|
||||
{
|
||||
|
||||
parameters[i] = int.Parse(tmpParameter);
|
||||
|
||||
}
|
||||
else if (mdEd.DataType == typeof(double))
|
||||
{
|
||||
|
||||
parameters[i] = double.Parse(tmpParameter);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tmpParameter != null && mdEd.DataType != null)
|
||||
{
|
||||
|
||||
parameters[i] = ConvertValue(tmpParameter, mdEd.DataType);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// json文本反序列化为对象
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="targetType"></param>
|
||||
/// <returns></returns>
|
||||
private dynamic ConvertValue(string value, Type targetType)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
return JsonConvert.DeserializeObject(value, targetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (JsonReaderException ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
return value;
|
||||
}
|
||||
catch (JsonSerializationException ex)
|
||||
{
|
||||
// 如果无法转为对应的JSON对象
|
||||
int startIndex = ex.Message.IndexOf("to type '") + "to type '".Length; // 查找类型信息开始的索引
|
||||
int endIndex = ex.Message.IndexOf('\''); // 查找类型信息结束的索引
|
||||
var typeInfo = ex.Message.Substring(startIndex,endIndex); // 提取出错类型信息,该怎么传出去?
|
||||
Console.WriteLine("无法转为对应的JSON对象:" + typeInfo);
|
||||
return null;
|
||||
}
|
||||
catch // (Exception ex)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#region 完整的ExecuteAsync调用方法(不要删除)
|
||||
//public virtual async Task<object?> ExecuteAsync(DynamicContext context)
|
||||
//{
|
||||
// MethodDetails md = MethodDetails;
|
||||
// object? result = null;
|
||||
// if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
|
||||
// {
|
||||
// if (md.ExplicitDatas.Length == 0)
|
||||
// {
|
||||
// if (md.ReturnType == typeof(void))
|
||||
// {
|
||||
// ((Action<object>)del).Invoke(md.ActingInstance);
|
||||
// }
|
||||
// else if (md.ReturnType == typeof(Task<FlipflopContext>))
|
||||
// {
|
||||
// // 调用委托并获取结果
|
||||
// FlipflopContext flipflopContext = await ((Func<object, Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance);
|
||||
|
||||
// if (flipflopContext != null)
|
||||
// {
|
||||
// if (flipflopContext.State == FfState.Cancel)
|
||||
// {
|
||||
// throw new Exception("this async task is cancel.");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (flipflopContext.State == FfState.Succeed)
|
||||
// {
|
||||
// CurrentState = true;
|
||||
// result = flipflopContext.Data;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// CurrentState = false;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// result = ((Func<object, object>)del).Invoke(md.ActingInstance);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// object?[]? parameters = GetParameters(context, MethodDetails);
|
||||
// if (md.ReturnType == typeof(void))
|
||||
// {
|
||||
// ((Action<object, object[]>)del).Invoke(md.ActingInstance, parameters);
|
||||
// }
|
||||
// else if (md.ReturnType == typeof(Task<FlipflopContext>))
|
||||
// {
|
||||
// // 调用委托并获取结果
|
||||
// FlipflopContext flipflopContext = await ((Func<object, object[], Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance, parameters);
|
||||
|
||||
// if (flipflopContext != null)
|
||||
// {
|
||||
// if (flipflopContext.State == FfState.Cancel)
|
||||
// {
|
||||
// throw new Exception("取消此异步");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// CurrentState = flipflopContext.State == FfState.Succeed;
|
||||
// result = flipflopContext.Data;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// result = ((Func<object, object[], object>)del).Invoke(md.ActingInstance, parameters);
|
||||
// }
|
||||
// }
|
||||
// context.SetFlowData(result);
|
||||
// }
|
||||
// return result;
|
||||
//}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
69
Library/Entity/ExplicitData.cs
Normal file
69
Library/Entity/ExplicitData.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Serein.Library.Entity
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
public class ExplicitData
|
||||
{
|
||||
/// <summary>
|
||||
/// 索引
|
||||
/// </summary>
|
||||
public int Index { get; set; }
|
||||
/// <summary>
|
||||
/// 是否为显式参数
|
||||
/// </summary>
|
||||
public bool IsExplicitData { get; set; }
|
||||
/// <summary>
|
||||
/// 显式类型
|
||||
/// </summary>
|
||||
public Type ExplicitType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示类型编号>
|
||||
/// </summary>
|
||||
public string ExplicitTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 方法需要的类型
|
||||
/// </summary>
|
||||
public Type DataType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 方法入参参数名称
|
||||
/// </summary>
|
||||
public string ParameterName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 入参值(在UI上输入的文本内容)
|
||||
/// </summary>
|
||||
|
||||
public string DataValue { get; set; }
|
||||
|
||||
|
||||
|
||||
public string[] Items { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
public ExplicitData Clone() => new ExplicitData()
|
||||
{
|
||||
Index = Index,
|
||||
IsExplicitData = IsExplicitData,
|
||||
ExplicitType = ExplicitType,
|
||||
DataType = DataType,
|
||||
ParameterName = ParameterName,
|
||||
ExplicitTypeName = ExplicitTypeName,
|
||||
DataValue = string.IsNullOrEmpty(DataValue) ? string.Empty : DataValue,
|
||||
Items = Items.Select(it => it).ToArray(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
183
Library/Entity/MethodDetails.cs
Normal file
183
Library/Entity/MethodDetails.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Enums;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Serein.Library.Entity
|
||||
{
|
||||
|
||||
|
||||
|
||||
public class MethodDetails
|
||||
{
|
||||
/// <summary>
|
||||
/// 拷贝
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public MethodDetails Clone()
|
||||
{
|
||||
return new MethodDetails
|
||||
{
|
||||
ActingInstance = ActingInstance,
|
||||
ActingInstanceType = ActingInstanceType,
|
||||
MethodDelegate = MethodDelegate,
|
||||
MethodDynamicType = MethodDynamicType,
|
||||
MethodGuid = Guid.NewGuid().ToString(),
|
||||
MethodTips = MethodTips,
|
||||
ReturnType = ReturnType,
|
||||
MethodName = MethodName,
|
||||
MethodLockName = MethodLockName,
|
||||
IsNetFramework = IsNetFramework,
|
||||
ExplicitDatas = ExplicitDatas.Select(it => it.Clone()).ToArray(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 作用实例
|
||||
/// </summary>
|
||||
|
||||
public Type ActingInstanceType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 作用实例
|
||||
/// </summary>
|
||||
|
||||
public object ActingInstance { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 方法GUID
|
||||
/// </summary>
|
||||
|
||||
public string MethodGuid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 方法名称
|
||||
/// </summary>
|
||||
|
||||
public string MethodName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 方法委托
|
||||
/// </summary>
|
||||
|
||||
public Delegate MethodDelegate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点类型
|
||||
/// </summary>
|
||||
public NodeType MethodDynamicType { get; set; }
|
||||
/// <summary>
|
||||
/// 锁名称
|
||||
/// </summary>
|
||||
|
||||
public string MethodLockName { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 方法说明
|
||||
/// </summary>
|
||||
|
||||
public string MethodTips { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 参数内容
|
||||
/// </summary>
|
||||
|
||||
public ExplicitData[] ExplicitDatas { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出参类型
|
||||
/// </summary>
|
||||
|
||||
public Type ReturnType { get; set; }
|
||||
|
||||
public bool IsNetFramework { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//public bool IsCanConnect(Type returnType)
|
||||
//{
|
||||
// if (ExplicitDatas.Length == 0)
|
||||
// {
|
||||
// // 目标不需要传参,可以舍弃结果?
|
||||
// return true;
|
||||
// }
|
||||
// var types = ExplicitDatas.Select(it => it.DataType).ToArray();
|
||||
// // 检查返回类型是否是元组类型
|
||||
// if (returnType.IsGenericType && IsValueTuple(returnType))
|
||||
// {
|
||||
|
||||
// return CompareGenericArguments(returnType, types);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// int index = 0;
|
||||
// if (types[index] == typeof(DynamicContext))
|
||||
// {
|
||||
// index++;
|
||||
// if (types.Length == 1)
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// // 被连接节点检查自己需要的参数类型,与发起连接的节点比较返回值类型
|
||||
// if (returnType == types[index])
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 检查元组类型
|
||||
///// </summary>
|
||||
///// <param name="type"></param>
|
||||
///// <returns></returns>
|
||||
//private bool IsValueTuple(Type type)
|
||||
//{
|
||||
// if (!type.IsGenericType) return false;
|
||||
|
||||
// var genericTypeDef = type.GetGenericTypeDefinition();
|
||||
// return genericTypeDef == typeof(ValueTuple<>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,,>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,,,>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,,,,>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,,,,,>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,,,,,,>) ||
|
||||
// genericTypeDef == typeof(ValueTuple<,,,,,,,>);
|
||||
//}
|
||||
|
||||
//private bool CompareGenericArguments(Type returnType, Type[] parameterTypes)
|
||||
//{
|
||||
// var genericArguments = returnType.GetGenericArguments();
|
||||
// var length = parameterTypes.Length;
|
||||
|
||||
// for (int i = 0; i < genericArguments.Length; i++)
|
||||
// {
|
||||
// if (i >= length) return false;
|
||||
|
||||
// if (IsValueTuple(genericArguments[i]))
|
||||
// {
|
||||
// // 如果当前参数也是 ValueTuple,递归检查嵌套的泛型参数
|
||||
// if (!CompareGenericArguments(genericArguments[i], parameterTypes.Skip(i).ToArray()))
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// else if (genericArguments[i] != parameterTypes[i])
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return true;
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
202
Library/Entity/SereinOutputFileData.cs
Normal file
202
Library/Entity/SereinOutputFileData.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using Serein.Library.Api;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Entity
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 输出文件
|
||||
/// </summary>
|
||||
public class SereinOutputFileData
|
||||
{
|
||||
/// <summary>
|
||||
/// 基础
|
||||
/// </summary>
|
||||
|
||||
public Basic Basic { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 依赖的DLL
|
||||
/// </summary>
|
||||
|
||||
public Library[] Librarys { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 起始节点GUID
|
||||
/// </summary>
|
||||
|
||||
public string StartNode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点集合
|
||||
/// </summary>
|
||||
|
||||
public NodeInfo[] Nodes { get; set; }
|
||||
|
||||
///// <summary>
|
||||
///// 区域集合
|
||||
///// </summary>
|
||||
|
||||
//public Region[] Regions { get; set; }
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基础
|
||||
/// </summary>
|
||||
public class Basic
|
||||
{
|
||||
/// <summary>
|
||||
/// 画布
|
||||
/// </summary>
|
||||
|
||||
public FlowCanvas canvas { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本
|
||||
/// </summary>
|
||||
|
||||
public string versions { get; set; }
|
||||
|
||||
// 预览位置
|
||||
|
||||
// 缩放比例
|
||||
}
|
||||
/// <summary>
|
||||
/// 画布
|
||||
/// </summary>
|
||||
public class FlowCanvas
|
||||
{
|
||||
/// <summary>
|
||||
/// 宽度
|
||||
/// </summary>
|
||||
public float width { get; set; }
|
||||
/// <summary>
|
||||
/// 高度
|
||||
/// </summary>
|
||||
public float lenght { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DLL
|
||||
/// </summary>
|
||||
public class Library
|
||||
{
|
||||
/// <summary>
|
||||
/// DLL名称
|
||||
/// </summary>
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 路径
|
||||
/// </summary>
|
||||
|
||||
public string Path { get; set; }
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 节点
|
||||
/// </summary>
|
||||
public class NodeInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// GUID
|
||||
/// </summary>
|
||||
|
||||
public string Guid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
|
||||
public string MethodName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示标签
|
||||
/// </summary>
|
||||
|
||||
public string Label { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 类型
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 真分支节点GUID
|
||||
/// </summary>
|
||||
|
||||
public string[] TrueNodes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 假分支节点
|
||||
/// </summary>
|
||||
|
||||
public string[] FalseNodes { get; set; }
|
||||
/// <summary>
|
||||
/// 上游分支
|
||||
/// </summary>
|
||||
public string[] UpstreamNodes { get; set; }
|
||||
/// <summary>
|
||||
/// 异常分支
|
||||
/// </summary>
|
||||
public string[] ErrorNodes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
public Parameterdata[] ParameterData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 如果是区域控件,则会存在子项。
|
||||
/// </summary>
|
||||
public NodeInfo[] ChildNodes { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 于画布中的位置
|
||||
/// </summary>
|
||||
|
||||
public Position Position { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否选中
|
||||
/// </summary>
|
||||
public bool IsSelect { get; set; }
|
||||
}
|
||||
|
||||
public class Parameterdata
|
||||
{
|
||||
public bool state { get; set; }
|
||||
public string value { get; set; }
|
||||
public string expression { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 节点于画布中的位置
|
||||
/// </summary>
|
||||
public class Position
|
||||
{
|
||||
public float X { get; set; }
|
||||
public float Y { get; set; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 区域
|
||||
/// </summary>
|
||||
public class Region
|
||||
{
|
||||
public string guid { get; set; }
|
||||
public NodeInfo[] ChildNodes { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
28
Library/Enums/ConnectionType.cs
Normal file
28
Library/Enums/ConnectionType.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Serein.Library.Enums
|
||||
{
|
||||
public enum ConnectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// 真分支
|
||||
/// </summary>
|
||||
IsSucceed,
|
||||
/// <summary>
|
||||
/// 假分支
|
||||
/// </summary>
|
||||
IsFail,
|
||||
/// <summary>
|
||||
/// 异常发生分支
|
||||
/// </summary>
|
||||
IsError,
|
||||
/// <summary>
|
||||
/// 上游分支(执行当前节点前会执行一次上游分支)
|
||||
/// </summary>
|
||||
Upstream,
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -9,6 +9,10 @@ namespace Serein.Library.Enums
|
||||
|
||||
public enum FlowStateType
|
||||
{
|
||||
/// <summary>
|
||||
/// 待执行
|
||||
/// </summary>
|
||||
None,
|
||||
/// <summary>
|
||||
/// 成功(方法成功执行)
|
||||
/// </summary>
|
||||
|
||||
@@ -9,15 +9,15 @@ namespace Serein.Library.Enums
|
||||
public enum NodeType
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// 初始化(事件,不生成节点)
|
||||
/// </summary>
|
||||
Init,
|
||||
/// <summary>
|
||||
/// 开始载入
|
||||
/// 开始载入(事件,不生成节点)
|
||||
/// </summary>
|
||||
Loading,
|
||||
/// <summary>
|
||||
/// 结束
|
||||
/// 结束(事件,不生成节点)
|
||||
/// </summary>
|
||||
Exit,
|
||||
|
||||
@@ -26,13 +26,39 @@ namespace Serein.Library.Enums
|
||||
/// </summary>
|
||||
Flipflop,
|
||||
/// <summary>
|
||||
/// 条件节点
|
||||
/// 条件
|
||||
/// </summary>
|
||||
Condition,
|
||||
/// <summary>
|
||||
/// 动作节点
|
||||
/// 动作
|
||||
/// </summary>
|
||||
Action,
|
||||
}
|
||||
|
||||
|
||||
public enum NodeControlType
|
||||
{
|
||||
None,
|
||||
/// <summary>
|
||||
/// 动作节点
|
||||
/// </summary>
|
||||
Action,
|
||||
/// <summary>
|
||||
/// 触发器节点
|
||||
/// </summary>
|
||||
Flipflop,
|
||||
/// <summary>
|
||||
/// 表达式操作节点
|
||||
/// </summary>
|
||||
ExpOp,
|
||||
/// <summary>
|
||||
/// 表达式操作节点
|
||||
/// </summary>
|
||||
ExpCondition,
|
||||
/// <summary>
|
||||
/// 条件节点区域
|
||||
/// </summary>
|
||||
ConditionRegion,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace Serein.Library.Attributes
|
||||
public bool Scan { get; set; } = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 标记一个方法是什么类型,加载dll后用来拖拽到画布中
|
||||
/// </summary>
|
||||
@@ -40,10 +42,14 @@ namespace Serein.Library.Attributes
|
||||
MethodTips = methodTips;
|
||||
LockName = lockName;
|
||||
}
|
||||
public bool Scan { get; set; }
|
||||
public string MethodTips { get; }
|
||||
public NodeType MethodDynamicType { get; }
|
||||
public string LockName { get; }
|
||||
public bool Scan;
|
||||
public string MethodTips;
|
||||
public NodeType MethodDynamicType;
|
||||
/// <summary>
|
||||
/// 推荐触发器手动设置返回类型
|
||||
/// </summary>
|
||||
public Type ReturnType;
|
||||
public string LockName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,4 +4,24 @@
|
||||
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Utils\SerinExpression\**" />
|
||||
<EmbeddedResource Remove="Utils\SerinExpression\**" />
|
||||
<None Remove="Utils\SerinExpression\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Base\NodeBase.cs" />
|
||||
<Compile Remove="Base\NodeModelBaseData.cs" />
|
||||
<Compile Remove="Base\NodeModelBaseFunc.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Base\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
338
Library/Utils/SerinExpression/ConditionResolver.cs
Normal file
338
Library/Utils/SerinExpression/ConditionResolver.cs
Normal file
@@ -0,0 +1,338 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
{
|
||||
/// <summary>
|
||||
/// 条件解析抽象类
|
||||
/// </summary>
|
||||
public abstract class ConditionResolver
|
||||
{
|
||||
public abstract bool Evaluate(object obj);
|
||||
}
|
||||
|
||||
public class PassConditionResolver : ConditionResolver
|
||||
{
|
||||
public Operator Op { get; set; }
|
||||
public override bool Evaluate(object obj)
|
||||
{
|
||||
return Op switch
|
||||
{
|
||||
Operator.Pass => true,
|
||||
Operator.NotPass => false,
|
||||
_ => throw new NotSupportedException("不支持的条件类型")
|
||||
};
|
||||
}
|
||||
|
||||
public enum Operator
|
||||
{
|
||||
Pass,
|
||||
NotPass,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ValueTypeConditionResolver<T> : ConditionResolver 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)
|
||||
{
|
||||
if (obj is T typedObj)
|
||||
{
|
||||
double numericValue = Convert.ToDouble(typedObj);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public class BoolConditionResolver : ConditionResolver
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public class StringConditionResolver : ConditionResolver
|
||||
{
|
||||
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);
|
||||
}*/
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public class MemberConditionResolver<T> : ConditionResolver 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 override bool Evaluate(object? obj)
|
||||
{
|
||||
//object? memberValue = GetMemberValue(obj, MemberPath);
|
||||
if (TargetObj is T typedObj)
|
||||
{
|
||||
return new ValueTypeConditionResolver<T>
|
||||
{
|
||||
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;
|
||||
//}
|
||||
}
|
||||
|
||||
public class MemberStringConditionResolver : ConditionResolver
|
||||
{
|
||||
|
||||
public string MemberPath { get; set; }
|
||||
|
||||
public StringConditionResolver.Operator Op { get; set; }
|
||||
|
||||
public string Value { get; set; }
|
||||
|
||||
|
||||
public override bool Evaluate(object obj)
|
||||
{
|
||||
object memberValue = GetMemberValue(obj, MemberPath);
|
||||
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('.');
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
342
Library/Utils/SerinExpression/SerinConditionParser.cs
Normal file
342
Library/Utils/SerinExpression/SerinConditionParser.cs
Normal file
@@ -0,0 +1,342 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
{
|
||||
|
||||
public class SerinConditionParser
|
||||
{
|
||||
public static bool To<T>(T data, string expression)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
return ConditionParse(data, expression).Evaluate(data);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static ConditionResolver 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('.');
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// 解析对象表达式
|
||||
/// </summary>
|
||||
private static ConditionResolver ParseObjectExpression(object data, string expression)
|
||||
{
|
||||
var parts = expression.Split(' ');
|
||||
string operatorStr = parts[0];
|
||||
string valueStr = string.Join(' ', parts, 1, parts.Length - 1);
|
||||
|
||||
int typeStartIndex = expression.IndexOf('<');
|
||||
int typeEndIndex = expression.IndexOf('>');
|
||||
|
||||
string memberPath;
|
||||
Type type;
|
||||
object? targetObj;
|
||||
if (typeStartIndex + typeStartIndex == -2)
|
||||
{
|
||||
memberPath = operatorStr;
|
||||
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));
|
||||
}
|
||||
targetObj = GetMemberValue(data, memberPath);
|
||||
|
||||
Type? tempType = typeStr switch
|
||||
{
|
||||
"int" => typeof(int),
|
||||
"double" => typeof(double),
|
||||
"bool" => typeof(bool),
|
||||
"string" => typeof(string),
|
||||
_ => Type.GetType(typeStr)
|
||||
};
|
||||
type = tempType ?? throw new ArgumentException("对象表达式无效的类型声明");
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (type == typeof(int))
|
||||
{
|
||||
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])
|
||||
};
|
||||
}
|
||||
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])
|
||||
};
|
||||
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
return new MemberConditionResolver<bool>
|
||||
{
|
||||
//MemberPath = memberPath,
|
||||
TargetObj = targetObj,
|
||||
Op = (ValueTypeConditionResolver<bool>.Operator)ParseBoolOperator(operatorStr)
|
||||
};
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
return new MemberStringConditionResolver
|
||||
{
|
||||
MemberPath = memberPath,
|
||||
Op = ParseStringOperator(operatorStr),
|
||||
Value = valueStr
|
||||
};
|
||||
}
|
||||
|
||||
throw new NotSupportedException($"Type {type} is not supported.");
|
||||
}
|
||||
|
||||
private static ConditionResolver 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 typeStr = parts[0];
|
||||
string operatorStr = parts[0];
|
||||
string valueStr = string.Join(' ', parts, 1, parts.Length - 1);
|
||||
|
||||
Type type = data.GetType();//Type.GetType(typeStr);
|
||||
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 ValueTypeConditionResolver<int>
|
||||
{
|
||||
Op = op,
|
||||
RangeStart = rangeStart,
|
||||
RangeEnd = rangeEnd,
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0]),
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
int value = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
return new ValueTypeConditionResolver<int>
|
||||
{
|
||||
Op = op,
|
||||
Value = value,
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
double value = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
return new ValueTypeConditionResolver<double>
|
||||
{
|
||||
Op = ParseValueTypeOperator<double>(operatorStr),
|
||||
Value = value,
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||
};
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
bool value = bool.Parse(valueStr);
|
||||
return new BoolConditionResolver
|
||||
{
|
||||
Op = ParseBoolOperator(operatorStr),
|
||||
Value = value,
|
||||
};
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
return new StringConditionResolver
|
||||
{
|
||||
Op = ParseStringOperator(operatorStr),
|
||||
Value = valueStr
|
||||
};
|
||||
}
|
||||
|
||||
throw new NotSupportedException($"Type {type} is not supported.");
|
||||
}
|
||||
|
||||
|
||||
private static ValueTypeConditionResolver<T>.Operator ParseValueTypeOperator<T>(string operatorStr) where T : struct, IComparable<T>
|
||||
{
|
||||
return operatorStr switch
|
||||
{
|
||||
">" => ValueTypeConditionResolver<T>.Operator.GreaterThan,
|
||||
"<" => ValueTypeConditionResolver<T>.Operator.LessThan,
|
||||
"=" => ValueTypeConditionResolver<T>.Operator.Equal,
|
||||
"==" => ValueTypeConditionResolver<T>.Operator.Equal,
|
||||
">=" => ValueTypeConditionResolver<T>.Operator.GreaterThanOrEqual,
|
||||
"≥" => ValueTypeConditionResolver<T>.Operator.GreaterThanOrEqual,
|
||||
"<=" => ValueTypeConditionResolver<T>.Operator.LessThanOrEqual,
|
||||
"≤" => ValueTypeConditionResolver<T>.Operator.LessThanOrEqual,
|
||||
"equals" => ValueTypeConditionResolver<T>.Operator.Equal,
|
||||
"in" => ValueTypeConditionResolver<T>.Operator.InRange,
|
||||
"!in" => ValueTypeConditionResolver<T>.Operator.OutOfRange,
|
||||
_ => throw new ArgumentException($"Invalid operator {operatorStr} for value type.")
|
||||
};
|
||||
}
|
||||
|
||||
private static BoolConditionResolver.Operator ParseBoolOperator(string operatorStr)
|
||||
{
|
||||
return operatorStr switch
|
||||
{
|
||||
"is" => BoolConditionResolver.Operator.Is,
|
||||
"==" => BoolConditionResolver.Operator.Is,
|
||||
"equals" => BoolConditionResolver.Operator.Is,
|
||||
//"isFalse" => BoolConditionNode.Operator.IsFalse,
|
||||
_ => throw new ArgumentException($"Invalid operator {operatorStr} for bool type.")
|
||||
};
|
||||
}
|
||||
|
||||
private static StringConditionResolver.Operator ParseStringOperator(string operatorStr)
|
||||
{
|
||||
return operatorStr switch
|
||||
{
|
||||
"c" => StringConditionResolver.Operator.Contains,
|
||||
"nc" => StringConditionResolver.Operator.DoesNotContain,
|
||||
"sw" => StringConditionResolver.Operator.StartsWith,
|
||||
"ew" => StringConditionResolver.Operator.EndsWith,
|
||||
|
||||
"contains" => StringConditionResolver.Operator.Contains,
|
||||
"doesNotContain" => StringConditionResolver.Operator.DoesNotContain,
|
||||
"equals" => StringConditionResolver.Operator.Equal,
|
||||
"==" => StringConditionResolver.Operator.Equal,
|
||||
"notEquals" => StringConditionResolver.Operator.NotEqual,
|
||||
"!=" => StringConditionResolver.Operator.NotEqual,
|
||||
"startsWith" => StringConditionResolver.Operator.StartsWith,
|
||||
"endsWith" => StringConditionResolver.Operator.EndsWith,
|
||||
_ => throw new ArgumentException($"Invalid operator {operatorStr} for string type.")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
216
Library/Utils/SerinExpression/SerinExpressionEvaluator.cs
Normal file
216
Library/Utils/SerinExpression/SerinExpressionEvaluator.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
using System.Data;
|
||||
|
||||
namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
{
|
||||
public class SerinArithmeticExpressionEvaluator
|
||||
{
|
||||
private static readonly DataTable table = new DataTable();
|
||||
|
||||
public static double Evaluate(string expression, double inputValue)
|
||||
{
|
||||
// 替换占位符@为输入值
|
||||
expression = expression.Replace("@", inputValue.ToString());
|
||||
try
|
||||
{
|
||||
// 使用 DataTable.Compute 方法计算表达式
|
||||
var result = table.Compute(expression, string.Empty);
|
||||
return Convert.ToDouble(result);
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new ArgumentException("Invalid arithmetic expression.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class SerinExpressionEvaluator
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="expression">表达式</param>
|
||||
/// <param name="targetObJ">操作对象</param>
|
||||
/// <param name="isChange">是否改变了对象(get语法)</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);
|
||||
if (parts.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("Invalid expression format.");
|
||||
}
|
||||
|
||||
var operation = parts[0].ToLower();
|
||||
var operand = parts[1][0] == '.' ? parts[1][1..] : parts[1];
|
||||
|
||||
var result = operation switch
|
||||
{
|
||||
"@num" => ComputedNumber(targetObJ, operand),
|
||||
"@call" => InvokeMethod(targetObJ, operand),
|
||||
"@get" => GetMember(targetObJ, operand),
|
||||
"@set" => SetMember(targetObJ, operand),
|
||||
_ => throw new NotSupportedException($"Operation {operation} is not supported.")
|
||||
};
|
||||
|
||||
isChange = operation switch
|
||||
{
|
||||
"@num" => true,
|
||||
"@call" => true,
|
||||
"@get" => true,
|
||||
"@set" => false,
|
||||
_ => throw new NotSupportedException($"Operation {operation} is not supported.")
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private static readonly char[] separator = ['(', ')'];
|
||||
private static readonly char[] separatorArray = [','];
|
||||
|
||||
private static object InvokeMethod(object target, string methodCall)
|
||||
{
|
||||
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);
|
||||
if (method == null)
|
||||
{
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
private static object GetMember(object target, string memberPath)
|
||||
{
|
||||
var members = memberPath.Split('.');
|
||||
foreach (var member in members)
|
||||
{
|
||||
|
||||
if (target == null) return null;
|
||||
|
||||
|
||||
var property = target.GetType().GetProperty(member);
|
||||
if (property != null)
|
||||
{
|
||||
|
||||
target = property.GetValue(target);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var field = target.GetType().GetField(member);
|
||||
if (field != null)
|
||||
{
|
||||
|
||||
target = field.GetValue(target);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Member {member} not found on target.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return target;
|
||||
|
||||
}
|
||||
|
||||
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 property = target.GetType().GetProperty(member);
|
||||
|
||||
if (property != null)
|
||||
{
|
||||
|
||||
target = property.GetValue(target);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var field = target.GetType().GetField(member);
|
||||
if (field != null)
|
||||
{
|
||||
|
||||
target = field.GetValue(target);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Member {member} not found on target.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var lastMember = members.Last();
|
||||
|
||||
var lastProperty = target.GetType().GetProperty(lastMember);
|
||||
|
||||
if (lastProperty != null)
|
||||
{
|
||||
var convertedValue = Convert.ChangeType(value, lastProperty.PropertyType);
|
||||
lastProperty.SetValue(target, convertedValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
var lastField = target.GetType().GetField(lastMember);
|
||||
if (lastField != null)
|
||||
{
|
||||
var convertedValue = Convert.ChangeType(value, lastField.FieldType);
|
||||
lastField.SetValue(target, convertedValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Member {lastMember} not found on target.");
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
private static double ComputedNumber(object value, string expression)
|
||||
{
|
||||
double numericValue = Convert.ToDouble(value);
|
||||
if (!string.IsNullOrEmpty(expression))
|
||||
{
|
||||
numericValue = SerinArithmeticExpressionEvaluator.Evaluate(expression, numericValue);
|
||||
}
|
||||
|
||||
return numericValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Library/Utils/SerinExpressionEvaluator.cs
Normal file
9
Library/Utils/SerinExpressionEvaluator.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
//public abstract class SerinExpressionEvaluator
|
||||
//{
|
||||
// public abstract string Evaluate(string expression ,object obj , out bool isChange);
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user