FuzzyRule.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright 2010 Juan Rada-Vilela
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. /*
  13. * FuzzyRule.cpp
  14. *
  15. * Created on: Dec 13, 2009
  16. * Author: jcrada
  17. */
  18. #include "FuzzyRule.h"
  19. #include <stack>
  20. #include <iosfwd>
  21. #include "StrOp.h"
  22. namespace fl {
  23. const std::string FuzzyRule::FR_IF = "if";
  24. const std::string FuzzyRule::FR_IS = "is";
  25. const std::string FuzzyRule::FR_THEN = "then";
  26. const std::string FuzzyRule::FR_AND = "and";
  27. const std::string FuzzyRule::FR_OR = "or";
  28. const std::string FuzzyRule::FR_WITH = "with";
  29. FuzzyRule::FuzzyRule() : _antecedent(NULL) {
  30. setFuzzyOperator(fuzzyOperator().DefaultFuzzyOperator());
  31. }
  32. FuzzyRule::FuzzyRule(const FuzzyOperator& fuzzy_op)
  33. : _fuzzy_operator(&fuzzy_op), _antecedent(NULL) {
  34. }
  35. FuzzyRule::~FuzzyRule() {
  36. if (_antecedent) {
  37. delete _antecedent;
  38. }
  39. FuzzyConsequent* fconsequent = NULL;
  40. for (int i = numberOfConsequents() - 1; i >= 0; --i) {
  41. fconsequent = consequent(i);
  42. _consequents.pop_back();
  43. delete fconsequent;
  44. }
  45. }
  46. void FuzzyRule::setFuzzyOperator(const FuzzyOperator& fuzzy_op){
  47. this->_fuzzy_operator = &fuzzy_op;
  48. if (antecedent()){
  49. antecedent()->setFuzzyOperator(fuzzy_op);
  50. }
  51. //Todo: check if consequents must have a fuzzyoperator
  52. for (int i =0 ; i < numberOfConsequents() ;++i){
  53. }
  54. }
  55. const FuzzyOperator& FuzzyRule::fuzzyOperator() const{
  56. return *this->_fuzzy_operator;
  57. }
  58. void FuzzyRule::setAntecedent(FuzzyAntecedent* antecedent) {
  59. this->_antecedent = antecedent;
  60. }
  61. FuzzyAntecedent* FuzzyRule::antecedent() const {
  62. return this->_antecedent;
  63. }
  64. FuzzyConsequent* FuzzyRule::consequent(int index) const {
  65. return this->_consequents[index];
  66. }
  67. void FuzzyRule::addConsequent(FuzzyConsequent* consequent) {
  68. this->_consequents.push_back(consequent);
  69. }
  70. int FuzzyRule::numberOfConsequents() const {
  71. return this->_consequents.size();
  72. }
  73. bool FuzzyRule::isValid() const {
  74. return this->_antecedent && this->_consequents.size() > 0;
  75. }
  76. flScalar FuzzyRule::firingStrength() const {
  77. if (antecedent()) {
  78. return antecedent()->degreeOfTruth();
  79. }
  80. throw InvalidArgumentException(FL_AT,"Antecedent was not parsed correctly");
  81. }
  82. void FuzzyRule::fire(flScalar strength) {
  83. if (!FuzzyOperation::IsEq(strength, flScalar(0.0))) {
  84. // FL_LOG("DoT: " << strength << " - Fired: " << toString());
  85. for (int i = 0; i < numberOfConsequents(); ++i) {
  86. consequent(i)->execute(strength);
  87. }
  88. }
  89. }
  90. std::string FuzzyRule::toString() const {
  91. std::stringstream ss;
  92. ss << FR_IF << " " << _antecedent->toString() << " " << FR_THEN;
  93. for (int i = 0; i < numberOfConsequents(); ++i) {
  94. ss << " " + consequent(i)->toString() + (i < numberOfConsequents() - 1 ? FR_AND : "");
  95. }
  96. return ss.str();
  97. }
  98. void FuzzyRule::ExtractFromRule(const std::string& rule, std::string& antecedent, std::string& consequent) {
  99. std::vector<std::string> parse = StrOp::SplitByWord(rule, FR_IF);
  100. if (parse.size() != 1) {
  101. antecedent = "invalid";
  102. consequent = "invalid";
  103. return;
  104. }
  105. parse = StrOp::SplitByWord(parse[0], FR_THEN);
  106. if (parse.size() != 2){
  107. antecedent = "invalid";
  108. consequent = "invalid";
  109. return;
  110. }
  111. antecedent = parse[0];
  112. consequent = parse[1];
  113. }
  114. }// namespace fuzzy_lite