破坏性更新,移除了Core/Framework(对应的上下文类移动到了Library)

This commit is contained in:
fengjiayi
2025-01-22 21:09:52 +08:00
parent 652707f980
commit eb1505596a
68 changed files with 1034 additions and 2600 deletions

View File

@@ -1,33 +1,16 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Core;
using Serein.Library.FlowNode;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
using Serein.NodeFlow.Model;
using Serein.NodeFlow.Tool;
using Serein.Script.Node;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Reactive;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.Loader;
using System.Security.AccessControl;
using System.Text;
using System.Xml.Linq;
namespace Serein.NodeFlow.Env
{
/// <summary>
/// 运行环境
/// </summary>
@@ -44,7 +27,7 @@ namespace Serein.NodeFlow.Env
/// <summary>
/// 流程运行环境
/// </summary>
public FlowEnvironment(UIContextOperation uiContextOperation)
public FlowEnvironment()
{
this.sereinIOC = new SereinIOC();
this.IsGlobalInterrupt = false;
@@ -57,7 +40,6 @@ namespace Serein.NodeFlow.Env
}
};
this.UIContextOperation = uiContextOperation; // 为加载的类库提供在UI线程上执行某些操作的封装工具类
this.FlowLibraryManagement = new FlowLibraryManagement(this); // 实例化类库管理
#region
@@ -74,7 +56,6 @@ namespace Serein.NodeFlow.Env
PersistennceInstance.Add(typeof(FlowInterruptTool).FullName, new FlowInterruptTool()); // 缓存流程实例
PersistennceInstance.Add(typeof(IFlowEnvironment).FullName, (FlowEnvironment)this); // 缓存流程实例
PersistennceInstance.Add(typeof(ISereinIOC).FullName, this); // 缓存容器服务
PersistennceInstance.Add(typeof(UIContextOperation).FullName, uiContextOperation); // 缓存封装好的UI线程上下文
ReRegisterPersistennceInstance();
@@ -1242,6 +1223,21 @@ namespace Serein.NodeFlow.Env
return FlowLibraryManagement.TryGetDelegateDetails(assemblyName, methodName, out delegateDetails);
}
/// <summary>
/// 设置在UI线程操作的线程上下文
/// </summary>
/// <param name="uiContextOperation"></param>
public void SetUIContextOperation(UIContextOperation uiContextOperation)
{
this.UIContextOperation = uiContextOperation;
var fullName = typeof(UIContextOperation).FullName;
if (!string.IsNullOrEmpty(fullName))
{
PersistennceInstance[fullName] = uiContextOperation; // 缓存封装好的UI线程上下文
}
}
/// <summary>
/// 移动了某个节点(远程插件使用)
@@ -1401,6 +1397,8 @@ namespace Serein.NodeFlow.Env
#endregion
#region

View File

