2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
using Serein.Library.Api;
|
2025-07-28 20:04:56 +08:00
|
|
|
|
using Serein.Script;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
using System.Dynamic;
|
|
|
|
|
|
using System.Linq.Expressions;
|
2024-08-06 16:09:46 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow.Model
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 条件节点(用于条件控件)
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[NodeProperty(ValuePath = NodeValuePath.Node)]
|
|
|
|
|
|
public partial class SingleConditionNode : NodeModelBase
|
2024-08-06 16:09:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否为自定义参数
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[PropertyInfo(IsNotification = true)]
|
2024-12-12 20:31:50 +08:00
|
|
|
|
private bool _isExplicitData;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
2024-08-06 16:09:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 自定义参数值
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[PropertyInfo(IsNotification = true)]
|
2024-12-12 20:31:50 +08:00
|
|
|
|
private string? _explicitData;
|
2024-10-28 22:18:48 +08:00
|
|
|
|
|
2024-08-06 16:09:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 条件表达式
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[PropertyInfo(IsNotification = true)]
|
|
|
|
|
|
private string _expression;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
}
|
2024-08-06 16:09:46 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public partial class SingleConditionNode : NodeModelBase
|
|
|
|
|
|
{
|
2024-12-24 22:23:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 条件表达式节点是基础节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override bool IsBase => true;
|
|
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 表达式参数索引
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private const int INDEX_EXPRESSION = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public SingleConditionNode(IFlowEnvironment environment):base(environment)
|
|
|
|
|
|
{
|
2024-12-12 20:31:50 +08:00
|
|
|
|
this.IsExplicitData = false;
|
|
|
|
|
|
this.ExplicitData = string.Empty;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
this.Expression = "PASS";
|
|
|
|
|
|
}
|
2024-08-06 16:09:46 +08:00
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
public override void OnCreating()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 这里的这个参数是为了方便使用入参控制点,参数无意义
|
|
|
|
|
|
var pd = new ParameterDetails[1];
|
|
|
|
|
|
pd[INDEX_EXPRESSION] = new ParameterDetails
|
|
|
|
|
|
{
|
|
|
|
|
|
Index = INDEX_EXPRESSION,
|
|
|
|
|
|
Name = nameof(Expression),
|
|
|
|
|
|
IsExplicitData = false,
|
|
|
|
|
|
DataValue = string.Empty,
|
|
|
|
|
|
DataType = typeof(string),
|
|
|
|
|
|
ExplicitType = typeof(string),
|
|
|
|
|
|
ArgDataSourceNodeGuid = string.Empty,
|
|
|
|
|
|
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
|
|
|
|
|
|
NodeModel = this,
|
2024-12-14 23:46:37 +08:00
|
|
|
|
//Convertor = null,
|
2025-03-15 15:43:42 +08:00
|
|
|
|
InputType = ParameterValueInputType.Input,
|
2024-12-12 20:31:50 +08:00
|
|
|
|
Items = null,
|
2025-03-15 16:03:58 +08:00
|
|
|
|
Description = "条件节点入参控制点"
|
2024-12-12 20:31:50 +08:00
|
|
|
|
};
|
|
|
|
|
|
this.MethodDetails.ParameterDetailss = [..pd];
|
|
|
|
|
|
}
|
2024-10-28 21:52:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导出方法信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
dynamic data = new ExpandoObject();
|
|
|
|
|
|
data.Expression = Expression ?? "";
|
|
|
|
|
|
data.ExplicitData = ExplicitData ?? "";
|
|
|
|
|
|
data.IsExplicitData = IsExplicitData;
|
|
|
|
|
|
nodeInfo.CustomData = data;
|
|
|
|
|
|
return nodeInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载自定义数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
public override void LoadCustomData(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Expression = nodeInfo.CustomData?.Expression ?? "";
|
|
|
|
|
|
this.ExplicitData = nodeInfo.CustomData?.ExplicitData ?? "";
|
|
|
|
|
|
this.IsExplicitData = nodeInfo.CustomData?.IsExplicitData ?? false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重写节点的方法执行
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-23 16:20:41 +08:00
|
|
|
|
public override async Task<FlowResult> ExecutingAsync(IFlowContext context, CancellationToken token)
|
2024-08-06 16:09:46 +08:00
|
|
|
|
{
|
2025-03-21 18:26:01 +08:00
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
|
{
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context);
|
2025-03-21 18:26:01 +08:00
|
|
|
|
}
|
2024-08-06 16:09:46 +08:00
|
|
|
|
// 接收上一节点参数or自定义参数内容
|
2024-10-14 17:29:28 +08:00
|
|
|
|
object? parameter;
|
2024-10-28 22:18:48 +08:00
|
|
|
|
object? result = null;
|
|
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
if (!IsExplicitData)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 使用自动取参
|
|
|
|
|
|
var pd = MethodDetails.ParameterDetailss[INDEX_EXPRESSION];
|
2025-03-21 18:26:01 +08:00
|
|
|
|
var hasNode = context.Env.TryGetNodeModel(pd.ArgDataSourceNodeGuid, out var argSourceNode);
|
2025-07-28 20:04:56 +08:00
|
|
|
|
if (!hasNode)
|
2025-03-21 18:26:01 +08:00
|
|
|
|
{
|
2025-07-28 20:04:56 +08:00
|
|
|
|
/*context.NextOrientation = ConnectionInvokeType.IsError;
|
|
|
|
|
|
return new FlowResult(this.Guid, context);*/
|
|
|
|
|
|
parameter = null;
|
2024-10-14 17:29:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-28 20:04:56 +08:00
|
|
|
|
if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeData)
|
|
|
|
|
|
{
|
|
|
|
|
|
result = context.GetFlowData(argSourceNode.Guid).Value; // 使用自定义节点的参数
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeDataOfInvoke)
|
|
|
|
|
|
{
|
|
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource();
|
|
|
|
|
|
var nodeResult = await argSourceNode.ExecutingAsync(context, cts.Token);
|
|
|
|
|
|
result = nodeResult.Value;
|
|
|
|
|
|
cts?.Cancel();
|
|
|
|
|
|
cts?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
result = context.TransmissionData(this.Guid).Value; // 条件节点透传上一节点的数据
|
|
|
|
|
|
}
|
|
|
|
|
|
parameter = result; // 使用上一节点的参数
|
2024-10-14 17:29:28 +08:00
|
|
|
|
}
|
2025-07-28 20:04:56 +08:00
|
|
|
|
|
2024-10-28 22:18:48 +08:00
|
|
|
|
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-12-12 20:31:50 +08:00
|
|
|
|
var exp = ExplicitData?.ToString();
|
2025-07-28 20:04:56 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(exp) && exp.StartsWith("@Get", StringComparison.OrdinalIgnoreCase))
|
2024-10-28 22:18:48 +08:00
|
|
|
|
{
|
2025-07-28 20:04:56 +08:00
|
|
|
|
parameter = GetValueExpressionAsync(context, result, exp);
|
2024-10-28 22:18:48 +08:00
|
|
|
|
}
|
2024-12-12 20:31:50 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
parameter = ExplicitData; // 使用自定义的参数
|
|
|
|
|
|
}
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
2024-10-28 22:18:48 +08:00
|
|
|
|
|
2024-12-18 00:05:42 +08:00
|
|
|
|
bool judgmentResult = false;
|
2024-09-09 16:42:01 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-28 20:04:56 +08:00
|
|
|
|
judgmentResult = await ConditionExpressionAsync(context, parameter, Expression);
|
|
|
|
|
|
//judgmentResult = SereinConditionParser.To(parameter, Expression);
|
2024-12-18 00:05:42 +08:00
|
|
|
|
context.NextOrientation = judgmentResult ? ConnectionInvokeType.IsSucceed : ConnectionInvokeType.IsFail;
|
2024-09-09 16:42:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-10-24 23:32:43 +08:00
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsError;
|
2024-11-04 23:30:52 +08:00
|
|
|
|
context.ExceptionOfRuning = ex;
|
2024-09-09 16:42:01 +08:00
|
|
|
|
}
|
2024-11-08 17:30:51 +08:00
|
|
|
|
|
|
|
|
|
|
SereinEnv.WriteLine(InfoType.INFO, $"{result} {Expression} -> " + context.NextOrientation);
|
2024-12-18 00:05:42 +08:00
|
|
|
|
//return result;
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context, judgmentResult);
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-02 16:48:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-28 20:04:56 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 解析取值表达式
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private SereinScript getValueScript;
|
|
|
|
|
|
private string getValueExpression;
|
|
|
|
|
|
private async Task<object?> GetValueExpressionAsync(IFlowContext flowContext, object? data, string expression)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dataName = nameof(data);
|
|
|
|
|
|
if (!expression.Equals(getValueExpression))
|
|
|
|
|
|
{
|
|
|
|
|
|
getValueExpression = expression;
|
|
|
|
|
|
getValueScript = new SereinScript();
|
|
|
|
|
|
var dataType = data is null ? typeof(object) : data.GetType();
|
|
|
|
|
|
if (expression[0..4].Equals("@get", StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
|
{
|
2025-07-28 20:21:50 +08:00
|
|
|
|
getValueExpression = expression[4..];
|
2025-07-28 20:04:56 +08:00
|
|
|
|
// 表达式默认包含 “.”
|
2025-07-28 20:21:50 +08:00
|
|
|
|
getValueExpression = $"return {dataName}{expression};";
|
2025-07-28 20:04:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 表达式默认包含 “.”
|
2025-07-28 20:21:50 +08:00
|
|
|
|
getValueExpression = $"return {getValueExpression};";
|
2025-07-28 20:04:56 +08:00
|
|
|
|
}
|
2025-07-28 20:21:50 +08:00
|
|
|
|
var resultType = getValueScript.ParserScript(getValueExpression, new Dictionary<string, Type>
|
2025-07-28 20:04:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
{ dataName, dataType},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IScriptInvokeContext scriptContext = new ScriptInvokeContext(flowContext);
|
|
|
|
|
|
scriptContext.SetVarValue(dataName, data);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await getValueScript.InterpreterAsync(scriptContext);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 解析条件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private SereinScript conditionScript;
|
|
|
|
|
|
private string conditionExpression;
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> ConditionExpressionAsync(IFlowContext flowContext, object? data, string expression)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dataName = nameof(data);
|
|
|
|
|
|
if (!expression.Equals(conditionExpression))
|
|
|
|
|
|
{
|
|
|
|
|
|
conditionExpression = expression;
|
|
|
|
|
|
conditionScript = new SereinScript();
|
|
|
|
|
|
var dataType = data is null ? typeof(object) : data.GetType();
|
2025-07-28 20:21:50 +08:00
|
|
|
|
conditionExpression = expression.Trim();
|
2025-07-28 20:04:56 +08:00
|
|
|
|
if (expression[0] == '.')
|
|
|
|
|
|
{
|
|
|
|
|
|
// 对象取值
|
2025-07-28 20:21:50 +08:00
|
|
|
|
conditionExpression = $"return {dataName}{expression};";
|
2025-07-28 20:04:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 直接表达式
|
2025-07-28 20:21:50 +08:00
|
|
|
|
conditionExpression = $"return {dataName}.{expression};";
|
2025-07-28 20:04:56 +08:00
|
|
|
|
}
|
2025-07-28 20:21:50 +08:00
|
|
|
|
var resultType = conditionScript.ParserScript(conditionExpression, new Dictionary<string, Type>
|
2025-07-28 20:04:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
{ dataName, dataType},
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IScriptInvokeContext scriptContext = new ScriptInvokeContext(flowContext);
|
|
|
|
|
|
scriptContext.SetVarValue(dataName, data);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await conditionScript.InterpreterAsync(scriptContext);
|
|
|
|
|
|
if(result is bool @bool)
|
|
|
|
|
|
{
|
|
|
|
|
|
return @bool;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
flowContext.NextOrientation = ConnectionInvokeType.IsError;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|