Function.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Author: Juan Rada-Vilela, Ph.D.
  3. Copyright (C) 2010-2014 FuzzyLite Limited
  4. All rights reserved
  5. This file is part of fuzzylite.
  6. fuzzylite is free software: you can redistribute it and/or modify it under
  7. the terms of the GNU Lesser General Public License as published by the Free
  8. Software Foundation, either version 3 of the License, or (at your option)
  9. any later version.
  10. fuzzylite is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  13. for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with fuzzylite. If not, see <http://www.gnu.org/licenses/>.
  16. fuzzylite™ is a trademark of FuzzyLite Limited.
  17. */
  18. #ifndef FL_FUNCTION_H
  19. #define FL_FUNCTION_H
  20. #include "fl/term/Term.h"
  21. #include <map>
  22. #include <string>
  23. namespace fl {
  24. class Engine;
  25. class FL_API Function : public Term {
  26. /****************************
  27. * Parsing Elements
  28. ****************************/
  29. public:
  30. typedef scalar(*Unary)(scalar);
  31. typedef scalar(*Binary)(scalar, scalar);
  32. struct FL_API Element {
  33. enum Type {
  34. OPERATOR, FUNCTION
  35. };
  36. std::string name;
  37. std::string description;
  38. Type type;
  39. Unary unary;
  40. Binary binary;
  41. int arity;
  42. int precedence; //Operator
  43. int associativity;
  44. Element(const std::string& name, const std::string& description, Type type);
  45. Element(const std::string& name, const std::string& description,
  46. Type type, Unary unary, int precedence = 0, int associativity = -1);
  47. Element(const std::string& name, const std::string& description,
  48. Type type, Binary binary, int precedence = 0, int associativity = -1);
  49. virtual ~Element();
  50. FL_DEFAULT_COPY_AND_MOVE(Element)
  51. virtual bool isOperator() const;
  52. virtual bool isFunction() const;
  53. virtual Element* clone() const;
  54. virtual std::string toString() const;
  55. };
  56. /**************************
  57. * Tree elements, wrap Elements into Nodes.
  58. **************************/
  59. struct FL_API Node {
  60. FL_unique_ptr<Element> element;
  61. FL_unique_ptr<Node> left;
  62. FL_unique_ptr<Node> right;
  63. std::string variable;
  64. scalar value;
  65. Node(Element* element, Node* left = fl::null, Node* right = fl::null);
  66. Node(const std::string& variable);
  67. Node(scalar value);
  68. Node(const Node& source);
  69. Node& operator=(const Node& rhs);
  70. virtual ~Node();
  71. FL_DEFAULT_MOVE(Node)
  72. virtual scalar evaluate(const std::map<std::string, scalar>*
  73. variables = fl::null) const;
  74. virtual Node* clone() const;
  75. virtual std::string toString() const;
  76. virtual std::string toPrefix(const Node* node = fl::null) const;
  77. virtual std::string toInfix(const Node* node = fl::null) const;
  78. virtual std::string toPostfix(const Node* node = fl::null) const;
  79. private:
  80. void copyFrom(const Node& source);
  81. };
  82. /******************************
  83. * Term
  84. ******************************/
  85. protected:
  86. FL_unique_ptr<Node> _root;
  87. std::string _formula;
  88. const Engine* _engine;
  89. public:
  90. mutable std::map<std::string, scalar> variables;
  91. Function(const std::string& name = "",
  92. const std::string& formula = "", const Engine* engine = fl::null);
  93. Function(const Function& other);
  94. Function& operator=(const Function& other);
  95. virtual ~Function() FL_IOVERRIDE;
  96. FL_DEFAULT_MOVE(Function)
  97. static Function* create(const std::string& name,
  98. const std::string& formula,
  99. const Engine* engine = fl::null); // throw (fl::Exception);
  100. virtual scalar membership(scalar x) const FL_IOVERRIDE;
  101. virtual scalar evaluate(const std::map<std::string, scalar>* variables) const;
  102. virtual std::string className() const FL_IOVERRIDE;
  103. virtual std::string parameters() const FL_IOVERRIDE;
  104. virtual void configure(const std::string& parameters) FL_IOVERRIDE;
  105. virtual void setFormula(const std::string& formula);
  106. virtual std::string getFormula() const;
  107. virtual void setEngine(const Engine* engine);
  108. virtual const Engine* getEngine() const;
  109. virtual Node* root() const;
  110. virtual bool isLoaded() const;
  111. virtual void unload();
  112. virtual void load(); // throw (fl::Exception);
  113. virtual void load(const std::string& formula); // throw (fl::Exception);
  114. virtual void load(const std::string& formula, const Engine* engine); // throw (fl::Exception);
  115. virtual Node* parse(const std::string& formula); // throw (fl::Exception);
  116. virtual std::string toPostfix(const std::string& formula) const; //throw (fl::Exception);
  117. virtual std::string space(const std::string& formula) const;
  118. virtual Function* clone() const FL_IOVERRIDE;
  119. static Term* constructor();
  120. static void main();
  121. };
  122. }
  123. #endif /* FL_FUNCTION_H */