WF4.0 Beta1中的规则引擎变化

开发 开发工具
本文将介绍WF4.0 Beta1中的规则引擎变化,在WF3.x时代我们可以使用声明性的条件和代码方式的条件,也可以使用支持正向链接的RuleSet,WF4.0提供了统一的完全声明式的表达式(Expression)。

在WF3.x时代我们可以使用声明性的条件和代码方式的条件,也可以使用支持正向链接的RuleSet。当然我们还可以使用基于CodeDOM的API来用代码的方式声明。

在微软刚刚发布的WF4.0 Beta1中我们已经看不到这些了,WF4.0提供了统一的完全声明式的表达式(Expression)。现在的版本只支持使用VB来构建表达式(Expression),但是在设计上是可以支持任何语言的,微软也会在未来的版本中提供对其他语言的支持。

WF4.0中表达式是ValueExpression类型的,我们在VS中构造表达式的窗口是ExpressionTextBox类的实例,它也是可以再外部重新宿主的,只不过只有和VS结合的时候才有智能感知和颜色的支持。在表达式中我们可以引用工作流中的变量和参数。这些信息都会被序列化到XAML中。提供了表达式(Expression)并不是就不要原来的方式,微软在开发WF4.0一个很重要的部分就是对WF3.x全面兼容。在WF4.0中提供了一个Interop活动可以帮助我们很好的完成现有WF3.x程序的迁移,我们只需要简单的设置它的Body Type属性即可,我们可以将WF4.0中的变量和参数绑定到WF3.x中的依赖属性上,如下图:  

参数绑定  

