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 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 General Public License for more
19 You should have received a copy of the GNU 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 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 = " ";
51 FGCondition::FGCondition(FGConfigFile* AC_cfg, FGPropertyManager* PropertyManager) :
52 PropertyManager(PropertyManager)
54 string property1, property2;
56 mComparison["EQ"] = eEQ;
57 mComparison["NE"] = eNE;
58 mComparison["GT"] = eGT;
59 mComparison["GE"] = eGE;
60 mComparison["LT"] = eLT;
61 mComparison["LE"] = eLE;
62 mComparison["=="] = eEQ;
63 mComparison["!="] = eNE;
64 mComparison[">"] = eGT;
65 mComparison[">="] = eGE;
66 mComparison["<"] = eLT;
67 mComparison["<="] = eLE;
69 TestParam1 = TestParam2 = 0L;
75 if (AC_cfg->GetValue("CONDITION_GROUP").empty()) { // define a condition
77 *AC_cfg >> property1 >> conditional >> property2;
78 TestParam1 = PropertyManager->GetNode(property1, true);
79 Comparison = mComparison[conditional];
81 if (property2.find_first_not_of("-.0123456789eE") == string::npos) {
82 TestValue = atof(property2.c_str());
84 TestParam2 = PropertyManager->GetNode(property2, true);
89 } else { // define a condition group
91 if (AC_cfg->GetValue("LOGIC") == "OR") Logic = eOR;
92 else if (AC_cfg->GetValue("LOGIC") == "AND") Logic = eAND;
94 AC_cfg->GetNextConfigLine();
95 while (AC_cfg->GetValue() != string("/CONDITION_GROUP")) {
96 conditions.push_back(FGCondition(AC_cfg, PropertyManager));
99 AC_cfg->GetNextConfigLine();
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 FGCondition::~FGCondition(void)
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 bool FGCondition::Evaluate(void )
116 vector <FGCondition>::iterator iConditions;
122 iConditions = conditions.begin();
124 while (iConditions < conditions.end()) {
125 if (!iConditions->Evaluate()) pass = false;
129 } else if (Logic == eOR) {
132 while (iConditions < conditions.end()) {
133 if (iConditions->Evaluate()) pass = true;
139 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
140 else compareValue = TestValue;
142 switch (Comparison) {
144 cerr << "Undefined comparison operator." << endl;
147 pass = TestParam1->getDoubleValue() == compareValue;
150 pass = TestParam1->getDoubleValue() != compareValue;
153 pass = TestParam1->getDoubleValue() > compareValue;
156 pass = TestParam1->getDoubleValue() >= compareValue;
159 pass = TestParam1->getDoubleValue() < compareValue;
162 pass = TestParam1->getDoubleValue() <= compareValue;
165 cerr << "Unknown comparison operator." << endl;
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 void FGCondition::PrintCondition(void )
176 vector <FGCondition>::iterator iConditions;
183 cerr << "unset logic for test condition" << endl;
186 scratch = " if all of the following are true";
189 scratch = " if any of the following are true:";
192 scratch = " UNKNOWN";
193 cerr << "Unknown logic for test condition" << endl;
196 iConditions = conditions.begin();
197 cout << scratch << endl;
198 while (iConditions < conditions.end()) {
199 iConditions->PrintCondition();
203 if (TestParam2 != 0L)
204 cout << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
206 cout << TestParam1->GetName() << " " << conditional << " " << TestValue;
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 // The bitmasked value choices are as follows:
212 // unset: In this case (the default) JSBSim would only print
213 // out the normally expected messages, essentially echoing
214 // the config files as they are read. If the environment
215 // variable is not set, debug_lvl is set to 1 internally
216 // 0: This requests JSBSim not to output any messages
218 // 1: This value explicity requests the normal JSBSim
220 // 2: This value asks for a message to be printed out when
221 // a class is instantiated
222 // 4: When this value is set, a message is displayed when a
223 // FGModel object executes its Run() method
224 // 8: When this value is set, various runtime state variables
225 // are printed out periodically
226 // 16: When set various parameters are sanity checked and
227 // a message is printed out when they go out of bounds
229 void FGCondition::Debug(int from)
231 if (debug_lvl <= 0) return;
233 if (debug_lvl & 1) { // Standard console startup message output
234 if (from == 0) { // Constructor
238 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
239 if (from == 0) cout << "Instantiated: FGCondition" << endl;
240 if (from == 1) cout << "Destroyed: FGCondition" << endl;
242 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
244 if (debug_lvl & 8 ) { // Runtime state variables
246 if (debug_lvl & 16) { // Sanity checking
248 if (debug_lvl & 64) {
249 if (from == 0) { // Constructor
250 cout << IdSrc << endl;
251 cout << IdHdr << endl;