示例#1
0
/**
 * Responds to a commitment to vaccinate event by recording the herd's status
 * as "waiting".
 *
 * @param self the model.
 * @param event a commitment to vaccinate event.
 */
void
handle_commitment_to_vaccinate_event (struct ergadm_model_t_ *self,
                                      EVT_commitment_to_vaccinate_event_t * event)
{
  local_data_t *local_data;
  HRD_herd_t *herd;
  unsigned int nherds, nanimals;

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
         "----- ENTER handle_commitment_to_vaccinate_event (%s)", MODEL_NAME);
#endif

  local_data = (local_data_t *) (self->model_data);
  herd = event->herd;

  if (local_data->status[herd->index] == NOT_ON_WAITING_LIST)
    {
      local_data->status[herd->index] = event->day;

      /* Increment the count of herds awaiting vaccination. */
      RPT_reporting_add_integer (local_data->nherds_awaiting_vaccination, 1, NULL);
      if (local_data->nherds_awaiting_vaccination_by_prodtype->frequency != RPT_never)
        RPT_reporting_add_integer1 (local_data->nherds_awaiting_vaccination_by_prodtype, 1,
                                    herd->production_type_name);
      nherds = RPT_reporting_get_integer (local_data->nherds_awaiting_vaccination, NULL);
      if (nherds > local_data->peak_nherds)
        {
          local_data->peak_nherds = nherds;
          RPT_reporting_set_integer (local_data->peak_nherds_awaiting_vaccination, nherds, NULL);
        }

      /* Increment the count of animals awaiting vaccination. */
      RPT_reporting_add_integer (local_data->nanimals_awaiting_vaccination, herd->size, NULL);
      if (local_data->nanimals_awaiting_vaccination_by_prodtype->frequency != RPT_never)
        RPT_reporting_add_integer1 (local_data->nanimals_awaiting_vaccination_by_prodtype,
                                    herd->size, herd->production_type_name);
      nanimals = RPT_reporting_get_integer (local_data->nanimals_awaiting_vaccination, NULL);
      if (nanimals > local_data->peak_nanimals)
        {
          local_data->peak_nanimals = nanimals;
          RPT_reporting_set_integer (local_data->peak_nanimals_awaiting_vaccination, nanimals,
                                     NULL);
        }
    }

#if DEBUG
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
         "----- EXIT handle_commitment_to_vaccinate_event (%s)", MODEL_NAME);
#endif
}
示例#2
0
/**
 * Records the day of the first detection.
 *
 * @param self the model.
 * @param event a detection event.
 */
void
handle_detection_event (struct ergadm_model_t_ *self, EVT_detection_event_t * event)
{
    local_data_t *local_data;
    HRD_herd_t *herd;
    char *peek;
    gboolean first;
    HRD_update_t update;

#if DEBUG
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER handle_detection_event (%s)", MODEL_NAME);
#endif

    local_data = (local_data_t *) (self->model_data);
    herd = event->herd;

    peek = RPT_reporting_get_text (local_data->detections, NULL);
    first = (peek == NULL) || (strlen (peek) == 0);

    g_string_printf (local_data->target, first ? "%u" : ",%u", herd->index);
    RPT_reporting_append_text (local_data->detections, local_data->target->str, NULL);

    if (NULL != guilib_detect_herd)
    {
        update.index = herd->index;
        update.success = 2;       /* Unused */
        guilib_detect_herd (update);
    }
    /*
       #if ARDEBUG
       printf( "Successful detection: herd index %d\n", herd->index );
       #endif
     */

    if (local_data->first_detection)
    {
        RPT_reporting_set_integer (local_data->day_1st_detection, event->day, NULL);
        local_data->first_detection = FALSE;
    }
    RPT_reporting_add_integer (local_data->nherds_detected, 1, NULL);
    RPT_reporting_add_integer1 (local_data->nherds_detected_by_prodtype, 1,
                                herd->production_type_name);
    RPT_reporting_add_integer (local_data->cumul_nherds_detected, 1, NULL);
    RPT_reporting_add_integer1 (local_data->cumul_nherds_detected_by_prodtype, 1,
                                herd->production_type_name);

#if DEBUG
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- EXIT handle_detection_event (%s)", MODEL_NAME);
#endif
}
示例#3
0
/**
 * Responds to a new day event by updating the reporting variables.
 *
 * @param self the model.
 * @param herds a list of herds.
 * @param zones a list of zones.
 * @param event a new day event.
 */
