[Stick to the top] Stick to learning WF Article Indexes
WF provides a very flexible rule engine, mainly manifested in two forms: Conditions on the activity and Rule Set ruleset. This article describes in detail the conditions of the activity.
The following conditions are available for the activities provided by WF:
Ifelsebranchactivity, which is included in the ifelseactivity and executed when the conditional calculation result is true.
As long as the conditional calculation result of an activity is true, whileactivity continuously executes any activity. Re-calculate the condition when each loop is completed.
Conditionedactivitygroup executes any activity in a row until its conditional calculation result is true. Each single activity in the conditionedactivitygroup has the when condition. Each activity is executed only when the value of the when condition is true.
Replicatgatetivity is executed when the untilcondition attribute is calculated as true.
We can use conditions in custom activities created by ourselves. We have two ways to set conditions. One is to declare the row Rule Conditions and serialize them to the. Rules file. The other isCodeCondition.ProgramTo set conditions, and finally return with the result attribute. As shown in:
The expression supports the following Relational operators:
Equal sign ("=" or "= ")
Big Yu ("> ")
Greater than or equal to ("> = ")
Minor sign ("<")
Less than or equal to ("<= ")
You can use the following arithmetic operators:
Plus sign ("+ ")
Minus ("-")
Multiplication number ("*")
Division ("/")
Modulo ("Mod ")
You can use the following operators to merge/Negative Expressions:
And ("and" or "&&")
Or ("or" or "| ")
Not ("not" or "! ")
Bitwise AND ("&")
By bit or ("| ")
The code method is relatively simple. We only need to write our logic code in the event handler function generated by the system. Finally, we can set the result attribute of the conditionaleventargs parameter as the return value. Conditions are compiled and become part of the Assembly. The runtime engine executes this method and uses the result attribute as the result of conditional calculation.
Let's take a look at what WF has done for us when we use rule conditions? It will serialize the rule conditions to the. Rules file and then create a new ruleconditionreference. Set the conditionname attribute to the value of the name attribute of the ruleexpressioncondition element in the. Rules file. Finally, set the condition attribute to the created ruleconditionreference. The generated code is as follows:
System. workflow. Activities. Rules.RuleconditionreferenceRuleconditionreference1 =New
System. workflow. Activities. Rules.Ruleconditionreference();This. Ifelsebranchactivity1 =NewSystem. workflow. Activities.Ifelsebranchactivity(); Ruleconditionreference1.conditionname ="Rule1";This. Ifelsebranchactivity1.condition = ruleconditionreference1; tHis. Ifelsebranchactivity1.name ="Ifelsebranchactivity1";
After learning about this process, let's take a look at the content in the. Rules file. We simply set a simple rule condition for ifelseactivity to generate
. Rules is as follows:
< Ruledefinitions Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/workflow " > < Ruledefinitions. Conditions > < Ruleexpressioncondition Name = " Rule1 " > < Ruleexpressioncondition. Expression > < Ns0: codebinaryoperatorexpression Operator = " Valueequality " Xmlns: ns0 = " CLR-
Namespace: system. codedom; Assembly = system, version = 2.0.0.0,
Culture = neutral, publickeytoken = b77a5c561934e089 " > < Ns0: codebinaryoperatorexpression. Left > < Ns0: codepropertyreferenceexpression Propertyname = " Test " > < Ns0: codepropertyreferenceexpression. targetobject > < Ns0: codethisreferenceexpression /> </ Ns0: codepropertyreferenceexpression. targetobject > </ Ns0: codepropertyreferenceexpression > </ Ns0: codebinaryoperatorexpression. Left > < Ns0: codebinaryoperatorexpression. Right > < Ns0: codeprimitiveexpression > < Ns0: codeprimitiveexpression. Value > < NS1: String Xmlns: NS1 = "CLR-namespace: system; Assembly = mscorlib,
Version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 " > </ NS1: String > </ Ns0: codeprimitiveexpression. Value > </ Ns0: codeprimitiveexpression > </ Ns0: codebinaryoperatorexpression. Right > </ Ns0: codebinaryoperatorexpression > </ Ruleexpressioncondition. Expression > </ Ruleexpressioncondition > </ Ruledefinitions. Conditions > </ Ruledefinitions >
We can clearly see that the rules in the generated. Rules file are represented in the form of code Dom statements.WF uses system. codedom in. NET Framework
Type provided. You can use these types to create your conditions. Some types are supported in the condition, some types are supported in the Rule operation, and some types are both supported.
Supported.
The following tables show the supportedSystem. codedomType.
| Class |
Used |
| Codearrayindexerexpression |
Condition, Operation |
| Codeassignstatement |
Operation |
| Codebinaryoperatorexpression |
Condition, Operation |
| Codecastexpression |
Condition, Operation |
| Codedireexpression expression |
Condition, Operation |
| Codeexpressionstatement |
Operation |
| Codefieldreferenceexpression |
Condition, Operation |
| Codeindexerexpression |
Condition, Operation |
| Codemethodinvokeexpression |
Condition, Operation |
| Codemethodreferenceexpression |
Condition, Operation |
| Codeprimitiveexpression |
Condition, Operation |
| Codepropertyreferenceexpression |
Condition, Operation |
| Codethisreferenceexpression |
Condition, Operation |
| Codetypereference |
As part of the expression |
| Codetypereferenceexpression |
Condition, Operation |
| Codebinaryoperatortype |
Support Context |
| Add |
Condition, Operation |
| Bitwiseand |
Condition, Operation |
| Bitwiseor |
Condition, Operation |
| Booleanand |
Condition, Operation |
| Booleanor |
Condition, Operation |
| Divide |
Condition, Operation |
| Greaterthan |
Condition, Operation |
| Greaterthanorequal |
Condition, Operation |
| Identityequality |
Condition, Operation |
| Identityinequality |
Condition, Operation |
| Lessthan |
Condition, Operation |
| Lessthanorequal |
Condition, Operation |
| Modulus |
Condition, Operation |
| Multiply |
Condition, Operation |
| Subtract |
Condition, Operation |
| Valueequality |
Condition, Operation |
We recommend that you use rule conditions in these two methods, because they are part of the Rule Engine and can be dynamically updated after the workflow instance runs.