Extending logic

From FlexRule Wiki
Jump to: navigation, search


There are some scenarios when validation conditions require communication back to the application or execution of a custom behaviour. These situations can be categorised into two areas:

  • Validate a custom condition
  • Take a custom action

In these scenarios you can use Alias or Check commands to extend the validation engine.

Custom condition

Sometimes in validation rules you may need to access an external resource, or for example implement a custom condition to participate in a validation chain. You can accomplish these tasks simply by using either Alias or Check.

Using Check

To use the Check command you need to

  1. Add an input parameter to the rule
  2. Pass the defined parameter value to the rule when it is called
  3. Call Check command in the chain of the conditions

The following sample rule defines an input parameter and calls the custom logic using Check

  1. <Validation name="PersonValidation">
  2.   <Declaration>
  3.     <Define name="MinAge" direction="In"/>
  4. 	<!-- Define the input parameter -->
  5.     <Define name="fin" direction="In" />
  6.   </Declaration>
  8.   <Logic name="Logic with custom call">
  9.     <Group>
  10.       <Check value="fin.DbExists($this)"/>
  11.       <And />
  12.       <Null value="Name" negate="true"/>
  13.       <And />
  14.       <Empty value="Name" negate="true"/>
  15.       <Or />
  16.       <Null value="Family" negate="true"/>
  17.       <And />
  18.       <Empty value="Family" negate="true" />
  19.     </Group>
  20.   </Logic>
  22. </Validation>

The following code is the class definition of the fin parameter

  1. class PersonDbAccess
  2. {
  3.     public bool DbExists(object obj)
  4.     {
  5.         Person person = (Person) obj;
  6.         // Check if the person exists
  8.         return false;
  9.     }
  10. }

Using Alias

Note pinned.pngAlias command is not supported from version FlexRule Runtime v7.3. Instead Check command or direct expression to call a method can be used.

To read more about check here.

Sending Information to Method

Sometimes it is necessary that the method which implements Alias receives some additional information. This additional information is available via AliasContext. In AliasContext your code has the ability to access some settings of the alias command and make decisions based on these. By using this mechanism, the rule command can pass information to the method.

For example, in the following code we are passing a name attribute to the method in order to make a decision based on this:

  1. <Validation name="PersonValidation">
  2.   <Declaration>
  3.     <Define name="MinAge" direction="In"/>
  4.     <!-- Define the input parameter -->
  5.     <Define name="fin" direction="In" />
  6.     <!-- Define Alias for method DbExists named as FindPersonRecord -->
  7.     <Alias method="fin.DbExists" as="FindPersonRecord"/>
  8.   </Declaration>
  10.   <Logic name="Logic with custom call">
  11.     <Group>
  12.       <FindPersonRecord name="5044162C-3DAA-499C-87DC-E325366B10E3" />
  13.       <And />
  14.       <Null value="Name" negate="true"/>
  15.       <And />
  16.       <Empty value="Name" negate="true"/>
  17.       <Or />
  18.       <Null value="Family" negate="true"/>
  19.       <And />
  20.       <Empty value="Family" negate="true" />
  21.     </Group>
  22.   </Logic>
  24. </Validation>

The method looks similar to the following code. It also allows you to check for more detail about the Alias information by the IAliasInfo interface.

  1. class PersonDbAccess
  2. {
  3.     public bool DbExists(AliasContext context, object obj)
  4.     {
  5.         // Check for the Alias information: Name, Method and Reference
  6.         var aliasInfo = context.Alias;
  8.         // Check for the command settings
  9.         if (context.Dictionary.ContainsKey("name"))
  10.         {
  11.             string name = context.Dictionary["name"];
  12.             // Do what it is required with 'name' here
  13.         }
  15.         Person per = (Person)obj;
  16.         // Check the person record
  18.         return true;
  19.     }
  20. }

Custom actions

In the logic of the validation you can have a Then section. If the logic condition is satisfied, Then will be executed. There are two actions you may want to take in the Then section:

  1. Set a variable value
  2. Call your custom action

Set variable value

To set a variable value in the rule you simply define some output parameters and set them using the Var command.

  1. <Validation name="PersonValidation">
  2.   <Declaration>
  3.     <Define name="MinAge" direction="In"/>
  4.     <!-- Define an output parameter to be set in Then section -->
  5. 	<Define name="state" direction="Out" />
  6.   </Declaration>
  8.   <Logic name="basic validation 2">
  9.     <Group>
  10.       <Check value="Age &lt; MinAge"/>
  11.     </Group>
  12.     <Then>
  13.       <Var name="state" value="0i" />
  14.     </Then>
  15.   </Logic>
  17. </Validation>

Call your custom action

Create a custom cakes and inject it to your model using Input parameters.

  1. class PersonDbAccess
  2. {
  3.     public void WriteDb(object obj)
  4.     {
  5.         Person person = (Person) obj;
  6.         // Do whatever is required for the person
  7.     }
  8. }

Then use the expression and call them object member. In this case, the method of the object.

Next sections

In the next couple of articles we will cover different aspects of modelling and executing the validation rules using 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