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 "FGPropertyValue.h"
39 #include "input_output/FGXMLElement.h"
40 #include "input_output/FGPropertyManager.h"
48 static const char *IdSrc = "$Id: FGCondition.cpp,v 1.14 2011/04/05 20:20:21 andgi Exp $";
49 static const char *IdHdr = ID_CONDITION;
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 string FGCondition::indent = " ";
57 // This constructor is called when tests are inside an element
58 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
59 PropertyManager(PropertyManager), isGroup(true)
61 string property1, property2, logic;
62 Element* condition_element;
64 InitializeConditionals();
66 TestParam1 = TestParam2 = 0L;
72 logic = element->GetAttributeValue("logic");
74 if (logic == "OR") Logic = eOR;
75 else if (logic == "AND") Logic = eAND;
77 cerr << "Unrecognized LOGIC token " << logic << endl;
80 Logic = eAND; // default
83 condition_element = element->GetElement();
84 while (condition_element) {
85 conditions.push_back(new FGCondition(condition_element, PropertyManager));
86 condition_element = element->GetNextElement();
88 for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
89 string data = element->GetDataLine(i);
90 conditions.push_back(new FGCondition(data, PropertyManager));
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 // This constructor is called when there are no nested test groups inside the
100 FGCondition::FGCondition(const string& test, FGPropertyManager* PropertyManager) :
101 PropertyManager(PropertyManager), isGroup(false)
103 string property1, property2, compare_string;
104 vector <string> test_strings;
106 InitializeConditionals();
108 TestParam1 = TestParam2 = 0L;
110 Comparison = ecUndef;
114 test_strings = split(test, ' ');
115 if (test_strings.size() == 3) {
116 property1 = test_strings[0];
117 conditional = test_strings[1];
118 property2 = test_strings[2];
120 cerr << endl << " Conditional test is invalid: \"" << test
121 << "\" has " << test_strings.size() << " elements in the "
122 << "test condition." << endl;
126 FGPropertyManager *node = PropertyManager->GetNode(property1, false);
128 TestParam1 = new FGPropertyValue(node);
130 TestParam1 = new FGPropertyValue(property1, PropertyManager);
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 node = PropertyManager->GetNode(property2, false);
141 TestParam2 = new FGPropertyValue(node);
143 TestParam2 = new FGPropertyValue(property2, PropertyManager);
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 void FGCondition::InitializeConditionals(void)
152 mComparison["EQ"] = eEQ;
153 mComparison["NE"] = eNE;
154 mComparison["GT"] = eGT;
155 mComparison["GE"] = eGE;
156 mComparison["LT"] = eLT;
157 mComparison["LE"] = eLE;
158 mComparison["eq"] = eEQ;
159 mComparison["ne"] = eNE;
160 mComparison["gt"] = eGT;
161 mComparison["ge"] = eGE;
162 mComparison["lt"] = eLT;
163 mComparison["le"] = eLE;
164 mComparison["=="] = eEQ;
165 mComparison["!="] = eNE;
166 mComparison[">"] = eGT;
167 mComparison[">="] = eGE;
168 mComparison["<"] = eLT;
169 mComparison["<="] = eLE;
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 FGCondition::~FGCondition(void)
178 for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 bool FGCondition::Evaluate(void )
190 if (TestParam1 == 0L) {
195 for (unsigned int i=0; i<conditions.size(); i++) {
196 if (!conditions[i]->Evaluate()) pass = false;
199 } else { // Logic must be eOR
202 for (unsigned int i=0; i<conditions.size(); i++) {
203 if (conditions[i]->Evaluate()) pass = true;
210 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
211 else compareValue = TestValue;
213 switch (Comparison) {
215 cerr << "Undefined comparison operator." << endl;
218 pass = TestParam1->getDoubleValue() == compareValue;
221 pass = TestParam1->getDoubleValue() != compareValue;
224 pass = TestParam1->getDoubleValue() > compareValue;
227 pass = TestParam1->getDoubleValue() >= compareValue;
230 pass = TestParam1->getDoubleValue() < compareValue;
233 pass = TestParam1->getDoubleValue() <= compareValue;
236 cerr << "Unknown comparison operator." << endl;
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 void FGCondition::PrintCondition(void )
253 cerr << "unset logic for test condition" << endl;
256 scratch = " if all of the following are true:";
259 scratch = " if any of the following are true:";
262 scratch = " UNKNOWN";
263 cerr << "Unknown logic for test condition" << endl;
266 cout << scratch << endl;
267 for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
270 if (TestParam2 != 0L)
271 cout << " " << TestParam1->GetName() << " "
272 << conditional << " "
273 << TestParam2->GetName();
275 cout << " " << TestParam1->GetName() << " "
276 << conditional << " " << TestValue;
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 // The bitmasked value choices are as follows:
282 // unset: In this case (the default) JSBSim would only print
283 // out the normally expected messages, essentially echoing
284 // the config files as they are read. If the environment
285 // variable is not set, debug_lvl is set to 1 internally
286 // 0: This requests JSBSim not to output any messages
288 // 1: This value explicity requests the normal JSBSim
290 // 2: This value asks for a message to be printed out when
291 // a class is instantiated
292 // 4: When this value is set, a message is displayed when a
293 // FGModel object executes its Run() method
294 // 8: When this value is set, various runtime state variables
295 // are printed out periodically
296 // 16: When set various parameters are sanity checked and
297 // a message is printed out when they go out of bounds
299 void FGCondition::Debug(int from)
301 if (debug_lvl <= 0) return;
303 if (debug_lvl & 1) { // Standard console startup message output
304 if (from == 0) { // Constructor
308 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
309 if (from == 0) cout << "Instantiated: FGCondition" << endl;
310 if (from == 1) cout << "Destroyed: FGCondition" << endl;
312 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
314 if (debug_lvl & 8 ) { // Runtime state variables
316 if (debug_lvl & 16) { // Sanity checking
318 if (debug_lvl & 64) {
319 if (from == 0) { // Constructor
320 cout << IdSrc << endl;
321 cout << IdHdr << endl;