Referencing external logic

From FlexRule Wiki
Jump to: navigation, search

Introduction

In the application, you may need to define some commonly used logic and then start reusing this. This separation can happen in two layers:

  1. Splitting into different Logic inside a Validation
  2. Splitting into Logic outside the Validation

Common logic

you may find Common logic useful for multiple Validations. For instance, IsNullOrEmpty, IsInRange,... You may implement this and reuse it inside a Validation or cross multiple Validations. If you want to use it across multiple Validations, you need to use the Include command to reference external logic to a Validation.

RuleSet and ModelContainer

In order to create a RuleSet, you simply load your ModelContainer and add it to a RuleSet collection using the AddRule method.

  1. private RuleSet GetRuleSet()
  2. {
  3.     RuleSet rs = RuleSet.HierarchicalRuleSet();
  4.     rs.AddModel("Common/Strings", LoadAdapterUtility.LoadModel("Common.IsNullOrEmpty.xml"));
  5.     return rs;
  6. }

Then you can use the RuleSet to create your Validator

  1. private Validator GetValidator()
  2. {
  3.     RuleSet rs = GetRuleSet();
  4.     // append new logic to ruleset
  5.     rs.AddModel("rules/validations", LoadAdapterUtility.LoadModel("Conditional_requiredField_UsingCommonRules.xml"));
  6.  
  7.     // creating Validator directly, or we could use RuntimeEngine instead
  8.     var validator = new Validator(rs, "ruleset://rules/validations/PersonValidation");
  9.     return validator;
  10. }

Direct file reference

Final Sample

  1. <Logic name="NullOrEmpty" variable="$input">
  2.   <Or>
  3.     <Null value="$input"/>
  4.     <Empty value="$input"/>
  5.   </Or>
  6. </Logic>

Referencing use RuleSet and ModelContainer

  1. <Validation name="PersonValidation">
  2.   <!-- Referencing external logic -->
  3.   <Include source="ruleset://Common/Strings/NullOrEmpty"/>
  4.  
  5.   <Logic name="check email using False">
  6.     <False message="Adult must have an email">
  7.       <When>
  8.         <And processAll="true">
  9.           <Check value= "Age>=18"/>
  10.           <Validate logic="NullOrEmpty" value="Email"/>
  11.         </And>
  12.       </When>
  13.     </False>
  14.   </Logic>
  15.  
  16. </Validation>

Next sections

In the next couple of articles we will cover different aspects of modelling and executing the validation rules using a Validation engine.

  1. Introduction to validation rules
  2. Validating hierarchy (Inheritance relation)
  3. Validating association (Aggregation, Composition)
  4. Validation rule execution and collecting results
  5. Pass extra input values to validation rules
  6. Extending validation conditions and actions
  7. How to apply rules under some conditions
  8. Referencing commonly used logic
  9. Sample for Order processing validation logic