1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGCondition.cpp
7 -------------- Copyright (C) 2003 Jon S. Berndt (jsb@hal-pc.org) --------------
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
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
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.
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.
27 --------------------------------------------------------------------------------
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 COMMENTS, REFERENCES, and NOTES
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 #include "FGCondition.h"
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_CONDITION;
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 string FGCondition::indent = " ";
51 // This constructor is called when tests are inside an element
52 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
53 PropertyManager(PropertyManager), isGroup(true)
55 string property1, property2, logic;
56 Element* condition_element;
58 InitializeConditionals();
60 TestParam1 = TestParam2 = 0L;
66 logic = element->GetAttributeValue("logic");
68 if (logic == "OR") Logic = eOR;
69 else if (logic == "AND") Logic = eAND;
71 cerr << "Unrecognized LOGIC token " << logic << endl;
74 Logic = eAND; // default
77 condition_element = element->GetElement();
78 while (condition_element) {
79 conditions.push_back(new FGCondition(condition_element, PropertyManager));
80 condition_element = element->GetNextElement();
82 for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
83 string data = element->GetDataLine(i);
84 conditions.push_back(new FGCondition(data, PropertyManager));
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 // This constructor is called when there are no nested test groups inside the
94 FGCondition::FGCondition(string test, FGPropertyManager* PropertyManager) :
95 PropertyManager(PropertyManager), isGroup(false)
97 string property1, property2, compare_string;
98 vector <string> test_strings;
100 InitializeConditionals();
102 TestParam1 = TestParam2 = 0L;
104 Comparison = ecUndef;
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];
114 cerr << endl << " Conditional test is invalid: \"" << test
115 << "\" has " << test_strings.size() << " elements in the "
116 << "test condition." << endl;
120 TestParam1 = PropertyManager->GetNode(property1, true);
121 Comparison = mComparison[conditional];
122 if (is_number(property2)) {
123 TestValue = atof(property2.c_str());
125 TestParam2 = PropertyManager->GetNode(property2, true);
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 void FGCondition::InitializeConditionals(void)
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;
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 FGCondition::~FGCondition(void)
157 for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 bool FGCondition::Evaluate(void )
169 if (TestParam1 == 0L) {
174 for (unsigned int i=0; i<conditions.size(); i++) {
175 if (!conditions[i]->Evaluate()) pass = false;
178 } else { // Logic must be eOR
181 for (unsigned int i=0; i<conditions.size(); i++) {
182 if (conditions[i]->Evaluate()) pass = true;
189 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
190 else compareValue = TestValue;
192 switch (Comparison) {
194 cerr << "Undefined comparison operator." << endl;
197 pass = TestParam1->getDoubleValue() == compareValue;
200 pass = TestParam1->getDoubleValue() != compareValue;
203 pass = TestParam1->getDoubleValue() > compareValue;
206 pass = TestParam1->getDoubleValue() >= compareValue;
209 pass = TestParam1->getDoubleValue() < compareValue;
212 pass = TestParam1->getDoubleValue() <= compareValue;
215 cerr << "Unknown comparison operator." << endl;
222 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 void FGCondition::PrintCondition(void )
232 cerr << "unset logic for test condition" << endl;
235 scratch = " if all of the following are true:";
238 scratch = " if any of the following are true:";
241 scratch = " UNKNOWN";
242 cerr << "Unknown logic for test condition" << endl;
245 cout << scratch << endl;
246 for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
249 if (TestParam2 != 0L)
250 cout << " " << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
252 cout << " " << TestParam1->GetName() << " " << conditional << " " << TestValue;
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
264 // 1: This value explicity requests the normal JSBSim
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
275 void FGCondition::Debug(int from)
277 if (debug_lvl <= 0) return;
279 if (debug_lvl & 1) { // Standard console startup message output
280 if (from == 0) { // Constructor
284 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
285 if (from == 0) cout << "Instantiated: FGCondition" << endl;
286 if (from == 1) cout << "Destroyed: FGCondition" << endl;
288 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
290 if (debug_lvl & 8 ) { // Runtime state variables
292 if (debug_lvl & 16) { // Sanity checking
294 if (debug_lvl & 64) {
295 if (from == 0) { // Constructor
296 cout << IdSrc << endl;
297 cout << IdHdr << endl;