/**
   * @see
   *     PatientDataEvaluator#evaluate(org.openmrs.module.reporting.data.patient.definition.PatientDataDefinition,
   *     org.openmrs.module.reporting.evaluation.EvaluationContext)
   */
  public EvaluatedPatientData evaluate(PatientDataDefinition definition, EvaluationContext context)
      throws EvaluationException {

    CalculationDataDefinition def = (CalculationDataDefinition) definition;
    EvaluatedPatientData c = new EvaluatedPatientData(def, context);

    // return right away if there is nothing to evaluate
    if (context.getBaseCohort() != null && context.getBaseCohort().isEmpty()) {
      return c;
    }

    // evaluate the calculation
    PatientCalculationService service = Context.getService(PatientCalculationService.class);
    CalculationResultMap resultMap =
        service.evaluate(
            context.getBaseCohort().getMemberIds(),
            def.getCalculation(),
            def.getCalculationParameters(),
            context);

    // move data into return object
    for (Map.Entry<Integer, CalculationResult> entry : resultMap.entrySet()) {
      c.addData(entry.getKey(), entry.getValue());
    }

    return c;
  }
 /**
  * Extracts patients from a calculation result map with date results in the given range
  *
  * @param results the calculation result map
  * @param minDateInclusive the minimum date (inclusive)
  * @param maxDateInclusive the maximum date (inclusive)
  * @return the extracted patient ids
  */
 protected static Set<Integer> datesWithinRange(
     CalculationResultMap results, Date minDateInclusive, Date maxDateInclusive) {
   Set<Integer> ret = new HashSet<Integer>();
   for (Map.Entry<Integer, CalculationResult> e : results.entrySet()) {
     Date result = null;
     try {
       result = e.getValue().asType(Date.class);
     } catch (Exception ex) {
       // pass
     }
     if (result != null) {
       if (OpenmrsUtil.compareWithNullAsEarliest(result, minDateInclusive) >= 0
           && OpenmrsUtil.compareWithNullAsLatest(result, maxDateInclusive) <= 0) {
         ret.add(e.getKey());
       }
     }
   }
   return ret;
 }