Decision tables: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
(Improved and updated D entry) |
||
Line 194: | Line 194: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.algorithm, std.exception, std.string, |
<lang d>import std.stdio, std.algorithm, std.exception, std.string, |
||
std.array, std.conv; |
|||
struct |
struct DecisionTable { |
||
alias immutable( |
alias immutable(bool)[] IBA; |
||
const string[] conds, actions; |
|||
immutable |
immutable IBA[IBA] rules; |
||
⚫ | |||
private static immutable(bool[]) growTo(in bool[] b, |
|||
this(string[] c, string[] a, byte[][][] q) { |
|||
in size_t newLen) |
|||
⚫ | |||
pure nothrow { |
|||
⚫ | |||
bool[] result = new bool[newLen]; |
|||
result[0 .. b.length] = b[]; |
|||
return assumeUnique(result); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
string[] |
this(in string[] c, in string[] a, in bool[][][] q) |
||
const pure nothrow { |
|||
byte[] testedBytes = array(map!"to!byte(a?1:0)"(tested)) ; |
|||
conds = c; |
|||
⚫ | |||
⚫ | |||
foreach (p; q) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
string[] test(in bool[] tested, |
|||
⚫ | |||
⚫ | |||
⚫ | |||
const pure /*nothrow*/ { |
|||
auto rule = tested.idup in rules ; |
|||
string[] rightActions; |
|||
auto iTested = growTo(tested, conds.length); |
|||
if (iTested in rules) |
|||
foreach (i, immutable(byte) e; rules[iTested]) |
|||
if (e) |
|||
rightActions ~= actions[i]; |
|||
⚫ | |||
⚫ | |||
return rightActions; |
|||
⚫ | |||
} |
} |
||
void consult() { |
void consult() const { |
||
bool[] query |
bool[] query; |
||
string answer ; |
|||
foreach( |
foreach (cond; conds) { |
||
write( |
write(cond, "? [y=yes/others=no] "); |
||
string answer = "no"; |
|||
⚫ | |||
query ~= (answer.length > 0 && answer.tolower[0..1] == "y") ; |
|||
⚫ | |||
} catch (StdioException) { |
|||
⚫ | |||
⚫ | |||
query ~= !!answer.startsWith('y', 'Y'); |
|||
} |
} |
||
⚫ | |||
test(query).join("\n").writeln(); |
|||
} |
} |
||
} |
} |
||
void main() { |
void main() { |
||
enum { F = false, T = true } |
|||
const d = DecisionTable( |
|||
["Printer is unrecognised", |
["Printer is unrecognised", |
||
"A red light is flashing", |
"A red light is flashing", |
||
"Printer does not print"], |
"Printer does not print"], |
||
["Check the power cable", |
["Check the power cable", |
||
"Check the printer-computer cable", |
"Check the printer-computer cable", |
||
Line 253: | Line 266: | ||
"Check/replace ink", |
"Check/replace ink", |
||
"Check for paper jam"], |
"Check for paper jam"], |
||
⚫ | |||
[[[T, F, F], [F, F, T]], |
|||
[[ |
[[F, T, F], [F, F, F, T]], |
||
[[ |
[[T, T, F], [F, F, T, T]], |
||
[[ |
[[F, F, T], [F, F, F, F, T]], |
||
[[ |
[[T, F, T], [T, T, T]], |
||
[[ |
[[F, T, T], [F, F, F, T, T]], |
||
⚫ | |||
] |
] |
||
) |
); |
||
d.consult() |
d.consult(); |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Sample output: |
|||
<pre>Printer is unrecognised? [y=yes/others=no] |
<pre>Printer is unrecognised? [y=yes/others=no] no |
||
A red light is flashing? [y=yes/others=no] |
A red light is flashing? [y=yes/others=no] no |
||
Printer does not print? [y=yes/others=no] |
Printer does not print? [y=yes/others=no] no |
||
It is fine :)</pre> |
|||
Ensure printer software is installed |
|||
Check/replace ink</pre> |
|||
=={{header|Go}}== |
=={{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. |
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. |