1414import java .util .List ;
1515import java .util .Queue ;
1616import java .util .Stack ;
17- import net .sf .jsqlparser .expression .BinaryExpression ;
1817import net .sf .jsqlparser .expression .Expression ;
1918import net .sf .jsqlparser .expression .NotExpression ;
20- import net .sf .jsqlparser .expression .operators .relational .LikeExpression ;
2119
2220public class CNFConverter {
2321
@@ -55,9 +53,8 @@ public static Expression convertToCNF(Expression expr) {
5553 }
5654
5755 /**
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.
6158 * return the converted expression.
6259 *
6360 * @param express the original expression tree.
@@ -82,9 +79,8 @@ private Expression convert(Expression express)
8279 }
8380
8481 /**
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.
8884 *
8985 * @param express the original expression tree.
9086 */
@@ -96,9 +92,8 @@ private void reorder(Expression express) {
9692 }
9793
9894 /**
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.
10297 */
10398 private void pushNotDown () {
10499 /* set the two temp parameters to their staring point. */
@@ -114,15 +109,12 @@ private void pushNotDown() {
114109 }
115110
116111 /**
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.
126118 *
127119 * @param index the index of the children appeared in parents array.
128120 */
@@ -149,9 +141,8 @@ private void pushNot(int index) {
149141 }
150142
151143 /**
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.
155146 *
156147 * @param index the index of the children appeared in parents array.
157148 */
@@ -176,15 +167,15 @@ private void handleNot(int index) {
176167 * and connect that operator with the parent and return. */
177168 if (!(child instanceof MultiAndExpression )
178169 && !(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+ // }
186177 ((MultipleExpression ) temp2 ).setChild (index , child );
187- return ;
178+ // return;
188179 } else if (child instanceof MultiAndExpression ) {
189180 MultiAndExpression and = (MultiAndExpression ) child ;
190181 List <Expression > list = new ArrayList <Expression >();
@@ -214,11 +205,10 @@ private void handleNot(int index) {
214205 }
215206
216207 /**
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.
222212 */
223213 private void gather () {
224214 Queue <Expression > queue = new LinkedList <Expression >();
@@ -296,11 +286,10 @@ private void gather() {
296286 }
297287
298288 /**
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.
304293 */
305294 private void pushAndUp () {
306295 Queue <Mule > queue = new LinkedList <Mule >();
@@ -346,16 +335,13 @@ private void pushAndUp() {
346335 }
347336
348337 /**
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.
359345 *
360346 * @param stack the stack stores a list of combined data.
361347 */
@@ -412,13 +398,11 @@ private void pushAnd(Stack<Mule> stack) {
412398 }
413399
414400 /**
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.
422406 */
423407 private void changeBack () {
424408 if (!(root instanceof MultiAndExpression )) {
0 commit comments