Program: 189670
Project: 189670
Buggy source:
@Override public CalculationResultMap evaluate(Collection<Integer> cohort,Map<String,Object> params,PatientCalculationContext context){
  PregnancyStage stage=(params != null && params.containsKey("String_Node_Str")) ? (PregnancyStage)params.get("String_Node_Str") : null;
  Concept result=(params != null && params.containsKey("String_Node_Str")) ? (Concept)params.get("String_Node_Str") : null;
  Boolean partner=(params != null && params.containsKey("String_Node_Str")) ? (Boolean)params.get("String_Node_Str") : false;
  Program mchmsProgram=MetadataUtils.getProgram(MchMetadata._Program.MCHMS);
  Set<Integer> alivePatients=Filters.alive(cohort,context);
  CalculationResultMap activePatientPrograms=Calculations.activeEnrollment(mchmsProgram,alivePatients,context);
  Set<Integer> aliveMchmsPatients=CalculationUtils.patientsThatPass(activePatientPrograms);
  Concept hivStatusConcept=partner ? Dictionary.getConcept(Dictionary.PARTNER_HIV_STATUS) : Dictionary.getConcept(Dictionary.HIV_STATUS);
  Concept hivTestDateConcept=partner ? Dictionary.getConcept(Dictionary.DATE_OF_PARTNER_HIV_DIAGNOSIS) : Dictionary.getConcept(Dictionary.DATE_OF_HIV_DIAGNOSIS);
  CalculationResultMap lastHivStatusObss=Calculations.lastObs(hivStatusConcept,aliveMchmsPatients,context);
  CalculationResultMap lastHivTestDateObss=Calculations.lastObs(hivTestDateConcept,aliveMchmsPatients,context);
  CalculationResultMap lastDeliveryDateObss=Calculations.lastObs(Dictionary.getConcept(Dictionary.DATE_OF_CONFINEMENT),aliveMchmsPatients,context);
  CalculationResultMap resultMap=new CalculationResultMap();
  for (  Integer ptId : cohort) {
    Concept patientsLastHivStatus=EmrCalculationUtils.codedObsResultForPatient(lastHivStatusObss,ptId);
    Date patientsLastHivTestDate=EmrCalculationUtils.datetimeObsResultForPatient(lastHivTestDateObss,ptId);
    CalculationResult activePatientProgram=activePatientPrograms.get(ptId);
    boolean qualified=false;
    if (aliveMchmsPatients.contains(ptId) && (patientsLastHivStatus != null && (patientsLastHivStatus.equals(Dictionary.getConcept(Dictionary.POSITIVE)) || patientsLastHivStatus.equals(Dictionary.getConcept(Dictionary.NEGATIVE)))) && (activePatientProgram != null)) {
      Date enrollmentDate=((PatientProgram)activePatientProgram.getValue()).getDateEnrolled();
      Date deliveryDate=EmrCalculationUtils.datetimeObsResultForPatient(lastDeliveryDateObss,ptId);
      if (deliveryDate != null && deliveryDate.before(enrollmentDate)) {
        deliveryDate=null;
      }
      qualified=qualifiedByStage(stage,enrollmentDate,patientsLastHivTestDate,deliveryDate) && (result == null ? true : result.equals(patientsLastHivStatus));
    }
    resultMap.put(ptId,new BooleanResult(qualified,this,context));
  }
  return resultMap;
}

Fixed version (ground truth):
@Override public CalculationResultMap evaluate(Collection<Integer> cohort,Map<String,Object> params,PatientCalculationContext context){
  PregnancyStage stage=(params != null && params.containsKey("String_Node_Str")) ? (PregnancyStage)params.get("String_Node_Str") : null;
  Concept result=(params != null && params.containsKey("String_Node_Str")) ? (Concept)params.get("String_Node_Str") : null;
  Boolean partner=(params != null && params.containsKey("String_Node_Str")) ? (Boolean)params.get("String_Node_Str") : false;
  Program mchmsProgram=MetadataUtils.getProgram(MchMetadata._Program.MCHMS);
  Set<Integer> alivePatients=Filters.alive(cohort,context);
  CalculationResultMap activePatientPrograms=Calculations.activeEnrollment(mchmsProgram,alivePatients,context);
  Set<Integer> aliveMchmsPatients=CalculationUtils.patientsThatPass(activePatientPrograms);
  Concept hivStatusConcept=partner ? Dictionary.getConcept(Dictionary.PARTNER_HIV_STATUS) : Dictionary.getConcept(Dictionary.HIV_STATUS);
  Concept hivTestDateConcept=partner ? Dictionary.getConcept(Dictionary.DATE_OF_PARTNER_HIV_DIAGNOSIS) : Dictionary.getConcept(Dictionary.DATE_OF_HIV_DIAGNOSIS);
  CalculationResultMap lastHivStatusObss=Calculations.lastObs(hivStatusConcept,aliveMchmsPatients,context);
  CalculationResultMap lastHivTestDateObss=Calculations.lastObs(hivTestDateConcept,aliveMchmsPatients,context);
  CalculationResultMap lastDeliveryDateObss=Calculations.lastObs(Dictionary.getConcept(Dictionary.DATE_OF_CONFINEMENT),aliveMchmsPatients,context);
  CalculationResultMap resultMap=new CalculationResultMap();
  for (  Integer ptId : cohort) {
    Concept lastHivStatus=EmrCalculationUtils.codedObsResultForPatient(lastHivStatusObss,ptId);
    Date lastHivTestDate=EmrCalculationUtils.datetimeObsResultForPatient(lastHivTestDateObss,ptId);
    CalculationResult activePatientProgram=activePatientPrograms.get(ptId);
    boolean qualified=false;
    if (aliveMchmsPatients.contains(ptId) && (lastHivStatus != null && (lastHivStatus.equals(Dictionary.getConcept(Dictionary.POSITIVE)) || lastHivStatus.equals(Dictionary.getConcept(Dictionary.NEGATIVE)))) && (activePatientProgram != null)) {
      Date enrollmentDate=((PatientProgram)activePatientProgram.getValue()).getDateEnrolled();
      Date deliveryDate=EmrCalculationUtils.datetimeObsResultForPatient(lastDeliveryDateObss,ptId);
      if (deliveryDate != null && deliveryDate.before(enrollmentDate)) {
        deliveryDate=null;
      }
      qualified=qualifiedByStage(stage,enrollmentDate,lastHivTestDate,deliveryDate) && (result == null ? true : result.equals(lastHivStatus));
    }
    resultMap.put(ptId,new BooleanResult(qualified,this,context));
  }
  return resultMap;
}