Skip to content

Commit

Permalink
Improve fitsInto
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 16, 2024
1 parent 16223ab commit 8a392fb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

Expand All @@ -45,10 +44,13 @@ public static class Rule extends Precondition {
@Override
boolean fitsInto(Precondition p) {
if (p instanceof Rule) {
return ((Rule) p).rule.equals(rule);
} else {
return p.fitsInto(this);
return this.equals(p);
} else if (p instanceof Or) {
return ((Or) p).preconditions.stream().anyMatch(this::fitsInto);
} else if (p instanceof And) {
return ((And) p).preconditions.stream().anyMatch(this::fitsInto);
}
return false; // unreachable code
}

@Override
Expand All @@ -61,23 +63,26 @@ public String toString() {
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public static class Or extends Precondition {
Set<Precondition> preConditions;
Set<Precondition> preconditions;
int indent;

@Override
boolean fitsInto(Precondition p) {
throw new NotImplementedException();
if (p instanceof Or) {
return this.equals(p);
}
return false;
}

@Override
public Precondition prune() {
for (Precondition p : preConditions) {
for (Precondition p : preconditions) {
int matches = 0;
for (Precondition p2 : preConditions) {
for (Precondition p2 : preconditions) {
if (p == p2 || p.fitsInto(p2)) {
matches++;
}
if (matches == preConditions.size()) {
if (matches == preconditions.size()) {
return p;
}
}
Expand All @@ -88,35 +93,31 @@ public Precondition prune() {

@Override
public String toString() {
return joinPreconditions(preConditions, "or", indent);
return joinPreconditions(preconditions, "or", indent);
}
}

@Value
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public static class And extends Precondition {
Set<Precondition> preConditions;
Set<Precondition> preconditions;
int indent;

@Override
boolean fitsInto(Precondition p) {
if (p instanceof Rule) {
return preConditions.contains(p);
} else if (p instanceof Or) {
throw new NotImplementedException();
} else if (p instanceof And) {
if (preConditions.size() > ((And) p).preConditions.size()) {
if (p instanceof And) {
if (preconditions.size() > ((And) p).preconditions.size()) {
return false;
}
return preConditions.stream().allMatch(it -> it.fitsInto(p));
return preconditions.stream().allMatch(it -> it.fitsInto(p));
}
throw new IllegalArgumentException("Type is not supported: " + p.getClass());
return false;
}

@Override
public String toString() {
return joinPreconditions(preConditions, "and", indent);
return joinPreconditions(preconditions, "and", indent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,63 @@ void toStringWithInden() {
")");
}

@Test
void ruleFitsInRule() {
assertThat(new Precondition.Rule("A").fitsInto(new Precondition.Rule("A"))).isTrue();
}

@Test
void orFitsInOr() {
boolean result = new Precondition.Or(
setOf(new Precondition.Rule("A"), new Precondition.Rule("B")),
4
).fitsInto(new Precondition.Or(
setOf(new Precondition.Rule("B"), new Precondition.Rule("A")),
4
));

assertThat(result).isTrue();
}

@Test
void ardFitsNotInAndWithDifferentRules() {
boolean result = new Precondition.Or(
setOf(new Precondition.Rule("A"), new Precondition.Rule("C")),
4
).fitsInto(new Precondition.Or(
setOf(new Precondition.Rule("B"), new Precondition.Rule("A")),
4
));

assertThat(result).isFalse();
}

@Test
void andFitsInAnd() {
boolean result = new Precondition.And(
setOf(new Precondition.Rule("A")),
4
).fitsInto(new Precondition.And(
setOf(new Precondition.Rule("B"), new Precondition.Rule("A")),
4
));

assertThat(result).isTrue();
}

@Test
void andFitsNotInAndWithDifferentRules() {
boolean result = new Precondition.And(
setOf(new Precondition.Rule("A"), new Precondition.Rule("C")),
4
).fitsInto(new Precondition.And(
setOf(new Precondition.Rule("B"), new Precondition.Rule("A")),
4
));

assertThat(result).isFalse();
}

@Test
void sameRulesArePrunedAutomatically() {
Set<Precondition> result = setOf(new Precondition.Rule("A"), new Precondition.Rule("A"));
Expand All @@ -67,12 +124,12 @@ void sameRulesArePrunedAutomatically() {

@Test
void sameRulesArePrunedAutomaticallyInAnOr() {
Precondition x = new Precondition.Or(
Precondition result = new Precondition.Or(
setOf(new Precondition.Rule("A"), new Precondition.Rule("A")),
4
);

assertThat(x).isEqualTo(new Precondition.Or(
assertThat(result).isEqualTo(new Precondition.Or(
setOf(new Precondition.Rule("A")),
4
));
Expand Down

0 comments on commit 8a392fb

Please sign in to comment.