WF规则设计器
WF规则设计器
WF的引擎提供了三种启动工作流的方法:
- 指定引workFlow的类,这需要引用workFlow的DLL,或将workFlow与引擎放在同一个项目中
- 指定引workFlow的xoml文件(动态编译)
- 指定workFlow的xoml的字符串
这三种方式的例子我都写过,各自的优点我也说过,这里就不再描述了,
本例使用的是第三种,这种的好处是可将xoml字串存入数据库中,要修改流程可以直接对字符串操作,我的一个基于Visio开发的WF的设计器用的就是这种方式生
WF还提供了流程与规则相分离的方式,在启动流程时可以选择一个规则,这样就可以在不修改流程的情况下,通过使用为同的规则调整流程的条件轨迹
我以前的例子只提供了流程设计器,今天发一个规则设计器的例子
WF的规则与规则设计器类结构
例子:IfElseBranchActivity、WhileActivity、ConditionedActivityGroup、 ReplicatorActivity的规则编辑器
例子下载:https://files.cnblogs.com/wxwinter/WfEditRule.rar
创建一个工作流
将工作流的rules文件与xoml文件复制到[规则设计器]
启动一下看结果
修改规则
再启动一下看结果
规则设计器中操作规则的代码
//从字串得到rules字串得到RuleDefinitions private void button3_Click(object sender, EventArgs e) { System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
byte[] a = utf8.GetBytes(this.rules.Text);
System.IO.MemoryStream m = new System.IO.MemoryStream(a);
XmlReader x = XmlReader.Create(m);
object objectRule = serializer.Deserialize(x);
ruleDefinitions = objectRule as RuleDefinitions; this.ruleslist.DisplayMember = "Name"; this.rulesSetlist.DisplayMember = "Name";
// Conditions中的CodeExpression列表 foreach (var v in ruleDefinitions.Conditions) { //添加到列表框 this.ruleslist.Items.Add(v); }
// RuleSets中的RuleSet列表 foreach (var v in ruleDefinitions.RuleSets) { //添加到列表框 this.rulesSetlist.Items.Add(v); } } |
// 修改RuleDefinitions的Conditions的CodeExpression规则 private void button2_Click(object sender, EventArgs e) { RuleExpressionCondition ruleExpressionCondition = this.ruleslist.SelectedItem as RuleExpressionCondition; if (ruleExpressionCondition != null) { CodeExpression codeExpression = ruleExpressionCondition.Expression;
RuleConditionDialog ruleConditionDialog = new RuleConditionDialog(typeof(System.Workflow.ComponentModel.Activity), null, codeExpression);
if (ruleConditionDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { CodeExpression expression = ruleConditionDialog.Expression; RuleExpressionCondition condition = new RuleExpressionCondition(expression); RuleValidation validation = new RuleValidation(typeof(System.Workflow.ComponentModel.Activity), null); if (condition.Validate(validation)) { ruleExpressionCondition.Expression = ruleConditionDialog.Expression; } else { Console.WriteLine("语法有错"); } }
} } |
// 为RuleDefinitions的Conditions添加CodeExpression规则 private void button4_Click(object sender, EventArgs e) {
RuleConditionDialog ruleConditionDialog = new RuleConditionDialog(typeof(System.Workflow.ComponentModel.Activity), null, null);
if (ruleConditionDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { CodeExpression expression = ruleConditionDialog.Expression; RuleExpressionCondition condition = new RuleExpressionCondition(expression); RuleValidation validation = new RuleValidation(typeof(System.Workflow.ComponentModel.Activity), null); if (condition.Validate(validation)) { condition.Name = this.newRulesName.Text; this.ruleslist.Items.Add(condition);
} else { Console.WriteLine("语法有错"); } } } |
//保存RuleDefinitions到字串得到rules字串得 private void button5_Click(object sender, EventArgs e) {
WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
System.IO.MemoryStream stream=new System.IO.MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
serializer.Serialize(writer, ruleDefinitions); writer.Flush();
byte[] by;
by = stream.GetBuffer(); System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(); string s = utf8.GetString(by); writer.Close();
this.rules.Text = s;
} |