| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGDistributor.h |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 09/27/2013 |
| 6 | |
| 7 | ------------- Copyright (C) 2013 [email protected] ------------- |
| 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 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | |
| 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 30 | SENTRY |
| 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 32 | |
| 33 | #ifndef FGDISTRIBUTOR_H |
| 34 | #define FGDISTRIBUTOR_H |
| 35 | |
| 36 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 | INCLUDES |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 39 | |
| 40 | #include "FGFCSComponent.h" |
| 41 | #include "math/FGCondition.h" |
| 42 | #include "math/FGParameterValue.h" |
| 43 | |
| 44 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 45 | FORWARD DECLARATIONS |
| 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 47 | |
| 48 | namespace JSBSim { |
| 49 | |
| 50 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 51 | CLASS DOCUMENTATION |
| 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 53 | |
| 54 | /** Encapsulates a distributor for the flight control system. |
| 55 | |
| 56 | The distributor component models a distributor - |
| 57 | |
| 58 | Within a test, additional tests can be specified, which allows for |
| 59 | complex groupings of logical comparisons. Each test contains |
| 60 | additional conditions, as well as possibly additional tests. |
| 61 | |
| 62 | @code{.xml} |
| 63 | <distributor name="name/is/irrelevant" type="exclusive|inclusive"> |
| 64 | |
| 65 | <case> |
| 66 | [<test logic="{AND|OR}" value="{property|value}"> |
| 67 | {property} {conditional} {property|value} |
| 68 | <test logic="{AND|OR}"> |
| 69 | {property} {conditional} {property|value} |
| 70 | ... |
| 71 | </test> |
| 72 | ... |
| 73 | </test>] <!-- Optional <test> one time. A <case> without a <test> is always executed --> |
| 74 | <property value="number|property"> property_name </property> |
| 75 | ... |
| 76 | </case> |
| 77 | |
| 78 | ... <!-- Additional cases --> |
| 79 | |
| 80 | </distributor> |
| 81 | @endcode |
| 82 | |
| 83 | If the distributor type is exclusive no further <case> components are evaluated |
| 84 | once a case <test> condition has been found to be true. |
| 85 | |
| 86 | If the distributor type is inclusive all the <case> components are evaluated |
| 87 | no matter how many <case> conditions are true. |
| 88 | |
| 89 | Whether the distributor type is inclusive or exclusive the <case> component |
| 90 | without <test> is always executed. |
| 91 | |
| 92 | Here's an example that evaluate the sign of the property test/number and sets |
| 93 | test/default to the value of test/reference. |
| 94 | |
| 95 | @code{.xml} |
| 96 | <distributor> |
| 97 | <case> |
| 98 | <test> |
| 99 | test/number lt 0.0 |
| 100 | </test> |
| 101 | <property value="-1.0"> test/sign </property> |
| 102 | </case> |
| 103 | <case> |
| 104 | <test> |
| 105 | test/number ge 0.0 |
| 106 | </test> |
| 107 | <property value="1.0"> test/sign </property> |
| 108 | </case> |
| 109 | <!-- default case --> |
| 110 | <case> |
| 111 | <property value="test/reference"> test/default </property> |
| 112 | </case> |
| 113 | </distributor> |
| 114 | @endcode |
| 115 | |
| 116 | Note: In the "logic" attribute, "AND" is the default logic, if none is supplied. |
| 117 | |
| 118 | @author Jon S. Berndt |
| 119 | */ |
| 120 | |
| 121 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 122 | CLASS DECLARATION |
| 123 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 124 | |
| 125 | class FGDistributor : public FGFCSComponent |
| 126 | { |
| 127 | public: |
| 128 | /** Constructor |
| 129 | @param fcs a pointer to the parent FGFCS class |
| 130 | @param element a pointer to the Element (from the config file XML tree) |
| 131 | that represents this distributor component */ |
| 132 | FGDistributor(FGFCS* fcs, Element* element); |
| 133 | |
| 134 | /// Destructor |
| 135 | ~FGDistributor(); |
| 136 | |
| 137 | /** Executes the distributor logic. |
| 138 | @return true - always*/ |
| 139 | bool Run(void) override; |
| 140 | |
| 141 | private: |
| 142 | |
| 143 | enum eType {eInclusive=0, eExclusive} Type; |
| 144 | |
| 145 | class PropValPair { |
| 146 | public: |
| 147 | PropValPair(const std::string& prop, const std::string& val, |
| 148 | FGPropertyManager* propMan) |
| 149 | : Prop(new FGPropertyValue(prop, propMan)), |
| 150 | Val(new FGParameterValue(val, propMan)) {} |
| 151 | |
| 152 | void SetPropToValue() { |
| 153 | try { |
| 154 | Prop->SetValue(Val->GetValue()); |
| 155 | } |
| 156 | catch(...) { |
| 157 | throw(Prop->GetName()+" in distributor component is not known"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | std::string GetPropName() { return Prop->GetName(); } |
| 162 | std::string GetValString() { return Val->GetName(); } |
| 163 | bool GetLateBoundProp() { return Prop->IsLateBound(); } |
| 164 | bool GetLateBoundValue() {return Val->IsLateBound(); |
| 165 | } |
| 166 | private: |
| 167 | FGPropertyValue_ptr Prop; |
| 168 | FGParameterValue_ptr Val; |
| 169 | }; |
| 170 | |
| 171 | class Case { |
| 172 | public: |
| 173 | Case() : Test(nullptr) {} |
| 174 | |
| 175 | ~Case() { |
| 176 | for (auto pair: PropValPairs) delete pair; |
| 177 | } |
| 178 | |
| 179 | void SetTest(FGCondition* test) {Test = test;} |
| 180 | FGCondition* GetTest(void) {return Test;} |
| 181 | void AddPropValPair(PropValPair* pvPair) {PropValPairs.push_back(pvPair);} |
| 182 | void SetPropValPairs() { |
| 183 | for (auto pair: PropValPairs) pair->SetPropToValue(); |
| 184 | } |
| 185 | std::vector<PropValPair*>::const_iterator IterPropValPairs(void) const |
| 186 | { return PropValPairs.cbegin(); } |
| 187 | std::vector<PropValPair*>::const_iterator EndPropValPairs(void) const |
| 188 | { return PropValPairs.cend(); } |
| 189 | bool HasTest() {return Test != nullptr;} |
| 190 | bool GetTestResult() { return Test->Evaluate(); } |
| 191 | |
| 192 | private: |
| 193 | FGCondition* Test; |
| 194 | std::vector <PropValPair*> PropValPairs; |
| 195 | }; |
| 196 | |
| 197 | std::vector <Case*> Cases; |
| 198 | |
| 199 | void Debug(int from) override; |
| 200 | }; |
| 201 | } |
| 202 | #endif |