summaryrefslogtreecommitdiff
path: root/fuzzylite/src/rule/Rule.cpp
blob: 0338cd9946549cef629d877d62dad3051d7a4c28 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 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/Rule.h"

#include "fl/Exception.h"
#include "fl/imex/FllExporter.h"
#include "fl/norm/Norm.h"
#include "fl/Operation.h"

namespace fl {

    Rule::Rule(const std::string& text, scalar weight)
    : _enabled(true), _text(text), _weight(weight), _activationDegree(0.0), _triggered(false),
    _antecedent(new Antecedent), _consequent(new Consequent) { }

    Rule::Rule(const Rule& other) : _enabled(other._enabled), _text(other._text),
    _weight(other._weight), _activationDegree(other._activationDegree), _triggered(false),
    _antecedent(new Antecedent), _consequent(new Consequent) { }

    Rule& Rule::operator=(const Rule& other) {
        if (this != &other) {
            _enabled = other._enabled;
            _text = other._text;
            _weight = other._weight;
            _activationDegree = other._activationDegree;
            _triggered = other._triggered;
            _antecedent.reset(new Antecedent);
            _consequent.reset(new Consequent);
        }
        return *this;
    }

    Rule::~Rule() {
        if (_antecedent.get()) _antecedent->unload();
        if (_consequent.get()) _consequent->unload();
    }

    void Rule::setText(const std::string& text) {
        this->_text = text;
    }

    std::string Rule::getText() const {
        return this->_text;
    }

    void Rule::setWeight(scalar weight) {
        this->_weight = weight;
    }

    scalar Rule::getWeight() const {
        return this->_weight;
    }

    void Rule::setAntecedent(Antecedent* antecedent) {
        this->_antecedent.reset(antecedent);
    }

    Antecedent* Rule::getAntecedent() const {
        return this->_antecedent.get();
    }

    void Rule::setConsequent(Consequent* consequent) {
        this->_consequent.reset(consequent);
    }

    Consequent* Rule::getConsequent() const {
        return this->_consequent.get();
    }

    void Rule::setEnabled(bool active) {
        this->_enabled = active;
    }

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

    void Rule::setActivationDegree(scalar activationDegree) {
        this->_activationDegree = activationDegree;
    }

    scalar Rule::getActivationDegree() const {
        return this->_activationDegree;
    }

    void Rule::deactivate() {
        _activationDegree = 0.0;
        _triggered = false;
    }

    scalar Rule::activateWith(const TNorm* conjunction, const SNorm* disjunction) {
        if (not isLoaded()) {
            throw Exception("[rule error] the following rule is not loaded: " + getText(), FL_AT);
        }
        _activationDegree = _weight * _antecedent->activationDegree(conjunction, disjunction);
        return _activationDegree;
    }

    void Rule::trigger(const TNorm* implication) {
        if (not isLoaded()) {
            throw Exception("[rule error] the following rule is not loaded: " + getText(), FL_AT);
        }
        if (_enabled and Op::isGt(_activationDegree, 0.0)) {
            FL_DBG("[firing with " << Op::str(_activationDegree) << "] " << toString());
            _consequent->modify(_activationDegree, implication);
            _triggered = true;
        }
    }

    bool Rule::isTriggered() const {
        return this->_triggered;
    }

    Complexity Rule::complexityOfActivation(const TNorm* conjunction, const SNorm* disjunction) const {
        Complexity result;
        result.comparison(1).arithmetic(1);
        if (isLoaded()) {
            result += _antecedent->complexity(conjunction, disjunction);
        }
        return result;
    }

    Complexity Rule::complexityOfFiring(const TNorm* implication) const {
        Complexity result;
        result.comparison(3);
        if (isLoaded()) {
            result += _consequent->complexity(implication);
        }
        return result;
    }

    Complexity Rule::complexity(const TNorm* conjunction, const SNorm* disjunction,
            const TNorm* implication) const {
        return complexityOfActivation(conjunction, disjunction)
                + complexityOfFiring(implication);
    }

    bool Rule::isLoaded() const {
        return _antecedent.get() and _consequent.get()
                and _antecedent->isLoaded() and _consequent->isLoaded();
    }

    void Rule::unload() {
        deactivate();
        if (getAntecedent()) getAntecedent()->unload();
        if (getConsequent()) getConsequent()->unload();
    }

    void Rule::load(const Engine* engine) {
        load(getText(), engine);
    }

    void Rule::load(const std::string& rule, const Engine* engine) {
        deactivate();
        setEnabled(true);
        setText(rule);
        std::istringstream tokenizer(rule.substr(0, rule.find_first_of('#')));
        std::string token;
        std::ostringstream ossAntecedent, ossConsequent;
        scalar weight = 1.0;

        enum FSM {
            S_NONE, S_IF, S_THEN, S_WITH, S_END
        };
        FSM state = S_NONE;
        try {
            while (tokenizer >> token) {

                switch (state) {
                    case S_NONE:
                        if (token == Rule::ifKeyword()) state = S_IF;
                        else {
                            std::ostringstream ex;
                            ex << "[syntax error] expected keyword <" << Rule::ifKeyword() <<
                                    ">, but found <" << token << "> in rule: " << rule;
                            throw Exception(ex.str(), FL_AT);
                        }
                        break;
                    case S_IF:
                        if (token == Rule::thenKeyword()) state = S_THEN;
                        else ossAntecedent << token << " ";
                        break;
                    case S_THEN:
                        if (token == Rule::withKeyword()) state = S_WITH;
                        else ossConsequent << token << " ";
                        break;
                    case S_WITH:
                        try {
                            weight = Op::toScalar(token);
                            state = S_END;
                        } catch (Exception& e) {
                            std::ostringstream ex;
                            ex << "[syntax error] expected a numeric value as the weight of the rule: "
                                    << rule;
                            e.append(ex.str(), FL_AT);
                            throw;
                        }
                        break;
                    case S_END:
                    {
                        std::ostringstream ex;
                        ex << "[syntax error] unexpected token <" << token << "> at the end of rule";
                        throw Exception(ex.str(), FL_AT);
                    }

                    default:
                        std::ostringstream ex;
                        ex << "[syntax error] unexpected state <" << state << ">";
                        throw Exception(ex.str(), FL_AT);
                }
            }
            if (state == S_NONE) {
                std::ostringstream ex;
                ex << "[syntax error] " << (rule.empty() ? "empty rule" : ("ignored rule: " + rule));
                throw Exception(ex.str(), FL_AT);
            } else if (state == S_IF) {
                std::ostringstream ex;
                ex << "[syntax error] keyword <" << Rule::thenKeyword() << "> not found in rule: " << rule;
                throw Exception(ex.str(), FL_AT);
            } else if (state == S_WITH) {
                std::ostringstream ex;
                ex << "[syntax error] expected a numeric value as the weight of the rule: " << rule;
                throw Exception(ex.str(), FL_AT);
            }

            getAntecedent()->load(ossAntecedent.str(), engine);
            getConsequent()->load(ossConsequent.str(), engine);
            setWeight(weight);

        } catch (...) {
            unload();
            throw;
        }
    }

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

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

    Rule* Rule::parse(const std::string& rule, const Engine* engine) {
        FL_unique_ptr<Rule> result(new Rule);
        result->load(rule, engine);
        return result.release();
    }

}