Skip to content

Commit 105f102

Browse files
Recipe to simplify Boolean expressions using De Morgan's laws (#659)
* Basic impl of SimplifyBooleanExpressionWithDeMorgan * More thorough tests * negate() method * Handling other binary operators better * Preserve prefix * Recursing into left and right * Fix for the main logic * Merging comments * Handling mixed operators without allOperatorsAreSame() * Refactor to avoid else * Optimize imports
1 parent 9cbf2e8 commit 105f102

File tree

2 files changed

+437
-0
lines changed

2 files changed

+437
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.staticanalysis;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.Tree;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.java.JavaVisitor;
23+
import org.openrewrite.java.ParenthesizeVisitor;
24+
import org.openrewrite.java.tree.*;
25+
import org.openrewrite.marker.Markers;
26+
27+
import java.time.Duration;
28+
import java.util.ArrayList;
29+
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.Set;
32+
33+
import static java.util.Objects.requireNonNull;
34+
35+
public class SimplifyBooleanExpressionWithDeMorgan extends Recipe {
36+
37+
@Override
38+
public String getDisplayName() {
39+
return "Simplify boolean expressions using De Morgan's laws";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Applies De Morgan's laws to simplify boolean expressions with negation. " +
45+
"Transforms `!(a && b)` to `!a || !b` and `!(a || b)` to `!a && !b`.";
46+
}
47+
48+
@Override
49+
public Set<String> getTags() {
50+
return Collections.singleton("RSPEC-1125");
51+
}
52+
53+
@Override
54+
public Duration getEstimatedEffortPerOccurrence() {
55+
return Duration.ofMinutes(2);
56+
}
57+
58+
@Override
59+
public TreeVisitor<?, ExecutionContext> getVisitor() {
60+
return new JavaVisitor<ExecutionContext>() {
61+
62+
@Override
63+
public J visitUnary(J.Unary unary, ExecutionContext ctx) {
64+
if (unary.getOperator() == J.Unary.Type.Not && unary.getExpression() instanceof J.Parentheses) {
65+
J.Parentheses<?> parentheses = (J.Parentheses<?>) unary.getExpression();
66+
if (parentheses.getTree() instanceof J.Binary) {
67+
J.Binary binary = (J.Binary) parentheses.getTree();
68+
J.Parentheses<J.Binary> parenthesesBinary = (J.Parentheses<J.Binary>) unary.getExpression();
69+
70+
J.Binary.Type newOperator = null;
71+
if (binary.getOperator() == J.Binary.Type.And) {
72+
newOperator = J.Binary.Type.Or;
73+
} else if (binary.getOperator() == J.Binary.Type.Or) {
74+
newOperator = J.Binary.Type.And;
75+
}
76+
Expression left = binary.getLeft();
77+
Expression right = binary.getRight();
78+
79+
if (newOperator != null) {
80+
left = negate(left);
81+
left = (Expression) new ParenthesizeVisitor<>().visit(left, ctx);
82+
right = negate(right);
83+
right = (Expression) new ParenthesizeVisitor<>().visit(right, ctx);
84+
}
85+
86+
left = (Expression) this.visit(left, ctx);
87+
right = (Expression) this.visit(right, ctx);
88+
89+
if (newOperator == null) {
90+
J.Binary visitedBinary = binary.withLeft(left).withRight(right);
91+
return unary.withExpression(parenthesesBinary.withTree(visitedBinary));
92+
}
93+
Space prefix = unary.getPrefix();
94+
List<Comment> comments = new ArrayList<>(prefix.getComments());
95+
comments.addAll(parenthesesBinary.getComments());
96+
comments.addAll(binary.getComments());
97+
prefix = prefix.withComments(comments);
98+
binary = binary.withLeft(left).withRight(right).withOperator(newOperator).withPrefix(prefix);
99+
return new ParenthesizeVisitor<>().visit(binary, ctx);
100+
}
101+
}
102+
return requireNonNull(super.visitUnary(unary, ctx));
103+
}
104+
105+
private Expression negate(Expression expression) {
106+
if (expression instanceof J.Unary) {
107+
J.Unary unaryExpr = (J.Unary) expression;
108+
if (unaryExpr.getOperator() == J.Unary.Type.Not) {
109+
return unaryExpr.getExpression().withPrefix(expression.getPrefix());
110+
}
111+
}
112+
return new J.Unary(
113+
Tree.randomId(),
114+
expression.getPrefix(),
115+
Markers.EMPTY,
116+
new JLeftPadded<>(Space.EMPTY, J.Unary.Type.Not, Markers.EMPTY),
117+
expression.withPrefix(Space.EMPTY),
118+
JavaType.Primitive.Boolean
119+
);
120+
}
121+
};
122+
}
123+
}

0 commit comments

Comments
 (0)