void
handle_new_day_event (struct ergadm_model_t_ *self, HRD_herd_list_t * herds,
                      ZON_zone_list_t * zones, EVT_new_day_event_t * event)
{
    local_data_t *local_data;
    gboolean shape_due, area_due, num_areas_due, num_units_due;
    int i;
    ZON_zone_t *zone, *next_smaller_zone;
    GString *s;
    double area;
    unsigned int nherds;
    HRD_herd_t *herd;
    char *drill_down_list[3] = { NULL, NULL, NULL };

#if DEBUG
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER handle_new_day_event (%s)", MODEL_NAME);
#endif

    local_data = (local_data_t *) (self->model_data);

    shape_due = RPT_reporting_due (local_data->shape, event->day);
    area_due = RPT_reporting_due (local_data->area, event->day);
    num_areas_due = RPT_reporting_due (local_data->num_separate_areas, event->day);
    num_units_due = RPT_reporting_due (local_data->num_units, event->day);

    for (i = 0; i < local_data->nzones; i++)
    {
        zone = ZON_zone_list_get (zones, i);

        if (shape_due)
        {
            s = polygon_to_wkt (zone->poly);
            RPT_reporting_set_text1 (local_data->shape, s->str, zone->name);
            /* The string was copied so it can be freed. */
            g_string_free (s, TRUE);
        }

        if (area_due)
        {
            area = ZON_update_area (zone);
            RPT_reporting_set_real1 (local_data->area, area, zone->name);
        }

        if (num_areas_due)
            RPT_reporting_set_integer1 (local_data->num_separate_areas,
                                        zone->poly->num_contours, zone->name);
    }

    /* In the loop above, the area of each zone polygon was computed.  But since
     * zones are nested inside of each other, that's not exactly what we want:
     * we want the area displayed for an "outer" zone to exclude the area of the
     * smaller "inner" zones.  So we do that computation here. */
    if (area_due)
    {
        /* Start with the next-to-last zone, because the last one is the
         * "background" zone. */
        for (i = local_data->nzones - 2; i > 0; i--)
        {
            zone = ZON_zone_list_get (zones, i);
            next_smaller_zone = ZON_zone_list_get (zones, i - 1);
            zone->area -= next_smaller_zone->area;
            RPT_reporting_set_real1 (local_data->area, zone->area, zone->name);

            if (NULL != guilib_record_zone_area)
                guilib_record_zone_area (zone->level, zone->area);
        }

        /* Don't forget to report the smallest zone to the GUI! */
        if (NULL != guilib_record_zone_area)
        {
            zone = ZON_zone_list_get (zones, 0);
            guilib_record_zone_area (zone->level, zone->area);
        }
    }

    if (local_data->num_animal_days_by_prodtype->frequency != RPT_never || num_units_due)
    {
        nherds = zones->membership_length;
        for (i = 0; i < local_data->nzones; i++)
        {
            zone = ZON_zone_list_get (zones, i);
            RPT_reporting_set_integer1 (local_data->num_units, 0, zone->name);
        }
        for (i = 0; i < nherds; i++)
        {
            zone = zones->membership[i]->parent;
            RPT_reporting_add_integer1 (local_data->num_units, 1, zone->name);
            herd = HRD_herd_list_get (herds, i);
            if (herd->status != Destroyed)
            {
                drill_down_list[0] = herd->production_type_name;
                drill_down_list[1] = zone->name;
                RPT_reporting_add_integer (local_data->num_animal_days_by_prodtype, herd->size,
                                           drill_down_list);
            }
        }
    }

#if DEBUG
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- EXIT handle_new_day_event (%s)", MODEL_NAME);
#endif
}
示例#4
0
/**
 * Responds to a last day event by computing output variables that are only
 * needed on the last day.
 *
 * @param self the model.
 * @param zones a list of zones.
 * @param event a last day event.
 */
