Implement matchField testing for Expression.not (#201)

pull/205/head
Brian Sperlongano 2022-04-30 09:42:24 -04:00 zatwierdzone przez GitHub
rodzic 186af36c42
commit 891537e2bc
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 82 dodań i 5 usunięć

Wyświetl plik

@ -68,8 +68,8 @@ public record MultiExpression<T> (List<Entry<T>> expressions) {
and.children().forEach(child -> getRelevantKeys(child, acceptKey));
} else if (exp instanceof Expression.Or or) {
or.children().forEach(child -> getRelevantKeys(child, acceptKey));
} else if (exp instanceof Expression.Not) {
// ignore anything that's purely used as a filter
} else if (exp instanceof Expression.Not not) {
getRelevantMissingKeys(not.child(), acceptKey);
} else if (exp instanceof Expression.MatchField field) {
acceptKey.accept(field.field());
} else if (exp instanceof Expression.MatchAny any) {
@ -86,8 +86,8 @@ public record MultiExpression<T> (List<Entry<T>> expressions) {
and.children().forEach(child -> getRelevantMissingKeys(child, acceptKey));
} else if (exp instanceof Expression.Or or) {
or.children().forEach(child -> getRelevantMissingKeys(child, acceptKey));
} else if (exp instanceof Expression.Not) {
// ignore anything that's purely used as a filter
} else if (exp instanceof Expression.Not not) {
getRelevantKeys(not.child(), acceptKey);
} else if (exp instanceof Expression.MatchAny any && any.matchWhenMissing()) {
acceptKey.accept(any.field());
}
@ -100,7 +100,7 @@ public record MultiExpression<T> (List<Entry<T>> expressions) {
}
boolean caresAboutGeometryType =
expressions.stream().anyMatch(entry -> entry.expression.contains(exp -> exp instanceof Expression.MatchType));
return caresAboutGeometryType ? new GeometryTypeIndex<>(this) : new KeyIndex<>(this);
return caresAboutGeometryType ? new GeometryTypeIndex<>(this) : new KeyIndex<>(simplify());
}
/** Returns a copy of this multi-expression that replaces every expression using {@code mapper}. */

Wyświetl plik

@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.onthegomap.planetiler.expression.MultiExpression.Index;
import com.onthegomap.planetiler.reader.SimpleFeature;
import com.onthegomap.planetiler.reader.SourceFeature;
import com.onthegomap.planetiler.reader.WithTags;
@ -124,6 +125,18 @@ class MultiExpressionTest {
var index = MultiExpression.of(List.of(
entry("a", matchField("key"))
)).index();
matchFieldCheck(index);
}
@Test
void testDoubleInverseMatchField() {
var index = MultiExpression.of(List.of(
entry("a", not(not(matchField("key"))))
)).index();
matchFieldCheck(index);
}
private void matchFieldCheck(Index<String> index) {
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key", "value")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key", "")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key", "value2", "otherkey", "othervalue")));
@ -133,6 +146,20 @@ class MultiExpressionTest {
assertSameElements(List.of(), index.getMatches(featureWithTags()));
}
@Test
void testInverseMatchField() {
var index = MultiExpression.of(List.of(
entry("a", not(matchField("key")))
)).index();
assertSameElements(List.of(), index.getMatches(featureWithTags("key", "value")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key", "")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key", "value2", "otherkey", "othervalue")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key2", "value", "key3", "value")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key2", "value")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key2", "no")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags()));
}
@Test
void testStaticBooleanMatch() {
var index = MultiExpression.of(List.of(entry("t", TRUE))).index();
@ -271,6 +298,56 @@ class MultiExpressionTest {
assertSameElements(List.of(), index.getMatches(featureWithTags()));
}
@Test
void testXor() {
var index = MultiExpression.of(List.of(
entry("a",
or(
and(
not(matchAny("key1", "val1")),
matchAny("key2", "val2")
),
and(
matchAny("key1", "val1"),
not(matchAny("key2", "val2"))
))
)
)).index();
assertSameElements(List.of(), index.getMatches(featureWithTags("key1", "val1", "key2", "val2")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key1", "val1", "key2", "val2", "key3", "val3")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key1", "no", "key2", "val2")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key1", "val1", "key2", "no")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key1", "val1")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key2", "val2")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key1", "no", "key2", "no")));
assertSameElements(List.of(), index.getMatches(featureWithTags()));
}
@Test
void testXnor() {
var index = MultiExpression.of(List.of(
entry("a",
or(
and(
not(matchAny("key1", "val1")),
not(matchAny("key2", "val2"))
),
and(
matchAny("key1", "val1"),
matchAny("key2", "val2")
))
)
)).index();
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key1", "val1", "key2", "val2")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key1", "val1", "key2", "val2", "key3", "val3")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key1", "no", "key2", "val2")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key1", "val1", "key2", "no")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key1", "val1")));
assertSameElements(List.of(), index.getMatches(featureWithTags("key2", "val2")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags("key1", "no", "key2", "no")));
assertSameElements(List.of("a"), index.getMatches(featureWithTags()));
}
@Test
void testMatchesMultiple() {
var index = MultiExpression.of(List.of(