From 2cee9c0bf5a8f402fe19bf92d242b82bca8efedf Mon Sep 17 00:00:00 2001 From: Tim Bernhard Date: Tue, 12 Dec 2023 19:29:35 +0100 Subject: [PATCH] Try to use perfect matches first, in Nahima serach --- .../mcz/imagecapture/data/NahimaManager.java | 22 +++++++++++++++++++ .../mcz/imagecapture/utility/JSONUtility.java | 13 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java b/src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java index cf98d6b..aa56285 100644 --- a/src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java +++ b/src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java @@ -385,6 +385,10 @@ protected JSONObject findSimilarMatch(JSONArray foundObjects, String objectType, int requiredMatches = 0; for (Iterator 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; } @@ -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. * @@ -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) { diff --git a/src/main/java/edu/harvard/mcz/imagecapture/utility/JSONUtility.java b/src/main/java/edu/harvard/mcz/imagecapture/utility/JSONUtility.java index ba68ee8..5f9a18b 100644 --- a/src/main/java/edu/harvard/mcz/imagecapture/utility/JSONUtility.java +++ b/src/main/java/edu/harvard/mcz/imagecapture/utility/JSONUtility.java @@ -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 keys = obj1.keys(); while (keys.hasNext()) { String key = keys.next(); @@ -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) {