mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-02 22:36:35 +08:00
修改了无法保存项目文件的bug
This commit is contained in:
@@ -18,15 +18,15 @@ namespace Serein.NodeFlow.Base
|
||||
/// </summary>
|
||||
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||
{
|
||||
public abstract Parameterdata[] GetParameterdatas();
|
||||
public virtual NodeInfo ToInfo()
|
||||
internal abstract Parameterdata[] GetParameterdatas();
|
||||
internal virtual NodeInfo ToInfo()
|
||||
{
|
||||
if (MethodDetails == null) return null;
|
||||
// if (MethodDetails == null) return null;
|
||||
|
||||
var trueNodes = SuccessorNodes[ConnectionType.IsSucceed].Select(item => item.Guid); // 真分支
|
||||
var falseNodes = SuccessorNodes[ConnectionType.IsFail].Select(item => item.Guid);// 假分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 上游分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 异常分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 异常分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 上游分支
|
||||
|
||||
// 生成参数列表
|
||||
Parameterdata[] parameterData = GetParameterdatas();
|
||||
@@ -42,9 +42,41 @@ namespace Serein.NodeFlow.Base
|
||||
UpstreamNodes = upstreamNodes.ToArray(),
|
||||
ParameterData = parameterData.ToArray(),
|
||||
ErrorNodes = errorNodes.ToArray(),
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
internal virtual NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||
{
|
||||
var node = this;
|
||||
if (node != null)
|
||||
{
|
||||
node.Guid = nodeInfo.Guid;
|
||||
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
|
||||
{
|
||||
Parameterdata? pd = nodeInfo.ParameterData[i];
|
||||
node.MethodDetails.ExplicitDatas[i].IsExplicitData = pd.State;
|
||||
node.MethodDetails.ExplicitDatas[i].DataValue = pd.Value;
|
||||
}
|
||||
}
|
||||
|
||||
//if (control is ConditionNodeControl conditionNodeControl)
|
||||
//{
|
||||
// conditionNodeControl.ViewModel.IsCustomData = pd.state;
|
||||
// conditionNodeControl.ViewModel.CustomData = pd.value;
|
||||
// conditionNodeControl.ViewModel.Expression = pd.expression;
|
||||
//}
|
||||
//else if (control is ExpOpNodeControl expOpNodeControl)
|
||||
//{
|
||||
// expOpNodeControl.ViewModel.Expression = pd.expression;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// node.MethodDetails.ExplicitDatas[i].IsExplicitData = pd.state;
|
||||
// node.MethodDetails.ExplicitDatas[i].DataValue = pd.value;
|
||||
//}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -55,7 +87,7 @@ namespace Serein.NodeFlow.Base
|
||||
/// <returns></returns>
|
||||
public async Task StartExecution(IDynamicContext context)
|
||||
{
|
||||
var cts = context.SereinIoc.GetOrInstantiate<CancellationTokenSource>();
|
||||
var cts = context.SereinIoc.GetOrRegisterInstantiate<CancellationTokenSource>();
|
||||
|
||||
Stack<NodeModelBase> stack = [];
|
||||
stack.Push(this);
|
||||
@@ -66,12 +98,12 @@ namespace Serein.NodeFlow.Base
|
||||
var currentNode = stack.Pop();
|
||||
|
||||
// 设置方法执行的对象
|
||||
if (currentNode.MethodDetails is not null)
|
||||
if (currentNode.MethodDetails is not null && currentNode.MethodDetails.ActingInstanceType is not null)
|
||||
{
|
||||
// currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrInstantiate(MethodDetails.ActingInstanceType);
|
||||
// currentNode.MethodDetails.ActingInstance = context.SereinIoc.GetOrInstantiate(MethodDetails.ActingInstanceType);
|
||||
|
||||
currentNode.MethodDetails.ActingInstance = context.SereinIoc.GetOrInstantiate(currentNode.MethodDetails.ActingInstanceType);
|
||||
currentNode.MethodDetails.ActingInstance = context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType);
|
||||
}
|
||||
|
||||
// 获取上游分支,首先执行一次
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Serein.Library.Api;
|
||||
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Attributes;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
@@ -8,8 +9,10 @@ using Serein.NodeFlow.Model;
|
||||
using Serein.NodeFlow.Tool;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Mime;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using static Serein.NodeFlow.FlowStarter;
|
||||
@@ -44,20 +47,25 @@ namespace Serein.NodeFlow
|
||||
/// </summary>
|
||||
public class FlowEnvironment : IFlowEnvironment
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点的命名空间
|
||||
/// </summary>
|
||||
public const string NodeSpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
|
||||
|
||||
/// <summary>
|
||||
/// 加载Dll
|
||||
/// </summary>
|
||||
public event LoadDLLHandler OnDllLoad;
|
||||
/// <summary>
|
||||
/// 加载节点事件
|
||||
/// 项目加载完成
|
||||
/// </summary>
|
||||
public event LoadNodeHandler OnLoadNode;
|
||||
public event ProjectLoadedHandler OnProjectLoaded;
|
||||
/// <summary>
|
||||
/// 节点连接属性改变事件
|
||||
/// </summary>
|
||||
public event NodeConnectChangeHandler OnNodeConnectChange;
|
||||
/// <summary>
|
||||
/// 节点创建时间
|
||||
/// 节点创建事件
|
||||
/// </summary>
|
||||
public event NodeCreateHandler OnNodeCreate;
|
||||
/// <summary>
|
||||
@@ -75,11 +83,6 @@ namespace Serein.NodeFlow
|
||||
|
||||
private FlowStarter? nodeFlowStarter = null;
|
||||
|
||||
/// <summary>
|
||||
/// 节点的命名空间
|
||||
/// </summary>
|
||||
public const string NodeSpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
|
||||
|
||||
/// <summary>
|
||||
/// 一种轻量的IOC容器
|
||||
/// </summary>
|
||||
@@ -103,7 +106,7 @@ namespace Serein.NodeFlow
|
||||
|
||||
public Dictionary<string, NodeModelBase> Nodes { get; } = [];
|
||||
|
||||
// public List<NodeModelBase> Regions { get; } = [];
|
||||
public List<NodeModelBase> Regions { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 存放触发器节点(运行时全部调用)
|
||||
@@ -149,6 +152,8 @@ namespace Serein.NodeFlow
|
||||
var initMethods = MethodDetailss.Where(it => it.MethodDynamicType == NodeType.Init).ToList();
|
||||
var loadingMethods = MethodDetailss.Where(it => it.MethodDynamicType == NodeType.Loading).ToList();
|
||||
var exitMethods = MethodDetailss.Where(it => it.MethodDynamicType == NodeType.Exit).ToList();
|
||||
|
||||
|
||||
await nodeFlowStarter.RunAsync(StartNode,
|
||||
this,
|
||||
runMethodDetailess,
|
||||
@@ -180,6 +185,30 @@ namespace Serein.NodeFlow
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 运行环节加载了项目文件,需要创建节点控件
|
||||
/// </summary>
|
||||
/// <param name="nodeInfo"></param>
|
||||
/// <param name="methodDetailss"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private NodeControlType GetNodeControlType(NodeInfo nodeInfo)
|
||||
{
|
||||
// 创建控件实例
|
||||
NodeControlType controlType = nodeInfo.Type switch
|
||||
{
|
||||
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleActionNode)}" => NodeControlType.Action,// 动作节点控件
|
||||
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleFlipflopNode)}" => NodeControlType.Flipflop, // 触发器节点控件
|
||||
|
||||
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleConditionNode)}" => NodeControlType.ExpCondition,// 条件表达式控件
|
||||
$"{NodeStaticConfig.NodeSpaceName}.{nameof(SingleExpOpNode)}" => NodeControlType.ExpOp, // 操作表达式控件
|
||||
|
||||
$"{NodeStaticConfig.NodeSpaceName}.{nameof(CompositeConditionNode)}" => NodeControlType.ConditionRegion, // 条件区域控件
|
||||
_ => NodeControlType.None,
|
||||
};
|
||||
|
||||
return controlType;
|
||||
}
|
||||
|
||||
#region 对外暴露的接口
|
||||
|
||||
@@ -201,59 +230,120 @@ namespace Serein.NodeFlow
|
||||
(var assembly, var list) = LoadAssembly(dllFilePath);
|
||||
if (assembly is not null && list.Count > 0)
|
||||
{
|
||||
methodDetailss.AddRange(methodDetailss); // 暂存方法描述
|
||||
OnDllLoad?.Invoke(new LoadDLLEventArgs(assembly, methodDetailss)); // 通知UI创建dll面板显示
|
||||
MethodDetailss.AddRange(list); // 暂存方法描述
|
||||
OnDllLoad?.Invoke(new LoadDLLEventArgs(assembly, list)); // 通知UI创建dll面板显示
|
||||
}
|
||||
}
|
||||
// 方法加载完成,缓存到运行环境中。
|
||||
MethodDetailss.AddRange(methodDetailss);
|
||||
methodDetailss.Clear();
|
||||
//MethodDetailss.AddRange(methodDetailss);
|
||||
//methodDetailss.Clear();
|
||||
|
||||
|
||||
List<(NodeModelBase, string[])> regionChildNodes = new List<(NodeModelBase, string[])>();
|
||||
List<(NodeModelBase, Position)> ordinaryNodes = new List<(NodeModelBase, Position)>();
|
||||
// 加载节点
|
||||
foreach (var nodeInfo in projectFile.Nodes)
|
||||
{
|
||||
if (TryGetMethodDetails(nodeInfo.MethodName, out MethodDetails? methodDetails))
|
||||
var controlType = GetNodeControlType(nodeInfo);
|
||||
if(controlType == NodeControlType.None)
|
||||
{
|
||||
OnLoadNode?.Invoke(new LoadNodeEventArgs(nodeInfo, methodDetails));
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
TryGetMethodDetails(nodeInfo.MethodName, out MethodDetails? methodDetails); // 加载项目时尝试获取方法信息
|
||||
methodDetails ??= new MethodDetails();
|
||||
var nodeModel = CreateNode(controlType, methodDetails);
|
||||
nodeModel.LoadInfo(nodeInfo); // 创建节点model
|
||||
if (nodeModel is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
TryAddNode(nodeModel);
|
||||
if(nodeInfo.ChildNodeGuids?.Length > 0)
|
||||
{
|
||||
regionChildNodes.Add((nodeModel,nodeInfo.ChildNodeGuids));
|
||||
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, nodeInfo.Position));
|
||||
}
|
||||
else
|
||||
{
|
||||
ordinaryNodes.Add((nodeModel, nodeInfo.Position));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 加载区域的子项
|
||||
foreach((NodeModelBase region, string[] childNodeGuids) item in regionChildNodes)
|
||||
{
|
||||
foreach (var childNodeGuid in item.childNodeGuids)
|
||||
{
|
||||
Nodes.TryGetValue(childNodeGuid, out NodeModelBase? childNode);
|
||||
if (childNode is null)
|
||||
{
|
||||
// 节点尚未加载
|
||||
continue;
|
||||
}
|
||||
// 存在节点
|
||||
OnNodeCreate?.Invoke(new NodeCreateEventArgs(childNode, true, item.region.Guid));
|
||||
}
|
||||
}
|
||||
// 加载节点
|
||||
foreach ((NodeModelBase nodeModel, Position position) item in ordinaryNodes)
|
||||
{
|
||||
bool IsContinue = false;
|
||||
foreach ((NodeModelBase region, string[] childNodeGuids) item2 in regionChildNodes)
|
||||
{
|
||||
foreach (var childNodeGuid in item2.childNodeGuids)
|
||||
{
|
||||
if (item.nodeModel.Guid.Equals(childNodeGuid))
|
||||
{
|
||||
IsContinue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (IsContinue) continue;
|
||||
OnNodeCreate?.Invoke(new NodeCreateEventArgs(item.nodeModel, item.position));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 确定节点之间的连接关系
|
||||
foreach (var nodeInfo in projectFile.Nodes)
|
||||
{
|
||||
if (!Nodes.TryGetValue(nodeInfo.Guid, out NodeModelBase fromNode))
|
||||
if (!Nodes.TryGetValue(nodeInfo.Guid, out NodeModelBase? fromNode))
|
||||
{
|
||||
// 不存在对应的起始节点
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
List<(ConnectionType, string[])> nodeGuids = [(ConnectionType.IsSucceed,nodeInfo.TrueNodes),
|
||||
(ConnectionType.IsFail, nodeInfo.FalseNodes),
|
||||
(ConnectionType.IsError, nodeInfo.ErrorNodes),
|
||||
(ConnectionType.Upstream, nodeInfo.UpstreamNodes)];
|
||||
List<(ConnectionType connectionType, string[] guids)> allToNodes = [(ConnectionType.IsSucceed,nodeInfo.TrueNodes),
|
||||
(ConnectionType.IsFail, nodeInfo.FalseNodes),
|
||||
(ConnectionType.IsError, nodeInfo.ErrorNodes),
|
||||
(ConnectionType.Upstream, nodeInfo.UpstreamNodes)];
|
||||
|
||||
List<(ConnectionType, NodeModelBase[])> nodes = nodeGuids.Where(info => info.Item2.Length > 0)
|
||||
.Select(info => (info.Item1,
|
||||
info.Item2.Select(guid => Nodes[guid])
|
||||
List<(ConnectionType, NodeModelBase[])> fromNodes = allToNodes.Where(info => info.guids.Length > 0)
|
||||
.Select(info => (info.connectionType,
|
||||
info.guids.Select(guid => Nodes[guid])
|
||||
.ToArray()))
|
||||
.ToList();
|
||||
// 遍历每种类型的节点分支(四种)
|
||||
foreach ((ConnectionType connectionType, NodeModelBase[] nodeBases) item in nodes)
|
||||
foreach ((ConnectionType connectionType, NodeModelBase[] toNodes) item in fromNodes)
|
||||
{
|
||||
// 遍历当前类型分支的节点(确认连接关系)
|
||||
foreach (var node in item.nodeBases)
|
||||
foreach (var toNode in item.toNodes)
|
||||
{
|
||||
ConnectNode(fromNode, node, item.connectionType); // 加载时确定节点间的连接关系
|
||||
ConnectNode(fromNode, toNode, item.connectionType); // 加载时确定节点间的连接关系
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
OnProjectLoaded?.Invoke(new ProjectLoadedEventArgs());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 保存项目为项目文件
|
||||
/// </summary>
|
||||
@@ -268,6 +358,7 @@ namespace Serein.NodeFlow
|
||||
};
|
||||
return projectData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件路径中加载DLL
|
||||
/// </summary>
|
||||
@@ -281,66 +372,27 @@ namespace Serein.NodeFlow
|
||||
MethodDetailss.AddRange(list);
|
||||
OnDllLoad?.Invoke(new LoadDLLEventArgs(assembly, list));
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建节点
|
||||
/// 运行时创建节点
|
||||
/// </summary>
|
||||
/// <param name="nodeBase"></param>
|
||||
public void CreateNode(NodeControlType nodeControlType, MethodDetails? methodDetails = null)
|
||||
public void CreateNode(NodeControlType nodeControlType, Position position, MethodDetails? methodDetails = null)
|
||||
{
|
||||
// 确定创建的节点类型
|
||||
Type? nodeType = nodeControlType switch
|
||||
{
|
||||
NodeControlType.Action => typeof(SingleActionNode),
|
||||
NodeControlType.Flipflop => typeof(SingleFlipflopNode),
|
||||
|
||||
NodeControlType.ExpOp => typeof(SingleExpOpNode),
|
||||
NodeControlType.ExpCondition => typeof(SingleConditionNode),
|
||||
NodeControlType.ConditionRegion => typeof(CompositeConditionNode),
|
||||
_ => null
|
||||
};
|
||||
if (nodeType == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// 生成实例
|
||||
var nodeObj = Activator.CreateInstance(nodeType);
|
||||
if (nodeObj is not NodeModelBase nodeBase)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 配置基础的属性
|
||||
nodeBase.ControlType = nodeControlType;
|
||||
nodeBase.Guid = Guid.NewGuid().ToString();
|
||||
if (methodDetails != null)
|
||||
{
|
||||
var md = methodDetails.Clone();
|
||||
nodeBase.DisplayName = md.MethodTips;
|
||||
nodeBase.MethodDetails = md;
|
||||
}
|
||||
Nodes[nodeBase.Guid] = nodeBase;
|
||||
|
||||
// 如果是触发器,则需要添加到专属集合中
|
||||
if (nodeControlType == NodeControlType.Flipflop && nodeBase is SingleFlipflopNode flipflopNode)
|
||||
{
|
||||
var guid = flipflopNode.Guid;
|
||||
if (!FlipflopNodes.Exists(it => it.Guid.Equals(guid)))
|
||||
{
|
||||
FlipflopNodes.Add(flipflopNode);
|
||||
}
|
||||
}
|
||||
|
||||
var nodeModel = CreateNode(nodeControlType, methodDetails);
|
||||
TryAddNode(nodeModel);
|
||||
// 通知UI更改
|
||||
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeBase));
|
||||
OnNodeCreate?.Invoke(new NodeCreateEventArgs(nodeModel, position));
|
||||
// 因为需要UI先布置了元素,才能通知UI变更特效
|
||||
// 如果不存在流程起始控件,默认设置为流程起始控件
|
||||
if (StartNode is null)
|
||||
{
|
||||
SetStartNode(nodeBase);
|
||||
SetStartNode(nodeModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 移除节点
|
||||
/// </summary>
|
||||
@@ -452,12 +504,19 @@ namespace Serein.NodeFlow
|
||||
/// </summary>
|
||||
public bool TryGetMethodDetails(string name, out MethodDetails? md)
|
||||
{
|
||||
md = MethodDetailss.FirstOrDefault(it => it.MethodName == name);
|
||||
if (md == null)
|
||||
var isPass = false;
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
md = MethodDetailss.FirstOrDefault(it => it.MethodName == name);
|
||||
return md != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
md = null;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -531,6 +590,65 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建节点
|
||||
/// </summary>
|
||||
/// <param name="nodeBase"></param>
|
||||
private NodeModelBase CreateNode(NodeControlType nodeControlType,MethodDetails? methodDetails = null)
|
||||
{
|
||||
// 确定创建的节点类型
|
||||
Type? nodeType = nodeControlType switch
|
||||
{
|
||||
NodeControlType.Action => typeof(SingleActionNode),
|
||||
NodeControlType.Flipflop => typeof(SingleFlipflopNode),
|
||||
|
||||
NodeControlType.ExpOp => typeof(SingleExpOpNode),
|
||||
NodeControlType.ExpCondition => typeof(SingleConditionNode),
|
||||
NodeControlType.ConditionRegion => typeof(CompositeConditionNode),
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (nodeType == null)
|
||||
{
|
||||
throw new Exception($"节点类型错误[{nodeControlType}]");
|
||||
}
|
||||
// 生成实例
|
||||
var nodeObj = Activator.CreateInstance(nodeType);
|
||||
if (nodeObj is not NodeModelBase nodeBase)
|
||||
{
|
||||
throw new Exception($"无法创建目标节点类型的实例[{nodeControlType}]");
|
||||
}
|
||||
|
||||
// 配置基础的属性
|
||||
nodeBase.ControlType = nodeControlType;
|
||||
if (methodDetails != null)
|
||||
{
|
||||
var md = methodDetails.Clone();
|
||||
nodeBase.DisplayName = md.MethodTips;
|
||||
nodeBase.MethodDetails = md;
|
||||
}
|
||||
|
||||
// 如果是触发器,则需要添加到专属集合中
|
||||
if (nodeControlType == NodeControlType.Flipflop && nodeBase is SingleFlipflopNode flipflopNode)
|
||||
{
|
||||
var guid = flipflopNode.Guid;
|
||||
if (!FlipflopNodes.Exists(it => it.Guid.Equals(guid)))
|
||||
{
|
||||
FlipflopNodes.Add(flipflopNode);
|
||||
}
|
||||
}
|
||||
|
||||
return nodeBase;
|
||||
}
|
||||
|
||||
private bool TryAddNode(NodeModelBase nodeModel)
|
||||
{
|
||||
nodeModel.Guid ??= Guid.NewGuid().ToString();
|
||||
Nodes[nodeModel.Guid] = nodeModel;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接节点
|
||||
/// </summary>
|
||||
@@ -583,7 +701,6 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
|
||||
|
||||
|
||||
fromNode.SuccessorNodes[connectionType].Add(toNode); // 添加到起始节点的子分支
|
||||
toNode.PreviousNodes[connectionType].Add(fromNode); // 添加到目标节点的父分支
|
||||
OnNodeConnectChange?.Invoke(new NodeConnectChangeEventArgs(fromNode.Guid,
|
||||
@@ -603,6 +720,9 @@ namespace Serein.NodeFlow
|
||||
StartNode = newStartNode;
|
||||
OnStartNodeChange?.Invoke(new StartNodeChangeEventArgs(oldNodeGuid, StartNode.Guid));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 网络交互
|
||||
@@ -633,6 +753,42 @@ namespace Serein.NodeFlow
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static Type? ControlTypeToModel(this NodeControlType nodeControlType )
|
||||
{
|
||||
// 确定创建的节点类型
|
||||
Type? nodeType = nodeControlType switch
|
||||
{
|
||||
NodeControlType.Action => typeof(SingleActionNode),
|
||||
NodeControlType.Flipflop => typeof(SingleFlipflopNode),
|
||||
|
||||
NodeControlType.ExpOp => typeof(SingleExpOpNode),
|
||||
NodeControlType.ExpCondition => typeof(SingleConditionNode),
|
||||
NodeControlType.ConditionRegion => typeof(CompositeConditionNode),
|
||||
_ => null
|
||||
};
|
||||
return nodeType;
|
||||
}
|
||||
public static NodeControlType ModelToControlType(this NodeControlType nodeControlType)
|
||||
{
|
||||
var type = nodeControlType.GetType();
|
||||
NodeControlType controlType = type switch
|
||||
{
|
||||
Type when type == typeof(SingleActionNode) => NodeControlType.Action,
|
||||
Type when type == typeof(SingleFlipflopNode) => NodeControlType.Flipflop,
|
||||
|
||||
Type when type == typeof(SingleExpOpNode) => NodeControlType.ExpOp,
|
||||
Type when type == typeof(SingleConditionNode) => NodeControlType.ExpCondition,
|
||||
Type when type == typeof(CompositeConditionNode) => NodeControlType.ConditionRegion,
|
||||
_ => NodeControlType.None,
|
||||
};
|
||||
return controlType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static bool NotExitPreviousNode(this SingleFlipflopNode node)
|
||||
{
|
||||
ConnectionType[] ct = [ConnectionType.IsSucceed,
|
||||
|
||||
@@ -70,12 +70,12 @@ namespace Serein.NodeFlow
|
||||
/// </summary>
|
||||
/// <param name="startNode">起始节点</param>
|
||||
/// <param name="env">运行环境</param>
|
||||
/// <param name="runMd">环境中已加载的所有节点方法</param>
|
||||
/// <param name="runNodeMd">环境中已加载的所有节点方法</param>
|
||||
/// <param name="flipflopNodes">触发器节点</param>
|
||||
/// <returns></returns>
|
||||
public async Task RunAsync(NodeModelBase startNode,
|
||||
IFlowEnvironment env,
|
||||
List<MethodDetails> runMd,
|
||||
List<MethodDetails> runNodeMd,
|
||||
List<MethodDetails> initMethods,
|
||||
List<MethodDetails> loadingMethods,
|
||||
List<MethodDetails> exitMethods,
|
||||
@@ -89,6 +89,8 @@ namespace Serein.NodeFlow
|
||||
return;
|
||||
}
|
||||
|
||||
#region 选择运行环境的上下文
|
||||
|
||||
// 判断使用哪一种流程上下文
|
||||
var isNetFramework = true;
|
||||
if (isNetFramework)
|
||||
@@ -99,23 +101,38 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
Context = new Serein.Library.Core.NodeFlow.DynamicContext(SereinIOC, env);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 初始化运行环境的Ioc容器
|
||||
// 清除节点使用的对象
|
||||
foreach (var nodeMd in runMd)
|
||||
var thisRuningMds = new List<MethodDetails>();
|
||||
thisRuningMds.AddRange(runNodeMd);
|
||||
thisRuningMds.AddRange(initMethods);
|
||||
thisRuningMds.AddRange(loadingMethods);
|
||||
thisRuningMds.AddRange(exitMethods);
|
||||
|
||||
// .AddRange(initMethods).AddRange(loadingMethods).a
|
||||
foreach (var nodeMd in thisRuningMds)
|
||||
{
|
||||
nodeMd.ActingInstance = null;
|
||||
}
|
||||
|
||||
SereinIOC.Reset(); // 开始运行时清空ioc中注册的实例
|
||||
// 初始化ioc容器中的类型对象
|
||||
foreach (var md in runMd)
|
||||
foreach (var md in thisRuningMds)
|
||||
{
|
||||
SereinIOC.Register(md.ActingInstanceType);
|
||||
if(md.ActingInstanceType != null)
|
||||
{
|
||||
SereinIOC.Register(md.ActingInstanceType);
|
||||
}
|
||||
}
|
||||
SereinIOC.Build();
|
||||
foreach (var md in runMd)
|
||||
SereinIOC.Build(); // 流程启动前的初始化
|
||||
foreach (var md in thisRuningMds)
|
||||
{
|
||||
md.ActingInstance = SereinIOC.GetOrInstantiate(md.ActingInstanceType);
|
||||
if (md.ActingInstanceType != null)
|
||||
{
|
||||
md.ActingInstance = SereinIOC.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
}
|
||||
}
|
||||
|
||||
//foreach (var md in flipflopNodes.Select(it => it.MethodDetails).ToArray())
|
||||
@@ -124,23 +141,36 @@ namespace Serein.NodeFlow
|
||||
//}
|
||||
#endregion
|
||||
|
||||
|
||||
#region 创建Node中初始化、加载时、退出时调用的方法
|
||||
|
||||
#region 检查并修正初始化、加载时、退出时方法作用的对象,保证后续不会报错
|
||||
foreach (var md in initMethods) // 初始化
|
||||
{
|
||||
md.ActingInstance ??= Context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
|
||||
md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
}
|
||||
foreach (var md in loadingMethods) // 加载
|
||||
{
|
||||
md.ActingInstance ??= Context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
|
||||
md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
}
|
||||
foreach (var md in exitMethods) // 初始化
|
||||
{
|
||||
md.ActingInstance ??= Context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
|
||||
md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 执行初始化,绑定IOC容器,再执行加载时,设置流程退出时的回调函数
|
||||
|
||||
object?[]? args = [Context];
|
||||
foreach (var md in initMethods) // 初始化
|
||||
{
|
||||
object?[]? data = [md.ActingInstance, args];
|
||||
md.MethodDelegate.DynamicInvoke(data);
|
||||
}
|
||||
Context.SereinIoc.Build(); // 绑定初始化时注册的类型
|
||||
foreach (var md in loadingMethods) // 加载
|
||||
{
|
||||
object?[]? data = [md.ActingInstance, args];
|
||||
md.MethodDelegate.DynamicInvoke(data);
|
||||
}
|
||||
Context.SereinIoc.Build(); // 预防有人在加载时才注册类型,再绑定一次
|
||||
ExitAction = () =>
|
||||
{
|
||||
foreach (MethodDetails? md in exitMethods)
|
||||
@@ -159,34 +189,18 @@ namespace Serein.NodeFlow
|
||||
FlowState = RunState.Completion;
|
||||
FlipFlopState = RunState.Completion;
|
||||
};
|
||||
Context.SereinIoc.Build();
|
||||
#endregion
|
||||
|
||||
#region 执行初始化,然后绑定IOC容器,再执行加载时
|
||||
|
||||
foreach (var md in initMethods) // 初始化 - 调用方法
|
||||
{
|
||||
object?[]? data = [md.ActingInstance, args];
|
||||
md.MethodDelegate.DynamicInvoke(data);
|
||||
}
|
||||
Context.SereinIoc.Build();
|
||||
foreach (var md in loadingMethods) // 加载
|
||||
{
|
||||
object?[]? data = [md.ActingInstance, args];
|
||||
md.MethodDelegate.DynamicInvoke(data);
|
||||
}
|
||||
#endregion
|
||||
#region 开始启动流程
|
||||
|
||||
|
||||
// 节点任务的启动
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
if (flipflopNodes.Count > 0)
|
||||
{
|
||||
FlipFlopState = RunState.Running;
|
||||
// 如果存在需要启动的触发器,则开始启动
|
||||
FlipFlopCts = SereinIOC.GetOrInstantiate<NodeRunCts>();
|
||||
FlipFlopCts = SereinIOC.GetOrRegisterInstantiate<NodeRunCts>();
|
||||
// 使用 TaskCompletionSource 创建未启动的触发器任务
|
||||
var tasks = flipflopNodes.Select(async node =>
|
||||
{
|
||||
@@ -196,7 +210,7 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
await startNode.StartExecution(Context);
|
||||
// 等待结束
|
||||
if(FlipFlopCts != null)
|
||||
if (FlipFlopCts != null)
|
||||
{
|
||||
while (!FlipFlopCts.IsCancellationRequested)
|
||||
{
|
||||
@@ -207,7 +221,8 @@ namespace Serein.NodeFlow
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Out.WriteLineAsync(ex.ToString());
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -228,7 +243,7 @@ namespace Serein.NodeFlow
|
||||
object?[]? parameters = singleFlipFlopNode.GetParameters(context, md);
|
||||
// 调用委托并获取结果
|
||||
|
||||
md.ActingInstance = context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
|
||||
md.ActingInstance = context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
|
||||
IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);
|
||||
|
||||
|
||||
@@ -19,13 +19,13 @@ namespace Serein.NodeFlow.Model
|
||||
ActionNodes = actionNodes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override Parameterdata[] GetParameterdatas()
|
||||
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
public override NodeInfo ToInfo()
|
||||
internal override NodeInfo ToInfo()
|
||||
{
|
||||
if (MethodDetails == null) return null;
|
||||
|
||||
@@ -35,8 +35,8 @@ namespace Serein.NodeFlow.Model
|
||||
//var errorNodes = ErrorBranch.Select(item => item.Guid);// 异常分支
|
||||
var trueNodes = SuccessorNodes[ConnectionType.IsSucceed].Select(item => item.Guid); // 真分支
|
||||
var falseNodes = SuccessorNodes[ConnectionType.IsFail].Select(item => item.Guid);// 假分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 上游分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 异常分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 异常分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 上游分支
|
||||
// 生成参数列表
|
||||
Parameterdata[] parameterData = GetParameterdatas();
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace Serein.NodeFlow.Model
|
||||
UpstreamNodes = upstreamNodes.ToArray(),
|
||||
ParameterData = parameterData.ToArray(),
|
||||
ErrorNodes = errorNodes.ToArray(),
|
||||
ChildNodes = ActionNodes.Select(node => node.ToInfo()).ToArray(),
|
||||
ChildNodeGuids = ActionNodes.Select(node => node.Guid).ToArray(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,14 +61,14 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
}
|
||||
|
||||
public override Parameterdata[] GetParameterdatas()
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public override NodeInfo ToInfo()
|
||||
internal override NodeInfo ToInfo()
|
||||
{
|
||||
if (MethodDetails == null) return null;
|
||||
//if (MethodDetails == null) return null;
|
||||
|
||||
//var trueNodes = SucceedBranch.Select(item => item.Guid); // 真分支
|
||||
//var falseNodes = FailBranch.Select(item => item.Guid);// 假分支
|
||||
@@ -76,8 +76,8 @@ namespace Serein.NodeFlow.Model
|
||||
//var errorNodes = ErrorBranch.Select(item => item.Guid);// 异常分支
|
||||
var trueNodes = SuccessorNodes[ConnectionType.IsSucceed].Select(item => item.Guid); // 真分支
|
||||
var falseNodes = SuccessorNodes[ConnectionType.IsFail].Select(item => item.Guid);// 假分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 上游分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 异常分支
|
||||
var errorNodes = SuccessorNodes[ConnectionType.IsError].Select(item => item.Guid);// 异常分支
|
||||
var upstreamNodes = SuccessorNodes[ConnectionType.Upstream].Select(item => item.Guid);// 上游分支
|
||||
|
||||
// 生成参数列表
|
||||
Parameterdata[] parameterData = GetParameterdatas();
|
||||
@@ -93,7 +93,7 @@ namespace Serein.NodeFlow.Model
|
||||
UpstreamNodes = upstreamNodes.ToArray(),
|
||||
ParameterData = parameterData.ToArray(),
|
||||
ErrorNodes = errorNodes.ToArray(),
|
||||
ChildNodes = ConditionNodes.Select(node => node.ToInfo()).ToArray(),
|
||||
ChildNodeGuids = ConditionNodes.Select(node => node.Guid).ToArray(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -64,15 +64,15 @@ namespace Serein.NodeFlow.Model
|
||||
// context.SetFlowData(result);
|
||||
// }
|
||||
//}
|
||||
public override Parameterdata[] GetParameterdatas()
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
if (base.MethodDetails.ExplicitDatas.Length > 0)
|
||||
{
|
||||
return MethodDetails.ExplicitDatas
|
||||
.Select(it => new Parameterdata
|
||||
{
|
||||
state = it.IsExplicitData,
|
||||
value = it.DataValue,
|
||||
State = it.IsExplicitData,
|
||||
Value = it.DataValue,
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
@@ -55,31 +55,43 @@ namespace Serein.NodeFlow.Model
|
||||
return result;
|
||||
}
|
||||
|
||||
public override Parameterdata[] GetParameterdatas()
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
if (base.MethodDetails.ExplicitDatas.Length > 0)
|
||||
var value = CustomData switch
|
||||
{
|
||||
return MethodDetails.ExplicitDatas
|
||||
.Select(it => new Parameterdata
|
||||
{
|
||||
state = IsCustomData,
|
||||
expression = Expression,
|
||||
value = CustomData switch
|
||||
{
|
||||
Type when CustomData.GetType() == typeof(int)
|
||||
&& CustomData.GetType() == typeof(double)
|
||||
&& CustomData.GetType() == typeof(float)
|
||||
=> ((double)CustomData).ToString(),
|
||||
Type when CustomData.GetType() == typeof(bool) => ((bool)CustomData).ToString(),
|
||||
_ => CustomData?.ToString()!,
|
||||
}
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
else
|
||||
Type when CustomData.GetType() == typeof(int)
|
||||
&& CustomData.GetType() == typeof(double)
|
||||
&& CustomData.GetType() == typeof(float)
|
||||
=> ((double)CustomData).ToString(),
|
||||
Type when CustomData.GetType() == typeof(bool) => ((bool)CustomData).ToString(),
|
||||
_ => CustomData?.ToString()!,
|
||||
};
|
||||
return [new Parameterdata
|
||||
{
|
||||
return [];
|
||||
State = IsCustomData,
|
||||
Expression = Expression,
|
||||
Value = value,
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal override NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||
{
|
||||
var node = this;
|
||||
if (node != null)
|
||||
{
|
||||
node.Guid = nodeInfo.Guid;
|
||||
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
|
||||
{
|
||||
Parameterdata? pd = nodeInfo.ParameterData[i];
|
||||
node.IsCustomData = pd.State;
|
||||
node.CustomData = pd.Value;
|
||||
node.Expression = pd.Expression;
|
||||
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
//public override void Execute(DynamicContext context)
|
||||
|
||||
@@ -48,23 +48,25 @@ namespace Serein.NodeFlow.Model
|
||||
|
||||
}
|
||||
|
||||
public override Parameterdata[] GetParameterdatas()
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
if (base.MethodDetails.ExplicitDatas.Length > 0)
|
||||
return [new Parameterdata{ Expression = Expression}];
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal override NodeModelBase LoadInfo(NodeInfo nodeInfo)
|
||||
{
|
||||
var node = this;
|
||||
if (node != null)
|
||||
{
|
||||
return MethodDetails.ExplicitDatas
|
||||
.Select(it => new Parameterdata
|
||||
{
|
||||
state = it.IsExplicitData,
|
||||
// value = it.DataValue,
|
||||
expression = Expression,
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
return [];
|
||||
node.Guid = nodeInfo.Guid;
|
||||
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
|
||||
{
|
||||
node.Expression = nodeInfo.ParameterData[i].Expression;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,15 @@ namespace Serein.NodeFlow.Model
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Parameterdata[] GetParameterdatas()
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
{
|
||||
if (base.MethodDetails.ExplicitDatas.Length > 0)
|
||||
{
|
||||
return MethodDetails.ExplicitDatas
|
||||
.Select(it => new Parameterdata
|
||||
{
|
||||
state = it.IsExplicitData,
|
||||
value = it.DataValue
|
||||
State = it.IsExplicitData,
|
||||
Value = it.DataValue
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
16
NodeFlow/NodeStaticConfig.cs
Normal file
16
NodeFlow/NodeStaticConfig.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
public static class NodeStaticConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点的命名空间
|
||||
/// </summary>
|
||||
public const string NodeSpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,8 @@ namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||
public override bool Evaluate(object? obj)
|
||||
{
|
||||
//object? memberValue = GetMemberValue(obj, MemberPath);
|
||||
|
||||
|
||||
if (TargetObj is T typedObj)
|
||||
{
|
||||
return new ValueTypeConditionResolver<T>
|
||||
|
||||
@@ -18,7 +18,16 @@ namespace Serein.NodeFlow.Tool.SereinExpression.Resolver
|
||||
|
||||
public override bool Evaluate(object obj)
|
||||
{
|
||||
object memberValue = GetMemberValue(obj, MemberPath);
|
||||
object memberValue;
|
||||
if (!string.IsNullOrWhiteSpace(MemberPath))
|
||||
{
|
||||
memberValue = GetMemberValue(obj, MemberPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
memberValue = obj;
|
||||
}
|
||||
|
||||
if (memberValue is string strObj)
|
||||
{
|
||||
return new StringConditionResolver
|
||||
|
||||
@@ -12,7 +12,9 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
try
|
||||
{
|
||||
|
||||
return ConditionParse(data, expression).Evaluate(data);
|
||||
var parse = ConditionParse(data, expression);
|
||||
var result = parse.Evaluate(data);
|
||||
return result;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -24,7 +26,7 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
|
||||
public static SereinConditionResolver ConditionParse(object data, string expression)
|
||||
{
|
||||
if (expression.StartsWith('.')) // 表达式前缀属于从上一个节点数据对象获取成员值
|
||||
if (expression.StartsWith('.') || expression.StartsWith('<')) // 表达式前缀属于从上一个节点数据对象获取成员值
|
||||
{
|
||||
return ParseObjectExpression(data, expression);
|
||||
}
|
||||
@@ -128,9 +130,6 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
operatorStr = parts[0].ToLower(); // 操作类型
|
||||
valueStr = string.Join(' ', parts.Skip(1)); // 表达式值
|
||||
}
|
||||
|
||||
targetObj = GetMemberValue(data, memberPath);// 获取对象成员,作为表达式的目标对象
|
||||
|
||||
Type? tempType = typeStr switch
|
||||
{
|
||||
"int" => typeof(int),
|
||||
@@ -140,6 +139,15 @@ namespace Serein.NodeFlow.Tool.SereinExpression
|
||||
_ => Type.GetType(typeStr)
|
||||
};
|
||||
type = tempType ?? throw new ArgumentException("对象表达式无效的类型声明");
|
||||
if (string.IsNullOrWhiteSpace(memberPath))
|
||||
{
|
||||
targetObj = Convert.ChangeType(data, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetObj = GetMemberValue(data, memberPath);// 获取对象成员,作为表达式的目标对象
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#region 解析类型 int
|
||||
|
||||
Reference in New Issue
Block a user