void
handle_last_day_event (struct ergadm_model_t_ *self, ZON_zone_list_t * zones,
                       EVT_new_day_event_t * event)
{
    local_data_t *local_data;
    gboolean skip_shape, skip_area, skip_num_areas, skip_num_units;
    int i;
    ZON_zone_t *zone, *next_smaller_zone;
    GString *s;
    double area;

#if DEBUG
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER handle_last_day_event (%s)", MODEL_NAME);
#endif

    local_data = (local_data_t *) (self->model_data);

    /* Some of the output variables are computationally intensive.  If they're
     * reported "never", or if they were already computed today by
     * handle_new_day_event, don't bother to compute them. */
    skip_shape = local_data->shape->frequency == RPT_never
                 || RPT_reporting_due (local_data->shape, event->day);
    skip_area = local_data->area->frequency == RPT_never
                || RPT_reporting_due (local_data->area, event->day);
    skip_num_areas = local_data->num_separate_areas->frequency == RPT_never
                     || RPT_reporting_due (local_data->num_separate_areas, event->day);
    skip_num_units = local_data->num_units->frequency == RPT_never
                     || RPT_reporting_due (local_data->num_units, event->day);

    /* We don't have to worry about num_animal_days_by_prodtype because, unless
     * it's set to be reported "never", it will be updated every day by
     * handle_new_day_event. */

    for (i = 0; i < local_data->nzones; i++)
    {
        zone = ZON_zone_list_get (zones, i);

        if (!skip_shape)
        {
            s = polygon_to_wkt (zone->poly);
            RPT_reporting_set_text1 (local_data->shape, s->str, zone->name);
            /* The string was copied so it can be freed. */
            g_string_free (s, TRUE);
        }

        if (!skip_area)
        {
            area = ZON_update_area (zone);
            RPT_reporting_set_real1 (local_data->area, area, zone->name);
        }

        if (!skip_num_areas)
            RPT_reporting_set_integer1 (local_data->num_separate_areas,
                                        zone->poly->num_contours, zone->name);
    }

    if (!skip_area)
    {
        /* Start with the next-to-last zone, because the last one is the
         * "background" zone. */
        for (i = local_data->nzones - 2; i > 0; i--)
        {
            zone = ZON_zone_list_get (zones, i);
            next_smaller_zone = ZON_zone_list_get (zones, i - 1);
            zone->area -= next_smaller_zone->area;
            RPT_reporting_set_real1 (local_data->area, zone->area, zone->name);
        }
    }

    if (!skip_num_units)
    {
        for (i = 0; i < local_data->nzones; i++)
        {
            zone = ZON_zone_list_get (zones, i);
            RPT_reporting_set_integer1 (local_data->num_units, 0, zone->name);
        }
        for (i = 0; i < zones->membership_length; i++)
        {
            zone = zones->membership[i]->parent;
            RPT_reporting_add_integer1 (local_data->num_units, 1, zone->name);
        }
    }

#if DEBUG
    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- EXIT handle_last_day_event (%s)", MODEL_NAME);
#endif
}
/**
 * Responds to an exposure event by recording it.
 *
 * @param self the model.
 * @param event an exposure event.
 */
