Embedded rules

Decision Tables and Scripting Rules are strong tools for decision making, but they can be even stronger when working together.

Leos Rehacek
Desarrollador Fullstack
?
Infórmate
Check icon A checkmark inside a circle signifying "yes" Minus icon A minus inside a circle signifying "no" PROS Icon A plus symbol representing positive aspects or benefits. CONS Icon A minus symbol representing negative aspects or drawbacks.

As a DecisionRules user, you may already know that you can create rules in the form of Decision Tables or Scripting Rules. These two features can be also combined in Rule flow, which is used for more complex rules/conditions. However, it is also possible to call the Decision Table directly from the Scripting rule!

Why is this feature useful?

Decision Tables and Scripting Rules differ in the ease of creating the conditions inside DecisionRules. Decision Tables are designed as user-friendly as possible even for non-technical people. Even though we offer a large set of different operations and operators, sometimes it can happen that it is still not enough for certain more complex use-cases.

For a better picture, imagine the case when you are letting your employees / teams / customers register to your system. But for being registered, for example for certain benefits, the user needs to accomplish some conditions. Sure, the majority of those conditions could be easily specified in Decision Tables. But sometimes there are conditions that must be evaluated within a group of users. And exactly at this moment, embedded rules in Scripting Rules are coming to the scene.

How to call a rule from Scripting Rules

This feature can be used very easily. All you need is to know the ruleID and employ the right method. The method is called solve().

// Get data from Input
const data = input.data;
// Check if data is in Input and if data is an Array
if(data && Array.isArray(data)){
const dates = [];
// Iterate over data array
for(let dat of data){
// Check if dat (member of data array) has parameter date;
if(dat.date){
// add date to dates
dates.push(dat.date);
}
}
// Create BULK input data for DT
const DTdata = [];
for(let date of dates){
const input = {
"age": date
}
DTdata.push(input);
}
// log input data for DT
log(JSON.stringify(DTdata));
// Call first DT with FIRST_MATCH strategy
const DTresult = DR.solve("0000-0000-0000-0000", DTdata, 1, SolverStrategy.FIRST_MATCH);
// log result from DT
log(JSON.stringify(DTresult));
// transform data from DT to another DT
let blocked = 0;
for(let res of DTresult){
if(Array.isArray(res)){
if(res[0].result && res[0].result == "BLOCKED"){
blocked ++;
}
}
}
// Create input data for DT
const DTdata2 = {
"total amount": dates.length,
"blocked": blocked
}
// log input data for DT
log(JSON.stringify(DTdata2));
// Call second DT with FIRST_MATCH strategy
const DTresult2 = DR.solve("1111-1111-1111-1111", DTdata2, 1, SolverStrategy.FIRST_MATCH);
// log result from DT
log(JSON.stringify(DTresult2));
// pass result from DT to SR result
output.result = DTresult2[0].result;
}
return output;

As you can see, the rule is called via DR.solve(ruleId, data, version, SolverStrategy). For more details, see the tutorial in our documentation.

From the previous formula it is now visible that there are other parameters that have to be specified before running the solver. The first parameter, as was mentioned before, is the ruleId. Then obviously you need to have input data for your solver, and then specify the version of the rule you are going to use. The last parameter is the execution strategy (Standard, First match, ...) and you are good to go!

To find out more, check our documentation or log in to the app and try it out on your own.

Thank you for reading!

¿Te gustó la lectura? Demos el siguiente paso juntos.