破坏性更新,移除了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

@@ -0,0 +1,384 @@
using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.Library
{
/// <summary>
/// 不提供流程操作能力,仅提供容器功能
/// </summary>
public class ContainerFlowEnvironment : IFlowEnvironment, ISereinIOC
{
/// <summary>
/// 本地运行环境缓存的持久化实例
/// </summary>
private Dictionary<string, object> PersistennceInstance { get; } = new Dictionary<string, object>();
public ContainerFlowEnvironment()
{
}
private ISereinIOC sereinIOC => this;
public ISereinIOC IOC => sereinIOC;
public string EnvName => throw new NotImplementedException();
public bool IsGlobalInterrupt => throw new NotImplementedException();
public bool IsControlRemoteEnv => throw new NotImplementedException();
public InfoClass InfoClass { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public RunState FlowState { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public RunState FlipFlopState { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public IFlowEnvironment CurrentEnv => this;
public UIContextOperation UIContextOperation { get; set; }
/// <summary>
/// 设置在UI线程操作的线程上下文
/// </summary>
/// <param name="uiContextOperation"></param>
public void SetUIContextOperation(UIContextOperation uiContextOperation)
{
this.UIContextOperation = uiContextOperation;
}
public void ActivateFlipflopNode(string nodeGuid)
{
throw new NotImplementedException();
}
public Task<bool> ChangeParameter(string nodeGuid, bool isAdd, int paramIndex)
{
throw new NotImplementedException();
}
public Task<bool> ConnectArgSourceNodeAsync(string fromNodeGuid, string toNodeGuid, JunctionType fromNodeJunctionType, JunctionType toNodeJunctionType, ConnectionArgSourceType argSourceType, int argIndex)
{
throw new NotImplementedException();
}
public Task<bool> ConnectInvokeNodeAsync(string fromNodeGuid, string toNodeGuid, JunctionType fromNodeJunctionType, JunctionType toNodeJunctionType, ConnectionInvokeType invokeType)
{
throw new NotImplementedException();
}
public Task<(bool, RemoteMsgUtil)> ConnectRemoteEnv(string addres, int port, string token)
{
throw new NotImplementedException();
}
public Task<NodeInfo> CreateNodeAsync(NodeControlType nodeType, PositionOfUI position, MethodDetailsInfo methodDetailsInfo = null)
{
throw new NotImplementedException();
}
public Task<bool> ExitFlowAsync()
{
throw new NotImplementedException();
}
public void ExitRemoteEnv()
{
throw new NotImplementedException();
}
public Task<FlowEnvInfo> GetEnvInfoAsync()
{
throw new NotImplementedException();
}
public Task<SereinProjectData> GetProjectInfoAsync()
{
throw new NotImplementedException();
}
public Task<object> InvokeNodeAsync(IDynamicContext context, string nodeGuid)
{
throw new NotImplementedException();
}
public void LoadAllNativeLibraryOfRuning(string path, bool isRecurrence = true)
{
throw new NotImplementedException();
}
public void LoadLibrary(string dllPath)
{
throw new NotImplementedException();
}
public bool LoadNativeLibraryOfRuning(string file)
{
throw new NotImplementedException();
}
public Task LoadNodeInfosAsync(List<NodeInfo> nodeInfos)
{
throw new NotImplementedException();
}
public void LoadProject(FlowEnvInfo flowEnvInfo, string filePath)
{
throw new NotImplementedException();
}
public void MonitorObjectNotification(string nodeGuid, object monitorData, MonitorObjectEventArgs.ObjSourceType sourceType)
{
throw new NotImplementedException();
}
public void MoveNode(string nodeGuid, double x, double y)
{
throw new NotImplementedException();
}
public void NodeLocated(string nodeGuid)
{
throw new NotImplementedException();
}
public Task NotificationNodeValueChangeAsync(string nodeGuid, string path, object value)
{
throw new NotImplementedException();
}
public Task<bool> PlaceNodeToContainerAsync(string nodeGuid, string containerNodeGuid)
{
throw new NotImplementedException();
}
public Task<bool> RemoveConnectArgSourceAsync(string fromNodeGuid, string toNodeGuid, int argIndex)
{
throw new NotImplementedException();
}
public Task<bool> RemoveConnectInvokeAsync(string fromNodeGuid, string toNodeGuid, ConnectionInvokeType connectionType)
{
throw new NotImplementedException();
}
public Task<bool> RemoveNodeAsync(string nodeGuid)
{
throw new NotImplementedException();
}
public void SaveProject()
{
throw new NotImplementedException();
}
public Task<bool> SetConnectPriorityInvoke(string fromNodeGuid, string toNodeGuid, ConnectionInvokeType connectionType)
{
throw new NotImplementedException();
}
public Task<string> SetStartNodeAsync(string nodeGuid)
{
throw new NotImplementedException();
}
public Task<bool> StartAsyncInSelectNode(string startNodeGuid)
{
throw new NotImplementedException();
}
public Task<bool> StartFlowAsync()
{
throw new NotImplementedException();
}
public Task StartRemoteServerAsync(int port = 7525)
{
throw new NotImplementedException();
}
public void StopRemoteServer()
{
throw new NotImplementedException();
}
public Task<bool> TakeOutNodeToContainerAsync(string nodeGuid)
{
throw new NotImplementedException();
}
public void TerminateFlipflopNode(string nodeGuid)
{
throw new NotImplementedException();
}
public void TriggerInterrupt(string nodeGuid, string expression, InterruptTriggerEventArgs.InterruptTriggerType type)
{
throw new NotImplementedException();
}
public bool TryGetDelegateDetails(string assemblyName, string methodName, out DelegateDetails del)
{
throw new NotImplementedException();
}
public bool TryGetMethodDetailsInfo(string assemblyName, string methodName, out MethodDetailsInfo mdInfo)
{
throw new NotImplementedException();
}
public bool TryUnloadLibrary(string assemblyFullName)
{
throw new NotImplementedException();
}
public void WriteLine(InfoType type, string message, InfoClass @class = InfoClass.Trivial)
{
throw new NotImplementedException();
}
#region IOC容器相关
ISereinIOC ISereinIOC.Reset()
{
sereinIOC.Reset();
lock (PersistennceInstance)
{
foreach (var kvp in PersistennceInstance)
{
IOC.RegisterPersistennceInstance(kvp.Key, kvp.Value);
}
} // 重置后重新登记
return this;
}
ISereinIOC ISereinIOC.Register(Type type, params object[] parameters)
{
sereinIOC.Register(type, parameters);
return this;
}
ISereinIOC ISereinIOC.Register<T>(params object[] parameters)
{
sereinIOC.Register<T>(parameters);
return this;
}
ISereinIOC ISereinIOC.Register<TService, TImplementation>(params object[] parameters)
{
sereinIOC.Register<TService, TImplementation>(parameters);
return this;
}
//T ISereinIOC.GetOrRegisterInstantiate<T>()
//{
// return sereinIOC.GetOrRegisterInstantiate<T>();
//}
//object ISereinIOC.GetOrRegisterInstantiate(Type type)
//{
// return sereinIOC.GetOrRegisterInstantiate(type);
//}
object ISereinIOC.Get(Type type)
{
return sereinIOC.Get(type);
}
T ISereinIOC.Get<T>()
{
return (T)sereinIOC.Get(typeof(T));
}
T ISereinIOC.Get<T>(string key)
{
return sereinIOC.Get<T>(key);
}
bool ISereinIOC.RegisterPersistennceInstance(string key, object instance)
{
if (PersistennceInstance.ContainsKey(key))
{
return false;
}
PersistennceInstance.Add(key, instance); // 记录需要持久化的实例
return sereinIOC.RegisterPersistennceInstance(key, instance);
}
bool ISereinIOC.RegisterInstance(string key, object instance)
{
return sereinIOC.RegisterInstance(key, instance);
}
object ISereinIOC.Instantiate(Type type)
{
return sereinIOC.Instantiate(type);
}
T ISereinIOC.Instantiate<T>()
{
return sereinIOC.Instantiate<T>();
}
ISereinIOC ISereinIOC.Build()
{
sereinIOC.Build();
return this;
}
ISereinIOC ISereinIOC.Run<T>(Action<T> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2>(Action<T1, T2> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2, T3>(Action<T1, T2, T3> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5, T6>(Action<T1, T2, T3, T4, T5, T6> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5, T6, T7>(Action<T1, T2, T3, T4, T5, T6, T7> action)
{
sereinIOC.Run(action);
return this;
}
ISereinIOC ISereinIOC.Run<T1, T2, T3, T4, T5, T6, T7, T8>(Action<T1, T2, T3, T4, T5, T6, T7, T8> action)
{
sereinIOC.Run(action);
return this;
}
#endregion
}
}

View File

@@ -0,0 +1,251 @@
using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Serein.Library
{
/// <summary>
/// 动态流程上下文
/// </summary>
public class DynamicContext : IDynamicContext
{
/// <summary>
/// 动态流程上下文
/// </summary>
/// <param name="flowEnvironment"></param>
public DynamicContext(IFlowEnvironment flowEnvironment)
{
Env = flowEnvironment;
RunState = RunState.Running;
}
private readonly string _guid = global::System.Guid.NewGuid().ToString();
string IDynamicContext.Guid => _guid;
/// <summary>
/// 运行环境
/// </summary>
public IFlowEnvironment Env { get; }
/// <summary>
/// 运行状态
/// </summary>
public RunState RunState { get; set; } = RunState.NoStart;
/// <summary>
/// 用来在当前流程上下文间传递数据
/// </summary>
//public Dictionary<string, object> ContextShareData { get; } = new Dictionary<string, object>();
public object Tag { get; set; }
/// <summary>
/// 当前节点执行完成后,设置该属性,让运行环境判断接下来要执行哪个分支的节点。
/// </summary>
public ConnectionInvokeType NextOrientation { get; set; }
/// <summary>
/// 运行时异常信息
/// </summary>
public Exception ExceptionOfRuning { get; set; }
/// <summary>
/// 每个流程上下文分别存放节点的当前数据
/// </summary>
private readonly ConcurrentDictionary<string, object> dictNodeFlowData = new ConcurrentDictionary<string, object>();
/// <summary>
/// 每个流程上下文存储运行时节点的调用关系
/// </summary>
private readonly ConcurrentDictionary<NodeModelBase, NodeModelBase> dictPreviousNodes = new ConcurrentDictionary<NodeModelBase, NodeModelBase>();
/// <summary>
/// 设置运行时上一节点
/// </summary>
/// <param name="currentNodeModel">当前节点</param>
/// <param name="PreviousNode">上一节点</param>
public void SetPreviousNode(NodeModelBase currentNodeModel, NodeModelBase PreviousNode)
{
dictPreviousNodes.AddOrUpdate(currentNodeModel, (_) => PreviousNode, (o, n) => PreviousNode);
}
/// <summary>
/// 获取当前节点的运行时上一节点
/// </summary>
/// <param name="currentNodeModel"></param>
/// <returns></returns>
public NodeModelBase GetPreviousNode(NodeModelBase currentNodeModel)
{
if (dictPreviousNodes.TryGetValue(currentNodeModel, out var node))
{
return node;
}
else
{
return null;
}
}
/// <summary>
/// 获取节点当前数据
/// </summary>
/// <param name="nodeGuid">节点</param>
/// <returns></returns>
public object GetFlowData(string nodeGuid)
{
if (dictNodeFlowData.TryGetValue(nodeGuid, out var data))
{
return data;
}
else
{
return null;
}
}
/// <summary>
/// 添加或更新当前节点数据
/// </summary>
/// <param name="nodeGuid">节点</param>
/// <param name="flowData">新的数据</param>
public void AddOrUpdate(string nodeGuid, object flowData)
{
// this.dictNodeFlowData.TryGetValue(nodeGuid, out var oldFlowData);
dictNodeFlowData.AddOrUpdate(nodeGuid, _ => flowData, (o,n ) => flowData);
}
/// <summary>
/// 上一节点数据透传到下一节点
/// </summary>
/// <param name="nodeModel"></param>
public object TransmissionData(NodeModelBase nodeModel)
{
if (dictPreviousNodes.TryGetValue(nodeModel, out var previousNode)) // 首先获取当前节点的上一节点
{
if (dictNodeFlowData.TryGetValue(previousNode.Guid, out var data)) // 其次获取上一节点的数据
{
return data;
//AddOrUpdate(nodeModel.Guid, data); // 然后作为当前节点的数据记录在上下文中
}
}
return null;
}
/// <summary>
/// 结束流程
/// </summary>
public void Exit()
{
foreach (var nodeObj in dictNodeFlowData.Values)
{
if (nodeObj is null)
{
}
else
{
if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
{
disposable?.Dispose();
}
}
}
if (Tag != null && typeof(IDisposable).IsAssignableFrom(Tag?.GetType()) && Tag is IDisposable tagDisposable)
{
tagDisposable?.Dispose();
}
this.Tag = null;
this.dictNodeFlowData?.Clear();
RunState = RunState.Completion;
}
private void Dispose(ref IDictionary<string, object> keyValuePairs)
{
foreach (var nodeObj in keyValuePairs.Values)
{
if (nodeObj is null)
{
continue;
}
if (nodeObj is IDisposable disposable) /* typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) &&*/
{
disposable?.Dispose();
}
else if (nodeObj is IDictionary<string, object> tmpDict)
{
Dispose(ref tmpDict);
}
else if (nodeObj is ICollection<object> tmpList)
{
Dispose(ref tmpList);
}
else if (nodeObj is IList<object> tmpList2)
{
Dispose(ref tmpList2);
}
}
keyValuePairs.Clear();
}
private void Dispose(ref ICollection<object> list)
{
foreach (var nodeObj in list)
{
if (nodeObj is null)
{
continue;
}
if (nodeObj is IDisposable disposable) /* typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) &&*/
{
disposable?.Dispose();
}
else if (nodeObj is IDictionary<string, object> tmpDict)
{
Dispose(ref tmpDict);
}
else if (nodeObj is ICollection<object> tmpList)
{
Dispose(ref tmpList);
}
else if (nodeObj is IList<object> tmpList2)
{
Dispose(ref tmpList2);
}
}
list.Clear();
}
private void Dispose(ref IList<object> list)
{
foreach (var nodeObj in list)
{
if (nodeObj is null)
{
continue;
}
if (nodeObj is IDisposable disposable) /* typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) &&*/
{
disposable?.Dispose();
}
else if (nodeObj is IDictionary<string, object> tmpDict)
{
Dispose(ref tmpDict);
}
else if (nodeObj is ICollection<object> tmpList)
{
Dispose(ref tmpList);
}
else if (nodeObj is IList<object> tmpList2)
{
Dispose(ref tmpList2);
}
}
list.Clear();
}
}
}

View File

@@ -0,0 +1,93 @@
using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Threading.Tasks;
namespace Serein.Library
{
public static class FlipflopFunc
{
/// <summary>
/// 传入触发器方法的返回类型尝试获取Task[Flipflop[]] 中的泛型类型
/// </summary>
//public static Type GetFlipflopInnerType(Type type)
//{
// // 检查是否为泛型类型且为 Task<>
// if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
// {
// // 获取 Task<> 的泛型参数类型,即 Flipflop<>
// var innerType = type.GetGenericArguments()[0];
// // 检查泛型参数是否为 Flipflop<>
// if (innerType.IsGenericType && innerType.GetGenericTypeDefinition() == typeof(FlipflopContext<>))
// {
// // 获取 Flipflop<> 的泛型参数类型,即 T
// var flipflopInnerType = innerType.GetGenericArguments()[0];
// // 返回 Flipflop<> 中的具体类型
// return flipflopInnerType;
// }
// }
// // 如果不符合条件,返回 null
// return null;
//}
public static bool IsTaskOfFlipflop(Type type)
{
// 检查是否为泛型类型且为 Task<>
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
{
// 获取 Task<> 的泛型参数类型
var innerType = type.GetGenericArguments()[0];
if (innerType.IsGenericType && type.GetGenericTypeDefinition() == typeof(IFlipflopContext<>))
{
var flipflop = type.GetGenericArguments()[0];
return true;
}
// 判断 innerType 是否继承 IFlipflopContext
//if (typeof(IFlipflopContext).IsAssignableFrom(innerType))
//{
// return true;
//}
//else
//{
// return false;
//}
// 检查泛型参数是否为 Flipflop<>
//if (innerType == typeof(IFlipflopContext))
//if (innerType.IsGenericType && innerType.GetGenericTypeDefinition() == typeof(FlipflopContext<>))
//{
//return true;
//}
}
return false;
}
}
/// <summary>
/// 触发器上下文
/// </summary>
public class FlipflopContext<TResult> : IFlipflopContext<TResult>
{
public FlipflopStateType State { get; set; }
public TriggerDescription Type { get; set; }
public TResult Value { get; set; }
public FlipflopContext(FlipflopStateType ffState)
{
State = ffState;
}
public FlipflopContext(FlipflopStateType ffState, TResult value)
{
State = ffState;
Value = value;
}
}
}