]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGSwitch.cpp
Merge branch 'jsd/atmos' into topic/atmos-merge
[flightgear.git] / src / FDM / JSBSim / models / flight_control / FGSwitch.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGSwitch.cpp
4  Author:       Jon S. Berndt
5  Date started: 4/2000
6
7  ------------- Copyright (C) 2000 -------------
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 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 Lesser General Public License for more
17  details.
18
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.
22
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.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28
29 HISTORY
30 --------------------------------------------------------------------------------
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES,  and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36 The switch component is defined as follows (see the API documentation for more
37 information):
38
39 @code
40 <switch name="switch1">
41   <default value="{property|value}"/>
42   <test logic="{AND|OR}" value="{property|value}">
43     {property} {conditional} {property|value}
44     <test logic="{AND|OR}">
45       {property} {conditional} {property|value}
46       ...
47     </test>
48     ...
49   </test>
50   <test logic="{AND|OR}" value="{property|value}">
51     {property} {conditional} {property|value}
52     ...
53   </test>
54   ...
55 </switch>
56 @endcode
57
58 Also, see the header file (FGSwitch.h) for further details.
59
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 INCLUDES
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63
64 #include "FGSwitch.h"
65
66 namespace JSBSim {
67
68 static const char *IdSrc = "$Id$";
69 static const char *IdHdr = ID_SWITCH;
70
71 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 CLASS IMPLEMENTATION
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74
75 FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
76 {
77   string value, logic;
78   struct test *current_test;
79   Element *test_element, *condition_element;
80
81   FGFCSComponent::bind(); // Bind() this component here in case it is used
82                           // in its own definition for a sample-and-hold
83
84   test_element = element->GetElement();
85   while (test_element) {
86     if (test_element->GetName() == "default") {
87       current_test = new struct test;
88       current_test->Logic = eDefault;
89       tests.push_back(current_test);
90     } else if (test_element->GetName() == "test") { // here's a test
91       current_test = new struct test;
92       logic = test_element->GetAttributeValue("logic");
93       if (logic == "OR") current_test->Logic = eOR;
94       else if (logic == "AND") current_test->Logic = eAND;
95       else if (logic.size() == 0) current_test->Logic = eAND; // default
96       else { // error
97         cerr << "Unrecognized LOGIC token " << logic << " in switch component: " << Name << endl;
98       }
99       for (unsigned int i=0; i<test_element->GetNumDataLines(); i++) {
100         string input_data = test_element->GetDataLine(i);
101         if (input_data.size() <= 1) {
102           // Make sure there are no bad data lines that consist solely of whitespace
103           cerr << fgred << "  Bad data line in switch component: " << Name << reset << endl;
104           continue;
105         }
106         current_test->conditions.push_back(new FGCondition(input_data, PropertyManager));
107       }
108
109       condition_element = test_element->GetElement(); // retrieve condition groups
110       while (condition_element) {
111         current_test->conditions.push_back(new FGCondition(condition_element, PropertyManager));
112         condition_element = test_element->GetNextElement();
113       }
114
115       tests.push_back(current_test);
116     }
117
118     if (test_element->GetName() != "output"
119         && test_element->GetName() != "description") { // this is not an output element
120       value = test_element->GetAttributeValue("value");
121       if (value.empty()) {
122         cerr << "No VALUE supplied for switch component: " << Name << endl;
123       } else {
124         if (is_number(value)) {
125           current_test->OutputVal = atof(value.c_str());
126         } else {
127           // "value" must be a property if execution passes to here.
128           if (value[0] == '-') {
129             current_test->sign = -1.0;
130             value.erase(0,1);
131           } else {
132             current_test->sign = 1.0;
133           }
134           current_test->OutputProp = PropertyManager->GetNode(value);
135         }
136       }
137     }
138     test_element = element->GetNextElement();
139   }
140
141   Debug(0);
142 }
143
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 FGSwitch::~FGSwitch()
147 {
148   for (unsigned int i=0; i<tests.size(); i++) {
149     for (unsigned int j=0; j<tests[i]->conditions.size(); j++) delete tests[i]->conditions[j];
150     delete tests[i];
151   }
152
153   Debug(1);
154 }
155
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157
158 bool FGSwitch::Run(void )
159 {
160   bool pass = false;
161   double default_output=0.0;
162
163   for (unsigned int i=0; i<tests.size(); i++) {
164     if (tests[i]->Logic == eDefault) {
165       default_output = tests[i]->GetValue();
166     } else if (tests[i]->Logic == eAND) {
167       pass = true;
168       for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
169         if (!tests[i]->conditions[j]->Evaluate()) pass = false;
170       }
171     } else if (tests[i]->Logic == eOR) {
172       pass = false;
173       for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
174         if (tests[i]->conditions[j]->Evaluate()) pass = true;
175       }
176     } else {
177       cerr << "Invalid logic test" << endl;
178     }
179
180     if (pass) {
181       Output = tests[i]->GetValue();
182       break;
183     }
184   }
185   
186   if (!pass) Output = default_output;
187
188   Clip();
189   if (IsOutput) SetOutput();
190
191   return true;
192 }
193
194 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 //    The bitmasked value choices are as follows:
196 //    unset: In this case (the default) JSBSim would only print
197 //       out the normally expected messages, essentially echoing
198 //       the config files as they are read. If the environment
199 //       variable is not set, debug_lvl is set to 1 internally
200 //    0: This requests JSBSim not to output any messages
201 //       whatsoever.
202 //    1: This value explicity requests the normal JSBSim
203 //       startup messages
204 //    2: This value asks for a message to be printed out when
205 //       a class is instantiated
206 //    4: When this value is set, a message is displayed when a
207 //       FGModel object executes its Run() method
208 //    8: When this value is set, various runtime state variables
209 //       are printed out periodically
210 //    16: When set various parameters are sanity checked and
211 //       a message is printed out when they go out of bounds
212
213 void FGSwitch::Debug(int from)
214 {
215   string comp, scratch;
216   string indent = "        ";
217   bool first = false;
218
219   if (debug_lvl <= 0) return;
220
221   if (debug_lvl & 1) { // Standard console startup message output
222     if (from == 0) { // Constructor
223       for (unsigned int i=0; i<tests.size(); i++) {
224
225         scratch = " if ";
226
227         switch(tests[i]->Logic) {
228         case (elUndef):
229           comp = " UNSET ";
230           cerr << "Unset logic for test condition" << endl;
231           break;
232         case (eAND):
233           comp = " AND ";
234           break;
235         case (eOR):
236           comp=" OR ";
237           break;
238         case (eDefault):
239           scratch = " by default.";
240           break;
241         default:
242           comp = " UNKNOWN ";
243           cerr << "Unknown logic for test condition" << endl;
244         }
245
246         if (tests[i]->OutputProp != 0L)
247           if (tests[i]->sign < 0)
248             cout << indent << "Switch VALUE is - " << tests[i]->OutputProp->GetName() << scratch << endl;
249           else
250             cout << indent << "Switch VALUE is " << tests[i]->OutputProp->GetName() << scratch << endl;
251         else
252           cout << indent << "Switch VALUE is " << tests[i]->OutputVal << scratch << endl;
253
254         first = true;
255         for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
256           if (!first) cout << indent << comp << " ";
257           else cout << indent << " ";
258           first = false;
259           tests[i]->conditions[j]->PrintCondition();
260           cout << endl;
261         }
262         cout << endl;
263       }
264       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
265     }
266   }
267   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
268     if (from == 0) cout << "Instantiated: FGSwitch" << endl;
269     if (from == 1) cout << "Destroyed:    FGSwitch" << endl;
270   }
271   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
272   }
273   if (debug_lvl & 8 ) { // Runtime state variables
274   }
275   if (debug_lvl & 16) { // Sanity checking
276   }
277   if (debug_lvl & 64) {
278     if (from == 0) { // Constructor
279       cout << IdSrc << endl;
280       cout << IdHdr << endl;
281     }
282   }
283 }
284
285 } //namespace JSBSim
286