01: package org.drools.decisiontable.parser;
02:
03: import java.util.ArrayList;
04: import java.util.HashMap;
05: import java.util.Iterator;
06: import java.util.List;
07: import java.util.Map;
08:
09: import org.drools.decisiontable.model.SnippetBuilder;
10:
11: /**
12: * Builds up a consequence entry.
13: * @author Michael Neale
14: *
15: */
16: public class RhsBuilder implements SourceBuilder {
17:
18: private Map templates;
19: private String variable;
20: private List values;
21: private boolean hasValues;
22:
23: /**
24: * @param boundVariable Pass in a bound variable if there is one.
25: * Any cells below then will be called as methods on it.
26: * Leaving it blank will make it work in "classic" mode.
27: */
28: public RhsBuilder(String boundVariable) {
29: this .variable = boundVariable == null ? "" : boundVariable
30: .trim();
31: this .templates = new HashMap();
32: this .values = new ArrayList();
33: }
34:
35: public void addTemplate(int col, String content) {
36: Integer key = new Integer(col);
37: content = content.trim();
38: if (isBoundVar()) {
39: content = variable + "." + content + ";";
40: }
41: this .templates.put(key, content);
42: }
43:
44: private boolean isBoundVar() {
45: return !("".equals(variable));
46: }
47:
48: public void addCellValue(int col, String value) {
49: hasValues = true;
50: String template = (String) this .templates.get(new Integer(col));
51: SnippetBuilder snip = new SnippetBuilder(template);
52:
53: this .values.add(snip.build(value));
54:
55: }
56:
57: public void clearValues() {
58: this .hasValues = false;
59: this .values.clear();
60: }
61:
62: public String getResult() {
63: StringBuffer buf = new StringBuffer();
64: for (Iterator iter = this .values.iterator(); iter.hasNext();) {
65: buf.append(iter.next());
66: if (iter.hasNext()) {
67: buf.append('\n');
68: }
69: }
70: return buf.toString();
71: }
72:
73: public boolean hasValues() {
74:
75: return hasValues;
76: }
77:
78: }
|