/* Decision tables (Rosetta Code) in Picat. http://rosettacode.org/wiki/Decision_tables """ Decision Tables [http://en.wikipedia.org/wiki/Decision_table] 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. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. % import cp. main => go. % % Interactive version. % Answer with YnNn go => conditions(Conditions), solutions(Solutions), Answers=query(Conditions), println(join(decision_table(Solutions,Answers),'\n')), nl. % % 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. % % 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(member(A, ["Y","N","y","n"]))) printf("%s? ", Text), A := read_line(), if member(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]] ].