Decision tables: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Phix}}: added a gui version) |
m (→{{header|Wren}}: Minor tidy) |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 15:
{{trans|Kotlin}}
<
(‘Printer prints’ , ‘NNNNYYYY’),
(‘A red light is flashing’ , ‘YYNNYYNN’),
Line 53:
I action[1][r] == ‘Y’
print(‘ ’action[0])
L.break</
{{out}}
Line 70:
=={{header|Ada}}==
First the specification of a generic decision table package:
<
type Condition is (<>);
type Action is (<>);
Line 89:
end Generic_Decision_Table;
</syntaxhighlight>
Next, the implementation of the generic decision table package:
<
procedure React(Rules: Rule_A) is
A: Answers;
Line 111:
end Generic_Decision_Table;
</syntaxhighlight>
That was easy! Now we implement the printer troubleshooting application:
<
procedure Printer_Decision_Table is
Line 180:
begin
DT.React(R);
end Printer_Decision_Table;</
Sample output:
<pre>> ./printer_decision_table
Line 211:
=={{header|AutoHotkey}}==
<
(
Printer does not print |Y|Y|Y|Y|N|N|N|N|
Line 285:
Res .= Act%A_Index% "`n"
GuiControl,, Output, % Res
return</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f DECISION_TABLES.AWK DECISION_TABLES.TXT
BEGIN {
Line 366:
return
}
</syntaxhighlight>
<p>decision table:</p>
<pre>
Line 401:
=={{header|C}}==
With flaky keyboard input:<
#define N_COND 3
Line 441:
}
return 0;
}</
A red light is flashing? n
Printer is unrecognised? y
Line 448:
Check the power cable
Check the printer-computer cable
Ensure printer software is installed</
=={{header|C++}}==
{{trans|Java}}
<
#include <string>
#include <vector>
Line 515:
delete[] answers;
return 0;
}</
{{out}}
<pre>Please answer the following questions with a y or n:
Line 527:
=={{header|COBOL}}==
<
identification division.
program-id. 'decisiontable'.
Line 621:
end program 'decisiontable'.
</syntaxhighlight>
{{out}}
Line 634:
=={{header|D}}==
<
immutable struct DecisionTable {
Line 717:
d.consult;
}</
{{out}}
<pre>Printer is unrecognised? [y=yes/others=no] no
Line 726:
===Alternative Version===
{{trans|C}}
<
struct DataPair(size_t N) {
Line 766:
writeln(" ", sol.message);
}
}</
{{out}}
<pre>Printer does not print? [y=yes/others=no] no
Line 777:
=={{header|FreeBASIC}}==
<
{ {false, false, false, false, true, true, true, true},_
{ true, true, false, false, true, true, false, false},_
Line 816:
dontdoit:
next j
next i</
=={{header|Go}}==
Go has no specific support for decision tables, but they can be implemented easily. With a little ingenuity, literal data can be arranged in rows and columns in a way that preserves the visual associations of decision tables. Go has an init function that might be useful for compiling decision tables at program startup. And Go maps allow efficient lookup of actions given conditions.
<
import (
Line 903:
fmt.Println(a)
}
}</
Output:
<pre>
Line 911:
=={{header|Icon}} and {{header|Unicon}}==
<
record act(text,flags,aflags)
Line 961:
}
}
end</
Sample Output:<pre>Printer does not print ? Y
Line 970:
=={{header|J}}==
'''Solution''':<
'RULE_NAMES RULES'=: |:':'&cut;._2 noun define
Line 996:
options=. a #~ y {~ #. 'Y'={.@toupper@deb@(1!:1)@1:@smoutput@,&'?'@dtb"1 q
(,~ ('/'cut'Suggested resolutions:/Solution unknown.') {::~ 0=#) options
)</
'''Example''' (''solution found''):<
Having trouble? Let's track down the problem:
Printer does not print?
Line 1,009:
Check the printer-computer cable
Ensure printer software is installed
Check/replace ink </
'''Example''' (''solution not found''):<
Having trouble? Let's track down the problem:
Printer does not print?
Line 1,019:
N
Solution unknown.
</syntaxhighlight>
=== Comments ===
Line 1,028:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.io.IOException;
import java.io.InputStreamReader;
Line 1,111:
}
}
}</
{{out}}
<pre>Please answer the following questions with a y or n:
Line 1,126:
===Interactive===
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo.
<
<head></head>
<body>
Line 1,181:
});
</script>
</html></
===DecTab object===
This implements a DecisionTable object called <code>DecTab</code>. The JavaScript dialect is Microsoft's JScript.
<
var DecTab = function () {
this.conditions = [];
Line 1,233:
}
}
</syntaxhighlight>
The task is rendered as follows
<
function Ask(q) {
WScript.StdOut.Write(q);
Line 1,287:
.RulesActions(["Y", "N", "Y"], [])
.Decide();
</syntaxhighlight>
And a sample run
<
C:\>CScript DecisionTable.js
Printer prints [Y/N]?Y
Line 1,295:
Printer is recognized by computer [Y/N]?Y
Check/replace ink.
</syntaxhighlight>
=={{header|jq}}==
Line 1,308:
prompts for this task are written to stderr, which unfortunately makes them
untidy.
<
input
| if . == "Y" or . == "y" then "Y"
Line 1,360:
interact
</syntaxhighlight>
'''Example (transcript)'''
<pre>
Line 1,378:
=={{header|Julia}}==
{{trans|Perl}}
<
("A red light is flashing", 0b11001100),
("Printer is unrecognised", 0b10101010)]
Line 1,411:
decide(queries, answers)
</
<pre>
Printer does not print?: y
Line 1,421:
=={{header|Kotlin}}==
<
val conditions = listOf(
Line 1,469:
return
}
}</
Sample input/output:
Line 1,486:
=={{header|Lua}}==
{{trans|C++}}
<
local ans = '?'
repeat
Line 1,543:
end
end
end</
{{out}}
<pre>Please answer the following questions with a y or n:
Line 1,556:
=={{header|Nim}}==
We have defined a generic decision table object and used it with the Wikipedia printer example.
<
####################################################################################################
Line 1,651:
dt.addRule({PrinterPrints, RedLightFlashing, PrinterRecognized}, [CheckInk])
dt.apply()</
{{out}}
Line 1,664:
=={{header|Perl}}==
<
sub decide($q,$a) {
my @q = @{$q};
my %a = %{$a};
my($cnt,$bit) = (1,0);
for my $prompt (@q) {
print "$prompt: ";
Line 1,690 ⟶ 1,688:
'Printer does not print',
);
my %answers = (
0b00100000 => 'Check the power cable',
Line 1,698 ⟶ 1,697:
);
decide(\@queries,\%answers);</
{{out}}
<pre>Printer is unrecognised: n
Line 1,709 ⟶ 1,708:
and should cope fairly well with different lengths, missing/conflicting options, etc.
=== console version ===
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (wait_key)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 1,799 ⟶ 1,798:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 1,811 ⟶ 1,810:
=== GUI version ===
{{libheader|Phix/pGUI}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 1,903 ⟶ 1,902:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</
=={{header|Picat}}==
===Interactive version===
<
%
% Interactive version.
Line 1,968 ⟶ 1,967:
["Check/replace ink", [1,1,0,0,1,1,0,0]],
["Check for paper jam", [0,1,0,1,0,0,0,0]]
].</
{{out}}
Line 1,985 ⟶ 1,984:
For debugging purposes it it's nice to be able to print all the possible combinations of answers/solutions.
<syntaxhighlight lang="picat">%
% Go through all combinations of answers
%
Line 2,006 ⟶ 2,005:
nl
end,
nl.</
{{out}}
Line 2,040 ⟶ 2,039:
=={{header|PicoLisp}}==
We allow ourselves a luxurious user interface:
<
(out NIL (prin (car Cond) "? "))
(in NIL
Line 2,049 ⟶ 2,048:
T )
(T (member Reply '(NIL N NO No n no false 0)))
(prinl "Please answer 'Yes' or 'No'") ) ) ) )</
The decision table used in the example:
<
("Printer does not print" T T T T NIL NIL NIL NIL)
("A red light is flashing" T T NIL NIL T T NIL NIL)
Line 2,061 ⟶ 2,060:
("Ensure printer software is installed" T NIL T NIL T NIL T)
("Check/replace ink" T T NIL NIL T T)
("Check for paper jam" NIL T NIL T) )</
The decision can be made directly on the condition and action data, without the need to create intermediate tables:
<
(let Reply (mapcar yes? *Conditions)
(extract and
Line 2,070 ⟶ 2,069:
(unless (pick '((Flg) (<> Flg (next))) Reply)
(rest) ) ) )
(mapcar car *Actions) ) ) )</
Output:
<pre>: (decide)
Line 2,091 ⟶ 2,090:
=={{header|Python}}==
<
'''
Create a Decision table then use it
Line 2,130 ⟶ 2,129:
dt_user(dt)
dt_user(dt)
dt_user(dt)</
'''Sample Run'''
Line 2,181 ⟶ 2,180:
Try this:
Pray!</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> [ say "Check power cable." cr ] is cpc ( --> )
[ say "Check printer-computer cable." cr ] is cpcc ( --> )
[ say "Ensure printer software installed." cr ] is epsi ( --> )
[ say "Check/replace ink." cr ] is cri ( --> )
[ say "Check for paper jam." cr ] is cpj ( --> )
[ say "No problem detected." cr ] is npd ( --> )
[ [ dup $ " (y/n) " join input
dup $ "" = iff drop again ]
nip 0 peek upper char Y = ] is condition ( $ --> b )
[ $ "Does the printer print?"
condition
$ "Is a red light flashing?"
condition
$ "Does the computer recognise the printer?"
condition
1 << | 1 << |
[ table
( n n n ) [ cpc cpcc epsi ]
( y n n ) epsi
( n y n ) [ cpcc epsi cri ]
( y y n ) epsi
( n n y ) cpj
( y n y ) npd
( n y y ) [ cri cpj ]
( y y y ) cri ]
cr do ] is diagnose ( --> )</syntaxhighlight>
{{out}}
As a dialogue in the Quackery shell.
<pre>/O> diagnose
...
Does the printer print? (y/n) n
Is a red light flashing? (y/n) n
Does the computer recognise the printer? (y/n) y
Check for paper jam.
</pre>
=={{header|Racket}}==
Line 2,187 ⟶ 2,231:
I thought it might be fun to merge them.
<
(define (ask-y/n q)
Line 2,274 ⟶ 2,318:
╚═╩════════════╩════════════════════════════════════════════╩═╩═╩═╩═╩═══════╝)
(run-decision-table printer-troubleshooting-2dtable)</
{{out}}
Line 2,286 ⟶ 2,330:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
so prompt(.value ~ "? ") ~~ /:i ^y/;
Line 2,308 ⟶ 2,352:
);
say '';
}</
A bit of explanation: we pass in two pair lists for the questions and solutions; we ignore the keys of the questions, since they can be
generated by regarding them as a binary counter from right to left, with the least significant bit on the bottom. The <tt>@q.map</tt> runs the prompts and maps them to booleans
Line 2,347 ⟶ 2,391:
::* visual fidelity aids for postmortem analysis (as in logs)
::* a method of allowing the user to quit (opt-out of) the interrogation
<
Q.=; Q.1 = 'Does the printer not print?'
Q.2 = 'Is there a red light flashing on the printer?'
Line 2,397 ⟶ 2,441:
if (abbrev('YES', u) | abbrev("NO", u)) & words(x)==1 then return u1
say 'invalid response: ' x; oops= 1
end /*forever*/</
{{out|output|text= (a screen scraping using a DOS prompt window for some of the possible responses):}}
Line 2,493 ⟶ 2,537:
=={{header|Ring}}==
<
# Project : Decision tables
Line 2,528 ⟶ 2,572:
see "invalid input: " + upper(notprinting) + " " + upper(flashing) + " " + upper(notrecognized) + nl
ok
</syntaxhighlight>
Output:
<pre>
Line 2,540 ⟶ 2,584:
=={{header|Ruby}}==
<
def initialize(conditions, actions)
@conditions = conditions
Line 2,592 ⟶ 2,636:
]
)
loop {dt.run}</
Example
Line 2,659 ⟶ 2,703:
=={{header|Scala}}==
{{trans|Java}}
<
import scala.util.control.Breaks
Line 2,734 ⟶ 2,778:
}
}
}</
{{out}}
<pre>Please answer the following questions with a y or n:
Line 2,746 ⟶ 2,790:
=={{header|Sidef}}==
{{trans|Raku}}
<
var bits = q.map { |p|
Line 2,781 ⟶ 2,825:
)
say ''
}</
{{out}}
<pre>
Line 2,810 ⟶ 2,854:
=={{header|Tcl}}==
<
#http://rosettacode.org/wiki/Keyboard_Input/Obtain_a_Y_or_N_response#Tcl
Line 2,849 ⟶ 2,893:
}
}
}</
Demonstration:
<
"Printer does not print"
"A red light is flashing"
Line 2,862 ⟶ 2,906:
"Check for paper jam" {0 1 0 1}
}
printerDiagnosisTable consult</
Output:
<pre>
Line 2,875 ⟶ 2,919:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<
import "./str" for Str
var conditions = [
Line 2,928 ⟶ 2,972:
break
}
}</
{{out}}
Sample input/output:
<pre>
Please answer the following questions with a y or n:
Printer prints ? n
A red light is flashing ? n
Printer is recognized by computer ? n
Recommended action(s):
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
</pre>
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib; \for ToUpper, Print
int Conditions, Actions, NC, NA, NR, NP, Answers(3), Input, Outer, YN, R, C, A;
char S;
[Conditions:= [
["Printer prints" , "NNNNYYYY"],
["A red light is flashing" , "YYNNYYNN"],
["Printer is recognized by computer", "NYNYNYNY"]
];
Actions:= [
["Check the power cable" , "NNYNNNNN"],
["Check the printer-computer cable" , "YNYNNNNN"],
["Ensure printer software is installed", "YNYNYNYN"],
["Check/replace ink" , "YYNNNYNN"],
["Check for paper jam" , "NYNYNNNN"]
];
NC:= 3;
NA:= 5;
NR:= 8; \number of rules
NP:= 7; \index of 'no problem' rule
Print("Please answer the following questions with a y or n:\n");
for C:= 0 to NC-1 do
[loop [Print(" %s ? ", Conditions(C,0));
OpenI(0);
Input:= ToUpper(ChIn(0));
if Input = ^Y or Input = ^N then quit;
];
Answers(C):= Input = ^Y;
];
Print("\nRecommended action(s):\n");
for R:= 0 to NR-1 do
[Outer:= false;
for C:= 0 to NC-1 do
[YN:= if Answers(C) then ^Y else ^N;
S:= Conditions(C,1); \to access character bytes not integers
if S(R) # YN then
[Outer:= true;
C:= NC; \break
];
];
if not Outer then
[if R = NP then
Print(" None (no problem detected)\n")
else [for A:= 0 to NA-1 do
[S:= Actions(A,1);
if S(R) = ^Y then
Print(" %s\n", Actions(A,0));
];
];
R:= NR; \break
];
];
]</syntaxhighlight>
{{out}}
<pre>
Please answer the following questions with a y or n:
|