尝试将节点流导出为c#代码文件

This commit is contained in:
fengjiayi
2025-07-06 14:34:49 +08:00
parent 162dc7bcf8
commit b25fd9c83c
45 changed files with 1625 additions and 361 deletions

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Serein.NodeFlow.Services
{
@@ -113,5 +114,53 @@ namespace Serein.NodeFlow.Services
return flowCanvasDetails.Nodes.Count > 0;
}
public void ToCsharpCoreFile()
{
// TODO: 实现将流程模型转换为C# Core文件的逻辑
// 遍历每个画布
int canvas_index = 0;
HashSet<Type> assemblyFlowClasss = new HashSet<Type>(); // 用于创建依赖注入项
StringBuilder stringBuilder = new StringBuilder();
foreach (var canvas in FlowCanvass.Values)
{
int flowTemplateId = canvas_index++;
string flowTemplateClassName = $"FlowTemplate{flowTemplateId}";
HashSet<Type> flowClasss = new HashSet<Type>();
// 收集程序集信息
foreach (var node in canvas.Nodes)
{
var instanceType = node.MethodDetails.ActingInstanceType;
if(instanceType is not null)
{
flowClasss.Add(instanceType);
assemblyFlowClasss.Add(instanceType);
}
}
// 生成方法信息
foreach (var node in canvas.Nodes)
{
var instanceType = node.MethodDetails.ActingInstanceType;
var returnType = node.MethodDetails.ReturnType;
var methodName = node.MethodDetails.MethodAnotherName;
var instanceTypeFullName = instanceType.FullName;
var returnTypeFullName = returnType == typeof(void) ? "void" : returnType.FullName;
string methodContext = $"private {returnTypeFullName} NodeMethod_{methodName}({nameof(IDynamicContext)} context)";
SereinEnv.WriteLine(InfoType.INFO, methodContext);
}
}
}
}
}

View File

@@ -50,12 +50,12 @@ namespace Serein.NodeFlow.Services
/// <summary>
/// 重做
/// </summary>
public void Redo()
public async Task Redo()
{
if (redoStack.Count > 0)
{
var command = redoStack.Pop();
var state = command.Execute();
var state = await command.ExecuteAsync();
if (state)
{
undoStack.Push(command); // 将重做的命令推入撤销栈
@@ -64,10 +64,10 @@ namespace Serein.NodeFlow.Services
}
internal void Execute(IOperation operation)
internal async Task Execute(IOperation operation)
{
sereinIOC.InjectDependenciesProperty(operation); // 注入所需要的依赖
var state = operation.Execute();
var state = await operation.ExecuteAsync();
if (state)
{
// 执行后,推入撤销栈,并清空重做栈

View File

@@ -273,14 +273,11 @@ namespace Serein.NodeFlow.Services
var pool = WorkOptions.FlowContextPool;
var context = pool.Allocate();
var token = WorkOptions.CancellationTokenSource.Token;
await startNode.StartFlowAsync(context, token); // 开始运行时从选定节点开始运行
var result = await startNode.StartFlowAsync(context, token); // 开始运行时从选定节点开始运行
context.Reset();
pool.Free(context);
}
/// <summary>
/// 尝试添加全局触发器
/// </summary>
@@ -295,6 +292,7 @@ namespace Serein.NodeFlow.Services
}
}
/// <summary>
/// 尝试移除全局触发器
/// </summary>
@@ -344,7 +342,7 @@ namespace Serein.NodeFlow.Services
{
var context = pool.Allocate(); // 启动全局触发器时新建上下文
var newFlowData = await singleFlipFlopNode.ExecutingAsync(context, singleToken); // 获取触发器等待Task
context.AddOrUpdate(singleFlipFlopNode, newFlowData);
context.AddOrUpdate(singleFlipFlopNode.Guid, newFlowData);
if (context.NextOrientation == ConnectionInvokeType.None)
{
continue;
@@ -388,7 +386,7 @@ namespace Serein.NodeFlow.Services
{
continue;
}
context.SetPreviousNode(nextNodes[i], singleFlipFlopNode); // 设置调用关系
context.SetPreviousNode(nextNodes[i].Guid, singleFlipFlopNode.Guid); // 设置调用关系
if (nextNodes[i].DebugSetting.IsInterrupt) // 执行触发前检查终端
{
@@ -407,7 +405,7 @@ namespace Serein.NodeFlow.Services
continue;
}
context.SetPreviousNode(nextNodes[i], singleFlipFlopNode);
context.SetPreviousNode(nextNodes[i].Guid, singleFlipFlopNode.Guid);
if (nextNodes[i].DebugSetting.IsInterrupt) // 执行触发前
{
await nextNodes[i].DebugSetting.GetInterruptTask.Invoke();