void
handle_exposure_event (struct naadsm_model_t_ *self, EVT_exposure_event_t * event)
{
  local_data_t *local_data;
  HRD_herd_t *exposing_herd, *exposed_herd;
  const char *cause;
  char *peek;
  gboolean first_of_cause;
  const char *drill_down_list[3] = { NULL, NULL, NULL };
  HRD_expose_t update;
  
#if DEBUG
  g_debug ("----- ENTER handle_exposure_event (%s)", MODEL_NAME);
#endif

  local_data = (local_data_t *) (self->model_data);
  exposing_herd = event->exposing_herd;
  exposed_herd = event->exposed_herd;

  /* Update the text string that lists exposed herd indices. */
  cause = NAADSM_contact_type_abbrev[event->contact_type];
  peek = RPT_reporting_get_text1 (local_data->exposures, cause);
  first_of_cause = (peek == NULL) || (strlen (peek) == 0);

  g_string_printf (local_data->source_and_target,
                   first_of_cause ? "%u->%u" : ",%u->%u",
                   event->exposing_herd->index, event->exposed_herd->index);
  RPT_reporting_append_text1 (local_data->exposures, local_data->source_and_target->str,
                              cause);
                                
  update.src_index = exposing_herd->index;
  update.src_status = (NAADSM_disease_state) exposing_herd->status;
  update.dest_index = exposed_herd->index;
  update.dest_status = (NAADSM_disease_state) exposed_herd->status;
  
  update.initiated_day = (int) event->initiated_day;
  update.finalized_day = (int) event->initiated_day + event->delay;
  
  if( TRUE == event->adequate )
    update.is_adequate = NAADSM_SuccessTrue;
  else
    update.is_adequate = NAADSM_SuccessFalse;
  
  switch (event->contact_type)
    {
      case NAADSM_DirectContact:
      case NAADSM_IndirectContact:
      case NAADSM_AirborneSpread:
        update.exposure_method = event->contact_type;
        break;      
      default:
        /* If this condition occurs, someone forgot something. */
        g_error( "An unrecognized exposure mechanism (%s) occurred in handle_exposure_event", cause );
        update.exposure_method = 0;     
    }

      
#ifdef USE_SC_GUILIB
  sc_expose_herd( exposed_herd, update );
#else	  
  if (NULL != naadsm_expose_herd)
    {
      naadsm_expose_herd (update);
    }
#endif  

#if UNDEFINED
  printf ("Herd at index %d exposed by method %s\n", event->exposed_herd->index, cause);
#endif

  /* Update the counts of exposures. */
  RPT_reporting_add_integer  (local_data->num_units_exposed, 1, NULL);
  RPT_reporting_add_integer1 (local_data->num_units_exposed_by_cause, 1, cause);
  RPT_reporting_add_integer1 (local_data->num_units_exposed_by_prodtype, 1, exposed_herd->production_type_name);
  RPT_reporting_add_integer  (local_data->num_animals_exposed, exposed_herd->size, NULL);
  RPT_reporting_add_integer1 (local_data->num_animals_exposed_by_cause, exposed_herd->size, cause);
  RPT_reporting_add_integer1 (local_data->num_animals_exposed_by_prodtype, exposed_herd->size, exposed_herd->production_type_name);
  RPT_reporting_add_integer  (local_data->cumul_num_units_exposed, 1, NULL);
  RPT_reporting_add_integer1 (local_data->cumul_num_units_exposed_by_cause, 1, cause);
  RPT_reporting_add_integer1 (local_data->cumul_num_units_exposed_by_prodtype, 1, exposed_herd->production_type_name);
  RPT_reporting_add_integer  (local_data->cumul_num_animals_exposed, exposed_herd->size, NULL);
  RPT_reporting_add_integer1 (local_data->cumul_num_animals_exposed_by_cause, exposed_herd->size,
                              cause);
  RPT_reporting_add_integer1 (local_data->cumul_num_animals_exposed_by_prodtype, exposed_herd->size,
                              exposed_herd->production_type_name);
  drill_down_list[0] = cause;
  drill_down_list[1] = exposed_herd->production_type_name;
  if (local_data->num_units_exposed_by_cause_and_prodtype->frequency != RPT_never)
    RPT_reporting_add_integer (local_data->num_units_exposed_by_cause_and_prodtype, 1, drill_down_list);
  if (local_data->num_animals_exposed_by_cause_and_prodtype->frequency != RPT_never)
    RPT_reporting_add_integer (local_data->num_animals_exposed_by_cause_and_prodtype, exposed_herd->size,
                               drill_down_list);
  if (local_data->cumul_num_units_exposed_by_cause_and_prodtype->frequency != RPT_never)
    RPT_reporting_add_integer (local_data->cumul_num_units_exposed_by_cause_and_prodtype, 1, drill_down_list);
  if (local_data->cumul_num_animals_exposed_by_cause_and_prodtype->frequency != RPT_never)
    RPT_reporting_add_integer (local_data->cumul_num_animals_exposed_by_cause_and_prodtype,
                               exposed_herd->size, drill_down_list);
  if (event->adequate)
    {
      RPT_reporting_add_integer (local_data->num_adequate_exposures, 1, NULL);
      RPT_reporting_add_integer (local_data->cumul_num_adequate_exposures, 1, NULL);
    }

#if DEBUG
  g_debug ("----- EXIT handle_exposure_event (%s)", MODEL_NAME);
#endif
}