@@ -12,9 +12,9 @@ namespace Serein.NodeFlow.Env
/// </summary>
public class FlowEnvironmentDecorator : IFlowEnvironment, IFlowEnvironmentEvent, ISereinIOC
{
public FlowEnvironmentDecorator(UIContextOperation uiContextOperation)
public FlowEnvironmentDecorator()
{
flowEnvironment = new FlowEnvironment(uiContextOperation);
flowEnvironment = new FlowEnvironment();
// 默认使用本地环境
currentFlowEnvironment = flowEnvironment;
currentFlowEnvironmentEvent = flowEnvironment;
@@ -511,7 +511,14 @@ namespace Serein.NodeFlow.Env
{
currentFlowEnvironment.TriggerInterrupt(nodeGuid, expression, type);
}
/// <summary>
/// 设置在UI线程操作的线程上下文
/// </summary>
/// <param name="uiContextOperation"></param>
public void SetUIContextOperation(UIContextOperation uiContextOperation)
{
currentFlowEnvironment.SetUIContextOperation(uiContextOperation);
}
public bool TryGetDelegateDetails(string libraryName, string methodName, out DelegateDetails del)
{
return currentFlowEnvironment.TryGetDelegateDetails(libraryName, methodName, out del);

View File

@@ -1244,7 +1244,7 @@ namespace Serein.NodeFlow.Env
del = null;
return false;
}
/// <summary>
/// 对象监视表达式
@@ -1268,7 +1268,14 @@ namespace Serein.NodeFlow.Env
this.WriteLine(InfoType.INFO, "远程环境尚未实现的接口TriggerInterrupt");
}
/// <summary>
/// 设置在UI线程操作的线程上下文
/// </summary>
/// <param name="uiContextOperation"></param>
public void SetUIContextOperation(UIContextOperation uiContextOperation)
{
// 无须实现
}
#endregion

View File

@@ -1,9 +1,6 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Core;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Utils;
using Serein.Library.Web;
using Serein.NodeFlow.Model;
using Serein.NodeFlow.Tool;
using System.Collections.Concurrent;
@@ -39,11 +36,7 @@ namespace Serein.NodeFlow
public async Task StartFlowInSelectNodeAsync(IFlowEnvironment env, NodeModelBase startNode)
{
IDynamicContext context;
#if NET6_0_OR_GREATER
context = new Serein.Library.Core.DynamicContext(env); // 从起始节点启动流程时创建上下文
#else
Context = new Serein.Library.Framework.DynamicContext(env);
#endif
context = new Serein.Library.DynamicContext(env); // 从起始节点启动流程时创建上下文
await startNode.StartFlowAsync(context); // 开始运行时从选定节点开始运行
context.Exit();
}
@@ -93,12 +86,7 @@ namespace Serein.NodeFlow
#region
// 判断使用哪一种流程上下文
IDynamicContext Context;
#if NET6_0_OR_GREATER
Context = new Serein.Library.Core.DynamicContext(env); // 从起始节点启动流程时创建上下文
#else
Context = new Serein.Library.Framework.DynamicContext(env);
#endif
IDynamicContext Context = new Serein.Library.DynamicContext(env); // 从起始节点启动流程时创建上下文
#endregion
#region Ioc容器
@@ -342,7 +330,7 @@ namespace Serein.NodeFlow
{
try
{
var context = new DynamicContext(env); // 启动全局触发器时新建上下文
var context = new Library.DynamicContext(env); // 启动全局触发器时新建上下文
var newFlowData = await singleFlipFlopNode.ExecutingAsync(context); // 获取触发器等待Task
context.AddOrUpdate(singleFlipFlopNode.Guid, newFlowData);
await NodeModelBase.RefreshFlowDataAndExpInterrupt(context, singleFlipFlopNode, newFlowData); // 全局触发器触发后刷新该触发器的节点数据

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.19</Version>
<Version>1.1.0</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@@ -47,6 +47,7 @@
<Compile Remove="Tool\ExpressionHelper.cs" />
<Compile Remove="Tool\NodeModelBaseFunc.cs" />
<Compile Remove="Tool\TcsSignal.cs" />
<Compile Remove="Tool\ToCSharpCodeHelper.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,262 @@
using Serein.Library;
using Serein.Library.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Serein.NodeFlow.Tool
{
internal class ToCSharpCodeHelper
{
/// <summary>
/// 运行环境
/// </summary>
private readonly IFlowEnvironment env;
/// <summary>
/// 环境中已加载的所有节点
/// </summary>
private readonly List<NodeModelBase> nodes;
/// <summary>
/// 获取流程启动时在不同时间点需要自动实例化的类型
/// </summary>
private readonly Dictionary<RegisterSequence, List<Type>> autoRegisterTypes;
/// <summary>
/// 初始化方法
/// </summary>
private readonly List<MethodDetails> initMethods;
/// <summary>
/// 加载时方法
/// </summary>
private readonly List<MethodDetails> loadingMethods;
/// <summary>
/// 结束时方法
/// </summary>
private readonly List<MethodDetails> exitMethods;
/// <summary>
/// 开始运行(需要准备好方法信息)
/// </summary>
/// <param name="env">运行环境</param>
/// <param name="nodes">环境中已加载的所有节点</param>
/// <param name="autoRegisterTypes">获取流程启动时在不同时间点需要自动实例化的类型</param>
/// <param name="initMethods">初始化方法</param>
/// <param name="loadingMethods">加载时方法</param>
/// <param name="exitMethods">结束时方法</param>
/// <returns></returns>
public ToCSharpCodeHelper(IFlowEnvironment env,
List<NodeModelBase> nodes,
Dictionary<RegisterSequence, List<Type>> autoRegisterTypes,
List<MethodDetails> initMethods,
List<MethodDetails> loadingMethods,
List<MethodDetails> exitMethods)
{
this.env = env;
this.nodes = nodes;
this.autoRegisterTypes = autoRegisterTypes;
this.initMethods = initMethods;
this.loadingMethods = loadingMethods;
this.exitMethods = exitMethods;
}
public string ToCode()
{
StringBuilder sb = new StringBuilder();
// 确认命名空间
sb.AppendLine($"using System;");
sb.AppendLine($"using System.Linq;");
sb.AppendLine($"using System.Threading;");
sb.AppendLine($"using System.Threading.Tasks;");
sb.AppendLine($"using System.Collections.Concurrent;");
sb.AppendLine($"using System.Collections.Generic;");
sb.AppendLine($"using Serein.Library;");
sb.AppendLine($"using Serein.Library.Api;");
sb.AppendLine($"using Serein.NodeFlow;");
sb.AppendLine(
"""
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Serein.Library;
using Serein.Library.Api;
using Serein.NodeFlow;
// 这里添加引用
namespace NodeToCode
{
class Program
{
private readonly IFlowEnvironment env; // 流程运行环境
""" +
DefineVariableCode() + // 这里定义变量
"""
public void Main(string[] args)
{
""" +
MainOfCSharpCode() + // 这里初始化运行环境
"""
}
""" +
BusinessOfCSharpCode() + // 流程逻辑代码
"""
}
}
"""
);
//sb.AppendLine($"public {returnType} {methodName}({parmasStr})");
//sb.AppendLine($"{{");
//sb.AppendLine($"}}");
return "";
}
public void Main(string[] args) {
var plcLoginControl = ioc.Get<PlcLoginControl>();
var networkLoginControl = ioc.Get<PlcLoginControl>();
if(nodeModel.)
plcLoginControl.Init("", networkLoginControl.Func("",""))
foreach (var md in ffMd)
{
Execution(md);
}
plcLoginControl.Init("129.123.41.21",123);
}
public void Execution(Action action)
{
}
public object Execution(Func<object> action)
{
}
public async object Execution(Func<Task> task)
{
await task.Invoke();
}
private string MainOfCSharpCode()
{
return "";
}
private string BusinessOfCSharpCode()
{
// 定义变量
// 确定需要实例化的类型
// 实例化方法
// 确认调用的方法
// 确认开辟的线程
return "";
}
private string DefineVariableCode()
{
}
/*
*/
public StringBuilder ToMethodCode(StringBuilder sb, string methodName, Type returnType, Type[] parmas)
{
var parmasStr = string.Join("," + Environment.NewLine, parmas.Select(t => $"{t} {t.Name.Replace('.', '_')}"));
sb.AppendLine($"public {returnType} {methodName}({parmasStr})");
sb.AppendLine($"{{");
sb.AppendLine($"}}");
return sb;
}
/// <summary>
/// 添加代码
/// </summary>
/// <param name="sb">字符串构建器</param>
/// <param name="retractCount">缩进次数4个空格</param>
/// <param name="code">要添加的代码</param>
/// <returns>字符串构建器本身</returns>
private static StringBuilder AddCode(StringBuilder sb,
int retractCount = 0,
string code = "")
{
if (!string.IsNullOrWhiteSpace(code))
{
var retract = new string(' ', retractCount * 4);
sb.AppendLine(retract + code);
}
return sb;
}
public static IEnumerable<string> SeparateLineIntoMultipleDefinitions(ReadOnlySpan<char> line)
{
List<string> definitions = new List<string>();
bool captureIsStarted = false;
int equalSignCount = 0;
int lastEqualSignPosition = 0;
int captureStart = 0;
int captureEnd = 0;
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if (c != ',' && !char.IsWhiteSpace(c))
{
if (captureIsStarted)
{
captureEnd = i;
}
else
{
captureStart = i;
captureIsStarted = true;
}
if (c == '=')
{
equalSignCount++;
lastEqualSignPosition = i;
}
}
else
{
if (equalSignCount == 1 && lastEqualSignPosition > captureStart && lastEqualSignPosition < captureEnd)
{
definitions.Add(line[captureStart..(captureEnd + 1)].ToString());
}
equalSignCount = 0;
captureIsStarted = false;
}
}
if (captureIsStarted && equalSignCount == 1 && lastEqualSignPosition > captureStart && lastEqualSignPosition < captureEnd)
{
definitions.Add(line[captureStart..(captureEnd + 1)].ToString());
}
return definitions;
}
}
}