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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 static const char *IdSrc = "$Id$";
73 static const char *IdHdr = ID_SWITCH;
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
79 FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
82 struct test *current_test;
83 Element *test_element, *condition_element;
85 FGFCSComponent::bind(); // Bind() this component here in case it is used
86 // in its own definition for a sample-and-hold
88 test_element = element->GetElement();
89 while (test_element) {
90 if (test_element->GetName() == "default") {
91 current_test = new struct test;
92 current_test->Logic = eDefault;
93 tests.push_back(current_test);
94 } else if (test_element->GetName() == "test") { // here's a test
95 current_test = new struct test;
96 logic = test_element->GetAttributeValue("logic");
97 if (logic == "OR") current_test->Logic = eOR;
98 else if (logic == "AND") current_test->Logic = eAND;
99 else if (logic.size() == 0) current_test->Logic = eAND; // default
101 cerr << "Unrecognized LOGIC token " << logic << " in switch component: " << Name << endl;
103 for (unsigned int i=0; i<test_element->GetNumDataLines(); i++) {
104 string input_data = test_element->GetDataLine(i);
105 if (input_data.size() <= 1) {
106 // Make sure there are no bad data lines that consist solely of whitespace
107 cerr << fgred << " Bad data line in switch component: " << Name << reset << endl;
110 current_test->conditions.push_back(new FGCondition(input_data, PropertyManager));
113 condition_element = test_element->GetElement(); // retrieve condition groups
114 while (condition_element) {
115 current_test->conditions.push_back(new FGCondition(condition_element, PropertyManager));
116 condition_element = test_element->GetNextElement();
119 tests.push_back(current_test);
122 if (test_element->GetName() != "output"
123 && test_element->GetName() != "description") { // this is not an output element
124 value = test_element->GetAttributeValue("value");
126 cerr << "No VALUE supplied for switch component: " << Name << endl;
128 if (is_number(value)) {
129 current_test->OutputVal = atof(value.c_str());
131 // "value" must be a property if execution passes to here.
132 if (value[0] == '-') {
133 current_test->sign = -1.0;
136 current_test->sign = 1.0;
138 current_test->OutputProp = PropertyManager->GetNode(value);
142 test_element = element->GetNextElement();
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 FGSwitch::~FGSwitch()
152 for (unsigned int i=0; i<tests.size(); i++) {
153 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) delete tests[i]->conditions[j];
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 bool FGSwitch::Run(void )
165 double default_output=0.0;
167 for (unsigned int i=0; i<tests.size(); i++) {
168 if (tests[i]->Logic == eDefault) {
169 default_output = tests[i]->GetValue();
170 } else if (tests[i]->Logic == eAND) {
172 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
173 if (!tests[i]->conditions[j]->Evaluate()) pass = false;
175 } else if (tests[i]->Logic == eOR) {
177 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
178 if (tests[i]->conditions[j]->Evaluate()) pass = true;
181 cerr << "Invalid logic test" << endl;
185 Output = tests[i]->GetValue();
190 if (!pass) Output = default_output;
193 if (IsOutput) SetOutput();
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 // The bitmasked value choices are as follows:
200 // unset: In this case (the default) JSBSim would only print
201 // out the normally expected messages, essentially echoing
202 // the config files as they are read. If the environment
203 // variable is not set, debug_lvl is set to 1 internally
204 // 0: This requests JSBSim not to output any messages
206 // 1: This value explicity requests the normal JSBSim
208 // 2: This value asks for a message to be printed out when
209 // a class is instantiated
210 // 4: When this value is set, a message is displayed when a
211 // FGModel object executes its Run() method
212 // 8: When this value is set, various runtime state variables
213 // are printed out periodically
214 // 16: When set various parameters are sanity checked and
215 // a message is printed out when they go out of bounds
217 void FGSwitch::Debug(int from)
219 string comp, scratch;
223 if (debug_lvl <= 0) return;
225 if (debug_lvl & 1) { // Standard console startup message output
226 if (from == 0) { // Constructor
227 for (unsigned int i=0; i<tests.size(); i++) {
231 switch(tests[i]->Logic) {
234 cerr << "Unset logic for test condition" << endl;
243 scratch = " by default.";
247 cerr << "Unknown logic for test condition" << endl;
250 if (tests[i]->OutputProp != 0L)
251 if (tests[i]->sign < 0)
252 cout << indent << "Switch VALUE is - " << tests[i]->OutputProp->GetName() << scratch << endl;
254 cout << indent << "Switch VALUE is " << tests[i]->OutputProp->GetName() << scratch << endl;
256 cout << indent << "Switch VALUE is " << tests[i]->OutputVal << scratch << endl;
259 for (unsigned int j=0; j<tests[i]->conditions.size(); j++) {
260 if (!first) cout << indent << comp << " ";
261 else cout << indent << " ";
263 tests[i]->conditions[j]->PrintCondition();
269 for (unsigned int i=0; i<OutputNodes.size(); i++)
270 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
274 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
275 if (from == 0) cout << "Instantiated: FGSwitch" << endl;
276 if (from == 1) cout << "Destroyed: FGSwitch" << endl;
278 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
280 if (debug_lvl & 8 ) { // Runtime state variables
282 if (debug_lvl & 16) { // Sanity checking
284 if (debug_lvl & 64) {
285 if (from == 0) { // Constructor
286 cout << IdSrc << endl;
287 cout << IdHdr << endl;