mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-04 07:16:35 +08:00
重写了触发器底层逻辑
This commit is contained in:
@@ -77,7 +77,7 @@ namespace Serein.NodeFlow.Base
|
||||
internal virtual NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||
{
|
||||
this.Guid = nodeInfo.Guid;
|
||||
if(this.MethodDetails is not null)
|
||||
if (this.MethodDetails is not null)
|
||||
{
|
||||
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
|
||||
{
|
||||
@@ -86,7 +86,7 @@ namespace Serein.NodeFlow.Base
|
||||
this.MethodDetails.ExplicitDatas[i].DataValue = pd.Value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -104,9 +104,9 @@ namespace Serein.NodeFlow.Base
|
||||
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
|
||||
stack.Push(this);
|
||||
var flowCts = context.Env.IOC.Get<CancellationTokenSource>(FlowStarter.FlipFlopCtsName);
|
||||
while (stack.Count > 0 ) // 循环中直到栈为空才会退出循环
|
||||
while (stack.Count > 0) // 循环中直到栈为空才会退出循环
|
||||
{
|
||||
if(flowCts is not null)
|
||||
if (flowCts is not null)
|
||||
{
|
||||
if (flowCts.IsCancellationRequested)
|
||||
break;
|
||||
@@ -118,7 +118,7 @@ namespace Serein.NodeFlow.Base
|
||||
|
||||
// 筛选出上游分支
|
||||
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream].Where(
|
||||
node => node.DebugSetting.IsEnable
|
||||
node => node.DebugSetting.IsEnable
|
||||
).ToArray();
|
||||
// 执行上游分支
|
||||
foreach (var upstreamNode in upstreamNodes)
|
||||
@@ -141,10 +141,10 @@ namespace Serein.NodeFlow.Base
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 上游分支执行完成,才执行当前节点
|
||||
object? newFlowData = await currentNode.ExecutingAsync(context);
|
||||
if (flowCts is null || flowCts.IsCancellationRequested || currentNode.NextOrientation == ConnectionType.None)
|
||||
if (flowCts is null || flowCts.IsCancellationRequested || currentNode.NextOrientation == ConnectionType.None)
|
||||
{
|
||||
// 不再执行
|
||||
break;
|
||||
@@ -204,14 +204,11 @@ namespace Serein.NodeFlow.Base
|
||||
md.ActingInstance ??= context.Env.IOC.Get(md.ActingInstanceType);
|
||||
object instance = md.ActingInstance;
|
||||
|
||||
|
||||
|
||||
object? result = null;
|
||||
|
||||
//Console.WriteLine($"(isTask, isTaskHaveResult):{(isTask, isTaskHaveResult)}");
|
||||
try
|
||||
{
|
||||
// Action/Func([方法作用的实例],[可能的参数值],[可能的返回值])
|
||||
|
||||
object?[]? args = GetParameters(context, this, md);
|
||||
result = await dd.Invoke(md.ActingInstance, args);
|
||||
NextOrientation = ConnectionType.IsSucceed;
|
||||
@@ -219,7 +216,7 @@ namespace Serein.NodeFlow.Base
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync($"节点[{this.MethodDetails?.MethodName}]异常:" + ex.Message);
|
||||
await Console.Out.WriteLineAsync($"节点[{this.MethodDetails?.MethodName}]异常:" + ex);
|
||||
NextOrientation = ConnectionType.IsError;
|
||||
RuningException = ex;
|
||||
return null;
|
||||
@@ -268,8 +265,8 @@ namespace Serein.NodeFlow.Base
|
||||
inputParameter = flowData; // 使用上一节点的对象
|
||||
}
|
||||
|
||||
// 存在转换器
|
||||
if (ed.Convertor is not null)
|
||||
// 入参存在取值转换器
|
||||
if (ed.ExplicitType.IsEnum && ed.Convertor is not null)
|
||||
{
|
||||
if (Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum))
|
||||
{
|
||||
@@ -286,96 +283,57 @@ namespace Serein.NodeFlow.Base
|
||||
}
|
||||
}
|
||||
|
||||
if ( ed.DataType != ed.ExplicitType) // 获取枚举转换器中记录的枚举
|
||||
// 入参存在类型转换器,获取枚举转换器中记录的枚举
|
||||
if (ed.ExplicitType.IsEnum && ed.DataType != ed.ExplicitType)
|
||||
{
|
||||
if (ed.ExplicitType.IsEnum && Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum)) // 获取对应的枚举项
|
||||
if (Enum.TryParse(ed.ExplicitType, ed.DataValue, out var resultEnum)) // 获取对应的枚举项
|
||||
{
|
||||
// 获取绑定的类型
|
||||
var type = EnumHelper.GetBoundValue(ed.ExplicitType, resultEnum, attr => attr.Value);
|
||||
if(type is Type enumBindType && enumBindType is not null)
|
||||
if (type is Type enumBindType && enumBindType is not null)
|
||||
{
|
||||
var value = context.Env.IOC.Instantiate(enumBindType);
|
||||
if(value is not null)
|
||||
if (value is not null)
|
||||
{
|
||||
parameters[i] = value;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if (ed.DataType.IsValueType)
|
||||
{
|
||||
if (inputParameter is null)
|
||||
{
|
||||
parameters[i] = Activator.CreateInstance(ed.DataType);
|
||||
}
|
||||
else
|
||||
{
|
||||
string? valueStr = inputParameter?.ToString();
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
{
|
||||
parameters[i] = Activator.CreateInstance(ed.DataType);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters[i] = ed.DataType switch
|
||||
{
|
||||
Type t when t.IsEnum => Enum.Parse(ed.DataType, ed.DataValue),// 需要枚举
|
||||
Type t when t == typeof(char) => char.Parse(valueStr),
|
||||
Type t when t == typeof(bool) => bool.Parse(valueStr),
|
||||
Type t when t == typeof(float) => float.Parse(valueStr),
|
||||
Type t when t == typeof(decimal) => decimal.Parse(valueStr),
|
||||
Type t when t == typeof(double) => double.Parse(valueStr),
|
||||
Type t when t == typeof(sbyte) => sbyte.Parse(valueStr),
|
||||
Type t when t == typeof(byte) => byte.Parse(valueStr),
|
||||
Type t when t == typeof(short) => short.Parse(valueStr),
|
||||
Type t when t == typeof(ushort) => ushort.Parse(valueStr),
|
||||
Type t when t == typeof(int) => int.Parse(valueStr),
|
||||
Type t when t == typeof(uint) => uint.Parse(valueStr),
|
||||
Type t when t == typeof(long) => long.Parse(valueStr),
|
||||
Type t when t == typeof(ulong) => ulong.Parse(valueStr),
|
||||
Type t when t == typeof(nint) => nint.Parse(valueStr),
|
||||
Type t when t == typeof(nuint) => nuint.Parse(valueStr),
|
||||
_ => throw new Exception($"调用节点对应方法[{nodeModel.MethodDetails.MethodName}]时,遇到了未在预期内的值类型入参:{ed.DataType.FullName}"),
|
||||
// Type t when Nullable.GetUnderlyingType(t) != null => inputParameter is null ? null : Convert.ChangeType(inputParameter, Nullable.GetUnderlyingType(t)),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters[i] = ed.DataType switch
|
||||
{
|
||||
Type t when t == typeof(IDynamicContext) => context, // 上下文
|
||||
Type t when t == typeof(string) => inputParameter?.ToString(),
|
||||
|
||||
//Type t when t == typeof(DateTime) => string.IsNullOrEmpty(valueStr) ? 0 : DateTime.Parse(valueStr),
|
||||
|
||||
Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||
Type t when t == typeof(NodeModelBase) => nodeModel, // 节点实体类
|
||||
|
||||
Type t when t.IsArray => (inputParameter as Array)?.Cast<object>().ToList(),
|
||||
Type t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => inputParameter,
|
||||
_ => inputParameter,
|
||||
// Type t when Nullable.GetUnderlyingType(t) != null => inputParameter is null ? null : Convert.ChangeType(inputParameter, Nullable.GetUnderlyingType(t)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex) // 节点参数类型转换异常
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (ed.DataType.IsValueType)
|
||||
{
|
||||
parameters[i] = new object();
|
||||
Console.WriteLine(ex);
|
||||
var valueStr = inputParameter?.ToString();
|
||||
parameters[i] = valueStr.ToValueData(ed.DataType);
|
||||
}
|
||||
else
|
||||
{
|
||||
var valueStr = inputParameter?.ToString();
|
||||
parameters[i] = ed.DataType switch
|
||||
{
|
||||
Type t when t == typeof(string) => valueStr,
|
||||
Type t when t == typeof(IDynamicContext) => context, // 上下文
|
||||
Type t when t == typeof(DateTime) => string.IsNullOrEmpty(valueStr) ? 0 : DateTime.Parse(valueStr),
|
||||
|
||||
Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||
Type t when t == typeof(NodeModelBase) => nodeModel, // 节点实体类
|
||||
|
||||
Type t when t.IsArray => (inputParameter as Array)?.Cast<object>().ToList(),
|
||||
Type t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => inputParameter,
|
||||
_ => inputParameter,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
@@ -384,10 +342,10 @@ namespace Serein.NodeFlow.Base
|
||||
/// 更新节点数据,并检查监视表达式是否生效
|
||||
/// </summary>
|
||||
/// <param name="newData"></param>
|
||||
public static async Task RefreshFlowDataAndExpInterrupt(IDynamicContext context,NodeModelBase nodeModel, object? newData = null)
|
||||
public static async Task RefreshFlowDataAndExpInterrupt(IDynamicContext context, NodeModelBase nodeModel, object? newData = null)
|
||||
{
|
||||
string guid = nodeModel.Guid;
|
||||
if(newData is not null)
|
||||
if (newData is not null)
|
||||
{
|
||||
await MonitorObjExpInterrupt(context, nodeModel, newData, 0); // 首先监视对象
|
||||
await MonitorObjExpInterrupt(context, nodeModel, newData, 1); // 然后监视节点
|
||||
@@ -399,7 +357,7 @@ namespace Serein.NodeFlow.Base
|
||||
{
|
||||
MonitorObjectEventArgs.ObjSourceType sourceType;
|
||||
string? key;
|
||||
if(monitorType == 0)
|
||||
if (monitorType == 0)
|
||||
{
|
||||
key = data?.GetType()?.FullName;
|
||||
sourceType = MonitorObjectEventArgs.ObjSourceType.IOCObj;
|
||||
@@ -444,7 +402,7 @@ namespace Serein.NodeFlow.Base
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 释放对象
|
||||
|
||||
@@ -43,6 +43,23 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
try
|
||||
{
|
||||
var getObjExp = CustomData?.ToString();
|
||||
|
||||
if (IsCustomData && !string.IsNullOrEmpty(getObjExp) && getObjExp.Length >= 4)
|
||||
{
|
||||
|
||||
var ExpOpOption = getObjExp[..4];
|
||||
if(ExpOpOption.ToLower() == "@get")
|
||||
{
|
||||
result = PreviousNode?.GetFlowData();
|
||||
if (result is not null)
|
||||
{
|
||||
result = SerinExpressionEvaluator.Evaluate(getObjExp, result, out _);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
var isPass = SereinConditionParser.To(result, Expression);
|
||||
NextOrientation = isPass ? ConnectionType.IsSucceed : ConnectionType.IsFail;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.Library.Ex;
|
||||
using Serein.Library.NodeFlow.Tool;
|
||||
using Serein.Library.Utils;
|
||||
using Serein.NodeFlow.Base;
|
||||
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||
@@ -42,44 +43,15 @@ namespace Serein.NodeFlow.Model
|
||||
{
|
||||
var args = GetParameters(context, this, md);
|
||||
var result = await dd.Invoke(md.ActingInstance, args);
|
||||
if (result is IFlipflopContext flipflopContext)
|
||||
dynamic flipflopContext = result;
|
||||
FlipflopStateType flipflopStateType = flipflopContext.State;
|
||||
NextOrientation = flipflopStateType.ToContentType();
|
||||
if (flipflopContext.Type == TriggerType.Overtime)
|
||||
{
|
||||
NextOrientation = flipflopContext.State.ToContentType();
|
||||
if (flipflopContext.TriggerData is null || flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime)
|
||||
{
|
||||
throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
|
||||
}
|
||||
return flipflopContext.TriggerData.Value;
|
||||
throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FlipflopException("触发器节点返回了非预期的类型", true, FlipflopException.CancelClass.Flow);
|
||||
}
|
||||
// Task<IFlipflopContext> flipflopTask;
|
||||
//var delType = dd.EmitMethodType;
|
||||
//var del = dd.EmitDelegate;
|
||||
//if (delType == EmitHelper.EmitMethodType.HasResultTask && del is Func<object, object?[]?, Task<object>> hasResultTask)
|
||||
//{
|
||||
// var flipflopTaskObj = await hasResultTask(instance, args);
|
||||
// if(flipflopTaskObj is IFlipflopContext flipflopContext)
|
||||
// {
|
||||
// NextOrientation = flipflopContext.State.ToContentType();
|
||||
// if (flipflopContext.TriggerData is null || flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime)
|
||||
// {
|
||||
// throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
|
||||
// }
|
||||
// return flipflopContext.TriggerData.Value;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new FlipflopException("触发器节点返回了非预期的类型", true, FlipflopException.CancelClass.Flow);
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// throw new FlipflopException("触发器节点构造了非预期的委托", true, FlipflopException.CancelClass.Flow);
|
||||
//}
|
||||
|
||||
return flipflopContext.Value;
|
||||
|
||||
}
|
||||
catch (FlipflopException ex)
|
||||
{
|
||||
@@ -87,14 +59,14 @@ namespace Serein.NodeFlow.Model
|
||||
{
|
||||
throw;
|
||||
}
|
||||
await Console.Out.WriteLineAsync($"触发器[{this.MethodDetails.MethodName}]异常:" + ex.Message);
|
||||
await Console.Out.WriteLineAsync($"触发器[{this.MethodDetails.MethodName}]异常:" + ex);
|
||||
NextOrientation = ConnectionType.None;
|
||||
RuningException = ex;
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync($"触发器[{this.MethodDetails.MethodName}]异常:" + ex.Message);
|
||||
await Console.Out.WriteLineAsync($"触发器[{this.MethodDetails.MethodName}]异常:" + ex);
|
||||
NextOrientation = ConnectionType.IsError;
|
||||
RuningException = ex;
|
||||
return null;
|
||||
@@ -104,7 +76,10 @@ namespace Serein.NodeFlow.Model
|
||||
// flipflopTask?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public static object GetContextValueDynamic(dynamic context)
|
||||
{
|
||||
return context.Value; // dynamic 会在运行时处理类型
|
||||
}
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
if (base.MethodDetails.ExplicitDatas.Length > 0)
|
||||
|
||||
@@ -79,11 +79,31 @@ public static class NodeMethodDetailsHelper
|
||||
|
||||
if (attribute.MethodDynamicType == Library.Enums.NodeType.Flipflop)
|
||||
{
|
||||
returnType = attribute.ReturnType;
|
||||
if (!isTask || taskResult != typeof(IFlipflopContext))
|
||||
if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
|
||||
{
|
||||
Console.WriteLine($"触发器节点的返回类型非预期类型,可能会导致流程异常。[{dllTypeMethodName}]当前返回类型为[{method.ReturnType}],而预期的返回类型应为[Task<IFlipflopContext>]");
|
||||
// 获取 Task<> 的泛型参数类型
|
||||
var innerType = method.ReturnType.GetGenericArguments()[0];
|
||||
if (innerType.IsGenericType && innerType.GetGenericTypeDefinition() == typeof(IFlipflopContext<>))
|
||||
{
|
||||
var flipflopType = innerType.GetGenericArguments()[0];
|
||||
returnType = flipflopType;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[{dllTypeMethodName}]跳过创建,返回类型非预期的Task<IFlipflopContext<TResult>>。");
|
||||
return (null, null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[{dllTypeMethodName}]跳过创建,因为触发器方法的返回值并非Task<>,将无法等待。");
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
//if (!isTask || taskResult != typeof(IFlipflopContext<object>))
|
||||
//{
|
||||
//
|
||||
//}
|
||||
|
||||
}
|
||||
else if(isTask)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -54,46 +55,73 @@ namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||
|
||||
public override bool Evaluate(object obj)
|
||||
{
|
||||
if (obj is T typedObj)
|
||||
|
||||
var evaluatedValue = obj.ToConvert<T>();
|
||||
if (!string.IsNullOrEmpty(ArithmeticExpression))
|
||||
{
|
||||
double numericValue = Convert.ToDouble(typedObj);
|
||||
if (!string.IsNullOrEmpty(ArithmeticExpression))
|
||||
{
|
||||
numericValue = SerinArithmeticExpressionEvaluator.Evaluate(ArithmeticExpression, numericValue);
|
||||
}
|
||||
evaluatedValue = SerinArithmeticExpressionEvaluator<T>.Evaluate(ArithmeticExpression, evaluatedValue);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
switch (Op)
|
||||
{
|
||||
case Operator.GreaterThan:
|
||||
return evaluatedValue.CompareTo(Value) > 0;
|
||||
case Operator.LessThan:
|
||||
return evaluatedValue.CompareTo(Value) < 0;
|
||||
case Operator.Equal:
|
||||
return evaluatedValue.CompareTo(Value) == 0;
|
||||
case Operator.GreaterThanOrEqual:
|
||||
return evaluatedValue.CompareTo(Value) >= 0;
|
||||
case Operator.LessThanOrEqual:
|
||||
return evaluatedValue.CompareTo(Value) <= 0;
|
||||
case Operator.InRange:
|
||||
return evaluatedValue.CompareTo(RangeStart) >= 0 && evaluatedValue.CompareTo(RangeEnd) <= 0;
|
||||
case Operator.OutOfRange:
|
||||
return evaluatedValue.CompareTo(RangeStart) < 0 || evaluatedValue.CompareTo(RangeEnd) > 0;
|
||||
}
|
||||
return false;
|
||||
|
||||
//if (obj is T typedObj)
|
||||
//{
|
||||
// numericValue = Convert.ToDouble(typedObj);
|
||||
// numericValue = Convert.ToDouble(obj);
|
||||
// if (!string.IsNullOrEmpty(ArithmeticExpression))
|
||||
// {
|
||||
// numericValue = SerinArithmeticExpressionEvaluator.Evaluate(ArithmeticExpression, numericValue);
|
||||
// }
|
||||
|
||||
// T evaluatedValue = (T)Convert.ChangeType(numericValue, typeof(T));
|
||||
|
||||
// /*return Op switch
|
||||
// {
|
||||
// Operator.GreaterThan => evaluatedValue.CompareTo(Value) > 0,
|
||||
// Operator.LessThan => evaluatedValue.CompareTo(Value) < 0,
|
||||
// Operator.Equal => evaluatedValue.CompareTo(Value) == 0,
|
||||
// Operator.GreaterThanOrEqual => evaluatedValue.CompareTo(Value) >= 0,
|
||||
// Operator.LessThanOrEqual => evaluatedValue.CompareTo(Value) <= 0,
|
||||
// Operator.InRange => evaluatedValue.CompareTo(RangeStart) >= 0 && evaluatedValue.CompareTo(RangeEnd) <= 0,
|
||||
// Operator.OutOfRange => evaluatedValue.CompareTo(RangeStart) < 0 || evaluatedValue.CompareTo(RangeEnd) > 0,
|
||||
// _ => throw new NotSupportedException("不支持的条件类型")
|
||||
// };*/
|
||||
// switch (Op)
|
||||
// {
|
||||
// case Operator.GreaterThan:
|
||||
// return evaluatedValue.CompareTo(Value) > 0;
|
||||
// case Operator.LessThan:
|
||||
// return evaluatedValue.CompareTo(Value) < 0;
|
||||
// case Operator.Equal:
|
||||
// return evaluatedValue.CompareTo(Value) == 0;
|
||||
// case Operator.GreaterThanOrEqual:
|
||||
// return evaluatedValue.CompareTo(Value) >= 0;
|
||||
// case Operator.LessThanOrEqual:
|
||||
// return evaluatedValue.CompareTo(Value) <= 0;
|
||||
// case Operator.InRange:
|
||||
// return evaluatedValue.CompareTo(RangeStart) >= 0 && evaluatedValue.CompareTo(RangeEnd) <= 0;
|
||||
// case Operator.OutOfRange:
|
||||
// return evaluatedValue.CompareTo(RangeStart) < 0 || evaluatedValue.CompareTo(RangeEnd) > 0;
|
||||
// }
|
||||
//}
|
||||
//return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serein.Library.Utils;
|
||||
using Serein.NodeFlow.Tool.SereinExpression.Resolver;
|
||||
using System.ComponentModel.Design;
|
||||
using System.Globalization;
|
||||
@@ -31,7 +32,7 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
|
||||
public static SereinConditionResolver ConditionParse(object? data, string expression)
|
||||
{
|
||||
if (expression.StartsWith('.') || expression.StartsWith('<')) // 表达式前缀属于从上一个节点数据对象获取成员值
|
||||
if (expression.StartsWith('.')) // 表达式前缀属于从上一个节点数据对象获取成员值
|
||||
{
|
||||
return ParseObjectExpression(data, expression);
|
||||
}
|
||||
@@ -138,11 +139,22 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
}
|
||||
Type? tempType = typeStr switch
|
||||
{
|
||||
"int" => typeof(int),
|
||||
"double" => typeof(double),
|
||||
"bool" => typeof(bool),
|
||||
"float" => typeof(float),
|
||||
"decimal" => typeof(decimal),
|
||||
"double" => typeof(double),
|
||||
"sbyte" => typeof(sbyte),
|
||||
"byte" => typeof(byte),
|
||||
"short" => typeof(short),
|
||||
"ushort" => typeof(ushort),
|
||||
"int" => typeof(int),
|
||||
"uint" => typeof(uint),
|
||||
"long" => typeof(long),
|
||||
"ulong" => typeof(ulong),
|
||||
"nint" => typeof(nint),
|
||||
"nuint" => typeof(nuint),
|
||||
"string" => typeof(string),
|
||||
_ => Type.GetType(typeStr)
|
||||
_ => Type.GetType(typeStr),
|
||||
};
|
||||
type = tempType ?? throw new ArgumentException("对象表达式无效的类型声明");
|
||||
if (string.IsNullOrWhiteSpace(memberPath))
|
||||
@@ -157,6 +169,10 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
}
|
||||
|
||||
#region 解析类型 int
|
||||
if (type.IsValueType)
|
||||
{
|
||||
//return GetValueResolver(type, valueStr, operatorStr, parts);
|
||||
}
|
||||
if (type == typeof(int))
|
||||
{
|
||||
var op = ParseValueTypeOperator<int>(operatorStr);
|
||||
@@ -278,52 +294,76 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
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))
|
||||
string operatorStr;
|
||||
string valueStr;
|
||||
Type type;
|
||||
// 尝试获取指定类型
|
||||
int typeStartIndex = expression.IndexOf('<');
|
||||
int typeEndIndex = expression.IndexOf('>');
|
||||
if (typeStartIndex + typeStartIndex == -2)
|
||||
{
|
||||
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])
|
||||
};
|
||||
|
||||
}
|
||||
// 如果不需要转为指定类型
|
||||
operatorStr = parts[0];
|
||||
valueStr = string.Join(' ', parts, 1, parts.Length - 1);
|
||||
type = data.GetType();
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
double value = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
return new ValueTypeConditionResolver<double>
|
||||
else
|
||||
{//string typeStr = parts[0];
|
||||
string typeStr = expression.Substring(typeStartIndex + 1, typeEndIndex - typeStartIndex - 1)
|
||||
.Trim().ToLower(); // 手动置顶的类型
|
||||
parts = expression.Substring(typeEndIndex + 1).Trim().Split(' ');
|
||||
operatorStr = parts[0].ToLower(); // 操作类型
|
||||
valueStr = string.Join(' ', parts.Skip(1)); // 表达式值
|
||||
|
||||
|
||||
type = typeStr switch
|
||||
{
|
||||
Op = ParseValueTypeOperator<double>(operatorStr),
|
||||
Value = value,
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||
"bool" => typeof(bool),
|
||||
"float" => typeof(float),
|
||||
"decimal" => typeof(decimal),
|
||||
"double" => typeof(double),
|
||||
"sbyte" => typeof(sbyte),
|
||||
"byte" => typeof(byte),
|
||||
"short" => typeof(short),
|
||||
"ushort" => typeof(ushort),
|
||||
"int" => typeof(int),
|
||||
"uint" => typeof(uint),
|
||||
"long" => typeof(long),
|
||||
"ulong" => typeof(ulong),
|
||||
"nint" => typeof(nint),
|
||||
"nuint" => typeof(nuint),
|
||||
_ => typeof(string),
|
||||
};
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (type == typeof(bool))
|
||||
{
|
||||
bool value = bool.Parse(valueStr);
|
||||
return new BoolConditionResolver
|
||||
@@ -332,6 +372,10 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
Value = value,
|
||||
};
|
||||
}
|
||||
else if (type.IsValueType)
|
||||
{
|
||||
return GetValueResolver(type, valueStr, operatorStr, parts);
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
return new StringConditionResolver
|
||||
@@ -344,6 +388,133 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
throw new NotSupportedException($"Type {type} is not supported.");
|
||||
}
|
||||
|
||||
public static SereinConditionResolver GetValueResolver(Type valueType, string valueStr, string operatorStr, string[] parts)// where T : struct, IComparable<T>
|
||||
{
|
||||
SereinConditionResolver resolver = valueType switch
|
||||
{
|
||||
Type t when t == typeof(float) => GetValueResolver<float>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(decimal) => GetValueResolver<decimal>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(double) => GetValueResolver<double>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(sbyte) => GetValueResolver<sbyte>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(byte) => GetValueResolver<byte>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(short) => GetValueResolver<short>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(ushort) => GetValueResolver<ushort>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(int) => GetValueResolver<int>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(uint) => GetValueResolver<uint>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(long) => GetValueResolver<long>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(ulong) => GetValueResolver<ulong>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(nint) => GetValueResolver<nint>(valueStr, operatorStr, parts),
|
||||
Type t when t == typeof(nuint) => GetValueResolver<nuint>(valueStr, operatorStr, parts),
|
||||
_ => throw new ArgumentException("非预期值类型")
|
||||
};
|
||||
return resolver;
|
||||
}
|
||||
|
||||
|
||||
private static ValueTypeConditionResolver<T> GetValueResolver<T>(string valueStr, string operatorStr, string[] parts)
|
||||
where T :struct, IComparable<T>
|
||||
{
|
||||
var op = ParseValueTypeOperator<T>(operatorStr);
|
||||
if (op == ValueTypeConditionResolver<T>.Operator.InRange || op == ValueTypeConditionResolver<T>.Operator.OutOfRange)
|
||||
{
|
||||
var temp = valueStr.Split('-');
|
||||
var leftNum = string.Empty;
|
||||
var rightNum = string.Empty;
|
||||
if (temp.Length < 2 || temp.Length > 4)
|
||||
{
|
||||
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||
}
|
||||
else if (temp.Length == 2)
|
||||
{
|
||||
leftNum = temp[0];
|
||||
rightNum = temp[1];
|
||||
}
|
||||
else if (temp.Length == 3)
|
||||
{
|
||||
if (string.IsNullOrEmpty(temp[0])
|
||||
&& !string.IsNullOrEmpty(temp[1])
|
||||
&& !string.IsNullOrEmpty(temp[2]))
|
||||
{
|
||||
leftNum = "-" + temp[1];
|
||||
rightNum = temp[2];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||
}
|
||||
}
|
||||
else if (temp.Length == 4)
|
||||
{
|
||||
if (string.IsNullOrEmpty(temp[0])
|
||||
&& !string.IsNullOrEmpty(temp[1])
|
||||
&& string.IsNullOrEmpty(temp[2])
|
||||
&& !string.IsNullOrEmpty(temp[3]))
|
||||
{
|
||||
leftNum = "-" + temp[1];
|
||||
rightNum = temp[3];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"范围无效:{valueStr}。");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new ValueTypeConditionResolver<T>
|
||||
{
|
||||
Op = op,
|
||||
RangeStart = leftNum.ToValueData<T>(),
|
||||
RangeEnd = rightNum.ToValueData<T>(),
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0]),
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new ValueTypeConditionResolver<T>
|
||||
{
|
||||
Op = op,
|
||||
Value = valueStr.ToValueData<T>(),
|
||||
ArithmeticExpression = GetArithmeticExpression(parts[0])
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
//public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
||||
//{
|
||||
// return (T)ValueParse(typeof(T), value);
|
||||
//}
|
||||
|
||||
//public static object ValueParse(Type type, object value)
|
||||
//{
|
||||
|
||||
// string? valueStr = value.ToString();
|
||||
// if (string.IsNullOrEmpty(valueStr))
|
||||
// {
|
||||
// throw new ArgumentException("value is null");
|
||||
// }
|
||||
// object result = type switch
|
||||
// {
|
||||
// Type t when t.IsEnum => Enum.Parse(type, valueStr),
|
||||
// Type t when t == typeof(bool) => bool.Parse(valueStr),
|
||||
// Type t when t == typeof(float) => float.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(decimal) => decimal.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(double) => double.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(sbyte) => sbyte.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(byte) => byte.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(short) => short.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(ushort) => ushort.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(int) => int.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(uint) => uint.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(long) => long.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(ulong) => ulong.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(nint) => nint.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// Type t when t == typeof(nuint) => nuint.Parse(valueStr, CultureInfo.InvariantCulture),
|
||||
// _ => throw new ArgumentException("非预期值类型")
|
||||
// };
|
||||
// return result;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Data;
|
||||
using Serein.Library.Utils;
|
||||
using System.Data;
|
||||
|
||||
namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
{
|
||||
@@ -9,19 +10,20 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
/// </summary>
|
||||
/// <param name="obj">操作的对象</param>
|
||||
/// <returns></returns>
|
||||
public class SerinArithmeticExpressionEvaluator
|
||||
public class SerinArithmeticExpressionEvaluator<T> where T : struct, IComparable<T>
|
||||
{
|
||||
private static readonly DataTable table = new DataTable();
|
||||
|
||||
public static double Evaluate(string expression, double inputValue)
|
||||
public static T Evaluate(string expression, T inputValue)
|
||||
{
|
||||
|
||||
// 替换占位符@为输入值
|
||||
expression = expression.Replace("@", inputValue.ToString());
|
||||
try
|
||||
{
|
||||
// 使用 DataTable.Compute 方法计算表达式
|
||||
var result = table.Compute(expression, string.Empty);
|
||||
return Convert.ToDouble(result);
|
||||
return (T)result;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -84,8 +86,9 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
/// <param name="methodCall">方法名称</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
private static object? InvokeMethod(object target, string methodCall)
|
||||
private static object? InvokeMethod(object? target, string methodCall)
|
||||
{
|
||||
if (target is null) return null;
|
||||
var methodParts = methodCall.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (methodParts.Length != 2)
|
||||
{
|
||||
@@ -344,15 +347,15 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
/// <param name="value"></param>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
private static double ComputedNumber(object value, string expression)
|
||||
private static decimal ComputedNumber(object value, string expression)
|
||||
{
|
||||
double numericValue = Convert.ToDouble(value);
|
||||
if (!string.IsNullOrEmpty(expression))
|
||||
{
|
||||
numericValue = SerinArithmeticExpressionEvaluator.Evaluate(expression, numericValue);
|
||||
}
|
||||
return ComputedNumber<decimal>(value, expression);
|
||||
}
|
||||
|
||||
return numericValue;
|
||||
private static T ComputedNumber<T>(object value, string expression) where T : struct, IComparable<T>
|
||||
{
|
||||
T result = value.ToConvert<T>();
|
||||
return SerinArithmeticExpressionEvaluator<T>.Evaluate(expression, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user