Decision tables: Difference between revisions

Content added Content deleted
(Improved and updated D entry)
Line 194: Line 194:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.exception, std.string, std.array, std.conv ;
<lang d>import std.stdio, std.algorithm, std.exception, std.string,
std.array, std.conv;


struct Decision {
struct DecisionTable {
alias immutable(byte)[] IBA ;
alias immutable(bool)[] IBA;
immutable string[] conds ;
const string[] conds, actions;
immutable string[] actions ;
immutable IBA[IBA] rules;
immutable IBA[IBA] rules ;


private static immutable(bool[]) growTo(in bool[] b,
this(string[] c, string[] a, byte[][][] q) {
in size_t newLen)
conds = c.idup ;
pure nothrow {
actions = a.idup ;
IBA[IBA] r ;
bool[] result = new bool[newLen];
foreach(p ; q) {
result[0 .. b.length] = b[];
p[0].length = conds.length ;
return assumeUnique(result);
p[1].length = actions.length ;
r[p[0].idup] = p[1].idup ;
}
rules = assumeUnique(r) ;
}
}


string[] test(bool[] tested, string NoMatchMsg = "it is fine :)") {
this(in string[] c, in string[] a, in bool[][][] q)
const pure nothrow {
byte[] testedBytes = array(map!"to!byte(a?1:0)"(tested)) ;
return test(testedBytes, NoMatchMsg) ;
conds = c;
actions = a;
IBA[IBA] r;
foreach (p; q)
r[growTo(p[0], conds.length)] =
growTo(p[1], actions.length);
rules = assumeUnique(r);
}
}

string[] test(byte[] tested, string NoMatchMsg = "it is fine :)") {
string[] test(in bool[] tested,
string[] rightActions ;
in string NoMatchMsg="It is fine :)")
tested.length = conds.length ;
const pure /*nothrow*/ {
auto rule = tested.idup in rules ;
if(rule !is null)
string[] rightActions;
foreach(i, e ; *rule)
auto iTested = growTo(tested, conds.length);
if(e)
if (iTested in rules)
rightActions ~= actions[i] ;
foreach (i, immutable(byte) e; rules[iTested])
if(rightActions.length > 0)
if (e)
return rightActions ;
rightActions ~= actions[i];

return [NoMatchMsg] ;
if (!rightActions.empty)
return rightActions;
return [NoMatchMsg];
}
}


void consult() {
void consult() const {
bool[] query ;
bool[] query;

string answer ;
foreach(c;conds) {
foreach (cond; conds) {
write(c,"? [y=yes/others=no] ") ;
write(cond, "? [y=yes/others=no] ");
readf("%s\n", &answer) ;
string answer = "no";
try {
query ~= (answer.length > 0 && answer.tolower[0..1] == "y") ;
answer = stdin.readln();
} catch (StdioException) {
writeln("no");
}
query ~= !!answer.startsWith('y', 'Y');
}
}

writeln(test(query).join("\n")) ;
test(query).join("\n").writeln();
}
}
}
}


void main() {
void main() {
Decision d = Decision(
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"],

[[[1,0,0],[0,0,1]],
[[0,1,0],[0,0,0,1]],
[[[T, F, F], [F, F, T]],
[[1,1,0],[0,0,1,1]],
[[F, T, F], [F, F, F, T]],
[[0,0,1],[0,0,0,0,1]],
[[T, T, F], [F, F, T, T]],
[[1,0,1],[1,1,1]],
[[F, F, T], [F, F, F, F, T]],
[[0,1,1],[0,0,0,1,1]],
[[T, F, T], [T, T, T]],
[[1,1,1],[0,1,1,1,0]]
[[F, T, T], [F, F, F, T, T]],
[[T, T, T], [F, T, T, T, F]]
]
]
) ;
);

d.consult() ;
d.consult();
}</lang>
}</lang>
{{out}}
Sample output:
<pre>Printer is unrecognised? [y=yes/others=no] y
<pre>Printer is unrecognised? [y=yes/others=no] no
A red light is flashing? [y=yes/others=no] y
A red light is flashing? [y=yes/others=no] no
Printer does not print? [y=yes/others=no] n
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.