在WF4.0 beta1中没有提供对正向链接的RuleSet功能,官方已经声明会在将来的版本中加大这部分的投入。现在如果我们要想在WF4.0 Beta1使用类似的功能我们可以开发一个自定义活动来完成,下面的例子来源于WF Samples中,首先是活动的代码部分:

  1. namespace Microsoft.Samples.Rules  
  2. {  
  3.     using System;  
  4.     using System.Activities;  
  5.     using System.ComponentModel;  
  6.     using System.Workflow.Activities.Rules;  
  7.     using System.Workflow.ComponentModel.Compiler;  
  8.  
  9.     [Designer(typeof(Microsoft.Samples.Rules.PolicyDesigner))]  
  10.     public sealed class Policy40Activity : NativeActivity  
  11.     {  
  12.         public RuleSet RuleSet { get; set; }  
  13.  
  14.         [IsRequired]  
  15.         public InOutArgument TargetObject { get; set; }  
  16.         public OutArgument<ValidationErrorCollection> ValidationErrors { get; set; }  
  17.  
  18.         protected override void OnOpen(DeclaredEnvironment environment)  
  19.         {  
  20.             if (this.RuleSet == null)  
  21.             {  
  22.                 throw new System.ArgumentNullException("RuleSet property can't be null");  
  23.             }  
  24.         }  
  25.  
  26.         protected override void Execute(ActivityExecutionContext context)  
  27.         {  
  28.             // validate before running  
  29.             Type targetType = this.TargetObject.Get(context).GetType();  
  30.             RuleValidation validation = new RuleValidation(targetType, null);  
  31.             if (!this.RuleSet.Validate(validation))  
  32.             {  
  33.                 // set the validation error out argument  
  34.                 this.ValidationErrors.Set(context, validation.Errors);  
  35.                 // throw a validation exception  
  36.                 throw new ValidationException(string.Format("The ruleset is not valid. {0} validation errors                   found (check the ValidationErrors property for more information).", validation.Errors.Count));  
  37.             }  
  38.             // execute the ruleset  
  39.             object evaluatedTarget = this.TargetObject.Get(context);  
  40.             RuleEngine engine = new RuleEngine(this.RuleSet, validation);  
  41.             engine.Execute(evaluatedTarget);  
  42.             // update the target object  
  43.             this.TargetObject.Set(context, evaluatedTarget);  
  44.         }  
  45.     }  

下面是活动的设计器部分,在WF4.0中提供了对活动设计器的可视化支持:

  1. <sad:WorkflowElementDesigner x:Class="Microsoft.Samples.Rules.PolicyDesigner" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:sad="clr-namespace:System.Activities.Design;assembly=System.Activities.Design" 
  5.     xmlns:sadv="clr-namespace:System.Activities.Design.View;assembly=System.Activities.Design"> 
  6.     <sad:WorkflowElementDesigner.Resources> 
  7.         <sadv:ArgumentToExpressionConverter x:Uid="sadv:ArgumentToExpressionConverter_1"          x:Key="argumentToExpressionConverter" /> 
  8.     </sad:WorkflowElementDesigner.Resources> 
  9.     <Grid> 
  10.         <Grid.RowDefinitions> 
  11.             <RowDefinition x:Uid="RowDefinition_1" /> 
  12.             <RowDefinition x:Uid="RowDefinition_2" /> 
  13.         </Grid.RowDefinitions> 
  14.         <Grid.ColumnDefinitions> 
  15.             <ColumnDefinition x:Uid="ColumnDefinition_1" Width="70*"  /> 
  16.             <ColumnDefinition x:Uid="ColumnDefinition_2" Width="196*" /> 
  17.         </Grid.ColumnDefinitions> 
  18.         <Label Content="Target Object" Name="label1" Margin="0,5,0,7"/> 
  19.         <sadv:ExpressionTextBox   
  20.             x:Uid="ExpressionTextBox_1"   
  21.             Grid.Row="0" Grid.Column="1"   
  22.             AutomationProperties.AutomationId="TargetObject"              
  23.             Width="190" Margin="9,7,9,7" MaxLines="1"   
  24.             VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"   
  25.             Expression="{Binding Path=ModelItem.TargetObject, Mode=TwoWay, Converter={StaticResource                          argumentToExpressionConverter}, ConverterParameter=InOut}" 
  26.             UseLocationExpression="True" 
  27.             OwnerActivity="{Binding Path=ModelItem, Mode=TwoWay}" /> 
  28.         <Button Content="Edit RuleSet" Name="button1" Width="190" Margin="9,9,9,9" Click="button1_Click"          Grid.Row="1" Grid.Column="1" /> 
  29.     </Grid> 
  30. </sad:WorkflowElementDesigner> 

效果如下图:

运行效果


下面是当点击按钮后,会出现RuleSet的编辑器:

  1. using System;  
  2. using System.Activities;  
  3. using System.Windows;  
  4. using System.Windows.Forms;  
  5. using System.Workflow.Activities.Rules;  
  6. using System.Workflow.Activities.Rules.Design;  
  7.  
  8. namespace Microsoft.Samples.Rules  
  9. {  
  10.     // Interaction logic for PolicyDesigner.xaml  
  11.     public partial class PolicyDesigner  
  12.     {  
  13.         public PolicyDesigner()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.  
  18.         private void button1_Click(object sender, RoutedEventArgs e)  
  19.         {  
  20.             // verifiy that TargetObject property has been configured  
  21.             object targetObject = ModelItem.Properties["TargetObject"].ComputedValue;  
  22.             if (targetObject == null)  
  23.             {  
  24.                 System.Windows.MessageBox.Show("TargetObject needs to be configured before adding the rules");  
  25.                 return;  
  26.             }  
  27.             // verify that target object is correctly configured  
  28.             InOutArgument arg = targetObject as InOutArgument;  
  29.             if (arg == null)  
  30.             {  
  31.                 System.Windows.MessageBox.Show("Invalid target object");  
  32.                 return;  
  33.             }  
  34.             // open the ruleset editor  
  35.             Type targetObjectType = arg.ArgumentType;  
  36.             RuleSet ruleSet = ModelItem.Properties["RuleSet"].ComputedValue as RuleSet;  
  37.             if (ruleSet == null)  
  38.                 ruleSet = new RuleSet();  
  39.             RuleSetDialog ruleSetDialog = new RuleSetDialog(targetObjectType, null, ruleSet);  
  40.             DialogResult result = ruleSetDialog.ShowDialog();  
  41.             // update the model item  
  42.             if (result == DialogResult.OK)             {  
  43.                 ModelItem.Properties["RuleSet"].SetValue(ruleSetDialog.RuleSet);  
  44.             }  
  45.         }  
  46.     }  

这样我们就可以再WF4.0中使用该活动了,如下图:

WF4.0中活动

【编辑推荐】

  1. 浅谈WF 4.0 Beta1中的跟踪机制
  2. 微软MVP初探WF 4.0 beta1 崭新面貌让人吃惊
  3. 详解在Workflow工作流中如何使用角色
  4. 详解工作流架构与实现
  5. 用UML描述工作流管理
责任编辑:彭凡 来源: cnblogs
相关推荐

2009-06-15 10:20:47

WF 4.0 Beta跟踪机制

2009-06-22 09:36:06

WF 4.0 beta跟踪配置

2009-07-16 10:41:40

WF 4.0 beta

2009-10-28 09:23:27

WF4.0 Beta2

2009-05-20 10:26:09

Visual StudWF微软

2009-07-28 10:00:47

VS2010 beta

2013-02-26 09:42:09

RailsRuby

2010-01-14 09:35:10

WF4.0

2012-04-16 15:18:15

JythonJVM

2010-09-13 10:02:11

Firefox 4.0

2009-05-20 09:20:29

Visual Stud试用WPF

2009-12-01 10:37:45

Hiweed LinuLinuxDeepin

2010-01-14 14:12:14

Visual Stud

2010-02-01 09:19:32

WF 4.0

2011-07-06 16:26:32

jQuery Mobi

2021-04-22 10:13:34

鸿蒙HarmonyOS应用

2018-09-30 11:53:39

开源 操作系统功能

2011-08-25 10:28:51

Ubuntu 11.1

2010-12-10 09:17:50

开源虚拟机Virtua

2009-05-20 13:03:40

Visual StudSilverlight微软
点赞
收藏

51CTO技术栈公众号