-
Notifications
You must be signed in to change notification settings - Fork 90
Recipe to simplify Boolean expressions using De Morgan's laws #659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6fa1b10
Basic impl of SimplifyBooleanExpressionWithDeMorgan
greg-at-moderne 6339422
More thorough tests
greg-at-moderne a0c3945
negate() method
greg-at-moderne 73e96f4
Handling other binary operators better
greg-at-moderne 2c010d8
Preserve prefix
greg-at-moderne b2116fa
Recursing into left and right
greg-at-moderne 8c7c5b0
Fix for the main logic
greg-at-moderne 89ed0ca
Merging comments
greg-at-moderne 02d40ae
Handling mixed operators without allOperatorsAreSame()
greg-at-moderne e61e84c
Refactor to avoid else
greg-at-moderne 6cba288
Optimize imports
greg-at-moderne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
src/main/java/org/openrewrite/staticanalysis/SimplifyBooleanExpressionWithDeMorgan.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.Tree; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.ParenthesizeVisitor; | ||
| import org.openrewrite.java.tree.*; | ||
| import org.openrewrite.marker.Markers; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class SimplifyBooleanExpressionWithDeMorgan extends Recipe { | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Simplify boolean expressions using De Morgan's laws"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Applies De Morgan's laws to simplify boolean expressions with negation. " + | ||
| "Transforms `!(a && b)` to `!a || !b` and `!(a || b)` to `!a && !b`."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return Collections.singleton("RSPEC-1125"); | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getEstimatedEffortPerOccurrence() { | ||
| return Duration.ofMinutes(2); | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new JavaVisitor<ExecutionContext>() { | ||
|
|
||
| @Override | ||
| public J visitUnary(J.Unary unary, ExecutionContext ctx) { | ||
| if (unary.getOperator() == J.Unary.Type.Not && unary.getExpression() instanceof J.Parentheses) { | ||
| J.Parentheses<?> parentheses = (J.Parentheses<?>) unary.getExpression(); | ||
| if (parentheses.getTree() instanceof J.Binary) { | ||
| J.Binary binary = (J.Binary) parentheses.getTree(); | ||
| J.Parentheses<J.Binary> parenthesesBinary = (J.Parentheses<J.Binary>) unary.getExpression(); | ||
|
|
||
| J.Binary.Type newOperator = null; | ||
| if (binary.getOperator() == J.Binary.Type.And) { | ||
| newOperator = J.Binary.Type.Or; | ||
| } else if (binary.getOperator() == J.Binary.Type.Or) { | ||
| newOperator = J.Binary.Type.And; | ||
| } | ||
| Expression left = binary.getLeft(); | ||
| Expression right = binary.getRight(); | ||
|
|
||
| if (newOperator != null) { | ||
| left = negate(left); | ||
| left = (Expression) new ParenthesizeVisitor<>().visit(left, ctx); | ||
| right = negate(right); | ||
| right = (Expression) new ParenthesizeVisitor<>().visit(right, ctx); | ||
| } | ||
|
|
||
| left = (Expression) this.visit(left, ctx); | ||
| right = (Expression) this.visit(right, ctx); | ||
|
|
||
| if (newOperator == null) { | ||
| J.Binary visitedBinary = binary.withLeft(left).withRight(right); | ||
| return unary.withExpression(parenthesesBinary.withTree(visitedBinary)); | ||
| } | ||
| Space prefix = unary.getPrefix(); | ||
| List<Comment> comments = new ArrayList<>(prefix.getComments()); | ||
| comments.addAll(parenthesesBinary.getComments()); | ||
| comments.addAll(binary.getComments()); | ||
| prefix = prefix.withComments(comments); | ||
| binary = binary.withLeft(left).withRight(right).withOperator(newOperator).withPrefix(prefix); | ||
| return new ParenthesizeVisitor<>().visit(binary, ctx); | ||
| } | ||
| } | ||
| return requireNonNull(super.visitUnary(unary, ctx)); | ||
| } | ||
|
|
||
| private Expression negate(Expression expression) { | ||
| if (expression instanceof J.Unary) { | ||
| J.Unary unaryExpr = (J.Unary) expression; | ||
| if (unaryExpr.getOperator() == J.Unary.Type.Not) { | ||
| return unaryExpr.getExpression().withPrefix(expression.getPrefix()); | ||
| } | ||
| } | ||
| return new J.Unary( | ||
| Tree.randomId(), | ||
| expression.getPrefix(), | ||
| Markers.EMPTY, | ||
| new JLeftPadded<>(Space.EMPTY, J.Unary.Type.Not, Markers.EMPTY), | ||
| expression.withPrefix(Space.EMPTY), | ||
| JavaType.Primitive.Boolean | ||
| ); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListUtils.concatmight be somewhat preferred here, just to ensure that pattern is used where applicable as well.