-
Notifications
You must be signed in to change notification settings - Fork 35
Simplify Conjunctions #182
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
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -183,6 +183,18 @@ public Predicate clone() { | |
| return new Predicate(exp.clone()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) | ||
| return true; | ||
| if (obj == null) | ||
| return false; | ||
| if (getClass() != obj.getClass()) | ||
| return false; | ||
| Predicate other = (Predicate) obj; | ||
| return exp.equals(other.exp); | ||
| } | ||
|
|
||
| public Expression getExpression() { | ||
| return exp; | ||
| } | ||
|
|
@@ -195,6 +207,22 @@ private static boolean isBooleanLiteral(Expression expr, boolean value) { | |
| return expr instanceof LiteralBoolean && expr.isBooleanTrue() == value; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if c2 is a conjunct in c1 | ||
| */ | ||
| private static boolean containsConjunct(Predicate c1, Predicate c2) { | ||
| if (c1.toString().equals(c2.toString())) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used string comparison instead of
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets juts add this to the javadoc so we know in the future |
||
| return true; | ||
| if (c1.getExpression()instanceof BinaryExpression be && be.getOperator().equals(Ops.AND)) | ||
| return containsConjunct(new Predicate(be.getFirstOperand()), c2) | ||
| || containsConjunct(new Predicate(be.getSecondOperand()), c2); | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new predicate representing the conjunction of c1 and c2 Contains simplification rules for redundant | ||
| * conjuncts such as not adding conjunct if already present in conjunction | ||
| */ | ||
| public static Predicate createConjunction(Predicate c1, Predicate c2) { | ||
| // simplification: (true && x) = x, (false && x) = false | ||
| if (isBooleanLiteral(c1.getExpression(), true)) | ||
|
|
@@ -205,6 +233,13 @@ public static Predicate createConjunction(Predicate c1, Predicate c2) { | |
| return c1; | ||
| if (isBooleanLiteral(c2.getExpression(), false)) | ||
| return c2; | ||
|
|
||
| // check if conjunct is already present in the conjunction | ||
| if (containsConjunct(c1, c2)) | ||
| return c1; | ||
| if (containsConjunct(c2, c1)) | ||
| return c2; | ||
|
|
||
rcosta358 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new Predicate(new BinaryExpression(c1.getExpression(), Ops.AND, c2.getExpression())); | ||
| } | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Added missing
equals()for predicates anyway.