WF rule designer
The WF engine provides three methods to start a workflow:
- Specifies the class for referencing workFlow. This requires referencing the workFlow DLL or placing workFlow and engine in the same project.
- Specify the xoml file cited for workFlow (dynamic compilation)
- The xoml string of the specified workFlow.
I have written examples of these three methods. I have also said their advantages and I will not describe them here,
In this example, the third type is used. The advantage is that the xoml string can be stored in the database. to modify the process, you can directly operate on the string, one of my WF designers developed based on Visio is created in this way.
WF also provides a way to separate the process from the rule. You can select a rule when starting the process so that you can leave the process unchanged, adjust the condition track of the process by using the same rule
In my previous example, I only provided the process designer. Today I am sending an example of the rule designer.
WF rules and rule designer Class Structure
Example: Rule editor for IfElseBranchActivity, WhileActivity, ConditionedActivityGroup, and ReplicatorActivity
Download: http://files.cnblogs.com/wxwinter/WfEditRule.rar
Create a Workflow
Copy the workflow's rules file and xoml file to the [Rule Designer]
Start to see the results
Modify rules
Start and check the result.
Code for operating rules in the Rule designer
// Obtain the RuleDefinitions string from the string
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 ();
XmlReader x = XmlReader. Create (m );
Object objectRule = serializer. Deserialize (x );
RuleDefinitions = objectRule RuleDefinitions;
This. ruleslist. DisplayMember = "Name ";
This. rulesSetlist. DisplayMember = "Name ";
// CodeExpression list in Conditions
Foreach (var v in ruleDefinitions. Conditions)
{
// Add to the list box
This. ruleslist. Items. Add (v );
}
// RuleSet list in RuleSets
Foreach (var v in ruleDefinitions. RuleSets)
{
// Add to the list box
This. rulesSetlist. Items. Add (v );
}
} |
// Modify the CodeExpression rules of the Conditions of RuleDefinitions
Private Void button2_Click (object sender, EventArgs e)
{
RuleExpressionCondition ruleExpressionCondition = this. ruleslist. SelectedItem 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 ("syntax error ");
}
}
}
} |
// Add a CodeExpression rule for the condition of RuleDefinitions
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 ("syntax error ");
}
}
} |
// Save RuleDefinitions to get the rules string
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 = stream. GetBuffer ();
System. Text. UTF8Encoding utf8 = new System. Text. UTF8Encoding ();
String s = utf8.GetString ();
Writer. Close ();
This. rules. Text = s;
} |