]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGCondition.cpp
Attached patches remove BORLANDC, and hence SG_MATH_EXCEPTION_CLASH and SG_INCOM
[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(new 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(new 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   for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
155
156   Debug(1);
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 bool FGCondition::Evaluate(void )
162 {
163   bool pass = false;
164   double compareValue;
165
166   if (TestParam1 == 0L) {
167
168     if (Logic == eAND) {
169
170       pass = true;
171       for (unsigned int i=0; i<conditions.size(); i++) {
172         if (!conditions[i]->Evaluate()) pass = false;
173       }
174
175     } else { // Logic must be eOR
176
177       pass = false;
178       for (unsigned int i=0; i<conditions.size(); i++) {
179         if (conditions[i]->Evaluate()) pass = true;
180       }
181
182     }
183
184   } else {
185
186     if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
187     else compareValue = TestValue;
188
189     switch (Comparison) {
190     case ecUndef:
191       cerr << "Undefined comparison operator." << endl;
192       break;
193     case eEQ:
194       pass = TestParam1->getDoubleValue() == compareValue;
195       break;
196     case eNE:
197       pass = TestParam1->getDoubleValue() != compareValue;
198       break;
199     case eGT:
200       pass = TestParam1->getDoubleValue() > compareValue;
201       break;
202     case eGE:
203       pass = TestParam1->getDoubleValue() >= compareValue;
204       break;
205     case eLT:
206       pass = TestParam1->getDoubleValue() < compareValue;
207       break;
208     case eLE:
209       pass = TestParam1->getDoubleValue() <= compareValue;
210       break;
211     default:
212      cerr << "Unknown comparison operator." << endl;
213     }
214   }
215
216   return pass;
217 }
218
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
221 void FGCondition::PrintCondition(void )
222 {
223   string scratch;
224
225   if (isGroup) {
226     switch(Logic) {
227     case (elUndef):
228       scratch = " UNSET";
229       cerr << "unset logic for test condition" << endl;
230       break;
231     case (eAND):
232       scratch = " if all of the following are true:";
233       break;
234     case (eOR):
235       scratch = " if any of the following are true:";
236       break;
237     default:
238       scratch = " UNKNOWN";
239       cerr << "Unknown logic for test condition" << endl;
240     }
241
242     cout << scratch << endl;
243     for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
244
245   } else {
246     if (TestParam2 != 0L)
247       cout << "    " << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
248     else
249       cout << "    " << TestParam1->GetName() << " " << conditional << " " << TestValue;
250   }
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 //    The bitmasked value choices are as follows:
255 //    unset: In this case (the default) JSBSim would only print
256 //       out the normally expected messages, essentially echoing
257 //       the config files as they are read. If the environment
258 //       variable is not set, debug_lvl is set to 1 internally
259 //    0: This requests JSBSim not to output any messages
260 //       whatsoever.
261 //    1: This value explicity requests the normal JSBSim
262 //       startup messages
263 //    2: This value asks for a message to be printed out when
264 //       a class is instantiated
265 //    4: When this value is set, a message is displayed when a
266 //       FGModel object executes its Run() method
267 //    8: When this value is set, various runtime state variables
268 //       are printed out periodically
269 //    16: When set various parameters are sanity checked and
270 //       a message is printed out when they go out of bounds
271
272 void FGCondition::Debug(int from)
273 {
274   if (debug_lvl <= 0) return;
275
276   if (debug_lvl & 1) { // Standard console startup message output
277     if (from == 0) { // Constructor
278
279     }
280   }
281   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
282     if (from == 0) cout << "Instantiated: FGCondition" << endl;
283     if (from == 1) cout << "Destroyed:    FGCondition" << endl;
284   }
285   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
286   }
287   if (debug_lvl & 8 ) { // Runtime state variables
288   }
289   if (debug_lvl & 16) { // Sanity checking
290   }
291   if (debug_lvl & 64) {
292     if (from == 0) { // Constructor
293       cout << IdSrc << endl;
294       cout << IdHdr << endl;
295     }
296   }
297 }
298
299 } //namespace JSBSim
300