GIT练习

This commit is contained in:
fengjiayi
2024-08-05 10:11:58 +08:00
parent 1b7d61c390
commit 75333e621f
84 changed files with 11677 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
using Serein.DynamicFlow;
using System.Diagnostics;
namespace Serein.DynamicFlow.NodeModel
{
/// <summary>
/// 组合动作节点(用于动作区域)
/// </summary>
public class CompositeActionNode : NodeBase
{
public List<SingleActionNode> ActionNodes;
/// <summary>
/// 组合动作节点(用于动作区域)
/// </summary>
public CompositeActionNode(List<SingleActionNode> actionNodes)
{
ActionNodes = actionNodes;
}
public void AddNode(SingleActionNode node)
{
ActionNodes.Add(node);
MethodDetails ??= node.MethodDetails;
}
//public override void Execute(DynamicContext context)
//{
// //Dictionary<int,object> dict = new Dictionary<int,object>();
// for (int i = 0; i < ActionNodes.Count; i++)
// {
// SingleActionNode? action = ActionNodes[i];
// try
// {
// action.Execute(context);
// }
// catch (Exception ex)
// {
// Debug.Write(ex.Message);
// return;
// }
// }
// CurrentState = true;
// return;
// /*foreach (var nextNode in TrueBranchNextNodes)
// {
// nextNode.ExecuteStack(context);
// }*/
//}
}
}

View File

