]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGCondition.cpp
Fix stall widths for the "auxilliary" (reverse flow) stalls so they
[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   mComparison["EQ"] = eEQ;
55   mComparison["NE"] = eNE;
56   mComparison["GT"] = eGT;
57   mComparison["GE"] = eGE;
58   mComparison["LT"] = eLT;
59   mComparison["LE"] = eLE;
60   mComparison["=="] = eEQ;
61   mComparison["!="] = eNE;
62   mComparison[">"]  = eGT;
63   mComparison[">="] = eGE;
64   mComparison["<"]  = eLT;
65   mComparison["<="] = eLE;
66
67   TestParam1  = TestParam2 = 0L;
68   TestValue   = 0.0;
69   Comparison  = ecUndef;
70   Logic       = elUndef;
71   conditions.clear();
72
73   if (AC_cfg->GetValue("CONDITION_GROUP").empty()) {  // define a condition
74
75     *AC_cfg >> property1 >> conditional >> property2;
76     TestParam1 = PropertyManager->GetNode(property1, true);
77     Comparison = mComparison[conditional];
78
79     if (property2.find_first_not_of("-.0123456789eE") == string::npos) {
80       TestValue = atof(property2.c_str());
81     } else {
82       TestParam2 = PropertyManager->GetNode(property2, true);
83     }
84
85     isGroup = false;
86
87   } else { // define a condition group
88
89     if (AC_cfg->GetValue("LOGIC") == "OR")       Logic = eOR;
90     else if (AC_cfg->GetValue("LOGIC") == "AND") Logic = eAND;
91
92     AC_cfg->GetNextConfigLine();
93     while (AC_cfg->GetValue() != string("/CONDITION_GROUP")) {
94       conditions.push_back(FGCondition(AC_cfg, PropertyManager));
95     }
96     isGroup = true;
97     AC_cfg->GetNextConfigLine();
98   }
99
100   Debug(0);
101 }
102
103 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104
105 FGCondition::~FGCondition(void)
106 {
107   Debug(1);
108 }
109
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111
112 bool FGCondition::Evaluate(void )
113 {
114   vector <FGCondition>::iterator iConditions;
115   bool pass = false;
116   double compareValue;
117
118   if (Logic == eAND) {
119
120     iConditions = conditions.begin();
121     pass = true;
122     while (iConditions < conditions.end()) {
123       if (!iConditions->Evaluate()) pass = false;
124       *iConditions++;
125     }
126
127   } else if (Logic == eOR) {
128
129     pass = false;
130     while (iConditions < conditions.end()) {
131       if (iConditions->Evaluate()) pass = true;
132       *iConditions++;
133     }
134
135   } else {
136
137     if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
138     else compareValue = TestValue;
139
140     switch (Comparison) {
141     case ecUndef:
142       cerr << "Undefined comparison operator." << endl;
143       break;
144     case eEQ:
145       pass = TestParam1->getDoubleValue() == compareValue;
146       break;
147     case eNE:
148       pass = TestParam1->getDoubleValue() != compareValue;
149       break;
150     case eGT:
151       pass = TestParam1->getDoubleValue() > compareValue;
152       break;
153     case eGE:
154       pass = TestParam1->getDoubleValue() >= compareValue;
155       break;
156     case eLT:
157       pass = TestParam1->getDoubleValue() < compareValue;
158       break;
159     case eLE:
160       pass = TestParam1->getDoubleValue() <= compareValue;
161       break;
162     default:
163      cerr << "Unknown comparison operator." << endl;
164     }
165   }
166
167   return pass;
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172 void FGCondition::PrintCondition(void )
173 {
174   vector <FGCondition>::iterator iConditions;
175   string scratch;
176
177   if (isGroup) {
178     switch(Logic) {
179     case (elUndef):
180       scratch = " UNSET";
181       cerr << "unset logic for test condition" << endl;
182       break;
183     case (eAND):
184       scratch = " if all of the following are true";
185       break;
186     case (eOR):
187       scratch = " if any of the following are true:";
188       break;
189     default:
190       scratch = " UNKNOWN";
191       cerr << "Unknown logic for test condition" << endl;
192     }
193
194     iConditions = conditions.begin();
195     cout << scratch << endl;
196     while (iConditions < conditions.end()) {
197       iConditions->PrintCondition();
198       *iConditions++;
199     }
200   } else {
201     if (TestParam2 != 0L)
202       cout << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
203     else
204       cout << TestParam1->GetName() << " " << conditional << " " << TestValue;
205   }
206 }
207
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
210 void FGCondition::convert(void)
211 {
212   if (conditions.empty())
213     cout << "                " << property1 << " " << conditional << " " << property2 << endl;
214   else
215     for (int i; i<conditions.size(); i++) conditions[i].convert();
216 }
217
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 //    The bitmasked value choices are as follows:
220 //    unset: In this case (the default) JSBSim would only print
221 //       out the normally expected messages, essentially echoing
222 //       the config files as they are read. If the environment
223 //       variable is not set, debug_lvl is set to 1 internally
224 //    0: This requests JSBSim not to output any messages
225 //       whatsoever.
226 //    1: This value explicity requests the normal JSBSim
227 //       startup messages
228 //    2: This value asks for a message to be printed out when
229 //       a class is instantiated
230 //    4: When this value is set, a message is displayed when a
231 //       FGModel object executes its Run() method
232 //    8: When this value is set, various runtime state variables
233 //       are printed out periodically
234 //    16: When set various parameters are sanity checked and
235 //       a message is printed out when they go out of bounds
236
237 void FGCondition::Debug(int from)
238 {
239   if (debug_lvl <= 0) return;
240
241   if (debug_lvl & 1) { // Standard console startup message output
242     if (from == 0) { // Constructor
243
244     }
245   }
246   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
247     if (from == 0) cout << "Instantiated: FGCondition" << endl;
248     if (from == 1) cout << "Destroyed:    FGCondition" << endl;
249   }
250   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
251   }
252   if (debug_lvl & 8 ) { // Runtime state variables
253   }
254   if (debug_lvl & 16) { // Sanity checking
255   }
256   if (debug_lvl & 64) {
257     if (from == 0) { // Constructor
258       cout << IdSrc << endl;
259       cout << IdHdr << endl;
260     }
261   }
262 }
263
264 } //namespace JSBSim
265