void element_print(scew_element const* element, FILE* out, unsigned int indent) { unsigned int closed = 0; XML_Char const* contents; scew_element* child = NULL; scew_attribute* attribute = NULL; if (element == NULL) { return; } indent_print(out, indent); scew_fprintf(out, _XT("<%s"), scew_element_name(element)); attribute = NULL; while ((attribute = scew_attribute_next(element, attribute)) != NULL) { attribute_print(attribute, out); } contents = scew_element_contents(element); if ((contents == NULL) && (element->child == NULL) && (element->parent != NULL)) { scew_fprintf(out, _XT("/>\n")); closed = 1; } else { scew_fprintf(out, _XT(">")); if (contents == NULL) { scew_fprintf(out, _XT("\n")); } } child = NULL; while ((child = scew_element_next(element, child)) != NULL) { element_print(child, out, indent + 1); } if (contents != NULL) { scew_fprintf(out, _XT("%s"), contents); } else if (!closed) { indent_print(out, indent); } if (!closed) { scew_fprintf(out, _XT("</%s>\n"), scew_element_name(element)); } }
static void print_element (scew_element *element, unsigned int indent) { XML_Char const *contents = NULL; scew_list *list = NULL; if (element == NULL) { return; } /* Prints the starting element tag with its attributes. */ print_indent (indent); scew_printf (_XT("<%s"), scew_element_name (element)); print_attributes (element); scew_printf (_XT(">")); contents = scew_element_contents (element); if (contents == NULL) { scew_printf (_XT("\n")); } /** * Call print_element function again for each child of the current * element. */ list = scew_element_children (element); while (list != NULL) { scew_element *child = scew_list_data (list); print_element (child, indent + 1); list = scew_list_next (list); } /* Prints element's content. */ if (contents != NULL) { scew_printf (_XT("%s"), contents); } else { print_indent (indent); } /* Prints the closing element tag. */ scew_printf (_XT("</%s>\n"), scew_element_name (element)); }
/******************************************************************************* * TestParser * * This is a test function to verify that the library is properly opening and * reading xml files. ******************************************************************************/ DLLIMPORT void TestParser( char *Filename ) { scew_parser *parser; scew_element *params; int elementCount = 0; int i; char Buffer[1024]; parser = scew_parser_create (); if (parser != NULL) { scew_parser_load_file (parser, Filename); params = scew_tree_root (scew_parser_tree (parser)); elementCount = scew_element_count (params); sprintf(Buffer, "Root has %u children\n", elementCount); MessageBox( 0, Buffer, "Debugging", MB_ICONINFORMATION ); for (i = 0; i < elementCount; i++) { scew_element *newParams = scew_element_by_index (params, i); sprintf(Buffer, "This child has %u subchildren\n", scew_element_count( newParams )); MessageBox( 0, Buffer, "Debugging", MB_ICONINFORMATION ); sprintf(Buffer, "Child %i name='%s'\n", i, scew_element_name (scew_element_by_index (params, i))); MessageBox( 0, Buffer, "Debugging", MB_ICONINFORMATION ); } scew_parser_free( parser ); } }
/******************************************************************************* * Function: Element_Name * * This function returns a char * representing a pointer to the name of the * element specified in the input parameter, _element. * * Parameters: * _element: A void * representing a valid pointer to a scew_element * structure. This pointer must have been created previously by * some other function in this library. * * Returns: * char *: Representing a pointer to a NULL terminated character array, * which contains the name of the element given in the input * parameter, _element. A NULL value is returned on error. * ******************************************************************************/ DLLIMPORT char *Element_Name( void *_element ) { char *ret_val = NULL; if ( _element != NULL ) { ret_val = (char *)scew_element_name( (scew_element *)_element ); } return ret_val; }
/* load_sprite: load all the sprites in SPRITE_INDEX and their graphics */ void load_sprites(void) { size_t i, y; char bmpname[NAME_LN]; scew_tree *t = NULL; scew_parser *p = NULL; scew_element *e = NULL; scew_element *r = NULL; SPRITE *s = NULL; /* setup the parser */ p = scew_parser_create(); scew_parser_ignore_whitespaces(p, 1); if (!scew_parser_load_file(p, SPRITE_INDEX)) { printf("%s\n", scew_error_expat_string(scew_error_code())); return; } t = scew_parser_tree(p); r = scew_tree_root(t); s = create_sprite(); /* read all the sprites from SPRITE_INDEX */ /* parse the SPRITE_INDEX file and outline the list with their names */ e = scew_element_next(r, e); while (e) { strncpy(s->long_name, (char *) scew_element_name(e), LONG_NAME_LN); s->x = s->y = 0; sprite_list = append_sprite_list(s, sprite_list, &sprite_list_sz); e = scew_element_next(r, e); } /* load all their graphics and the sprite data */ gfx_list = complete_sprite_list(sprite_list, sprite_list_sz, gfx_list, &gfx_list_sz); /* create a list_element for each of them and put them in a list */ y = SPRITE_WIN.y; for (i = 0; i < sprite_list_sz; i++) { sprintf(bmpname, "%s0", sprite_list[i].name); spr_elem_lst = add_list_element(rec(SPRITE_WIN.x, y, LIST_ELEM_W, LIST_ELEM_H), 0, DEFAULT_COLOR, SEL_COLOR, sprite_list[i].name, search_gfx_list(bmpname, gfx_list, gfx_list_sz), spr_elem_lst, &spr_elem_lst_sz); y += LIST_ELEM_H; } /* cleanup */ scew_element_free(r); destroy_sprite(s); scew_element_free(e); scew_parser_free(p); }
/******************************************************************************* * Function: Element_Name_By_Index * * This function returns a char * representing a pointer to the name of the * element specified as the Index(th) child of the input parameter _element. * * Parameters: * _element: A void * representing a valid pointer to a scew_element * structure. This pointer must have been created previously by * some other function in this library. * Index: An integer value, zero based, representing the index of the * child element of _element. * * Returns: * char *: Representing a pointer to a NULL terminated character array, * which contains the name of the element established by the Index(th) * child element of _element. A NULL value is returned on error. * ******************************************************************************/ DLLIMPORT char *Element_Name_By_Index(void *_element, int Index) { char *ret_val = NULL; if (( _element != NULL ) && ( Index >=0 )) { scew_element *temp = scew_element_by_index((scew_element *)_element, Index); if ( temp != NULL ) ret_val = (char *)scew_element_name( temp ); }; return ret_val; }
/** * @brief Return project file XML's root element. * @param [in] tree XML tree we got from the parser. * @return Root element pointer. */ scew_element* ProjectFile::GetRootElement(scew_tree * tree) { scew_element * root = NULL; if (tree != NULL) { root = scew_tree_root(tree); } if (root != NULL) { // Make sure we have correct root element if (strcmp(Root_element_name, scew_element_name(root)) != 0) { root = NULL; } } return root; }
m->events_listened_for = events_listened_for; m->nevents_listened_for = NEVENTS_LISTENED_FOR; m->outputs = g_ptr_array_sized_new (6); m->model_data = local_data; m->run = run; m->reset = reset; m->is_listening_for = is_listening_for; m->has_pending_actions = has_pending_actions; m->has_pending_infections = has_pending_infections; 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); local_data->detections = RPT_new_reporting ("detections", NULL, RPT_text, RPT_never, FALSE); local_data->day_1st_detection = RPT_new_reporting ("time-to-first-detection", NULL, RPT_integer, RPT_never, TRUE); local_data->nherds_detected = RPT_new_reporting ("num-units-detected", NULL, RPT_integer, RPT_never, FALSE); local_data->nherds_detected_by_prodtype = RPT_new_reporting ("num-units-detected-by-production-type", NULL, RPT_group, RPT_never, FALSE); local_data->cumul_nherds_detected = RPT_new_reporting ("cumulative-num-units-detected", NULL, RPT_integer, RPT_never, TRUE); local_data->cumul_nherds_detected_by_prodtype = RPT_new_reporting ("cumulative-num-units-detected-by-production-type", NULL, RPT_group, RPT_never, TRUE); g_ptr_array_add (m->outputs, local_data->detections); g_ptr_array_add (m->outputs, local_data->day_1st_detection);
/** * 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; } } }
arcus_atlas *atlas_from_xml(scew_element *element) { arcus_atlas *atlas; scew_list *list, *i; int expected_transforms, ii, jj, kk; if (!element) { set_error_string("atlas_from_xml: NULL element"); return NULL; } if (strcmp(scew_element_name(element), "atlas") != 0) { set_error_string("atlas_from_xml: element name != 'atlas'"); return NULL; } atlas = atlas_create(); if (!atlas) return NULL; list = scew_element_list_by_name(element, "system"); if (!list) { set_error_string("atlas_from_xml: no systems in atlas"); atlas_destroy(atlas); return NULL; } for (i = list ; i ; i = scew_list_next(i)) { scew_element *e; arcus_system *s; e = (scew_element *)scew_list_data(i); s = system_from_xml(e); if (!s) { atlas_destroy(atlas); return NULL; } atlas->systems = (arcus_system **)realloc(atlas->systems, (atlas->num_systems + 1) * sizeof(arcus_system *)); atlas->systems[atlas->num_systems] = s; atlas->num_systems++; } // If there's only one system, ignore the transforms if (atlas->num_systems == 1) return atlas; // Figure out how many transforms we should have (n choose k, both directions) expected_transforms = binomial(atlas->num_systems, 2) * 2; // Load transforms list = scew_element_list_by_name(element, "transform"); if (!list) { set_error_string("atlas_from_xml: no transforms in atlas when expected"); atlas_destroy(atlas); return NULL; } if (scew_list_size(list) != expected_transforms) { set_error_string("atlas_from_xml: wrong number of transforms in atlas"); atlas_destroy(atlas); return NULL; } for (i = list ; i ; i = scew_list_next(i)) { scew_element *e; arcus_transform *t; e = (scew_element *)scew_list_data(i); t = transform_from_xml(e); if (!t) { atlas_destroy(atlas); return NULL; } atlas->transforms = (arcus_transform **)realloc(atlas->transforms, (atlas->num_transforms + 1) * sizeof(arcus_transform *)); atlas->transforms[atlas->num_transforms] = t; atlas->num_transforms++; } // Load differential transforms list = scew_element_list_by_name(element, "transform_diff"); if (!list) { set_error_string("atlas_from_xml: no differential transforms in atlas when expected"); atlas_destroy(atlas); return NULL; } if (scew_list_size(list) != expected_transforms) { set_error_string("atlas_from_xml: wrong number of differential transforms in atlas"); atlas_destroy(atlas); return NULL; } for (i = list ; i ; i = scew_list_next(i)) { scew_element *e; arcus_transform *t; e = (scew_element *)scew_list_data(i); t = transform_from_xml(e); if (!t) { atlas_destroy(atlas); return NULL; } atlas->diff_transforms = (arcus_transform **)realloc(atlas->diff_transforms, (atlas->num_diff_transforms + 1) * sizeof(arcus_transform *)); atlas->diff_transforms[atlas->num_diff_transforms] = t; atlas->num_diff_transforms++; } // Check the accuracy and completeness of the transform lists for (ii = 0 ; ii < expected_transforms ; ii++) { arcus_transform *t, *dt; int found_t_from = 0, found_t_to = 0; int found_dt_from = 0, found_dt_to = 0; t = atlas->transforms[ii]; dt = atlas->diff_transforms[ii]; for (jj = 0 ; jj < atlas->num_systems ; jj++) { if (strcmp(system_id(atlas->systems[jj]), transform_from(t)) == 0) found_t_from = 1; if (strcmp(system_id(atlas->systems[jj]), transform_from(dt)) == 0) found_dt_from = 1; if (strcmp(system_id(atlas->systems[jj]), transform_to(t)) == 0) found_t_to = 1; if (strcmp(system_id(atlas->systems[jj]), transform_to(dt)) == 0) found_dt_to = 1; } if (!found_t_from || !found_t_to) { set_error_string("atlas_from_xml: transform has invalid from/to"); atlas_destroy(atlas); return NULL; } if (!found_dt_from || !found_dt_to) { set_error_string("atlas_from_xml: transform_diff has invalid from/to"); atlas_destroy(atlas); return NULL; } } for (ii = 0 ; ii < atlas->num_systems ; ii++) { for (jj = 0 ; jj < atlas->num_systems ; jj++) { int found_ij = 0, found_ji = 0, found_ij_d = 0, found_ji_d = 0; if (ii == jj) continue; arcus_system *si, *sj; si = atlas->systems[ii]; sj = atlas->systems[jj]; for (kk = 0 ; kk < expected_transforms ; kk++) { arcus_transform *t = atlas->transforms[kk]; if (strcmp(system_id(si), transform_from(t)) == 0 && strcmp(system_id(sj), transform_to(t)) == 0) found_ij = 1; else if (strcmp(system_id(sj), transform_from(t)) == 0 && strcmp(system_id(si), transform_to(t)) == 0) found_ji = 1; t = atlas->diff_transforms[kk]; if (strcmp(system_id(si), transform_from(t)) == 0 && strcmp(system_id(sj), transform_to(t)) == 0) found_ij_d = 1; else if (strcmp(system_id(sj), transform_from(t)) == 0 && strcmp(system_id(si), transform_to(t)) == 0) found_ji_d = 1; } if (!found_ij || !found_ji) { set_error_string("atlas_from_xml: no transforms found for one pair of systems"); atlas_destroy(atlas); return NULL; } if (!found_ij_d || !found_ji_d) { set_error_string("atlas_from_xml: no differential transforms found for one pair of systems"); atlas_destroy(atlas); return NULL; } } } return atlas; }
/** * 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; }
arcus_system *system_from_xml(scew_element *element) { arcus_system *sys; scew_attribute *attr; const char *attr_val; scew_list *list; scew_element *e; const char *contents; if (!element) { set_error_string("system_from_xml: NULL element"); return NULL; } if (strcmp(scew_element_name(element), "system") != 0) { set_error_string("system_from_xml: element name != 'system'"); return NULL; } sys = system_create(); if (!sys) return NULL; // Get system id attr = scew_element_attribute_by_name(element, "id"); if (!attr) { set_error_string("system_from_xml: system without id"); system_destroy(sys); return NULL; } attr_val = scew_attribute_value(attr); if (!attr_val) { set_error_string("system_from_xml: attribute without value (scew bug?)"); system_destroy(sys); return NULL; } sys->id = strdup(attr_val); // Load metric list = scew_element_list_by_name(element, "metric"); if (!list) { set_error_string("system_create: no metric in system"); system_destroy(sys); return NULL; } if (scew_list_size(list) != 1) { set_error_string("system_create: more than one metric in system"); system_destroy(sys); return NULL; } sys->metric = metric_from_xml((scew_element *)scew_list_data(list)); if (!sys->metric) { system_destroy(sys); return NULL; } // Load christoffel symbols sys->cs = christoffel_from_xml(element); if (!sys->cs) { system_destroy(sys); return NULL; } // Load range list = scew_element_list_by_name(element, "range"); if (!list) { set_error_string("system_create: no range in system"); system_destroy(sys); return NULL; } if (scew_list_size(list) != 1) { set_error_string("system_create: more than one range in system"); system_destroy(sys); return NULL; } e = (scew_element *)scew_list_data(list); if (!e || strcmp(scew_element_name(e), "range") != 0) { set_error_string("system_create: no/wrong range element in list (SCEW bug?)"); system_destroy(sys); return NULL; } contents = scew_element_contents(e); if (!contents || !contents[0]) { set_error_string("system_create: no contents in range element"); system_destroy(sys); return NULL; } if (!af_formula_setexpr(sys->range, contents)) { set_error_string("system_create: cannot set range function expression"); system_destroy(sys); return NULL; } return sys; }