How to Build Composite Condition in CodeDOM to execute Rules

Go To


I am trying to build Rules with my custom UI and execute with CodeDom for my workflow module. I don’t want to use the Rules interface which is given with WF. In order to build/ execute a Rule, I am creating a Condition and ThenAction/ElseActions. At present I am able to execute a single condition successfully. Means Suppose an Order Entity is there with the properties like OrderID, Amount, NoOfItems, Discount, and TotalAmount.

Able to Build the Below condition:

if (this.Amount > 1000)
    thisDiscount = 100;

Building Left / Right conditions:

CodePropertyReferenceExpression technologyRef = new CodePropertyReferenceExpression(thisRef, " Amount");

CodePrimitiveExpression wfConstant = new CodePrimitiveExpression(1000);

// if ( this. Amount > 1000)

CodeBinaryOperatorExpression cond = new CodeBinaryOperatorExpression();

cond.Left = technologyRef;

cond.Operator = CodeBinaryOperatorType.GreaterThanOrEqual;

cond.Right = wfConstant;

Rule rule = new Rule("rule1");

rule.Condition = new RuleExpressionCondition(cond);

Not Able to Build the Below condition:

if (this.Amount > 1000 && NoOfItems > 5)
    thisDiscount = 150;

Can anyone help me to build multiple conditions for a Rule? Means validation on multiple properties in a single IF statement.

2012-04-04 05:16
by Anil Karanam
What exactly is the problem? Why can't you build the second condition? What have you tried - svick 2012-04-04 09:17
My problem is, i am not getting how to add second part of condition ie " && NoOfItems > 5" to the first condition. How to add two conditions to the same rule with a selected operator in between two statements. I am able to add multiple rules but not miltiple conditions to the same rule - Anil Karanam 2012-04-04 10:24


The tree for your first condition would look something like this:

     / \
Amount  1000

And that's exactly how you represented it in your code: one CodeBinaryOperatorExpression for the < and then an expression for each of the children. Doing the same for the second expression is almost the same, but the tree will be bigger:

       /            \
      <              >
     / \            / \
Amount 1000 NoOfItems  5

That is, you will have one CodeBinaryOperatorExpression that represents the && (CodeBinaryOperatorType.BooleanAnd), whose left child will be the < node and its right child will be the > node.

2012-04-04 10:38
by svick
Svick, thank you very much for your explanation. It solved my problem - Anil Karanam 2012-04-04 11:17