Decision tables: Difference between revisions
Content deleted Content added
m Alphabetize |
No edit summary |
||
Line 2: | Line 2: | ||
[[wp:Decision_table|Decision Tables]] are a precise yet compact way to model complicated logic. Demonstrate how your language implements decision tables. Use the example of Printer Troubleshooting given in the Wikipedia article. |
[[wp:Decision_table|Decision Tables]] are a precise yet compact way to model complicated logic. Demonstrate how your language implements decision tables. Use the example of Printer Troubleshooting given in the Wikipedia article. |
||
=={{header|Ada}}== |
|||
First the specification of a generic decision table package: |
|||
<lang Ada>generic |
|||
type Condition is (<>); |
|||
type Action is (<>); |
|||
with function Ask_Question (Cond: Condition) return Boolean; |
|||
with procedure Give_Answer (Act: Action); |
|||
with procedure No_Answer; |
|||
package Generic_Decision_Table is |
|||
type Answers is array(Condition) of Boolean; |
|||
type Rule_R is record |
|||
If_Then: Answers; |
|||
Act: Action; |
|||
end record; |
|||
type Rule_A is array(Positive range <>) of Rule_R; |
|||
procedure React(Rules: Rule_A); |
|||
end Generic_Decision_Table; |
|||
</lang> |
|||
Next, the implementation of the generic decision table package: |
|||
<lang Ada>package body Generic_Decision_Table is |
|||
procedure React(Rules: Rule_A) is |
|||
A: Answers; |
|||
Some_Answer: Boolean := False; |
|||
begin |
|||
for C in A'Range loop |
|||
A(C) := Ask_Question(C); |
|||
end loop; |
|||
for R in Rules'Range loop |
|||
if A = Rules(R).If_Then then |
|||
Give_Answer(Rules(R).Act); |
|||
Some_Answer := True; |
|||
end if; |
|||
end loop; |
|||
if not Some_Answer then |
|||
No_Answer; |
|||
end if; |
|||
end React; |
|||
end Generic_Decision_Table; |
|||
</lang> |
|||
Finally, the Printer Troubleshooting program, using the generic decision table: |
|||
<lang Ada>with Generic_Decision_Table, Ada.Text_IO; |
|||
procedure Printer_Decision_Table is |
|||
type Condition is (Does_Not_Print, Red_Light_Flashing, Unrecognised); |
|||
type Action is (Power_Cable, Printer_Computer_Cable, Software_Installed, |
|||
New_Ink, Paper_Jam); |
|||
function Question(Cond: Condition) return Boolean is |
|||
use Ada.Text_IO; |
|||
Ch: Character; |
|||
begin |
|||
case Cond is |
|||
when Does_Not_Print => |
|||
Put("Printer does not print?"); |
|||
when Red_Light_Flashing => |
|||
Put("A red light is flashing?"); |
|||
when Unrecognised => |
|||
Put("Printer is unrecognised?"); |
|||
end case; |
|||
Put_Line (" y/Y => 'yes', any other input: 'no'"); |
|||
Get(Ch); |
|||
return (Ch='y') or (Ch='Y'); |
|||
end Question; |
|||
procedure Answer(Act: Action) is |
|||
use Ada.Text_IO; |
|||
begin |
|||
case Act is |
|||
when Power_Cable => |
|||
Put_Line("Check the power cable!"); |
|||
when Printer_Computer_Cable => |
|||
Put_Line("Check the printer-computer cable!"); |
|||
when Software_Installed => |
|||
Put_Line("Ensure the printer software is installed!"); |
|||
when New_Ink => |
|||
Put_Line("Check/replace ink!"); |
|||
when Paper_Jam => |
|||
Put_Line("Check for paper jam!"); |
|||
end case; |
|||
end Answer; |
|||
procedure No_Answer is |
|||
begin |
|||
Ada.Text_IO.Put_Line("Sorry! I have no idea what to do now!"); |
|||
end No_Answer; |
|||
package DT is new Generic_Decision_Table |
|||
(Condition, Action, Question, Answer, No_Answer); |
|||
R: DT.Rule_A := (((True, False, True), Power_Cable), |
|||
((True, True, True), Printer_Computer_Cable), |
|||
((True, False, True), Printer_Computer_Cable), |
|||
((True, True, True), Software_Installed), |
|||
((True, False, True), Software_Installed), |
|||
((False, True, True), Software_Installed), |
|||
((False, False, True), Software_Installed), |
|||
((True, True, True), New_Ink), |
|||
((True, True, False), New_Ink), |
|||
((False, True, True), New_Ink), |
|||
((False, True, False), New_Ink), |
|||
((True, True, False), Paper_Jam), |
|||
((True, False, False), Paper_Jam) |
|||
); |
|||
begin |
|||
DT.React(R); |
|||
end Printer_Decision_Table;</lang> |
|||
Sample output: |
|||
<pre>> ./printer_decision_table |
|||
Printer does not print? y/Y => 'yes', any other input: 'no' |
|||
y |
|||
A red light is flashing? y/Y => 'yes', any other input: 'no' |
|||
n |
|||
Printer is unrecognised? y/Y => 'yes', any other input: 'no' |
|||
n |
|||
Check for paper jam! |
|||
> ./printer_decision_table |
|||
Printer does not print? y/Y => 'yes', any other input: 'no' |
|||
y |
|||
A red light is flashing? y/Y => 'yes', any other input: 'no' |
|||
y |
|||
Printer is unrecognised? y/Y => 'yes', any other input: 'no' |
|||
n |
|||
Check/replace ink! |
|||
Check for paper jam! |
|||
> ./printer_decision_table |
|||
Printer does not print? y/Y => 'yes', any other input: 'no' |
|||
n |
|||
A red light is flashing? y/Y => 'yes', any other input: 'no' |
|||
n |
|||
Printer is unrecognised? y/Y => 'yes', any other input: 'no' |
|||
n |
|||
Sorry! I have no idea what to do now!</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.algorithm, std.exception, std.string, std.array, std.conv ; |
<lang d>import std.stdio, std.algorithm, std.exception, std.string, std.array, std.conv ; |