1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Loads and runs JSBSim scripts.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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 Software
12 Foundation; either version 2 of the License, or (at your option) any later
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
20 You should have received a copy of the GNU Lesser General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class wraps up the simulation scripting routines.
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES, and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 #include "input_output/FGXMLElement.h"
46 #include "input_output/FGXMLParse.h"
47 #include "initialization/FGTrim.h"
57 static const char *IdSrc = "$Id: FGScript.cpp,v 1.42 2010/11/24 12:58:39 jberndt Exp $";
58 static const char *IdHdr = ID_FGSCRIPT;
60 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
72 PropertyManager=FDMExec->GetPropertyManager();
77 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 for (i=0; i<local_properties.size(); i++) delete local_properties[i];
84 local_properties.clear();
86 for (i=0; i<Events.size(); i++) delete Events[i].Condition;
92 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 bool FGScript::LoadScript(string script, double deltaT)
96 string aircraft="", initialize="", comparison = "", prop_name="";
97 string notifyPropertyName="";
98 Element *element=0, *run_element=0, *event_element=0;
99 Element *condition_element=0, *set_element=0, *delay_element=0;
100 Element *notify_element = 0L, *notify_property_element = 0L;
101 Element *property_element = 0L;
102 Element *output_element = 0L;
103 Element *input_element = 0L;
105 double dt = 0.0, value = 0.0;
106 struct event *newEvent;
107 FGCondition *newCondition;
109 document = LoadXMLDocument(script);
112 cerr << "File: " << script << " could not be loaded." << endl;
116 // Set up input and output files if specified
118 output_element = document->FindElement("output");
119 input_element = document->FindElement("input");
121 if (document->GetName() != string("runscript")) {
122 cerr << "File: " << script << " is not a script file" << endl;
126 ScriptName = document->GetAttributeValue("name");
128 // First, find "run" element and set delta T
130 run_element = document->FindElement("run");
133 cerr << "No \"run\" element found in script." << endl;
139 StartTime = run_element->GetAttributeValueAsNumber("start");
140 FDMExec->Setsim_time(StartTime);
141 EndTime = run_element->GetAttributeValueAsNumber("end");
144 dt = run_element->GetAttributeValueAsNumber("dt");
147 cout << endl << "Overriding simulation step size from the command line. New step size is: "
148 << deltaT << " seconds (" << 1/deltaT << " Hz)" << endl << endl;
153 // read aircraft and initialization files
155 element = document->FindElement("use");
157 aircraft = element->GetAttributeValue("aircraft");
158 if (!aircraft.empty()) {
159 result = FDMExec->LoadModel(aircraft);
160 if (!result) return false;
162 cerr << "Aircraft must be specified in use element." << endl;
166 initialize = element->GetAttributeValue("initialize");
167 if (initialize.empty()) {
168 cerr << "Initialization file must be specified in use element." << endl;
173 cerr << "No \"use\" directives in the script file." << endl;
177 // Now, read input spec if given.
178 if (input_element > 0) {
179 FDMExec->GetInput()->Load(input_element);
182 // Now, read output spec if given.
183 if (output_element > 0) {
184 string output_file = output_element->GetAttributeValue("file");
185 if (output_file.empty()) {
186 cerr << "No logging directives file was specified." << endl;
188 if (!FDMExec->SetOutputDirectives(output_file)) return false;
192 // Read local property/value declarations
193 property_element = run_element->FindElement("property");
194 while (property_element) {
199 title = property_element->GetDataLine();
200 if ( ! property_element->GetAttributeValue("value").empty())
201 value = property_element->GetAttributeValueAsNumber("value");
203 LocalProps *localProp = new LocalProps(value);
204 localProp->title = title;
205 local_properties.push_back(localProp);
206 if (PropertyManager->HasNode(title)) {
207 PropertyManager->GetNode(title)->setDoubleValue(value);
209 PropertyManager->Tie(localProp->title, localProp->value);
211 property_element = run_element->FindNextElement("property");
214 // Read "events" from script
216 event_element = run_element->FindElement("event");
217 while (event_element) { // event processing
219 // Create the event structure
220 newEvent = new struct event();
222 // Retrieve the event name if given
223 newEvent->Name = event_element->GetAttributeValue("name");
225 // Is this event persistent? That is, does it execute every time the
226 // condition triggers to true, or does it execute as a one-shot event, only?
227 if (event_element->GetAttributeValue("persistent") == string("true")) {
228 newEvent->Persistent = true;
231 // Does this event execute continuously when triggered to true?
232 if (event_element->GetAttributeValue("continuous") == string("true")) {
233 newEvent->Continuous = true;
236 // Process the conditions
237 condition_element = event_element->FindElement("condition");
238 if (condition_element != 0) {
240 newCondition = new FGCondition(condition_element, PropertyManager);
241 } catch(string str) {
242 cout << endl << fgred << str << reset << endl << endl;
245 newEvent->Condition = newCondition;
247 cerr << "No condition specified in script event " << newEvent->Name << endl;
251 // Is there a delay between the time this event is triggered, and when the event
252 // actions are executed?
254 delay_element = event_element->FindElement("delay");
255 if (delay_element) newEvent->Delay = event_element->FindElementValueAsNumber("delay");
256 else newEvent->Delay = 0.0;
258 // Notify about when this event is triggered?
259 if ((notify_element = event_element->FindElement("notify")) != 0) {
260 newEvent->Notify = true;
261 notify_property_element = notify_element->FindElement("property");
262 while (notify_property_element) {
263 notifyPropertyName = notify_property_element->GetDataLine();
264 if (PropertyManager->GetNode(notifyPropertyName)) {
265 newEvent->NotifyProperties.push_back( PropertyManager->GetNode(notifyPropertyName) );
267 cout << endl << fgred << " Could not find the property named "
268 << notifyPropertyName << " in script" << endl << " \""
269 << ScriptName << "\". This unknown property will not be "
270 << "echoed for notification." << reset << endl;
272 notify_property_element = notify_element->FindNextElement("property");
276 // Read set definitions (these define the actions to be taken when the event is triggered).
277 set_element = event_element->FindElement("set");
278 while (set_element) {
279 prop_name = set_element->GetAttributeValue("name");
280 newEvent->SetParam.push_back( PropertyManager->GetNode(prop_name) );
281 //Todo - should probably do some safety checking here to make sure one or the other
282 //of value or function is specified.
283 if (!set_element->GetAttributeValue("value").empty()) {
284 value = set_element->GetAttributeValueAsNumber("value");
285 newEvent->Functions.push_back((FGFunction*)0L);
286 } else if (set_element->FindElement("function")) {
288 newEvent->Functions.push_back(new FGFunction(PropertyManager, set_element->FindElement("function")));
290 newEvent->SetValue.push_back(value);
291 newEvent->OriginalValue.push_back(0.0);
292 newEvent->newValue.push_back(0.0);
293 newEvent->ValueSpan.push_back(0.0);
294 string tempCompare = set_element->GetAttributeValue("type");
295 if (to_lower(tempCompare).find("delta") != string::npos) newEvent->Type.push_back(FG_DELTA);
296 else if (to_lower(tempCompare).find("bool") != string::npos) newEvent->Type.push_back(FG_BOOL);
297 else if (to_lower(tempCompare).find("value") != string::npos) newEvent->Type.push_back(FG_VALUE);
298 else newEvent->Type.push_back(FG_VALUE); // DEFAULT
299 tempCompare = set_element->GetAttributeValue("action");
300 if (to_lower(tempCompare).find("ramp") != string::npos) newEvent->Action.push_back(FG_RAMP);
301 else if (to_lower(tempCompare).find("step") != string::npos) newEvent->Action.push_back(FG_STEP);
302 else if (to_lower(tempCompare).find("exp") != string::npos) newEvent->Action.push_back(FG_EXP);
303 else newEvent->Action.push_back(FG_STEP); // DEFAULT
305 if (!set_element->GetAttributeValue("tc").empty())
306 newEvent->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));
308 newEvent->TC.push_back(1.0); // DEFAULT
310 newEvent->Transiting.push_back(false);
312 set_element = event_element->FindNextElement("set");
314 Events.push_back(*newEvent);
317 event_element = run_element->FindNextElement("event");
322 FGInitialCondition *IC=FDMExec->GetIC();
323 if ( ! IC->Load( initialize )) {
324 cerr << "Initialization unsuccessful" << endl;
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 bool FGScript::RunScript(void)
336 unsigned event_ctr = 0;
338 double currentTime = FDMExec->GetSimTime();
339 double newSetValue = 0;
341 if (currentTime > EndTime) return false; //Script done!
343 // Iterate over all events.
344 for (unsigned int ev_ctr=0; ev_ctr < Events.size(); ev_ctr++) {
345 // Determine whether the set of conditional tests for this condition equate
346 // to true and should cause the event to execute. If the conditions evaluate
347 // to true, then the event is triggered. If the event is not persistent,
348 // then this trigger will remain set true. If the event is persistent,
349 // the trigger will reset to false when the condition evaluates to false.
350 if (Events[ev_ctr].Condition->Evaluate()) {
351 if (!Events[ev_ctr].Triggered) {
353 // The conditions are true, do the setting of the desired Event parameters
354 for (i=0; i<Events[ev_ctr].SetValue.size(); i++) {
355 Events[ev_ctr].OriginalValue[i] = Events[ev_ctr].SetParam[i]->getDoubleValue();
356 if (Events[ev_ctr].Functions[i] != 0) { // Parameter should be set to a function value
357 Events[ev_ctr].SetValue[i] = Events[ev_ctr].Functions[i]->GetValue();
359 switch (Events[ev_ctr].Type[i]) {
362 Events[ev_ctr].newValue[i] = Events[ev_ctr].SetValue[i];
365 Events[ev_ctr].newValue[i] = Events[ev_ctr].OriginalValue[i] + Events[ev_ctr].SetValue[i];
368 cerr << "Invalid Type specified" << endl;
371 Events[ev_ctr].StartTime = currentTime + Events[ev_ctr].Delay;
372 Events[ev_ctr].ValueSpan[i] = Events[ev_ctr].newValue[i] - Events[ev_ctr].OriginalValue[i];
373 Events[ev_ctr].Transiting[i] = true;
376 Events[ev_ctr].Triggered = true;
378 } else if (Events[ev_ctr].Persistent) { // If the event is persistent, reset the trigger.
379 Events[ev_ctr].Triggered = false; // Reset the trigger for persistent events
380 Events[ev_ctr].Notified = false; // Also reset the notification flag
381 } else if (Events[ev_ctr].Continuous) { // If the event is continuous, reset the trigger.
382 Events[ev_ctr].Triggered = false; // Reset the trigger for persistent events
383 Events[ev_ctr].Notified = false; // Also reset the notification flag
386 if ((currentTime >= Events[ev_ctr].StartTime) && Events[ev_ctr].Triggered) {
388 for (i=0; i<Events[ev_ctr].SetValue.size(); i++) {
389 if (Events[ev_ctr].Transiting[i]) {
390 Events[ev_ctr].TimeSpan = currentTime - Events[ev_ctr].StartTime;
391 switch (Events[ev_ctr].Action[i]) {
393 if (Events[ev_ctr].TimeSpan <= Events[ev_ctr].TC[i]) {
394 newSetValue = Events[ev_ctr].TimeSpan/Events[ev_ctr].TC[i] * Events[ev_ctr].ValueSpan[i] + Events[ev_ctr].OriginalValue[i];
396 newSetValue = Events[ev_ctr].newValue[i];
397 if (Events[ev_ctr].Continuous != true) Events[ev_ctr].Transiting[i] = false;
401 newSetValue = Events[ev_ctr].newValue[i];
403 // If this is not a continuous event, reset the transiting flag.
404 // Otherwise, it is known that the event is a continuous event.
405 // Furthermore, if the event is to be determined by a function,
406 // then the function will be continuously calculated.
407 if (Events[ev_ctr].Continuous != true)
408 Events[ev_ctr].Transiting[i] = false;
409 else if (Events[ev_ctr].Functions[i] != 0)
410 newSetValue = Events[ev_ctr].Functions[i]->GetValue();
414 newSetValue = (1 - exp( -Events[ev_ctr].TimeSpan/Events[ev_ctr].TC[i] )) * Events[ev_ctr].ValueSpan[i] + Events[ev_ctr].OriginalValue[i];
417 cerr << "Invalid Action specified" << endl;
420 Events[ev_ctr].SetParam[i]->setDoubleValue(newSetValue);
424 // Print notification values after setting them
425 if (Events[ev_ctr].Notify && !Events[ev_ctr].Notified) {
426 cout << endl << " Event " << event_ctr << " (" << Events[ev_ctr].Name << ")"
427 << " executed at time: " << currentTime << endl;
428 for (j=0; j<Events[ev_ctr].NotifyProperties.size();j++) {
429 cout << " " << Events[ev_ctr].NotifyProperties[j]->GetRelativeName()
430 << " = " << Events[ev_ctr].NotifyProperties[j]->getDoubleValue() << endl;
433 Events[ev_ctr].Notified = true;
443 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 // The bitmasked value choices are as follows:
445 // unset: In this case (the default) JSBSim would only print
446 // out the normally expected messages, essentially echoing
447 // the config files as they are read. If the environment
448 // variable is not set, debug_lvl is set to 1 internally
449 // 0: This requests JSBSim not to output any messages
451 // 1: This value explicity requests the normal JSBSim
453 // 2: This value asks for a message to be printed out when
454 // a class is instantiated
455 // 4: When this value is set, a message is displayed when a
456 // FGModel object executes its Run() method
457 // 8: When this value is set, various runtime state variables
458 // are printed out periodically
459 // 16: When set various parameters are sanity checked and
460 // a message is printed out when they go out of bounds
462 void FGScript::Debug(int from)
464 if (debug_lvl <= 0) return;
466 if (debug_lvl & 1) { // Standard console startup message output
467 if (from == 0) { // Constructor
468 } else if (from == 3) {
469 } else if (from == 4) { // print out script data
471 cout << "Script: \"" << ScriptName << "\"" << endl;
472 cout << " begins at " << StartTime << " seconds and runs to " << EndTime
473 << " seconds with dt = " << setprecision(6) << FDMExec->GetDeltaT() << " (" <<
474 ceil(1.0/FDMExec->GetDeltaT()) << " Hz)" << endl;
477 for (unsigned int i=0; i<local_properties.size(); i++) {
478 cout << "Local property: " << local_properties[i]->title
479 << " = " << PropertyManager->GetNode(local_properties[i]->title)->getDoubleValue()
483 if (local_properties.size() > 0) cout << endl;
485 for (unsigned i=0; i<Events.size(); i++) {
486 cout << "Event " << i;
487 if (!Events[i].Name.empty()) cout << " (" << Events[i].Name << ")";
490 if (Events[i].Persistent)
491 cout << " " << "Whenever triggered, executes once";
492 else if (Events[i].Continuous)
493 cout << " " << "While true, always executes";
495 cout << " " << "When first triggered, executes once";
497 Events[i].Condition->PrintCondition();
499 cout << endl << " Actions taken";
500 if (Events[i].Delay > 0.0)
501 cout << " (after a delay of " << Events[i].Delay << " secs)";
502 cout << ":" << endl << " {";
503 for (unsigned j=0; j<Events[i].SetValue.size(); j++) {
504 if (Events[i].SetValue[j] == 0.0 && Events[i].Functions[j] != 0L) {
505 if (Events[i].SetParam[j] == 0) {
506 cerr << fgred << highint << endl
507 << " An attempt has been made to access a non-existent property" << endl
508 << " in this event. Please check the property names used, spelling, etc."
512 cout << endl << " set " << Events[i].SetParam[j]->GetRelativeName("/fdm/jsbsim/")
513 << " to function value";
515 if (Events[i].SetParam[j] == 0) {
516 cerr << fgred << highint << endl
517 << " An attempt has been made to access a non-existent property" << endl
518 << " in this event. Please check the property names used, spelling, etc."
522 cout << endl << " set " << Events[i].SetParam[j]->GetRelativeName("/fdm/jsbsim/")
523 << " to " << Events[i].SetValue[j];
526 switch (Events[i].Type[j]) {
529 cout << " (constant";
535 cout << " (unspecified type";
538 switch (Events[i].Action[j]) {
543 cout << " via step)";
546 cout << " via exponential approach";
549 cout << " via unspecified action)";
552 if (Events[i].Action[j] == FG_RAMP || Events[i].Action[j] == FG_EXP)
553 cout << " with time constant " << Events[i].TC[j] << ")";
555 cout << endl << " }" << endl;
557 // Print notifications
558 if (Events[i].Notify) {
559 if (Events[i].NotifyProperties.size() > 0) {
560 cout << " Notifications" << ":" << endl << " {" << endl;
561 for (unsigned j=0; j<Events[i].NotifyProperties.size();j++) {
563 << Events[i].NotifyProperties[j]->GetRelativeName("/fdm/jsbsim/")
566 cout << " }" << endl;
573 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
574 if (from == 0) cout << "Instantiated: FGScript" << endl;
575 if (from == 1) cout << "Destroyed: FGScript" << endl;
577 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
579 if (debug_lvl & 8 ) { // Runtime state variables
581 if (debug_lvl & 16) { // Sanity checking
583 if (debug_lvl & 64) {
584 if (from == 0) { // Constructor
585 cout << IdSrc << endl;
586 cout << IdHdr << endl;