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