Decision tables: Difference between revisions

m
m (→‎{{header|Phix}}: added a gui version)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 4 users not shown)
Line 15:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="11l">V conditions = [
(‘Printer prints’ , ‘NNNNYYYY’),
(‘A red light is flashing’ , ‘YYNNYYNN’),
Line 53:
I action[1][r] == ‘Y’
print(‘ ’action[0])
L.break</langsyntaxhighlight>
 
{{out}}
Line 70:
=={{header|Ada}}==
First the specification of a generic decision table package:
<langsyntaxhighlight Adalang="ada">generic
type Condition is (<>);
type Action is (<>);
Line 89:
 
end Generic_Decision_Table;
</syntaxhighlight>
</lang>
Next, the implementation of the generic decision table package:
<langsyntaxhighlight Adalang="ada">package body Generic_Decision_Table is
procedure React(Rules: Rule_A) is
A: Answers;
Line 111:
 
end Generic_Decision_Table;
</syntaxhighlight>
</lang>
That was easy! Now we implement the printer troubleshooting application:
<langsyntaxhighlight Adalang="ada">with Generic_Decision_Table, Ada.Text_IO;
 
procedure Printer_Decision_Table is
Line 180:
begin
DT.React(R);
end Printer_Decision_Table;</langsyntaxhighlight>
Sample output:
<pre>> ./printer_decision_table
Line 211:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Conditions =
(
Printer does not print |Y|Y|Y|Y|N|N|N|N|
Line 285:
Res .= Act%A_Index% "`n"
GuiControl,, Output, % Res
return</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DECISION_TABLES.AWK DECISION_TABLES.TXT
BEGIN {
Line 366:
return
}
</syntaxhighlight>
</lang>
<p>decision table:</p>
<pre>
Line 401:
 
=={{header|C}}==
With flaky keyboard input:<langsyntaxhighlight Clang="c">#include <stdio.h>
#define N_COND 3
Line 441:
}
return 0;
}</langsyntaxhighlight>output<syntaxhighlight lang="text">Printer does not print? y
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</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <vector>
Line 515:
delete[] answers;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Please answer the following questions with a y or n:
Line 527:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> >> SOURCE FORMAT FREE
identification division.
program-id. 'decisiontable'.
Line 621:
 
end program 'decisiontable'.
</syntaxhighlight>
</lang>
 
{{out}}
Line 634:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.exception, std.array;
 
immutable struct DecisionTable {
Line 717:
 
d.consult;
}</langsyntaxhighlight>
{{out}}
<pre>Printer is unrecognised? [y=yes/others=no] no
Line 726:
===Alternative Version===
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.string;
 
struct DataPair(size_t N) {
Line 766:
writeln(" ", sol.message);
}
}</langsyntaxhighlight>
{{out}}
<pre>Printer does not print? [y=yes/others=no] no
Line 777:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as boolean rules(0 to 2, 0 to 7) =_
{ {false, false, false, false, true, true, true, true},_
{ true, true, false, false, true, true, false, false},_
Line 816:
dontdoit:
next j
next i</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 903:
fmt.Println(a)
}
}</langsyntaxhighlight>
Output:
<pre>
Line 911:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">record cond(text,flags)
record act(text,flags,aflags)
 
Line 961:
}
}
end</langsyntaxhighlight>
 
Sample Output:<pre>Printer does not print ? Y
Line 970:
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j">require'strings'
 
'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
)</langsyntaxhighlight>
 
'''Example''' (''solution found''):<langsyntaxhighlight lang="j"> troubleshoot ''
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 </langsyntaxhighlight>
'''Example''' (''solution not found''):<langsyntaxhighlight lang="j"> troubleshoot ''
Having trouble? Let's track down the problem:
Printer does not print?
Line 1,019:
N
Solution unknown.
</syntaxhighlight>
</lang>
 
=== Comments ===
Line 1,028:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Line 1,111:
}
}
}</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="javascript"><html>
<head></head>
<body>
Line 1,181:
});
</script>
</html></langsyntaxhighlight>
===DecTab object===
This implements a DecisionTable object called <code>DecTab</code>. The JavaScript dialect is Microsoft's JScript.
<langsyntaxhighlight lang="javascript">
var DecTab = function () {
this.conditions = [];
Line 1,233:
}
}
</syntaxhighlight>
</lang>
The task is rendered as follows
<langsyntaxhighlight lang="javascript">
function Ask(q) {
WScript.StdOut.Write(q);
Line 1,287:
.RulesActions(["Y", "N", "Y"], [])
.Decide();
</syntaxhighlight>
</lang>
And a sample run
<langsyntaxhighlight lang="text">
C:\>CScript DecisionTable.js
Printer prints [Y/N]?Y
Line 1,295:
Printer is recognized by computer [Y/N]?Y
Check/replace ink.
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
Line 1,308:
prompts for this task are written to stderr, which unfortunately makes them
untidy.
<langsyntaxhighlight lang="jq">def get_response:
input
| if . == "Y" or . == "y" then "Y"
Line 1,360:
interact
 
</syntaxhighlight>
</lang>
'''Example (transcript)'''
<pre>
Line 1,378:
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang="julia">const queries = [("Printer does not print", 0b11110000),
("A red light is flashing", 0b11001100),
("Printer is unrecognised", 0b10101010)]
Line 1,411:
 
decide(queries, answers)
</langsyntaxhighlight>{{out}}
<pre>
Printer does not print?: y
Line 1,421:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
val conditions = listOf(
Line 1,469:
return
}
}</langsyntaxhighlight>
 
