1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGDistributor.cpp
4 Author: Jon S. Berndt
5 Date started: 9/2013
6
7 ------------- Copyright (C) 2013 -------------
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
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later 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
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on 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 Also, see the header file (FGDistributor.h) for further details.
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include "FGDistributor.h"
43
44 using namespace std;
45
46 namespace JSBSim {
47
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 CLASS IMPLEMENTATION
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
FGDistributor(FGFCS * fcs,Element * element)52 FGDistributor::FGDistributor(FGFCS* fcs, Element* element)
53 : FGFCSComponent(fcs, element)
54 {
55 bind(element); // Bind() this component here in case it is used in its own
56 // definition for a sample-and-hold
57
58 string type_string = element->GetAttributeValue("type");
59 if (type_string == "inclusive") Type = eInclusive;
60 else if (type_string == "exclusive") Type = eExclusive;
61 else {
62 throw("Not a known Distributor type, "+type_string);
63 }
64
65 Element* case_element = element->FindElement("case");
66 while (case_element) {
67 Case* current_case = new Case;
68 Element* test_element = case_element->FindElement("test");
69 if (test_element) current_case->SetTest(new FGCondition(test_element, PropertyManager));
70 Element* prop_val_element = case_element->FindElement("property");
71 while (prop_val_element) {
72 string value_string = prop_val_element->GetAttributeValue("value");
73 string property_string = prop_val_element->GetDataLine();
74 current_case->AddPropValPair(new PropValPair(property_string, value_string, PropertyManager));
75 prop_val_element = case_element->FindNextElement("property");
76 }
77 Cases.push_back(current_case);
78 case_element = element->FindNextElement("case");
79 }
80
81 Debug(0);
82 }
83
84 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85
~FGDistributor()86 FGDistributor::~FGDistributor()
87 {
88 for (auto Case: Cases) delete Case;
89 Debug(1);
90 }
91
92 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93
Run(void)94 bool FGDistributor::Run(void )
95 {
96 bool completed = false;
97 for (auto Case: Cases) { // Loop through all Cases
98 if (Case->HasTest()) {
99 if (Case->GetTestResult() && !((Type == eExclusive) && completed)) {
100 Case->SetPropValPairs();
101 completed = true;
102 }
103 } else { // If no test present, execute always
104 Case->SetPropValPairs();
105 }
106 }
107
108 return true;
109 }
110
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 // The bitmasked value choices are as follows:
113 // unset: In this case (the default) JSBSim would only print
114 // out the normally expected messages, essentially echoing
115 // the config files as they are read. If the environment
116 // variable is not set, debug_lvl is set to 1 internally
117 // 0: This requests JSBSim not to output any messages
118 // whatsoever.
119 // 1: This value explicity requests the normal JSBSim
120 // startup messages
121 // 2: This value asks for a message to be printed out when
122 // a class is instantiated
123 // 4: When this value is set, a message is displayed when a
124 // FGModel object executes its Run() method
125 // 8: When this value is set, various runtime state variables
126 // are printed out periodically
127 // 16: When set various parameters are sanity checked and
128 // a message is printed out when they go out of bounds
129
Debug(int from)130 void FGDistributor::Debug(int from)
131 {
132 if (debug_lvl <= 0) return;
133
134 if (debug_lvl & 1) { // Standard console startup message output
135 if (from == 0) { // Constructor
136 unsigned int ctr=0;
137 for (auto Case: Cases) {
138 std::cout << " Case: " << ctr << endl;
139 if (Case->HasTest()) {
140 Case->GetTest()->PrintCondition(" ");
141 } else {
142 std::cout << " Set these properties by default: " << std::endl;
143 }
144 std::cout << std::endl;
145 for (auto propVal = Case->IterPropValPairs(); propVal != Case->EndPropValPairs(); ++propVal) {
146 std::cout << " Set property " << (*propVal)->GetPropName();
147 if ((*propVal)->GetLateBoundProp()) std::cout << " (late bound)";
148 std::cout << " to " << (*propVal)->GetValString();
149 if ((*propVal)->GetLateBoundValue()) std::cout << " (late bound)";
150 std::cout << std::endl;
151 }
152 ctr++;
153 }
154 }
155 }
156 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
157 if (from == 0) cout << "Instantiated: FGDistributor" << endl;
158 if (from == 1) cout << "Destroyed: FGDistributor" << endl;
159 }
160 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
161 }
162 if (debug_lvl & 8 ) { // Runtime state variables
163 }
164 if (debug_lvl & 16) { // Sanity checking
165 }
166 if (debug_lvl & 64) {
167 if (from == 0) { // Constructor
168 }
169 }
170 }
171
172 } //namespace JSBSim
173