Skip to content

Commit acbe4ec

Browse files
authored
#175 Change behavior for zip codes that don't exist. (#189)
Signed-off-by: jzonthemtn <jeff.zemerick@philterd.ai>
1 parent 38e1c19 commit acbe4ec

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

phileas-model/src/main/java/ai/philterd/phileas/model/metadata/zipcode/ZipCodeMetadataResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,24 @@
2020
public class ZipCodeMetadataResponse extends MetadataResponse {
2121

2222
private final int population;
23+
private final boolean exists;
2324

2425
public ZipCodeMetadataResponse(int population) {
2526
this.population = population;
27+
this.exists = true;
28+
}
29+
30+
public ZipCodeMetadataResponse(int population, boolean exists) {
31+
this.population = population;
32+
this.exists = exists;
2633
}
2734

2835
public int getPopulation() {
2936
return population;
3037
}
3138

39+
public boolean isExists() {
40+
return exists;
41+
}
42+
3243
}

phileas-model/src/main/java/ai/philterd/phileas/model/metadata/zipcode/ZipCodeMetadataService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public ZipCodeMetadataResponse getMetadata(final ZipCodeMetadataRequest request)
3636

3737
final int population = zipCodes2010Census.getOrDefault(request.getZipCode(), -1);
3838

39-
return new ZipCodeMetadataResponse(population);
39+
if(population == -1) {
40+
// The zip code was not found.
41+
return new ZipCodeMetadataResponse(-1, false);
42+
} else {
43+
return new ZipCodeMetadataResponse(population);
44+
}
4045

4146
}
4247

phileas-model/src/main/java/ai/philterd/phileas/model/policy/filters/strategies/rules/ZipCodeFilterStrategy.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,29 @@ public boolean evaluateCondition(Policy policy, String context, String documentI
7474
final int value = Integer.parseInt(parsedCondition.getValue());
7575

7676
final ZipCodeMetadataResponse response = zipCodeMetadataService.getMetadata(new ZipCodeMetadataRequest(token));
77-
final long populationForZipCode = response.getPopulation();
7877

79-
if (StringUtils.equalsIgnoreCase(POPULATION, parsedCondition.getField())) {
78+
if(response.isExists()) {
8079

81-
conditionsSatisfied = switch (parsedCondition.getOperator()) {
82-
case GREATER_THAN -> (populationForZipCode > value);
83-
case LESS_THAN -> (populationForZipCode < value);
84-
case GREATER_THAN_EQUALS -> (populationForZipCode >= value);
85-
case LESS_THAN_EQUALS -> (populationForZipCode <= value);
86-
case EQUALS -> (populationForZipCode == value);
87-
case NOT_EQUALS -> (populationForZipCode != value);
88-
default -> conditionsSatisfied;
89-
};
80+
final long populationForZipCode = response.getPopulation();
81+
82+
if (StringUtils.equalsIgnoreCase(POPULATION, parsedCondition.getField())) {
83+
84+
conditionsSatisfied = switch (parsedCondition.getOperator()) {
85+
case GREATER_THAN -> (populationForZipCode > value);
86+
case LESS_THAN -> (populationForZipCode < value);
87+
case GREATER_THAN_EQUALS -> (populationForZipCode >= value);
88+
case LESS_THAN_EQUALS -> (populationForZipCode <= value);
89+
case EQUALS -> (populationForZipCode == value);
90+
case NOT_EQUALS -> (populationForZipCode != value);
91+
default -> conditionsSatisfied;
92+
};
93+
94+
}
95+
96+
} else {
97+
98+
// The zip code did not exist.
99+
conditionsSatisfied = false;
90100

91101
}
92102

phileas-model/src/test/java/ai/philterd/test/phileas/model/policy/filters/strategies/rules/ZipCodeFilterStrategyTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ public void evaluateCondition5() throws IOException {
111111

112112
}
113113

114+
@Test
115+
public void evaluateConditionInvalidZipCode() throws IOException {
116+
117+
ZipCodeFilterStrategy strategy = new ZipCodeFilterStrategy();
118+
119+
final boolean conditionSatisfied = strategy.evaluateCondition(getPolicy(), "context", "documentid", "12345", WINDOW,"population > 1", 1.0, attributes);
120+
121+
Assertions.assertFalse(conditionSatisfied);
122+
123+
}
124+
114125
@Test
115126
public void staticReplacement1() throws Exception {
116127

0 commit comments

Comments
 (0)