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