THE RAZI

Hi everone

We have a situation where will be using Windows Workflow Foundation Rule Engine along with our custom application. Now the main point of foucs is to create rules on basis of some external application (say Microsoft Analysis Services Dataminig generates rules and they will be used ) , now as we gather WF uses CodeDom to save rules , now we need to write a convertor that takes rules from one side (SQL BI ) and convert it into WF (CodeDom ) Format . Now here i need you guys support for the following

(SQL Datamining Generated Rules ---->WF CodeDom)

a) Let me know the best possible option(s) for this

b) can any one provide detail codeDom demos (I already browsed WF Team Member Blog , i think Mustufa's and some one else) but need more samples for that.

c) Need to know the pain points / bottle neck that can aries

You can always email me any suggesstion / Solutions at razi_rais@yahoo.com

Best regards

Razi Bin Rais




Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Tom Lake - MSFT

The CodeDOM that WF rules use is in the System.CodeDOM namespace and is not specific to WF. A quick reference can be found at http://msdn2.microsoft.com/en-us/library/f1dfsbhc.aspx. Can you give some examples of the code you want demos for e.g. variable == "some value" or ((CustomActivity)this.GetActivityByName("foo")).Property <= 10




Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Sentient

Tom,

Is there an XSD schema of the CodeDom I looked into the XamlWorkflow2006.xsd but that did not include the CodeDom expressions.

Second question: Can the xml CodeDom definition direct be de-serialized into CodeDom objects

Thanks





Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Kavita Kamani - MSFT

Hi Sentient,

I have a post on my blog on how to create rules using the rules OM (which has CodeDom'isms to it) - http://blogs.msdn.com/kavitak/archive/2006/10/12/tinkering-with-the-rules-om.aspx

WorkflowMarkupSerializer is used to go from the in memory RuleDefinitions to a serialized representation. RuleDefinitions is the root object in the .rules file. If you create the rules using the OM as per the blog post, then you can add the rules to a ruleset and add it to a RuleDefinitions and call serialize on it to get the corresponding XML.






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Sobia

Hi Kavita,

can u plz provide me some code sample for invoking the method via code in then or else action while creating the rules programatically

plz reply as soon as possible

regards,






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Kavita Kamani - MSFT

Here you go -

using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;
using System.Workflow.Activities.Rules;
using System.Workflow.ComponentModel.Compiler;

namespace RulesUsingCode
{
class Program
{
static void Main(string[] args)
{
Rule discountRule = new Rule("ResidentialDiscount");
discountRule.ReevaluationBehavior = RuleReevaluationBehavior.Always;
discountRule.Priority = 0;

RuleStatementAction discountAction = new RuleStatementAction(
new CodeAssignStatement(new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "Discount"),
new CodePrimitiveExpression(5)));

RuleCondition discountCondition = new RuleExpressionCondition(
new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "OrderValue"),
CodeBinaryOperatorType.GreaterThan,
new CodePrimitiveExpression(500)));
discountRule.Condition = discountCondition;
discountRule.ThenActions.Add(discountAction);

RuleSet ruleSet = new RuleSet("DiscountRuleSet");
ruleSet.Rules.Add(discountRule);

RuleValidation ruleValidation = new RuleValidation(typeof(Shopping), null);
RuleEngine engine = new RuleEngine(ruleSet, ruleValidation);

Shopping shoppingUnder500 = new Shopping(100);
engine.Execute(shoppingUnder500);
Console.WriteLine("shoppingUnder500 - Discount evaluated: " + shoppingUnder500.Discount);

Shopping shoppingOver500 = new Shopping(600);
engine.Execute(shoppingOver500);
Console.WriteLine("shoppingOver500 - Discount evaluated: " + shoppingOver500.Discount);
}
}

public class Shopping
{
private int orderValue;

public int OrderValue
{
get { return orderValue; }
set { orderValue = value; }
}
private int discount;

public int Discount
{
get { return discount; }
set { discount = value; }
}
public Shopping(int orderValue)
{
this.orderValue = orderValue;
this.discount = 0;
}

}
}






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

THE RAZI

Dear Kavita

Can you give example how to save the rules you create through code above into .rules file (i-e into codeDom). is there any save method in RuleSet , or is there any other workaound for this.






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Bruce Bukovics

Once you have a RuleSet constructed, you can add it to a RuleDefinitions object and serialize it like this:

//Serialize rules
RuleDefinitions ruleDefs = new RuleDefinitions();
ruleDefs.RuleSets.Add(ruleSet);
using (XmlWriter xmlWriter = XmlWriter.Create(fileName))
{
WorkflowMarkupSerializer markupSerializer = new WorkflowMarkupSerializer();
markupSerializer.Serialize(xmlWriter, ruleDefinitions);
}

Hope this helps.

Bruce Bukovics http://www.bukovics.com
Pro WF: Windows Workflow in .NET 3.0 http://www.learnworkflow.com
.NET 2.0 Interoperability Recipes http://www.interopbook.com








Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Sobia

 Kavita Kamani - MSFT wrote:

Here you go -

using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;
using System.Workflow.Activities.Rules;
using System.Workflow.ComponentModel.Compiler;

