Decision tables: Difference between revisions

Line 1,804:
Check/replace ink
2 answers
</pre>
 
=={{header|Picat}}==
<lang Picat>import util.
%
% Interactive version.
% Answer with YnNn
%
go =>
conditions(Conditions),
solutions(Solutions),
 
Answers=query(Conditions),
println(join(decision_table(Solutions,Answers),'\n')),
nl.
 
%
% Query the user about the problem.
% Valid responses: YyNn
%
query(Conditions) = Answers =>
Answers = [0 : _I in 1..Conditions[1,2].length],
println("Please answer the questions with YyNn."),
foreach([Text,Cond] in Conditions)
A = "",
while (not(membchk(A, ["Y","N","y","n"])))
printf("%s? ", Text),
A := read_line(),
if membchk(A,["Y","y"]) then
Answers := map(\/, Answers,Cond)
end
end
end,
nl.
 
%
% Get the result given the answers (list of 01).
%
decision_table(Solutions,Answers) = Result =>
Result = [],
if sum(Answers) = 0 then
Result := ["Nice, no problem."]
else
foreach([Text,Sol] in Solutions)
if map(/\,Sol,Answers) == Sol then
Result := Result ++ [Text]
end
end
end.
 
 
conditions(Conditions) =>
Conditions = [
["Printer does not print", [1,1,1,1,0,0,0,0]],
["A red light is flashing", [1,1,0,0,1,1,0,0]],
["Printer is unrecognised", [1,0,1,0,1,0,1,0]]
].
 
solutions(Solutions) =>
Solutions = [
["Check the power cable", [0,0,1,0,0,0,0,0]],
["Check the printer-computer cable", [1,0,1,0,0,0,0,0]],
["Ensure printer software is installed",[1,0,1,0,1,0,1,0]],
["Check/replace ink", [1,1,0,0,1,1,0,0]],
["Check for paper jam", [0,1,0,1,0,0,0,0]]
].</lang>
 
Sample run:
<pre>
Please answer the questions with YyNn.
Printer does not print? y
A red light is flashing? n
Printer is unrecognised? y
 
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
Check for paper jam</pre>
 
For debugging purposes it it's nice to be able to print all the possible combinations of answers/solutions.
 
<lang Picat>%
% Go through all combinations of answers
%
go2 =>
PS = power_set([1,2,3]).delete([]).sort(),
println(PS),
conditions(Conditions),
solutions(Solutions),
AllCond = [Cond : [_,Cond] in Conditions],
foreach(P in PS)
println(p=P),
println(conditions=join([Conditions[I].first() : I in P],', ')),
A = [0:_I in 1..Conditions[1,2].length],
foreach(C in [AllCond[I] : I in P])
A := map(\/,A,C)
end,
println(a=A),
println(join(decision_table(Solutions,A),'\n')),
nl
end,
nl.</lang>
 
Output (truncated):
<pre>
p = [1]
conditions = Printer does not print
a = [1,1,1,1,0,0,0,0]
Check the power cable
Check the printer-computer cable
Check for paper jam
 
p = [1,2]
conditions = Printer does not print, A red light is flashing
a = [1,1,1,1,1,1,0,0]
Check the power cable
Check the printer-computer cable
Check/replace ink
Check for paper jam
 
p = [1,2,3]
conditions = Printer does not print, A red light is flashing, Printer is unrecognised
a = [1,1,1,1,1,1,1,0]
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
Check/replace ink
Check for paper jam
 
...
</pre>
 
495

edits