summaryrefslogtreecommitdiff
path: root/fuzzylite/fl/rule/RuleBlock.h
blob: d4ccace185abc11449eece951f9fa2c6c4c7cc3d (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 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_RULEBLOCK_H
#define FL_RULEBLOCK_H

#include "fl/fuzzylite.h"

#include "fl/activation/Activation.h"
#include "fl/Complexity.h"

#include <string>
#include <vector>


namespace fl {

    class Engine;
    class Rule;
    class TNorm;
    class SNorm;

    /**
      The RuleBlock class contains a set of Rule%s and fuzzy logic
      operators required to control an Engine.

      @author Juan Rada-Vilela, Ph.D.
      @see Engine
      @see Rule
      @see Antecedent
      @see Consequent
      @since 4.0
     */
    class FL_API RuleBlock {
    private:
        bool _enabled;
        std::string _name;
        std::string _description;
        std::vector<Rule*> _rules;
        FL_unique_ptr<TNorm> _conjunction;
        FL_unique_ptr<SNorm> _disjunction;
        FL_unique_ptr<TNorm> _implication;
        FL_unique_ptr<Activation> _activation;

        void copyFrom(const RuleBlock& source);

    public:
        explicit RuleBlock(const std::string& name = "");
        RuleBlock(const RuleBlock& other);
        RuleBlock& operator=(const RuleBlock& other);
        virtual ~RuleBlock();
        FL_DEFAULT_MOVE(RuleBlock)

        /**
        Enables the rule block
        @param enabled whether the rule block is enabled
         */
        virtual void setEnabled(bool enabled);
        /**
          Indicates whether the rule block is enabled
          @return whether the rule block is enabled
         */
        virtual bool isEnabled() const;
        
        /**
          Activates the rule block
         */
        virtual void activate();

        /**
          Sets the name of the rule block
          @param name is the name of the rule block
         */
        virtual void setName(std::string name);
        /**
          Gets the name of the rule block
          @return the name of the rule block
         */
        virtual std::string getName() const;

        /**
          Gets the description of the rule block
          @return the description of the rule block
         */
        virtual std::string getDescription() const;

        /**
          Sets the description of the rule block
          @param description is the description of the rule block
         */
        virtual void setDescription(const std::string& description);
        /**
          Sets the conjunction operator
          @param conjunction is the conjunction operator
         */
        virtual void setConjunction(TNorm* conjunction);
        /**
          Gets the conjunction operator
          @return the conjunction operator
         */
        virtual TNorm* getConjunction() const;

        /**
          Sets the disjunction operator
          @param disjunction is the disjunction operator
         */
        virtual void setDisjunction(SNorm* disjunction);
        /**
          Gets the disjunction operator
          @return the disjunction operator
         */
        virtual SNorm* getDisjunction() const;

        /**
          Sets the implication operator
          @param implication is the implication operator
         */
        virtual void setImplication(TNorm* implication);
        /**
          Gets the implication operator
          @return the implication operator
         */
        virtual TNorm* getImplication() const;

        /**
          Sets the activation method
          @param activation is the activation method
         */
        virtual void setActivation(Activation* activation);
        /**
          Gets the activation method
          @return the activation method
         */
        virtual Activation* getActivation() const;

        /**
          Unloads all the rules in the rule block
         */
        virtual void unloadRules() const;
        /**
          Loads all the rules into the rule block
          @param engine is the engine where this rule block is registered
         */
        virtual void loadRules(const Engine* engine);

        /**
          Unloads all the rules in the rule block and then loads each rule again
          @param engine is the engine where this rule block is registered
         */
        virtual void reloadRules(const Engine* engine);

        /**
          Returns a string representation of the rule block in the FuzzyLite
          Language
          @return a string representation of the rule block in the  FuzzyLite
          Language
         */
        virtual std::string toString() const;

        /**
          Returns the estimated complexity of activating the rule block
          @return the estimated complexity of activating the rule block
         */
        virtual Complexity complexity() const;

        /**
          Adds the given rule to the rule block
          @param rule is the rule to add
         */
        virtual void addRule(Rule* rule);
        /**
          Inserts the rule at the specified index, shifting other rules to
          the right
          @param rule is the rule to insert
          @param index is the index at which to insert the rule
         */
        virtual void insertRule(Rule* rule, std::size_t index);
        /**
          Gets the rule at the specified index
          @param index is the index at which the rule is retrieved
          @return the rule at the specified index
         */
        virtual Rule* getRule(std::size_t index) const;
        /**
          Removes the rule at the specified index
          @param index is the index at which the rule will be removed,
          shifting other rules to the left
          @return the rule at the specified index
         */
        virtual Rule* removeRule(std::size_t index);
        /**
          Returns the number of rules added to the rule block
          @return the number of rules added to the rule block
         */
        virtual std::size_t numberOfRules() const;
        /**
          Sets the rules of the rule block
          @param rules is a vector of rules
         */
        virtual void setRules(const std::vector<Rule*>& rules);
        /**
          Returns an immutable vector of the rules added to the rule block
          @return an immutable vector of the rules added to the rule block
         */
        virtual const std::vector<Rule*>& rules() const;
        /**
          Returns a mutable vector of the rules added to the rule block
          @return a mutable vector of the rules added to the rule block
         */
        virtual std::vector<Rule*>& rules();

        /**
          Creates a clone of the rule block without the rules being loaded
          @return a clone of the rule block without the rules being loaded
         */
        virtual RuleBlock* clone() const;

    };
}
#endif /* RULEBLOCK_H */