Using an External Data Source
In RASON, it's possible to import input parameters from an external data-source within the dataSource section by using the binding "get"
method within the data section. In the example below (see Datasource example.json on the Editor page) the CSV file, DT Customers.txt is
imported into the RASON model within the datasources section. DT Customers.txt contains the following fields, as shown in the screenshot below:
custID, custAge and medHistory. The indexCols property is set to custID, which is, of course, the customer or patient ID. The valueCols
property is set to custAge and medHistory. Within the data section of the RASON model, custAge is bound to the age variable and medHistory
is bound to the med variable.
{
modelName: "DTDatasourceExammple",
modelType: "calculation",
modelDescription: "Decision table inputs from a datasource",
datasources: {
cust_data: {
type: "csv",
connection: "DT Customers.txt",
selection: "",
indexCols: ['custID'],
valueCols: ['custAge', 'medHistory'],
direction: "import"
}
},
data: {
age: { binding: 'cust_data', valueCol: 'custAge' },
med: { binding: 'cust_data', valueCol: 'medHistory' }
},
decisionTables: {
"PolicyUnique": {
hitPolicy: "U",
inputs: ['age', 'medHistory'],
outputs: ['riskRating', 'rule'],
rules: [
['>60,<25','good','medium','r1'],
['>60','bad','high','r2'],
['[25..60]','-','medium','r3'],
['<25','good','low','r4'],
['<25','bad','medium','r5']
]
}
},
formulas: {
"res": { formula: "PolicyUnique(,,age[1], med[1])", comment: "policy", finalValue: [] }
}
}
Decision Table Components
This table returns a risk rating for an individual based on their age and medical history (medHistory). A graphical representation of this
Decision Table is below. Notice that no Input or Output Values exist.
Decision Table Name: "PolicyUnique"
Hit Policy: "U" which stands for "Unique". A unique rule must "hit" evaluating to a unique result. If multiple rules are "hit",
an error will be returned.
Input Parameters: inputs: ['age', 'medHistory']
Input Values (Optional): No input values exist in this decision table.
Output Parameters: outputs: ['riskRating', 'rule'],
This example contains two output Parameters, riskRating and rule. After calculation, a table returns a few selected or all output parameter
values in the form of an array. If a table has a single output or a single output has been selected, the result will be a scalar value. These
input parameters belong to the local scope of this table.
Output Values (Optional): No output values exist in this decision table.
Decision Table Rules (or Unary tests):
rules: [
['>60,<25','good','medium','r1'],
['>60','bad','high','r2'],
['[25..60]','-','medium','r3'],
['<25','good','low','r4'],
['<25','bad','medium','r5']
]
Decision Table Results
In the formulas section, the table is calculated based on the variables age and med. Recall that these variables are bound to the data source
valuecols of custAge and medHistory, respectively.
"res": { formula: "PolicyUnique(,,age[1], med[1])", comment: "policy", finalValue: [] }
Decision Tables accept only scalar arguments as inputs. Since “age” and “med” were created by binding to an array, we pass only the first element
as in age[1] and med[1]. (Alternatively, we could have also bound a variable to a table.)
In this example, given an age of 54 and a "good" medical history, a "medium" risk rating is returned by rule 3.
Getting model results: GET https://rason.net/api/model/2590+DTDatasourceExample+2020-02-27-17-19-41-239540/result
{
"status": {
"code": 0,
"id": "2590+DTDatasourceExample+2020-02-27-17-19-41-239540",
"codeText": "Solver has completed the calculation."
},
"observations": {
"res": {
"value": [
["medium", "r3"]
]
}
}
The result is an array despite the ‘U’ policy, because 2 outputs are returned: "medium" and "r3".
This example demonstrates an implicit array return from a Decision table. In RASON, array results can be processed later through the
index operator [ ]. To access the first element res array, use res.value[1], to access the 2nd element, use res.value[2].
|