忘记改啥了*1

This commit is contained in:
fengjiayi
2024-11-02 16:48:40 +08:00
parent 0088d32f12
commit cd1642dcf7
45 changed files with 1022 additions and 447 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2,6 +2,7 @@
using Serein.Library.Api;
using Serein.Library.FlowNode;
using Serein.Library.Utils;
using Serein.NodeFlow.Tool;
namespace Serein.NodeFlow.Env
{
@@ -426,6 +427,30 @@ namespace Serein.NodeFlow.Env
#region
/// <summary>
/// 运行时加载
/// </summary>
/// <param name="file">文件名</param>
/// <returns></returns>
public bool LoadNativeLibraryOfRuning(string file)
{
return currentFlowEnvironment.LoadNativeLibraryOfRuning(file);
}
/// <summary>
/// 运行时加载指定目录下的类库
/// </summary>
/// <param name="path">目录</param>
/// <param name="isRecurrence">是否递归加载</param>
public void LoadAllNativeLibraryOfRuning(string path, bool isRecurrence = true)
{
currentFlowEnvironment.LoadAllNativeLibraryOfRuning(path,isRecurrence);
}
#endregion
#region IOC容器
public ISereinIOC Build()
{

View File

@@ -55,7 +55,7 @@ namespace Serein.NodeFlow.Env
var md = methodDetails.CloneOfNode(nodeModel.Env, nodeModel);
nodeModel.DisplayName = md.MethodAnotherName;
nodeModel.MethodDetails = md;
nodeModel.OnLoading();
nodeModel.OnCreating();
return nodeModel;
}

View File

@@ -835,5 +835,31 @@ namespace Serein.NodeFlow.Env
});
}
#region
/// <summary>
/// 运行时加载
/// </summary>
/// <param name="file">文件名</param>
/// <returns></returns>
public bool LoadNativeLibraryOfRuning(string file)
{
Console.WriteLine("远程环境尚未实现的接口LoadNativeLibraryOfRuning");
return false;
}
/// <summary>
/// 运行时加载指定目录下的类库
/// </summary>
/// <param name="path">目录</param>
/// <param name="isRecurrence">是否递归加载</param>
public void LoadAllNativeLibraryOfRuning(string path, bool isRecurrence = true)
{
Console.WriteLine("远程环境尚未实现的接口LoadAllNativeLibraryOfRuning");
}
#endregion
}
}

View File

@@ -1,10 +1,12 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Core;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Utils;
using Serein.Library.Web;
using Serein.NodeFlow.Env;
using Serein.NodeFlow.Model;
using Serein.NodeFlow.Tool;
using System.Collections.Concurrent;
namespace Serein.NodeFlow
@@ -48,9 +50,9 @@ namespace Serein.NodeFlow
{
IDynamicContext context;
#if NET6_0_OR_GREATER
context = new Serein.Library.Core.NodeFlow.DynamicContext(env); // 从起始节点启动流程时创建上下文
context = new Serein.Library.Core.DynamicContext(env); // 从起始节点启动流程时创建上下文
#else
Context = new Serein.Library.Framework.NodeFlow.DynamicContext(env);
Context = new Serein.Library.Framework.DynamicContext(env);
#endif
await startNode.StartFlowAsync(context); // 开始运行时从选定节点开始运行
context.Exit();
@@ -112,9 +114,9 @@ namespace Serein.NodeFlow
// 判断使用哪一种流程上下文
IDynamicContext Context;
#if NET6_0_OR_GREATER
Context = new Serein.Library.Core.NodeFlow.DynamicContext(env); // 从起始节点启动流程时创建上下文
Context = new Serein.Library.Core.DynamicContext(env); // 从起始节点启动流程时创建上下文
#else
Context = new Serein.Library.Framework.NodeFlow.DynamicContext(env);
Context = new Serein.Library.Framework.DynamicContext(env);
#endif
#endregion
@@ -233,13 +235,19 @@ namespace Serein.NodeFlow
await dd.InvokeAsync(md.ActingInstance, [Context]);
}
TerminateAllGlobalFlipflop();
if (_flipFlopCts != null && !_flipFlopCts.IsCancellationRequested)
{
_flipFlopCts?.Cancel();
_flipFlopCts?.Dispose();
}
} // 通知所有流程上下文停止运行
TerminateAllGlobalFlipflop(); // 确保所有触发器不再运行
NativeDllHelper.FreeLibrarys(); // 卸载所有已加载的 Native Dll
//NativeDllHelper.FreeLibrarys(); // 卸载所有已加载的 Native Dll
env.FlowState = RunState.Completion;
env.FlipFlopState = RunState.Completion;
@@ -379,8 +387,8 @@ namespace Serein.NodeFlow
await Console.Out.WriteLineAsync($"[{nextNodes[i].MethodDetails.MethodName}]中断已{cancelType},开始执行后继分支");
}
await nextNodes[i].StartFlowAsync(context); // 启动执行触发器后继分支的节点
context.Exit();
}
context.Exit();
});
}

