User Defined Functions and Variable Execution of Rule Sets
Recently we have had multiple requests to create a dynamic and variable execution structure for our rule sets. Often times we have metadata in our systems that determine which rule sets should be executed. One of the benefits to this method is that you can easily control which rule sets will execute simply by updating the related metadata in your database. So today, we’ll illustrate an example of a low-code dynamic execution structure to create a manifest of rule sets and execute them.
In this use case we are going to assume that we have a table that will contain the metadata of what rule sets are to be executed based on an action type field that is a key to a different table. A quick join from the tables would look like so:
So for example an ActionTypeID of 2 (Denial) it will execute the rule set, “Deny Application” and “Deactivate All Applicants” while an ActionTypeID of 3 (Certificiation) will execute “Certify Application” and “Activate All Applicants”.
We can accomplish this relatively simply by attaching a collection connected to our top level entity that will contain our rule set name to execute.
Next, we’ll use a SQL query to populate our rules collection and create a rule to fire that query (note we clear out the collection before firing to keep the execution clean).
To fire those rules, we just need to create a small, one-line User Defined Function to execute the rule set for the entity containing the rules. We’re going to use a parameter named, “ruleToApply” for the rule set we’re going to execute and pass in our top level Application Entity.
Then to execute the rule, we will create a rule in the context of our Business Rule Entity utilizing the Execute Method action. If you remember, the values in the Rule Name field in the database had spaces between each word. In order for this method to execute correctly the rule names must match exactly. So we used a Replace function attached to the Rule Name field to remove any spaces so that our rule set names will match perfectly.
Finally, we create our Execution Controller rule. We will have it execute two actions, the first will fire the rule set to clear and load our rule manifest, then an Execute Member Rule Set to fire each rule set in our Rules Manifest collection.
By using these methods, one can quickly and easily develop an execution controller that will enable your organization to quicklyand efficiently choose which rule set will execute for a variety of conditions.