-
Notifications
You must be signed in to change notification settings - Fork 4
feat(and-constraint): allow more than two children #24
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
Open
JuliaCoolia
wants to merge
6
commits into
main
Choose a base branch
from
issue-19
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−144
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c2ec8b
feat(and-constraint): allow more than two children
a7daa3a
feat: added getLeft and getRight for backwards compatibility
d5155b7
fix: implemented OrConstraint like AndConstraint allowing multiple ch…
ccd4b14
fix: throw ParseError in getLeft/getRight, also added setLeft/setRigh…
c4506eb
style: fix indentation
dee7d9d
fix: resolved merge conflict between issue-19 branch and main
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 |
|---|---|---|
|
|
@@ -3,67 +3,113 @@ | |
| import de.vill.model.building.AutomaticBrackets; | ||
| import de.vill.model.building.VariableReference; | ||
|
|
||
| import de.vill.exception.ParseError; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class AndConstraint extends Constraint { | ||
| private Constraint left; | ||
| private Constraint right; | ||
|
|
||
| private final List<Constraint> children = new ArrayList<>(); | ||
|
|
||
| public AndConstraint(Constraint... constraints) { | ||
| for (Constraint c : constraints) { | ||
| if (c != null) { | ||
| children.add(c); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public AndConstraint(Constraint left, Constraint right) { | ||
| this.left = left; | ||
| this.right = right; | ||
| this.children.add(left); | ||
| this.children.add(right); | ||
| } | ||
|
|
||
| public Constraint getLeft() { | ||
| return left; | ||
| if (children.isEmpty()) { | ||
| throw new ParseError("Left child can not be returned because there are no children."); | ||
| } else { | ||
| return children.get(0); | ||
| } | ||
| } | ||
|
|
||
| public Constraint getRight() { | ||
| return right; | ||
| if (children.isEmpty() || children.size() < 2) { | ||
| throw new ParseError("Right child can not be returned because there are less than two children."); | ||
| } else { | ||
| return children.get(children.size() - 1); | ||
| } | ||
| } | ||
|
|
||
| public List<Constraint> getChildren() { | ||
| return children; | ||
| } | ||
|
|
||
| public void setLeft(Constraint left) { | ||
| this.left = left; | ||
| if (children.isEmpty()) { | ||
| children.add(left); | ||
| } else { | ||
| children.set(0, left); | ||
| } | ||
| } | ||
|
|
||
| public void setRight(Constraint right){ | ||
| this.right = right; | ||
| public void setRight(Constraint right) { | ||
| if (children.size() < 2) { | ||
| if (children.size() < 1) { | ||
| children.add(null); | ||
| } | ||
| children.add(right); | ||
| } else { | ||
| children.set(children.size() - 1, right); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(boolean withSubmodels, String currentAlias) { | ||
| return AutomaticBrackets.enforceConstraintBracketsIfNecessary(this, left, withSubmodels, currentAlias) + | ||
| " & " + | ||
| AutomaticBrackets.enforceConstraintBracketsIfNecessary(this, right, withSubmodels, currentAlias); | ||
| return children.stream() | ||
| .map(c -> AutomaticBrackets.enforceConstraintBracketsIfNecessary(this, c, withSubmodels, currentAlias)) | ||
| .collect(Collectors.joining(" & ")); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Constraint> getConstraintSubParts() { | ||
| return Arrays.asList(left, right); | ||
| return new ArrayList<>(children); | ||
|
Member
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. Why do we need a new list here? Can we just return children? |
||
| } | ||
|
|
||
| @Override | ||
| public void replaceConstraintSubPart(Constraint oldSubConstraint, Constraint newSubConstraint) { | ||
| if (left == oldSubConstraint) { | ||
| left = newSubConstraint; | ||
| } else if (right == oldSubConstraint) { | ||
| right = newSubConstraint; | ||
| for (int i = 0; i < children.size(); i++) { | ||
| if (children.get(i) == oldSubConstraint) { | ||
| children.set(i, newSubConstraint); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Constraint clone() { | ||
| return new AndConstraint(left.clone(), right.clone()); | ||
| AndConstraint clone = new AndConstraint(); | ||
| for (Constraint c : children) { | ||
| clone.addChild(c.clone()); | ||
| } | ||
| return clone; | ||
| } | ||
|
|
||
| public void addChild(Constraint constraint) { | ||
| if (constraint != null) { | ||
| children.add(constraint); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode(int level) { | ||
| final int prime = 31; | ||
| int result = prime * level + (left == null ? 0 : left.hashCode(1 + level)); | ||
| result = prime * result + (right == null ? 0 : right.hashCode(1 + level)); | ||
| int result = prime * level; | ||
| for (Constraint c : children) { | ||
| result = prime * result + (c == null ? 0 : c.hashCode(1 + level)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
@@ -76,14 +122,15 @@ public boolean equals(Object obj) { | |
| return false; | ||
| } | ||
| AndConstraint other = (AndConstraint) obj; | ||
| return Objects.equals(left, other.left) && Objects.equals(right, other.right); | ||
| return Objects.equals(children, other.children); | ||
| } | ||
|
|
||
| @Override | ||
| public List<VariableReference> getReferences() { | ||
| List<VariableReference> references = new ArrayList<>(); | ||
| references.addAll(left.getReferences()); | ||
| references.addAll(right.getReferences()); | ||
| for (Constraint c : children) { | ||
| references.addAll(c.getReferences()); | ||
| } | ||
| return references; | ||
| } | ||
| } | ||
98 changes: 0 additions & 98 deletions
98
src/main/java/de/vill/model/constraint/MultiOrConstraint.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
For backwards compatibility, it may make sense to leave those and make them return the first and last element of the list respectively (same behavior as before when using the left/right constructor)