1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Loads and runs JSBSim scripts.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.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/FGXMLParse.h>
46 #include <initialization/FGTrim.h>
52 static const char *IdSrc = "$Id$";
53 static const char *IdHdr = ID_FGSCRIPT;
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65 FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
67 State = FDMExec->GetState();
68 PropertyManager=FDMExec->GetPropertyManager();
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 for (i=0; i<local_properties.size(); i++) delete local_properties[i];
79 local_properties.clear();
81 for (i=0; i<Events.size(); i++) delete Events[i].Condition;
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 bool FGScript::LoadScript( string script )
91 string aircraft="", initialize="", comparison = "", prop_name="";
92 string notifyPropertyName="";
93 Element *element=0, *run_element=0, *event_element=0;
94 Element *condition_element=0, *set_element=0, *delay_element=0;
95 Element *notify_element = 0L, *notify_property_element = 0L;
96 Element *property_element = 0L;
97 Element *output_element = 0L;
98 Element *input_element = 0L;
100 double dt = 0.0, value = 0.0;
101 struct event *newEvent;
102 FGCondition *newCondition;
104 document = LoadXMLDocument(script);
107 cerr << "File: " << script << " could not be loaded." << endl;
111 // Set up input and output files if specified
113 output_element = document->FindElement("output");
114 input_element = document->FindElement("input");
116 if (document->GetName() != string("runscript")) {
117 cerr << "File: " << script << " is not a script file" << endl;
121 ScriptName = document->GetAttributeValue("name");
123 // First, find "run" element and set delta T
125 run_element = document->FindElement("run");
128 cerr << "No \"run\" element found in script." << endl;
134 StartTime = run_element->GetAttributeValueAsNumber("start");
135 State->Setsim_time(StartTime);
136 EndTime = run_element->GetAttributeValueAsNumber("end");
137 dt = run_element->GetAttributeValueAsNumber("dt");
140 // read aircraft and initialization files
142 element = document->FindElement("use");
144 aircraft = element->GetAttributeValue("aircraft");
145 if (!aircraft.empty()) {
146 result = FDMExec->LoadModel(aircraft);
147 if (!result) return false;
149 cerr << "Aircraft must be specified in use element." << endl;
153 initialize = element->GetAttributeValue("initialize");
154 if (initialize.empty()) {
155 cerr << "Initialization file must be specified in use element." << endl;
160 cerr << "No \"use\" directives in the script file." << endl;
164 // Now, read input spec if given.
165 if (input_element > 0) {
166 FDMExec->GetInput()->Load(input_element);
169 // Now, read output spec if given.
170 if (output_element > 0) {
171 string output_file = output_element->GetAttributeValue("file");
172 if (output_file.empty()) {
173 cerr << "No logging directives file was specified." << endl;
175 FDMExec->SetOutputDirectives(output_file);
179 // Read local property/value declarations
180 property_element = run_element->FindElement("property");
181 while (property_element) {
184 if ( ! property_element->GetAttributeValue("value").empty())
185 value = property_element->GetAttributeValueAsNumber("value");
187 LocalProps *localProp = new LocalProps(value);
188 localProp->title = property_element->GetDataLine();
189 local_properties.push_back(localProp);
191 PropertyManager->Tie(localProp->title, (local_properties.back())->value);
192 property_element = run_element->FindNextElement("property");
195 // Read "events" from script
197 event_element = run_element->FindElement("event");
198 while (event_element) { // event processing
200 // Create the event structure
201 newEvent = new struct event();
203 // Retrieve the event name if given
204 newEvent->Name = event_element->GetAttributeValue("name");
206 // Is this event persistent? That is, does it execute every time the
207 // condition triggers to true, or does it execute as a one-shot event, only?
208 if (event_element->GetAttributeValue("persistent") == string("true")) {
209 newEvent->Persistent = true;
212 // Does this event execute continuously when triggered to true?
213 if (event_element->GetAttributeValue("continuous") == string("true")) {
214 newEvent->Continuous = true;
217 // Process the conditions
218 condition_element = event_element->FindElement("condition");
219 if (condition_element != 0) {
220 newCondition = new FGCondition(condition_element, PropertyManager);
221 newEvent->Condition = newCondition;
223 cerr << "No condition specified in script event " << newEvent->Name << endl;
227 // Is there a delay between the time this event is triggered, and when the event
228 // actions are executed?
230 delay_element = event_element->FindElement("delay");
231 if (delay_element) newEvent->Delay = event_element->FindElementValueAsNumber("delay");
232 else newEvent->Delay = 0.0;
234 // Notify about when this event is triggered?
235 if ((notify_element = event_element->FindElement("notify")) != 0) {
236 newEvent->Notify = true;
237 notify_property_element = notify_element->FindElement("property");
238 while (notify_property_element) {
239 notifyPropertyName = notify_property_element->GetDataLine();
240 if (PropertyManager->GetNode(notifyPropertyName)) {
241 newEvent->NotifyProperties.push_back( PropertyManager->GetNode(notifyPropertyName) );
243 cout << endl << fgred << " Could not find the property named "
244 << notifyPropertyName << " in script" << endl << " \""
245 << ScriptName << "\". This unknown property will not be "
246 << "echoed for notification." << reset << endl;
248 notify_property_element = notify_element->FindNextElement("property");
252 // Read set definitions (these define the actions to be taken when the event is triggered).
253 set_element = event_element->FindElement("set");
254 while (set_element) {
255 prop_name = set_element->GetAttributeValue("name");
256 newEvent->SetParam.push_back( PropertyManager->GetNode(prop_name) );
257 //Todo - should probably do some safety checking here to make sure one or the other
258 //of value or function is specified.
259 if (!set_element->GetAttributeValue("value").empty()) {
260 value = set_element->GetAttributeValueAsNumber("value");
261 newEvent->Functions.push_back((FGFunction*)0L);
262 } else if (set_element->FindElement("function")) {
264 newEvent->Functions.push_back(new FGFunction(PropertyManager, set_element->FindElement("function")));
266 newEvent->SetValue.push_back(value);
267 newEvent->OriginalValue.push_back(0.0);
268 newEvent->newValue.push_back(0.0);
269 newEvent->ValueSpan.push_back(0.0);
270 string tempCompare = set_element->GetAttributeValue("type");
271 if (tempCompare == "FG_DELTA") newEvent->Type.push_back(FG_DELTA);
272 else if (tempCompare == "FG_BOOL") newEvent->Type.push_back(FG_BOOL);
273 else if (tempCompare == "FG_VALUE") newEvent->Type.push_back(FG_VALUE);
274 else newEvent->Type.push_back(FG_VALUE); // DEFAULT
275 tempCompare = set_element->GetAttributeValue("action");
276 if (tempCompare == "FG_RAMP") newEvent->Action.push_back(FG_RAMP);
277 else if (tempCompare == "FG_STEP") newEvent->Action.push_back(FG_STEP);
278 else if (tempCompare == "FG_EXP") newEvent->Action.push_back(FG_EXP);
279 else newEvent->Action.push_back(FG_STEP); // DEFAULT
281 if (!set_element->GetAttributeValue("tc").empty())
282 newEvent->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));
284 newEvent->TC.push_back(1.0); // DEFAULT
286 newEvent->Transiting.push_back(false);
288 set_element = event_element->FindNextElement("set");
290 Events.push_back(*newEvent);
293 event_element = run_element->FindNextElement("event");
298 FGInitialCondition *IC=FDMExec->GetIC();
299 if ( ! IC->Load( initialize )) {
300 cerr << "Initialization unsuccessful" << endl;
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 bool FGScript::RunScript(void)
312 unsigned event_ctr = 0;
314 double currentTime = State->Getsim_time();
315 double newSetValue = 0;
317 if (currentTime > EndTime) return false; //Script done!
319 // Iterate over all events.
320 for (unsigned int ev_ctr=0; ev_ctr < Events.size(); ev_ctr++) {
321 // Determine whether the set of conditional tests for this condition equate
322 // to true and should cause the event to execute. If the conditions evaluate
323 // to true, then the event is triggered. If the event is not persistent,
324 // then this trigger will remain set true. If the event is persistent,
325 // the trigger will reset to false when the condition evaluates to false.
326 if (Events[ev_ctr].Condition->Evaluate()) {
327 if (!Events[ev_ctr].Triggered) {
329 // The conditions are true, do the setting of the desired Event parameters
330 for (i=0; i<Events[ev_ctr].SetValue.size(); i++) {
331 Events[ev_ctr].OriginalValue[i] = Events[ev_ctr].SetParam[i]->getDoubleValue();
332 if (Events[ev_ctr].Functions[i] != 0) { // Parameter should be set to a function value
333 Events[ev_ctr].SetValue[i] = Events[ev_ctr].Functions[i]->GetValue();
335 switch (Events[ev_ctr].Type[i]) {
338 Events[ev_ctr].newValue[i] = Events[ev_ctr].SetValue[i];
341 Events[ev_ctr].newValue[i] = Events[ev_ctr].OriginalValue[i] + Events[ev_ctr].SetValue[i];
344 cerr << "Invalid Type specified" << endl;
347 Events[ev_ctr].StartTime = currentTime + Events[ev_ctr].Delay;
348 Events[ev_ctr].ValueSpan[i] = Events[ev_ctr].newValue[i] - Events[ev_ctr].OriginalValue[i];
349 Events[ev_ctr].Transiting[i] = true;
352 Events[ev_ctr].Triggered = true;
354 } else if (Events[ev_ctr].Persistent) { // If the event is persistent, reset the trigger.
356 Events[ev_ctr].Triggered = false; // Reset the trigger for persistent events
357 Events[ev_ctr].Notified = false; // Also reset the notification flag
360 if ((currentTime >= Events[ev_ctr].StartTime) && Events[ev_ctr].Triggered) {
362 for (i=0; i<Events[ev_ctr].SetValue.size(); i++) {
363 if (Events[ev_ctr].Transiting[i]) {
364 Events[ev_ctr].TimeSpan = currentTime - Events[ev_ctr].StartTime;
365 switch (Events[ev_ctr].Action[i]) {
367 if (Events[ev_ctr].TimeSpan <= Events[ev_ctr].TC[i]) {
368 newSetValue = Events[ev_ctr].TimeSpan/Events[ev_ctr].TC[i] * Events[ev_ctr].ValueSpan[i] + Events[ev_ctr].OriginalValue[i];
370 newSetValue = Events[ev_ctr].newValue[i];
371 if (Events[ev_ctr].Continuous != true) Events[ev_ctr].Transiting[i] = false;
375 newSetValue = Events[ev_ctr].newValue[i];
377 // If this is not a continuous event, reset the transiting flag.
378 // Otherwise, it is known that the event is a continuous event.
379 // Furthermore, if the event is to be determined by a function,
380 // then the function will be continuously calculated.
381 if (Events[ev_ctr].Continuous != true)
382 Events[ev_ctr].Transiting[i] = false;
383 else if (Events[ev_ctr].Functions[i] != 0)
384 newSetValue = Events[ev_ctr].Functions[i]->GetValue();
388 newSetValue = (1 - exp( -Events[ev_ctr].TimeSpan/Events[ev_ctr].TC[i] )) * Events[ev_ctr].ValueSpan[i] + Events[ev_ctr].OriginalValue[i];
391 cerr << "Invalid Action specified" << endl;
394 Events[ev_ctr].SetParam[i]->setDoubleValue(newSetValue);
398 // Print notification values after setting them
399 if (Events[ev_ctr].Notify && !Events[ev_ctr].Notified) {
400 cout << endl << " Event " << event_ctr << " (" << Events[ev_ctr].Name << ")"
401 << " executed at time: " << currentTime << endl;
402 for (j=0; j<Events[ev_ctr].NotifyProperties.size();j++) {
403 cout << " " << Events[ev_ctr].NotifyProperties[j]->GetName()
404 << " = " << Events[ev_ctr].NotifyProperties[j]->getDoubleValue() << endl;
407 Events[ev_ctr].Notified = true;
417 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 // The bitmasked value choices are as follows:
419 // unset: In this case (the default) JSBSim would only print
420 // out the normally expected messages, essentially echoing
421 // the config files as they are read. If the environment
422 // variable is not set, debug_lvl is set to 1 internally
423 // 0: This requests JSBSim not to output any messages
425 // 1: This value explicity requests the normal JSBSim
427 // 2: This value asks for a message to be printed out when
428 // a class is instantiated
429 // 4: When this value is set, a message is displayed when a
430 // FGModel object executes its Run() method
431 // 8: When this value is set, various runtime state variables
432 // are printed out periodically
433 // 16: When set various parameters are sanity checked and
434 // a message is printed out when they go out of bounds
436 void FGScript::Debug(int from)
438 if (debug_lvl <= 0) return;
440 if (debug_lvl & 1) { // Standard console startup message output
441 if (from == 0) { // Constructor
442 } else if (from == 3) {
443 } else if (from == 4) { // print out script data
445 cout << "Script: \"" << ScriptName << "\"" << endl;
446 cout << " begins at " << StartTime << " seconds and runs to " << EndTime
447 << " seconds with dt = " << State->Getdt() << endl;
450 for (unsigned i=0; i<Events.size(); i++) {
451 cout << "Event " << i;
452 if (!Events[i].Name.empty()) cout << " (" << Events[i].Name << ")";
455 if (Events[i].Persistent)
456 cout << " " << "Always executes";
458 cout << " " << "Executes once";
460 Events[i].Condition->PrintCondition();
462 cout << endl << " Actions taken:" << endl << " {";
463 for (unsigned j=0; j<Events[i].SetValue.size(); j++) {
464 if (Events[i].SetValue[j] == 0.0 && Events[i].Functions[j] != 0L) {
465 if (Events[i].SetParam[j] == 0) {
466 cerr << fgred << highint << endl
467 << " An attempt has been made to access a non-existent property" << endl
468 << " in this event. Please check the property names used, spelling, etc."
472 cout << endl << " set " << Events[i].SetParam[j]->GetName()
473 << " to function value";
475 if (Events[i].SetParam[j] == 0) {
476 cerr << fgred << highint << endl
477 << " An attempt has been made to access a non-existent property" << endl
478 << " in this event. Please check the property names used, spelling, etc."
482 cout << endl << " set " << Events[i].SetParam[j]->GetName()
483 << " to " << Events[i].SetValue[j];
486 switch (Events[i].Type[j]) {
489 cout << " (constant";
495 cout << " (unspecified type";
498 switch (Events[i].Action[j]) {
503 cout << " via step)";
506 cout << " via exponential approach";
509 cout << " via unspecified action)";
512 if (Events[i].Action[j] == FG_RAMP || Events[i].Action[j] == FG_EXP)
513 cout << " with time constant " << Events[i].TC[j] << ")";
515 cout << endl << " }" << endl << endl;
520 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
521 if (from == 0) cout << "Instantiated: FGScript" << endl;
522 if (from == 1) cout << "Destroyed: FGScript" << endl;
524 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
526 if (debug_lvl & 8 ) { // Runtime state variables
528 if (debug_lvl & 16) { // Sanity checking
530 if (debug_lvl & 64) {
531 if (from == 0) { // Constructor
532 cout << IdSrc << endl;
533 cout << IdHdr << endl;