1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 2000 -------------
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
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
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 The switch component is defined as follows (see the API documentation for more
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}
50 <test logic="{AND|OR}" value="{property|value}">
51 {property} {conditional} {property|value}
58 Also, see the header file (FGSwitch.h) for further details.
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 static const char *IdSrc = "$Id$";
69 static const char *IdHdr = ID_SWITCH;
71 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
78 struct test *current_test;
79 Element *test_element, *condition_element;
81 FGFCSComponent::bind(); // Bind() this component here in case it is used
82 // in its own definition for a sample-and-hold
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
97 cerr << "Unrecognized LOGIC token " << logic << " in switch component: " << Name << endl;
99 for (unsigned int i=0; i<test_element->GetNumDataLines(); i++) {
100 current_test->conditions.push_back(new FGCondition(test_element->GetDataLine(i), PropertyManager));
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();
109 tests.push_back(current_test);
112 if (test_element->GetName() != "output"
113 && test_element->GetName() != "description") { // this is not an output element
114 value = test_element->GetAttributeValue("value");
116 cerr << "No VALUE supplied for switch component: " << Name << endl;
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());
122 // "value" must be a property if execution passes to here.
123 if (value[0] == '-') {
124 current_test->sign = -1.0;
127 current_test->sign = 1.0;
129 current_test->OutputProp = PropertyManager->GetNode(value);
133 test_element = element->GetNextElement();
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 FGSwitch::~FGSwitch()
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];
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 bool FGSwitch::Run(void )
156 double default_output=0.0;
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) {
163 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
164 if (!tests[i]->conditions[j]->Evaluate()) pass = false;
166 } else if (tests[i]->Logic == eOR) {
168 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
169 if (tests[i]->conditions[j]->Evaluate()) pass = true;
172 cerr << "Invalid logic test" << endl;
176 Output = tests[i]->GetValue();
181 if (!pass) Output = default_output;
184 if (IsOutput) SetOutput();
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
197 // 1: This value explicity requests the normal JSBSim
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
208 void FGSwitch::Debug(int from)
210 string comp, scratch;
214 if (debug_lvl <= 0) return;
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++) {
222 switch(tests[i]->Logic) {
225 cerr << "Unset logic for test condition" << endl;
234 scratch = " by default.";
238 cerr << "Unknown logic for test condition" << endl;
241 if (tests[i]->OutputProp != 0L)
242 if (tests[i]->sign < 0)
243 cout << indent << "Switch VALUE is - " << tests[i]->OutputProp->GetName() << scratch << endl;
245 cout << indent << "Switch VALUE is " << tests[i]->OutputProp->GetName() << scratch << endl;
247 cout << indent << "Switch VALUE is " << tests[i]->OutputVal << scratch << endl;
250 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
251 if (!first) cout << indent << comp << " ";
252 else cout << indent << " ";
254 tests[i]->conditions[j]->PrintCondition();
259 if (IsOutput) cout << " OUTPUT: " << OutputNode->getName() << endl;
262 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
263 if (from == 0) cout << "Instantiated: FGSwitch" << endl;
264 if (from == 1) cout << "Destroyed: FGSwitch" << endl;
266 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
268 if (debug_lvl & 8 ) { // Runtime state variables
270 if (debug_lvl & 16) { // Sanity checking
272 if (debug_lvl & 64) {
273 if (from == 0) { // Constructor
274 cout << IdSrc << endl;
275 cout << IdHdr << endl;