summaryrefslogtreecommitdiff
path: root/fuzzylite/fl/rule/Antecedent.h
blob: a424d95328f81adb48b74a634f6500de1ed5cbcf (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 fuzzylite (R), a fuzzy logic control library in C++.
 Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved.
 Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com>

 This file is part of fuzzylite.

 fuzzylite is free software: you can redistribute it and/or modify it under
 the terms of the FuzzyLite License included with the software.

 You should have received a copy of the FuzzyLite License along with
 fuzzylite. If not, see <http://www.fuzzylite.com/license/>.

 fuzzylite is a registered trademark of FuzzyLite Limited.
 */

#ifndef FL_ANTECEDENT_H
#define FL_ANTECEDENT_H

#include "fl/fuzzylite.h"

#include "fl/Complexity.h"

#include <string>

namespace fl {
    class Engine;
    class Rule;
    class TNorm;
    class SNorm;
    class Expression;

    /**
      The Antecedent class is an expression tree that represents and evaluates
      the antecedent of a Rule. The structure of a rule is: `if (antecedent)
      then (consequent)`. The structure of the antecedent of a rule is:

     `if variable is [hedge]* term [(and|or) variable is [hedge]* term]*`

      where `*`-marked elements may appear zero or more times, elements in
      brackets are optional, and elements in parentheses are compulsory.

      @author Juan Rada-Vilela, Ph.D.
      @see Consequent
      @see Rule
      @since 4.0
     */
    class FL_API Antecedent {
    private:
        std::string _text;
        FL_unique_ptr<Expression> _expression;

    public:
        Antecedent();
        virtual ~Antecedent();

        /**
          Sets the antecedent in text
          @param text is the antecedent in text
         */
        virtual void setText(const std::string& text);
        /**
          Gets the antecedent in text
          @return the antecedent in text
         */
        virtual std::string getText() const;

        /**
          Gets the expression tree of the antecedent
          @return the expression tree of the antecedent
         */
        virtual Expression* getExpression() const;

        /**
          Sets the expression tree of the antecedent
          @param expression is the expression tree of the antecedent
         */
        virtual void setExpression(Expression* expression);

        /**
          Indicates whether the antecedent is loaded
          @return whether the antecedent is loaded
         */
        virtual bool isLoaded() const;

        /**
          Unloads the antecedent
         */
        virtual void unload();

        /**
          Loads the antecedent with the text obtained from
          Antecedent::getText() and uses the engine to identify and retrieve
          references to the input variables and output variables as required

          @param engine is the engine from which the rules are part of
         */
        virtual void load(const Engine* engine);
        /**
          Loads the antecedent with the given text and uses the engine to
          identify and retrieve references to the input variables and output
          variables as required

          @param antecedent is the antecedent of the rule in text
          @param engine is the engine from which the rules are part of
         */
        virtual void load(const std::string& antecedent, const Engine* engine);

        /**
          Computes the estimated complexity of calculating the activation degree
          @return the estimated complexity of calculating the activation degree
         */
        virtual Complexity complexity(const TNorm* conjunction, const SNorm* disjunction) const;
        /**
          Computes the estimated complexity of recursively calculating the
          activation degree from the given node
          @return the estimated complexity of recursively calculating the
          activation degree from the given node
         */
        virtual Complexity complexity(const TNorm* conjunction, const SNorm* disjunction,
                const Expression* node) const;


        /**
          Computes the activation degree of the antecedent on the expression
          tree from the given node

          @param conjunction is the conjunction operator from the RuleBlock
          @param disjunction is the disjunction operator from the RuleBlock
          @param node is a node in the expression tree of the antecedent
          @return the activation degree of the antecedent
         */
        virtual scalar activationDegree(const TNorm* conjunction, const SNorm* disjunction,
                const Expression* node) const;

        /**
          Computes the activation degree of the antecedent on the expression
          tree from the root node

          @param conjunction is the conjunction operator from the RuleBlock
          @param disjunction is the disjunction operator from the RuleBlock
          @return the activation degree of the antecedent on the expression tree
         */
        virtual scalar activationDegree(const TNorm* conjunction, const SNorm* disjunction) const;

        /**
          Returns a string representation of the expression tree in infix
          notation

          @return a string representation of the expression tree in infix
          notation
         */
        virtual std::string toString() const;

        /**
          Returns a string represention of the given expression tree utilizing
          prefix notation
          @param node is a node in the expression tree of the antecedent
          @return a string represention of the given expression tree utilizing
          prefix notation
         */
        virtual std::string toPrefix(const Expression* node = fl::null) const;
        /**
          Returns a string represention of the given expression tree utilizing
          infix notation
          @param node is a node in the expression tree of the antecedent
          @return a string represention of the given expression tree utilizing
          infix notation
         */
        virtual std::string toInfix(const Expression* node = fl::null) const;
        /**
          Returns a string represention of the given expression tree utilizing
          postfix notation
          @param node is a node in the expression tree of the antecedent
          @return a string represention of the given expression tree utilizing
          postfix notation
         */
        virtual std::string toPostfix(const Expression* node = fl::null) const;


    private:
        FL_DISABLE_COPY(Antecedent)
    };
}
#endif /* FL_ANTECEDENT_H */