Decision tables: Difference between revisions

eliminate overly long lines, and dependence on invisible tab character
(bug fix)
(eliminate overly long lines, and dependence on invisible tab character)
Line 4:
=={{header|J}}==
'''Solution''':<lang j>require'strings'
( , (,.~'_NAMES',L:0~])RULE_NAMES ;:'RULES ACTIONS' ) =: , > split&.|:L:1 }.&.> (split~ (<'Actions:') i.&1@:= {cut;."1) }."1 TAB cut&> LF cut_2 noun define
Printer isdoes not print: unrecognised Y NY Y N Y N YN N N
Printer troubleshooter:
PrinterA doesred notlight printis flashing: Y Y Y Y N N Y Y N N
A red lightPrinter is flashingunrecognised: Y N Y N Y N Y Y N N
Printer is unrecognised Y N Y N Y N Y N
Actions:
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
)
 
'ACTION_NAMES ACTIONS'=: |:':'&cut;._2 noun define
boxIdx =: <"1@:(<"0)
Check the power cable: - - X - - - - -
RULE_TABLE =: ACTIONS ( (<,'X') = [ )`([: boxIdx (<,'Y') = ])`(0 $~ (,~ $&2)&({:@$)) } RULES
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~ RULES_NAMESRULE_NAMES ,&< ACTIONS_NAMESACTION_NAMES
:
'Rq Aa'=.x
smoutput 'Having trouble? Let''s track down the problem:'
> (,~ (<;._2'Suggested resolutions: /Solution unknown./') {~ 0options=#) TAB,&.> Aa #~ y {~ boxIdx#. (<,'Y')={.@toupper@:deb@(1!:1)@:1:@:smoutput@:(TAB , ,&'?')&.>dtb"1 Rq
(,~ ('/'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:
Line 53 ⟶ 54:
 
=== Comments ===
The only interesting line in this solution is the one that assigns <tt>RULE_TABLE</tt>. The rest is mostly ancillary support. This is because the task highlights some freedoms J's multidimensional nature provides to the programmer.
 
Here, we create an array with as many axes (dimensions) as there are rules (questions), plus one. All axes, except the last, have 2 points (to wit: false, true). The last axis contains a boolean vector indicating which actions to try (or not). Thus, J's multidimensional array is leveraged as a tree, and the user's answers provide a path to a leaf (which is accessed arraywise, i.e. all at once).
 
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.
6,962

edits