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 <simgear/compiler.h>
46 # include STL_IOSTREAM
47 # include STL_ITERATOR
49 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
50 # include <iostream.h>
58 #include <input_output/FGXMLParse.h>
59 #include <initialization/FGTrim.h>
63 static const char *IdSrc = "$Id$";
64 static const char *IdHdr = ID_FGSCRIPT;
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
78 State = FDMExec->GetState();
79 PropertyManager=FDMExec->GetPropertyManager();
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 for (i=0; i<local_properties.size(); i++)
89 PropertyManager->Untie(local_properties[i]->title);
91 for (i=0; i<local_properties.size(); i++)
92 delete local_properties[i];
94 local_properties.clear();
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 bool FGScript::LoadScript( string script )
102 string aircraft="", initialize="", comparison = "", prop_name="";
103 string notifyPropertyName="";
104 Element *element=0, *run_element=0, *event_element=0;
105 Element *condition_element=0, *set_element=0, *delay_element=0;
106 Element *notify_element = 0L, *notify_property_element = 0L;
107 Element *property_element = 0L;
109 double dt = 0.0, value = 0.0;
110 struct event *newEvent;
111 FGCondition *newCondition;
113 document = LoadXMLDocument(script);
115 if (document->GetName() != string("runscript")) {
116 cerr << "File: " << script << " is not a script file" << endl;
120 ScriptName = document->GetAttributeValue("name");
122 // First, find "run" element and set delta T
124 run_element = document->FindElement("run");
127 cerr << "No \"run\" element found in script." << endl;
133 StartTime = run_element->GetAttributeValueAsNumber("start");
134 State->Setsim_time(StartTime);
135 EndTime = run_element->GetAttributeValueAsNumber("end");
136 dt = run_element->GetAttributeValueAsNumber("dt");
139 // read aircraft and initialization files
141 element = document->FindElement("use");
143 aircraft = element->GetAttributeValue("aircraft");
144 if (!aircraft.empty()) {
145 result = FDMExec->LoadModel(aircraft);
146 if (!result) return false;
148 cerr << "Aircraft must be specified in use element." << endl;
152 initialize = element->GetAttributeValue("initialize");
153 if (initialize.empty()) {
154 cerr << "Initialization file must be specified in use element." << endl;
159 cerr << "No \"use\" directives in the script file." << endl;
163 // Read local property declarations
164 property_element = run_element->FindElement("property");
165 while (property_element) {
166 LocalProps *localProp = new LocalProps();
167 localProp->title = property_element->GetDataLine();
168 local_properties.push_back(localProp);
169 PropertyManager->Tie(localProp->title, (local_properties.back())->value);
170 property_element = run_element->FindNextElement("property");
173 // Read "events" from script
175 event_element = run_element->FindElement("event");
176 while (event_element) { // event processing
178 // Create the event structure
179 newEvent = new struct event();
181 // Retrieve the event name if given
182 newEvent->Name = event_element->GetAttributeValue("name");
184 // Is this event persistent? That is, does it execute repeatedly as long as the
185 // condition is true, or does it execute as a one-shot event, only?
186 if (event_element->GetAttributeValue("persistent") == string("true")) {
187 newEvent->Persistent = true;
190 // Process the conditions
191 condition_element = event_element->FindElement("condition");
192 if (condition_element != 0) {
193 newCondition = new FGCondition(condition_element, PropertyManager);
194 newEvent->Condition = newCondition;
196 cerr << "No condition specified in script event " << newEvent->Name << endl;
200 // Is there a delay between the time this event is triggered, and when the event
201 // actions are executed?
203 delay_element = event_element->FindElement("delay");
204 if (delay_element) newEvent->Delay = event_element->FindElementValueAsNumber("delay");
205 else newEvent->Delay = 0.0;
207 // Notify about when this event is triggered?
208 if ((notify_element = event_element->FindElement("notify")) != 0) {
209 newEvent->Notify = true;
210 notify_property_element = notify_element->FindElement("property");
211 while (notify_property_element) {
212 notifyPropertyName = notify_property_element->GetDataLine();
213 newEvent->NotifyProperties.push_back( PropertyManager->GetNode(notifyPropertyName) );
214 notify_property_element = notify_element->FindNextElement("property");
218 // Read set definitions (these define the actions to be taken when the event is triggered).
219 set_element = event_element->FindElement("set");
220 while (set_element) {
221 prop_name = set_element->GetAttributeValue("name");
222 newEvent->SetParam.push_back( PropertyManager->GetNode(prop_name) );
223 value = set_element->GetAttributeValueAsNumber("value");
224 newEvent->SetValue.push_back(value);
225 newEvent->OriginalValue.push_back(0.0);
226 newEvent->newValue.push_back(0.0);
227 newEvent->ValueSpan.push_back(0.0);
228 string tempCompare = set_element->GetAttributeValue("type");
229 if (tempCompare == "FG_DELTA") newEvent->Type.push_back(FG_DELTA);
230 else if (tempCompare == "FG_BOOL") newEvent->Type.push_back(FG_BOOL);
231 else if (tempCompare == "FG_VALUE") newEvent->Type.push_back(FG_VALUE);
232 else newEvent->Type.push_back(FG_VALUE); // DEFAULT
233 tempCompare = set_element->GetAttributeValue("action");
234 if (tempCompare == "FG_RAMP") newEvent->Action.push_back(FG_RAMP);
235 else if (tempCompare == "FG_STEP") newEvent->Action.push_back(FG_STEP);
236 else if (tempCompare == "FG_EXP") newEvent->Action.push_back(FG_EXP);
237 else newEvent->Action.push_back(FG_STEP); // DEFAULT
239 if (!set_element->GetAttributeValue("tc").empty())
240 newEvent->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));
242 newEvent->TC.push_back(1.0); // DEFAULT
244 newEvent->Transiting.push_back(false);
246 set_element = event_element->FindNextElement("set");
248 Events.push_back(*newEvent);
249 event_element = run_element->FindNextElement("event");
254 FGInitialCondition *IC=FDMExec->GetIC();
255 if ( ! IC->Load( initialize )) {
256 cerr << "Initialization unsuccessful" << endl;
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 bool FGScript::RunScript(void)
267 vector <struct event>::iterator iEvent = Events.begin();
269 unsigned event_ctr = 0;
271 double currentTime = State->Getsim_time();
272 double newSetValue = 0;
274 if (currentTime > EndTime) return false; //Script done!
276 // Iterate over all events.
277 while (iEvent < Events.end()) {
278 iEvent->PrevTriggered = iEvent->Triggered;
279 // Determine whether the set of conditional tests for this condition equate
280 // to true and should cause the event to execute.
281 if (iEvent->Condition->Evaluate()) {
282 if (!iEvent->Triggered) {
284 // The conditions are true, do the setting of the desired Event parameters
285 for (i=0; i<iEvent->SetValue.size(); i++) {
286 iEvent->OriginalValue[i] = iEvent->SetParam[i]->getDoubleValue();
287 switch (iEvent->Type[i]) {
290 iEvent->newValue[i] = iEvent->SetValue[i];
293 iEvent->newValue[i] = iEvent->OriginalValue[i] + iEvent->SetValue[i];
296 cerr << "Invalid Type specified" << endl;
299 iEvent->StartTime = currentTime + iEvent->Delay;
300 iEvent->ValueSpan[i] = iEvent->newValue[i] - iEvent->OriginalValue[i];
301 iEvent->Transiting[i] = true;
304 iEvent->Triggered = true;
305 } else if (iEvent->Persistent) {
306 iEvent->Triggered = false; // Reset the trigger for persistent events
307 iEvent->Notified = false; // Also reset the notification flag
310 if ((currentTime >= iEvent->StartTime) && iEvent->Triggered) {
312 for (i=0; i<iEvent->SetValue.size(); i++) {
313 if (iEvent->Transiting[i]) {
314 iEvent->TimeSpan = currentTime - iEvent->StartTime;
315 switch (iEvent->Action[i]) {
317 if (iEvent->TimeSpan <= iEvent->TC[i]) {
318 newSetValue = iEvent->TimeSpan/iEvent->TC[i] * iEvent->ValueSpan[i] + iEvent->OriginalValue[i];
320 newSetValue = iEvent->newValue[i];
321 iEvent->Transiting[i] = false;
325 newSetValue = iEvent->newValue[i];
326 iEvent->Transiting[i] = false;
329 newSetValue = (1 - exp( -iEvent->TimeSpan/iEvent->TC[i] )) * iEvent->ValueSpan[i] + iEvent->OriginalValue[i];
332 cerr << "Invalid Action specified" << endl;
335 iEvent->SetParam[i]->setDoubleValue(newSetValue);
339 // Print notification values after setting them
340 if (iEvent->Notify && !iEvent->Notified) {
341 cout << endl << " Event " << event_ctr << " (" << iEvent->Name << ")"
342 << " executed at time: " << currentTime << endl;
343 for (j=0; j<iEvent->NotifyProperties.size();j++) {
344 cout << " " << iEvent->NotifyProperties[j]->GetName()
345 << " = " << iEvent->NotifyProperties[j]->getDoubleValue() << endl;
348 iEvent->Notified = true;
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 // The bitmasked value choices are as follows:
361 // unset: In this case (the default) JSBSim would only print
362 // out the normally expected messages, essentially echoing
363 // the config files as they are read. If the environment
364 // variable is not set, debug_lvl is set to 1 internally
365 // 0: This requests JSBSim not to output any messages
367 // 1: This value explicity requests the normal JSBSim
369 // 2: This value asks for a message to be printed out when
370 // a class is instantiated
371 // 4: When this value is set, a message is displayed when a
372 // FGModel object executes its Run() method
373 // 8: When this value is set, various runtime state variables
374 // are printed out periodically
375 // 16: When set various parameters are sanity checked and
376 // a message is printed out when they go out of bounds
378 void FGScript::Debug(int from)
380 if (debug_lvl <= 0) return;
382 if (debug_lvl & 1) { // Standard console startup message output
383 if (from == 0) { // Constructor
384 } else if (from == 3) {
385 } else if (from == 4) { // print out script data
387 cout << "Script: \"" << ScriptName << "\"" << endl;
388 cout << " begins at " << StartTime << " seconds and runs to " << EndTime
389 << " seconds with dt = " << State->Getdt() << endl;
392 for (unsigned i=0; i<Events.size(); i++) {
393 cout << "Event " << i;
394 if (!Events[i].Name.empty()) cout << " (" << Events[i].Name << ")";
397 if (Events[i].Persistent)
398 cout << " " << "Always executes";
400 cout << " " << "Executes once";
402 Events[i].Condition->PrintCondition();
404 cout << endl << " Actions taken:" << endl << " {";
405 for (unsigned j=0; j<Events[i].SetValue.size(); j++) {
406 cout << endl << " set " << Events[i].SetParam[j]->GetName()
407 << " to " << Events[i].SetValue[j];
409 switch (Events[i].Type[j]) {
412 cout << " (constant";
418 cout << " (unspecified type";
421 switch (Events[i].Action[j]) {
426 cout << " via step)";
429 cout << " via exponential approach";
432 cout << " via unspecified action)";
435 if (Events[i].Action[j] == FG_RAMP || Events[i].Action[j] == FG_EXP)
436 cout << " with time constant " << Events[i].TC[j] << ")";
438 cout << endl << " }" << endl << endl;
443 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
444 if (from == 0) cout << "Instantiated: FGScript" << endl;
445 if (from == 1) cout << "Destroyed: FGScript" << endl;
447 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
449 if (debug_lvl & 8 ) { // Runtime state variables
451 if (debug_lvl & 16) { // Sanity checking
453 if (debug_lvl & 64) {
454 if (from == 0) { // Constructor
455 cout << IdSrc << endl;
456 cout << IdHdr << endl;