You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MORYX应用中实现可参数化的WorkplanStep?

在MORYX中创建可参数化的WorkplanStep

1. 定义参数模型

先创建一个数据契约类存储步骤的可配置参数,需标记[DataContract],参数属性标记[DataMember],确保MORYX能正常序列化、反序列化参数:

[DataContract]
public class CustomStepParameters
{
    [DataMember]
    public int TargetThreshold { get; set; } = 60; // 设置默认值

    [DataMember]
    public string ProcessLabel { get; set; } = "StandardProcess";

    [DataMember]
    public bool SkipLog { get; set; } = false;
}

2. 实现自定义可参数化WorkplanStep

继承WorkplanStepBase,引入参数模型,在步骤生命周期方法(如Initialize、Execute)中使用参数控制逻辑:

public class CustomParameterizedStep : WorkplanStepBase
{
    private CustomStepParameters _stepParams;

    /// <summary>
    /// 初始化步骤,加载配置参数
    /// </summary>
    public override void Initialize(IWorkplanStepConfiguration configuration)
    {
        base.Initialize(configuration);
        // 处理参数为空的情况,兜底默认实例
        _stepParams = configuration.Parameters as CustomStepParameters ?? new CustomStepParameters();
    }

    /// <summary>
    /// 执行步骤核心逻辑
    /// </summary>
    protected override void ExecuteStep(IWorkplanContext context)
    {
        // 用参数控制是否记录日志
        if (!_stepParams.SkipLog)
        {
            context.Log($"开始执行步骤:{_stepParams.ProcessLabel}");
        }

        // 用阈值参数做业务判断
        if (context.CurrentValue > _stepParams.TargetThreshold)
        {
            TriggerOverThresholdAction(context);
        }
        else
        {
            TriggerNormalProcess(context);
        }

        context.MarkStepCompleted();
    }

    // 示例业务方法
    private void TriggerOverThresholdAction(IWorkplanContext context)
    {
        // 超出阈值后的处理逻辑
    }

    private void TriggerNormalProcess(IWorkplanContext context)
    {
        // 正常流程处理逻辑
    }
}

3. 配置与注册步骤

创建配置类关联步骤和参数模型,再注册到MORYX容器:

[DataContract]
public class CustomStepConfiguration : WorkplanStepConfiguration
{
    public CustomStepConfiguration()
    {
        Parameters = new CustomStepParameters();
        StepType = typeof(CustomParameterizedStep);
    }
}

在模块的Initialize方法中完成注册:

public override void Initialize()
{
    base.Initialize();
    WorkplanRegistration.RegisterStep<CustomStepConfiguration, CustomParameterizedStep>();
}

关键注意事项

  • 参数模型的所有可配置属性必须标记[DataMember],否则无法被MORYX序列化
  • Initialize方法中一定要处理参数为空的场景,避免空引用异常
  • 如果需要在MORYX可视化界面中编辑参数,需为参数模型实现对应的编辑器控件(继承DataEditor)

内容的提问来源于stack exchange,提问作者marie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:33:20