Decision tables: Difference between revisions

Content added Content deleted
(Improved and updated D entry)
Line 194:
 
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.exception, std.string, std.array, std.conv ;
std.array, std.conv;
 
struct DecisionDecisionTable {
alias immutable(bytebool)[] IBA ;
immutableconst string[] conds, actions;
immutable stringIBA[IBA] actions 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 ;
IBAbool[IBA] rresult = new bool[newLen];
foreach(presult[0 ;.. q)b.length] {= b[];
return p[0].length = conds.length assumeUnique(result);
p[1].length = actions.length ;
r[p[0].idup] = p[1].idup ;
}
rules = assumeUnique(r) ;
}
 
this(in string[] test(boolc, in string[] testeda, stringin NoMatchMsgbool[][][] = "it is fine :q)") {
const pure nothrow {
byte[] testedBytes = array(map!"to!byte(a?1:0)"(tested)) ;
returnconds test(testedBytes, NoMatchMsg)= c;
actions = a.idup ;
immutable IBA[IBA] rules r;
foreach (p; q)
tested.length = r[growTo(p[0], conds.length)] ;=
growTo(p[1].length =, actions.length );
rules = assumeUnique(r) ;
}
 
string[] test(byte[] tested, string NoMatchMsg = "it is fine :)") {
string[] test(in bool[] tested,
string[] rightActions ;
string[] test(byte[] tested, in string NoMatchMsg = "itIt is fine :)") {
tested.length = conds.length ;
const pure /*nothrow*/ {
auto rule = tested.idup in rules ;
if(rulestring[] !is null)rightActions;
auto iTested = foreachgrowTo(itested, e conds.length); *rule)
if (iTested in if(erules)
foreach (i, immutable(byte) e; rightActions ~= actionsrules[iiTested] ;)
if(rightActions.length > 0 if (e)
return rightActions ~= actions[i];
 
return [NoMatchMsg] ;
string[]if (!rightActions ;.empty)
return rightActions;
return [NoMatchMsg] ;
}
 
void consult() const {
bool[] query ;
 
string answer ;
foreach (ccond; conds) {
write(ccond, "? [y=yes/others=no] ") ;
readf("%s\n",string &answer) = "no";
conds = c.iduptry ;{
query ~= (answer.length > 0 && answer.tolower[0..1] == "y") ;
r[p[0].idup] answer = p[1]stdin.idup readln();
} catch (StdioException) {
writeln(test(query).join("\nno")) ;
}
query ~= !!answer.startsWith('y', 'Y');
}
 
writeln(test(query).join("\n")) ;
test(query).join("\n").writeln();
}
}
 
void main() {
Decisionenum d{ F = Decision(false, T = true }
const d = DecisionTable(
["Printer is unrecognised",
"A red light is flashing",
"Printer does not print"],
 
["Check the power cable",
"Check the printer-computer cable",
Line 253 ⟶ 266:
"Check/replace ink",
"Check for paper jam"],
 
[[[1,0,0],[0,0,1]],
[[0[T,1 F,0 F], [0F,0 F,0,1 T]],
[[1F,1 T,0 F], [0F,0 F,1 F,1 T]],
[[0T,0 T,1 F], [0F,0 F,0 T,0,1 T]],
[[1F,0 F,1 T], [1F,1 F,1 F, F, T]],
[[0T,1 F,1 T], [0T,0 T,0,1,1 T]],
[[1F,1 T,1 T], [0F,1 F,1 F,1 T,0 T]],
[[[1T,0 T,0 T], [0F,0 T,1 T, T, F]],
]
) ;
 
d.consult() ;
}</lang>
{{out}}
Sample output:
<pre>Printer is unrecognised? [y=yes/others=no] yno
A red light is flashing? [y=yes/others=no] yno
Printer does not print? [y=yes/others=no] nno
It is fine :)</pre>
Ensure printer software is installed
 
Check/replace ink</pre>
=={{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.