]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGCondition.cpp
std namespace fix
[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 "FGPropertyValue.h"
39 #include "input_output/FGXMLElement.h"
40 #include "input_output/FGPropertyManager.h"
41 #include <iostream>
42 #include <cstdlib>
43
44 using namespace std;
45
46 namespace JSBSim {
47
48 static const char *IdSrc = "$Id: FGCondition.cpp,v 1.14 2011/04/05 20:20:21 andgi Exp $";
49 static const char *IdHdr = ID_CONDITION;
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55 string FGCondition::indent = "        ";
56
57 // This constructor is called when tests are inside an element
58 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
59   PropertyManager(PropertyManager), isGroup(true)
60 {
61   string property1, property2, logic;
62   Element* condition_element;
63
64   InitializeConditionals();
65
66   TestParam1  = TestParam2 = 0L;
67   TestValue   = 0.0;
68   Comparison  = ecUndef;
69   Logic       = elUndef;
70   conditions.clear();
71
72   logic = element->GetAttributeValue("logic");
73   if (!logic.empty()) {
74     if (logic == "OR") Logic = eOR;
75     else if (logic == "AND") Logic = eAND;
76     else { // error
77       cerr << "Unrecognized LOGIC token " << logic << endl;
78     }
79   } else {
80     Logic = eAND; // default
81   }
82
83   condition_element = element->GetElement();
84   while (condition_element) {
85     conditions.push_back(new FGCondition(condition_element, PropertyManager));
86     condition_element = element->GetNextElement();
87   }
88   for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
89     string data = element->GetDataLine(i);
90     conditions.push_back(new FGCondition(data, PropertyManager));
91   }
92
93   Debug(0);
94 }
95
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 // This constructor is called when there are no nested test groups inside the
98 // condition
99
100 FGCondition::FGCondition(const string& test, FGPropertyManager* PropertyManager) :
101   PropertyManager(PropertyManager), isGroup(false)
102 {
103   string property1, property2, compare_string;
104   vector <string> test_strings;
105
106   InitializeConditionals();
107
108   TestParam1  = TestParam2 = 0L;
109   TestValue   = 0.0;
110   Comparison  = ecUndef;
111   Logic       = elUndef;
112   conditions.clear();
113
114   test_strings = split(test, ' ');
115   if (test_strings.size() == 3) {
116     property1 = test_strings[0];
117     conditional = test_strings[1];
118     property2 = test_strings[2];
119   } else {
120     cerr << endl << "  Conditional test is invalid: \"" << test
121          << "\" has " << test_strings.size() << " elements in the "
122          << "test condition." << endl;
123     exit(-1);
124   }
125
126   FGPropertyManager *node = PropertyManager->GetNode(property1, false);
127   if (node) {
128     TestParam1 = new FGPropertyValue(node);
129   } else {
130     TestParam1 = new FGPropertyValue(property1, PropertyManager);
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     node = PropertyManager->GetNode(property2, false);
140     if (node) {
141       TestParam2 = new FGPropertyValue(node);
142     } else {
143       TestParam2 = new FGPropertyValue(property2, PropertyManager);
144     }
145   }
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 void FGCondition::InitializeConditionals(void)
151 {
152   mComparison["EQ"] = eEQ;
153   mComparison["NE"] = eNE;
154   mComparison["GT"] = eGT;
155   mComparison["GE"] = eGE;
156   mComparison["LT"] = eLT;
157   mComparison["LE"] = eLE;
158   mComparison["eq"] = eEQ;
159   mComparison["ne"] = eNE;
160   mComparison["gt"] = eGT;
161   mComparison["ge"] = eGE;
162   mComparison["lt"] = eLT;
163   mComparison["le"] = eLE;
164   mComparison["=="] = eEQ;
165   mComparison["!="] = eNE;
166   mComparison[">"]  = eGT;
167   mComparison[">="] = eGE;
168   mComparison["<"]  = eLT;
169   mComparison["<="] = eLE;
170 }
171
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173
174 FGCondition::~FGCondition(void)
175 {
176   delete TestParam1;
177   delete TestParam2;
178   for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
179
180   Debug(1);
181 }
182
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184
185 bool FGCondition::Evaluate(void )
186 {
187   bool pass = false;
188   double compareValue;
189
190   if (TestParam1 == 0L) {
191
192     if (Logic == eAND) {
193
194       pass = true;
195       for (unsigned int i=0; i<conditions.size(); i++) {
196         if (!conditions[i]->Evaluate()) pass = false;
197       }
198
199     } else { // Logic must be eOR
200
201       pass = false;
202       for (unsigned int i=0; i<conditions.size(); i++) {
203         if (conditions[i]->Evaluate()) pass = true;
204       }
205
206     }
207
208   } else {
209
210     if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
211     else compareValue = TestValue;
212
213     switch (Comparison) {
214     case ecUndef:
215       cerr << "Undefined comparison operator." << endl;
216       break;
217     case eEQ:
218       pass = TestParam1->getDoubleValue() == compareValue;
219       break;
220     case eNE:
221       pass = TestParam1->getDoubleValue() != compareValue;
222       break;
223     case eGT:
224       pass = TestParam1->getDoubleValue() > compareValue;
225       break;
226     case eGE:
227       pass = TestParam1->getDoubleValue() >= compareValue;
228       break;
229     case eLT:
230       pass = TestParam1->getDoubleValue() < compareValue;
231       break;
232     case eLE:
233       pass = TestParam1->getDoubleValue() <= compareValue;
234       break;
235     default:
236      cerr << "Unknown comparison operator." << endl;
237     }
238   }
239
240   return pass;
241 }
242
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244
245 void FGCondition::PrintCondition(void )
246 {
247   string scratch;
248
249   if (isGroup) {
250     switch(Logic) {
251     case (elUndef):
252       scratch = " UNSET";
253       cerr << "unset logic for test condition" << endl;
254       break;
255     case (eAND):
256       scratch = " if all of the following are true:";
257       break;
258     case (eOR):
259       scratch = " if any of the following are true:";
260       break;
261     default:
262       scratch = " UNKNOWN";
263       cerr << "Unknown logic for test condition" << endl;
264     }
265
266     cout << scratch << endl;
267     for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
268
269   } else {
270     if (TestParam2 != 0L)
271       cout << "    " << TestParam1->GetName() << " "
272                          << conditional << " "
273                          << TestParam2->GetName();
274     else
275       cout << "    " << TestParam1->GetName() << " "
276                      << conditional << " " << TestValue;
277   }
278 }
279
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 //    The bitmasked value choices are as follows:
282 //    unset: In this case (the default) JSBSim would only print
283 //       out the normally expected messages, essentially echoing
284 //       the config files as they are read. If the environment
285 //       variable is not set, debug_lvl is set to 1 internally
286 //    0: This requests JSBSim not to output any messages
287 //       whatsoever.
288 //    1: This value explicity requests the normal JSBSim
289 //       startup messages
290 //    2: This value asks for a message to be printed out when
291 //       a class is instantiated
292 //    4: When this value is set, a message is displayed when a
293 //       FGModel object executes its Run() method
294 //    8: When this value is set, various runtime state variables
295 //       are printed out periodically
296 //    16: When set various parameters are sanity checked and
297 //       a message is printed out when they go out of bounds
298
299 void FGCondition::Debug(int from)
300 {
301   if (debug_lvl <= 0) return;
302
303   if (debug_lvl & 1) { // Standard console startup message output
304     if (from == 0) { // Constructor
305
306     }
307   }
308   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
309     if (from == 0) cout << "Instantiated: FGCondition" << endl;
310     if (from == 1) cout << "Destroyed:    FGCondition" << endl;
311   }
312   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
313   }
314   if (debug_lvl & 8 ) { // Runtime state variables
315   }
316   if (debug_lvl & 16) { // Sanity checking
317   }
318   if (debug_lvl & 64) {
319     if (from == 0) { // Constructor
320       cout << IdSrc << endl;
321       cout << IdHdr << endl;
322     }
323   }
324 }
325
326 } //namespace JSBSim
327