Sample input/output:
Line 1,486:
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">function promptYN(q)
local ans = '?'
repeat
Line 1,543:
end
end
end</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Nimlang="nim">import strutils, tables
 
####################################################################################################
Line 1,651:
dt.addRule({PrinterPrints, RedLightFlashing, PrinterRecognized}, [CheckInk])
 
dt.apply()</langsyntaxhighlight>
 
{{out}}
Line 1,664:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl" line>use strictv5.36;
 
use warnings;
sub decide($q,$a) {
my @q = @{$q};
my %a = %{$a};
my($cnt,$bit) = (1,0);
 
sub decide {
our(@q,%a);
local *q = shift;
local *a = shift;
my $bit;
my $cnt = 1;
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);</langsyntaxhighlight>
{{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 ===
<!--<langsyntaxhighlight Phixlang="phix">-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,811 ⟶ 1,810:
=== GUI version ===
{{libheader|Phix/pGUI}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Picat}}==
===Interactive version===
<langsyntaxhighlight Picatlang="picat">import util.
%
% 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]]
].</langsyntaxhighlight>
 
{{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">%
<lang Picat>%
% Go through all combinations of answers
%
Line 2,006 ⟶ 2,005:
nl
end,
nl.</langsyntaxhighlight>
 
{{out}}
Line 2,040 ⟶ 2,039:
=={{header|PicoLisp}}==
We allow ourselves a luxurious user interface:
<langsyntaxhighlight PicoLisplang="picolisp">(de yes? (Cond)
(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'") ) ) ) )</langsyntaxhighlight>
The decision table used in the example:
<langsyntaxhighlight PicoLisplang="picolisp">(de *Conditions
("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) )</langsyntaxhighlight>
The decision can be made directly on the condition and action data, without the need to create intermediate tables:
<langsyntaxhighlight PicoLisplang="picolisp">(de decide ()
(let Reply (mapcar yes? *Conditions)
(extract and
Line 2,070 ⟶ 2,069:
(unless (pick '((Flg) (<> Flg (next))) Reply)
(rest) ) ) )
(mapcar car *Actions) ) ) )</langsyntaxhighlight>
Output:
<pre>: (decide)
Line 2,091 ⟶ 2,090:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
'''
Create a Decision table then use it
Line 2,130 ⟶ 2,129:
dt_user(dt)
dt_user(dt)
dt_user(dt)</langsyntaxhighlight>
 
'''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.
 
<langsyntaxhighlight lang="racket">#lang unstable/2d racket
 
(define (ask-y/n q)
Line 2,274 ⟶ 2,318:
╚═╩════════════╩════════════════════════════════════════════╩═╩═╩═╩═╩═══════╝)
 
(run-decision-table printer-troubleshooting-2dtable)</langsyntaxhighlight>
 
{{out}}
Line 2,286 ⟶ 2,330:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub decide (@q, @s) {
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
so prompt(.value ~ "? ") ~~ /:i ^y/;
Line 2,308 ⟶ 2,352:
);
say '';
}</langsyntaxhighlight>
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:
::* &nbsp; visual fidelity aids for postmortem analysis (as in logs)
::* &nbsp; a method of allowing the user to quit (opt-out of) the interrogation
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates a (query) decision table and possible corrective actions.*/
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*/</langsyntaxhighlight>
{{out|output|text=&nbsp; (a screen scraping using a DOS prompt window for some of the possible responses):}}
 
Line 2,493 ⟶ 2,537:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Decision tables
 
Line 2,528 ⟶ 2,572:
see "invalid input: " + upper(notprinting) + " " + upper(flashing) + " " + upper(notrecognized) + nl
ok
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,540 ⟶ 2,584:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class DecisionTable
def initialize(conditions, actions)
@conditions = conditions
Line 2,592 ⟶ 2,636:
]
)
loop {dt.run}</langsyntaxhighlight>
 
Example
Line 2,659 ⟶ 2,703:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.io.{BufferedReader, InputStreamReader}
 
import scala.util.control.Breaks
Line 2,734 ⟶ 2,778:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Please answer the following questions with a y or n:
Line 2,746 ⟶ 2,790:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func decide (q, s) {
 
var bits = q.map { |p|
Line 2,781 ⟶ 2,825:
)
say ''
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,810 ⟶ 2,854:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require TclOO
 
#http://rosettacode.org/wiki/Keyboard_Input/Obtain_a_Y_or_N_response#Tcl
Line 2,849 ⟶ 2,893:
}
}
}</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="tcl">DecisionTable create printerDiagnosisTable {
"Printer does not print"
"A red light is flashing"
Line 2,862 ⟶ 2,906:
"Check for paper jam" {0 1 0 1}
}
printerDiagnosisTable consult</langsyntaxhighlight>
Output:
<pre>
Line 2,875 ⟶ 2,919:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin, Stdout
import "./str" for Str
 
var conditions = [
Line 2,928 ⟶ 2,972:
break
}
}</langsyntaxhighlight>
 
{{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:
9,476

edits