summaryrefslogtreecommitdiff
path: root/fuzzylite/src/rule/RuleBlock.cpp
blob: d4e2a82f18f55547ff8a02fb40c8af53e5582362 (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
/*
 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.
 */

#include "fl/rule/RuleBlock.h"

#include "fl/activation/General.h"
#include "fl/imex/FllExporter.h"
#include "fl/norm/TNorm.h"
#include "fl/norm/SNorm.h"
#include "fl/rule/Rule.h"
#include "fl/Operation.h"

namespace fl {

    RuleBlock::RuleBlock(const std::string& name)
    : _enabled(true), _name(name), _description("") { }

    RuleBlock::RuleBlock(const RuleBlock& other) : _enabled(true), _name(other._name),
    _description(other._description) {
        copyFrom(other);
    }

    RuleBlock& RuleBlock::operator=(const RuleBlock& other) {
        if (this != &other) {
            for (std::size_t i = 0; i < _rules.size(); ++i) {
                delete _rules.at(i);
            }
            _rules.clear();
            _conjunction.reset(fl::null);
            _disjunction.reset(fl::null);
            _implication.reset(fl::null);
            _activation.reset(fl::null);

            copyFrom(other);
        }
        return *this;
    }

    void RuleBlock::copyFrom(const RuleBlock& source) {
        _enabled = source._enabled;
        _name = source._name;
        _description = source._description;
        if (source._conjunction.get()) _conjunction.reset(source._conjunction->clone());
        if (source._disjunction.get()) _disjunction.reset(source._disjunction->clone());
        if (source._implication.get()) _implication.reset(source._implication->clone());
        if (source._activation.get()) _activation.reset(source._activation->clone());
        for (std::size_t i = 0; i < source._rules.size(); ++i) {
            _rules.push_back(source._rules.at(i)->clone());
        }
    }

    RuleBlock::~RuleBlock() {
        for (std::size_t i = 0; i < _rules.size(); ++i) {
            delete _rules.at(i);
        }
        _rules.clear();
    }

    Complexity RuleBlock::complexity() const {
        Complexity result;
        result.comparison(1);
        if (_activation.get()) {
            result += _activation->complexity(this);
        } else {
            for (std::size_t i = 0; i < _rules.size(); ++i) {
                result += _rules.at(i)->complexity(
                        _conjunction.get(), _disjunction.get(), _implication.get());
            }
        }
        return result;
    }

    void RuleBlock::activate() {
        if (not _activation.get()) {
            _activation.reset(new General);
        }
        _activation->activate(this);
    }

    void RuleBlock::unloadRules() const {
        for (std::size_t i = 0; i < _rules.size(); ++i) {
            _rules.at(i)->unload();
        }
    }

    void RuleBlock::loadRules(const Engine* engine) {
        std::ostringstream exceptions;
        bool throwException = false;
        for (std::size_t i = 0; i < _rules.size(); ++i) {
            Rule* rule = _rules.at(i);
            if (rule->isLoaded()) {
                rule->unload();
            }
            try {
                rule->load(engine);
            } catch (std::exception& ex) {
                throwException = true;
                exceptions << ex.what() << "\n";
            }
        }
        if (throwException) {
            Exception exception("[ruleblock error] the following "
                    "rules could not be loaded:\n" + exceptions.str(), FL_AT);
            throw exception;
        }
    }

    void RuleBlock::reloadRules(const Engine* engine) {
        unloadRules();
        loadRules(engine);
    }

    void RuleBlock::setName(std::string name) {
        this->_name = name;
    }

    std::string RuleBlock::getName() const {
        return this->_name;
    }

    void RuleBlock::setDescription(const std::string& description) {
        this->_description = description;
    }

    std::string RuleBlock::getDescription() const {
        return this->_description;
    }

    void RuleBlock::setConjunction(TNorm* tnorm) {
        this->_conjunction.reset(tnorm);
    }

    TNorm* RuleBlock::getConjunction() const {
        return this->_conjunction.get();
    }

    void RuleBlock::setDisjunction(SNorm* snorm) {
        this->_disjunction.reset(snorm);
    }

    SNorm* RuleBlock::getDisjunction() const {
        return this->_disjunction.get();
    }

    void RuleBlock::setImplication(TNorm* implication) {
        this->_implication.reset(implication);
    }

    TNorm* RuleBlock::getImplication() const {
        return this->_implication.get();
    }

    void RuleBlock::setActivation(Activation* activation) {
        this->_activation.reset(activation);
    }

    Activation* RuleBlock::getActivation() const {
        return this->_activation.get();
    }

    void RuleBlock::setEnabled(bool enabled) {
        this->_enabled = enabled;
    }

    bool RuleBlock::isEnabled() const {
        return this->_enabled;
    }

    std::string RuleBlock::toString() const {
        return FllExporter().toString(this);
    }

    /**
     * Operations for std::vector _rules
     */
    void RuleBlock::addRule(Rule* rule) {
        _rules.push_back(rule);
    }

    void RuleBlock::insertRule(Rule* rule, std::size_t index) {
        _rules.insert(_rules.begin() + index, rule);
    }

    Rule* RuleBlock::getRule(std::size_t index) const {
        return _rules.at(index);
    }

    Rule* RuleBlock::removeRule(std::size_t index) {
        Rule* result = _rules.at(index);
        _rules.erase(_rules.begin() + index);
        return result;
    }

    std::size_t RuleBlock::numberOfRules() const {
        return _rules.size();
    }

    const std::vector<Rule*>& RuleBlock::rules() const {
        return this->_rules;
    }

    void RuleBlock::setRules(const std::vector<Rule*>& rules) {
        this->_rules = rules;
    }

    std::vector<Rule*>& RuleBlock::rules() {
        return this->_rules;
    }

    RuleBlock* RuleBlock::clone() const {
        return new RuleBlock(*this);
    }

}