]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGCondition.cpp
Update to the latest version of JSBSim
[flightgear.git] / src / FDM / JSBSim / filtersjb / FGCondition.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGCondition.cpp
4  Author:       Jon S. Berndt
5  Date started: 1/2/2003
6  
7  -------------- Copyright (C) 2003 Jon S. Berndt (jsb@hal-pc.org) --------------
8
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
12  version.
13
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
17  details.
18
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.
22
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 HISTORY
27 --------------------------------------------------------------------------------
28
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 COMMENTS, REFERENCES,  and NOTES
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 INCLUDES
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37 #include "FGCondition.h"
38
39 namespace JSBSim {
40
41 static const char *IdSrc = "$Id$";
42 static const char *IdHdr = ID_CONDITION;
43
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 CLASS IMPLEMENTATION
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47
48 string FGCondition::indent = "        ";
49
50
51 FGCondition::FGCondition(FGConfigFile* AC_cfg, FGPropertyManager* PropertyManager) :
52   PropertyManager(PropertyManager)
53 {
54   string property1, property2;
55
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;
68
69   TestParam1  = TestParam2 = 0L;
70   TestValue   = 0.0;
71   Comparison  = ecUndef;
72   Logic       = elUndef;
73   conditions.clear();
74
75   if (AC_cfg->GetValue("CONDITION_GROUP").empty()) {  // define a condition
76
77     *AC_cfg >> property1 >> conditional >> property2;
78     TestParam1 = PropertyManager->GetNode(property1, true);
79     Comparison = mComparison[conditional];
80
81     if (property2.find_first_not_of("-.0123456789eE") == string::npos) {
82       TestValue = atof(property2.c_str());
83     } else {
84       TestParam2 = PropertyManager->GetNode(property2, true);
85     }
86
87     isGroup = false;
88
89   } else { // define a condition group
90
91     if (AC_cfg->GetValue("LOGIC") == "OR")       Logic = eOR;
92     else if (AC_cfg->GetValue("LOGIC") == "AND") Logic = eAND;
93
94     AC_cfg->GetNextConfigLine();
95     while (AC_cfg->GetValue() != string("/CONDITION_GROUP")) {
96       conditions.push_back(*(new FGCondition(AC_cfg, PropertyManager)));
97     }
98     isGroup = true;
99     AC_cfg->GetNextConfigLine();
100   }
101
102   Debug(0);
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 FGCondition::~FGCondition(void)
108 {
109   Debug(1);
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 bool FGCondition::Evaluate(void )
115 {
116   vector <FGCondition>::iterator iConditions;
117   bool pass = false;
118   double compareValue;
119
120   if (Logic == eAND) {
121
122     iConditions = conditions.begin();
123     pass = true;
124     while (iConditions < conditions.end()) {
125       if (!iConditions->Evaluate()) pass = false;
126       *iConditions++;
127     }
128
129   } else if (Logic == eOR) {
130
131     pass = false;
132     while (iConditions < conditions.end()) {
133       if (iConditions->Evaluate()) pass = true;
134       *iConditions++;
135     }
136
137   } else {
138
139     if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
140     else compareValue = TestValue;
141
142     switch (Comparison) {
143     case ecUndef:
144       cerr << "Undefined comparison operator." << endl;
145       break;
146     case eEQ:
147       pass = TestParam1->getDoubleValue() == compareValue;
148       break;
149     case eNE:
150       pass = TestParam1->getDoubleValue() != compareValue;
151       break;
152     case eGT:
153       pass = TestParam1->getDoubleValue() > compareValue;
154       break;
155     case eGE:
156       pass = TestParam1->getDoubleValue() >= compareValue;
157       break;
158     case eLT:
159       pass = TestParam1->getDoubleValue() < compareValue;
160       break;
161     case eLE:
162       pass = TestParam1->getDoubleValue() <= compareValue;
163       break;
164     default:
165      cerr << "Unknown comparison operator." << endl;
166     }
167   }
168
169   return pass;
170 }
171
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173
174 void FGCondition::PrintCondition(void )
175 {
176   vector <FGCondition>::iterator iConditions;
177   string scratch;
178   bool first = false;
179
180   if (isGroup) {
181     switch(Logic) {
182     case (elUndef):
183       scratch = " UNSET";
184       cerr << "unset logic for test condition" << endl;
185       break;
186     case (eAND):
187       scratch = " if all of the following are true";
188       break;
189     case (eOR):
190       scratch = " if any of the following are true:";
191       break;
192     default:
193       scratch = " UNKNOWN";
194       cerr << "Unknown logic for test condition" << endl;
195     }
196
197     iConditions = conditions.begin();
198     cout << scratch << endl;
199     while (iConditions < conditions.end()) {
200       iConditions->PrintCondition();
201       *iConditions++;
202     }
203   } else {
204     if (TestParam2 != 0L)
205       cout << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
206     else  
207       cout << TestParam1->GetName() << " " << conditional << " " << TestValue;
208   }
209 }
210
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 //    The bitmasked value choices are as follows:
213 //    unset: In this case (the default) JSBSim would only print
214 //       out the normally expected messages, essentially echoing
215 //       the config files as they are read. If the environment
216 //       variable is not set, debug_lvl is set to 1 internally
217 //    0: This requests JSBSim not to output any messages
218 //       whatsoever.
219 //    1: This value explicity requests the normal JSBSim
220 //       startup messages
221 //    2: This value asks for a message to be printed out when
222 //       a class is instantiated
223 //    4: When this value is set, a message is displayed when a
224 //       FGModel object executes its Run() method
225 //    8: When this value is set, various runtime state variables
226 //       are printed out periodically
227 //    16: When set various parameters are sanity checked and
228 //       a message is printed out when they go out of bounds
229
230 void FGCondition::Debug(int from)
231 {
232   if (debug_lvl <= 0) return;
233
234   if (debug_lvl & 1) { // Standard console startup message output
235     if (from == 0) { // Constructor
236
237     }
238   }
239   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
240     if (from == 0) cout << "Instantiated: FGCondition" << endl;
241     if (from == 1) cout << "Destroyed:    FGCondition" << endl;
242   }
243   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
244   }
245   if (debug_lvl & 8 ) { // Runtime state variables
246   }
247   if (debug_lvl & 16) { // Sanity checking
248   }
249   if (debug_lvl & 64) {
250     if (from == 0) { // Constructor
251       cout << IdSrc << endl;
252       cout << IdHdr << endl;
253     }
254   }
255 }
256
257 } //namespace JSBSim
258