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