View File

@@ -32,7 +32,7 @@ namespace Serein.NodeFlow.Model
/// <summary>
/// 加载完成后调用的方法
/// </summary>
public override void OnLoading()
public override void OnCreating()
{
Console.WriteLine("CompositeConditionNode 暂未实现 OnLoading");
}

View File

@@ -17,9 +17,9 @@ namespace Serein.NodeFlow.Model
/// <summary>
/// 加载完成后调用的方法
/// </summary>
public override void OnLoading()
public override void OnCreating()
{
Console.WriteLine("SingleActionNode 暂未实现 OnLoading");
// Console.WriteLine("SingleActionNode 暂未实现 OnLoading");
}
public override ParameterData[] GetParameterdatas()

View File

@@ -1,5 +1,6 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using System.ComponentModel;
@@ -22,14 +23,13 @@ namespace Serein.NodeFlow.Model
/// 自定义参数值
/// </summary>
[PropertyInfo(IsNotification = true)]
private object? _customData;
private string? _customData;
/// <summary>
/// 条件表达式
/// </summary>
[PropertyInfo(IsNotification = true)]
private string _expression;
}
public partial class SingleConditionNode : NodeModelBase
@@ -39,32 +39,9 @@ namespace Serein.NodeFlow.Model
this.IsCustomData = false;
this.CustomData = null;
this.Expression = "PASS";
}
/// <summary>
/// 加载完成后调用的方法
/// </summary>
public override void OnLoading()
{
var pd = new ParameterDetails
{
Index = 0,
Name = "Exp",
DataType = typeof(object),
ExplicitType = typeof(object),
IsExplicitData = false,
DataValue = string.Empty,
ArgDataSourceNodeGuid = string.Empty,
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
NodeModel = this,
Convertor = null,
ExplicitTypeName = "Value",
Items = Array.Empty<string>(),
};
this.MethodDetails.ParameterDetailss = new ParameterDetails[] { pd };
}
/// <summary>
@@ -138,36 +115,102 @@ namespace Serein.NodeFlow.Model
return result;
}
public override ParameterData[] GetParameterdatas()
{
var 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()!,
};
return [new ParameterData
{
State = IsCustomData,
Expression = Expression,
Value = value,
}];
var pd1 = MethodDetails.ParameterDetailss[0];
var pd2 = MethodDetails.ParameterDetailss[1];
var pd3 = MethodDetails.ParameterDetailss[2];
return [
new ParameterData // 保存表达式
{
Value = Expression ,
SourceNodeGuid = pd1.ArgDataSourceNodeGuid,
SourceType = pd1.ArgDataSourceType.ToString(),
},
new ParameterData // 保存自定义参数
{
Value = CustomData?.ToString() ,
SourceNodeGuid = pd2.ArgDataSourceNodeGuid,
SourceType = pd2.ArgDataSourceType.ToString(),
},
new ParameterData // 参数来源状态
{
Value = IsCustomData.ToString() ,
SourceNodeGuid = pd3.ArgDataSourceNodeGuid,
SourceType = pd3.ArgDataSourceType.ToString(),
}];
}
public override void OnCreating()
{
// 自定义节点初始化默认的参数实体
var tmpParameterDetails = new ParameterDetails[3];
for (int index = 0; index <= 2; index++)
{
tmpParameterDetails[index] = new ParameterDetails
{
Index = index,
IsExplicitData = false,
DataValue = string.Empty,
ArgDataSourceNodeGuid = string.Empty,
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
NodeModel = this,
Convertor = null,
ExplicitTypeName = "Value",
Items = Array.Empty<string>(),
};
}
var pd1 = tmpParameterDetails[0]; // 表达式
var pd2 = tmpParameterDetails[1]; // 自定义参数
var pd3 = tmpParameterDetails[2]; // 参数来源
// 表达式
pd1.Name = nameof(Expression);
pd1.DataType = typeof(string);
pd1.ExplicitType = typeof(string);
// 自定义参数
pd2.Name = nameof(CustomData);
pd2.DataType = typeof(string);
pd2.ExplicitType = typeof(string);
// 参数来源
pd3.Name = nameof(IsCustomData);
pd3.DataType = typeof(bool);
pd3.ExplicitType = typeof(bool);
//this.MethodDetails.ParameterDetailss = new ParameterDetails[2] { pd1, pd2 };
this.MethodDetails.ParameterDetailss = [..tmpParameterDetails];
}
public override NodeModelBase LoadInfo(NodeInfo nodeInfo)
{
var node = this;
node.Guid = nodeInfo.Guid;
this.Guid = nodeInfo.Guid;
this.Position = nodeInfo.Position;// 加载位置信息
var pdInfo1 = nodeInfo.ParameterData[0];
this.Expression = pdInfo1.Value; // 加载表达式
var pdInfo2 = nodeInfo.ParameterData[1];
this.CustomData = pdInfo2.Value; // 加载自定义参数信息
var pdInfo3 = nodeInfo.ParameterData[2];
bool.TryParse(pdInfo3.Value,out var @bool); // 参数来源状态
this.IsCustomData = @bool;
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;
var pd = this.MethodDetails.ParameterDetailss[i]; // 本节点的参数信息
ParameterData? pdInfo = nodeInfo.ParameterData[i]; // 项目文件的保存信息
pd.ArgDataSourceNodeGuid = pdInfo.SourceNodeGuid;
pd.ArgDataSourceType = EnumHelper.ConvertEnum<ConnectionArgSourceType>(pdInfo.SourceType);
}
return this;
}

