NCommet.Modules.BRE is a project that implements ITypedTreeValidator using the rule engine of WWF. By using this implementation, we don't have to hard code any of the rules for the validation of the hierarchy we are creating. Instead, we have to provide an external .xml file that contains these rules, which can be built using a UI tool.

Rule Engine accepts an .xml file, which contains ruleSets. Each ruleSet contains rules that are executed with a certain priority. Rule Engine also accepts an object to which these rules have access. This object provides input to the rules and accepts an output from them. In our implementation we have 4 ruleSets and their associated classes:
  1. CanAttach, NCommet.Modules.BRE.RECanAttachInput
  2. CanContain, NCommet.Modules.BRE.RECanContainInput
  3. CanRemove, NCommet.Modules.BRE.RECanRemoveInput
  4. CanSetRoot, NCommet.Modules.BRE.RECanSetRootInput
These 4 classes inherit from the class NCommet.Modules.BRE.REInput, which is abstract and internal. The fields of this class that are fully accessible to the rules are the following:
  1. bool isValid: This field is set to true by the rules, if the action that is performed is allowed.
  2. bool doGet: This field is set to true by the typed tree validator, if the call is coming from an information method instead of a decision method. In this case, the rule simply fills the set of the valid values instead of validating the input.
  3. ICollection<string> errors: This field contains any error messages, which are describing why the action that was performed was invalid.
  4. ILog log: Through this field, the rule has access to the logging mechanism of the application.
To give an example, suppose we build a blog application using NCommet. This application contains three item types: Blog, Post and Comment. Each Blog can have one or more Posts as a child and each Post can have one or more Comments as a child. Lets see how we can build the rule for CanAttach using Rule Engine. First of all, we create a new ruleSet with the name CanAttach and we assign NCommet.Modules.BRE.RECanAttachInput as its associated type. We are now ready to start adding rules to the ruleSet. To do this, we use the field NameValueCollection tbl of RECanAttachInput to store all the possible combinations of item types.

erst-canattach-settable.png

As we can see this rule has a priority value of 5. We could have set any other number but we have to keep in mind that the rules are executed with descending priority (i.e. if the rest of the rules have a priority less than 5 this rule will be executed first).

We now need a rule that will stop the evaluation of the rest rules in the case when the doGet field is true. We remind here that this a value set by the typed tree validator when a method is called that only needs to get the valid input values and not to check the validity of the input.

erst-canattach-isget.png

Finally, we have the simple rule that checks if the tbl contains the right parent-child combinations

erst-canattach-decide.png

When we create the rest of the ruleSets, we export the rules to an .xml file with the extension .rules. If we have a web application, we place this file in the App_Data folder and we add the following lines in the configuration file of the application:

<ncommet persister="NCommet.Modules.Dao.Storage, NCommet.Modules.Dao"
         typedTreeValidator="NCommet.Modules.BRE.TypeTreeValidatorImpl, NCommet.Modules.BRE">
</ncommet>

<configSections>
  ...
  <section name="NCommet.Modules.BRE" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.1.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  ...
</configSections>

...

<NCommet.Modules.BRE>
  <add key="RuleFile" value="~/App_Data/Blogs.rules"/>
  <add key="RuleFilePathType" value="Web"/>
</NCommet.Modules.BRE>

Last edited Apr 15, 2008 at 12:41 PM by klai, version 4

Comments

No comments yet.