mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-08 08:56:35 +08:00
修改了脚本执行的一些BUG。
This commit is contained in:
@@ -1026,6 +1026,10 @@ namespace Serein.NodeFlow.Env
|
||||
#region 参数调用关系
|
||||
foreach (var toNode in NodeModels.Values)
|
||||
{
|
||||
if(toNode.MethodDetails.ParameterDetailss == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for (var i = 0; i < toNode.MethodDetails.ParameterDetailss.Length; i++)
|
||||
{
|
||||
var pd = toNode.MethodDetails.ParameterDetailss[i];
|
||||
@@ -1589,6 +1593,8 @@ namespace Serein.NodeFlow.Env
|
||||
NodeConnectChangeEventArgs.ConnectChangeType.Remote // 是创建连接还是删除连接
|
||||
))); // 通知UI
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < argInfo.Length; i++)
|
||||
{
|
||||
ParameterDetails? pd = nodeModel.MethodDetails.ParameterDetailss[i];
|
||||
|
||||
@@ -22,7 +22,8 @@ namespace Serein.NodeFlow.Env
|
||||
{
|
||||
if(nodeControlType == NodeControlType.ExpCondition
|
||||
|| nodeControlType == NodeControlType.ExpOp
|
||||
|| nodeControlType == NodeControlType.GlobalData)
|
||||
|| nodeControlType == NodeControlType.GlobalData
|
||||
|| nodeControlType == NodeControlType.Script)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -70,6 +71,8 @@ namespace Serein.NodeFlow.Env
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从节点信息读取节点类型
|
||||
/// </summary>
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
object instance = md.ActingInstance;
|
||||
|
||||
var args = await GetParametersAsync(context, this, md);
|
||||
var args = await GetParametersAsync(context, this);
|
||||
// 因为这里会返回不确定的泛型 IFlipflopContext<TRsult>
|
||||
// 而我们只需要获取到 State 和 Value(返回的数据)
|
||||
// 所以使用 dynamic 类型接收
|
||||
|
||||
@@ -5,7 +5,9 @@ using Serein.Script;
|
||||
using Serein.Script.Node;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -28,7 +30,7 @@ namespace Serein.NodeFlow.Model
|
||||
private IScriptFlowApi ScriptFlowApi { get; }
|
||||
|
||||
private ASTNode mainNode;
|
||||
|
||||
private SereinScriptInterpreter ScriptInterpreter;
|
||||
/// <summary>
|
||||
/// 构建流程脚本节点
|
||||
/// </summary>
|
||||
@@ -37,14 +39,11 @@ namespace Serein.NodeFlow.Model
|
||||
{
|
||||
//ScriptFlowApi = environment.IOC.Get<ScriptFlowApi>();
|
||||
ScriptFlowApi = new ScriptFlowApi(environment, this);
|
||||
|
||||
|
||||
MethodInfo? method = this.GetType().GetMethod(nameof(GetFlowApi));
|
||||
if (method != null)
|
||||
{
|
||||
SereinScriptInterpreter.AddFunction(nameof(GetFlowApi), method, () => this); // 挂载获取流程接口
|
||||
}
|
||||
ScriptInterpreter = new SereinScriptInterpreter();
|
||||
}
|
||||
|
||||
static SingleScriptNode()
|
||||
{
|
||||
// 挂载静态方法
|
||||
var tempMethods = typeof(BaseFunc).GetMethods().Where(method =>
|
||||
!(method.Name.Equals("GetHashCode")
|
||||
@@ -52,21 +51,74 @@ namespace Serein.NodeFlow.Model
|
||||
|| method.Name.Equals("ToString")
|
||||
|| method.Name.Equals("GetType")
|
||||
)).Select(method => (method.Name, method)).ToArray();
|
||||
|
||||
// 加载基础方法
|
||||
foreach ((string name, MethodInfo method) item in tempMethods)
|
||||
{
|
||||
SereinScriptInterpreter.AddFunction(item.name, item.method); // 加载基础方法
|
||||
SereinScriptInterpreter.AddStaticFunction(item.name, item.method);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override void OnCreating()
|
||||
{
|
||||
MethodInfo? method = this.GetType().GetMethod(nameof(GetFlowApi));
|
||||
if (method != null)
|
||||
{
|
||||
ScriptInterpreter.AddFunction(nameof(GetFlowApi), method, () => this); // 挂载获取流程接口
|
||||
}
|
||||
|
||||
var md = MethodDetails;
|
||||
var pd = md.ParameterDetailss ??= new ParameterDetails[1];
|
||||
md.ParamsArgIndex = 0;
|
||||
pd[0] = new ParameterDetails
|
||||
{
|
||||
Index = 0,
|
||||
Name = "object",
|
||||
IsExplicitData = true,
|
||||
DataValue = string.Empty,
|
||||
DataType = typeof(object),
|
||||
ExplicitType = typeof(object),
|
||||
ArgDataSourceNodeGuid = string.Empty,
|
||||
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
|
||||
NodeModel = this,
|
||||
ExplicitTypeName = "Value",
|
||||
Items = null,
|
||||
IsParams = true,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载脚本代码
|
||||
/// 导出脚本代码
|
||||
/// </summary>
|
||||
public void LoadScript()
|
||||
/// <param name="nodeInfo"></param>
|
||||
/// <returns></returns>
|
||||
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
|
||||
{
|
||||
dynamic data = new ExpandoObject();
|
||||
data.Script = Script ?? "";
|
||||
nodeInfo.CustomData = data;
|
||||
return nodeInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载自定义数据
|
||||
/// </summary>
|
||||
/// <param name="nodeInfo"></param>
|
||||
public override void LoadCustomData(NodeInfo nodeInfo)
|
||||
{
|
||||
this.Script = nodeInfo.CustomData?.Script ?? "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新加载脚本代码
|
||||
/// </summary>
|
||||
public void ReloadScript()
|
||||
{
|
||||
try
|
||||
{
|
||||
mainNode = new SereinScriptParser(Script).Parse();
|
||||
var p = new SereinScriptParser(Script);
|
||||
mainNode = p.Parse();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -81,27 +133,33 @@ namespace Serein.NodeFlow.Model
|
||||
/// <returns></returns>
|
||||
public override async Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
var @params = await NodeModelBase.GetParametersAsync(context, this);
|
||||
ScriptFlowApi.Context= context;
|
||||
context.AddOrUpdate($"{context.Guid}_{this.Guid}_Params", @params[0]); // 后面再改
|
||||
|
||||
mainNode ??= new SereinScriptParser(Script).Parse();
|
||||
SereinScriptInterpreter scriptInterpreter = new SereinScriptInterpreter();
|
||||
var result = await scriptInterpreter.InterpretAsync(mainNode); // 从入口节点执行
|
||||
scriptInterpreter.ResetVar();
|
||||
IScriptInvokeContext scriptContext = new ScriptInvokeContext();
|
||||
var result = await ScriptInterpreter.InterpretAsync(scriptContext, mainNode); // 从入口节点执行
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public IScriptFlowApi GetFlowApi()
|
||||
{
|
||||
return ScriptFlowApi;
|
||||
|
||||
#region 挂载的方法
|
||||
|
||||
public IScriptFlowApi GetFlowApi()
|
||||
{
|
||||
return ScriptFlowApi;
|
||||
}
|
||||
|
||||
private static class BaseFunc
|
||||
{
|
||||
public static DateTime GetNow() => DateTime.Now;
|
||||
|
||||
public static Type TypeOf(object type)
|
||||
{
|
||||
return type.GetType();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void Print(object value)
|
||||
{
|
||||
@@ -120,7 +178,7 @@ namespace Serein.NodeFlow.Model
|
||||
public static bool ToBool(object value)
|
||||
{
|
||||
return bool.Parse(value.ToString());
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static async Task Delay(object value)
|
||||
@@ -138,6 +196,7 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Serein.Library;
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -23,42 +24,47 @@ namespace Serein.NodeFlow
|
||||
/// </summary>
|
||||
public NodeModelBase NodeModel { get; private set; }
|
||||
|
||||
IDynamicContext IScriptFlowApi.Context { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
||||
public IDynamicContext? Context{ get; set; }
|
||||
|
||||
private string _paramsKey => $"{Context?.Guid}_{NodeModel.Guid}_Params";
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建流程脚本接口
|
||||
/// </summary>
|
||||
/// <param name="environment">运行环境</param>
|
||||
/// <param name="nodeModel">节点</param>
|
||||
public ScriptFlowApi(IFlowEnvironment environment, IDynamicContext dynamicContext, NodeModelBase nodeModel)
|
||||
public ScriptFlowApi(IFlowEnvironment environment, NodeModelBase nodeModel)
|
||||
{
|
||||
Env = environment;
|
||||
NodeModel = nodeModel;
|
||||
}
|
||||
|
||||
Task<object> IScriptFlowApi.CallNode(string nodeGuid)
|
||||
public Task<object> CallNode(string nodeGuid)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
object IScriptFlowApi.GetDataOfParams(int index)
|
||||
public object? GetArgData(int index)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var obj = Context?.GetFlowData(_paramsKey);
|
||||
if (obj is object[] @params && index < @params.Length)
|
||||
{
|
||||
return @params[index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
object IScriptFlowApi.GetDataOfParams(string name)
|
||||
|
||||
public object? GetFlowData()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return Context?.GetFlowData(NodeModel.Guid);
|
||||
}
|
||||
|
||||
object IScriptFlowApi.GetFlowData()
|
||||
public object? GetGlobalData(string keyName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
object IScriptFlowApi.GetGlobalData(string keyName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return SereinEnv.GetFlowGlobalData(keyName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user