@@ -213,7 +213,6 @@ public class CNFConverter {
213213 // notice temp1 will be settled as the root and temp2 will be
214214 // settled as the dummy root.
215215 private boolean isUsed = false ;
216- private CloneHelper clone = new CloneHelper ();
217216
218217 private class Mule {
219218
@@ -234,9 +233,8 @@ public static Expression convertToCNF(Expression expr) {
234233 }
235234
236235 /**
237- * this method takes an expression tree and converts that into a CNF form. Notice the 5 steps
238- * shown above will turn into 5 different methods. For the sake of testing, I set them public.
239- * return the converted expression.
236+ * this method takes an expression tree and converts that into a CNF form. Notice the 5 steps shown above will turn
237+ * into 5 different methods. For the sake of testing, I set them public. return the converted expression.
240238 *
241239 * @param express the original expression tree.
242240 */
@@ -260,21 +258,21 @@ private Expression convert(Expression express)
260258 }
261259
262260 /**
263- * this is the first step that rebuild the expression tree. Use the standard specified in the
264- * above class. Traverse the original tree recursively and rebuild the tree from that.
261+ * this is the first step that rebuild the expression tree. Use the standard specified in the above class. Traverse
262+ * the original tree recursively and rebuild the tree from that.
265263 *
266264 * @param express the original expression tree.
267265 */
268266 private void reorder (Expression express ) {
269- root = clone .modify (express );
267+ root = CloneHelper .modify (express );
270268 List <Expression > list = new ArrayList <Expression >();
271269 list .add (root );
272270 dummy = new MultiAndExpression (list );
273271 }
274272
275273 /**
276- * This method is used to deal with pushing not operators down. Since it needs an extra
277- * parameter, I will create a new method to handle this.
274+ * This method is used to deal with pushing not operators down. Since it needs an extra parameter, I will create a
275+ * new method to handle this.
278276 */
279277 private void pushNotDown () {
280278 /* set the two temp parameters to their staring point. */
@@ -290,11 +288,10 @@ private void pushNotDown() {
290288 }
291289
292290 /**
293- * This method is the helper function to push not operators down. traverse the tree thoroughly,
294- * when we meet the not operator. We only need to consider these three operators:
295- * MultiAndOperator, MultiOrOperator, NotOperator. Handle them in a seperate way. when we finish
296- * the traverse, the expression tree will have all the not operators pushed as downwards as they
297- * could. In the method, I use two global variables: temp1 and temp2 to traverse the expression
291+ * This method is the helper function to push not operators down. traverse the tree thoroughly, when we meet the not
292+ * operator. We only need to consider these three operators: MultiAndOperator, MultiOrOperator, NotOperator. Handle
293+ * them in a seperate way. when we finish the traverse, the expression tree will have all the not operators pushed
294+ * as downwards as they could. In the method, I use two global variables: temp1 and temp2 to traverse the expression
298295 * tree. Notice that temp2 will always be the parent of temp1.
299296 *
300297 * @param index the index of the children appeared in parents array.
@@ -322,8 +319,8 @@ private void pushNot(int index) {
322319 }
323320
324321 /**
325- * This function mainly deals with pushing not operators down. check the child. If it is not a
326- * logic operator(and or or). stop at that point. Else use De Morgan law to push not downwards.
322+ * This function mainly deals with pushing not operators down. check the child. If it is not a logic operator(and or
323+ * or). stop at that point. Else use De Morgan law to push not downwards.
327324 *
328325 * @param index the index of the children appeared in parents array.
329326 */
@@ -386,10 +383,9 @@ private void handleNot(int index) {
386383 }
387384
388385 /**
389- * This method serves as dealing with the third step. It is used to put all the adjacent same
390- * multi operators together. BFS the tree and do it node by node. In the end we will get the
391- * tree where all the same multi operators store in the same odd level of the tree or in the
392- * same even level of the tree.
386+ * This method serves as dealing with the third step. It is used to put all the adjacent same multi operators
387+ * together. BFS the tree and do it node by node. In the end we will get the tree where all the same multi operators
388+ * store in the same odd level of the tree or in the same even level of the tree.
393389 */
394390 @ SuppressWarnings ({"PMD.CyclomaticComplexity" })
395391 private void gather () {
@@ -468,10 +464,10 @@ private void gather() {
468464 }
469465
470466 /**
471- * First, BFS the tree and gather all the or operators and their parents into a stack. Next, pop
472- * them out and push the and operators under the or operators upwards(if there are). Do this
473- * level by level, which means during each level we will call the gather() method to make the
474- * tree uniform. When we move out of the stack. The expression tree shall be in CNF form.
467+ * First, BFS the tree and gather all the or operators and their parents into a stack. Next, pop them out and push
468+ * the and operators under the or operators upwards(if there are). Do this level by level, which means during each
469+ * level we will call the gather() method to make the tree uniform. When we move out of the stack. The expression
470+ * tree shall be in CNF form.
475471 */
476472 private void pushAndUp () {
477473 Queue <Mule > queue = new LinkedList <Mule >();
@@ -517,12 +513,11 @@ private void pushAndUp() {
517513 }
518514
519515 /**
520- * This helper function is used to deal with pushing and up: generally, pop the top element out
521- * of the stack, use BFS to traverse the tree and push and up. It will case the expression tree
522- * to have the and as the new root and multiple or as the children. Push them on the queue and
523- * repeat the same process until the newly generated or operator does not have any and operators
524- * in it(which means no elements will be added into the queue). when one level is finished,
525- * regroup the tree. Do this until the stack is empty, the result will be the expression in CNF
516+ * This helper function is used to deal with pushing and up: generally, pop the top element out of the stack, use
517+ * BFS to traverse the tree and push and up. It will case the expression tree to have the and as the new root and
518+ * multiple or as the children. Push them on the queue and repeat the same process until the newly generated or
519+ * operator does not have any and operators in it(which means no elements will be added into the queue). when one
520+ * level is finished, regroup the tree. Do this until the stack is empty, the result will be the expression in CNF
526521 * form.
527522 *
528523 * @param stack the stack stores a list of combined data.
@@ -570,7 +565,7 @@ private void pushAnd(Stack<Mule> stack) {
570565 MultiAndExpression newand = new MultiAndExpression (list );
571566 parents .setChild (parents .getIndex (children ), newand );
572567 for (int i = 0 ; i < and .size (); i ++) {
573- Expression temp = clone .shallowCopy (children );
568+ Expression temp = CloneHelper .shallowCopy (children );
574569 MultipleExpression mtemp = (MultipleExpression ) temp ;
575570 mtemp .addChild (mtemp .size (), and .getChild (i ));
576571 newand .addChild (i , mtemp );
@@ -581,21 +576,20 @@ private void pushAnd(Stack<Mule> stack) {
581576 }
582577
583578 /**
584- * This is the final step of the CNF conversion: now we have the Expression tree that has one
585- * multiple and expression with a list of multiple or expression as the child. So we need to
586- * convert the multiple expression back to the binary counterparts. Note the converted tree is
587- * left inclined. Also I attach a parenthesis node before the or expression that is attached to
588- * the and expression to make the generated result resembles the CNF form.
579+ * This is the final step of the CNF conversion: now we have the Expression tree that has one multiple and
580+ * expression with a list of multiple or expression as the child. So we need to convert the multiple expression back
581+ * to the binary counterparts. Note the converted tree is left inclined. Also I attach a parenthesis node before the
582+ * or expression that is attached to the and expression to make the generated result resembles the CNF form.
589583 */
590584 private void changeBack () {
591585 if (!(root instanceof MultiAndExpression )) {
592586 return ;
593587 }
594588 MultipleExpression temp = (MultipleExpression ) root ;
595589 for (int i = 0 ; i < temp .size (); i ++) {
596- temp .setChild (i , clone .changeBack (true , temp .getChild (i )));
590+ temp .setChild (i , CloneHelper .changeBack (true , temp .getChild (i )));
597591 }
598- root = clone .changeBack (false , temp );
592+ root = CloneHelper .changeBack (false , temp );
599593 }
600594
601595}
0 commit comments