locked
Can Rules engine be used outside WF RRS feed

  • Question

  • Hi,

    Can Rules engine be used outside WF? Also shall I get code to create a WPF interface for changing the rules after the application is deployed?

    Regards
    Dnana
    Tuesday, January 12, 2010 12:46 PM

Answers

  • Hi Dnana

    It is possible to use the RuleEngine outside the WF. Its a control that can be popped up from any place. TIn fact there are two variants of the same
    1)RuleSet Dialog

    RuleSetDialog

     

    ruleSetDialog = new RuleSetDialog(obj, null, ruleSetObj);


    2)Rule Condition Dialog

    RuleConditionDialog

     

    conditionDialog = new RuleConditionDialog(myType, null, codeExpression);

     

     



    Yes you can use it even after application is deployed.
    You can serialise the RuleSet object and even stor in your database as an xml, can reload it, validate it and execute it as well.
    Anyone who can write normal c# can write rule in fact any object oriented language. but its not very user friendly for a not technical user.


    I got this implemented in my product already but honestly it is not suitable for a non technical user. If you really want a nice way for your end user to write rule then please create your own front end and reuse the Microsoft Rule Execution engine. This is a right match. I have asked Microsoft if they are going to upgrade RuleEditor in 4.0 and this is what i got back.
    http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/00085e12-016a-4db6-8f8e-dc08c2a35f80


    Thanks
    Ammit
    • Proposed as answer by Ammit._ Tuesday, January 12, 2010 6:52 PM
    • Marked as answer by After2050 Wednesday, January 13, 2010 12:00 PM
    Tuesday, January 12, 2010 5:30 PM

All replies

  • Hi Dnana

    It is possible to use the RuleEngine outside the WF. Its a control that can be popped up from any place. TIn fact there are two variants of the same
    1)RuleSet Dialog

    RuleSetDialog

     

    ruleSetDialog = new RuleSetDialog(obj, null, ruleSetObj);


    2)Rule Condition Dialog

    RuleConditionDialog

     

    conditionDialog = new RuleConditionDialog(myType, null, codeExpression);

     

     



    Yes you can use it even after application is deployed.
    You can serialise the RuleSet object and even stor in your database as an xml, can reload it, validate it and execute it as well.
    Anyone who can write normal c# can write rule in fact any object oriented language. but its not very user friendly for a not technical user.


    I got this implemented in my product already but honestly it is not suitable for a non technical user. If you really want a nice way for your end user to write rule then please create your own front end and reuse the Microsoft Rule Execution engine. This is a right match. I have asked Microsoft if they are going to upgrade RuleEditor in 4.0 and this is what i got back.
    http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/00085e12-016a-4db6-8f8e-dc08c2a35f80


    Thanks
    Ammit
    • Proposed as answer by Ammit._ Tuesday, January 12, 2010 6:52 PM
    • Marked as answer by After2050 Wednesday, January 13, 2010 12:00 PM
    Tuesday, January 12, 2010 5:30 PM
  • Hi Eclipsys,

    Thanks for your reply. I understood that by using the RuleSetDialog or RuleConditionDialog we can add/edit a rule.
    I got a sample code from MSDN,

                RuleSet ruleSet = null;        // no rule set to start with
                using (RuleSetDialog dialog = new RuleSetDialog(typeof(TestClass), null, ruleSet))
                {
                    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        ruleSet = dialog.RuleSet;
                        RuleValidation validation = new RuleValidation(typeof(TestClass), null);
                        RuleEngine engine = new RuleEngine(ruleSet, validation);
                        TestClass testObject = new TestClass();
                        engine.Execute(testObject);
                    }
                }
    In the above code when we create the RuleSetDialog why we are passing the TestClass; Once we create the rule set how we can store it as an XML file?

    Regards
    Dnana
    Wednesday, January 13, 2010 12:11 PM
  • Hi Dnana,

    The type you are passing is the one that gets attached to your RuleSet. In other words the rule you write is written against a type.

    e.g. you write a Rule against Foo class so you need to pass it while opening the Editor thats how the editor gives you all the intellisense etc. for that class that is why you can just type in this. and see all the properties of the class you passed while opening editor.

    Right now you can have only one type per rule , please check following link this is something my team mate asked to MSFT a few times back.

    http://social.msdn.microsoft.com/Forums/en-US/windowsworkflowfoundation/thread/e4fc4cda-e214-4874-8927-e0ee1814971d
     While executing the Rule you also need to pass the same object else it wont work in other words if you write Rule on Foo class you can not pass DO class' object while executing the rule. so this becomes your contract for that RuleSet.

    To answer your second question - you can serialize the RuleSet object and get an xml out of it and get it stored in Db or your store.

    Following code will help you serialize a RuleSet.

        string str;
        StringBuilder sb = new StringBuilder();
        try
        {
            if (ruleSet != null)
            {
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                StringWriter w = new StringWriter(sb, CultureInfo.InvariantCulture);
                XmlTextWriter writer = new XmlTextWriter(w);
                serializer.Serialize(writer, ruleSet);
                writer.Flush();
                writer.Close();
                w.Flush();
                w.Close();
                return sb.ToString();
            }
            str = null;
        }
        catch (Exception)
        {
            throw;
        }
     
    
    Thanks
    Ammit
    • Proposed as answer by Ammit._ Wednesday, January 13, 2010 12:38 PM
    Wednesday, January 13, 2010 12:38 PM