]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGCondition.cpp
PAtch by Andreas Gaeb to eliminate NaN's in the location code
[flightgear.git] / src / FDM / JSBSim / math / FGCondition.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGCondition.cpp
4  Author:       Jon S. Berndt
5  Date started: 1/2/2003
6
7  -------------- Copyright (C) 2003 Jon S. Berndt (jon@jsbsim.org) --------------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 HISTORY
27 --------------------------------------------------------------------------------
28
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 COMMENTS, REFERENCES,  and NOTES
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 INCLUDES
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37 #include "FGCondition.h"
38 #include "input_output/FGXMLElement.h"
39 #include "input_output/FGPropertyManager.h"
40 #include <iostream>
41 #include <cstdlib>
42
43 using namespace std;
44
45 namespace JSBSim {
46
47 static const char *IdSrc = "$Id: FGCondition.cpp,v 1.13 2010/07/14 05:50:40 ehofman Exp $";
48 static const char *IdHdr = ID_CONDITION;
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 CLASS IMPLEMENTATION
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 string FGCondition::indent = "        ";
55
56 // This constructor is called when tests are inside an element
57 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
58   PropertyManager(PropertyManager), isGroup(true)
59 {
60   string property1, property2, logic;
61   Element* condition_element;
62
63   InitializeConditionals();
64
65   TestParam1  = TestParam2 = 0L;
66   TestValue   = 0.0;
67   Comparison  = ecUndef;
68   Logic       = elUndef;
69   conditions.clear();
70
71   logic = element->GetAttributeValue("logic");
72   if (!logic.empty()) {
73     if (logic == "OR") Logic = eOR;
74     else if (logic == "AND") Logic = eAND;
75     else { // error
76       cerr << "Unrecognized LOGIC token " << logic << endl;
77     }
78   } else {
79     Logic = eAND; // default
80   }
81
82   condition_element = element->GetElement();
83   while (condition_element) {
84     conditions.push_back(new FGCondition(condition_element, PropertyManager));
85     condition_element = element->GetNextElement();
86   }
87   for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
88     string data = element->GetDataLine(i);
89     conditions.push_back(new FGCondition(data, PropertyManager));
90   }
91
92   Debug(0);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 // This constructor is called when there are no nested test groups inside the
97 // condition
98
99 FGCondition::FGCondition(const string& test, FGPropertyManager* PropertyManager) :
100   PropertyManager(PropertyManager), isGroup(false)
101 {
102   string property1, property2, compare_string;
103   vector <string> test_strings;
104
105   InitializeConditionals();
106
107   TestParam1  = TestParam2 = 0L;
108   TestValue   = 0.0;
109   Comparison  = ecUndef;
110   Logic       = elUndef;
111   conditions.clear();
112
113   test_strings = split(test, ' ');
114   if (test_strings.size() == 3) {
115     property1 = test_strings[0];
116     conditional = test_strings[1];
117     property2 = test_strings[2];
118   } else {
119     cerr << endl << "  Conditional test is invalid: \"" << test
120          << "\" has " << test_strings.size() << " elements in the "
121          << "test condition." << endl;
122     exit(-1);
123   }
124
125   TestParam1 = PropertyManager->GetNode(property1, false);
126   if (!TestParam1) {
127       cerr << fgred << "  In condition: " << test << ". Unknown property "
128            << property1 << " referenced." << endl
129            << "Creating property.  Check usage." << reset << endl;
130       TestParam1 = PropertyManager->GetNode(property1, true);
131   }
132   Comparison = mComparison[conditional];
133   if (Comparison == ecUndef) {
134         throw("Comparison operator: \""+conditional+"\" does not exist.  Please check the conditional.");
135   }
136   if (is_number(property2)) {
137     TestValue = atof(property2.c_str());
138   } else {
139     TestParam2 = PropertyManager->GetNode(property2, false);
140     if (!TestParam2) {
141         cerr << fgred << "  In condition: " << test << ". Unknown property "
142              << property2 << " referenced." << endl
143              << "Creating property.  Check usage." << reset << endl;
144         TestParam2 = PropertyManager->GetNode(property2, true);
145     }
146   }
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 void FGCondition::InitializeConditionals(void)
152 {
153   mComparison["EQ"] = eEQ;
154   mComparison["NE"] = eNE;
155   mComparison["GT"] = eGT;
156   mComparison["GE"] = eGE;
157   mComparison["LT"] = eLT;
158   mComparison["LE"] = eLE;
159   mComparison["eq"] = eEQ;
160   mComparison["ne"] = eNE;
161   mComparison["gt"] = eGT;
162   mComparison["ge"] = eGE;
163   mComparison["lt"] = eLT;
164   mComparison["le"] = eLE;
165   mComparison["=="] = eEQ;
166   mComparison["!="] = eNE;
167   mComparison[">"]  = eGT;
168   mComparison[">="] = eGE;
169   mComparison["<"]  = eLT;
170   mComparison["<="] = eLE;
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 FGCondition::~FGCondition(void)
176 {
177   for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
178
179   Debug(1);
180 }
181
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184 bool FGCondition::Evaluate(void )
185 {
186   bool pass = false;
187   double compareValue;
188
189   if (TestParam1 == 0L) {
190
191     if (Logic == eAND) {
192
193       pass = true;
194       for (unsigned int i=0; i<conditions.size(); i++) {
195         if (!conditions[i]->Evaluate()) pass = false;
196       }
197
198     } else { // Logic must be eOR
199
200       pass = false;
201       for (unsigned int i=0; i<conditions.size(); i++) {
202         if (conditions[i]->Evaluate()) pass = true;
203       }
204
205     }
206
207   } else {
208
209     if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
210     else compareValue = TestValue;
211
212     switch (Comparison) {
213     case ecUndef:
214       cerr << "Undefined comparison operator." << endl;
215       break;
216     case eEQ:
217       pass = TestParam1->getDoubleValue() == compareValue;
218       break;
219     case eNE:
220       pass = TestParam1->getDoubleValue() != compareValue;
221       break;
222     case eGT:
223       pass = TestParam1->getDoubleValue() > compareValue;
224       break;
225     case eGE:
226       pass = TestParam1->getDoubleValue() >= compareValue;
227       break;
228     case eLT:
229       pass = TestParam1->getDoubleValue() < compareValue;
230       break;
231     case eLE:
232       pass = TestParam1->getDoubleValue() <= compareValue;
233       break;
234     default:
235      cerr << "Unknown comparison operator." << endl;
236     }
237   }
238
239   return pass;
240 }
241
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243
244 void FGCondition::PrintCondition(void )
245 {
246   string scratch;
247
248   if (isGroup) {
249     switch(Logic) {
250     case (elUndef):
251       scratch = " UNSET";
252       cerr << "unset logic for test condition" << endl;
253       break;
254     case (eAND):
255       scratch = " if all of the following are true:";
256       break;
257     case (eOR):
258       scratch = " if any of the following are true:";
259       break;
260     default:
261       scratch = " UNKNOWN";
262       cerr << "Unknown logic for test condition" << endl;
263     }
264
265     cout << scratch << endl;
266     for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
267
268   } else {
269     if (TestParam2 != 0L)
270       cout << "    " << TestParam1->GetRelativeName() << " "
271                          << conditional << " "
272                          << TestParam2->GetRelativeName();
273     else
274       cout << "    " << TestParam1->GetRelativeName() << " "
275                      << conditional << " " << TestValue;
276   }
277 }
278
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 //    The bitmasked value choices are as follows:
281 //    unset: In this case (the default) JSBSim would only print
282 //       out the normally expected messages, essentially echoing
283 //       the config files as they are read. If the environment
284 //       variable is not set, debug_lvl is set to 1 internally
285 //    0: This requests JSBSim not to output any messages
286 //       whatsoever.
287 //    1: This value explicity requests the normal JSBSim
288 //       startup messages
289 //    2: This value asks for a message to be printed out when
290 //       a class is instantiated
291 //    4: When this value is set, a message is displayed when a
292 //       FGModel object executes its Run() method
293 //    8: When this value is set, various runtime state variables
294 //       are printed out periodically
295 //    16: When set various parameters are sanity checked and
296 //       a message is printed out when they go out of bounds
297
298 void FGCondition::Debug(int from)
299 {
300   if (debug_lvl <= 0) return;
301
302   if (debug_lvl & 1) { // Standard console startup message output
303     if (from == 0) { // Constructor
304
305     }
306   }
307   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
308     if (from == 0) cout << "Instantiated: FGCondition" << endl;
309     if (from == 1) cout << "Destroyed:    FGCondition" << endl;
310   }
311   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
312   }
313   if (debug_lvl & 8 ) { // Runtime state variables
314   }
315   if (debug_lvl & 16) { // Sanity checking
316   }
317   if (debug_lvl & 64) {
318     if (from == 0) { // Constructor
319       cout << IdSrc << endl;
320       cout << IdHdr << endl;
321     }
322   }
323 }
324
325 } //namespace JSBSim
326