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$";
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, true);
126 Comparison = mComparison[conditional];
127 if (is_number(property2)) {
128 TestValue = atof(property2.c_str());
130 TestParam2 = PropertyManager->GetNode(property2, true);
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 void FGCondition::InitializeConditionals(void)
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;
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 FGCondition::~FGCondition(void)
162 for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 bool FGCondition::Evaluate(void )
174 if (TestParam1 == 0L) {
179 for (unsigned int i=0; i<conditions.size(); i++) {
180 if (!conditions[i]->Evaluate()) pass = false;
183 } else { // Logic must be eOR
186 for (unsigned int i=0; i<conditions.size(); i++) {
187 if (conditions[i]->Evaluate()) pass = true;
194 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
195 else compareValue = TestValue;
197 switch (Comparison) {
199 cerr << "Undefined comparison operator." << endl;
202 pass = TestParam1->getDoubleValue() == compareValue;
205 pass = TestParam1->getDoubleValue() != compareValue;
208 pass = TestParam1->getDoubleValue() > compareValue;
211 pass = TestParam1->getDoubleValue() >= compareValue;
214 pass = TestParam1->getDoubleValue() < compareValue;
217 pass = TestParam1->getDoubleValue() <= compareValue;
220 cerr << "Unknown comparison operator." << endl;
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 void FGCondition::PrintCondition(void )
237 cerr << "unset logic for test condition" << endl;
240 scratch = " if all of the following are true:";
243 scratch = " if any of the following are true:";
246 scratch = " UNKNOWN";
247 cerr << "Unknown logic for test condition" << endl;
250 cout << scratch << endl;
251 for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
254 if (TestParam2 != 0L)
255 cout << " " << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
257 cout << " " << TestParam1->GetName() << " " << conditional << " " << TestValue;
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
269 // 1: This value explicity requests the normal JSBSim
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
280 void FGCondition::Debug(int from)
282 if (debug_lvl <= 0) return;
284 if (debug_lvl & 1) { // Standard console startup message output
285 if (from == 0) { // Constructor
289 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
290 if (from == 0) cout << "Instantiated: FGCondition" << endl;
291 if (from == 1) cout << "Destroyed: FGCondition" << endl;
293 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
295 if (debug_lvl & 8 ) { // Runtime state variables
297 if (debug_lvl & 16) { // Sanity checking
299 if (debug_lvl & 64) {
300 if (from == 0) { // Constructor
301 cout << IdSrc << endl;
302 cout << IdHdr << endl;