Decision tables: Difference between revisions
Content added Content deleted
(Added COBOL) |
(usable code object in javascript) |
||
Line 851: | Line 851: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===Interactive=== |
|||
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo. |
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo. |
||
<lang javascript><html> |
<lang javascript><html> |
||
Line 908: | Line 909: | ||
</script> |
</script> |
||
</html></lang> |
</html></lang> |
||
===DecTab object=== |
|||
This implements a DecisionTable object called <code>DecTab</code>. The JavaScript dialect is Microsoft's JScript. |
|||
<lang javascript> |
|||
var DecTab = function () { |
|||
this.conditions = []; |
|||
this.rules = []; |
|||
this.actions = []; |
|||
} |
|||
DecTab.prototype.Conditions = function () { |
|||
var args = [].slice.call(arguments); |
|||
for (var i = 0; i < args.length; i++) { |
|||
this.conditions.push(args[i]); |
|||
} |
|||
return this; |
|||
}; |
|||
DecTab.prototype.RulesActions = function (rules, actions) { |
|||
var count = 0; |
|||
var actionable; |
|||
this.rules.push(rules) |
|||
this.actions.push(actions); |
|||
return this; |
|||
}; |
|||
DecTab.prototype.Decide = function () { |
|||
var decision = []; |
|||
var decided = false; |
|||
for (var i = 0; i < this.conditions.length; i++) { |
|||
decision.push((this.conditions[i]())); |
|||
} |
|||
var decisionstring = "." + decision.join(".") + "."; |
|||
for (i = 0; i < this.rules.length; i++) { |
|||
var rule = []; |
|||
for (var j = 0; j < this.rules[i].length; j++) { |
|||
rule.push((this.rules[i][j])); |
|||
} |
|||
var rulestring = "." + rule.join(".") + "."; |
|||
if (rulestring === decisionstring) { |
|||
decided = true; |
|||
for (var k = 0; k < this.actions[i].length; k++) { |
|||
this.actions[i][k](); |
|||
} |
|||
break; |
|||
} |
|||
} |
|||
if (!decided) { |
|||
WScript.Echo("No decision table rows matched."); |
|||
} |
|||
} |
|||
</lang> |
|||
The task is rendered as follows |
|||
<lang javascript> |
|||
function Ask(q) { |
|||
WScript.StdOut.Write(q); |
|||
WScript.StdOut.Write(" [Y/N]?"); |
|||
var ans = WScript.StdIn.ReadLine(); |
|||
ans = ans.substr(0, 1).toUpperCase() === "Y" ? "Y" : "N"; |
|||
return ans; |
|||
} |
|||
function Tell(w) { |
|||
WScript.Echo(w); |
|||
} |
|||
function Check_the_power_cable() { |
|||
Tell("Check the power cable."); |
|||
} |
|||
function Check_the_printer_computer_cable() { |
|||
Tell("Check the printer-computer cable."); |
|||
} |
|||
function Ensure_printer_software_is_installed() { |
|||
Tell("Ensure printer software is installed."); |
|||
} |
|||
function Check_replace_ink() { |
|||
Tell("Check/replace ink."); |
|||
} |
|||
function Check_for_paper_jam() { |
|||
Tell("Check for paper jam."); |
|||
} |
|||
function Printer_Prints() { |
|||
return Ask("Printer prints"); |
|||
} |
|||
function A_red_light_is_flashing() { |
|||
return Ask("A red light is flashing"); |
|||
} |
|||
function Printer_is_recognized_by_computer() { |
|||
return Ask("Printer is recognized by computer"); |
|||
} |
|||
var DT = new DecTab() |
|||
.Conditions( |
|||
Printer_Prints, |
|||
A_red_light_is_flashing, |
|||
Printer_is_recognized_by_computer) |
|||
.RulesActions(["N", "Y", "N"], [Check_the_printer_computer_cable, Ensure_printer_software_is_installed, Check_replace_ink]) |
|||
.RulesActions(["N", "Y", "Y"], [Check_replace_ink, Check_for_paper_jam]) |
|||
.RulesActions(["N", "N", "N"], [Check_the_power_cable, Check_the_printer_computer_cable, Ensure_printer_software_is_installed]) |
|||
.RulesActions(["N", "N", "Y"], [Check_for_paper_jam]) |
|||
.RulesActions(["Y", "Y", "N"], [Ensure_printer_software_is_installed, Check_replace_ink]) |
|||
.RulesActions(["Y", "Y", "Y"], [Check_replace_ink]) |
|||
.RulesActions(["Y", "N", "N"], [Ensure_printer_software_is_installed]) |
|||
.RulesActions(["Y", "N", "Y"], []) |
|||
.Decide(); |
|||
</lang> |
|||
And a sample run |
|||
<lang text> |
|||
C:\>CScript DecisionTable.js |
|||
Printer prints [Y/N]?Y |
|||
A red light is flashing [Y/N]?Y |
|||
Printer is recognized by computer [Y/N]?Y |
|||
Check/replace ink. |
|||
</lang> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |