Decision table authoring
Write a decision table in a spreadsheet and turn it into the JSON of rules eval with rules from_csv or rules from_rows, then check it with rules check: all syntax errors at once with their rule and column, rules that overlap, input combinations that no rule covers, rules that can never fire and values that are not allowed. Typical uses: let business people maintain rules in a sheet, review a pricing grid before putting it in production, find the typo that makes a rule useless.
rules from_csv <csv>
Description
A decision table from a CSV sheet: the JSON table, ready for rules eval
A decision table from a CSV sheet: the JSON table, ready for rules eval
Parameters
csv: The CSV text: a header row, then one row per rule (4 MB, 10000 rules at most) - String - required
csv: The CSV text: a header row, then one row per rule (4 MB, 10000 rules at most) - String - required
admin
rules from_csv "customer;orderSize;discount Business;<10;0.1 Business;>=10;0.15 Private;-;0.05"
mentdb
{"hit_policy": "UNIQUE", "inputs": [{"name": "customer", "type": "string"}, {"name": "orderSize", "type": "number"}], "outputs": [{"name": "discount", "type": "number"}], "rules": [{"when": ["Business", "<10"], "then": [0.1]}, {"when": ["Business", ">=10"], "then": [0.15]}, {"when": ["Private", "-"], "then": [0.05]}]}
rules from_csv <csv> <json_params>
Description
A decision table from a CSV sheet with parameters: hit_policy (UNIQUE), aggregation, name, outputs (1: the number of last columns, or a list of names), types ({column: type}), values_row (false), id_column, description_column, default ({output: value}), separator (; , | or tab)
A decision table from a CSV sheet with parameters: hit_policy (UNIQUE), aggregation, name, outputs (1: the number of last columns, or a list of names), types ({column: type}), values_row (false), id_column, description_column, default ({output: value}), separator (; , | or tab)
Parameters
csv: The CSV text - String - required
json_params: The parameters, as a JSON object - String - required
csv: The CSV text - String - required
json_params: The parameters, as a JSON object - String - required
admin
rules from_csv "customer;orderSize;discount Business;<10;0.1 Business;>=10;0.15 Private;-;0.05" "{\"hit_policy\": \"FIRST\", \"default\": {\"discount\": 0}}"
mentdb
{"hit_policy": "FIRST", "inputs": [{"name": "customer", "type": "string"}, {"name": "orderSize", "type": "number"}], "outputs": [{"name": "discount", "type": "number", "default": 0}], "rules": [{"when": ["Business", "<10"], "then": [0.1]}, {"when": ["Business", ">=10"], "then": [0.15]}, {"when": ["Private", "-"], "then": [0.05]}]}
PRIORITY with the values row, ids and comments
admin
rules from_csv "id;risk;amount:number;status;comment ;;;\"DECLINE\",\"REFER\",\"ACCEPT\"; R1;-;<=5000;ACCEPT;small amounts R2;high;-;DECLINE; R3;medium,high;>5000;REFER;to check by hand" "{\"hit_policy\": \"PRIORITY\", \"values_row\": true, \"id_column\": \"id\", \"description_column\": \"comment\", \"outputs\": [\"status\"]}"
mentdb
{"hit_policy": "PRIORITY", "inputs": [{"name": "risk", "type": "string"}, {"name": "amount", "type": "number"}], "outputs": [{"name": "status", "type": "string", "values": ["DECLINE", "REFER", "ACCEPT"]}], "rules": [{"id": "R1", "description": "small amounts", "when": ["-", "<=5000"], "then": ["ACCEPT"]}, {"id": "R2", "when": ["high", "-"], "then": ["DECLINE"]}, {"id": "R3", "description": "to check by hand", "when": ["medium,high", ">5000"], "then": ["REFER"]}]}
Used in use cases (1 recipe)
Financial analyst › Apply a discount grid that the sales team keeps in a spreadsheet
Financial analyst › Apply a discount grid that the sales team keeps in a spreadsheet
rules from_rows <rows>
Description
A decision table from rows: a JSON array whose first element is the header, or a JSON array of objects (the keys of the first object are the columns)
A decision table from rows: a JSON array whose first element is the header, or a JSON array of objects (the keys of the first object are the columns)
Parameters
rows: The rows, as JSON - String - required
rows: The rows, as JSON - String - required
admin
rules from_rows "[[\"country\", \"weight\", \"carrier\"], [\"FR\", \"<=2\", \"La Poste\"], [\"FR\", \">2\", \"Colissimo\"], [\"not(FR)\", null, \"DHL\"]]"
mentdb
{"hit_policy": "UNIQUE", "inputs": [{"name": "country", "type": "string"}, {"name": "weight", "type": "number"}], "outputs": [{"name": "carrier", "type": "string"}], "rules": [{"when": ["FR", "<=2"], "then": ["La Poste"]}, {"when": ["FR", ">2"], "then": ["Colissimo"]}, {"when": ["not(FR)", "-"], "then": ["DHL"]}]}
From objects (csv read, sql to json)
admin
rules from_rows "[{\"segment\": \"retail\", \"rate\": 2}, {\"segment\": \"pro\", \"rate\": 5}]"
mentdb
{"hit_policy": "UNIQUE", "inputs": [{"name": "segment", "type": "string"}], "outputs": [{"name": "rate", "type": "number"}], "rules": [{"when": ["retail"], "then": [2]}, {"when": ["pro"], "then": [5]}]}
rules from_rows <rows> <json_params>
Description
A decision table from rows with the parameters of rules from_csv (except separator)
A decision table from rows with the parameters of rules from_csv (except separator)
Parameters
rows: The rows, as JSON - String - required
json_params: The parameters, as a JSON object - String - required
rows: The rows, as JSON - String - required
json_params: The parameters, as a JSON object - String - required
admin
rules from_rows "[[\"basket\", \"member\", \"points\"], [\">=50\", \"-\", 5], [\">=100\", \"-\", 10], [\"-\", \"true\", 2]]" "{\"hit_policy\": \"C+\"}"
mentdb
{"hit_policy": "C+", "inputs": [{"name": "basket", "type": "number"}, {"name": "member", "type": "boolean"}], "outputs": [{"name": "points", "type": "number"}], "rules": [{"when": [">=50", "-"], "then": [5]}, {"when": [">=100", "-"], "then": [10]}, {"when": ["-", "true"], "then": [2]}]}
rules check <table>
Description
The problems of a decision table: JSON {valid, hit_policy, inputs, outputs, rules, errors, warnings, analyzed, complete, overlaps, overlaps_total, gaps, gaps_total, gaps_note, never_fire}. valid is false when the table has an error or an overlap that breaks its hit policy; gaps and never_fire are for you to judge
The problems of a decision table: JSON {valid, hit_policy, inputs, outputs, rules, errors, warnings, analyzed, complete, overlaps, overlaps_total, gaps, gaps_total, gaps_note, never_fire}. valid is false when the table has an error or an overlap that breaks its hit policy; gaps and never_fire are for you to judge
Parameters
table: The decision table, as JSON {"hit_policy", "inputs", "outputs", "rules"} (4 MB at most), for example the result of rules from_csv - String - required
table: The decision table, as JSON {"hit_policy", "inputs", "outputs", "rules"} (4 MB at most), for example the result of rules from_csv - String - required
admin
rules check "{\"hit_policy\": \"UNIQUE\", \"inputs\": [{\"name\": \"segment\", \"type\": \"string\", \"values\": [\"retail\", \"pro\", \"vip\"]}, {\"name\": \"amount\", \"type\": \"number\"}], \"outputs\": [\"rate:number\"], \"rules\": [[\"\\"retail\\"\", \"<100\", 0], [\"\\"retail\\"\", \"[100..500]\", 2], [\"\\"pro\\", \\"vip\\"\", \">=0\", 5], [\"\\"vip\\"\", \">1000\", 8], [\"\\"retial\\"\", \"-\", 1]]}"
mentdb
{"valid": false, "hit_policy": "UNIQUE", "inputs": 2, "outputs": 1, "rules": 5, "errors": [], "warnings": [{"kind": "warning", "rule": "5", "column": "segment", "message": "the value \"retial\" is not an allowed value of the input"}], "analyzed": true, "complete": true, "overlaps": [{"rules": ["3", "4"], "example": {"segment": "\"vip\"", "amount": ">1000"}}], "overlaps_total": 1, "gaps": [{"segment": "\"pro\"", "amount": "<0"}, {"segment": "\"retail\"", "amount": ">500"}, {"segment": "\"vip\"", "amount": "<0"}], "gaps_total": 3, "gaps_note": "no rule matches the inputs of the gaps: the result is null", "never_fire": [{"rule": "5", "reason": "its entry for 'segment' matches no allowed value"}]}
FIRST: a rule hidden by an earlier one
admin
rules check "{\"hit_policy\": \"FIRST\", \"inputs\": [{\"name\": \"age\", \"type\": \"number\", \"integer\": true}], \"outputs\": [\"band:string\"], \"rules\": [[\"[0..17]\", \"child\"], [\"[18..64]\", \"adult\"], [\">=65\", \"senior\"], [\"[30..40]\", \"thirties\"]]}"
mentdb
{"valid": true, "hit_policy": "FIRST", "inputs": 1, "outputs": 1, "rules": 4, "errors": [], "warnings": [], "analyzed": true, "complete": true, "gaps": [{"age": "<0"}], "gaps_total": 1, "gaps_note": "no rule matches the inputs of the gaps: the result is null", "never_fire": [{"rule": "4", "reason": "every input it matches is taken by rule 2 (hit policy FIRST)"}]}
Syntax errors, all at once
admin
rules check "{\"inputs\": [\"age:number\", \"city:string\"], \"outputs\": [\"zone:string\"], \"rules\": [[\"<18 x\", \"Paris\", \"A\"], [\"[a..b]\", \"A (FR)\", \"B\"]]}"
mentdb
{"valid": false, "hit_policy": "UNIQUE", "inputs": 2, "outputs": 1, "rules": 2, "errors": [{"kind": "error", "rule": "1", "column": "age", "message": "unexpected text near 'x' in '<18 x'"}, {"kind": "error", "rule": "2", "column": "age", "message": "a number is expected near 'a..b]' in '[a..b]'"}, {"kind": "error", "rule": "2", "column": "city", "message": "write this text in double quotes near '(FR)' in 'A (FR)'"}], "warnings": [], "analyzed": false, "reason": "the table has errors: fix them, then check again to see gaps, overlaps and rules that never fire"}
rules check <table> <json_params>
Description
The problems with parameters: max_gaps (50: the gaps given in the list; gaps_total counts them all), max_overlaps (100)
The problems with parameters: max_gaps (50: the gaps given in the list; gaps_total counts them all), max_overlaps (100)
Parameters
table: The decision table, as JSON {"hit_policy", "inputs", "outputs", "rules"} (4 MB at most), for example the result of rules from_csv - String - required
json_params: The parameters, as a JSON object - String - required
table: The decision table, as JSON {"hit_policy", "inputs", "outputs", "rules"} (4 MB at most), for example the result of rules from_csv - String - required
json_params: The parameters, as a JSON object - String - required
admin
rules check "{\"hit_policy\": \"UNIQUE\", \"inputs\": [{\"name\": \"segment\", \"type\": \"string\", \"values\": [\"retail\", \"pro\", \"vip\"]}, {\"name\": \"amount\", \"type\": \"number\"}], \"outputs\": [\"rate:number\"], \"rules\": [[\"\\"retail\\"\", \"<100\", 0], [\"\\"retail\\"\", \"[100..500]\", 2], [\"\\"pro\\", \\"vip\\"\", \">=0\", 5], [\"\\"vip\\"\", \">1000\", 8], [\"\\"retial\\"\", \"-\", 1]]}" "{\"max_gaps\": 1}"
mentdb
{"valid": false, "hit_policy": "UNIQUE", "inputs": 2, "outputs": 1, "rules": 5, "errors": [], "warnings": [{"kind": "warning", "rule": "5", "column": "segment", "message": "the value \"retial\" is not an allowed value of the input"}], "analyzed": true, "complete": true, "overlaps": [{"rules": ["3", "4"], "example": {"segment": "\"vip\"", "amount": ">1000"}}], "overlaps_total": 1, "gaps": [{"segment": "\"pro\"", "amount": "<0"}], "gaps_total": 3, "gaps_note": "no rule matches the inputs of the gaps: the result is null", "never_fire": [{"rule": "5", "reason": "its entry for 'segment' matches no allowed value"}]}
Used in use cases (3 recipes)
Financial analyst › Apply a discount grid that the sales team keeps in a spreadsheet
Logistics & supply chain › Choose the carrier of each parcel with a table checked before use
Financial analyst › Pre-approve a loan with a priority table and keep the reasons in the file
Financial analyst › Apply a discount grid that the sales team keeps in a spreadsheet
Logistics & supply chain › Choose the carrier of each parcel with a table checked before use
Financial analyst › Pre-approve a loan with a priority table and keep the reasons in the file
Often used with
rules eval_rows · rules eval · rules explain · rules from_csv · string char · table group_by
rules eval_rows · rules eval · rules explain · rules from_csv · string char · table group_by