namespace RulesUsingCode
{
    class Program
    {
        static void Main(string[] args)
        {
            Rule discountRule = new Rule("ResidentialDiscount");
            discountRule.ReevaluationBehavior = RuleReevaluationBehavior.Always;
            discountRule.Priority = 0;

            RuleStatementAction discountAction = new RuleStatementAction(
                new CodeAssignStatement(new CodePropertyReferenceExpression(
                                            new CodeThisReferenceExpression(), "Discount"),
                                        new CodePrimitiveExpression(5)));

            RuleCondition discountCondition = new RuleExpressionCondition(
                new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(
                                                    new CodeThisReferenceExpression(), "OrderValue"),
                                                CodeBinaryOperatorType.GreaterThan,
                                                new CodePrimitiveExpression(500)));
            discountRule.Condition = discountCondition;
            discountRule.ThenActions.Add(discountAction);

            RuleSet ruleSet = new RuleSet("DiscountRuleSet");
            ruleSet.Rules.Add(discountRule);

            RuleValidation ruleValidation = new RuleValidation(typeof(Shopping), null);
            RuleEngine engine = new RuleEngine(ruleSet, ruleValidation);
           
            Shopping shoppingUnder500 = new Shopping(100);
            engine.Execute(shoppingUnder500);
            Console.WriteLine("shoppingUnder500 - Discount evaluated: " + shoppingUnder500.Discount);

            Shopping shoppingOver500 = new Shopping(600);
            engine.Execute(shoppingOver500);
            Console.WriteLine("shoppingOver500 - Discount evaluated: " + shoppingOver500.Discount);
        }
    }

    public class Shopping
    {
        private int orderValue;

        public int OrderValue
        {
            get { return orderValue; }
            set { orderValue = value; }
        }
        private int discount;

        public int Discount
        {
            get { return discount; }
            set { discount = value; }
        }
        public Shopping(int orderValue)
        {
            this.orderValue = orderValue;
            this.discount = 0;
        }
       
    }
}

 

the same thing i have achieve thru this code:

CodeMethodReferenceExpression thenActionMethod = new CodeMethodReferenceExpression(newRef, ddlThenAction.SelectedItem.Text);

CodePropertyReferenceExpression propertyRef = new CodePropertyReferenceExpression(newRef, ThenActionSetParam);

CodeMethodInvokeExpression methodInvokeExp = new CodeMethodInvokeExpression(thenActionMethod, propertyRef);

RuleStatementAction thenAction =

new RuleStatementAction(new CodeExpressionStatement(methodInvokeExp));

 

thnks kavita for your help and gud responses!

Have a nice day!

Regards,

Sobia.






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

THE RAZI

Hey can you please specify is there any way i can use the xml rules saved in file in policy activity. Or there is some other way around for that.




Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Kavita Kamani - MSFT

You have to write a custom policy activity for this. There is a PolicyFromFile sample here - http://wf.netfx3.com/files/folders/rules_samples/entry8850.aspx




Re: Windows Workflow Foundation Creating WF Rules From CodeDom

THE RAZI

Dear Kavita

In the code snippet above you created rule with ordervalue > 500 for this you use codeDom obj model like

new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "OrderValue"),
CodeBinaryOperatorType.GreaterThan,
new CodePrimitiveExpression(500)));

i

As we dont have much indepth knowledge of codeDom can you please provide codeDom statements for

1) if we have Shooping card a class variable say

Shopping shopping ;

and want to write codeDom for

this.shopping.OrderValue > 500

ii) this.shopping.OrderValue > 500 && this.shopping.Discount == 90

I will also encourge you to point out some useful resources on web that provide good starting point of codeDom for generating statements like above. Moreover is there any was we can reverse engineer .net code from codeDom Expresseion like we can give xml representation of CodeDom and get .NET compalint code agaist it.

Best regards

Razi






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Jurgen Willis

CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression();

CodeFieldReferenceExpression shoppingRef = new CodeFieldReferenceExpression(thisRef, "shopping");

CodePropertyReferenceExpression orderValueRef = new CodePropertyReferenceExpression(shoppingRef,"OrderValue");

CodePropertyReferenceExpression discountRef = new CodePropertyReferenceExpression(shoppingRef,"Discount");

// this.shopping.OrderValue > 500

CodeBinaryOperatorExpression orderValueTest = new CodeBinaryOperatorExpression(orderValueRef, CodeBinaryOperatorType.GreaterThan, new CodePrimitiveExpression(500));

// this.shopping.Discount == 90

CodeBinaryOperatorExpression discountTest = new CodeBinaryOperatorExpression(discountRef, CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(90));

//this.shopping.OrderValue > 500 && this.shopping.Discount

CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(orderValueTest, CodeBinaryOperatorType.BooleanAnd, discountTest);

As for CodeDom references, you can find lots of information on MSDN:

http://search.msdn.microsoft.com/search/Redirect.aspx title=Using+the+CodeDOM+(.NET+Framework+Developer's+Guide)&url=http://msdn.microsoft.com/library/en-us/cpguide/html/cpconUsingCodeDOM.asp

http://search.msdn.microsoft.com/search/Redirect.aspx title=System.CodeDom&url=http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcodedom.asp

http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfSystemCodeDomCompiler.asp






Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Jas98004

Hi Kavita, this is a very useful sample. However, I am having an issue adapting it for my situation.

Your sample checks the value of OrderValue and then sets the value of Discount.

In my check, I need to cal a method VerifyNameExists("Name") and call another function if true - ActionFunction("Name")

Where VerifyNameExists() returns bool and ActionFunction() returns void.

How would I call functions and pass these params

thanks!

Jas





Re: Windows Workflow Foundation Creating WF Rules From CodeDom

Jas98004

ok Figured this out - works great! Used - CodeMethodInvokeExpression

very cool

tahnks,
Jas