1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGCondition.cpp
7 -------------- Copyright (C) 2003 Jon S. Berndt (jon@jsbsim.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"
38 #include "input_output/FGXMLElement.h"
39 #include "input_output/FGPropertyManager.h"
47 static const char *IdSrc = "$Id: FGCondition.cpp,v 1.13 2010/07/14 05:50:40 ehofman Exp $";
48 static const char *IdHdr = ID_CONDITION;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 string FGCondition::indent = " ";
56 // This constructor is called when tests are inside an element
57 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
58 PropertyManager(PropertyManager), isGroup(true)
60 string property1, property2, logic;
61 Element* condition_element;
63 InitializeConditionals();
65 TestParam1 = TestParam2 = 0L;
71 logic = element->GetAttributeValue("logic");
73 if (logic == "OR") Logic = eOR;
74 else if (logic == "AND") Logic = eAND;
76 cerr << "Unrecognized LOGIC token " << logic << endl;
79 Logic = eAND; // default
82 condition_element = element->GetElement();
83 while (condition_element) {
84 conditions.push_back(new FGCondition(condition_element, PropertyManager));
85 condition_element = element->GetNextElement();
87 for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
88 string data = element->GetDataLine(i);
89 conditions.push_back(new FGCondition(data, PropertyManager));
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 // This constructor is called when there are no nested test groups inside the
99 FGCondition::FGCondition(const string& test, FGPropertyManager* PropertyManager) :
100 PropertyManager(PropertyManager), isGroup(false)
102 string property1, property2, compare_string;
103 vector <string> test_strings;
105 InitializeConditionals();
107 TestParam1 = TestParam2 = 0L;
109 Comparison = ecUndef;
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];
119 cerr << endl << " Conditional test is invalid: \"" << test
120 << "\" has " << test_strings.size() << " elements in the "
121 << "test condition." << endl;
125 TestParam1 = PropertyManager->GetNode(property1, false);
127 cerr << fgred << " In condition: " << test << ". Unknown property "
128 << property1 << " referenced." << endl
129 << "Creating property. Check usage." << reset << endl;
130 TestParam1 = PropertyManager->GetNode(property1, true);
132 Comparison = mComparison[conditional];
133 if (Comparison == ecUndef) {
134 throw("Comparison operator: \""+conditional+"\" does not exist. Please check the conditional.");
136 if (is_number(property2)) {
137 TestValue = atof(property2.c_str());
139 TestParam2 = PropertyManager->GetNode(property2, false);
141 cerr << fgred << " In condition: " << test << ". Unknown property "
142 << property2 << " referenced." << endl
143 << "Creating property. Check usage." << reset << endl;
144 TestParam2 = PropertyManager->GetNode(property2, true);
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 void FGCondition::InitializeConditionals(void)
153 mComparison["EQ"] = eEQ;
154 mComparison["NE"] = eNE;
155 mComparison["GT"] = eGT;
156 mComparison["GE"] = eGE;
157 mComparison["LT"] = eLT;
158 mComparison["LE"] = eLE;
159 mComparison["eq"] = eEQ;
160 mComparison["ne"] = eNE;
161 mComparison["gt"] = eGT;
162 mComparison["ge"] = eGE;
163 mComparison["lt"] = eLT;
164 mComparison["le"] = eLE;
165 mComparison["=="] = eEQ;
166 mComparison["!="] = eNE;
167 mComparison[">"] = eGT;
168 mComparison[">="] = eGE;
169 mComparison["<"] = eLT;
170 mComparison["<="] = eLE;
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 FGCondition::~FGCondition(void)
177 for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 bool FGCondition::Evaluate(void )
189 if (TestParam1 == 0L) {
194 for (unsigned int i=0; i<conditions.size(); i++) {
195 if (!conditions[i]->Evaluate()) pass = false;
198 } else { // Logic must be eOR
201 for (unsigned int i=0; i<conditions.size(); i++) {
202 if (conditions[i]->Evaluate()) pass = true;
209 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
210 else compareValue = TestValue;
212 switch (Comparison) {
214 cerr << "Undefined comparison operator." << endl;
217 pass = TestParam1->getDoubleValue() == compareValue;
220 pass = TestParam1->getDoubleValue() != compareValue;
223 pass = TestParam1->getDoubleValue() > compareValue;
226 pass = TestParam1->getDoubleValue() >= compareValue;
229 pass = TestParam1->getDoubleValue() < compareValue;
232 pass = TestParam1->getDoubleValue() <= compareValue;
235 cerr << "Unknown comparison operator." << endl;
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 void FGCondition::PrintCondition(void )
252 cerr << "unset logic for test condition" << endl;
255 scratch = " if all of the following are true:";
258 scratch = " if any of the following are true:";
261 scratch = " UNKNOWN";
262 cerr << "Unknown logic for test condition" << endl;
265 cout << scratch << endl;
266 for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
269 if (TestParam2 != 0L)
270 cout << " " << TestParam1->GetRelativeName() << " "
271 << conditional << " "
272 << TestParam2->GetRelativeName();
274 cout << " " << TestParam1->GetRelativeName() << " "
275 << conditional << " " << TestValue;
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 // The bitmasked value choices are as follows:
281 // unset: In this case (the default) JSBSim would only print
282 // out the normally expected messages, essentially echoing
283 // the config files as they are read. If the environment
284 // variable is not set, debug_lvl is set to 1 internally
285 // 0: This requests JSBSim not to output any messages
287 // 1: This value explicity requests the normal JSBSim
289 // 2: This value asks for a message to be printed out when
290 // a class is instantiated
291 // 4: When this value is set, a message is displayed when a
292 // FGModel object executes its Run() method
293 // 8: When this value is set, various runtime state variables
294 // are printed out periodically
295 // 16: When set various parameters are sanity checked and
296 // a message is printed out when they go out of bounds
298 void FGCondition::Debug(int from)
300 if (debug_lvl <= 0) return;
302 if (debug_lvl & 1) { // Standard console startup message output
303 if (from == 0) { // Constructor
307 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
308 if (from == 0) cout << "Instantiated: FGCondition" << endl;
309 if (from == 1) cout << "Destroyed: FGCondition" << endl;
311 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
313 if (debug_lvl & 8 ) { // Runtime state variables
315 if (debug_lvl & 16) { // Sanity checking
317 if (debug_lvl & 64) {
318 if (from == 0) { // Constructor
319 cout << IdSrc << endl;
320 cout << IdHdr << endl;