]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGSwitch.cpp
Sync. with JSBSim cvs
[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         current_test->conditions.push_back(new FGCondition(test_element->GetDataLine(i), PropertyManager));
101       }
102
103       condition_element = test_element->GetElement(); // retrieve condition groups
104       while (condition_element) {
105         current_test->conditions.push_back(new FGCondition(condition_element, PropertyManager));
106         condition_element = test_element->GetNextElement();
107       }
108
109       tests.push_back(current_test);
110     }
111
112     if (test_element->GetName() != "output") { // this is not an output element
113       value = test_element->GetAttributeValue("value");
114       if (value.empty()) {
115         cerr << "No VALUE supplied for switch component: " << Name << endl;
116       } else {
117         if (value.find_first_not_of("-.0123456789eE") == string::npos) {
118           // if true (and execution falls into this block), "value" is a number.
119           current_test->OutputVal = atof(value.c_str());
120         } else {
121           // "value" must be a property if execution passes to here.
122           if (value[0] == '-') {
123             current_test->sign = -1.0;
124             value.erase(0,1);
125           } else {
126             current_test->sign = 1.0;
127           }
128           current_test->OutputProp = PropertyManager->GetNode(value);
129         }
130       }
131     }
132     test_element = element->GetNextElement();
133   }
134
135   Debug(0);
136 }
137
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 FGSwitch::~FGSwitch()
141 {
142   for (unsigned int i=0; i<tests.size(); i++) {
143     for (unsigned int j=0; j<tests[i]->conditions.size(); j++) delete tests[i]->conditions[j];
144     delete tests[i];
145   }
146
147   Debug(1);
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 bool FGSwitch::Run(void )
153 {
154   bool pass = false;
155   double default_output=0.0;
156
157   for (unsigned int i=0; i<tests.size(); i++) {
158     if (tests[i]->Logic == eDefault) {
159       default_output = tests[i]->GetValue();
160     } else if (tests[i]->Logic == eAND) {
161       pass = true;
162       for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
163         if (!tests[i]->conditions[j]->Evaluate()) pass = false;
164       }
165     } else if (tests[i]->Logic == eOR) {
166       pass = false;
167       for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
168         if (tests[i]->conditions[j]->Evaluate()) pass = true;
169       }
170     } else {
171       cerr << "Invalid logic test" << endl;
172     }
173
174     if (pass) {
175       Output = tests[i]->GetValue();
176       break;
177     }
178   }
179   
180   if (!pass) Output = default_output;
181
182   Clip();
183   if (IsOutput) SetOutput();
184
185   return true;
186 }
187
188 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 //    The bitmasked value choices are as follows:
190 //    unset: In this case (the default) JSBSim would only print
191 //       out the normally expected messages, essentially echoing
192 //       the config files as they are read. If the environment
193 //       variable is not set, debug_lvl is set to 1 internally
194 //    0: This requests JSBSim not to output any messages
195 //       whatsoever.
196 //    1: This value explicity requests the normal JSBSim
197 //       startup messages
198 //    2: This value asks for a message to be printed out when
199 //       a class is instantiated
200 //    4: When this value is set, a message is displayed when a
201 //       FGModel object executes its Run() method
202 //    8: When this value is set, various runtime state variables
203 //       are printed out periodically
204 //    16: When set various parameters are sanity checked and
205 //       a message is printed out when they go out of bounds
206
207 void FGSwitch::Debug(int from)
208 {
209   string comp, scratch;
210   string indent = "        ";
211   bool first = false;
212
213   if (debug_lvl <= 0) return;
214
215   if (debug_lvl & 1) { // Standard console startup message output
216     if (from == 0) { // Constructor
217       for (unsigned int i=0; i<tests.size(); i++) {
218
219         scratch = " if ";
220
221         switch(tests[i]->Logic) {
222         case (elUndef):
223           comp = " UNSET ";
224           cerr << "Unset logic for test condition" << endl;
225           break;
226         case (eAND):
227           comp = " AND ";
228           break;
229         case (eOR):
230           comp=" OR ";
231           break;
232         case (eDefault):
233           scratch = " by default.";
234           break;
235         default:
236           comp = " UNKNOWN ";
237           cerr << "Unknown logic for test condition" << endl;
238         }
239
240         if (tests[i]->OutputProp != 0L)
241           if (tests[i]->sign < 0)
242             cout << indent << "Switch VALUE is - " << tests[i]->OutputProp->GetName() << scratch << endl;
243           else
244             cout << indent << "Switch VALUE is " << tests[i]->OutputProp->GetName() << scratch << endl;
245         else
246           cout << indent << "Switch VALUE is " << tests[i]->OutputVal << scratch << endl;
247
248         first = true;
249         for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
250           if (!first) cout << indent << comp << " ";
251           else cout << indent << " ";
252           first = false;
253           tests[i]->conditions[j]->PrintCondition();
254           cout << endl;
255         }
256         cout << endl;
257       }
258       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
259     }
260   }
261   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
262     if (from == 0) cout << "Instantiated: FGSwitch" << endl;
263     if (from == 1) cout << "Destroyed:    FGSwitch" << endl;
264   }
265   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
266   }
267   if (debug_lvl & 8 ) { // Runtime state variables
268   }
269   if (debug_lvl & 16) { // Sanity checking
270   }
271   if (debug_lvl & 64) {
272     if (from == 0) { // Constructor
273       cout << IdSrc << endl;
274       cout << IdHdr << endl;
275     }
276   }
277 }
278
279 } //namespace JSBSim
280