summaryrefslogtreecommitdiff
path: root/fuzzylite/fl/term/Function.h
blob: 99580b7c13827973d7741742d0fd00c7258440b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 Author: Juan Rada-Vilela, Ph.D.
 Copyright (C) 2010-2014 FuzzyLite Limited
 All rights reserved

 This file is part of fuzzylite.

 fuzzylite is free software: you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation, either version 3 of the License, or (at your option)
 any later version.

 fuzzylite is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with fuzzylite.  If not, see <http://www.gnu.org/licenses/>.

 fuzzylite™ is a trademark of FuzzyLite Limited.

 */

#ifndef FL_FUNCTION_H
#define FL_FUNCTION_H

#include "fl/term/Term.h"

#include <map>
#include <string>

namespace fl {

    class Engine;

    class FL_API Function : public Term {
        /****************************
         * Parsing Elements
         ****************************/

    public:
        typedef scalar(*Unary)(scalar);
        typedef scalar(*Binary)(scalar, scalar);

        struct FL_API Element {

            enum Type {
                OPERATOR, FUNCTION
            };
            std::string name;
            std::string description;
            Type type;
            Unary unary;
            Binary binary;
            int arity;
            int precedence; //Operator
            int associativity;
            Element(const std::string& name, const std::string& description, Type type);
            Element(const std::string& name, const std::string& description,
                    Type type, Unary unary, int precedence = 0, int associativity = -1);
            Element(const std::string& name, const std::string& description,
                    Type type, Binary binary, int precedence = 0, int associativity = -1);
            virtual ~Element();
            FL_DEFAULT_COPY_AND_MOVE(Element)

            virtual bool isOperator() const;
            virtual bool isFunction() const;

            virtual Element* clone() const;

            virtual std::string toString() const;

        };

        /**************************
         * Tree elements, wrap Elements into Nodes.
         **************************/

        struct FL_API Node {
            FL_unique_ptr<Element> element;
            FL_unique_ptr<Node> left;
            FL_unique_ptr<Node> right;
            std::string variable;
            scalar value;

            explicit Node(Element* element, Node* left = fl::null, Node* right = fl::null);
            explicit Node(const std::string& variable);
            explicit Node(scalar value);
            Node(const Node& source);
            Node& operator=(const Node& rhs);
            virtual ~Node();
            FL_DEFAULT_MOVE(Node)

            virtual scalar evaluate(const std::map<std::string, scalar>*
                    variables = fl::null) const;

            virtual Node* clone() const;

            virtual std::string toString() const;
            virtual std::string toPrefix(const Node* node = fl::null) const;
            virtual std::string toInfix(const Node* node = fl::null) const;
            virtual std::string toPostfix(const Node* node = fl::null) const;
        private:
            void copyFrom(const Node& source);
        };




        /******************************
         * Term
         ******************************/

    protected:
        FL_unique_ptr<Node> _root;
        std::string _formula;
        const Engine* _engine;
    public:
        mutable std::map<std::string, scalar> variables;
        explicit Function(const std::string& name = "",
                const std::string& formula = "", const Engine* engine = fl::null);
        Function(const Function& other);
        Function& operator=(const Function& other);
        virtual ~Function() FL_IOVERRIDE;
        FL_DEFAULT_MOVE(Function)

        static Function* create(const std::string& name,
                const std::string& formula,
                const Engine* engine = fl::null); // throw (fl::Exception);

        virtual scalar membership(scalar x) const FL_IOVERRIDE;

        virtual scalar evaluate(const std::map<std::string, scalar>* variables) const;

        virtual std::string className() const FL_IOVERRIDE;
        virtual std::string parameters() const FL_IOVERRIDE;
        virtual void configure(const std::string& parameters) FL_IOVERRIDE;

        virtual void setFormula(const std::string& formula);
        virtual std::string getFormula() const;

        virtual void setEngine(const Engine* engine);
        virtual const Engine* getEngine() const;

        virtual Node* root() const;

        virtual bool isLoaded() const;
        virtual void unload();
        virtual void load(); // throw (fl::Exception);
        virtual void load(const std::string& formula); // throw (fl::Exception);
        virtual void load(const std::string& formula, const Engine* engine); // throw (fl::Exception);

        virtual Node* parse(const std::string& formula); // throw (fl::Exception);

        virtual std::string toPostfix(const std::string& formula) const; //throw (fl::Exception);

        virtual std::string space(const std::string& formula) const;

        virtual Function* clone() const FL_IOVERRIDE;

        static Term* constructor();

        static void main();

    };

}

#endif  /* FL_FUNCTION_H */