use of net.sf.jsqlparser.expression.NotExpression in project JSqlParser by JSQLParser.
the class CNFConverter method handleNot.
/**
* This function mainly deals with pushing not operators down.
* check the child. If it is not a logic operator(and or or).
* stop at that point. Else use De Morgan law to push not downwards.
* @param index the index of the children appeared in parents array.
*/
private void handleNot(int index) {
child = ((NotExpression) temp1).getExpression();
// takes down the number of not operators.
int nums = 1;
while (child instanceof NotExpression) {
child = ((NotExpression) child).getExpression();
nums++;
}
/* if the number of not operators are even. we could get
* rid of all the not operators. set the child to the parent. */
if (nums % 2 == 0) {
((MultipleExpression) temp2).setChild(index, child);
temp1 = child;
pushNot(-1);
} else {
/* otherwise there will be one not left to push.
* if the child is not these two types of operators.
* that means we reach the leaves of the logical part.
* set a new not operator whose child is the current one
* and connect that operator with the parent and return. */
if (!(child instanceof MultiAndExpression) && !(child instanceof MultiOrExpression)) {
if (child instanceof LikeExpression) {
((LikeExpression) child).setNot(true);
} else if (child instanceof BinaryExpression) {
((BinaryExpression) child).setNot();
} else {
child = new NotExpression(child);
}
((MultipleExpression) temp2).setChild(index, child);
return;
} else if (child instanceof MultiAndExpression) {
MultiAndExpression and = (MultiAndExpression) child;
List<Expression> list = new ArrayList<Expression>();
for (int i = 0; i < and.size(); i++) {
/* push not to every element in the operator. */
NotExpression not = new NotExpression(and.getChild(i));
list.add(not);
}
/* the De Morgan law shows we need to change and to or. */
temp1 = new MultiOrExpression(list);
((MultipleExpression) temp2).setChild(index, temp1);
pushNot(-1);
} else if (child instanceof MultiOrExpression) {
MultiOrExpression or = (MultiOrExpression) child;
List<Expression> list = new ArrayList<Expression>();
for (int i = 0; i < or.size(); i++) {
/* push not to every element in the operator. */
NotExpression not = new NotExpression(or.getChild(i));
list.add(not);
}
/* the De Morgan law shows we need to change or to and. */
temp1 = new MultiAndExpression(list);
((MultipleExpression) temp2).setChild(index, temp1);
pushNot(-1);
}
}
}
use of net.sf.jsqlparser.expression.NotExpression in project JSqlParser by JSQLParser.
the class CloneHelper method modify.
/**
* This method is used for changing the logical structure of the tree.
* The main method is to convert and operator and or operator to let
* them have multiple children (reflected in MultipleExpression.java
* from the conditional package). Note if the value from the conditional
* operator has a not attached to it we need to append an not operator
* ahead of it since the not operator needed to be pushed down during
* the second step. Also, I will leave out all the parenthesis expression
* which is connected to the conditional operator.
* @param express the expression that will be modified
* @return the modified expression.
*/
public Expression modify(Expression express) {
if (express instanceof NotExpression) {
return new NotExpression(modify(((NotExpression) express).getExpression()));
}
if (express instanceof Parenthesis) {
Parenthesis parenthesis = (Parenthesis) express;
Expression result = modify(parenthesis.getExpression());
if (parenthesis.isNot()) {
return new NotExpression(result);
}
return result;
}
if (express instanceof AndExpression) {
AndExpression and = (AndExpression) express;
List<Expression> list = new ArrayList<Expression>();
list.add(modify(and.getLeftExpression()));
list.add(modify(and.getRightExpression()));
MultiAndExpression result = new MultiAndExpression(list);
if (and.isNot()) {
return new NotExpression(result);
}
return result;
}
if (express instanceof OrExpression) {
OrExpression or = (OrExpression) express;
List<Expression> list = new ArrayList<Expression>();
list.add(modify(or.getLeftExpression()));
list.add(modify(or.getRightExpression()));
MultiOrExpression result = new MultiOrExpression(list);
if (or.isNot()) {
return new NotExpression(result);
}
return result;
}
if (express instanceof BinaryExpression) {
BinaryExpression binary = (BinaryExpression) express;
if (binary.isNot()) {
binary.removeNot();
return new NotExpression(modify(binary));
}
}
/* at this stage, there is no need to modify, just simply return. */
return express;
}
Aggregations