Skip to content

Commit

Permalink
Try to use perfect matches first, in Nahima serach
Browse files Browse the repository at this point in the history
  • Loading branch information
GenieTim committed Dec 12, 2023
1 parent b7e2510 commit 2cee9c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ protected JSONObject findSimilarMatch(JSONArray foundObjects, String objectType,
int requiredMatches = 0;
for (Iterator<String> it = selectionHelper.keys(); it.hasNext(); ) {
String key = it.next();
if (key.startsWith("_")) {
continue;
}
// TODO: compare more than just Strings
if (selectionHelper.get(key) instanceof String && !((String) selectionHelper.get(key)).contains("DataShot")) {
requiredMatches += 1;
}
Expand Down Expand Up @@ -419,6 +423,19 @@ protected JSONObject findSimilarMatch(JSONArray foundObjects, String objectType,
return null;
}

protected JSONObject findExactMatch(JSONArray foundObjects, String objectType, JSONObject selectionHelper) {
for (int i = 0; i < foundObjects.length(); ++i) {
JSONObject testObj = foundObjects.getJSONObject(i);
if (JSONUtility.areEqualIgnoringUnderscore(testObj, selectionHelper)) {
return testObj;
}
if (testObj.has(objectType) && JSONUtility.areEqualIgnoringUnderscore(selectionHelper, testObj.getJSONObject(objectType))) {
return testObj;
}
}
return null;
}

/**
* Search an object in Nahima by a string. Create it if it is not found.
*
Expand Down Expand Up @@ -522,6 +539,11 @@ public JSONObject resolveOrCreateInteractive(String name, String objectType, Str
log.info("Got " + foundObjects.length() + " != 1 " + objectType + " status. {}", results);
// create / select correct
if (foundObjects.length() > 1) {
JSONObject perfectMatch = findExactMatch(foundObjects, objectType, inner);
if (perfectMatch != null) {
return perfectMatch;
}
// TODO: maybe, we only want the perfect match, and nothing else?!?
// first, loop objects to see whether we can find exactly one exact match
JSONObject bestMatch = findSimilarMatch(foundObjects, objectType, inner, !this.interactive);
if (bestMatch != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public static boolean areEqualIgnoringUnderscore(JSONObject obj1, JSONObject obj
}

private static boolean areEqualIgnoringUnderscore(JSONObject obj1, JSONObject obj2, boolean inverted) {
assert(obj1 != null);
if (obj1 == null) {
return obj2 == null;
}
if (obj2 == null) {
return false;
}
Iterator<String> keys = obj1.keys();
while (keys.hasNext()) {
String key = keys.next();
Expand All @@ -51,6 +56,12 @@ private static boolean areEqualIgnoringUnderscore(JSONObject obj1, JSONObject ob
}

private static boolean objectsAreEqual(Object val1, Object val2) {
if (val1 == null) {
return val2 == null;
}
if (val2 == null) {
return false;
}
if (val1 instanceof JSONObject && val2 instanceof JSONObject) {
return areEqualIgnoringUnderscore((JSONObject) val1, (JSONObject) val2);
} else if (val1 instanceof JSONArray && val2 instanceof JSONArray) {
Expand Down

0 comments on commit 2cee9c0

Please sign in to comment.