1 | |
package org.kuali.student.common.messagebuilder.impl; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanNode; |
7 | |
import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException; |
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
public class SuccessMessageBuilder { |
13 | |
|
14 | |
private BooleanOperators booleanOperators; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | 28 | public SuccessMessageBuilder(BooleanOperators bo) { |
23 | 28 | this.booleanOperators = bo; |
24 | 28 | } |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public String buildSuccessMessage(List<BooleanNode> nodeList) throws MessageBuilderException { |
34 | |
|
35 | 3 | List<BooleanNode> rootNodeList = new ArrayList<BooleanNode>(); |
36 | 3 | for(BooleanNode node : nodeList) { |
37 | 14 | if(node.getParent() == null) { |
38 | 4 | rootNodeList.add(node); |
39 | |
} |
40 | 14 | buildSuccessMessage(node); |
41 | |
} |
42 | 3 | if(rootNodeList.size() > 1) { |
43 | 1 | throw new MessageBuilderException("Node list contains more than one root node: " + rootNodeList); |
44 | |
} |
45 | 2 | return rootNodeList.get(0).getNodeMessage(); |
46 | |
} |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public String buildSuccessMessage(final BooleanNode node) { |
55 | |
|
56 | 146 | if(node.getLabel().equals("*")) { |
57 | 34 | buildAndNodeSuccessMessage(node); |
58 | |
} |
59 | |
|
60 | 112 | else if(node.getLabel().equals("+")) { |
61 | 31 | buildOr1NodeSuccessMessage(node); |
62 | 31 | buildOr2NodeSuccessMessage(node); |
63 | |
} |
64 | 146 | return node.getNodeMessage(); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private void buildAndNodeSuccessMessage(BooleanNode node) { |
74 | 34 | if(node.getLabel().equals("*") && |
75 | |
node.getLeftNode() != null && |
76 | |
node.getRightNode() != null && |
77 | |
node.getLeftNode().getValue() == true && |
78 | |
node.getRightNode().getValue() == true && |
79 | |
node.getLeftNode().getNodeMessage() != null && |
80 | |
node.getRightNode().getNodeMessage() != null) { |
81 | 22 | String logMessage = node.getLeftNode().getNodeMessage() + " " + |
82 | |
this.booleanOperators.getAndOperator() + " " + |
83 | |
node.getRightNode().getNodeMessage(); |
84 | |
|
85 | 22 | if(node.getParent() != null && |
86 | |
( (node.getLabel().equals("+") && |
87 | |
node.getParent().getLabel().equals("*")) || |
88 | |
(node.getLabel().equals("*") && |
89 | |
node.getParent().getLabel().equals("+")))) { |
90 | 10 | logMessage = "(" + logMessage + ")"; |
91 | |
} |
92 | 22 | node.setNodeMessage(logMessage); |
93 | |
} |
94 | 34 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private void buildOr1NodeSuccessMessage(BooleanNode node) { |
104 | |
|
105 | 31 | if(node.getLabel().equals("+") && |
106 | |
node.getLeftNode() != null && |
107 | |
node.getRightNode() != null && |
108 | |
((node.getLeftNode().getValue() == false && |
109 | |
node.getRightNode().getValue() == true && |
110 | |
node.getRightNode().getNodeMessage() != null ) || |
111 | |
(node.getLeftNode().getValue() == true && |
112 | |
node.getRightNode().getValue() == false && |
113 | |
node.getLeftNode().getNodeMessage() != null))) { |
114 | 14 | String logMessage = "test"; |
115 | |
|
116 | 14 | if (node.getLeftNode().getValue() == true) |
117 | 10 | logMessage = node.getLeftNode().getNodeMessage(); |
118 | 4 | else if (node.getRightNode().getValue() == true) |
119 | 4 | logMessage = node.getRightNode().getNodeMessage(); |
120 | |
|
121 | 14 | node.setNodeMessage(logMessage); |
122 | |
} |
123 | 31 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
private void buildOr2NodeSuccessMessage(BooleanNode node) { |
132 | |
|
133 | 31 | if(node.getLabel().equals("+") && |
134 | |
node.getLeftNode() != null && |
135 | |
node.getRightNode() != null && |
136 | |
node.getLeftNode().getValue() == true && |
137 | |
node.getRightNode().getValue() == true && |
138 | |
node.getLeftNode().getNodeMessage() != null && |
139 | |
node.getRightNode().getNodeMessage() != null) { |
140 | 12 | String logMessage = node.getLeftNode().getNodeMessage() + " " + |
141 | |
this.booleanOperators.getOrOperator() + " " + |
142 | |
node.getRightNode().getNodeMessage(); |
143 | |
|
144 | 12 | if(node.getParent() != null && |
145 | |
( (node.getLabel().equals("+") && |
146 | |
node.getParent().getLabel().equals("*")) || |
147 | |
(node.getLabel().equals("*") && |
148 | |
node.getParent().getLabel().equals("+")))) { |
149 | 4 | logMessage = "(" + logMessage + ")"; |
150 | |
} |
151 | 12 | node.setNodeMessage(logMessage); |
152 | |
} |
153 | 31 | } |
154 | |
} |