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