Skip to content

Commit cfa38d7

Browse files
committed
Changed matching logic with regard to missing/null keys. (#12)
Previously, if a INPUT cell was a blank value, it would only match to blank value input. For regular input values this was not an issue since if you didn't supply an input it was defaulted to blank. However intermediate values during the staging process were not handled the same way. If the input was not in the context, it would not match. Now it will match since keys not in the context are handled exactly the same as blank. (cherry picked from commit b31c2f9)
1 parent e717712 commit cfa38d7

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies {
2626
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
2727

2828
// morphia annotations are needed to make it easier to use in SEER*API
29-
compile 'org.mongodb.morphia:morphia:0.108'
29+
compile 'org.mongodb.morphia:morphia:1.0.1'
3030

3131
compile 'com.google.guava:guava:18.0'
3232

src/main/java/com/imsweb/staging/entities/StagingStringRange.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ public boolean contains(String value, Map<String, String> context) {
6767
if (matchesAll())
6868
return true;
6969

70+
// make null values match the same as if they were blank
71+
if (value == null)
72+
value = "";
73+
7074
// translate the context values if they are there
7175
String low = DecisionEngine.translateValue(_low, context);
7276
String high = DecisionEngine.translateValue(_high, context);
7377

7478
// if the context value(s) failed or the low and high values are different length, return false
75-
if (value == null || low.length() != high.length() || low.length() != value.length())
79+
if (low.length() != high.length() || low.length() != value.length())
7680
return false;
7781

7882
// compare value to low and high

src/test/java/com/imsweb/decisionengine/DecisionEngineTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,52 @@ public void testMatchTable() throws IOException {
355355
Assert.assertNull(DecisionEngine.matchTable(matchTable, input));
356356
}
357357

358+
@Test
359+
public void testMatchTableWithBlankOrMissingInput() throws IOException {
360+
BasicDataProvider provider = new BasicDataProvider();
361+
BasicTable table = new BasicTable("basic_test_table");
362+
table.addColumnDefinition("size", ColumnType.INPUT);
363+
table.addColumnDefinition("description", ColumnType.DESCRIPTION);
364+
table.addColumnDefinition("result", ColumnType.ENDPOINT);
365+
table.addRawRow("", "BLANK", "MATCH");
366+
table.addRawRow("1", "ONE", "MATCH");
367+
table.addRawRow("2", "TWO", "MATCH");
368+
provider.addTable(table);
369+
370+
Table matchTable = provider.getTable("basic_test_table");
371+
Assert.assertNotNull(matchTable);
372+
373+
// create context of input fields
374+
Map<String, String> input = new HashMap<String, String>();
375+
376+
// first try it with missing input (null should match just like blank))
377+
Assert.assertNotNull(DecisionEngine.matchTable(matchTable, input));
378+
379+
// now add blank input
380+
input.put("size", "");
381+
Assert.assertNotNull(DecisionEngine.matchTable(matchTable, input));
382+
383+
// test matching on multiple mising values
384+
table = new BasicTable("basic_test_table_multi");
385+
table.addColumnDefinition("a", ColumnType.INPUT);
386+
table.addColumnDefinition("b", ColumnType.INPUT);
387+
table.addColumnDefinition("c", ColumnType.INPUT);
388+
table.addColumnDefinition("d", ColumnType.INPUT);
389+
table.addColumnDefinition("e", ColumnType.INPUT);
390+
table.addRawRow("1", "", "", "", "");
391+
table.addRawRow("2", "", "", "", "");
392+
provider.addTable(table);
393+
394+
matchTable = provider.getTable("basic_test_table_multi");
395+
Assert.assertNotNull(matchTable);
396+
397+
// first try it with missing input (null should match just like blank))
398+
Assert.assertNull(DecisionEngine.matchTable(matchTable, new HashMap<String, String>()));
399+
400+
input.put("a", "2");
401+
Assert.assertNotNull(DecisionEngine.matchTable(matchTable, input));
402+
}
403+
358404
@Test
359405
public void testMatchOnSpecificKeys() {
360406
BasicDataProvider provider = new BasicDataProvider();

src/test/java/com/imsweb/decisionengine/basic/BasicStringRange.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public BasicStringRange() {
2323

2424
/**
2525
* Construct a BasicStringRange with a low and high bound
26-
* @param low
27-
* @param high
26+
* @param low low value
27+
* @param high high value
2828
*/
2929
public BasicStringRange(String low, String high) {
3030
if (low == null || high == null)
@@ -50,17 +50,21 @@ public String getHigh() {
5050

5151
/**
5252
* If low and high are both null, then this range matches all strings
53-
* @return
53+
* @return true if range matches anything
5454
*/
5555
private boolean matchesAll() {
5656
return _low == null && _high == null;
5757
}
5858

5959
@Override
6060
public boolean contains(String value, Map<String, String> context) {
61+
// make null values match the same as if they were blank
62+
if (value == null)
63+
value = "";
64+
6165
if (_usesContext)
62-
return (matchesAll() || (value != null && DecisionEngine.translateValue(_low, context).compareTo(value) <= 0 && DecisionEngine.translateValue(_high, context).compareTo(value) >= 0));
66+
return (matchesAll() || (DecisionEngine.translateValue(_low, context).compareTo(value) <= 0 && DecisionEngine.translateValue(_high, context).compareTo(value) >= 0));
6367
else
64-
return (matchesAll() || (value != null && _low.compareTo(value) <= 0 && _high.compareTo(value) >= 0));
68+
return (matchesAll() || (_low.compareTo(value) <= 0 && _high.compareTo(value) >= 0));
6569
}
6670
}

0 commit comments

Comments
 (0)