2004-03-15 09:24:57 +00:00
|
|
|
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
Module: FGCondition.cpp
|
|
|
|
Author: Jon S. Berndt
|
|
|
|
Date started: 1/2/2003
|
|
|
|
|
|
|
|
-------------- Copyright (C) 2003 Jon S. Berndt (jsb@hal-pc.org) --------------
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
2007-01-15 12:48:54 +00:00
|
|
|
the terms of the GNU Lesser General Public License as published by the Free Software
|
2004-03-15 09:24:57 +00:00
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2007-01-15 12:48:54 +00:00
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
2004-03-15 09:24:57 +00:00
|
|
|
details.
|
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along with
|
2004-03-15 09:24:57 +00:00
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
Further information about the GNU Lesser General Public License can also be found on
|
2004-03-15 09:24:57 +00:00
|
|
|
the world wide web at http://www.gnu.org.
|
|
|
|
|
|
|
|
HISTORY
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
COMMENTS, REFERENCES, and NOTES
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
INCLUDES
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
|
|
|
|
|
|
|
|
#include "FGCondition.h"
|
|
|
|
|
|
|
|
namespace JSBSim {
|
|
|
|
|
|
|
|
static const char *IdSrc = "$Id$";
|
|
|
|
static const char *IdHdr = ID_CONDITION;
|
|
|
|
|
|
|
|
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
CLASS IMPLEMENTATION
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
|
|
|
|
|
|
|
|
string FGCondition::indent = " ";
|
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
// This constructor is called when tests are inside an element
|
2006-01-12 15:04:22 +00:00
|
|
|
FGCondition::FGCondition(Element* element, FGPropertyManager* PropertyManager) :
|
|
|
|
PropertyManager(PropertyManager), isGroup(true)
|
2004-03-15 09:24:57 +00:00
|
|
|
{
|
2006-01-12 15:04:22 +00:00
|
|
|
string property1, property2, logic;
|
|
|
|
Element* condition_element;
|
|
|
|
|
|
|
|
InitializeConditionals();
|
2004-03-15 09:24:57 +00:00
|
|
|
|
|
|
|
TestParam1 = TestParam2 = 0L;
|
|
|
|
TestValue = 0.0;
|
|
|
|
Comparison = ecUndef;
|
|
|
|
Logic = elUndef;
|
|
|
|
conditions.clear();
|
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
logic = element->GetAttributeValue("logic");
|
2007-01-15 12:48:54 +00:00
|
|
|
if (!logic.empty()) {
|
|
|
|
if (logic == "OR") Logic = eOR;
|
|
|
|
else if (logic == "AND") Logic = eAND;
|
|
|
|
else { // error
|
|
|
|
cerr << "Unrecognized LOGIC token " << logic << endl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Logic = eAND; // default
|
2006-01-12 15:04:22 +00:00
|
|
|
}
|
2007-01-15 12:48:54 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
condition_element = element->GetElement();
|
|
|
|
while (condition_element) {
|
2008-07-10 17:23:02 +00:00
|
|
|
conditions.push_back(new FGCondition(condition_element, PropertyManager));
|
2006-01-12 15:04:22 +00:00
|
|
|
condition_element = element->GetNextElement();
|
|
|
|
}
|
2007-01-15 12:48:54 +00:00
|
|
|
for (unsigned int i=0; i<element->GetNumDataLines(); i++) {
|
|
|
|
string data = element->GetDataLine(i);
|
2008-07-10 17:23:02 +00:00
|
|
|
conditions.push_back(new FGCondition(data, PropertyManager));
|
2006-01-12 15:04:22 +00:00
|
|
|
}
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
Debug(0);
|
|
|
|
}
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2007-01-15 12:48:54 +00:00
|
|
|
//This constructor is called when there are no nested test groups inside the
|
|
|
|
// condition
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
FGCondition::FGCondition(string test, FGPropertyManager* PropertyManager) :
|
|
|
|
PropertyManager(PropertyManager), isGroup(false)
|
|
|
|
{
|
|
|
|
string property1, property2, compare_string;
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
InitializeConditionals();
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
TestParam1 = TestParam2 = 0L;
|
|
|
|
TestValue = 0.0;
|
|
|
|
Comparison = ecUndef;
|
|
|
|
Logic = elUndef;
|
|
|
|
conditions.clear();
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
unsigned int start = 0, end = 0;
|
2006-01-12 15:04:22 +00:00
|
|
|
start = test.find_first_not_of(" ");
|
|
|
|
end = test.find_first_of(" ", start+1);
|
|
|
|
property1 = test.substr(start,end-start);
|
|
|
|
start = test.find_first_not_of(" ",end);
|
|
|
|
end = test.find_first_of(" ",start+1);
|
|
|
|
conditional = test.substr(start,end-start);
|
|
|
|
start = test.find_first_not_of(" ",end);
|
|
|
|
end = test.find_first_of(" ",start+1);
|
|
|
|
property2 = test.substr(start,end-start);
|
|
|
|
|
|
|
|
TestParam1 = PropertyManager->GetNode(property1, true);
|
|
|
|
Comparison = mComparison[conditional];
|
|
|
|
if (property2.find_first_not_of("-.0123456789eE") == string::npos) {
|
|
|
|
TestValue = atof(property2.c_str());
|
|
|
|
} else {
|
|
|
|
TestParam2 = PropertyManager->GetNode(property2, true);
|
2004-03-15 09:24:57 +00:00
|
|
|
}
|
2006-01-12 15:04:22 +00:00
|
|
|
}
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2006-01-12 15:04:22 +00:00
|
|
|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
void FGCondition::InitializeConditionals(void)
|
|
|
|
{
|
|
|
|
mComparison["EQ"] = eEQ;
|
|
|
|
mComparison["NE"] = eNE;
|
|
|
|
mComparison["GT"] = eGT;
|
|
|
|
mComparison["GE"] = eGE;
|
|
|
|
mComparison["LT"] = eLT;
|
|
|
|
mComparison["LE"] = eLE;
|
|
|
|
mComparison["eq"] = eEQ;
|
|
|
|
mComparison["ne"] = eNE;
|
|
|
|
mComparison["gt"] = eGT;
|
|
|
|
mComparison["ge"] = eGE;
|
|
|
|
mComparison["lt"] = eLT;
|
|
|
|
mComparison["le"] = eLE;
|
|
|
|
mComparison["=="] = eEQ;
|
|
|
|
mComparison["!="] = eNE;
|
|
|
|
mComparison[">"] = eGT;
|
|
|
|
mComparison[">="] = eGE;
|
|
|
|
mComparison["<"] = eLT;
|
|
|
|
mComparison["<="] = eLE;
|
2004-03-15 09:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
FGCondition::~FGCondition(void)
|
|
|
|
{
|
2008-07-10 17:23:02 +00:00
|
|
|
for (unsigned int i=0; i<conditions.size(); i++) delete conditions[i];
|
|
|
|
|
2004-03-15 09:24:57 +00:00
|
|
|
Debug(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
bool FGCondition::Evaluate(void )
|
|
|
|
{
|
|
|
|
bool pass = false;
|
|
|
|
double compareValue;
|
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
if (TestParam1 == 0L) {
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
if (Logic == eAND) {
|
2004-03-15 09:24:57 +00:00
|
|
|
|
2007-01-15 12:48:54 +00:00
|
|
|
pass = true;
|
2008-07-10 17:23:02 +00:00
|
|
|
for (unsigned int i=0; i<conditions.size(); i++) {
|
|
|
|
if (!conditions[i]->Evaluate()) pass = false;
|
2007-01-15 12:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else { // Logic must be eOR
|
|
|
|
|
|
|
|
pass = false;
|
2008-07-10 17:23:02 +00:00
|
|
|
for (unsigned int i=0; i<conditions.size(); i++) {
|
|
|
|
if (conditions[i]->Evaluate()) pass = true;
|
2007-01-15 12:48:54 +00:00
|
|
|
}
|
2004-03-15 09:24:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (TestParam2 != 0L) compareValue = TestParam2->getDoubleValue();
|
|
|
|
else compareValue = TestValue;
|
|
|
|
|
|
|
|
switch (Comparison) {
|
|
|
|
case ecUndef:
|
|
|
|
cerr << "Undefined comparison operator." << endl;
|
|
|
|
break;
|
|
|
|
case eEQ:
|
|
|
|
pass = TestParam1->getDoubleValue() == compareValue;
|
|
|
|
break;
|
|
|
|
case eNE:
|
|
|
|
pass = TestParam1->getDoubleValue() != compareValue;
|
|
|
|
break;
|
|
|
|
case eGT:
|
|
|
|
pass = TestParam1->getDoubleValue() > compareValue;
|
|
|
|
break;
|
|
|
|
case eGE:
|
|
|
|
pass = TestParam1->getDoubleValue() >= compareValue;
|
|
|
|
break;
|
|
|
|
case eLT:
|
|
|
|
pass = TestParam1->getDoubleValue() < compareValue;
|
|
|
|
break;
|
|
|
|
case eLE:
|
|
|
|
pass = TestParam1->getDoubleValue() <= compareValue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cerr << "Unknown comparison operator." << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
void FGCondition::PrintCondition(void )
|
|
|
|
{
|
|
|
|
string scratch;
|
|
|
|
|
|
|
|
if (isGroup) {
|
|
|
|
switch(Logic) {
|
|
|
|
case (elUndef):
|
|
|
|
scratch = " UNSET";
|
|
|
|
cerr << "unset logic for test condition" << endl;
|
|
|
|
break;
|
|
|
|
case (eAND):
|
2007-01-15 12:48:54 +00:00
|
|
|
scratch = " if all of the following are true:";
|
2004-03-15 09:24:57 +00:00
|
|
|
break;
|
|
|
|
case (eOR):
|
|
|
|
scratch = " if any of the following are true:";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
scratch = " UNKNOWN";
|
|
|
|
cerr << "Unknown logic for test condition" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << scratch << endl;
|
2008-07-10 17:23:02 +00:00
|
|
|
for (unsigned int i=0; i<conditions.size(); i++) conditions[i]->PrintCondition();
|
|
|
|
|
2004-03-15 09:24:57 +00:00
|
|
|
} else {
|
|
|
|
if (TestParam2 != 0L)
|
2007-01-15 12:48:54 +00:00
|
|
|
cout << " " << TestParam1->GetName() << " " << conditional << " " << TestParam2->GetName();
|
2004-03-15 09:24:57 +00:00
|
|
|
else
|
2007-01-15 12:48:54 +00:00
|
|
|
cout << " " << TestParam1->GetName() << " " << conditional << " " << TestValue;
|
2004-03-15 09:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
// The bitmasked value choices are as follows:
|
|
|
|
// unset: In this case (the default) JSBSim would only print
|
|
|
|
// out the normally expected messages, essentially echoing
|
|
|
|
// the config files as they are read. If the environment
|
|
|
|
// variable is not set, debug_lvl is set to 1 internally
|
|
|
|
// 0: This requests JSBSim not to output any messages
|
|
|
|
// whatsoever.
|
|
|
|
// 1: This value explicity requests the normal JSBSim
|
|
|
|
// startup messages
|
|
|
|
// 2: This value asks for a message to be printed out when
|
|
|
|
// a class is instantiated
|
|
|
|
// 4: When this value is set, a message is displayed when a
|
|
|
|
// FGModel object executes its Run() method
|
|
|
|
// 8: When this value is set, various runtime state variables
|
|
|
|
// are printed out periodically
|
|
|
|
// 16: When set various parameters are sanity checked and
|
|
|
|
// a message is printed out when they go out of bounds
|
|
|
|
|
|
|
|
void FGCondition::Debug(int from)
|
|
|
|
{
|
|
|
|
if (debug_lvl <= 0) return;
|
|
|
|
|
|
|
|
if (debug_lvl & 1) { // Standard console startup message output
|
|
|
|
if (from == 0) { // Constructor
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
|
|
|
|
if (from == 0) cout << "Instantiated: FGCondition" << endl;
|
|
|
|
if (from == 1) cout << "Destroyed: FGCondition" << endl;
|
|
|
|
}
|
|
|
|
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
|
|
|
|
}
|
|
|
|
if (debug_lvl & 8 ) { // Runtime state variables
|
|
|
|
}
|
|
|
|
if (debug_lvl & 16) { // Sanity checking
|
|
|
|
}
|
|
|
|
if (debug_lvl & 64) {
|
|
|
|
if (from == 0) { // Constructor
|
|
|
|
cout << IdSrc << endl;
|
|
|
|
cout << IdHdr << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace JSBSim
|
|
|
|
|