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"
41 static const char *IdSrc = "$Id$";
42 static const char *IdHdr = ID_CONDITION;
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 string FGCondition::indent = " ";
50 // This constructor is called when tests are inside an element
51 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
52 PropertyManager(PropertyManager), isGroup(true)
54 string property1, property2, logic;
55 Element* condition_element;
57 InitializeConditionals();
59 TestParam1 = TestParam2 = 0L;
65 logic = element->GetAttributeValue("logic");
67 if (logic == "OR") Logic = eOR;
68 else if (logic == "AND") Logic = eAND;
70 cerr << "Unrecognized LOGIC token " << logic << endl;
73 Logic = eAND; // default
76 condition_element = element->GetElement();
77 while (condition_element) {
78 conditions.push_back(new FGCondition(condition_element, PropertyManager));
79 condition_element = element->GetNextElement();
81 for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
82 string data = element->GetDataLine(i);
83 conditions.push_back(new FGCondition(data, PropertyManager));
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 //This constructor is called when there are no nested test groups inside the
93 FGCondition::FGCondition(string test, FGPropertyManager* PropertyManager) :
94 PropertyManager(PropertyManager), isGroup(false)
96 string property1, property2, compare_string;
98 InitializeConditionals();
100 TestParam1 = TestParam2 = 0L;
102 Comparison = ecUndef;
106 unsigned int start = 0, end = 0;
107 start = test.find_first_not_of(" ");
108 end = test.find_first_of(" ", start+1);
109 property1 = test.substr(start,end-start);
110 start = test.find_first_not_of(" ",end);
111 end = test.find_first_of(" ",start+1);
112 conditional = test.substr(start,end-start);
113 start = test.find_first_not_of(" ",end);
114 end = test.find_first_of(" ",start+1);
115 property2 = test.substr(start,end-start);
117 TestParam1 = PropertyManager->GetNode(property1, true);
118 Comparison = mComparison[conditional];
119 if (property2.find_first_not_of("-.0123456789eE") == string::npos) {
120 TestValue = atof(property2.c_str());
122 TestParam2 = PropertyManager->GetNode(property2, true);
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 void FGCondition::InitializeConditionals(void)
130 mComparison["EQ"] = eEQ;
131 mComparison["NE"] = eNE;
132 mComparison["GT"] = eGT;
133 mComparison["GE"] = eGE;
134 mComparison["LT"] = eLT;
135 mComparison["LE"] = eLE;
136 mComparison["eq"] = eEQ;
137 mComparison["ne"] = eNE;
138 mComparison["gt"] = eGT;
139 mComparison["ge"] = eGE;
140 mComparison["lt"] = eLT;
141 mComparison["le"] = eLE;
142 mComparison["=="] = eEQ;
143 mComparison["!="] = eNE;
144 mComparison[">"] = eGT;
145 mComparison[">="] = eGE;
146 mComparison["<"] = eLT;
147 mComparison["<="] = eLE;
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 FGCondition::~FGCondition(void)
154 for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 bool FGCondition::Evaluate(void )
166 if (TestParam1 == 0L) {
171 for (unsigned int i=0; i<conditions.size(); i++) {
172 if (!conditions[i]->Evaluate()) pass = false;
175 } else { // Logic must be eOR
178 for (unsigned int i=0; i<conditions.size(); i++) {
179 if (conditions[i]->Evaluate()) pass = true;
186 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
187 else compareValue = TestValue;
189 switch (Comparison) {
191 cerr << "Undefined comparison operator." << endl;
194 pass = TestParam1->getDoubleValue() == compareValue;
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 cerr << "Unknown comparison operator." << endl;
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 void FGCondition::PrintCondition(void )
229 cerr << "unset logic for test condition" << endl;
232 scratch = " if all of the following are true:";
235 scratch = " if any of the following are true:";
238 scratch = " UNKNOWN";
239 cerr << "Unknown logic for test condition" << endl;
242 cout << scratch << endl;
243 for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
246 if (TestParam2 != 0L)
247 cout << " " << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
249 cout << " " << TestParam1->GetName() << " " << conditional << " " << TestValue;
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 // The bitmasked value choices are as follows:
255 // unset: In this case (the default) JSBSim would only print
256 // out the normally expected messages, essentially echoing
257 // the config files as they are read. If the environment
258 // variable is not set, debug_lvl is set to 1 internally
259 // 0: This requests JSBSim not to output any messages
261 // 1: This value explicity requests the normal JSBSim
263 // 2: This value asks for a message to be printed out when
264 // a class is instantiated
265 // 4: When this value is set, a message is displayed when a
266 // FGModel object executes its Run() method
267 // 8: When this value is set, various runtime state variables
268 // are printed out periodically
269 // 16: When set various parameters are sanity checked and
270 // a message is printed out when they go out of bounds
272 void FGCondition::Debug(int from)
274 if (debug_lvl <= 0) return;
276 if (debug_lvl & 1) { // Standard console startup message output
277 if (from == 0) { // Constructor
281 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
282 if (from == 0) cout << "Instantiated: FGCondition" << endl;
283 if (from == 1) cout << "Destroyed: FGCondition" << endl;
285 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
287 if (debug_lvl & 8 ) { // Runtime state variables
289 if (debug_lvl & 16) { // Sanity checking
291 if (debug_lvl & 64) {
292 if (from == 0) { // Constructor
293 cout << IdSrc << endl;
294 cout << IdHdr << endl;