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 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 General Public License for more
19 You should have received a copy of the GNU 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 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
39 <COMPONENT NAME="switch1" TYPE="SWITCH">
40 <TEST LOGIC="{AND|OR|DEFAULT}" OUTPUT="{property|value}">
41 {property} {conditional} {property|value}
42 <CONDITION_GROUP LOGIC="{AND|OR}">
43 {property} {conditional} {property|value}
48 <TEST LOGIC="{AND|OR}" OUTPUT="{property|value}">
49 {property} {conditional} {property|value}
55 Also, see the header file (FGSwitch.h) for further details.
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65 static const char *IdSrc = "$Id$";
66 static const char *IdHdr = ID_SWITCH;
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
75 struct test *current_test;
76 Element *test_element, *condition_element;
78 test_element = element->GetElement();
79 while (test_element) {
80 if (test_element->GetName() == "default") {
81 tests.push_back(test());
82 current_test = &tests.back();
83 current_test->Logic = eDefault;
84 } else if (test_element->GetName() == "test") { // here's a test
85 tests.push_back(test());
86 current_test = &tests.back();
87 logic = test_element->GetAttributeValue("logic");
88 if (logic == "OR") current_test->Logic = eOR;
89 else if (logic == "AND") current_test->Logic = eAND;
90 else if (logic.size() == 0) current_test->Logic = eAND; // default
92 cerr << "Unrecognized LOGIC token " << logic << " in switch component: " << Name << endl;
94 for (int i=0; i<test_element->GetNumDataLines(); i++)
95 current_test->conditions.push_back(FGCondition(test_element->GetDataLine(i), PropertyManager));
97 condition_element = test_element->GetElement(); // retrieve condition groups
98 while (condition_element) {
99 current_test->conditions.push_back(FGCondition(condition_element, PropertyManager));
100 condition_element = test_element->GetNextElement();
105 if (test_element->GetName() != "output") { // this is not an output element
106 value = test_element->GetAttributeValue("value");
108 cerr << "No VALUE supplied for switch component: " << Name << endl;
110 if (value.find_first_not_of("-.0123456789eE") == string::npos) {
111 // if true (and execution falls into this block), "value" is a number.
112 current_test->OutputVal = atof(value.c_str());
114 // "value" must be a property if execution passes to here.
115 if (value[0] == '-') {
116 current_test->sign = -1.0;
119 current_test->sign = 1.0;
121 current_test->OutputProp = PropertyManager->GetNode(value);
126 test_element = element->GetNextElement();
129 FGFCSComponent::bind();
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 FGSwitch::~FGSwitch()
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 bool FGSwitch::Run(void )
145 vector <test>::iterator iTests = tests.begin();
146 vector <FGCondition>::iterator iConditions;
149 while (iTests < tests.end()) {
150 iConditions = iTests->conditions.begin();
152 if (iTests->Logic == eDefault) {
153 Output = iTests->GetValue();
154 } else if (iTests->Logic == eAND) {
156 while (iConditions < iTests->conditions.end()) {
157 if (!iConditions->Evaluate()) pass = false;
160 } else if (iTests->Logic == eOR) {
162 while (iConditions < iTests->conditions.end()) {
163 if (iConditions->Evaluate()) pass = true;
167 cerr << "Invalid logic test" << endl;
171 Output = iTests->GetValue();
178 if (IsOutput) SetOutput();
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 // The bitmasked value choices are as follows:
185 // unset: In this case (the default) JSBSim would only print
186 // out the normally expected messages, essentially echoing
187 // the config files as they are read. If the environment
188 // variable is not set, debug_lvl is set to 1 internally
189 // 0: This requests JSBSim not to output any messages
191 // 1: This value explicity requests the normal JSBSim
193 // 2: This value asks for a message to be printed out when
194 // a class is instantiated
195 // 4: When this value is set, a message is displayed when a
196 // FGModel object executes its Run() method
197 // 8: When this value is set, various runtime state variables
198 // are printed out periodically
199 // 16: When set various parameters are sanity checked and
200 // a message is printed out when they go out of bounds
202 void FGSwitch::Debug(int from)
204 vector <test>::iterator iTests = tests.begin();
205 vector <FGCondition>::iterator iConditions;
206 string comp, scratch;
210 if (debug_lvl <= 0) return;
212 if (debug_lvl & 1) { // Standard console startup message output
213 if (from == 0) { // Constructor
214 while (iTests < tests.end()) {
218 switch(iTests->Logic) {
221 cerr << "Unset logic for test condition" << endl;
230 scratch = " by default.";
234 cerr << "Unknown logic for test condition" << endl;
237 if (iTests->OutputProp != 0L)
238 if (iTests->sign < 0)
239 cout << indent << "Switch VALUE is - " << iTests->OutputProp->GetName() << scratch << endl;
241 cout << indent << "Switch VALUE is " << iTests->OutputProp->GetName() << scratch << endl;
243 cout << indent << "Switch VALUE is " << iTests->OutputVal << scratch << endl;
245 iConditions = iTests->conditions.begin();
247 while (iConditions < iTests->conditions.end()) {
248 if (!first) cout << indent << comp << " ";
249 else cout << indent << " ";
251 iConditions->PrintCondition();
258 if (IsOutput) cout << " OUTPUT: " << OutputNode->getName() << endl;
261 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
262 if (from == 0) cout << "Instantiated: FGSwitch" << endl;
263 if (from == 1) cout << "Destroyed: FGSwitch" << endl;
265 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
267 if (debug_lvl & 8 ) { // Runtime state variables
269 if (debug_lvl & 16) { // Sanity checking
271 if (debug_lvl & 64) {
272 if (from == 0) { // Constructor
273 cout << IdSrc << endl;
274 cout << IdHdr << endl;