/* get_grid_item: updates the grid_item with fresh data */ void get_grid_item(void) { int x, y; scew_attribute *a = NULL; scew_element *e = NULL; scew_element *sub_e = NULL; scew_element *r = NULL; POINT p; if (!maptree) return; p = get_mouse_pos(); x = p.x - (p.x % TILE_W); y = p.y - (p.y % TILE_H) + map_offset; r = scew_tree_root(maptree); if (mode == TILE_MODE) { e = scew_element_by_name(r, "Tiles"); } else if (mode == SPRITE_MODE) { e = scew_element_by_name(r, "Sprites"); } else return; sub_e = scew_element_next(e, sub_e); while (sub_e) { a = scew_attribute_by_name(sub_e, "x"); if (x == atoi(scew_attribute_value(a))) { a = scew_attribute_by_name(sub_e, "y"); if (y == atoi(scew_attribute_value(a))) { grid_item.x = x; grid_item.y = y; grid_item.mode = mode; a = scew_attribute_by_name(sub_e, "name"); strncpy(grid_item.name, scew_attribute_value(a), NAME_LN); if (mode == TILE_MODE) { a = scew_attribute_by_name(sub_e, "passable"); grid_item.passable = atoi(scew_attribute_value(a)); } else { grid_item.passable = -1; } break; } } sub_e = scew_element_next(e, sub_e); } }
/******************************************************************************* * Function: Element_By_Name * * This function returns a void * representing a pointer to a valid * scew_element structure, which is a child element of the _element parameter * named the same as the _name parameter. * * Parameters: * _element: void * which represents a pointer to a scew_element. This * pointer must have been created previously by some other function * in this library. * _name: A pointer to a NULL terminated charater array representing the * name of the element to search for as a child of the _element * parameter. * * Returns: * void *: Representing a pointer to a valid scew_element structure, which * meets the constraints of the input parameters, or NULL on error. * ******************************************************************************/ DLLIMPORT void *Element_By_Name( void *_element, char *_name) { void * ret_val = NULL; if (( _element != NULL ) && ( _name != NULL )) { ret_val = scew_element_by_name( (scew_element *)_element, _name ); } return ret_val; }
void scew_element_del_by_name(scew_element* element, XML_Char const* name) { scew_element_free(scew_element_by_name(element, name)); }
m->to_string = to_string; m->printf = local_printf; m->fprintf = local_fprintf; m->free = local_free; /* Make sure the right XML subtree was sent. */ g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0); #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setting production types"); #endif local_data->production_types = herds->production_type_names; local_data->production_type = naadsm_read_prodtype_attribute (params, "production-type", herds->production_type_names); e = scew_element_by_name (params, "latent-period"); g_assert (e != NULL); local_data->latent_period = PAR_get_PDF (e); e = scew_element_by_name (params, "infectious-subclinical-period"); g_assert (e != NULL); local_data->infectious_subclinical_period = PAR_get_PDF (e); e = scew_element_by_name (params, "infectious-clinical-period"); g_assert (e != NULL); local_data->infectious_clinical_period = PAR_get_PDF (e); e = scew_element_by_name (params, "immunity-period"); g_assert (e != NULL); local_data->immunity_period = PAR_get_PDF (e);
/** * Adds a set of parameters to a contact spread model. */ void set_params (struct naadsm_model_t_ *self, PAR_parameter_t * params) { local_data_t *local_data = (local_data_t *) (self->model_data); unsigned int nzones = ZON_zone_list_length (local_data->zones); unsigned int nprod_types = local_data->production_types->len; scew_element *e, **ee; gboolean success; gboolean has_destruction_cost_params = FALSE; gboolean has_vaccination_cost_params = FALSE; gboolean has_surveillance_cost_param = FALSE; destruction_cost_data_t destruction_cost_params = {}; vaccination_cost_data_t vaccination_cost_params = {}; double surveillance_cost_param = 0; gboolean *production_type = NULL; gboolean *zone = NULL; unsigned int i, j; unsigned int noutputs; RPT_reporting_t *output; const XML_Char *variable_name; #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER set_params (%s)", MODEL_NAME); #endif /* Make sure the right XML subtree was sent. */ g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0); #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setting production types"); #endif /* Destruction Cost Parameters */ e = scew_element_by_name (params, "appraisal"); if (e != NULL) { destruction_cost_params.appraisal = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-unit appraisal cost to 0", MODEL_NAME); destruction_cost_params.appraisal = 0; } } else { g_warning ("%s: per-unit appraisal cost missing, setting to 0", MODEL_NAME); destruction_cost_params.appraisal = 0; } e = scew_element_by_name (params, "euthanasia"); if (e != NULL) { destruction_cost_params.euthanasia = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-animal euthanasia cost to 0", MODEL_NAME); destruction_cost_params.euthanasia = 0; } } else { g_warning ("%s: per-animal euthanasia cost missing, setting to 0", MODEL_NAME); destruction_cost_params.euthanasia = 0; } e = scew_element_by_name (params, "indemnification"); if (e != NULL) { destruction_cost_params.indemnification = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-animal indemnification cost to 0", MODEL_NAME); destruction_cost_params.indemnification = 0; } } else { g_warning ("%s: per-animal indemnification cost missing, setting to 0", MODEL_NAME); destruction_cost_params.indemnification = 0; } e = scew_element_by_name (params, "carcass-disposal"); if (e != NULL) { destruction_cost_params.carcass_disposal = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-animal carcass disposal cost to 0", MODEL_NAME); destruction_cost_params.carcass_disposal = 0; } } else { g_warning ("%s: per-animal carcass disposal cost missing, setting to 0", MODEL_NAME); destruction_cost_params.carcass_disposal = 0; } e = scew_element_by_name (params, "cleaning-disinfecting"); if (e != NULL) { destruction_cost_params.cleaning_disinfecting = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-unit cleaning and disinfecting cost to 0", MODEL_NAME); destruction_cost_params.cleaning_disinfecting = 0; } } else { g_warning ("%s: per-unit cleaning and disinfecting cost missing, setting to 0", MODEL_NAME); destruction_cost_params.cleaning_disinfecting = 0; } /* Vaccination Cost Parameters */ e = scew_element_by_name (params, "vaccination-fixed"); if (e != NULL) { vaccination_cost_params.vaccination_fixed = PAR_get_money (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting per-unit vaccination cost to 0", MODEL_NAME); vaccination_cost_params.vaccination_fixed = 0; } } else { g_warning ("%s: per-unit vaccination cost missing, setting to 0", MODEL_NAME); vaccination_cost_params.vaccination_fixed = 0; } e = scew_element_by_name (params, "vaccination"); if (e != NULL) { vaccination_cost_params.vaccination = PAR_get_money (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting per-animal vaccination cost to 0", MODEL_NAME); vaccination_cost_params.vaccination = 0; } } else { g_warning ("%s: per-animal vaccination cost missing, setting to 0", MODEL_NAME); vaccination_cost_params.vaccination = 0; } e = scew_element_by_name (params, "baseline-vaccination-capacity"); if (e != NULL) { vaccination_cost_params.baseline_capacity = (unsigned int) PAR_get_unitless (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting baseline vaccination capacity to 1,000,000", MODEL_NAME); vaccination_cost_params.baseline_capacity = 1000000; } } else { g_warning ("%s: baseline vaccination capacity missing, setting to 1,000,000", MODEL_NAME); vaccination_cost_params.baseline_capacity = 1000000; } e = scew_element_by_name (params, "additional-vaccination"); if (e != NULL) { vaccination_cost_params.extra_vaccination = PAR_get_money (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting additional per-animal vaccination cost to 0", MODEL_NAME); vaccination_cost_params.extra_vaccination = 0; } } else { g_warning ("%s: additional per-animal vaccination cost missing, setting to 0", MODEL_NAME); vaccination_cost_params.extra_vaccination = 0; } /* No vaccinations have been performed yet. */ vaccination_cost_params.capacity_used = 0; /* Surveillance Cost Parameters */ e = scew_element_by_name (params, "surveillance"); if (e != NULL) { surveillance_cost_param = PAR_get_money (e, &success); if (success) { has_surveillance_cost_param = TRUE; } else { g_warning ("%s: setting per-animal zone surveillance cost to 0", MODEL_NAME); surveillance_cost_param = 0; } } else { g_warning ("%s: per-animal zone surveillance cost missing, setting to 0", MODEL_NAME); surveillance_cost_param = 0; } /* Set the reporting frequency for the output variables. */ ee = scew_element_list (params, "output", &noutputs); #if DEBUG g_debug ("%i output variables", noutputs); #endif for (i = 0; i < noutputs; i++) { e = ee[i]; variable_name = scew_element_contents (scew_element_by_name (e, "variable-name")); /* Do the outputs include a variable with this name? */ for (j = 0; j < self->outputs->len; j++) { output = (RPT_reporting_t *) g_ptr_array_index (self->outputs, j); if (strcmp (output->name, variable_name) == 0) break; } if (j == self->outputs->len) g_warning ("no output variable named \"%s\", ignoring", variable_name); else { RPT_reporting_set_frequency (output, RPT_string_to_frequency (scew_element_contents (scew_element_by_name (e, "frequency")))); #if DEBUG g_debug ("report \"%s\" %s", variable_name, RPT_frequency_name[output->frequency]); #endif } } free (ee); /* Read zone and production type attributes to determine which * entries should be filled in. */ if (scew_attribute_by_name (params, "zone") != NULL) zone = naadsm_read_zone_attribute (params, local_data->zones); else zone = NULL; production_type = naadsm_read_prodtype_attribute (params, "production-type", local_data->production_types); /* Copy the parameters into the selected zone and production types. */ for (i = 0; i < nprod_types; i++) { if (production_type[i]) { if (has_destruction_cost_params) { if (local_data->destruction_cost_params == NULL) { local_data->destruction_cost_params = g_new0 (destruction_cost_data_t *, nprod_types); g_assert (local_data->destruction_cost_params != NULL); } if (local_data->destruction_cost_params[i] == NULL) { local_data->destruction_cost_params[i] = g_new0 (destruction_cost_data_t, 1); g_assert (local_data->destruction_cost_params[i] != NULL); } memcpy (local_data->destruction_cost_params[i], &destruction_cost_params, sizeof (destruction_cost_data_t)); } if (has_vaccination_cost_params) { if (local_data->vaccination_cost_params == NULL) { local_data->vaccination_cost_params = g_new0 (vaccination_cost_data_t *, nprod_types); g_assert (local_data->vaccination_cost_params != NULL); } if (local_data->vaccination_cost_params[i] == NULL) { local_data->vaccination_cost_params[i] = g_new0 (vaccination_cost_data_t, 1); g_assert (local_data->vaccination_cost_params[i] != NULL); } memcpy (local_data->vaccination_cost_params[i], &vaccination_cost_params, sizeof (vaccination_cost_data_t)); } if (has_surveillance_cost_param) { if (zone) { for (j = 0; j < nzones; j++) { if (zone[j]) { if (local_data->surveillance_cost_param == NULL) { local_data->surveillance_cost_param = g_new0 (double *, nzones); g_assert (local_data->surveillance_cost_param != NULL); } if (local_data->surveillance_cost_param[j] == NULL) { local_data->surveillance_cost_param[j] = g_new0 (double, nprod_types); g_assert (local_data->surveillance_cost_param[j] != NULL); } local_data->surveillance_cost_param[j][i] = surveillance_cost_param; } } }
m->to_string = to_string; m->printf = local_printf; m->fprintf = local_fprintf; m->free = local_free; /* Make sure the right XML subtree was sent. */ g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0); #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setting production types"); #endif local_data->production_types = herds->production_type_names; local_data->production_type = ergadm_read_prodtype_attribute (params, "production-type", herds->production_type_names); e = scew_element_by_name (params, "appraisal"); if (e != NULL) { local_data->appraisal = PAR_get_money (e, &success); if (success == FALSE) { g_warning ("%s: setting per-unit appraisal cost to 0", MODEL_NAME); local_data->appraisal = 0; } } else { g_warning ("%s: per-unit appraisal cost missing, setting to 0", MODEL_NAME); local_data->appraisal = 0; }
m->to_string = to_string; m->printf = local_printf; m->fprintf = local_fprintf; m->free = local_free; /* Make sure the right XML subtree was sent. */ g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0); #if DEBUG g_debug ("setting production types"); #endif local_data->production_types = herds->production_type_names; local_data->production_type = naadsm_read_prodtype_attribute (params, "production-type", herds->production_type_names); e = scew_element_by_name (params, "priority"); if (e != NULL) { local_data->priority = (int) round (PAR_get_unitless (e, &success)); if (success == FALSE) { g_warning ("%s: setting priority to 1", MODEL_NAME); local_data->priority = 1; } if (local_data->priority < 1) { g_warning ("%s: priority cannot be less than 1, setting to 1", MODEL_NAME); local_data->priority = 1; } } else
/** * Adds a set of parameters to an airborne spread model. */ void set_params (struct ergadm_model_t_ *self, scew_element * params) { local_data_t *local_data; gboolean *from_production_type, *to_production_type; unsigned int nprod_types, i, j; param_block_t *param_block; scew_element const *e; gboolean success; gboolean use_rtree; #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER set_params (%s)", MODEL_NAME); #endif /* Make sure the right XML subtree was sent. */ g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0); local_data = (local_data_t *) (self->model_data); /* Find out which to-from production type combinations these parameters apply * to. */ from_production_type = ergadm_read_prodtype_attribute (params, "from-production-type", local_data->production_types); to_production_type = ergadm_read_prodtype_attribute (params, "to-production-type", local_data->production_types); nprod_types = local_data->production_types->len; for (i = 0; i < nprod_types; i++) if (from_production_type[i] == TRUE) for (j = 0; j < nprod_types; j++) if (to_production_type[j] == TRUE) { /* If necessary, create a row in the 2D array for this from- * production type. */ if (local_data->param_block[i] == NULL) local_data->param_block[i] = g_new0 (param_block_t *, nprod_types); /* Create a parameter block for this to-from production type * combination, or overwrite the existing one. */ param_block = local_data->param_block[i][j]; if (param_block == NULL) { param_block = g_new (param_block_t, 1); local_data->param_block[i][j] = param_block; #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setting parameters for %s -> %s", (char *) g_ptr_array_index (local_data->production_types, i), (char *) g_ptr_array_index (local_data->production_types, j)); #endif } else { g_warning ("overwriting previous parameters for %s -> %s", (char *) g_ptr_array_index (local_data->production_types, i), (char *) g_ptr_array_index (local_data->production_types, j)); } e = scew_element_by_name (params, "prob-spread-1km"); param_block->prob_spread_1km = PAR_get_probability (e, &success); if (success == FALSE) { g_warning ("setting probability of spread at 1 km to 0"); param_block->prob_spread_1km = 0; } e = scew_element_by_name (params, "wind-direction-start"); param_block->wind_dir_start = PAR_get_angle (e, &success); if (success == FALSE) { g_warning ("setting start of wind direction range to 0"); param_block->wind_dir_start = 0; } /* Force the angle into the range [0,360). */ while (param_block->wind_dir_start < 0) param_block->wind_dir_start += 360; while (param_block->wind_dir_start >= 360) param_block->wind_dir_start -= 360; e = scew_element_by_name (params, "wind-direction-end"); param_block->wind_dir_end = PAR_get_angle (e, &success); if (success == FALSE) { g_warning ("setting end of wind direction range to 360"); param_block->wind_dir_end = 360; } /* Force the angle into the range [0,360]. */ while (param_block->wind_dir_end < 0) param_block->wind_dir_end += 360; while (param_block->wind_dir_end > 360) param_block->wind_dir_end -= 360; /* Note that start > end is allowed. See the header comment. */ param_block->wind_range_crosses_0 = (param_block->wind_dir_start > param_block->wind_dir_end); /* Setting both start and end to 0 seems like a sensible way to * turn off airborne spread, but headings close to north will * "sneak through" this restriction because we allow a little * wiggle room for rounding errors. If the user tries this, * instead set prob-spread-1km=0. */ if (param_block->wind_dir_start == 0 && param_block->wind_dir_end == 0) param_block->prob_spread_1km = 0; e = scew_element_by_name (params, "max-spread"); param_block->max_spread = PAR_get_length (e, &success); if (success == FALSE) { g_warning ("setting maximum distance of spread to 0"); param_block->max_spread = 0; } /* The maximum spread distance cannot be negative. */ if (param_block->max_spread < 0) { g_warning ("maximum distance of spread cannot be negative, setting to 0"); param_block->max_spread = 0; } /* Setting the maximum spread distance to 0 seems like a sensible * way to turn off airborne spread, but max-spread <= 1 doesn't * make sense in the formula. If the user tries this, instead set * max-spread=2, prob-spread-1km=0. */ if (param_block->max_spread <= 1) { param_block->max_spread = 2; param_block->prob_spread_1km = 0; } e = scew_element_by_name (params, "delay"); if (e != NULL) { param_block->delay = PAR_get_PDF (e); } else param_block->delay = PDF_new_point_dist (0); /* Keep track of the maximum distance of spread from each * production type. This determines whether we will use the R-tree * index when looking for herds to spread infection to. */ if (param_block->max_spread > local_data->max_spread[i]) { local_data->max_spread[i] = param_block->max_spread; /* Use the following heuristic to decide whether to use the * R-tree index in searches: if the ratio of the diameter of * circle of maximum spread to the short axis of the oriented * bounding rectangle around the herds is <= 0.25, use the * R-tree. */ if (local_data->short_axis_length > 0) { use_rtree = (param_block->max_spread * 2 / local_data->short_axis_length) <= 0.25; } else { use_rtree = FALSE; } #if defined(USE_RTREE) && USE_RTREE == 0 /* For debugging purposes, you can #define USE_RTREE to 0 to never * use the spatial index, or 1 to always use it. */ use_rtree = FALSE; #endif local_data->use_rtree_index[i] = use_rtree; } } g_free (from_production_type); g_free (to_production_type); #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- EXIT set_params (%s)", MODEL_NAME); #endif return; }
/** * @brief Reads the paths data from the XML data. * This function reads the paths data inside given element in XML data. * @param [in] parent Parent element for the paths data. * @return TRUE if pathdata was found from the file. */ BOOL ProjectFile::GetPathsData(scew_element * parent) { BOOL bFoundPaths = FALSE; scew_element *paths = NULL; if (parent != NULL) { paths = scew_element_by_name(parent, Paths_element_name); } if (paths != NULL) { bFoundPaths = TRUE; scew_element *left = NULL; scew_element *right = NULL; scew_element *filter = NULL; scew_element *subfolders = NULL; scew_element *left_ro = NULL; scew_element *right_ro = NULL; left = scew_element_by_name(paths, Left_element_name); right = scew_element_by_name(paths, Right_element_name); filter = scew_element_by_name(paths, Filter_element_name); subfolders = scew_element_by_name(paths, Subfolders_element_name); left_ro = scew_element_by_name(paths, Left_ro_element_name); right_ro = scew_element_by_name(paths, Right_ro_element_name); if (left) { LPCSTR path = NULL; path = scew_element_contents(left); m_leftFile = UTF82T(path).c_str(); m_bHasLeft = TRUE; } if (right) { LPCSTR path = NULL; path = scew_element_contents(right); m_rightFile = UTF82T(path).c_str(); m_bHasRight = TRUE; } if (filter) { LPCSTR filtername = NULL; filtername = scew_element_contents(filter); m_filter = UTF82T(filtername).c_str(); m_bHasFilter = TRUE; } if (subfolders) { LPCSTR folders = NULL; folders = scew_element_contents(subfolders); m_subfolders = atoi(folders); m_bHasSubfolders = TRUE; } if (left_ro) { LPCSTR readonly = NULL; readonly = scew_element_contents(left_ro); m_bLeftReadOnly = (atoi(readonly) != 0); } if (right_ro) { LPCSTR readonly = NULL; readonly = scew_element_contents(right_ro); m_bRightReadOnly = (atoi(readonly) != 0); } } return bFoundPaths; }