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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73 FGSwitch::FGSwitch(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
77 struct test *current_test;
78 struct FGCondition *current_condition;
80 Type = AC_cfg->GetValue("TYPE");
81 Name = AC_cfg->GetValue("NAME");
83 AC_cfg->GetNextConfigLine();
84 while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
86 // See the above documentation, or the API docs, for information on what
87 // the SWITCH component is supposed to look like in the configuration file.
88 // Below, the switch component is read in.
90 if (token == "TEST") {
91 tests.push_back(*(new test));
92 current_test = &tests.back();
94 if (AC_cfg->GetValue("LOGIC") == "OR") {
95 current_test->Logic = eOR;
96 } else if (AC_cfg->GetValue("LOGIC") == "AND") {
97 current_test->Logic = eAND;
98 } else if (AC_cfg->GetValue("LOGIC") == "DEFAULT") {
99 current_test->Logic = eDefault;
101 cerr << "Unrecognized LOGIC token in switch component: " << Name << endl;
104 value = AC_cfg->GetValue("VALUE");
106 cerr << "No VALUE supplied for switch component: " << Name << endl;
108 if (value.find_first_not_of("-.0123456789eE") == string::npos) {
109 // if true (and execution falls into this block), "value" is a number.
110 current_test->OutputVal = atof(value.c_str());
112 // "value" must be a property if execution passes to here.
113 current_test->OutputProp = PropertyManager->GetNode(value);
117 AC_cfg->GetNextConfigLine();
118 while (AC_cfg->GetValue() != string("/TEST")) {
119 current_test->conditions.push_back(*(new FGCondition(AC_cfg, PropertyManager)));
122 AC_cfg->GetNextConfigLine();
125 FGFCSComponent::bind();
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 FGSwitch::~FGSwitch()
137 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 bool FGSwitch::Run(void )
141 vector <test>::iterator iTests = tests.begin();
142 vector <FGCondition>::iterator iConditions;
145 FGFCSComponent::Run(); // call the base class for initialization of Input
147 while (iTests < tests.end()) {
148 iConditions = iTests->conditions.begin();
150 if (iTests->Logic == eDefault) {
151 Output = iTests->GetValue();
152 } else if (iTests->Logic == eAND) {
154 while (iConditions < iTests->conditions.end()) {
155 if (!iConditions->Evaluate()) pass = false;
158 } else if (iTests->Logic == eOR) {
160 while (iConditions < iTests->conditions.end()) {
161 if (iConditions->Evaluate()) pass = true;
165 cerr << "Invalid logic test" << endl;
169 Output = iTests->GetValue();
178 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 // The bitmasked value choices are as follows:
180 // unset: In this case (the default) JSBSim would only print
181 // out the normally expected messages, essentially echoing
182 // the config files as they are read. If the environment
183 // variable is not set, debug_lvl is set to 1 internally
184 // 0: This requests JSBSim not to output any messages
186 // 1: This value explicity requests the normal JSBSim
188 // 2: This value asks for a message to be printed out when
189 // a class is instantiated
190 // 4: When this value is set, a message is displayed when a
191 // FGModel object executes its Run() method
192 // 8: When this value is set, various runtime state variables
193 // are printed out periodically
194 // 16: When set various parameters are sanity checked and
195 // a message is printed out when they go out of bounds
197 void FGSwitch::Debug(int from)
199 vector <test>::iterator iTests = tests.begin();
200 vector <FGCondition>::iterator iConditions;
201 string comp, scratch;
205 if (debug_lvl <= 0) return;
207 if (debug_lvl & 1) { // Standard console startup message output
208 if (from == 0) { // Constructor
209 while (iTests < tests.end()) {
213 switch(iTests->Logic) {
216 cerr << "Unset logic for test condition" << endl;
225 scratch = " by default.";
229 cerr << "Unknown logic for test condition" << endl;
232 if (iTests->OutputProp != 0L)
233 cout << indent << "Switch VALUE is " << iTests->OutputProp->GetName() << scratch << endl;
235 cout << indent << "Switch VALUE is " << iTests->OutputVal << scratch << endl;
237 iConditions = iTests->conditions.begin();
239 while (iConditions < iTests->conditions.end()) {
240 if (!first) cout << indent << comp << " ";
241 else cout << indent << " ";
243 iConditions->PrintCondition();
252 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
253 if (from == 0) cout << "Instantiated: FGSwitch" << endl;
254 if (from == 1) cout << "Destroyed: FGSwitch" << endl;
256 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
258 if (debug_lvl & 8 ) { // Runtime state variables
260 if (debug_lvl & 16) { // Sanity checking
262 if (debug_lvl & 64) {
263 if (from == 0) { // Constructor
264 cout << IdSrc << endl;
265 cout << IdHdr << endl;