@@ -0,0 +1,69 @@
using Serein.DynamicFlow.Tool;
using System.Diagnostics;
namespace Serein.DynamicFlow.NodeModel
{
/// <summary>
/// 组合条件节点(用于条件区域)
/// </summary>
public class CompositeConditionNode : NodeBase
{
public List<SingleConditionNode> ConditionNodes { get; } =[];
public void AddNode(SingleConditionNode node)
{
ConditionNodes.Add(node);
MethodDetails ??= node.MethodDetails;
}
public override object? Execute(DynamicContext context)
{
// bool allTrue = ConditionNodes.All(condition => Judge(context,condition.MethodDetails));
// bool IsAllTrue = true; // 初始化为 true
FlowState = true;
foreach (SingleConditionNode? node in ConditionNodes)
{
if (!Judge(context, node))
{
FlowState = false;
break;// 一旦发现条件为假,立即退出循环
}
}
return PreviousNode?.FlowData;
//if (IsAllTrue)
//{
// foreach (var nextNode in TrueBranchNextNodes)
// {
// nextNode.ExecuteStack(context);
// }
//}
//else
//{
// foreach (var nextNode in FalseBranchNextNodes)
// {
// nextNode.ExecuteStack(context);
// }
//}
}
private bool Judge(DynamicContext context, SingleConditionNode node)
{
try
{
node.Execute(context);
return node.FlowState;
}
catch (Exception ex)
{
Debug.Write(ex.Message);
}
return false;
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.DynamicFlow.NodeModel
{
public class CompositeLoopNode : NodeBase
{
}
}

View File

@@ -0,0 +1,449 @@
using Serein.DynamicFlow;
using Serein.DynamicFlow.Tool;
using Newtonsoft.Json;
using SqlSugar;
namespace Serein.DynamicFlow.NodeModel
{
public enum ConnectionType
{
IsTrue,
IsFalse,
IsEx,
}
/// <summary>
/// 节点基类(数据):条件控件,动作控件,条件区域,动作区域
/// </summary>
public abstract class NodeBase : IDynamicFlowNode
{
public MethodDetails MethodDetails { get; set; }
public string Guid { get; set; }
public string DisplayName { get; set; }
public bool IsStart { get; set; }
public string DelegateName { get; set; }
/// <summary>
/// 运行时的上一节点
/// </summary>
public NodeBase? PreviousNode { get; set; }
/// <summary>
/// 上一节点集合
/// </summary>
public List<NodeBase> PreviousNodes { get; set; } = [];
/// <summary>
/// 下一节点集合(真分支)
/// </summary>
public List<NodeBase> TrueBranch { get; set; } = [];
/// <summary>
/// 下一节点集合(假分支)
/// </summary>
public List<NodeBase> FalseBranch { get; set; } = [];
/// <summary>
/// 异常分支
/// </summary>
public List<NodeBase> ExBranch { get; set; } = [];
/// <summary>
/// 当前状态(进入真分支还是假分支,异常分支在异常中确定)
/// </summary>
public bool FlowState { get; set; } = true;
//public ConnectionType NextType { get; set; } = ConnectionType.IsTrue;
/// <summary>
/// 当前传递数据
/// </summary>
public object? FlowData { get; set; } = null;
// 正常流程节点调用
public virtual object? Execute(DynamicContext context)
{
MethodDetails md = MethodDetails;
object? result = null;
if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
{
if (md.ExplicitDatas.Length == 0)
{
if (md.ReturnType == typeof(void))
{
((Action<object>)del).Invoke(md.ActingInstance);
}
else
{
result = ((Func<object, object>)del).Invoke(md.ActingInstance);
}
}
else
{
object?[]? parameters = GetParameters(context, MethodDetails);
if (md.ReturnType == typeof(void))
{
((Action<object, object[]>)del).Invoke(md.ActingInstance, parameters);
}
else
{
result = ((Func<object, object[], object>)del).Invoke(md.ActingInstance, parameters);
}
}
// context.SetFlowData(result);
// CurrentData = result;
}
return result;
}
// 触发器调用
public virtual async Task<object?> ExecuteAsync(DynamicContext context)
{
MethodDetails md = MethodDetails;
object? result = null;
if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
{
if (md.ExplicitDatas.Length == 0)
{
// 调用委托并获取结果
FlipflopContext flipflopContext = await ((Func<object, Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance);
if (flipflopContext != null)
{
if (flipflopContext.State == FfState.Cancel)
{
throw new Exception("this async task is cancel.");
}
else
{
if (flipflopContext.State == FfState.Succeed)
{
FlowState = true;
result = flipflopContext.Data;
}
else
{
FlowState = false;
}
}
}
}
else
{
object?[]? parameters = GetParameters(context, MethodDetails);
// 调用委托并获取结果
FlipflopContext flipflopContext = await ((Func<object, object[], Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance, parameters);
if (flipflopContext != null)
{
if (flipflopContext.State == FfState.Cancel)
{
throw new Exception("取消此异步");
}
else
{
FlowState = flipflopContext.State == FfState.Succeed;
result = flipflopContext.Data;
}
}
}
// context.SetFlowData(result);
// CurrentData = result;
}
return result;
}
public async Task ExecuteStack(DynamicContext context)
{
var cts = context.ServiceContainer.Get<CancellationTokenSource>();
Stack<NodeBase> stack =[];
stack.Push(this);
while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环
{
// 从栈中弹出一个节点作为当前节点进行处理
var currentNode = stack.Pop();
//currentNode.MethodDetails.ActingInstance ??= context.ServiceContainer.Get(
// currentNode.MethodDetails.ActingInstanceType
// );
if (currentNode.MethodDetails != null)
{
currentNode.MethodDetails.ActingInstance ??= context.ServiceContainer.Get(MethodDetails.ActingInstanceType);
}
if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == DynamicNodeType.Flipflop)
{
currentNode.FlowData = await currentNode.ExecuteAsync(context);
}
else
{
currentNode.FlowData = currentNode.Execute(context);
}
var nextNodes = currentNode.FlowState ? currentNode.TrueBranch
: currentNode.FalseBranch;
// 将下一个节点集合中的所有节点逆序推入栈中
for (int i = nextNodes.Count - 1; i >= 0; i--)
{
nextNodes[i].PreviousNode = currentNode;
stack.Push(nextNodes[i]);
}
}
}
public object[]? GetParameters(DynamicContext context, MethodDetails md)
{
// 用正确的大小初始化参数数组
var types = md.ExplicitDatas.Select(it => it.DataType).ToArray();
if (types.Length == 0)
{
return [md.ActingInstance];
}
object[]? parameters = new object[types.Length];
for (int i = 0; i < types.Length; i++)
{
var mdEd = md.ExplicitDatas[i];
Type type = mdEd.DataType;
if (type == typeof(DynamicContext))
{
parameters[i] = context;
}
else if (type == typeof(MethodDetails))
{
parameters[i] = md;
}
else if (type == typeof(NodeBase))
{
parameters[i] = this;
}
else if (mdEd.IsExplicitData) // 显式参数
{
if (mdEd.DataType.IsEnum)
{
var enumValue = Enum.Parse(mdEd.DataType, mdEd.DataValue);
parameters[i] = enumValue;
}
else if (mdEd.ExplicitType == typeof(string))
{
parameters[i] = mdEd.DataValue;
}
else if (mdEd.ExplicitType == typeof(bool))
{
parameters[i] = bool.Parse(mdEd.DataValue);
}
else if (mdEd.ExplicitType == typeof(int))
{
parameters[i] = int.Parse(mdEd.DataValue);
}
else if (mdEd.ExplicitType == typeof(double))
{
parameters[i] = double.Parse(mdEd.DataValue);
}
else
{
parameters[i] = ConvertValue(mdEd.DataValue, mdEd.ExplicitType);
}
}
else
{
//var tmpParameter = context.GetFlowData()?.ToString();
var tmpParameter = PreviousNode?.FlowData?.ToString();
if (mdEd.DataType.IsEnum)
{
var enumValue = Enum.Parse(mdEd.DataType, tmpParameter);
parameters[i] = enumValue;
}
else if (mdEd.DataType == typeof(string))
{
parameters[i] = tmpParameter;
}
else if (mdEd.DataType == typeof(bool))
{
parameters[i] = bool.Parse(tmpParameter);
}
else if (mdEd.DataType == typeof(int))
{
parameters[i] = int.Parse(tmpParameter);
}
else if (mdEd.DataType == typeof(double))
{
parameters[i] = double.Parse(tmpParameter);
}
else
{
if (tmpParameter != null && mdEd.DataType!= null)
{
parameters[i] = ConvertValue(tmpParameter, mdEd.DataType);
}
}
}
}
return parameters;
}
private dynamic? ConvertValue(string value, Type targetType)
{
try
{
if (!string.IsNullOrEmpty(value))
{
return JsonConvert.DeserializeObject(value, targetType);
}
else
{
return null;
}
}
catch (JsonReaderException ex)
{
Console.WriteLine(ex);
return value;
}
catch (JsonSerializationException ex)
{
// 如果无法转为对应的JSON对象
int startIndex = ex.Message.IndexOf("to type '") + "to type '".Length; // 查找类型信息开始的索引
int endIndex = ex.Message.IndexOf('\''); // 查找类型信息结束的索引
var typeInfo = ex.Message[startIndex..endIndex]; // 提取出错类型信息,该怎么传出去?
Console.WriteLine("无法转为对应的JSON对象:"+typeInfo);
return null;
}
catch // (Exception ex)
{
return value;
}
}
#region ExecuteAsync调用方法
//public virtual async Task<object?> ExecuteAsync(DynamicContext context)
//{
// MethodDetails md = MethodDetails;
// object? result = null;
// if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
// {
// if (md.ExplicitDatas.Length == 0)
// {
// if (md.ReturnType == typeof(void))
// {
// ((Action<object>)del).Invoke(md.ActingInstance);
// }
// else if (md.ReturnType == typeof(Task<FlipflopContext>))
// {
// // 调用委托并获取结果
// FlipflopContext flipflopContext = await ((Func<object, Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance);
// if (flipflopContext != null)
// {
// if (flipflopContext.State == FfState.Cancel)
// {
// throw new Exception("this async task is cancel.");
// }
// else
// {
// if (flipflopContext.State == FfState.Succeed)
// {
// CurrentState = true;
// result = flipflopContext.Data;
// }
// else
// {
// CurrentState = false;
// }
// }
// }
// }
// else
// {
// result = ((Func<object, object>)del).Invoke(md.ActingInstance);
// }
// }
// else
// {
// object?[]? parameters = GetParameters(context, MethodDetails);
// if (md.ReturnType == typeof(void))
// {
// ((Action<object, object[]>)del).Invoke(md.ActingInstance, parameters);
// }
// else if (md.ReturnType == typeof(Task<FlipflopContext>))
// {
// // 调用委托并获取结果
// FlipflopContext flipflopContext = await ((Func<object, object[], Task<FlipflopContext>>)del).Invoke(MethodDetails.ActingInstance, parameters);
// if (flipflopContext != null)
// {
// if (flipflopContext.State == FfState.Cancel)
// {
// throw new Exception("取消此异步");
// }
// else
// {
// CurrentState = flipflopContext.State == FfState.Succeed;
// result = flipflopContext.Data;
// }
// }
// }
// else
// {
// result = ((Func<object, object[], object>)del).Invoke(md.ActingInstance, parameters);
// }
// }
// context.SetFlowData(result);
// }
// return result;
//}
#endregion
}
}
/* while (stack.Count > 0) // 循环中直到栈为空才会退出
{
// 从栈中弹出一个节点作为当前节点进行处理
var currentNode = stack.Pop();
if(currentNode is CompositeActionNode || currentNode is CompositeConditionNode)
{
currentNode.currentState = true;
}
else if (currentNode is CompositeConditionNode)
{
}
currentNode.Execute(context);
// 根据当前节点的执行结果选择下一节点集合
// 如果 currentState 为真,选择 TrueBranchNextNodes否则选择 FalseBranchNextNodes
var nextNodes = currentNode.currentState ? currentNode.TrueBranchNextNodes
: currentNode.FalseBranchNextNodes;
// 将下一个节点集合中的所有节点逆序推入栈中
for (int i = nextNodes.Count - 1; i >= 0; i--)
{
stack.Push(nextNodes[i]);
}
}*/

View File

@@ -0,0 +1,71 @@
using Serein.DynamicFlow.Tool;
using System.Diagnostics;
namespace Serein.DynamicFlow.NodeModel
{
/// <summary>
/// 单动作节点(用于动作控件)
/// </summary>
public class SingleActionNode : NodeBase
{
//public override void Execute(DynamicContext context)
//{
// try
// {
// Execute(context, base.MethodDetails);
// CurrentState = true;
// }
// catch (Exception ex)
// {
// Debug.Write(ex.Message);
// CurrentState = false;
// }
//}
//public void Execute(DynamicContext context, MethodDetails md)
//{
// if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
// {
// object? result = null;
// if (md.ExplicitDatas.Length == 0)
// {
// if (md.ReturnType == typeof(void))
// {
// ((Action<object>)del).Invoke(md.ActingInstance);
// }
// else
// {
// result = ((Func<object, object>)del).Invoke(md.ActingInstance);
// }
// }
// else
// {
// object?[]? parameters = GetParameters(context, MethodDetails);
// if (md.ReturnType == typeof(void))
// {
// ((Action<object, object[]>)del).Invoke(md.ActingInstance, parameters);
// }
// else
// {
// result = ((Func<object, object[], object>)del).Invoke(md.ActingInstance, parameters);
// }
// }
// // 根据 ExplicitDatas.Length 判断委托类型
// //var action = (Action<object, object[]>)del;
// // 调用委托并获取结果
// // action.Invoke(MethodDetails.ActingInstance, parameters);
// //parameters = [md.ActingInstance, "", 123, ""];
// context.SetFlowData(result);
// }
//}
}
}

View File

@@ -0,0 +1,72 @@
using Serein.DynamicFlow.SerinExpression;
using Serein.DynamicFlow.Tool;
using System.Diagnostics;
using System.Linq.Expressions;
namespace Serein.DynamicFlow.NodeModel
{
/// <summary>
/// 条件节点(用于条件控件)
/// </summary>
public class SingleConditionNode : NodeBase
{
/// <summary>
/// 是否为自定义参数
/// </summary>
public bool IsCustomData { get; set; }
/// <summary>
/// 自定义参数值
/// </summary>
public object? CustomData { get; set; }
/// <summary>
/// 条件表达式
/// </summary>
public string Expression { get; set; }
public override object? Execute(DynamicContext context)
{
// 接收上一节点参数or自定义参数内容
object? result;
if (IsCustomData)
{
result = CustomData;
}
else
{
result = PreviousNode?.FlowData;
}
FlowState = SerinConditionParser.To(result, Expression);
Console.WriteLine($"{result} {Expression} -> " + FlowState);
return result;
}
//public override void Execute(DynamicContext context)
//{
// CurrentState = Judge(context, base.MethodDetails);
//}
//private bool Judge(DynamicContext context, MethodDetails md)
//{
// try
// {
// if (DelegateCache.GlobalDicDelegates.TryGetValue(md.MethodName, out Delegate del))
// {
// object[] parameters = GetParameters(context, md);
// var temp = del.DynamicInvoke(parameters);
// //context.GetData(GetDyPreviousKey());
// return (bool)temp;
// }
// }
// catch (Exception ex)
// {
// Debug.Write(ex.Message);
// }
// return false;
//}
}
}

View File

@@ -0,0 +1,44 @@
using Serein.DynamicFlow.SerinExpression;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.DynamicFlow.NodeModel
{
/// <summary>
/// Expression Operation - 表达式操作
/// </summary>
public class SingleExpOpNode : NodeBase
{
public string Expression { get; set; }
public override object? Execute(DynamicContext context)
{
//if (PreviousNode != null && PreviousNode.FlowData == null)
//{
// // 存在
// throw new InvalidOperationException("previous node data is null.");
//}
//else
//{
//}
var data = PreviousNode?.FlowData;
var newData = SerinExpressionEvaluator.Evaluate(Expression, data, out bool isChange);
FlowState = true;
Console.WriteLine(newData);
if (isChange)
{
return newData;
}
else
{
return PreviousNode?.FlowData;
}
}
}
}

View File

@@ -0,0 +1,40 @@
using Serein.DynamicFlow.Tool;
namespace Serein.DynamicFlow.NodeModel
{
public class SingleFlipflopNode : NodeBase
{
//public override void Execute(DynamicContext context)
//{
// throw new NotImplementedException("无法以非await/async的形式调用触发器");
//}
//public virtual async Task ExecuteAsync(DynamicContext context, Action NextTask = null)
//{
// if (DelegateCache.GlobalDicDelegates.TryGetValue(MethodDetails.MethodName, out Delegate? del))
// {
// object?[]? parameters = GetParameters(context, MethodDetails);
// // 根据 ExplicitDatas.Length 判断委托类型
// var func = (Func<object, object[], Task<FlipflopContext>>)del;
// // 调用委托并获取结果
// FlipflopContext flipflopContext = await func.Invoke(MethodDetails.ActingInstance, parameters);
// if (flipflopContext != null)
// {
// if (flipflopContext.State == FfState.Cancel)
// {
// throw new Exception("取消此异步");
// }
// else
// {
// CurrentState = flipflopContext.State == FfState.Succeed;
// context.SetFlowData(flipflopContext.Data);
// }
// }
// }
//}
}
}