14
14
import java .util .List ;
15
15
import java .util .Queue ;
16
16
import java .util .Stack ;
17
- import net .sf .jsqlparser .expression .BinaryExpression ;
18
17
import net .sf .jsqlparser .expression .Expression ;
19
18
import net .sf .jsqlparser .expression .NotExpression ;
20
- import net .sf .jsqlparser .expression .operators .relational .LikeExpression ;
21
19
22
20
public class CNFConverter {
23
21
@@ -55,9 +53,8 @@ public static Expression convertToCNF(Expression expr) {
55
53
}
56
54
57
55
/**
58
- * this method takes an expression tree and converts that into
59
- * a CNF form. Notice the 5 steps shown above will turn into
60
- * 5 different methods. For the sake of testing, I set them public.
56
+ * this method takes an expression tree and converts that into a CNF form. Notice the 5 steps
57
+ * shown above will turn into 5 different methods. For the sake of testing, I set them public.
61
58
* return the converted expression.
62
59
*
63
60
* @param express the original expression tree.
@@ -82,9 +79,8 @@ private Expression convert(Expression express)
82
79
}
83
80
84
81
/**
85
- * this is the first step that rebuild the expression tree.
86
- * Use the standard specified in the above class. Traverse the
87
- * original tree recursively and rebuild the tree from that.
82
+ * this is the first step that rebuild the expression tree. Use the standard specified in the
83
+ * above class. Traverse the original tree recursively and rebuild the tree from that.
88
84
*
89
85
* @param express the original expression tree.
90
86
*/
@@ -96,9 +92,8 @@ private void reorder(Expression express) {
96
92
}
97
93
98
94
/**
99
- * This method is used to deal with pushing not operators down.
100
- * Since it needs an extra parameter, I will create a new
101
- * method to handle this.
95
+ * This method is used to deal with pushing not operators down. Since it needs an extra
96
+ * parameter, I will create a new method to handle this.
102
97
*/
103
98
private void pushNotDown () {
104
99
/* set the two temp parameters to their staring point. */
@@ -114,15 +109,12 @@ private void pushNotDown() {
114
109
}
115
110
116
111
/**
117
- * This method is the helper function to push not operators down.
118
- * traverse the tree thoroughly, when we meet the not operator.
119
- * We only need to consider these three operators: MultiAndOperator,
120
- * MultiOrOperator, NotOperator. Handle them in a seperate way.
121
- * when we finish the traverse, the expression tree will have
122
- * all the not operators pushed as downwards as they could.
123
- * In the method, I use two global variables: temp1 and temp2
124
- * to traverse the expression tree. Notice that temp2 will always
125
- * be the parent of temp1.
112
+ * This method is the helper function to push not operators down. traverse the tree thoroughly,
113
+ * when we meet the not operator. We only need to consider these three operators:
114
+ * MultiAndOperator, MultiOrOperator, NotOperator. Handle them in a seperate way. when we finish
115
+ * the traverse, the expression tree will have all the not operators pushed as downwards as they
116
+ * could. In the method, I use two global variables: temp1 and temp2 to traverse the expression
117
+ * tree. Notice that temp2 will always be the parent of temp1.
126
118
*
127
119
* @param index the index of the children appeared in parents array.
128
120
*/
@@ -149,9 +141,8 @@ private void pushNot(int index) {
149
141
}
150
142
151
143
/**
152
- * This function mainly deals with pushing not operators down.
153
- * check the child. If it is not a logic operator(and or or).
154
- * stop at that point. Else use De Morgan law to push not downwards.
144
+ * This function mainly deals with pushing not operators down. check the child. If it is not a
145
+ * logic operator(and or or). stop at that point. Else use De Morgan law to push not downwards.
155
146
*
156
147
* @param index the index of the children appeared in parents array.
157
148
*/
@@ -176,15 +167,15 @@ private void handleNot(int index) {
176
167
* and connect that operator with the parent and return. */
177
168
if (!(child instanceof MultiAndExpression )
178
169
&& !(child instanceof MultiOrExpression )) {
179
- if (child instanceof LikeExpression ) {
180
- ((LikeExpression ) child ).setNot ();
181
- } else if (child instanceof BinaryExpression ) {
182
- ((BinaryExpression ) child ).setNot ();
183
- } else {
184
- child = new NotExpression (child );
185
- }
170
+ // if (child instanceof LikeExpression) {
171
+ // ((LikeExpression) child).setNot();
172
+ // } else if (child instanceof BinaryExpression) {
173
+ // ((BinaryExpression) child).setNot();
174
+ // } else {
175
+ child = new NotExpression (child );
176
+ // }
186
177
((MultipleExpression ) temp2 ).setChild (index , child );
187
- return ;
178
+ // return;
188
179
} else if (child instanceof MultiAndExpression ) {
189
180
MultiAndExpression and = (MultiAndExpression ) child ;
190
181
List <Expression > list = new ArrayList <Expression >();
@@ -214,11 +205,10 @@ private void handleNot(int index) {
214
205
}
215
206
216
207
/**
217
- * This method serves as dealing with the third step. It is used
218
- * to put all the adjacent same multi operators together. BFS the
219
- * tree and do it node by node. In the end we will get the tree
220
- * where all the same multi operators store in the same odd level
221
- * of the tree or in the same even level of the tree.
208
+ * This method serves as dealing with the third step. It is used to put all the adjacent same
209
+ * multi operators together. BFS the tree and do it node by node. In the end we will get the
210
+ * tree where all the same multi operators store in the same odd level of the tree or in the
211
+ * same even level of the tree.
222
212
*/
223
213
private void gather () {
224
214
Queue <Expression > queue = new LinkedList <Expression >();
@@ -296,11 +286,10 @@ private void gather() {
296
286
}
297
287
298
288
/**
299
- * First, BFS the tree and gather all the or operators and their parents
300
- * into a stack. Next, pop them out and push the and operators under the
301
- * or operators upwards(if there are). Do this level by level, which means
302
- * during each level we will call the gather() method to make the tree uniform.
303
- * When we move out of the stack. The expression tree shall be in CNF form.
289
+ * First, BFS the tree and gather all the or operators and their parents into a stack. Next, pop
290
+ * them out and push the and operators under the or operators upwards(if there are). Do this
291
+ * level by level, which means during each level we will call the gather() method to make the
292
+ * tree uniform. When we move out of the stack. The expression tree shall be in CNF form.
304
293
*/
305
294
private void pushAndUp () {
306
295
Queue <Mule > queue = new LinkedList <Mule >();
@@ -346,16 +335,13 @@ private void pushAndUp() {
346
335
}
347
336
348
337
/**
349
- * This helper function is used to deal with pushing and up:
350
- * generally, pop the top element out of the stack,
351
- * use BFS to traverse the tree and push and up.
352
- * It will case the expression tree to have the and as the new
353
- * root and multiple or as the children. Push them on the queue
354
- * and repeat the same process until the newly generated or
355
- * operator does not have any and operators in it(which means no
356
- * elements will be added into the queue). when one level is
357
- * finished, regroup the tree. Do this until the stack is empty,
358
- * the result will be the expression in CNF form.
338
+ * This helper function is used to deal with pushing and up: generally, pop the top element out
339
+ * of the stack, use BFS to traverse the tree and push and up. It will case the expression tree
340
+ * to have the and as the new root and multiple or as the children. Push them on the queue and
341
+ * repeat the same process until the newly generated or operator does not have any and operators
342
+ * in it(which means no elements will be added into the queue). when one level is finished,
343
+ * regroup the tree. Do this until the stack is empty, the result will be the expression in CNF
344
+ * form.
359
345
*
360
346
* @param stack the stack stores a list of combined data.
361
347
*/
@@ -412,13 +398,11 @@ private void pushAnd(Stack<Mule> stack) {
412
398
}
413
399
414
400
/**
415
- * This is the final step of the CNF conversion: now we have the
416
- * Expression tree that has one multiple and expression with a list
417
- * of multiple or expression as the child. So we need to convert the
418
- * multiple expression back to the binary counterparts. Note the
419
- * converted tree is left inclined. Also I attach a parenthesis node
420
- * before the or expression that is attached to the and expression
421
- * to make the generated result resembles the CNF form.
401
+ * This is the final step of the CNF conversion: now we have the Expression tree that has one
402
+ * multiple and expression with a list of multiple or expression as the child. So we need to
403
+ * convert the multiple expression back to the binary counterparts. Note the converted tree is
404
+ * left inclined. Also I attach a parenthesis node before the or expression that is attached to
405
+ * the and expression to make the generated result resembles the CNF form.
422
406
*/
423
407
private void changeBack () {
424
408
if (!(root instanceof MultiAndExpression )) {
0 commit comments