mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-20 08:16:34 +08:00
修改了IOC的实现,删除了项目排除的文件
This commit is contained in:
@@ -3,7 +3,7 @@ using Serein.Library.Api;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.Library.Ex;
|
||||
using Serein.NodeFlow.Tool.SerinExpression;
|
||||
using Serein.NodeFlow.Tool.SereinExpression;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
public enum ConnectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// 真分支
|
||||
/// </summary>
|
||||
IsSucceed,
|
||||
/// <summary>
|
||||
/// 假分支
|
||||
/// </summary>
|
||||
IsFail,
|
||||
/// <summary>
|
||||
/// 异常发生分支
|
||||
/// </summary>
|
||||
IsError,
|
||||
/// <summary>
|
||||
/// 上游分支(执行当前节点前会执行一次上游分支)
|
||||
/// </summary>
|
||||
Upstream,
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
|
||||
using Serein.NodeFlow.Model;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Collections.Specialized.BitVector32;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
|
||||
//public enum FfState
|
||||
//{
|
||||
// Succeed,
|
||||
// Cancel,
|
||||
// Error,
|
||||
//}
|
||||
|
||||
//public class FlipflopContext
|
||||
//{
|
||||
// public FlowStateType State { get; set; }
|
||||
// public object? Data { get; set; }
|
||||
// public FlipflopContext(FlowStateType ffState, object? data = null)
|
||||
// {
|
||||
// State = ffState;
|
||||
// Data = data;
|
||||
// }
|
||||
//}
|
||||
|
||||
public static class FlipflopFunc
|
||||
{
|
||||
/// <summary>
|
||||
/// 传入触发器方法的返回类型,尝试获取Task[Flipflop[]] 中的泛型类型
|
||||
/// </summary>
|
||||
//public static Type GetFlipflopInnerType(Type type)
|
||||
//{
|
||||
// // 检查是否为泛型类型且为 Task<>
|
||||
// if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
|
||||
// {
|
||||
// // 获取 Task<> 的泛型参数类型,即 Flipflop<>
|
||||
// var innerType = type.GetGenericArguments()[0];
|
||||
|
||||
// // 检查泛型参数是否为 Flipflop<>
|
||||
// if (innerType.IsGenericType && innerType.GetGenericTypeDefinition() == typeof(FlipflopContext<>))
|
||||
// {
|
||||
// // 获取 Flipflop<> 的泛型参数类型,即 T
|
||||
// var flipflopInnerType = innerType.GetGenericArguments()[0];
|
||||
|
||||
// // 返回 Flipflop<> 中的具体类型
|
||||
// return flipflopInnerType;
|
||||
// }
|
||||
// }
|
||||
// // 如果不符合条件,返回 null
|
||||
// return null;
|
||||
//}
|
||||
|
||||
public static bool IsTaskOfFlipflop(Type type)
|
||||
{
|
||||
// 检查是否为泛型类型且为 Task<>
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
|
||||
{
|
||||
// 获取 Task<> 的泛型参数类型
|
||||
var innerType = type.GetGenericArguments()[0];
|
||||
|
||||
// 检查泛型参数是否为 Flipflop<>
|
||||
if (innerType == typeof(FlipflopContext))
|
||||
//if (innerType.IsGenericType && innerType.GetGenericTypeDefinition() == typeof(FlipflopContext<>))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 触发器上下文
|
||||
/// </summary>
|
||||
public class FlipflopContext//<TResult>
|
||||
{
|
||||
public LibraryCore.NodeFlow.FlowStateType State { get; set; }
|
||||
//public TResult? Data { get; set; }
|
||||
public object? Data { get; set; }
|
||||
public FlipflopContext(FlowStateType ffState)
|
||||
{
|
||||
State = ffState;
|
||||
}
|
||||
public FlipflopContext(FlowStateType ffState, object data)
|
||||
{
|
||||
State = ffState;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态流程上下文
|
||||
/// </summary>
|
||||
|
||||
public class DynamicContext(ISereinIoc serviceContainer)
|
||||
|
||||
{
|
||||
|
||||
private readonly string contextGuid = "";//System.Guid.NewGuid().ToString();
|
||||
|
||||
public ISereinIoc ServiceContainer { get; } = serviceContainer;
|
||||
private List<Type> InitServices { get; set; } = [];
|
||||
|
||||
// private ConcurrentDictionary<string, object?> ContextData { get; set; } = [];
|
||||
|
||||
//public void SetFlowData(object data)
|
||||
//{
|
||||
// var threadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
||||
// var name = $"{threadId}.{contextGuid}FlowData";
|
||||
// SetData(name,data);
|
||||
//}
|
||||
//public object GetFlowData(bool IsRetain = false)
|
||||
//{
|
||||
// var threadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
||||
// var name = $"{threadId}.{contextGuid}FlowData";
|
||||
// if (IsRetain)
|
||||
// {
|
||||
// return GetData(name);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return GetAndRemoteData(name);
|
||||
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public void InitService<T>()
|
||||
{
|
||||
InitService(typeof(T));
|
||||
}
|
||||
public void InitService(Type type)
|
||||
{
|
||||
if (!InitServices.Contains(type))
|
||||
{
|
||||
InitServices.Add(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
//throw new Exception("初始化时试图添加已存在的类型:"+type.Name);
|
||||
Console.WriteLine("初始化时试图添加已存在的类型:" + type.Name);
|
||||
}
|
||||
}
|
||||
public void Biuld()
|
||||
{
|
||||
foreach (var item in InitServices)
|
||||
{
|
||||
ServiceContainer.Register(item);
|
||||
}
|
||||
ServiceContainer.Build();
|
||||
}
|
||||
|
||||
//public object? RemoveData(string key)
|
||||
//{
|
||||
// if (ContextData.Remove(key, out var data))
|
||||
// {
|
||||
// return data;
|
||||
// }
|
||||
// return null;
|
||||
//}
|
||||
|
||||
//public void SetData<T>(string key, T value)
|
||||
//{
|
||||
// ContextData[key] = value;
|
||||
//}
|
||||
|
||||
//public T? GetData<T>(string key)
|
||||
//{
|
||||
// if (ContextData.TryGetValue(key, out object? value))
|
||||
// {
|
||||
// if(value == null)
|
||||
// {
|
||||
// return default;
|
||||
// }
|
||||
// if (value.GetType() == typeof(T))
|
||||
// {
|
||||
// return (T)value;
|
||||
// }
|
||||
|
||||
// }
|
||||
// return default;
|
||||
//}
|
||||
|
||||
//public object? GetData(string key)
|
||||
//{
|
||||
// if (ContextData.TryGetValue(key, out object? value))
|
||||
// {
|
||||
// return value;
|
||||
// }
|
||||
// return null;
|
||||
//}
|
||||
|
||||
|
||||
//public ConcurrentDictionary<string,Task> FlipFlopTasks { get; set; } = [];
|
||||
|
||||
public NodeRunTcs NodeRunCts { get; set; }
|
||||
public Task CreateTimingTask(Action action, int time = 100, int count = -1)
|
||||
{
|
||||
NodeRunCts ??= ServiceContainer.GetOrInstantiate<NodeRunTcs>();
|
||||
return Task.Factory.StartNew(async () =>
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
NodeRunCts.Token.ThrowIfCancellationRequested();
|
||||
await Task.Delay(time);
|
||||
action.Invoke();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyExtensions
|
||||
{
|
||||
public static TaskAwaiter GetAwaiter(this int i) => Task.Delay(i).GetAwaiter();
|
||||
}
|
||||
|
||||
|
||||
// if (time <= 0) throw new ArgumentException("时间不能≤0");
|
||||
}
|
||||
@@ -90,7 +90,7 @@ namespace Serein.NodeFlow
|
||||
public List<MethodDetails> MethodDetailss { get; } = [];
|
||||
|
||||
|
||||
public Dictionary<string,NodeModelBase> Nodes { get; } = [];
|
||||
public Dictionary<string, NodeModelBase> Nodes { get; } = [];
|
||||
|
||||
public List<NodeModelBase> Regions { get; } = [];
|
||||
|
||||
@@ -104,13 +104,15 @@ namespace Serein.NodeFlow
|
||||
/// <summary>
|
||||
/// 起始节点
|
||||
/// </summary>
|
||||
public NodeModelBase StartNode {
|
||||
get
|
||||
{
|
||||
public NodeModelBase StartNode
|
||||
{
|
||||
get
|
||||
{
|
||||
return _startNode;
|
||||
}
|
||||
set {
|
||||
if(_startNode is null)
|
||||
set
|
||||
{
|
||||
if (_startNode is null)
|
||||
{
|
||||
value.IsStart = true;
|
||||
_startNode = value;
|
||||
@@ -121,7 +123,8 @@ namespace Serein.NodeFlow
|
||||
value.IsStart = true;
|
||||
_startNode = value;
|
||||
}
|
||||
} }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步运行
|
||||
@@ -165,7 +168,7 @@ namespace Serein.NodeFlow
|
||||
public bool TryGetMethodDetails(string name, out MethodDetails? md)
|
||||
{
|
||||
md = MethodDetailss.FirstOrDefault(it => it.MethodName == name);
|
||||
if(md == null)
|
||||
if (md == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -187,7 +190,7 @@ namespace Serein.NodeFlow
|
||||
foreach (var dll in dllPaths)
|
||||
{
|
||||
var dllFilePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(filePath, dll));
|
||||
(var assembly, var list) = LoadAssembly(dllFilePath);
|
||||
(var assembly, var list) = LoadAssembly(dllFilePath);
|
||||
if (assembly is not null && list.Count > 0)
|
||||
{
|
||||
methodDetailss.AddRange(methodDetailss); // 暂存方法描述
|
||||
@@ -195,7 +198,7 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
}
|
||||
// 方法加载完成,缓存到运行环境中。
|
||||
MethodDetailss.AddRange(methodDetailss);
|
||||
MethodDetailss.AddRange(methodDetailss);
|
||||
methodDetailss.Clear();
|
||||
|
||||
|
||||
@@ -216,7 +219,7 @@ namespace Serein.NodeFlow
|
||||
// 不存在对应的起始节点
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
List<(ConnectionType, string[])> nodeGuids = [(ConnectionType.IsSucceed,nodeInfo.TrueNodes),
|
||||
(ConnectionType.IsFail, nodeInfo.FalseNodes),
|
||||
@@ -256,10 +259,10 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(fromNode is null || toNode is null)
|
||||
if (fromNode is null || toNode is null)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 开始连接
|
||||
ConnectNode(fromNode, toNode, connectionType); // 外部调用连接方法
|
||||
|
||||
@@ -282,7 +285,7 @@ namespace Serein.NodeFlow
|
||||
NodeControlType.ConditionRegion => typeof(CompositeConditionNode),
|
||||
_ => null
|
||||
};
|
||||
if(nodeType == null)
|
||||
if (nodeType == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -305,7 +308,7 @@ namespace Serein.NodeFlow
|
||||
Nodes[nodeBase.Guid] = nodeBase;
|
||||
|
||||
// 如果是触发器,则需要添加到专属集合中
|
||||
if (nodeControlType == NodeControlType.Flipflop && nodeBase is SingleFlipflopNode flipflopNode )
|
||||
if (nodeControlType == NodeControlType.Flipflop && nodeBase is SingleFlipflopNode flipflopNode)
|
||||
{
|
||||
var guid = flipflopNode.Guid;
|
||||
if (!FlipflopNodes.Exists(it => it.Guid.Equals(guid)))
|
||||
@@ -313,7 +316,7 @@ namespace Serein.NodeFlow
|
||||
FlipflopNodes.Add(flipflopNode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 通知UI更改
|
||||
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeBase));
|
||||
// 因为需要UI先布置了元素,才能通知UI变更特效
|
||||
@@ -338,7 +341,7 @@ namespace Serein.NodeFlow
|
||||
MethodDetailss.AddRange(list);
|
||||
OnDllLoad?.Invoke(new LoadDLLEventArgs(assembly, list));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -383,7 +386,7 @@ namespace Serein.NodeFlow
|
||||
NodeConnectChangeEventArgs.ChangeTypeEnum.Remote));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 移除节点
|
||||
/// </summary>
|
||||
/// <param name="nodeGuid"></param>
|
||||
@@ -402,10 +405,10 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 遍历所有父节点,从那些父节点中的子节点集合移除该节点
|
||||
foreach(var pnc in remoteNode.PreviousNodes)
|
||||
foreach (var pnc in remoteNode.PreviousNodes)
|
||||
{
|
||||
var pCType = pnc.Key; // 连接类型
|
||||
for (int i = 0; i < pnc.Value.Count; i++)
|
||||
@@ -446,9 +449,9 @@ namespace Serein.NodeFlow
|
||||
/// <param name="newNodeGuid"></param>
|
||||
public void SetStartNode(string newNodeGuid)
|
||||
{
|
||||
if(Nodes.TryGetValue(newNodeGuid, out NodeModelBase? newStartNodeModel))
|
||||
if (Nodes.TryGetValue(newNodeGuid, out NodeModelBase? newStartNodeModel))
|
||||
{
|
||||
if(newStartNodeModel != null)
|
||||
if (newStartNodeModel != null)
|
||||
{
|
||||
SetStartNode(newStartNodeModel);
|
||||
//var oldNodeGuid = "";
|
||||
@@ -491,7 +494,7 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
// 加载DLL,创建 MethodDetails、实例作用对象、委托方法
|
||||
var itemMethodDetails = MethodDetailsHelperTmp.GetList(item, false);
|
||||
foreach(var md in itemMethodDetails)
|
||||
foreach (var md in itemMethodDetails)
|
||||
{
|
||||
// var instanceType =
|
||||
// Activator.CreateInstance(md.ActingInstanceType);
|
||||
@@ -599,16 +602,7 @@ namespace Serein.NodeFlow
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public static class FlowFunc
|
||||
{
|
||||
public static Library.Entity.Library ToLibrary(this Assembly assembly)
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Serein.NodeFlow
|
||||
Context = new Serein.Library.Core.NodeFlow.DynamicContext(SereinIoc, flowEnvironment);
|
||||
}
|
||||
|
||||
MainCts = SereinIoc.CreateServiceInstance<NodeRunCts>();
|
||||
MainCts = SereinIoc.GetOrCreateServiceInstance<NodeRunCts>();
|
||||
|
||||
foreach (var md in methodDetailss)
|
||||
{
|
||||
|
||||
@@ -1,241 +0,0 @@
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Enums;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
|
||||
|
||||
/// <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>
|
||||
/// 入参值
|
||||
/// </summary>
|
||||
|
||||
public string DataValue { get; set; }
|
||||
|
||||
|
||||
|
||||
public string[] Items { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
public ExplicitData Clone() => new()
|
||||
{
|
||||
Index = Index,
|
||||
IsExplicitData = IsExplicitData,
|
||||
ExplicitType = ExplicitType,
|
||||
DataType = DataType,
|
||||
ParameterName = ParameterName,
|
||||
ExplicitTypeName = ExplicitTypeName,
|
||||
DataValue = string.IsNullOrEmpty(DataValue) ? string.Empty : DataValue,
|
||||
Items = [.. Items],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class MethodDetails : IMethodDetails
|
||||
{
|
||||
/// <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;
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using Serein.Library.Api;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.NodeFlow.Base;
|
||||
using Serein.NodeFlow.Tool.SerinExpression;
|
||||
using Serein.NodeFlow.Tool.SereinExpression;
|
||||
|
||||
namespace Serein.NodeFlow.Model
|
||||
{
|
||||
@@ -42,7 +42,7 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
try
|
||||
{
|
||||
var isPass = SerinConditionParser.To(result, Expression);
|
||||
var isPass = SereinConditionParser.To(result, Expression);
|
||||
NextOrientation = isPass ? ConnectionType.IsSucceed : ConnectionType.IsFail;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.NodeFlow.Base;
|
||||
using Serein.NodeFlow.Tool.SerinExpression;
|
||||
using Serein.NodeFlow.Tool.SereinExpression;
|
||||
using System.Text;
|
||||
|
||||
namespace Serein.NodeFlow.Model
|
||||
|
||||
@@ -1,337 +0,0 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Serein.NodeFlow.SerinExpression
|
||||
{
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,329 +0,0 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Serein.NodeFlow.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('.') /*&& expression.Contains('<') && expression.Contains('>')*/)
|
||||
{
|
||||
return ParseObjectExpression(data, expression);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ParseSimpleExpression(data, expression);
|
||||
}
|
||||
|
||||
|
||||
bool ContainsArithmeticOperators(string expression)
|
||||
{
|
||||
return expression.Contains('+') || expression.Contains('-') || expression.Contains('*') || expression.Contains('/');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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.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.")
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,214 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Serein.NodeFlow.Model.SingleExpOpNode;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace Serein.NodeFlow.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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.NodeFlow.Tool
|
||||
{
|
||||
|
||||
public enum DynamicNodeType
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
Init,
|
||||
/// <summary>
|
||||
/// 开始载入
|
||||
/// </summary>
|
||||
Loading,
|
||||
/// <summary>
|
||||
/// 结束
|
||||
/// </summary>
|
||||
Exit,
|
||||
|
||||
/// <summary>
|
||||
/// 触发器
|
||||
/// </summary>
|
||||
Flipflop,
|
||||
/// <summary>
|
||||
/// 条件节点
|
||||
/// </summary>
|
||||
Condition,
|
||||
/// <summary>
|
||||
/// 动作节点
|
||||
/// </summary>
|
||||
Action,
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用来判断一个类是否需要注册并构建实例(单例模式场景使用)
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class DynamicFlowAttribute(bool scan = true) : Attribute
|
||||
{
|
||||
public bool Scan { get; set; } = scan;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记一个方法是什么类型,加载dll后用来拖拽到画布中
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||||
public class MethodDetailAttribute(DynamicNodeType methodDynamicType,
|
||||
string methodTips = "",
|
||||
bool scan = true,
|
||||
string lockName = "") : Attribute
|
||||
{
|
||||
public bool Scan { get; set; } = scan;
|
||||
public string MethodTips { get; } = methodTips;
|
||||
public DynamicNodeType MethodDynamicType { get; } = methodDynamicType;
|
||||
public string LockName { get; } = lockName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为显式参数
|
||||
/// </summary>
|
||||
//[AttributeUsage(AttributeTargets.Parameter)]
|
||||
//public class ExplicitAttribute : Attribute // where TEnum : Enum
|
||||
//{
|
||||
//}
|
||||
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Printing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.NodeFlow.Tool
|
||||
{
|
||||
|
||||
#region 锁、tsk工具 (已注释)
|
||||
/*public class LockManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, LockQueue> _locks = new ConcurrentDictionary<string, LockQueue>();
|
||||
|
||||
public void CreateLock(string name)
|
||||
{
|
||||
_locks.TryAdd(name, new LockQueue());
|
||||
}
|
||||
|
||||
public async Task AcquireLockAsync(string name, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!_locks.ContainsKey(name))
|
||||
{
|
||||
throw new ArgumentException($"Lock with name '{name}' does not exist.");
|
||||
}
|
||||
|
||||
var lockQueue = _locks[name];
|
||||
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
lock (lockQueue.Queue)
|
||||
{
|
||||
lockQueue.Queue.Enqueue(tcs);
|
||||
if (lockQueue.Queue.Count == 1)
|
||||
{
|
||||
tcs.SetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
await tcs.Task.ConfigureAwait(false);
|
||||
|
||||
// 处理取消操作
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
cancellationToken.Register(() =>
|
||||
{
|
||||
lock (lockQueue.Queue)
|
||||
{
|
||||
if (lockQueue.Queue.Contains(tcs))
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void ReleaseLock(string name)
|
||||
{
|
||||
if (!_locks.ContainsKey(name))
|
||||
{
|
||||
throw new ArgumentException($"Lock with name '{name}' does not exist.");
|
||||
}
|
||||
|
||||
var lockQueue = _locks[name];
|
||||
|
||||
lock (lockQueue.Queue)
|
||||
{
|
||||
if (lockQueue.Queue.Count > 0)
|
||||
{
|
||||
lockQueue.Queue.Dequeue();
|
||||
|
||||
if (lockQueue.Queue.Count > 0)
|
||||
{
|
||||
var next = lockQueue.Queue.Peek();
|
||||
next.SetResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class LockQueue
|
||||
{
|
||||
public Queue<TaskCompletionSource<bool>> Queue { get; } = new Queue<TaskCompletionSource<bool>>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface ITaskResult
|
||||
{
|
||||
object Result { get; }
|
||||
}
|
||||
|
||||
public class TaskResult<T> : ITaskResult
|
||||
{
|
||||
public TaskResult(T result)
|
||||
{
|
||||
Result = result;
|
||||
}
|
||||
|
||||
public T Result { get; }
|
||||
|
||||
object ITaskResult.Result => Result;
|
||||
}
|
||||
|
||||
public class DynamicTasks
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, Task<ITaskResult>> TaskGuidPairs = new();
|
||||
public static Task<ITaskResult> GetTask(string Guid)
|
||||
{
|
||||
TaskGuidPairs.TryGetValue(Guid, out Task<ITaskResult> task);
|
||||
return task;
|
||||
}
|
||||
|
||||
public static bool AddTask<T>(string Guid, T result)
|
||||
{
|
||||
var task = Task.FromResult<ITaskResult>(new TaskResult<T>(result));
|
||||
|
||||
return TaskGuidPairs.TryAdd(Guid, task);
|
||||
}
|
||||
}
|
||||
public class TaskNodeManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, TaskQueue> _taskQueues = new ConcurrentDictionary<string, TaskQueue>();
|
||||
|
||||
public void CreateTaskNode(string name)
|
||||
{
|
||||
_taskQueues.TryAdd(name, new TaskQueue());
|
||||
}
|
||||
|
||||
public async Task WaitForTaskNodeAsync(string name, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!_taskQueues.ContainsKey(name))
|
||||
{
|
||||
throw new ArgumentException($"Task node with name '{name}' does not exist.");
|
||||
}
|
||||
|
||||
var taskQueue = _taskQueues[name];
|
||||
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
lock (taskQueue.Queue)
|
||||
{
|
||||
taskQueue.Queue.Enqueue(tcs);
|
||||
if (taskQueue.Queue.Count == 1)
|
||||
{
|
||||
tcs.SetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
await tcs.Task.ConfigureAwait(false);
|
||||
|
||||
// 处理取消操作
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
cancellationToken.Register(() =>
|
||||
{
|
||||
lock (taskQueue.Queue)
|
||||
{
|
||||
if (taskQueue.Queue.Contains(tcs))
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void CompleteTaskNode(string name)
|
||||
{
|
||||
if (!_taskQueues.ContainsKey(name))
|
||||
{
|
||||
throw new ArgumentException($"Task node with name '{name}' does not exist.");
|
||||
}
|
||||
|
||||
var taskQueue = _taskQueues[name];
|
||||
|
||||
lock (taskQueue.Queue)
|
||||
{
|
||||
if (taskQueue.Queue.Count > 0)
|
||||
{
|
||||
taskQueue.Queue.Dequeue();
|
||||
|
||||
if (taskQueue.Queue.Count > 0)
|
||||
{
|
||||
var next = taskQueue.Queue.Peek();
|
||||
next.SetResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class TaskQueue
|
||||
{
|
||||
public Queue<TaskCompletionSource<bool>> Queue { get; } = new Queue<TaskCompletionSource<bool>>();
|
||||
}
|
||||
}*/
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,460 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.NodeFlow.Tool.SerinExpression;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
|
||||
/// </summary>
|
||||
// public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||
public static class NodeFunc
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行节点对应的方法
|
||||
/// </summary>
|
||||
/// <param name="context">流程上下文</param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
public static object? Execute(this NodeModelBase nodeModelBase, IDynamicContext context)
|
||||
{
|
||||
MethodDetails md = nodeModelBase.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 = nodeModelBase.GetParameters(context, md);
|
||||
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)
|
||||
{
|
||||
nodeModelBase.FlowState = FlowStateType.Error;
|
||||
nodeModelBase.RuningException = ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行等待触发器的方法
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
/// <exception cref="RuningException"></exception>
|
||||
public static async Task<object?> ExecuteAsync(this NodeModelBase nodeModel, IDynamicContext context)
|
||||
{
|
||||
MethodDetails md = nodeModel.MethodDetails;
|
||||
object? result = null;
|
||||
|
||||
IFlipflopContext flipflopContext = null;
|
||||
try
|
||||
{
|
||||
// 调用委托并获取结果
|
||||
if (md.ExplicitDatas.Length == 0)
|
||||
{
|
||||
flipflopContext = await ((Func<object, Task<IFlipflopContext>>)md.MethodDelegate).Invoke(md.ActingInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
object?[]? parameters = nodeModel.GetParameters(context, md);
|
||||
flipflopContext = await ((Func<object, object[], Task<IFlipflopContext>>)md.MethodDelegate).Invoke(md.ActingInstance, parameters);
|
||||
}
|
||||
|
||||
if (flipflopContext != null)
|
||||
{
|
||||
nodeModel.FlowState = flipflopContext.State;
|
||||
if (flipflopContext.State == FlowStateType.Succeed)
|
||||
{
|
||||
result = flipflopContext.Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
nodeModel.FlowState = FlowStateType.Error;
|
||||
nodeModel.RuningException = ex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始执行
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task StartExecution(this NodeModelBase nodeModel, IDynamicContext context)
|
||||
{
|
||||
var cts = context.SereinIoc.GetOrInstantiate<CancellationTokenSource>();
|
||||
|
||||
Stack<NodeModelBase> stack = [];
|
||||
stack.Push(nodeModel);
|
||||
var md = nodeModel.MethodDetails;
|
||||
while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环
|
||||
{
|
||||
// 从栈中弹出一个节点作为当前节点进行处理
|
||||
var currentNode = stack.Pop();
|
||||
|
||||
// 设置方法执行的对象
|
||||
if (currentNode.MethodDetails != null)
|
||||
{
|
||||
currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrInstantiate(md.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);
|
||||
}
|
||||
|
||||
var nextNodes = currentNode.FlowState switch
|
||||
{
|
||||
FlowStateType.Succeed => currentNode.SucceedBranch,
|
||||
FlowStateType.Fail => currentNode.FailBranch,
|
||||
FlowStateType.Error => currentNode.ErrorBranch,
|
||||
_ => throw new Exception("非预期的枚举值")
|
||||
};
|
||||
|
||||
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
nextNodes[i].PreviousNode = currentNode;
|
||||
stack.Push(nextNodes[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对应的参数数组
|
||||
/// </summary>
|
||||
public static object[]? GetParameters(this NodeModelBase nodeModel, IDynamicContext context, MethodDetails md)
|
||||
{
|
||||
// 用正确的大小初始化参数数组
|
||||
var types = md.ExplicitDatas.Select(it => it.DataType).ToArray();
|
||||
if (types.Length == 0)
|
||||
{
|
||||
return [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 = nodeModel.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] = nodeModel;
|
||||
}
|
||||
else if (mdEd.IsExplicitData) // 显式参数
|
||||
{
|
||||
// 判断是否使用表达式解析
|
||||
if (mdEd.DataValue[0] == '@')
|
||||
{
|
||||
var expResult = SerinExpressionEvaluator.Evaluate(mdEd.DataValue, nodeModel.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] = nodeModel.PreviousNode?.FlowData;
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
var tmpParameter = nodeModel.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 static 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[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
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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,53 @@
|
||||
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 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;
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
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 = 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,100 @@
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
using System.Globalization;
|
||||
using Serein.NodeFlow.Tool.SereinExpression.Resolver;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
{
|
||||
|
||||
public class SerinConditionParser
|
||||
public class SereinConditionParser
|
||||
{
|
||||
public static bool To<T>(T data, string expression)
|
||||
{
|
||||
@@ -21,7 +22,7 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
}
|
||||
}
|
||||
|
||||
public static ConditionResolver ConditionParse(object data, string expression)
|
||||
public static SereinConditionResolver ConditionParse(object data, string expression)
|
||||
{
|
||||
if (expression.StartsWith('.')) // 表达式前缀属于从上一个节点数据对象获取成员值
|
||||
{
|
||||
@@ -78,51 +79,57 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解析对象表达式
|
||||
/// </summary>
|
||||
private static ConditionResolver ParseObjectExpression(object data, string expression)
|
||||
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);
|
||||
|
||||
int typeStartIndex = expression.IndexOf('<');
|
||||
int typeEndIndex = expression.IndexOf('>');
|
||||
|
||||
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 = GetMemberValue(data, operatorStr);
|
||||
|
||||
type = targetObj.GetType();
|
||||
|
||||
operatorStr = parts[1].ToLower();
|
||||
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();
|
||||
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));
|
||||
operatorStr = parts[1].ToLower(); // 操作类型
|
||||
valueStr = string.Join(' ', parts.Skip(2)); // 表达式值
|
||||
}
|
||||
else
|
||||
{
|
||||
operatorStr = parts[0].ToLower();
|
||||
valueStr = string.Join(' ', parts.Skip(1));
|
||||
operatorStr = parts[0].ToLower(); // 操作类型
|
||||
valueStr = string.Join(' ', parts.Skip(1)); // 表达式值
|
||||
}
|
||||
targetObj = GetMemberValue(data, memberPath);
|
||||
|
||||
targetObj = GetMemberValue(data, memberPath);// 获取对象成员,作为表达式的目标对象
|
||||
|
||||
Type? tempType = typeStr switch
|
||||
{
|
||||
@@ -135,8 +142,7 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
type = tempType ?? throw new ArgumentException("对象表达式无效的类型声明");
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 解析类型 int
|
||||
if (type == typeof(int))
|
||||
{
|
||||
int value = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
@@ -149,6 +155,8 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
#region 解析类型 double
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
double value = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
@@ -162,6 +170,8 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
};
|
||||
|
||||
}
|
||||
#endregion
|
||||
#region 解析类型 bool
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
return new MemberConditionResolver<bool>
|
||||
@@ -171,6 +181,8 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
Op = (ValueTypeConditionResolver<bool>.Operator)ParseBoolOperator(operatorStr)
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
#region 解析类型 string
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
return new MemberStringConditionResolver
|
||||
@@ -179,12 +191,22 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
Op = ParseStringOperator(operatorStr),
|
||||
Value = valueStr
|
||||
};
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
throw new NotSupportedException($"Type {type} is not supported.");
|
||||
}
|
||||
|
||||
private static ConditionResolver ParseSimpleExpression(object data, string expression)
|
||||
|
||||
/// <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()))
|
||||
{
|
||||
@@ -251,7 +273,6 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
@@ -285,6 +306,14 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <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>
|
||||
{
|
||||
return operatorStr switch
|
||||
@@ -304,6 +333,12 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 布尔操作类型
|
||||
/// </summary>
|
||||
/// <param name="operatorStr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
private static BoolConditionResolver.Operator ParseBoolOperator(string operatorStr)
|
||||
{
|
||||
return operatorStr switch
|
||||
@@ -316,6 +351,12 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串操作类型
|
||||
/// </summary>
|
||||
/// <param name="operatorStr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
private static StringConditionResolver.Operator ParseStringOperator(string operatorStr)
|
||||
{
|
||||
return operatorStr switch
|
||||
@@ -336,6 +377,6 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
_ => throw new ArgumentException($"Invalid operator {operatorStr} for string type.")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
12
NodeFlow/Tool/SereinExpression/SereinConditionResolver.cs
Normal file
12
NodeFlow/Tool/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);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,14 @@
|
||||
using System.Data;
|
||||
|
||||
namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用表达式操作/获取 对象的值
|
||||
/// 获取值 @get .xx.xxx
|
||||
/// 设置值 @set .xx.xxx = [data]
|
||||
/// </summary>
|
||||
/// <param name="obj">操作的对象</param>
|
||||
/// <returns></returns>
|
||||
public class SerinArithmeticExpressionEvaluator
|
||||
{
|
||||
private static readonly DataTable table = new DataTable();
|
||||
@@ -30,7 +37,7 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
/// </summary>
|
||||
/// <param name="expression">表达式</param>
|
||||
/// <param name="targetObJ">操作对象</param>
|
||||
/// <param name="isChange">是否改变了对象(get语法)</param>
|
||||
/// <param name="isChange">是否改变了对象(Set语法)</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
/// <exception cref="NotSupportedException"></exception>
|
||||
@@ -56,11 +63,11 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
|
||||
isChange = operation switch
|
||||
{
|
||||
"@num" => true,
|
||||
/*"@num" => true,
|
||||
"@call" => true,
|
||||
"@get" => true,
|
||||
"@get" => true,*/
|
||||
"@set" => false,
|
||||
_ => throw new NotSupportedException($"Operation {operation} is not supported.")
|
||||
_ => true,
|
||||
};
|
||||
|
||||
return result;
|
||||
@@ -70,6 +77,13 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
private static readonly char[] separator = ['(', ')'];
|
||||
private static readonly char[] separatorArray = [','];
|
||||
|
||||
/// <summary>
|
||||
/// 调用目标方法
|
||||
/// </summary>
|
||||
/// <param name="target">目标实例</param>
|
||||
/// <param name="methodCall">方法名称</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
private static object InvokeMethod(object target, string methodCall)
|
||||
{
|
||||
var methodParts = methodCall.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
||||
@@ -98,7 +112,13 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
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)
|
||||
{
|
||||
var members = memberPath.Split('.');
|
||||
@@ -135,7 +155,13 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
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);
|
||||
@@ -202,6 +228,12 @@ namespace Serein.NodeFlow.Tool.SerinExpression
|
||||
return target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算数学简单表达式
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
private static double ComputedNumber(object value, string expression)
|
||||
{
|
||||
double numericValue = Convert.ToDouble(value);
|
||||
@@ -1,337 +0,0 @@
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Serein.NodeFlow;
|
||||
using Serein.NodeFlow.Model;
|
||||
|
||||
namespace Serein.NodeFlow.Tool
|
||||
{
|
||||
public class TcsSignalException : Exception
|
||||
{
|
||||
public FlowStateType FsState { get; set; }
|
||||
public TcsSignalException(string? message) : base(message)
|
||||
{
|
||||
FsState = FlowStateType.Error;
|
||||
}
|
||||
}
|
||||
|
||||
public class TcsSignal<TSignal> where TSignal : struct, Enum
|
||||
{
|
||||
//public ConcurrentDictionary<TSignal, Queue<TaskCompletionSource<object>>> TcsEvent { get; } = new();
|
||||
public ConcurrentDictionary<TSignal, TaskCompletionSource<object>> TcsEvent { get; } = new();
|
||||
|
||||
public ConcurrentDictionary<TSignal, object> TcsLock { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 触发信号
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="signal">信号</param>
|
||||
/// <param name="value">传递的参数</param>
|
||||
/// <returns>是否成功触发</returns>
|
||||
public bool TriggerSignal<T>(TSignal signal, T value)
|
||||
{
|
||||
var tcsLock = TcsLock.GetOrAdd(signal, new object());
|
||||
lock (tcsLock)
|
||||
{
|
||||
if (TcsEvent.TryRemove(signal, out var waitTcs))
|
||||
{
|
||||
waitTcs.SetResult(value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public TaskCompletionSource<object> CreateTcs(TSignal signal)
|
||||
{
|
||||
var tcsLock = TcsLock.GetOrAdd(signal, new object());
|
||||
lock (tcsLock)
|
||||
{
|
||||
var tcs = TcsEvent.GetOrAdd(signal, new TaskCompletionSource<object>());
|
||||
return tcs;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void CancelTask()
|
||||
{
|
||||
foreach (var tcs in TcsEvent.Values)
|
||||
{
|
||||
tcs.SetException(new TcsSignalException("任务取消"));
|
||||
}
|
||||
TcsEvent.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user