37 #include "FGCondition.h" 38 #include "FGPropertyValue.h" 39 #include "input_output/FGXMLElement.h" 40 #include "input_output/FGPropertyManager.h" 48 IDENT(IdSrc,
"$Id: FGCondition.cpp,v 1.21 2015/02/27 20:36:47 bcoconni Exp $");
49 IDENT(IdHdr,ID_CONDITION);
55 string FGCondition::indent =
" ";
58 FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
61 string property1, property2, logic;
62 Element* condition_element;
64 InitializeConditionals();
66 TestParam1 = TestParam2 = 0L;
72 logic = element->GetAttributeValue(
"logic");
74 if (logic ==
"OR") Logic = eOR;
75 else if (logic ==
"AND") Logic = eAND;
77 cerr <<
"Unrecognized LOGIC token " << logic << endl;
83 condition_element = element->GetElement();
84 if (condition_element != 0) {
85 while (condition_element) {
86 conditions.push_back(
new FGCondition(condition_element, PropertyManager));
87 condition_element = element->GetNextElement();
90 for (
unsigned int i=0; i<element->GetNumDataLines(); i++) {
91 string data = element->GetDataLine(i);
92 conditions.push_back(
new FGCondition(data, PropertyManager));
103 FGCondition::FGCondition(
const string& test, FGPropertyManager* PropertyManager) :
106 string property1, property2, compare_string;
107 vector <string> test_strings;
109 InitializeConditionals();
111 TestParam1 = TestParam2 = 0L;
113 Comparison = ecUndef;
117 test_strings = split(test,
' ');
118 if (test_strings.size() == 3) {
119 property1 = test_strings[0];
120 conditional = test_strings[1];
121 property2 = test_strings[2];
123 cerr << endl <<
" Conditional test is invalid: \"" << test
124 <<
"\" has " << test_strings.size() <<
" elements in the " 125 <<
"test condition." << endl;
129 FGPropertyNode *node = PropertyManager->GetNode(property1,
false);
131 TestParam1 =
new FGPropertyValue(node);
133 TestParam1 =
new FGPropertyValue(property1, PropertyManager);
135 Comparison = mComparison[conditional];
136 if (Comparison == ecUndef) {
137 throw(
"Comparison operator: \""+conditional+
"\" does not exist. Please check the conditional.");
139 if (is_number(property2)) {
140 TestValue = atof(property2.c_str());
142 node = PropertyManager->GetNode(property2,
false);
144 TestParam2 =
new FGPropertyValue(node);
146 TestParam2 =
new FGPropertyValue(property2, PropertyManager);
153 void FGCondition::InitializeConditionals(
void)
155 mComparison[
"EQ"] = eEQ;
156 mComparison[
"NE"] = eNE;
157 mComparison[
"GT"] = eGT;
158 mComparison[
"GE"] = eGE;
159 mComparison[
"LT"] = eLT;
160 mComparison[
"LE"] = eLE;
161 mComparison[
"eq"] = eEQ;
162 mComparison[
"ne"] = eNE;
163 mComparison[
"gt"] = eGT;
164 mComparison[
"ge"] = eGE;
165 mComparison[
"lt"] = eLT;
166 mComparison[
"le"] = eLE;
167 mComparison[
"=="] = eEQ;
168 mComparison[
"!="] = eNE;
169 mComparison[
">"] = eGT;
170 mComparison[
">="] = eGE;
171 mComparison[
"<"] = eLT;
172 mComparison[
"<="] = eLE;
177 FGCondition::~FGCondition(
void)
181 for (
unsigned int i=0; i<conditions.size(); i++)
delete conditions[i];
188 bool FGCondition::Evaluate(
void )
193 if (TestParam1 == 0L) {
198 for (
unsigned int i=0; i<conditions.size(); i++) {
199 if (!conditions[i]->Evaluate()) pass =
false;
205 for (
unsigned int i=0; i<conditions.size(); i++) {
206 if (conditions[i]->Evaluate()) pass =
true;
213 if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
214 else compareValue = TestValue;
216 switch (Comparison) {
218 cerr <<
"Undefined comparison operator." << endl;
221 pass = TestParam1->getDoubleValue() == compareValue;
224 pass = TestParam1->getDoubleValue() != compareValue;
227 pass = TestParam1->getDoubleValue() > compareValue;
230 pass = TestParam1->getDoubleValue() >= compareValue;
233 pass = TestParam1->getDoubleValue() < compareValue;
236 pass = TestParam1->getDoubleValue() <= compareValue;
239 cerr <<
"Unknown comparison operator." << endl;
248 void FGCondition::PrintCondition(
string indent)
257 cerr <<
"unset logic for test condition" << endl;
260 scratch = indent +
"if all of the following are true: {";
263 scratch = indent +
"if any of the following are true: {";
266 scratch =
" UNKNOWN";
267 cerr <<
"Unknown logic for test condition" << endl;
269 cout << scratch << endl;
271 for (
unsigned int i=0; i<conditions.size(); i++) {
272 conditions[i]->PrintCondition(indent +
" ");
276 cout << indent <<
"}";
279 if (TestParam2 != 0L)
280 cout << indent << TestParam1->GetName() <<
" " 281 << conditional <<
" " 282 << TestParam2->GetName();
284 cout << indent << TestParam1->GetName() <<
" " 285 << conditional <<
" " << TestValue;
308 void FGCondition::Debug(
int from)
310 if (debug_lvl <= 0)
return;
317 if (debug_lvl & 2 ) {
318 if (from == 0) cout <<
"Instantiated: FGCondition" << endl;
319 if (from == 1) cout <<
"Destroyed: FGCondition" << endl;
321 if (debug_lvl & 4 ) {
323 if (debug_lvl & 8 ) {
325 if (debug_lvl & 16) {
327 if (debug_lvl & 64) {
329 cout << IdSrc << endl;
330 cout << IdHdr << endl;