• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fuzzylite / fuzzylite / 13598539256

28 Feb 2025 11:56PM UTC coverage: 56.022% (+3.6%) from 52.408%
13598539256

push

github

web-flow
Test: activation methods (#186)

* Getting started

* Add `Activation::toString()` to export to FLL

* Minor fix: Better configure Highest and Lowest

* Proportional: Set activation degree to zero if it is nan

* Rule: use methods to set/get properties inside object.

* RuleBlock: enhanced constructor and fluid interface

* Mock: mocker classes

* Tests: Activation methods

* Fix formatting

* Minor changes

* Sorted members of RuleBlock

42 of 54 new or added lines in 6 files covered. (77.78%)

1 existing line in 1 file now uncovered.

4907 of 8759 relevant lines covered (56.02%)

57516.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.77
/src/rule/RuleBlock.cpp
1
/*
2
fuzzylite (R), a fuzzy logic control library in C++.
3

4
Copyright (C) 2010-2024 FuzzyLite Limited. All rights reserved.
5
Author: Juan Rada-Vilela, PhD <jcrada@fuzzylite.com>.
6

7
This file is part of fuzzylite.
8

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

12
You should have received a copy of the FuzzyLite License along with
13
fuzzylite. If not, see <https://github.com/fuzzylite/fuzzylite/>.
14

15
fuzzylite is a registered trademark of FuzzyLite Limited.
16
*/
17

18
#include "fuzzylite/rule/RuleBlock.h"
19

20
#include "fuzzylite/Operation.h"
21
#include "fuzzylite/activation/General.h"
22
#include "fuzzylite/imex/FllExporter.h"
23
#include "fuzzylite/norm/SNorm.h"
24
#include "fuzzylite/norm/TNorm.h"
25
#include "fuzzylite/rule/Rule.h"
26

27
namespace fuzzylite {
28

29
    RuleBlock::RuleBlock(
91✔
30
        const std::string& name,
31
        const std::vector<Rule*>& rules,
32
        Activation* activation,
33
        TNorm* conjunction,
34
        SNorm* disjunction,
35
        TNorm* implication
36
    ) :
91✔
37
        _enabled(true),
91✔
38
        _name(name),
91✔
39
        _rules(rules),
91✔
40
        _activation(activation),
91✔
41
        _conjunction(conjunction),
91✔
42
        _disjunction(disjunction),
91✔
43
        _implication(implication) {}
182✔
44

45
    RuleBlock::RuleBlock(const RuleBlock& other) :
36✔
46
        _enabled(true),
36✔
47
        _name(other._name),
36✔
48
        _description(other._description) {
36✔
49
        copyFrom(other);
36✔
50
    }
36✔
51

52
    RuleBlock& RuleBlock::operator=(const RuleBlock& other) {
×
53
        if (this != &other) {
×
54
            for (std::size_t i = 0; i < _rules.size(); ++i)
×
55
                delete _rules.at(i);
×
56
            _rules.clear();
×
57
            _conjunction.reset(fl::null);
×
58
            _disjunction.reset(fl::null);
×
59
            _implication.reset(fl::null);
×
60
            _activation.reset(fl::null);
×
61

62
            copyFrom(other);
×
63
        }
64
        return *this;
×
65
    }
66

67
    void RuleBlock::copyFrom(const RuleBlock& source) {
36✔
68
        _enabled = source._enabled;
36✔
69
        _name = source._name;
36✔
70
        _description = source._description;
36✔
71
        if (source._conjunction.get())
36✔
72
            _conjunction.reset(source._conjunction->clone());
×
73
        if (source._disjunction.get())
36✔
74
            _disjunction.reset(source._disjunction->clone());
×
75
        if (source._implication.get())
36✔
76
            _implication.reset(source._implication->clone());
×
77
        if (source._activation.get())
36✔
78
            _activation.reset(source._activation->clone());
×
79
        for (std::size_t i = 0; i < source._rules.size(); ++i)
144✔
80
            _rules.push_back(source._rules.at(i)->clone());
108✔
81
    }
36✔
82

83
    RuleBlock::~RuleBlock() {
320✔
84
        for (std::size_t i = 0; i < _rules.size(); ++i)
1,292✔
85
            delete _rules.at(i);
1,038✔
86
        _rules.clear();
254✔
87
    }
320✔
88

89
    void RuleBlock::activate() {
31,238✔
90
        if (not _activation.get())
31,238✔
91
            _activation.reset(new General);
×
92
        _activation->activate(this);
31,238✔
93
    }
31,238✔
94

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

100
    void RuleBlock::loadRules(const Engine* engine) {
×
101
        std::ostringstream exceptions;
×
102
        bool throwException = false;
×
103
        for (std::size_t i = 0; i < _rules.size(); ++i) {
×
104
            Rule* rule = _rules.at(i);
×
105
            if (rule->isLoaded())
×
106
                rule->unload();
×
107
            try {
108
                rule->load(engine);
×
109
            } catch (std::exception& ex) {
×
110
                throwException = true;
×
111
                exceptions << ex.what() << "\n";
×
112
            }
×
113
        }
114
        if (throwException) {
×
115
            Exception exception(
116
                "[ruleblock error] the following "
117
                "rules could not be loaded:\n"
118
                    + exceptions.str(),
×
119
                FL_AT
×
120
            );
×
121
            throw exception;
×
122
        }
×
123
    }
×
124

125
    void RuleBlock::reloadRules(const Engine* engine) {
×
126
        unloadRules();
×
127
        loadRules(engine);
×
128
    }
×
129

130
    void RuleBlock::setName(std::string name) {
33✔
131
        this->_name = name;
33✔
132
    }
33✔
133

134
    std::string RuleBlock::getName() const {
324✔
135
        return this->_name;
324✔
136
    }
137

138
    void RuleBlock::setDescription(const std::string& description) {
1✔
139
        this->_description = description;
1✔
140
    }
1✔
141

142
    std::string RuleBlock::getDescription() const {
162✔
143
        return this->_description;
162✔
144
    }
145

146
    void RuleBlock::setConjunction(TNorm* tnorm) {
33✔
147
        this->_conjunction.reset(tnorm);
33✔
148
    }
33✔
149

150
    TNorm* RuleBlock::getConjunction() const {
31,400✔
151
        return this->_conjunction.get();
31,400✔
152
    }
153

154
    void RuleBlock::setDisjunction(SNorm* snorm) {
33✔
155
        this->_disjunction.reset(snorm);
33✔
156
    }
33✔
157

158
    SNorm* RuleBlock::getDisjunction() const {
31,400✔
159
        return this->_disjunction.get();
31,400✔
160
    }
161

162
    void RuleBlock::setImplication(TNorm* implication) {
33✔
163
        this->_implication.reset(implication);
33✔
164
    }
33✔
165

166
    TNorm* RuleBlock::getImplication() const {
31,400✔
167
        return this->_implication.get();
31,400✔
168
    }
169

170
    void RuleBlock::setActivation(Activation* activation) {
109✔
171
        this->_activation.reset(activation);
109✔
172
    }
109✔
173

174
    Activation* RuleBlock::getActivation() const {
432✔
175
        return this->_activation.get();
432✔
176
    }
177

178
    void RuleBlock::setEnabled(bool enabled) {
33✔
179
        this->_enabled = enabled;
33✔
180
    }
33✔
181

182
    bool RuleBlock::isEnabled() const {
31,346✔
183
        return this->_enabled;
31,346✔
184
    }
185

186
    std::string RuleBlock::toString() const {
162✔
187
        return FllExporter().toString(this);
648✔
188
    }
189

190
    /**
191
     * Operations for std::vector _rules
192
     */
193
    void RuleBlock::addRule(Rule* rule) {
303✔
194
        _rules.push_back(rule);
303✔
195
    }
303✔
196

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

201
    Rule* RuleBlock::getRule(std::size_t index) const {
285,353✔
202
        return _rules.at(index);
285,353✔
203
    }
204

205
    Rule* RuleBlock::removeRule(std::size_t index) {
×
206
        Rule* result = _rules.at(index);
×
207
        _rules.erase(_rules.begin() + index);
×
208
        return result;
×
209
    }
210

211
    std::size_t RuleBlock::numberOfRules() const {
31,958✔
212
        return _rules.size();
31,958✔
213
    }
214

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

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

223
    std::vector<Rule*>& RuleBlock::rules() {
252✔
224
        return this->_rules;
252✔
225
    }
226

227
    RuleBlock* RuleBlock::clone() const {
×
228
        return new RuleBlock(*this);
×
229
    }
230

231
    RuleBlock& RuleBlock::rules(const std::vector<Rule*>& rules) {
36✔
232
        _rules.insert(_rules.end(), rules.begin(), rules.end());
36✔
233
        return *this;
36✔
234
    }
235

NEW
236
    RuleBlock& RuleBlock::rule(Rule* rule) {
×
NEW
237
        addRule(rule);
×
NEW
238
        return *this;
×
239
    }
240

NEW
241
    RuleBlock& RuleBlock::conjunction(TNorm* conjunction) {
×
NEW
242
        setConjunction(conjunction);
×
NEW
243
        return *this;
×
244
    }
245

NEW
246
    RuleBlock& RuleBlock::disjunction(SNorm* disjunction) {
×
NEW
247
        setDisjunction(disjunction);
×
NEW
248
        return *this;
×
249
    }
250

NEW
251
    RuleBlock& RuleBlock::implication(TNorm* implication) {
×
NEW
252
        setImplication(implication);
×
NEW
253
        return *this;
×
254
    }
255

256
    RuleBlock& RuleBlock::activation(Activation* activation) {
76✔
257
        setActivation(activation);
76✔
258
        return *this;
76✔
259
    }
260

261
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc