Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 70 additions & 23 deletions src/main/java/de/vill/model/constraint/AndConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Member

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)

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);
Copy link
Member

Choose a reason for hiding this comment

The 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;
}

Expand All @@ -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 src/main/java/de/vill/model/constraint/MultiOrConstraint.java

This file was deleted.

Loading
Loading