Debian Codegraph @

src/FDM/JSBSim/math/FGCondition.cpp

raw symbols SCIP
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 ([email protected]) --------------
8
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
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later 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 Lesser General Public License for more
17 details.
18
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on 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 <iostream>
38 #include <cstdlib>
39 #include <stdexcept>
40 #include <assert.h>
41 #include <array>
42 #include <utility>
43
44 #include "FGCondition.h"
45 #include "FGPropertyValue.h"
46 #include "input_output/FGXMLElement.h"
47 #include "input_output/FGPropertyManager.h"
48 #include "FGParameterValue.h"
49
50 using namespace std;
51
52 namespace JSBSim {
53
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS IMPLEMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58 // This constructor is called when tests are inside an element
59 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager)
60 : Logic(elUndef), TestParam1(nullptr), TestParam2(nullptr),
61 Comparison(ecUndef)
62 {
63 string logic = element->GetAttributeValue("logic");
64 if (!logic.empty()) {
65 if (logic == "OR") Logic = eOR;
66 else if (logic == "AND") Logic = eAND;
67 else { // error
68 cerr << element->ReadFrom()
69 << "Unrecognized LOGIC token " << logic << endl;
70 throw BaseException("FGCondition: unrecognized logic value:'" + logic + "'");
71 }
72 } else {
73 Logic = eAND; // default
74 }
75
76 assert(Logic != elUndef);
77
78 for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
79 string data = element->GetDataLine(i);
80 conditions.push_back(make_shared<FGCondition>(data, PropertyManager, element));
81 }
82
83 Element* condition_element = element->GetElement();
84 const string& elName = element->GetName();
85
86 while (condition_element) {
87 string tagName = condition_element->GetName();
88
89 if (tagName != elName) {
90 cerr << condition_element->ReadFrom()
91 << "Unrecognized tag <" << tagName << "> in the condition statement."
92 << endl;
93 throw BaseException("FGCondition: unrecognized tag:'" + tagName + "'");
94 }
95
96 conditions.push_back(make_shared<FGCondition>(condition_element, PropertyManager));
97 condition_element = element->GetNextElement();
98 }
99
100 if (conditions.empty()) {
101 cerr << element->ReadFrom()
102 << "Empty conditional" << endl;
103 throw BaseException("Empty conditional");
104 }
105
106 Debug(0);
107 }
108
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 // This constructor is called when there are no nested test groups inside the
111 // condition
112
113 FGCondition::FGCondition(const string& test, FGPropertyManager* PropertyManager,
114 Element* el)
115 : Logic(elUndef), TestParam1(nullptr), TestParam2(nullptr),
116 Comparison(ecUndef)
117 {
118 static const array<pair<const char*, enum eComparison>, 18> mComparison {{
119 {"!=", eNE},
120 {"<", eLT},
121 {"<=", eLE},
122 {"==", eEQ},
123 {">", eGT},
124 {">=", eGE},
125 {"EQ", eEQ},
126 {"GE", eGE},
127 {"GT", eGT},
128 {"LE", eLE},
129 {"LT", eLT},
130 {"NE", eNE},
131 {"eq", eEQ},
132 {"ge", eGE},
133 {"gt", eGT},
134 {"le", eLE},
135 {"lt", eLT},
136 {"ne", eNE},
137 }};
138
139 vector<string> test_strings = split(test, ' ');
140
141 if (test_strings.size() == 3) {
142 TestParam1 = new FGPropertyValue(test_strings[0], PropertyManager);
143 conditional = test_strings[1];
144 TestParam2 = new FGParameterValue(test_strings[2], PropertyManager);
145 } else {
146 cerr << el->ReadFrom()
147 << " Conditional test is invalid: \"" << test
148 << "\" has " << test_strings.size() << " elements in the "
149 << "test condition." << endl;
150 throw BaseException("FGCondition: incorrect number of test elements:" + std::to_string(test_strings.size()));
151 }
152
153 assert(Comparison == ecUndef);
154 for(auto& elm: mComparison) {
155 if (conditional == elm.first) {
156 Comparison = elm.second;
157 break;
158 }
159 }
160
161 if (Comparison == ecUndef) {
162 throw BaseException("FGCondition: Comparison operator: \""+conditional
163 +"\" does not exist. Please check the conditional.");
164 }
165 }
166
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168
169 bool FGCondition::Evaluate(void )
170 {
171 bool pass = false;
172
173 if (!TestParam1) {
174
175 if (Logic == eAND) {
176
177 pass = true;
178 for (auto& cond: conditions) {
179 if (!cond->Evaluate()) pass = false;
180 }
181
182 } else { // Logic must be eOR
183
184 pass = false;
185 for (auto& cond: conditions) {
186 if (cond->Evaluate()) pass = true;
187 }
188
189 }
190
191 } else {
192 double value1 = TestParam1->GetValue();
193 double value2 = TestParam2->GetValue();
194
195 switch (Comparison) {
196 case eEQ:
197 pass = value1 == value2;
198 break;
199 case eNE:
200 pass = value1 != value2;
201 break;
202 case eGT:
203 pass = value1 > value2;
204 break;
205 case eGE:
206 pass = value1 >= value2;
207 break;
208 case eLT:
209 pass = value1 < value2;
210 break;
211 case eLE:
212 pass = value1 <= value2;
213 break;
214 default:
215 assert(false); // Should not be reached
216 break;
217 }
218 }
219
220 return pass;
221 }
222
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 void FGCondition::PrintCondition(string indent)
226 {
227 string scratch;
228
229 if (!conditions.empty()) {
230
231 switch(Logic) {
232 case (elUndef):
233 scratch = " UNSET";
234 cerr << "unset logic for test condition" << endl;
235 break;
236 case (eAND):
237 scratch = indent + "if all of the following are true: {";
238 break;
239 case (eOR):
240 scratch = indent + "if any of the following are true: {";
241 break;
242 default:
243 scratch = " UNKNOWN";
244 cerr << "Unknown logic for test condition" << endl;
245 }
246 cout << scratch << endl;
247
248 for (auto& cond: conditions) {
249 cond->PrintCondition(indent + " ");
250 cout << endl;
251 }
252
253 cout << indent << "}";
254
255 } else {
256 cout << indent << TestParam1->GetName() << " " << conditional
257 << " " << TestParam2->GetName();
258 }
259 }
260
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
268 // whatsoever.
269 // 1: This value explicity requests the normal JSBSim
270 // startup messages
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
279
280 void FGCondition::Debug(int from)
281 {
282 if (debug_lvl <= 0) return;
283
284 if (debug_lvl & 1) { // Standard console startup message output
285 if (from == 0) { // Constructor
286
287 }
288 }
289 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
290 if (from == 0) cout << "Instantiated: FGCondition" << endl;
291 if (from == 1) cout << "Destroyed: FGCondition" << endl;
292 }
293 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
294 }
295 if (debug_lvl & 8 ) { // Runtime state variables
296 }
297 if (debug_lvl & 16) { // Sanity checking
298 }
299 if (debug_lvl & 64) {
300 if (from == 0) { // Constructor
301 }
302 }
303 }
304
305 } //namespace JSBSim