2025-03-20 22:54:10 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using Serein.Library;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Api;
|
2024-11-02 16:48:40 +08:00
|
|
|
|
using Serein.Library.Utils;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Utils.SereinExpression;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
using System.Dynamic;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
using System.Reactive;
|
2024-10-29 10:46:39 +08:00
|
|
|
|
using System.Reflection.Metadata;
|
2024-08-06 16:09:46 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow.Model
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Expression Operation - 表达式操作
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[NodeProperty(ValuePath = NodeValuePath.Node)]
|
|
|
|
|
|
public partial class SingleExpOpNode : NodeModelBase
|
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-08-06 16:09:46 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public partial class SingleExpOpNode : 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 SingleExpOpNode(IFlowEnvironment environment) : base(environment)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-06 16:09:46 +08:00
|
|
|
|
|
2024-10-28 21:52:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载完成后调用的方法
|
|
|
|
|
|
/// </summary>
|
2024-11-02 16:48:40 +08:00
|
|
|
|
public override void OnCreating()
|
2024-10-28 21:52:45 +08:00
|
|
|
|
{
|
2024-12-12 20:31:50 +08:00
|
|
|
|
// 这里的这个参数是为了方便使用入参控制点,参数无意义
|
|
|
|
|
|
var pd = new ParameterDetails[1];
|
|
|
|
|
|
pd[INDEX_EXPRESSION] = new ParameterDetails
|
2024-10-29 10:46:39 +08:00
|
|
|
|
{
|
2024-12-12 20:31:50 +08:00
|
|
|
|
Index = INDEX_EXPRESSION,
|
2024-11-02 16:48:40 +08:00
|
|
|
|
Name = nameof(Expression),
|
2024-10-29 10:46:39 +08:00
|
|
|
|
IsExplicitData = false,
|
|
|
|
|
|
DataValue = string.Empty,
|
2024-12-12 20:31:50 +08:00
|
|
|
|
DataType = typeof(string),
|
|
|
|
|
|
ExplicitType = typeof(string),
|
2024-10-29 10:46:39 +08:00
|
|
|
|
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-10-29 10:46:39 +08:00
|
|
|
|
};
|
2024-12-12 20:31:50 +08:00
|
|
|
|
this.MethodDetails.ParameterDetailss = [.. pd];
|
|
|
|
|
|
}
|
2024-10-29 10:46:39 +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 ?? "";
|
|
|
|
|
|
nodeInfo.CustomData = data;
|
|
|
|
|
|
return nodeInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载自定义数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
public override void LoadCustomData(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Expression = nodeInfo.CustomData?.Expression ?? "";
|
2024-10-28 21:52:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-21 18:26:01 +08:00
|
|
|
|
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
|
2024-08-06 16:09:46 +08:00
|
|
|
|
{
|
2025-07-06 14:34:49 +08:00
|
|
|
|
if(token.IsCancellationRequested) return new FlowResult(this.Guid, context);
|
2025-03-20 22:54:10 +08:00
|
|
|
|
|
2024-10-29 10:46:39 +08:00
|
|
|
|
object? parameter = null;// context.TransmissionData(this); // 表达式节点使用上一节点数据
|
|
|
|
|
|
var pd = MethodDetails.ParameterDetailss[0];
|
|
|
|
|
|
|
2025-03-21 18:26:01 +08:00
|
|
|
|
var hasNode = context.Env.TryGetNodeModel(pd.ArgDataSourceNodeGuid, out var argSourceNode);
|
|
|
|
|
|
if (hasNode)
|
|
|
|
|
|
{
|
|
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsError;
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context);
|
2025-03-21 18:26:01 +08:00
|
|
|
|
}
|
2024-10-29 10:46:39 +08:00
|
|
|
|
if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeData)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 使用自定义节点的参数
|
2025-07-06 14:34:49 +08:00
|
|
|
|
parameter = context.GetFlowData(argSourceNode.Guid).Value;
|
2024-10-29 10:46:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (pd.ArgDataSourceType == ConnectionArgSourceType.GetOtherNodeDataOfInvoke)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 立刻调用目标节点,然后使用其返回值
|
2025-03-21 18:26:01 +08:00
|
|
|
|
var cts = new CancellationTokenSource();
|
|
|
|
|
|
var result = await argSourceNode.ExecutingAsync(context, cts.Token);
|
|
|
|
|
|
cts?.Cancel();
|
|
|
|
|
|
cts?.Dispose();
|
|
|
|
|
|
parameter = result.Value;
|
2024-10-29 10:46:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 条件节点透传上一节点的数据
|
2025-07-06 14:34:49 +08:00
|
|
|
|
parameter = context.TransmissionData(this.Guid);
|
2024-10-29 10:46:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-15 22:07:10 +08:00
|
|
|
|
try
|
2024-08-06 16:09:46 +08:00
|
|
|
|
{
|
2024-10-29 10:46:39 +08:00
|
|
|
|
var newData = SerinExpressionEvaluator.Evaluate(Expression, parameter, out bool isChange);
|
2024-09-15 22:07:10 +08:00
|
|
|
|
object? result = null;
|
|
|
|
|
|
if (isChange)
|
|
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
result = newData;
|
2024-09-15 22:07:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-10-29 10:46:39 +08:00
|
|
|
|
result = parameter;
|
2024-09-15 22:07:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-24 23:32:43 +08:00
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsSucceed;
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context, result);
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
2024-09-15 22:07:10 +08:00
|
|
|
|
catch (Exception ex)
|
2024-08-06 16:09:46 +08:00
|
|
|
|
{
|
2024-10-24 23:32:43 +08:00
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsError;
|
2024-11-04 23:30:52 +08:00
|
|
|
|
context.ExceptionOfRuning = ex;
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context);
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-09-15 12:15:32 +08:00
|
|
|
|
|
2024-08-06 16:09:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|