TakagiSugenoRule.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "TakagiSugenoRule.h"
  13. #include "TakagiSugenoConsequent.h"
  14. #include "DescriptiveAntecedent.h"
  15. #include "StrOp.h"
  16. namespace fl {
  17. TakagiSugenoRule::TakagiSugenoRule() : FuzzyRule() {
  18. }
  19. TakagiSugenoRule::TakagiSugenoRule(const std::string& rule, const FuzzyEngine& engine)
  20. : FuzzyRule() {
  21. try {
  22. parse(rule, engine);
  23. } catch (ParsingException& e) {
  24. FL_LOG(e.toString());
  25. }
  26. }
  27. TakagiSugenoRule::~TakagiSugenoRule() {
  28. }
  29. void TakagiSugenoRule::parse(const std::string& rule, const FuzzyEngine& engine) {
  30. std::string str_antecedent, str_consequent;
  31. ExtractFromRule(rule, str_antecedent, str_consequent);
  32. DescriptiveAntecedent* obj_antecedent = new DescriptiveAntecedent;
  33. obj_antecedent->parse(str_antecedent, engine);
  34. setAntecedent(obj_antecedent);
  35. std::vector<std::string> consequents = StrOp::SplitByWord(str_consequent, FuzzyRule::FR_AND);
  36. TakagiSugenoConsequent* obj_consequent = NULL;
  37. for (size_t i = 0; i < consequents.size(); ++i) {
  38. obj_consequent = new TakagiSugenoConsequent;
  39. try {
  40. std::string x = consequents[i];
  41. StrOp::FindReplace(x,"="," = ");
  42. obj_consequent->parse(x, engine);
  43. } catch (ParsingException& e) {
  44. delete obj_consequent;
  45. throw e;
  46. }
  47. addConsequent(obj_consequent);
  48. }
  49. }
  50. }