-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,15 @@ private static boolean isBooleanLiteral(Expression expr, boolean value) { | |
| return expr instanceof LiteralBoolean && expr.isBooleanTrue() == value; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| public static Predicate createConjunction(Predicate c1, Predicate c2) { | ||
| // simplification: (true && x) = x, (false && x) = false | ||
| if (isBooleanLiteral(c1.getExpression(), true)) | ||
|
|
@@ -205,6 +226,11 @@ public static Predicate createConjunction(Predicate c1, Predicate c2) { | |
| return c1; | ||
| if (isBooleanLiteral(c2.getExpression(), false)) | ||
| return c2; | ||
|
|
||
| // check if c2 is already present in the conjunctions of c1 | ||
| if (containsConjunct(c1, c2)) | ||
| return c1; | ||
|
|
||
|
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. there might be a case where c2 contains c1 so lets add that case. Also javadoc |
||
| return new Predicate(new BinaryExpression(c1.getExpression(), Ops.AND, c2.getExpression())); | ||
| } | ||
|
|
||
|
|
||
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.