如何在代码中创建WF规则?

时间:2009-05-26 20:30:58

标签: .net workflow-foundation rule-engine

我正在为WF中的自定义Activity创建 ActivityToolboxItem 。我正在尝试为CreateComponentsCore方法中的设计人员做尽可能多的工作。我最终想要得到的是:

1)我的自定义活动( TriggerActivity SequenceActivity ),包含以下内容:
2) TriggerEvaluatorActivity (一个简单的 Activity ),它绑定到TriggerActivity上的属性
3) IfElseActivity ,它有两个分支
4)包含
IfElseBranchActivity 5) TerminateActivity
6) IfElseBranchActivity 为空,当TriggerEvaluatorActivity的计算结果为true时执行流程。

设计人员使用一个或多个针对执行上下文执行复杂评估的实现来切换 TriggerEvaluatorActivity

在代码中设置它非常简单,但我完全坚持如何启动 ActivityCondition ,它将添加到 IfElseBranchActivity 来控制执行流程。

我需要做的就是评估 TriggerEvaluatorActivity 上的属性。我已经通过代码dom创建了一个漂亮可爱的规则,但是我无法直接在 IfElseBranchActivity 上设置它,我必须将规则存储在某处并在活动中引用它。

所以,我想我的问题是,一旦我在代码中创建了规则,如何从自定义ActivityToolboxItem中将我的规则添加到工作流中,以便我在代码中构建的活动可以引用它?

2 个答案:

答案 0 :(得分:1)

根据this question on MSDN,它存储在根Activity下的依赖属性中。不确定代码示例是否正确。


作品。这是一些示例代码:

protected override System.ComponentModel.IComponent[] 
    CreateComponentsCore(System.ComponentModel.Design.IDesignerHost host)
{
    var trigger = new Trigger() { Name = "Trigger" };
    var never = new Never() { Name = "NeverTrigger" };
    var ifelse = new IfElseActivity() { Name = "IfElse" };
    var stop = new TerminateActivity() { Name = "StopJob" };
    var failed = new IfElseBranchActivity() { Name = "NotTriggered" };
    var succeeded = new IfElseBranchActivity() { Name = "Triggered" };
    // build tree
    failed.Activities.Add(stop);
    ifelse.Activities.Add(failed);
    ifelse.Activities.Add(succeeded);
    trigger.Activities.Add(never);
    trigger.Activities.Add(ifelse);
    // create rule if it doesn't already exist
    var ruleDefinitions = GetRuleDefinitions
        (host.RootComponent as DependencyObject);
    if (!ruleDefinitions.Conditions.Contains(RuleName))
    {
        var neverTrigger = 
            new CodePropertyReferenceExpression
                (new CodeThisReferenceExpression(), 
                "NeverTrigger");
        var triggered = 
            new CodePropertyReferenceExpression
                (neverTrigger, 
                "Triggered");
        var falseSide = new CodeBinaryOperatorExpression();
        falseSide.Left = triggered;
        falseSide.Right = new CodePrimitiveExpression(false);
        falseSide.Operator = CodeBinaryOperatorType.ValueEquality;

        var ruleCondition = new RuleExpressionCondition
            (RuleName, falseSide);


        ruleDefinitions.Conditions.Add(ruleCondition);
    }
    // add rule to the failed branch; 
    // don't need a rule for the succeeded branch
    failed.Condition = new RuleConditionReference() 
        { ConditionName = RuleName };

    return new IComponent[] { trigger };
}

private RuleDefinitions GetRuleDefinitions
    (DependencyObject rootComponent)
{
    var ruleDefinitions = rootComponent.GetValue
        (RuleDefinitions.RuleDefinitionsProperty) as RuleDefinitions;
    if (ruleDefinitions == null)
    {
        ruleDefinitions = new RuleDefinitions();
        rootComponent.SetValue(RuleDefinitions.RuleDefinitionsProperty, 
            ruleDefinitions);
    }
    return ruleDefinitions;
}

答案 1 :(得分:0)

Will -

新规则可能会被标记,直到它们被验证为止。您是否看到在添加它们后是否可以立即直接验证它们?