Decision tables: Difference between revisions
Content added Content deleted
m (bug fix) |
m (Alphabetize) |
||
Line 1: | Line 1: | ||
{{draft task}} |
{{draft task}} |
||
[[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|J}}== |
|||
'''Solution''':<lang j>require'strings' |
|||
'RULE_NAMES RULES'=: |:':'&cut;._2 noun define |
|||
Printer does not print: Y Y Y Y N N N N |
|||
A red light is flashing: Y Y N N Y Y N N |
|||
Printer is unrecognised: Y N Y N Y N Y N |
|||
) |
|||
'ACTION_NAMES ACTIONS'=: |:':'&cut;._2 noun define |
|||
Check the power cable: - - X - - - - - |
|||
Check the printer-computer cable: X - X - - - - - |
|||
Ensure printer software is installed: X - X - X - X - |
|||
Check/replace ink: X X - - X X - - |
|||
Check for paper jam: - X - X - - - - |
|||
) |
|||
assert (-:~.)|: 'Y' =/&;: RULES |
|||
RULE_TABLE=: (,/'X'=/&;: ACTIONS) /:&|: 'Y' =/&;: RULES |
|||
troubleshoot =: verb define |
|||
RULE_TABLE troubleshoot~ RULE_NAMES ,&< ACTION_NAMES |
|||
: |
|||
'q a'=.x |
|||
smoutput 'Having trouble? Let''s track down the problem:' |
|||
options=. a #~ y {~ #. 'Y'={.@toupper@deb@(1!:1)@1:@smoutput@,&'?'@dtb"1 q |
|||
(,~ ('/'cut'Suggested resolutions:/Solution unknown.') {::~ 0=#) options |
|||
)</lang> |
|||
'''Example''' (''solution found''):<lang j> troubleshoot '' |
|||
Having trouble? Let's track down the problem: |
|||
Printer does not print? |
|||
Y |
|||
A red light is flashing? |
|||
Y |
|||
Printer is unrecognised? |
|||
Y |
|||
Suggested resolutions: |
|||
Check the printer-computer cable |
|||
Ensure printer software is installed |
|||
Check/replace ink </lang> |
|||
'''Example''' (''solution not found''):<lang j> troubleshoot '' |
|||
Having trouble? Let's track down the problem: |
|||
Printer does not print? |
|||
N |
|||
A red light is flashing? |
|||
N |
|||
Printer is unrecognised? |
|||
N |
|||
Solution unknown. |
|||
</lang> |
|||
=== Comments === |
|||
The only interesting line in this solution is the one that assigns <tt>RULE_TABLE</tt>. The rest is mostly ancillary support. |
|||
For large numbers of rules and few actions, J's native support of sparse arrays might provide a performance advantage, particularly in space. A minor note about the implementation here: the verb (function) <tt>troubleshoot</tt> is generalized, and reusable on any set of rule table, questions, and suggested actions. The default is to use those given in the printer troubleshooting table. |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 135: | Line 79: | ||
Ensure printer software is installed |
Ensure printer software is installed |
||
Check/replace ink</pre> |
Check/replace ink</pre> |
||
=={{header|J}}== |
|||
'''Solution''':<lang j>require'strings' |
|||
'RULE_NAMES RULES'=: |:':'&cut;._2 noun define |
|||
Printer does not print: Y Y Y Y N N N N |
|||
A red light is flashing: Y Y N N Y Y N N |
|||
Printer is unrecognised: Y N Y N Y N Y N |
|||
) |
|||
'ACTION_NAMES ACTIONS'=: |:':'&cut;._2 noun define |
|||
Check the power cable: - - X - - - - - |
|||
Check the printer-computer cable: X - X - - - - - |
|||
Ensure printer software is installed: X - X - X - X - |
|||
Check/replace ink: X X - - X X - - |
|||
Check for paper jam: - X - X - - - - |
|||
) |
|||
assert (-:~.)|: 'Y' =/&;: RULES |
|||
RULE_TABLE=: (,/'X'=/&;: ACTIONS) /:&|: 'Y' =/&;: RULES |
|||
troubleshoot =: verb define |
|||
RULE_TABLE troubleshoot~ RULE_NAMES ,&< ACTION_NAMES |
|||
: |
|||
'q a'=.x |
|||
smoutput 'Having trouble? Let''s track down the problem:' |
|||
options=. a #~ y {~ #. 'Y'={.@toupper@deb@(1!:1)@1:@smoutput@,&'?'@dtb"1 q |
|||
(,~ ('/'cut'Suggested resolutions:/Solution unknown.') {::~ 0=#) options |
|||
)</lang> |
|||
'''Example''' (''solution found''):<lang j> troubleshoot '' |
|||
Having trouble? Let's track down the problem: |
|||
Printer does not print? |
|||
Y |
|||
A red light is flashing? |
|||
Y |
|||
Printer is unrecognised? |
|||
Y |
|||
Suggested resolutions: |
|||
Check the printer-computer cable |
|||
Ensure printer software is installed |
|||
Check/replace ink </lang> |
|||
'''Example''' (''solution not found''):<lang j> troubleshoot '' |
|||
Having trouble? Let's track down the problem: |
|||
Printer does not print? |
|||
N |
|||
A red light is flashing? |
|||
N |
|||
Printer is unrecognised? |
|||
N |
|||
Solution unknown. |
|||
</lang> |
|||
=== Comments === |
|||
The only interesting line in this solution is the one that assigns <tt>RULE_TABLE</tt>. The rest is mostly ancillary support. |
|||
For large numbers of rules and few actions, J's native support of sparse arrays might provide a performance advantage, particularly in space. A minor note about the implementation here: the verb (function) <tt>troubleshoot</tt> is generalized, and reusable on any set of rule table, questions, and suggested actions. The default is to use those given in the printer troubleshooting table. |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
We allow ourselves a luxurious user interface: |
We allow ourselves a luxurious user interface: |