diff --git a/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/api/EmpiMatchOutcome.java b/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/api/EmpiMatchOutcome.java index 9f9c7348977..4290773c7af 100644 --- a/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/api/EmpiMatchOutcome.java +++ b/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/api/EmpiMatchOutcome.java @@ -26,6 +26,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder; * This data object captures the final outcome of an EMPI match */ public final class EmpiMatchOutcome { + public static final EmpiMatchOutcome POSSIBLE_DUPLICATE = new EmpiMatchOutcome(null, null).setMatchResultEnum(EmpiMatchResultEnum.POSSIBLE_DUPLICATE); public static final EmpiMatchOutcome NO_MATCH = new EmpiMatchOutcome(null, null).setMatchResultEnum(EmpiMatchResultEnum.NO_MATCH); public static final EmpiMatchOutcome NEW_PERSON_MATCH = new EmpiMatchOutcome(null, null).setMatchResultEnum(EmpiMatchResultEnum.MATCH).setNewPerson(true); @@ -58,6 +59,11 @@ public final class EmpiMatchOutcome { */ private EmpiMatchResultEnum myMatchResultEnum; + /** + * Total number of EMPI rules checked for this outcome + */ + private int myEmpiRuleCount; + public EmpiMatchOutcome(Long theVector, Double theScore) { vector = theVector; score = theScore; @@ -99,6 +105,29 @@ public final class EmpiMatchOutcome { return myEidMatch; } + /** + * Sets the number of EMPI rules checked for this match outcome + * + * @param theEmpiRuleCount + * Number of EMPI rules that were checked for this match outcome + * @return + * Returns this instance + */ + public EmpiMatchOutcome setEmpiRuleCount(int theEmpiRuleCount) { + myEmpiRuleCount = theEmpiRuleCount; + return this; + } + + /** + * Gets the number of EMPI rules checked for this match outcome + * + * @return + * Returns the number of rules + */ + public int getEmpiRuleCount() { + return myEmpiRuleCount; + } + /** @param theEidMatch the link was established via a shared EID */ public EmpiMatchOutcome setEidMatch(boolean theEidMatch) { myEidMatch = theEidMatch; @@ -112,17 +141,10 @@ public final class EmpiMatchOutcome { * Returns the normalized score */ public Double getNormalizedScore() { - if (vector == 0) { + if (myEmpiRuleCount == 0) { return 0.0; - } else if (score > vector) { - return 1.0; } - - double retVal = score / vector; - if (retVal < 0) { - retVal = 0.0; - } - return retVal; + return score / myEmpiRuleCount; } @Override diff --git a/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/rules/svc/EmpiResourceMatcherSvc.java b/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/rules/svc/EmpiResourceMatcherSvc.java index 90afa38bffa..d7ee12ee301 100644 --- a/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/rules/svc/EmpiResourceMatcherSvc.java +++ b/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/rules/svc/EmpiResourceMatcherSvc.java @@ -125,6 +125,9 @@ public class EmpiResourceMatcherSvc { } score += matchEvaluation.score; } - return new EmpiMatchOutcome(vector, score); + + EmpiMatchOutcome retVal = new EmpiMatchOutcome(vector, score); + retVal.setEmpiRuleCount(myFieldMatchers.size()); + return retVal; } } diff --git a/hapi-fhir-server-empi/src/test/java/ca/uhn/fhir/empi/api/EmpiMatchOutcomeTest.java b/hapi-fhir-server-empi/src/test/java/ca/uhn/fhir/empi/api/EmpiMatchOutcomeTest.java index ecd5d8f52f5..f8c5c34865a 100644 --- a/hapi-fhir-server-empi/src/test/java/ca/uhn/fhir/empi/api/EmpiMatchOutcomeTest.java +++ b/hapi-fhir-server-empi/src/test/java/ca/uhn/fhir/empi/api/EmpiMatchOutcomeTest.java @@ -6,22 +6,20 @@ import static org.junit.jupiter.api.Assertions.*; class EmpiMatchOutcomeTest { + public static final double DELTA = 0.0001; + @Test void testNormalizedScore() { EmpiMatchOutcome outcome = new EmpiMatchOutcome(0l, 0.0); assertEquals(0.0, outcome.getNormalizedScore()); - outcome = new EmpiMatchOutcome(10l, 10.0); - assertEquals(1.0, outcome.getNormalizedScore()); + outcome = new EmpiMatchOutcome(null, 10.0); + outcome.setEmpiRuleCount(10); + assertEquals(1.0, outcome.getNormalizedScore(), DELTA); - outcome = new EmpiMatchOutcome(10l, -10.0); - assertEquals(0.0, outcome.getNormalizedScore()); - - outcome = new EmpiMatchOutcome(3l, 2.0); - assertEquals(2.0 / 3.0, outcome.getNormalizedScore(), 0.0001); - - outcome = new EmpiMatchOutcome(5l, 19.0); - assertEquals(1.0, outcome.getNormalizedScore()); + outcome = new EmpiMatchOutcome(null, 2.0); + outcome.setEmpiRuleCount(3); + assertEquals(2.0 / 3.0, outcome.getNormalizedScore(), DELTA); } }