2025-07-27 23:34:01 +08:00
|
|
|
|
using Serein.Library;
|
2024-12-09 22:57:06 +08:00
|
|
|
|
using Serein.Library.Api;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
using System.Dynamic;
|
2024-12-09 22:57:06 +08:00
|
|
|
|
|
2025-07-29 14:25:31 +08:00
|
|
|
|
namespace Serein.NodeFlow.Model.Nodes
|
2024-12-09 22:57:06 +08:00
|
|
|
|
{
|
2024-12-12 20:31:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-03-18 11:52:54 +08:00
|
|
|
|
/// <summary>
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// Expression Operation - 表达式操作
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[NodeProperty(ValuePath = NodeValuePath.Node)]
|
|
|
|
|
|
public partial class SingleGlobalDataNode : NodeModelBase
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-07-29 14:25:31 +08:00
|
|
|
|
/// 全局数据的Key名称
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// </summary>
|
2025-05-28 23:19:00 +08:00
|
|
|
|
[PropertyInfo(IsNotification = true)]
|
2024-12-12 20:31:50 +08:00
|
|
|
|
private string _keyName;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-09 22:57:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 全局数据节点
|
|
|
|
|
|
/// </summary>
|
2024-12-24 22:23:53 +08:00
|
|
|
|
public partial class SingleGlobalDataNode : NodeModelBase, INodeContainer
|
2024-12-09 22:57:06 +08:00
|
|
|
|
{
|
2025-07-28 17:38:51 +08:00
|
|
|
|
string INodeContainer.Guid => this.Guid;
|
|
|
|
|
|
|
2024-12-24 22:23:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 全局数据节点是基础节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override bool IsBase => true;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据源只允许放置1个节点。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override int MaxChildrenCount => 1;
|
|
|
|
|
|
|
2024-12-09 22:57:06 +08:00
|
|
|
|
public SingleGlobalDataNode(IFlowEnvironment environment) : base(environment)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// <summary>
|
2024-12-24 11:51:12 +08:00
|
|
|
|
/// 数据来源的节点
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// </summary>
|
2025-07-29 14:25:31 +08:00
|
|
|
|
public IFlowNode? DataNode { get; private set; } = null;
|
2024-12-24 22:23:53 +08:00
|
|
|
|
|
2025-03-18 11:52:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 有节点被放置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeModel"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-05-31 12:15:01 +08:00
|
|
|
|
public bool PlaceNode(IFlowNode nodeModel)
|
2024-12-24 22:23:53 +08:00
|
|
|
|
{
|
2025-07-29 14:25:31 +08:00
|
|
|
|
if(nodeModel.ControlType is not (NodeControlType.Action or NodeControlType.Script))
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.WriteLine(InfoType.INFO, "放置在全局数据必须是有返回值的[Action]节点,[Script]节点。");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nodeModel.MethodDetails?.ReturnType is null
|
|
|
|
|
|
|| nodeModel.MethodDetails.ReturnType == typeof(void))
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.WriteLine(InfoType.INFO, "放置在全局数据必须是有返回值的[Action]节点,[Script]节点。");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-26 16:42:05 +08:00
|
|
|
|
if(DataNode is null)
|
2024-12-26 00:26:50 +08:00
|
|
|
|
{
|
2024-12-26 16:42:05 +08:00
|
|
|
|
// 放置节点
|
|
|
|
|
|
nodeModel.ContainerNode = this;
|
|
|
|
|
|
ChildrenNode.Add(nodeModel);
|
|
|
|
|
|
DataNode = nodeModel;
|
2025-07-29 14:25:31 +08:00
|
|
|
|
|
|
|
|
|
|
MethodDetails.IsAsync = nodeModel.MethodDetails.IsAsync;
|
|
|
|
|
|
MethodDetails.ReturnType = nodeModel.MethodDetails.ReturnType;
|
2024-12-26 16:42:05 +08:00
|
|
|
|
return true;
|
2024-12-26 00:26:50 +08:00
|
|
|
|
}
|
2025-07-28 17:38:51 +08:00
|
|
|
|
else if (DataNode.Guid != nodeModel.Guid)
|
2024-12-26 00:26:50 +08:00
|
|
|
|
{
|
2025-07-28 17:38:51 +08:00
|
|
|
|
Env.FlowEdit.TakeOutNodeToContainer(DataNode.CanvasDetails.Guid, DataNode.Guid);
|
|
|
|
|
|
Env.FlowEdit.PlaceNodeToContainer(this.CanvasDetails.Guid, nodeModel.Guid, this.Guid);
|
2024-12-26 16:42:05 +08:00
|
|
|
|
return false;
|
2024-12-26 00:26:50 +08:00
|
|
|
|
}
|
2025-07-28 17:38:51 +08:00
|
|
|
|
return false;
|
2024-12-26 16:42:05 +08:00
|
|
|
|
|
2024-12-24 22:23:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-26 16:42:05 +08:00
|
|
|
|
|
2025-05-31 12:15:01 +08:00
|
|
|
|
public bool TakeOutNode(IFlowNode nodeModel)
|
2024-12-24 22:23:53 +08:00
|
|
|
|
{
|
2024-12-26 16:42:05 +08:00
|
|
|
|
if (ChildrenNode.Contains(nodeModel))
|
|
|
|
|
|
{
|
|
|
|
|
|
ChildrenNode.Remove(nodeModel);
|
|
|
|
|
|
nodeModel.ContainerNode = null;
|
|
|
|
|
|
DataNode = null;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-24 22:23:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-26 16:42:05 +08:00
|
|
|
|
public async void TakeOutAll()
|
2024-12-24 22:23:53 +08:00
|
|
|
|
{
|
2024-12-26 16:42:05 +08:00
|
|
|
|
foreach (var nodeModel in ChildrenNode)
|
|
|
|
|
|
{
|
2025-07-04 21:31:07 +08:00
|
|
|
|
nodeModel.Env.FlowEdit.TakeOutNodeToContainer(nodeModel.CanvasDetails.Guid, nodeModel.Guid);
|
2024-12-26 16:42:05 +08:00
|
|
|
|
}
|
2024-12-24 22:23:53 +08:00
|
|
|
|
DataNode = null;
|
|
|
|
|
|
}
|
2024-12-12 20:31:50 +08:00
|
|
|
|
|
2025-05-28 23:19:00 +08:00
|
|
|
|
|
2024-12-12 20:31:50 +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-12-12 20:31:50 +08:00
|
|
|
|
{
|
2025-07-06 14:34:49 +08:00
|
|
|
|
if (token.IsCancellationRequested) return new FlowResult(this.Guid, context);
|
2024-12-12 20:31:50 +08:00
|
|
|
|
if (string.IsNullOrEmpty(KeyName))
|
|
|
|
|
|
{
|
|
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsError;
|
|
|
|
|
|
SereinEnv.WriteLine(InfoType.ERROR, $"全局数据的KeyName不能为空[{this.Guid}]");
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context);
|
2024-12-12 20:31:50 +08:00
|
|
|
|
}
|
2024-12-24 22:23:53 +08:00
|
|
|
|
if (DataNode is null)
|
2024-12-12 20:31:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsError;
|
2024-12-24 11:51:12 +08:00
|
|
|
|
SereinEnv.WriteLine(InfoType.ERROR, $"全局数据节点没有设置数据来源[{this.Guid}]");
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context);
|
2024-12-12 20:31:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-03-21 18:26:01 +08:00
|
|
|
|
|
|
|
|
|
|
var result = await DataNode.ExecutingAsync(context, token);
|
|
|
|
|
|
SereinEnv.AddOrUpdateFlowGlobalData(KeyName, result.Value);
|
2024-12-12 20:31:50 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
context.NextOrientation = ConnectionInvokeType.IsError;
|
|
|
|
|
|
context.ExceptionOfRuning = ex;
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context);
|
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.KeyName = KeyName; // 变量名称
|
|
|
|
|
|
|
2024-12-24 22:23:53 +08:00
|
|
|
|
nodeInfo.CustomData = data;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
return nodeInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载全局变量的数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
public override void LoadCustomData(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
KeyName = nodeInfo.CustomData?.KeyName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
/* /// <summary>
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// 需要移除数据节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override void Remove()
|
|
|
|
|
|
{
|
2025-03-22 18:14:48 +08:00
|
|
|
|
if (DataNode is null) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-12-12 20:31:50 +08:00
|
|
|
|
// 移除数据节点
|
2025-05-27 23:46:06 +08:00
|
|
|
|
_ = this.Env.RemoveNodeAsync(DataNode.CanvasDetails.Guid, DataNode.Guid);
|
2025-06-22 21:53:37 +08:00
|
|
|
|
}*/
|
2024-12-12 20:31:50 +08:00
|
|
|
|
|
2024-12-09 22:57:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|