View File

@@ -1,5 +1,6 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using System.Reactive;
using System.Reflection.Metadata;
@@ -32,14 +33,14 @@ namespace Serein.NodeFlow.Model
/// <summary>
/// 加载完成后调用的方法
/// </summary>
public override void OnLoading()
public override void OnCreating()
{
var pd = new ParameterDetails
{
Index = 0,
Name = "Exp",
DataType = typeof(object),
ExplicitType = typeof(object),
Name = nameof(Expression),
DataType = typeof(string),
ExplicitType = typeof(string),
IsExplicitData = false,
DataValue = string.Empty,
ArgDataSourceNodeGuid = string.Empty,
@@ -92,7 +93,7 @@ namespace Serein.NodeFlow.Model
}
context.NextOrientation = ConnectionInvokeType.IsSucceed;
return Task.FromResult(result);
return result;
}
catch (Exception ex)
{
@@ -105,7 +106,11 @@ namespace Serein.NodeFlow.Model
public override ParameterData[] GetParameterdatas()
{
return [new ParameterData { Expression = Expression }];
return [new ParameterData {
Value = Expression,
SourceNodeGuid = this.MethodDetails.ParameterDetailss[0].ArgDataSourceNodeGuid,
SourceType = this.MethodDetails.ParameterDetailss[0].ArgDataSourceType.ToString(),
}];
}
@@ -113,11 +118,17 @@ namespace Serein.NodeFlow.Model
public override NodeModelBase LoadInfo(NodeInfo nodeInfo)
{
var node = this;
this.Position = nodeInfo.Position;// 加载位置信息
node.Guid = nodeInfo.Guid;
this.Position = nodeInfo.Position;// 加载位置信息
var pdInfo1 = nodeInfo.ParameterData[0];
node.Expression = pdInfo1.Value; // 加载表达式
for (int i = 0; i < nodeInfo.ParameterData.Length; i++)
{
node.Expression = nodeInfo.ParameterData[i].Expression;
ParameterData? pd = nodeInfo.ParameterData[i];
node.MethodDetails.ParameterDetailss[i].ArgDataSourceNodeGuid = pd.SourceNodeGuid;
node.MethodDetails.ParameterDetailss[i].ArgDataSourceType = EnumHelper.ConvertEnum<ConnectionArgSourceType>(pd.SourceType);
}
return this;
}

View File

@@ -19,9 +19,9 @@ namespace Serein.NodeFlow.Model
/// <summary>
/// 加载完成后调用的方法
/// </summary>
public override void OnLoading()
public override void OnCreating()
{
Console.WriteLine("SingleFlipflopNode 暂未实现 OnLoading");
// Console.WriteLine("SingleFlipflopNode 暂未实现 OnLoading");
}
@@ -50,16 +50,18 @@ namespace Serein.NodeFlow.Model
object instance = md.ActingInstance;
try
{
var args = await GetParametersAsync(context, this, md);
var result = await dd.InvokeAsync(md.ActingInstance, args);
dynamic flipflopContext = result;
FlipflopStateType flipflopStateType = flipflopContext.State;
// 因为这里会返回不确定的泛型 IFlipflopContext<TRsult>
// 而我们只需要获取到 State 和 Value返回的数据
dynamic dynamicFlipflopContext = await dd.InvokeAsync(md.ActingInstance, args);
FlipflopStateType flipflopStateType = dynamicFlipflopContext.State;
context.NextOrientation = flipflopStateType.ToContentType();
if (flipflopContext.Type == TriggerType.Overtime)
if (dynamicFlipflopContext.Type == TriggerType.Overtime)
{
throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
}
return flipflopContext.Value;
return dynamicFlipflopContext.Value;
}
catch (FlipflopException ex)

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.18</Version>
<Version>1.0.19</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@@ -60,6 +60,7 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Serein.Library.MyGenerator\Serein.Library.NodeGenerator.csproj" OutputItemType="Analyzer" />

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Tool
{
/// <summary>
/// 管理加载在流程的程序集
/// </summary>
public class FlowLibraryLoader : AssemblyLoadContext
{
private Assembly _pluginAssembly;
/// <summary>
/// 加载程序集
/// </summary>
/// <param name="pluginPath"></param>
public FlowLibraryLoader(string pluginPath) : base(isCollectible: true)
{
_pluginAssembly = LoadFromAssemblyPath(pluginPath);
}
/// <summary>
/// 保持默认加载行为
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
protected override Assembly Load(AssemblyName assemblyName)
{
return null; // 保持默认加载行为
}
/// <summary>
/// 是否对程序集的引用
/// </summary>
public void UnloadPlugin()
{
_pluginAssembly = null; // 释放对程序集的引用
Unload(); // 触发卸载
// 强制进行垃圾回收,以便完成卸载
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}

View File

@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Tool
{
internal class NativeDllHelper
{
// 引入 Windows API 函数
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
// 引入 Unix/Linux 的动态库加载函数
[DllImport("libdl.so.2", SetLastError = true)]
private static extern IntPtr dlopen(string filename, int flag);
[DllImport("libdl.so.2", SetLastError = true)]
private static extern IntPtr dlsym(IntPtr handle, string symbol);
[DllImport("libdl.so.2", SetLastError = true)]
private static extern int dlclose(IntPtr handle);
private const int RTLD_NOW = 2;
// bool LoadDll(string file)
// void LoadAllDll(string path, bool isRecurrence = true);
private static List<IntPtr> Nints = new List<nint>();
/// <summary>
/// 加载单个Dll
/// </summary>
/// <param name="file"></param>
public static bool LoadDll(string file)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return LoadWindowsLibrarie(file);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return LoadLinuxLibrarie(file);
}
else
{
Console.WriteLine("Unsupported OS.");
return false;
}
}
public static void LoadAllDll(string path, bool isRecurrence = true)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
foreach (var file in Directory.GetFiles(path, "*.dll"))
{
LoadWindowsLibrarie(file);
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
foreach (var file in Directory.GetFiles(path, "*.so"))
{
LoadLinuxLibrarie(file);
}
}
else
{
Console.WriteLine("Unsupported OS.");
}
foreach (var dir in Directory.GetDirectories(path))
{
LoadAllDll(dir, true);
}
}
/// <summary>
/// 加载Windows类库
/// </summary>
/// <param name="path"></param>
/// <param name="isRecurrence">是否递归加载</param>
private static bool LoadWindowsLibrarie(string file)
{
IntPtr hModule = IntPtr.Zero;
try
{
hModule = LoadLibrary(file);
// 加载 DLL
if (hModule != IntPtr.Zero)
{
Nints.Add(hModule);
Console.WriteLine($"Loaded: {file}");
return true;
}
else
{
Console.WriteLine($"Failed to load {file}: {Marshal.GetLastWin32Error()}");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading {file}: {ex.Message}");
return false;
}
}
/// <summary>
/// 加载Linux类库
/// </summary>
/// <param name="file"></param>
private static bool LoadLinuxLibrarie(string file)
{
IntPtr handle = IntPtr.Zero;
try
{
handle = dlopen(file, RTLD_NOW);
if (handle != IntPtr.Zero)
{
Nints.Add(handle);
Console.WriteLine($"Loaded: {file}");
return true;
// 可以调用共享库中的函数
// IntPtr procAddress = dlsym(handle, "my_function");
}
else
{
Console.WriteLine($"Failed to load {file}: {Marshal.GetLastWin32Error()}");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading {file}: {ex.Message}");
return false;
}
}
/// <summary>
/// 卸载所有已加载DLL
/// </summary>
public static void FreeLibrarys()
{
for (int i = 0; i < Nints.Count; i++)
{
IntPtr hModule = Nints[i];
FreeLibrary(hModule);
}
Nints.Clear();
}
}
}

View File

@@ -174,11 +174,11 @@ public static class NodeMethodDetailsHelper
ConvertorInstance[key] = (instance, convertMethod);
}
Func<object, object> func = (enumValue) =>
object func(object enumValue)
{
(var obj,var methodInfo) = ConvertorInstance[key];
(var obj, var methodInfo) = ConvertorInstance[key];
return methodInfo?.Invoke(obj, [enumValue]);
};
}
// 确保实例实现了所需接口
ParameterDetails ed = GetExplicitDataOfParameter(it, index, paremType, true, func);
@@ -206,26 +206,29 @@ public static class NodeMethodDetailsHelper
}
private static ParameterDetails GetExplicitDataOfParameter(ParameterInfo parameterInfo,
int index,
Type paremType,
bool isExplicitData,
Func<object, object> func = null)
int index,
Type paremType,
bool isExplicitData,
Func<object, object> func = null)
{
bool hasParams = parameterInfo.IsDefined(typeof(ParamArrayAttribute)); // 判断是否为可变参数
string explicitTypeName = GetExplicitTypeName(paremType);
var items = GetExplicitItems(paremType, explicitTypeName);
if ("Bool".Equals(explicitTypeName)) explicitTypeName = "Select"; // 布尔值 转为 可选类型
return new ParameterDetails
{
IsExplicitData = isExplicitData, //attribute is null ? parameterInfo.HasDefaultValue : true,
Index = index,
ExplicitTypeName = explicitTypeName,
ExplicitType = paremType,
Convertor = func,
DataType = parameterInfo.ParameterType,
Index = index, // 索引
ExplicitTypeName = explicitTypeName, // Select/Bool/Value
ExplicitType = paremType,// 显示的入参类型
Convertor = func, // 转换器
DataType = parameterInfo.ParameterType, // 实际的入参类型
Name = parameterInfo.Name,
DataValue = parameterInfo.HasDefaultValue ? parameterInfo?.DefaultValue?.ToString() : "", // 如果存在默认值,则使用默认值
Items = items.ToArray(), // 如果是枚举值入参,则获取枚举类型的字面量
IsParams = hasParams, // 判断是否为可变参数
};
}