| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Author: Jon Berndt |
| 4 | Date started: 09/28/2004 |
| 5 | Purpose: XML element class |
| 6 | Called by: FGXMLParse |
| 7 | |
| 8 | ------------- Copyright (C) 2001 Jon S. Berndt ([email protected]) ------------- |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify it under |
| 11 | the terms of the GNU Lesser General Public License as published by the Free |
| 12 | Software Foundation; either version 2 of the License, or (at your option) any |
| 13 | later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 18 | details. |
| 19 | |
| 20 | You should have received a copy of the GNU Lesser General Public License along |
| 21 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 22 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | |
| 24 | Further information about the GNU Lesser General Public License can also be |
| 25 | found on the world wide web at http://www.gnu.org. |
| 26 | |
| 27 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 28 | INCLUDES |
| 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 30 | |
| 31 | #include <iostream> |
| 32 | #include <sstream> // for assembling the error messages / what of exceptions. |
| 33 | #include <stdexcept> // using domain_error, invalid_argument, and length_error. |
| 34 | #include "FGXMLElement.h" |
| 35 | #include "FGJSBBase.h" |
| 36 | |
| 37 | using namespace std; |
| 38 | |
| 39 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 40 | FORWARD DECLARATIONS |
| 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 42 | |
| 43 | namespace JSBSim { |
| 44 | |
| 45 | bool Element::converterIsInitialized = false; |
| 46 | map <string, map <string, double> > Element::convert; |
| 47 | |
| 48 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 49 | CLASS IMPLEMENTATION |
| 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 51 | |
| 52 | Element::Element(const string& nm) |
| 53 | { |
| 54 | name = nm; |
| 55 | parent = 0L; |
| 56 | element_index = 0; |
| 57 | line_number = -1; |
| 58 | |
| 59 | if (!converterIsInitialized) { |
| 60 | converterIsInitialized = true; |
| 61 | // convert ["from"]["to"] = factor, so: from * factor = to |
| 62 | // Length |
| 63 | convert["M"]["FT"] = 3.2808399; |
| 64 | convert["FT"]["M"] = 1.0/convert["M"]["FT"]; |
| 65 | convert["CM"]["FT"] = 0.032808399; |
| 66 | convert["FT"]["CM"] = 1.0/convert["CM"]["FT"]; |
| 67 | convert["KM"]["FT"] = 3280.8399; |
| 68 | convert["FT"]["KM"] = 1.0/convert["KM"]["FT"]; |
| 69 | convert["FT"]["IN"] = 12.0; |
| 70 | convert["IN"]["FT"] = 1.0/convert["FT"]["IN"]; |
| 71 | convert["IN"]["M"] = convert["IN"]["FT"] * convert["FT"]["M"]; |
| 72 | convert["M"]["IN"] = convert["M"]["FT"] * convert["FT"]["IN"]; |
| 73 | // Area |
| 74 | convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"]; |
| 75 | convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"]; |
| 76 | convert["CM2"]["FT2"] = convert["CM"]["FT"]*convert["CM"]["FT"]; |
| 77 | convert["FT2"]["CM2"] = 1.0/convert["CM2"]["FT2"]; |
| 78 | convert["M2"]["IN2"] = convert["M"]["IN"]*convert["M"]["IN"]; |
| 79 | convert["IN2"]["M2"] = 1.0/convert["M2"]["IN2"]; |
| 80 | convert["FT2"]["IN2"] = 144.0; |
| 81 | convert["IN2"]["FT2"] = 1.0/convert["FT2"]["IN2"]; |
| 82 | // Volume |
| 83 | convert["IN3"]["CC"] = 16.387064; |
| 84 | convert["CC"]["IN3"] = 1.0/convert["IN3"]["CC"]; |
| 85 | convert["FT3"]["IN3"] = 1728.0; |
| 86 | convert["IN3"]["FT3"] = 1.0/convert["FT3"]["IN3"]; |
| 87 | convert["M3"]["FT3"] = 35.3146667; |
| 88 | convert["FT3"]["M3"] = 1.0/convert["M3"]["FT3"]; |
| 89 | convert["LTR"]["IN3"] = 61.0237441; |
| 90 | convert["IN3"]["LTR"] = 1.0/convert["LTR"]["IN3"]; |
| 91 | convert["GAL"]["FT3"] = 0.133681; |
| 92 | convert["FT3"]["GAL"] = 1.0/convert["GAL"]["FT3"]; |
| 93 | convert["IN3"]["GAL"] = convert["IN3"]["FT3"]*convert["FT3"]["GAL"]; |
| 94 | convert["LTR"]["GAL"] = convert["LTR"]["IN3"]*convert["IN3"]["GAL"]; |
| 95 | convert["M3"]["GAL"] = 1000.*convert["LTR"]["GAL"]; |
| 96 | convert["CC"]["GAL"] = convert["CC"]["IN3"]*convert["IN3"]["GAL"]; |
| 97 | // Mass & Weight |
| 98 | convert["LBS"]["KG"] = 0.45359237; |
| 99 | convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"]; |
| 100 | convert["SLUG"]["KG"] = 14.59390; |
| 101 | convert["KG"]["SLUG"] = 1.0/convert["SLUG"]["KG"]; |
| 102 | // Moments of Inertia |
| 103 | convert["SLUG*FT2"]["KG*M2"] = 1.35594; |
| 104 | convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"]; |
| 105 | // Angles |
| 106 | convert["RAD"]["DEG"] = 180.0/M_PI; |
| 107 | convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"]; |
| 108 | // Angular rates |
| 109 | convert["RAD/SEC"]["DEG/SEC"] = convert["RAD"]["DEG"]; |
| 110 | convert["DEG/SEC"]["RAD/SEC"] = 1.0/convert["RAD/SEC"]["DEG/SEC"]; |
| 111 | // Spring force |
| 112 | convert["LBS/FT"]["N/M"] = 14.5939; |
| 113 | convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"]; |
| 114 | // Damping force |
| 115 | convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939; |
| 116 | convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"]; |
| 117 | // Damping force (Square Law) |
| 118 | convert["LBS/FT2/SEC2"]["N/M2/SEC2"] = 47.880259; |
| 119 | convert["N/M2/SEC2"]["LBS/FT2/SEC2"] = 1.0/convert["LBS/FT2/SEC2"]["N/M2/SEC2"]; |
| 120 | // Power |
| 121 | convert["WATTS"]["HP"] = 0.001341022; |
| 122 | convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"]; |
| 123 | // Force |
| 124 | convert["N"]["LBS"] = 0.22482; |
| 125 | convert["LBS"]["N"] = 1.0/convert["N"]["LBS"]; |
| 126 | // Velocity |
| 127 | convert["KTS"]["FT/SEC"] = 1.6878098571; |
| 128 | convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"]; |
| 129 | convert["M/S"]["FT/S"] = 3.2808399; |
| 130 | convert["M/S"]["KTS"] = convert["M/S"]["FT/S"]/convert["KTS"]["FT/SEC"]; |
| 131 | convert["M/SEC"]["FT/SEC"] = 3.2808399; |
| 132 | convert["FT/S"]["M/S"] = 1.0/convert["M/S"]["FT/S"]; |
| 133 | convert["M/SEC"]["FT/SEC"] = 3.2808399; |
| 134 | convert["FT/SEC"]["M/SEC"] = 1.0/convert["M/SEC"]["FT/SEC"]; |
| 135 | convert["KM/SEC"]["FT/SEC"] = 3280.8399; |
| 136 | convert["FT/SEC"]["KM/SEC"] = 1.0/convert["KM/SEC"]["FT/SEC"]; |
| 137 | // Torque |
| 138 | convert["FT*LBS"]["N*M"] = 1.35581795; |
| 139 | convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"]; |
| 140 | // Valve |
| 141 | convert["M4*SEC/KG"]["FT4*SEC/SLUG"] = convert["M"]["FT"]*convert["M"]["FT"]* |
| 142 | convert["M"]["FT"]*convert["M"]["FT"]/convert["KG"]["SLUG"]; |
| 143 | convert["FT4*SEC/SLUG"]["M4*SEC/KG"] = |
| 144 | 1.0/convert["M4*SEC/KG"]["FT4*SEC/SLUG"]; |
| 145 | // Pressure |
| 146 | convert["INHG"]["PSF"] = 70.7180803; |
| 147 | convert["PSF"]["INHG"] = 1.0/convert["INHG"]["PSF"]; |
| 148 | convert["ATM"]["INHG"] = 29.9246899; |
| 149 | convert["INHG"]["ATM"] = 1.0/convert["ATM"]["INHG"]; |
| 150 | convert["PSI"]["INHG"] = 2.03625437; |
| 151 | convert["INHG"]["PSI"] = 1.0/convert["PSI"]["INHG"]; |
| 152 | convert["INHG"]["PA"] = 3386.0; // inches Mercury to pascals |
| 153 | convert["PA"]["INHG"] = 1.0/convert["INHG"]["PA"]; |
| 154 | convert["LBS/FT2"]["N/M2"] = 14.5939/convert["FT"]["M"]; |
| 155 | convert["N/M2"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["N/M2"]; |
| 156 | convert["LBS/FT2"]["PA"] = convert["LBS/FT2"]["N/M2"]; |
| 157 | convert["PA"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["PA"]; |
| 158 | // Mass flow |
| 159 | convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"]; |
| 160 | convert["KG/SEC"]["LBS/SEC"] = convert["KG"]["LBS"]; |
| 161 | convert ["N/SEC"]["LBS/SEC"] = 0.224808943; |
| 162 | convert ["LBS/SEC"]["N/SEC"] = 1.0/convert ["N/SEC"]["LBS/SEC"]; |
| 163 | // Fuel Consumption |
| 164 | convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083; |
| 165 | convert["KG/KW*HR"]["LBS/HP*HR"] = 1.0/convert["LBS/HP*HR"]["KG/KW*HR"]; |
| 166 | // Density |
| 167 | convert["KG/L"]["LBS/GAL"] = 8.3454045; |
| 168 | convert["LBS/GAL"]["KG/L"] = 1.0/convert["KG/L"]["LBS/GAL"]; |
| 169 | // Gravitational |
| 170 | convert["FT3/SEC2"]["M3/SEC2"] = convert["FT3"]["M3"]; |
| 171 | convert["M3/SEC2"]["FT3/SEC2"] = convert["M3"]["FT3"]; |
| 172 | |
| 173 | // Length |
| 174 | convert["M"]["M"] = 1.00; |
| 175 | convert["KM"]["KM"] = 1.00; |
| 176 | convert["FT"]["FT"] = 1.00; |
| 177 | convert["IN"]["IN"] = 1.00; |
| 178 | // Area |
| 179 | convert["M2"]["M2"] = 1.00; |
| 180 | convert["FT2"]["FT2"] = 1.00; |
| 181 | // Volume |
| 182 | convert["IN3"]["IN3"] = 1.00; |
| 183 | convert["CC"]["CC"] = 1.0; |
| 184 | convert["M3"]["M3"] = 1.0; |
| 185 | convert["FT3"]["FT3"] = 1.0; |
| 186 | convert["LTR"]["LTR"] = 1.0; |
| 187 | convert["GAL"]["GAL"] = 1.0; |
| 188 | // Mass & Weight |
| 189 | convert["KG"]["KG"] = 1.00; |
| 190 | convert["LBS"]["LBS"] = 1.00; |
| 191 | // Moments of Inertia |
| 192 | convert["KG*M2"]["KG*M2"] = 1.00; |
| 193 | convert["SLUG*FT2"]["SLUG*FT2"] = 1.00; |
| 194 | // Angles |
| 195 | convert["DEG"]["DEG"] = 1.00; |
| 196 | convert["RAD"]["RAD"] = 1.00; |
| 197 | // Angular rates |
| 198 | convert["DEG/SEC"]["DEG/SEC"] = 1.00; |
| 199 | convert["RAD/SEC"]["RAD/SEC"] = 1.00; |
| 200 | // Spring force |
| 201 | convert["LBS/FT"]["LBS/FT"] = 1.00; |
| 202 | convert["N/M"]["N/M"] = 1.00; |
| 203 | // Damping force |
| 204 | convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00; |
| 205 | convert["N/M/SEC"]["N/M/SEC"] = 1.00; |
| 206 | // Damping force (Square law) |
| 207 | convert["LBS/FT2/SEC2"]["LBS/FT2/SEC2"] = 1.00; |
| 208 | convert["N/M2/SEC2"]["N/M2/SEC2"] = 1.00; |
| 209 | // Power |
| 210 | convert["HP"]["HP"] = 1.00; |
| 211 | convert["WATTS"]["WATTS"] = 1.00; |
| 212 | // Force |
| 213 | convert["N"]["N"] = 1.00; |
| 214 | // Velocity |
| 215 | convert["FT/SEC"]["FT/SEC"] = 1.00; |
| 216 | convert["KTS"]["KTS"] = 1.00; |
| 217 | convert["M/S"]["M/S"] = 1.0; |
| 218 | convert["M/SEC"]["M/SEC"] = 1.0; |
| 219 | convert["KM/SEC"]["KM/SEC"] = 1.0; |
| 220 | // Torque |
| 221 | convert["FT*LBS"]["FT*LBS"] = 1.00; |
| 222 | convert["N*M"]["N*M"] = 1.00; |
| 223 | // Valve |
| 224 | convert["M4*SEC/KG"]["M4*SEC/KG"] = 1.0; |
| 225 | convert["FT4*SEC/SLUG"]["FT4*SEC/SLUG"] = 1.0; |
| 226 | // Pressure |
| 227 | convert["PSI"]["PSI"] = 1.00; |
| 228 | convert["PSF"]["PSF"] = 1.00; |
| 229 | convert["INHG"]["INHG"] = 1.00; |
| 230 | convert["ATM"]["ATM"] = 1.0; |
| 231 | convert["PA"]["PA"] = 1.0; |
| 232 | convert["N/M2"]["N/M2"] = 1.00; |
| 233 | convert["LBS/FT2"]["LBS/FT2"] = 1.00; |
| 234 | // Mass flow |
| 235 | convert["LBS/SEC"]["LBS/SEC"] = 1.00; |
| 236 | convert["KG/MIN"]["KG/MIN"] = 1.0; |
| 237 | convert["LBS/MIN"]["LBS/MIN"] = 1.0; |
| 238 | convert["N/SEC"]["N/SEC"] = 1.0; |
| 239 | // Fuel Consumption |
| 240 | convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0; |
| 241 | convert["KG/KW*HR"]["KG/KW*HR"] = 1.0; |
| 242 | // Density |
| 243 | convert["KG/L"]["KG/L"] = 1.0; |
| 244 | convert["LBS/GAL"]["LBS/GAL"] = 1.0; |
| 245 | // Gravitational |
| 246 | convert["FT3/SEC2"]["FT3/SEC2"] = 1.0; |
| 247 | convert["M3/SEC2"]["M3/SEC2"] = 1.0; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 252 | |
| 253 | Element::~Element(void) |
| 254 | { |
| 255 | for (unsigned int i = 0; i < children.size(); ++i) |
| 256 | children[i]->SetParent(0); |
| 257 | } |
| 258 | |
| 259 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 260 | |
| 261 | string Element::GetAttributeValue(const string& attr) |
| 262 | { |
| 263 | if (HasAttribute(attr)) return attributes[attr]; |
| 264 | else return (""); |
| 265 | } |
| 266 | |
| 267 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 268 | |
| 269 | bool Element::SetAttributeValue(const std::string& key, const std::string& value) |
| 270 | { |
| 271 | bool ret = HasAttribute(key); |
| 272 | if (ret) |
| 273 | attributes[key] = value; |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 279 | |
| 280 | double Element::GetAttributeValueAsNumber(const string& attr) |
| 281 | { |
| 282 | string attribute = GetAttributeValue(attr); |
| 283 | |
| 284 | if (attribute.empty()) { |
| 285 | std::stringstream s; |
| 286 | s << ReadFrom() << "Expecting numeric attribute value, but got no data"; |
| 287 | cerr << s.str() << endl; |
| 288 | throw length_error(s.str()); |
| 289 | } |
| 290 | else { |
| 291 | double number=0; |
| 292 | if (is_number(trim(attribute))) |
| 293 | number = atof_locale_c(attribute); |
| 294 | else { |
| 295 | std::stringstream s; |
| 296 | s << ReadFrom() << "Expecting numeric attribute value, but got: " << attribute; |
| 297 | cerr << s.str() << endl; |
| 298 | throw BaseException(s.str()); |
| 299 | } |
| 300 | |
| 301 | return (number); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 306 | |
| 307 | Element* Element::GetElement(unsigned int el) |
| 308 | { |
| 309 | if (children.size() > el) { |
| 310 | element_index = el; |
| 311 | return children[el]; |
| 312 | } |
| 313 | else { |
| 314 | element_index = 0; |
| 315 | return 0L; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 320 | |
| 321 | Element* Element::GetNextElement(void) |
| 322 | { |
| 323 | if (children.size() > element_index+1) { |
| 324 | element_index++; |
| 325 | return children[element_index]; |
| 326 | } else { |
| 327 | element_index = 0; |
| 328 | return 0L; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 333 | |
| 334 | string Element::GetDataLine(unsigned int i) |
| 335 | { |
| 336 | if (!data_lines.empty()) return data_lines[i]; |
| 337 | else return string(""); |
| 338 | } |
| 339 | |
| 340 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 341 | |
| 342 | double Element::GetDataAsNumber(void) |
| 343 | { |
| 344 | if (data_lines.size() == 1) { |
| 345 | double number=0; |
| 346 | if (is_number(trim(data_lines[0]))) |
| 347 | number = atof_locale_c(data_lines[0]); |
| 348 | else { |
| 349 | std::stringstream s; |
| 350 | s << ReadFrom() << "Expected numeric value, but got: " << data_lines[0]; |
| 351 | cerr << s.str() << endl; |
| 352 | throw BaseException(s.str()); |
| 353 | } |
| 354 | |
| 355 | return number; |
| 356 | } else if (data_lines.empty()) { |
| 357 | std::stringstream s; |
| 358 | s << ReadFrom() << "Expected numeric value, but got no data"; |
| 359 | cerr << s.str() << endl; |
| 360 | throw length_error(s.str()); |
| 361 | } else { |
| 362 | cerr << ReadFrom() << "Attempting to get single data value in element " |
| 363 | << "<" << name << ">" << endl |
| 364 | << " from multiple lines:" << endl; |
| 365 | for(unsigned int i=0; i<data_lines.size(); ++i) |
| 366 | cerr << data_lines[i] << endl; |
| 367 | std::stringstream s; |
| 368 | s << ReadFrom() << "Attempting to get single data value in element " |
| 369 | << "<" << name << ">" |
| 370 | << " from multiple lines (" << data_lines.size() << ")."; |
| 371 | throw length_error(s.str()); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 376 | |
| 377 | unsigned int Element::GetNumElements(const string& element_name) |
| 378 | { |
| 379 | unsigned int number_of_elements=0; |
| 380 | Element* el=FindElement(element_name); |
| 381 | while (el) { |
| 382 | number_of_elements++; |
| 383 | el=FindNextElement(element_name); |
| 384 | } |
| 385 | return number_of_elements; |
| 386 | } |
| 387 | |
| 388 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 389 | |
| 390 | Element* Element::FindElement(const string& el) |
| 391 | { |
| 392 | if (el.empty() && children.size() >= 1) { |
| 393 | element_index = 1; |
| 394 | return children[0]; |
| 395 | } |
| 396 | for (unsigned int i=0; i<children.size(); i++) { |
| 397 | if (el == children[i]->GetName()) { |
| 398 | element_index = i+1; |
| 399 | return children[i]; |
| 400 | } |
| 401 | } |
| 402 | element_index = 0; |
| 403 | return 0L; |
| 404 | } |
| 405 | |
| 406 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 407 | |
| 408 | Element* Element::FindNextElement(const string& el) |
| 409 | { |
| 410 | if (el.empty()) { |
| 411 | if (element_index < children.size()) { |
| 412 | return children[element_index++]; |
| 413 | } else { |
| 414 | element_index = 0; |
| 415 | return 0L; |
| 416 | } |
| 417 | } |
| 418 | for (unsigned int i=element_index; i<children.size(); i++) { |
| 419 | if (el == children[i]->GetName()) { |
| 420 | element_index = i+1; |
| 421 | return children[i]; |
| 422 | } |
| 423 | } |
| 424 | element_index = 0; |
| 425 | return 0L; |
| 426 | } |
| 427 | |
| 428 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 429 | |
| 430 | double Element::FindElementValueAsNumber(const string& el) |
| 431 | { |
| 432 | Element* element = FindElement(el); |
| 433 | if (element) { |
| 434 | double value = element->GetDataAsNumber(); |
| 435 | value = DisperseValue(element, value); |
| 436 | return value; |
| 437 | } else { |
| 438 | std::stringstream s; |
| 439 | s << ReadFrom() << "Attempting to get non-existent element " << el; |
| 440 | cerr << s.str() << endl; |
| 441 | throw length_error(s.str()); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 446 | |
| 447 | bool Element::FindElementValueAsBoolean(const string& el) |
| 448 | { |
| 449 | Element* element = FindElement(el); |
| 450 | if (element) { |
| 451 | // check value as an ordinary number |
| 452 | double value = element->GetDataAsNumber(); |
| 453 | |
| 454 | // now check how it should return data |
| 455 | if (value == 0) { |
| 456 | return false; |
| 457 | } else { |
| 458 | return true; |
| 459 | } |
| 460 | } else { |
| 461 | cerr << ReadFrom() << "Attempting to get non-existent element " << el << " ;returning false" |
| 462 | << endl; |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 468 | |
| 469 | string Element::FindElementValue(const string& el) |
| 470 | { |
| 471 | Element* element = FindElement(el); |
| 472 | if (element) { |
| 473 | return element->GetDataLine(); |
| 474 | } else { |
| 475 | return ""; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 480 | |
| 481 | double Element::FindElementValueAsNumberConvertTo(const string& el, const string& target_units) |
| 482 | { |
| 483 | Element* element = FindElement(el); |
| 484 | |
| 485 | if (!element) { |
| 486 | std::stringstream s; |
| 487 | s << ReadFrom() << "Attempting to get non-existent element " << el; |
| 488 | cerr << s.str() << endl; |
| 489 | throw length_error(s.str()); |
| 490 | } |
| 491 | |
| 492 | string supplied_units = element->GetAttributeValue("unit"); |
| 493 | |
| 494 | if (!supplied_units.empty()) { |
| 495 | if (convert.find(supplied_units) == convert.end()) { |
| 496 | std::stringstream s; |
| 497 | s << element->ReadFrom() << "Supplied unit: \"" << supplied_units |
| 498 | << "\" does not exist (typo?)."; |
| 499 | cerr << s.str() << endl; |
| 500 | throw invalid_argument(s.str()); |
| 501 | } |
| 502 | if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) { |
| 503 | std::stringstream s; |
| 504 | s << element->ReadFrom() << "Supplied unit: \"" << supplied_units |
| 505 | << "\" cannot be converted to " << target_units; |
| 506 | cerr << s.str() << endl; |
| 507 | throw invalid_argument(s.str()); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | double value = element->GetDataAsNumber(); |
| 512 | |
| 513 | // Sanity check for angular values |
| 514 | if ((supplied_units == "RAD") && (fabs(value) > 2 * M_PI)) { |
| 515 | cerr << element->ReadFrom() << element->GetName() << " value " |
| 516 | << value << " RAD is outside the range [ -2*M_PI RAD ; +2*M_PI RAD ]" |
| 517 | << endl; |
| 518 | } |
| 519 | if ((supplied_units == "DEG") && (fabs(value) > 360.0)) { |
| 520 | cerr << element->ReadFrom() << element->GetName() << " value " |
| 521 | << value << " DEG is outside the range [ -360 DEG ; +360 DEG ]" |
| 522 | << endl; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | if (!supplied_units.empty()) { |
| 527 | value *= convert[supplied_units][target_units]; |
| 528 | } |
| 529 | |
| 530 | if ((target_units == "RAD") && (fabs(value) > 2 * M_PI)) { |
| 531 | cerr << element->ReadFrom() << element->GetName() << " value " |
| 532 | << value << " RAD is outside the range [ -2*M_PI RAD ; +2*M_PI RAD ]" |
| 533 | << endl; |
| 534 | } |
| 535 | if ((target_units == "DEG") && (fabs(value) > 360.0)) { |
| 536 | cerr << element->ReadFrom() << element->GetName() << " value " |
| 537 | << value << " DEG is outside the range [ -360 DEG ; +360 DEG ]" |
| 538 | << endl; |
| 539 | } |
| 540 | |
| 541 | value = DisperseValue(element, value, supplied_units, target_units); |
| 542 | |
| 543 | return value; |
| 544 | } |
| 545 | |
| 546 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 547 | |
| 548 | double Element::FindElementValueAsNumberConvertFromTo( const string& el, |
| 549 | const string& supplied_units, |
| 550 | const string& target_units) |
| 551 | { |
| 552 | Element* element = FindElement(el); |
| 553 | |
| 554 | if (!element) { |
| 555 | std::stringstream s; |
| 556 | s << ReadFrom() << "Attempting to get non-existent element " << el; |
| 557 | cerr << s.str() << endl; |
| 558 | throw length_error(s.str()); |
| 559 | } |
| 560 | |
| 561 | if (!supplied_units.empty()) { |
| 562 | if (convert.find(supplied_units) == convert.end()) { |
| 563 | std::stringstream s; |
| 564 | s << element->ReadFrom() << "Supplied unit: \"" << supplied_units |
| 565 | << "\" does not exist (typo?)."; |
| 566 | cerr << s.str() << endl; |
| 567 | throw invalid_argument(s.str()); |
| 568 | } |
| 569 | if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) { |
| 570 | std::stringstream s; |
| 571 | s << element->ReadFrom() << "Supplied unit: \"" << supplied_units |
| 572 | << "\" cannot be converted to " << target_units; |
| 573 | cerr << s.str() << endl; |
| 574 | throw invalid_argument(s.str()); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | double value = element->GetDataAsNumber(); |
| 579 | if (!supplied_units.empty()) { |
| 580 | value *= convert[supplied_units][target_units]; |
| 581 | } |
| 582 | |
| 583 | value = DisperseValue(element, value, supplied_units, target_units); |
| 584 | |
| 585 | return value; |
| 586 | } |
| 587 | |
| 588 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 589 | |
| 590 | FGColumnVector3 Element::FindElementTripletConvertTo( const string& target_units) |
| 591 | { |
| 592 | FGColumnVector3 triplet; |
| 593 | Element* item; |
| 594 | double value=0.0; |
| 595 | string supplied_units = GetAttributeValue("unit"); |
| 596 | |
| 597 | if (!supplied_units.empty()) { |
| 598 | if (convert.find(supplied_units) == convert.end()) { |
| 599 | std::stringstream s; |
| 600 | s << ReadFrom() << "Supplied unit: \"" << supplied_units |
| 601 | << "\" does not exist (typo?)."; |
| 602 | cerr << s.str() << endl; |
| 603 | throw invalid_argument(s.str()); |
| 604 | } |
| 605 | if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) { |
| 606 | std::stringstream s; |
| 607 | s << ReadFrom() << "Supplied unit: \"" << supplied_units |
| 608 | << "\" cannot be converted to " << target_units; |
| 609 | cerr << s.str() << endl; |
| 610 | throw invalid_argument(s.str()); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | item = FindElement("x"); |
| 615 | if (!item) item = FindElement("roll"); |
| 616 | if (item) { |
| 617 | value = item->GetDataAsNumber(); |
| 618 | if (!supplied_units.empty()) value *= convert[supplied_units][target_units]; |
| 619 | triplet(1) = DisperseValue(item, value, supplied_units, target_units); |
| 620 | } else { |
| 621 | triplet(1) = 0.0; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | item = FindElement("y"); |
| 626 | if (!item) item = FindElement("pitch"); |
| 627 | if (item) { |
| 628 | value = item->GetDataAsNumber(); |
| 629 | if (!supplied_units.empty()) value *= convert[supplied_units][target_units]; |
| 630 | triplet(2) = DisperseValue(item, value, supplied_units, target_units); |
| 631 | } else { |
| 632 | triplet(2) = 0.0; |
| 633 | } |
| 634 | |
| 635 | item = FindElement("z"); |
| 636 | if (!item) item = FindElement("yaw"); |
| 637 | if (item) { |
| 638 | value = item->GetDataAsNumber(); |
| 639 | if (!supplied_units.empty()) value *= convert[supplied_units][target_units]; |
| 640 | triplet(3) = DisperseValue(item, value, supplied_units, target_units); |
| 641 | } else { |
| 642 | triplet(3) = 0.0; |
| 643 | } |
| 644 | |
| 645 | return triplet; |
| 646 | } |
| 647 | |
| 648 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 649 | |
| 650 | double Element::DisperseValue(Element *e, double val, const std::string& supplied_units, |
| 651 | const std::string& target_units) |
| 652 | { |
| 653 | double value=val; |
| 654 | |
| 655 | bool disperse = false; |
| 656 | try { |
| 657 | char* num = getenv("JSBSIM_DISPERSE"); |
| 658 | if (num) { |
| 659 | disperse = (atoi(num) == 1); // set dispersions |
| 660 | } |
| 661 | } catch (...) { // if error set to false |
| 662 | disperse = false; |
| 663 | std::cerr << "Could not process JSBSIM_DISPERSE environment variable: Assumed NO dispersions." << endl; |
| 664 | } |
| 665 | |
| 666 | if (e->HasAttribute("dispersion") && disperse) { |
| 667 | double disp = e->GetAttributeValueAsNumber("dispersion"); |
| 668 | if (!supplied_units.empty()) disp *= convert[supplied_units][target_units]; |
| 669 | string attType = e->GetAttributeValue("type"); |
| 670 | RandomNumberGenerator generator; |
| 671 | |
| 672 | if (attType == "gaussian" || attType == "gaussiansigned") { |
| 673 | double grn = generator.GetNormalRandomNumber(); |
| 674 | if (attType == "gaussian") |
| 675 | value = val + disp*grn; |
| 676 | else // Assume gaussiansigned |
| 677 | value = (val + disp*grn)*FGJSBBase::sign(grn); |
| 678 | } else if (attType == "uniform" || attType == "uniformsigned") { |
| 679 | double urn = generator.GetUniformRandomNumber(); |
| 680 | if (attType == "uniform") |
| 681 | value = val + disp * urn; |
| 682 | else // Assume uniformsigned |
| 683 | value = (val + disp * urn)*FGJSBBase::sign(urn); |
| 684 | } else { |
| 685 | std::stringstream s; |
| 686 | s << ReadFrom() << "Unknown dispersion type" << attType; |
| 687 | cerr << s.str() << endl; |
| 688 | throw domain_error(s.str()); |
| 689 | } |
| 690 | |
| 691 | } |
| 692 | return value; |
| 693 | } |
| 694 | |
| 695 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 696 | |
| 697 | void Element::Print(unsigned int level) |
| 698 | { |
| 699 | unsigned int i, spaces; |
| 700 | |
| 701 | level+=2; |
| 702 | for (spaces=0; spaces<=level; spaces++) cout << " "; // format output |
| 703 | cout << "Element Name: " << name; |
| 704 | |
| 705 | map<string, string>::iterator it; |
| 706 | for (it = attributes.begin(); it != attributes.end(); ++it) |
| 707 | cout << " " << it->first << " = " << it->second; |
| 708 | |
| 709 | cout << endl; |
| 710 | for (i=0; i<data_lines.size(); i++) { |
| 711 | for (spaces=0; spaces<=level; spaces++) cout << " "; // format output |
| 712 | cout << data_lines[i] << endl; |
| 713 | } |
| 714 | for (i=0; i<children.size(); i++) { |
| 715 | children[i]->Print(level); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 720 | |
| 721 | void Element::AddAttribute(const string& name, const string& value) |
| 722 | { |
| 723 | attributes[name] = value; |
| 724 | } |
| 725 | |
| 726 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 727 | |
| 728 | void Element::AddData(string d) |
| 729 | { |
| 730 | string::size_type string_start = d.find_first_not_of(" \t"); |
| 731 | if (string_start != string::npos && string_start > 0) { |
| 732 | d.erase(0,string_start); |
| 733 | } |
| 734 | data_lines.push_back(d); |
| 735 | } |
| 736 | |
| 737 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 738 | |
| 739 | string Element::ReadFrom(void) const |
| 740 | { |
| 741 | ostringstream message; |
| 742 | |
| 743 | message << endl |
| 744 | << "In file " << GetFileName() << ": line " << GetLineNumber() |
| 745 | << endl; |
| 746 | |
| 747 | return message.str(); |
| 748 | } |
| 749 | |
| 750 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 751 | |
| 752 | void Element::MergeAttributes(Element* el) |
| 753 | { |
| 754 | map<string, string>::iterator it; |
| 755 | |
| 756 | for (it=el->attributes.begin(); it != el->attributes.end(); ++it) { |
| 757 | if (attributes.find(it->first) == attributes.end()) |
| 758 | attributes[it->first] = it->second; |
| 759 | else { |
| 760 | if (FGJSBBase::debug_lvl > 0 && (attributes[it->first] != it->second)) |
| 761 | cout << el->ReadFrom() << " Attribute '" << it->first << "' is overridden in file " |
| 762 | << GetFileName() << ": line " << GetLineNumber() << endl |
| 763 | << " The value '" << attributes[it->first] << "' will be used instead of '" |
| 764 | << it->second << "'." << endl; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | } // end namespace JSBSim |