Decision tables: Difference between revisions
Content added Content deleted
(Go solution) |
|||
Line 270: | Line 270: | ||
Ensure printer software is installed |
Ensure printer software is installed |
||
Check/replace ink</pre> |
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. |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"os" |
|||
) |
|||
type dtText struct { |
|||
rules, text string |
|||
} |
|||
var ptText = []dtText{ |
|||
{"YYYYNNNN", "Printer does not print"}, |
|||
{"YYNNYYNN", "A red light is flashing"}, |
|||
{"YNYNYNYN", "Printer is unrecognised"}, |
|||
{"--------", ""}, |
|||
{" X ", "Check the power cable"}, |
|||
{"X X ", "Check the printer-computer cable"}, |
|||
{"X X X X ", "Ensure printer software is installed"}, |
|||
{"XX XX ", "Check/replace ink"}, |
|||
{" X X ", "Check for paper jam"}, |
|||
} |
|||
type dtMap map[string][]string |
|||
func compileDT(t []dtText) (dtMap, os.Error) { |
|||
if len(t) == 0 { |
|||
return nil, os.NewError("Empty decision table") |
|||
} |
|||
var conditions, actions []dtText |
|||
ruleColumns := len(t[0].rules) |
|||
for i, row := range t { |
|||
if len(row.rules) != ruleColumns { |
|||
return nil, os.NewError("Inconsistent number of rule columns") |
|||
} |
|||
if len(row.text) == 0 { |
|||
if conditions != nil { |
|||
return nil, os.NewError("Multple separator lines") |
|||
} |
|||
if i == 0 { |
|||
return nil, os.NewError("No conditions specified") |
|||
} |
|||
if i == len(t)-1 { |
|||
return nil, os.NewError("No actions specified") |
|||
} |
|||
conditions = t[:i] |
|||
actions = t[i+1:] |
|||
} |
|||
} |
|||
if conditions == nil { |
|||
return nil, os.NewError("Missing separator line") |
|||
} |
|||
m := make(map[string][]string, ruleColumns) |
|||
kb := make([]byte, len(conditions)) |
|||
for col := 0; col < ruleColumns; col++ { |
|||
for i, c := range conditions { |
|||
kb[i] = c.rules[col] |
|||
} |
|||
key := string(kb) |
|||
for _, a := range actions { |
|||
if a.rules[col] != ' ' { |
|||
m[key] = append(m[key], a.text) |
|||
} |
|||
} |
|||
} |
|||
return m, nil |
|||
} |
|||
func init() { |
|||
var err os.Error |
|||
if ptMap, err = compileDT(ptText); err != nil { |
|||
fmt.Println(err) |
|||
os.Exit(1) |
|||
} |
|||
} |
|||
var ptMap dtMap |
|||
func main() { |
|||
for _, a := range ptMap["NYY"] { |
|||
fmt.Println(a) |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
Ensure printer software is installed |
|||
Check/replace ink |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |