1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.kuali.student.common.messagebuilder.booleanmessage.ast; |
17 |
|
|
18 |
|
import java.util.ArrayList; |
19 |
|
import java.util.List; |
20 |
|
import java.util.Locale; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import org.antlr.runtime.ANTLRStringStream; |
24 |
|
import org.antlr.runtime.CommonTokenStream; |
25 |
|
import org.antlr.runtime.RecognitionException; |
26 |
|
import org.antlr.runtime.Token; |
27 |
|
import org.antlr.runtime.tree.CommonTreeAdaptor; |
28 |
|
import org.antlr.runtime.tree.TreeAdaptor; |
29 |
|
|
30 |
|
import org.kuali.student.common.messagebuilder.booleanmessage.BooleanMessage; |
31 |
|
import org.kuali.student.common.messagebuilder.booleanmessage.ast.exceptions.BooleanFunctionException; |
32 |
|
import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionLexer; |
33 |
|
import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionParser; |
34 |
|
|
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.slf4j.LoggerFactory; |
37 |
|
|
|
|
| 0% |
Uncovered Elements: 83 (83) |
Complexity: 22 |
Complexity Density: 0.42 |
|
38 |
|
public class BinaryMessageTree { |
39 |
|
|
40 |
|
final static Logger logger = LoggerFactory.getLogger(BinaryMessageTree.class); |
41 |
|
|
42 |
|
private BooleanNode root; |
43 |
|
private ArrayList<BooleanNode> nodes; |
44 |
|
private String language; |
45 |
|
|
46 |
|
private Map<String, ? extends BooleanMessage> nodeMessageMap; |
47 |
|
|
48 |
|
private static final TreeAdaptor adaptor = new CommonTreeAdaptor() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
49 |
0
|
public Object create(Token payload) {... |
50 |
0
|
return new BooleanNode(payload); |
51 |
|
} |
52 |
|
}; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
57 |
0
|
public BinaryMessageTree() {... |
58 |
0
|
language = Locale.getDefault().getLanguage(); |
59 |
0
|
nodes = new ArrayList<BooleanNode>(); |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@param |
68 |
|
@param |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
70 |
0
|
public BinaryMessageTree(String language, Map<String, ? extends BooleanMessage> messageMap) {... |
71 |
0
|
this.language = language; |
72 |
0
|
nodeMessageMap = messageMap; |
73 |
0
|
nodes = new ArrayList<BooleanNode>(); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@return |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
85 |
0
|
public BooleanNode buildTree(String booleanFunction){... |
86 |
0
|
if (booleanFunction == null || booleanFunction.trim().isEmpty()) { |
87 |
0
|
return null; |
88 |
|
} |
89 |
|
|
90 |
0
|
BooleanFunctionLexer lex = new BooleanFunctionLexer(new ANTLRStringStream(booleanFunction) ); |
91 |
0
|
CommonTokenStream tokens = new CommonTokenStream(lex); |
92 |
|
|
93 |
0
|
BooleanFunctionParser parser = new BooleanFunctionParser(tokens); |
94 |
0
|
parser.setTreeAdaptor(adaptor); |
95 |
|
|
96 |
|
|
97 |
0
|
BooleanFunctionParser.booleanExpression_return booleanExpression = null; |
98 |
0
|
try { |
99 |
0
|
booleanExpression = parser.booleanExpression(); |
100 |
|
} catch (RecognitionException e) { |
101 |
0
|
String parserErrorMsg = parser.getErrorMessage(e, parser.getTokenNames()); |
102 |
0
|
String errorMsg = "Boolean Function Parser Error. " + |
103 |
|
"Invalid Boolean Expression: '" + booleanFunction + "'; " + parserErrorMsg; |
104 |
0
|
throw new BooleanFunctionException(errorMsg, e); |
105 |
|
} |
106 |
|
|
107 |
0
|
return root = (BooleanNode) booleanExpression.getTree(); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
@param |
116 |
|
@param |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
118 |
0
|
public void traverseTreePostOrder(BooleanNode bnode, BooleanNode parent) {... |
119 |
0
|
for (int i = 0; i < bnode.getChildCount(); i++) { |
120 |
0
|
traverseTreePostOrder((BooleanNode) bnode.getChild(i), bnode); |
121 |
|
} |
122 |
0
|
setNode(bnode); |
123 |
0
|
if (parent != null) { |
124 |
0
|
bnode.setParent(parent); |
125 |
|
} |
126 |
|
|
127 |
0
|
if(logger.isDebugEnabled()) { |
128 |
0
|
logger.debug(bnode.getText()); |
129 |
|
} |
130 |
0
|
nodes.add(bnode); |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
@param |
140 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.62 |
|
141 |
0
|
public void traverseTreePostOrderDontSetNode(BooleanNode bnode, BooleanNode parent) {... |
142 |
0
|
if (bnode != null) { |
143 |
0
|
for (int i = 0; i < bnode.getChildCount(); i++ ) { |
144 |
0
|
traverseTreePostOrderDontSetNode((BooleanNode)bnode.getChild(i), bnode); |
145 |
|
} |
146 |
|
|
147 |
0
|
if (parent != null) { |
148 |
0
|
bnode.setParent(parent); |
149 |
|
} |
150 |
|
|
151 |
0
|
if(logger.isDebugEnabled()) { |
152 |
0
|
logger.debug(bnode.getText()); |
153 |
|
} |
154 |
0
|
nodes.add(bnode); |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 4 |
Complexity Density: 0.27 |
|
165 |
0
|
private void setNode(BooleanNode bnode) {... |
166 |
0
|
bnode.setLanguage(language); |
167 |
|
|
168 |
0
|
if (bnode.getChildCount() == 0) { |
169 |
|
|
170 |
0
|
BooleanMessage message = nodeMessageMap.get(bnode.getLabel()); |
171 |
0
|
bnode.setValue(message.isSuccesful()); |
172 |
0
|
bnode.setNodeMessage(message.getMessage()); |
173 |
|
} |
174 |
|
else { |
175 |
|
|
176 |
0
|
BooleanNode child0 = (BooleanNode)bnode.getChild(0); |
177 |
0
|
BooleanNode child1 = (BooleanNode)bnode.getChild(1); |
178 |
|
|
179 |
0
|
if ( bnode.getLabel().equalsIgnoreCase("+") ) { |
180 |
|
|
181 |
0
|
Boolean newValue = child0.getValue() || child1.getValue(); |
182 |
0
|
bnode.setValue(newValue); |
183 |
0
|
bnode.setNodeMessage("null"); |
184 |
|
} |
185 |
0
|
else if ( bnode.getLabel().equalsIgnoreCase("*") ) { |
186 |
|
|
187 |
0
|
Boolean newValue = child0.getValue() && child1.getValue(); |
188 |
0
|
bnode.setValue(newValue); |
189 |
0
|
bnode.setNodeMessage("null"); |
190 |
|
} |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@return |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
0
|
public List<BooleanNode> getAllNodes() {... |
200 |
0
|
return nodes; |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
@return |
207 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
208 |
0
|
public BooleanNode getRoot() {... |
209 |
0
|
return root; |
210 |
|
} |
211 |
|
} |