summaryrefslogtreecommitdiff
path: root/fuzzylite/src/imex/FllImporter.cpp
blob: 2443d1028a8c510bd20a4850a86da4e1b25daa9c (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 Author: Juan Rada-Vilela, Ph.D.
 Copyright (C) 2010-2014 FuzzyLite Limited
 All rights reserved

 This file is part of fuzzylite.

 fuzzylite is free software: you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation, either version 3 of the License, or (at your option)
 any later version.

 fuzzylite is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with fuzzylite.  If not, see <http://www.gnu.org/licenses/>.

 fuzzylite™ is a trademark of FuzzyLite Limited.

 */

#include "fl/imex/FllImporter.h"

#include "fl/Headers.h"

#include <queue>

namespace fl {

    FllImporter::FllImporter(const std::string& separator) : Importer(),
    _separator(separator) {
    }

    FllImporter::~FllImporter() {

    }

    std::string FllImporter::name() const {
        return "FllImporter";
    }

    void FllImporter::setSeparator(const std::string& separator) {
        this->_separator = separator;
    }

    std::string FllImporter::getSeparator() const {
        return this->_separator;
    }

    Engine* FllImporter::fromString(const std::string& fll) const {
        FL_unique_ptr<Engine> engine(new Engine);

        std::string tag;
        std::ostringstream block;
        std::istringstream fclReader(fll);
        std::string line;
        std::queue<std::string> lineQueue;

        bool processPending = false;
        int lineNumber = 0;
        while (not lineQueue.empty() or std::getline(fclReader, line)) {
            if (not lineQueue.empty()) {
                line = lineQueue.front();
                lineQueue.pop();
            } else {
                line = clean(line);
                if (line.empty()) continue;
                std::vector<std::string> split = Op::split(line, _separator);
                line = clean(split.front());
                for (std::size_t i = 1; i < split.size(); ++i) {
                    lineQueue.push(clean(split.at(i)));
                }
                ++lineNumber;
            }
            if (line.empty()) continue;
            std::size_t colon = line.find_first_of(':');
            if (colon == std::string::npos) {
                throw fl::Exception("[import error] expected a colon at line " +
                        Op::str(lineNumber) + ": " + line, FL_AT);
            }
            std::string key = Op::trim(line.substr(0, colon));
            std::string value = Op::trim(line.substr(colon + 1));
            if ("Engine" == key) {
                engine->setName(value);
                continue;
            } else {
                processPending = (key == "InputVariable"
                        or key == "OutputVariable"
                        or key == "RuleBlock");
            }
            if (processPending) {
                process(tag, block.str(), engine.get());
                block.str(""); //clear buffer
                block.clear(); //clear error flags
                processPending = false;
                tag = key;
            }
            block << key << ":" << value << "\n";
        }
        process(tag, block.str(), engine.get());
        return engine.release();
    }

    void FllImporter::process(const std::string& tag, const std::string& block, Engine* engine) const {
        if (tag.empty()) return;
        if ("InputVariable" == tag) {
            processInputVariable(block, engine);
        } else if ("OutputVariable" == tag) {
            processOutputVariable(block, engine);
        } else if ("RuleBlock" == tag) {
            processRuleBlock(block, engine);
        } else {
            throw fl::Exception("[import error] block tag <" + tag + "> not recognized", FL_AT);
        }
    }

    void FllImporter::processInputVariable(const std::string& block, Engine* engine) const {
        std::istringstream reader(block);
        std::string line;
        InputVariable* inputVariable = new InputVariable;
        engine->addInputVariable(inputVariable);
        while (std::getline(reader, line)) {
            std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':');
            if ("InputVariable" == keyValue.first) {
                inputVariable->setName(Op::validName(keyValue.second));
            } else if ("enabled" == keyValue.first) {
                inputVariable->setEnabled(parseBoolean(keyValue.second));
            } else if ("range" == keyValue.first) {
                std::pair<scalar, scalar> range = parseRange(keyValue.second);
                inputVariable->setRange(range.first, range.second);
            } else if ("term" == keyValue.first) {
                inputVariable->addTerm(parseTerm(keyValue.second, engine));
            } else {
                throw fl::Exception("[import error] key <" + keyValue.first + "> not "
                        "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT);
            }
        }
    }

    void FllImporter::processOutputVariable(const std::string& block, Engine* engine) const {
        std::istringstream reader(block);
        std::string line;
        OutputVariable* outputVariable = new OutputVariable;
        engine->addOutputVariable(outputVariable);
        while (std::getline(reader, line)) {
            std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':');
            if ("OutputVariable" == keyValue.first) {
                outputVariable->setName(Op::validName(keyValue.second));
            } else if ("enabled" == keyValue.first) {
                outputVariable->setEnabled(parseBoolean(keyValue.second));
            } else if ("range" == keyValue.first) {
                std::pair<scalar, scalar> range = parseRange(keyValue.second);
                outputVariable->setRange(range.first, range.second);
            } else if ("default" == keyValue.first) {
                outputVariable->setDefaultValue(Op::toScalar(keyValue.second));
            } else if ("lock-previous" == keyValue.first or "lock-valid" == keyValue.first) {
                outputVariable->setLockPreviousOutputValue(parseBoolean(keyValue.second));
            } else if ("lock-range" == keyValue.first) {
                outputVariable->setLockOutputValueInRange(parseBoolean(keyValue.second));
            } else if ("defuzzifier" == keyValue.first) {
                outputVariable->setDefuzzifier(parseDefuzzifier(keyValue.second));
            } else if ("accumulation" == keyValue.first) {
                outputVariable->fuzzyOutput()->setAccumulation(parseSNorm(keyValue.second));
            } else if ("term" == keyValue.first) {
                outputVariable->addTerm(parseTerm(keyValue.second, engine));
            } else {
                throw fl::Exception("[import error] key <" + keyValue.first + "> not "
                        "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT);
            }
        }
    }

    void FllImporter::processRuleBlock(const std::string& block, Engine* engine) const {
        std::istringstream reader(block);
        std::string line;
        RuleBlock* ruleBlock = new RuleBlock;
        engine->addRuleBlock(ruleBlock);
        while (std::getline(reader, line)) {
            std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':');
            if ("RuleBlock" == keyValue.first) {
                ruleBlock->setName(keyValue.second);
            } else if ("enabled" == keyValue.first) {
                ruleBlock->setEnabled(parseBoolean(keyValue.second));
            } else if ("conjunction" == keyValue.first) {
                ruleBlock->setConjunction(parseTNorm(keyValue.second));
            } else if ("disjunction" == keyValue.first) {
                ruleBlock->setDisjunction(parseSNorm(keyValue.second));
            } else if ("activation" == keyValue.first) {
                ruleBlock->setActivation(parseTNorm(keyValue.second));
            } else if ("rule" == keyValue.first) {
                Rule* rule = new Rule;
                rule->setText(keyValue.second);
                try {
                    rule->load(engine);
                } catch (std::exception& ex) {
                    FL_LOG(ex.what());
                }
                ruleBlock->addRule(rule);
            } else {
                throw fl::Exception("[import error] key <" + keyValue.first + "> not "
                        "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT);
            }
        }
    }

    Term* FllImporter::parseTerm(const std::string& text, Engine* engine) const {
        std::vector<std::string> tokens = Op::split(text, " ");

        //MEDIUM Triangle 0.500 1.000 1.500

        if (tokens.size() < 2) {
            throw fl::Exception("[syntax error] expected a term in format <name class parameters>, "
                    "but found <" + text + ">", FL_AT);
        }
        FL_unique_ptr<Term> term;
        term.reset(FactoryManager::instance()->term()->constructObject(tokens.at(1)));
        Term::updateReference(term.get(), engine);
        term->setName(Op::validName(tokens.at(0)));
        std::ostringstream parameters;
        for (std::size_t i = 2; i < tokens.size(); ++i) {
            parameters << tokens.at(i);
            if (i + 1 < tokens.size()) parameters << " ";
        }
        term->configure(parameters.str());
        return term.release();
    }

    TNorm* FllImporter::parseTNorm(const std::string& name) const {
        if (name == "none") return FactoryManager::instance()->tnorm()->constructObject("");
        return FactoryManager::instance()->tnorm()->constructObject(name);
    }

    SNorm* FllImporter::parseSNorm(const std::string& name) const {
        if (name == "none") return FactoryManager::instance()->snorm()->constructObject("");
        return FactoryManager::instance()->snorm()->constructObject(name);
    }

    Defuzzifier* FllImporter::parseDefuzzifier(const std::string& text) const {
        std::vector<std::string> parameters = Op::split(text, " ");
        std::string name = parameters.at(0);
        if (name == "none") return FactoryManager::instance()->defuzzifier()->constructObject("");
        Defuzzifier* defuzzifier = FactoryManager::instance()->defuzzifier()->constructObject(name);
        if (parameters.size() > 1) {
            std::string parameter(parameters.at(1));
            if (IntegralDefuzzifier * integralDefuzzifier = dynamic_cast<IntegralDefuzzifier*> (defuzzifier)) {
                integralDefuzzifier->setResolution((int) Op::toScalar(parameter));
            } else if (WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<WeightedDefuzzifier*> (defuzzifier)) {
                WeightedDefuzzifier::Type type = WeightedDefuzzifier::Automatic;
                if (parameter == "Automatic") type = WeightedDefuzzifier::Automatic;
                else if (parameter == "TakagiSugeno") type = WeightedDefuzzifier::TakagiSugeno;
                else if (parameter == "Tsukamoto") type = WeightedDefuzzifier::Tsukamoto;
                else throw fl::Exception("[syntax error] unknown parameter of WeightedDefuzzifier <" + parameter + ">", FL_AT);
                weightedDefuzzifier->setType(type);
            }
        }
        return defuzzifier;
    }

    std::pair<scalar, scalar> FllImporter::parseRange(const std::string& text) const {
        std::pair<std::string, std::string> range = parseKeyValue(text, ' ');
        return std::pair<scalar, scalar>(Op::toScalar(range.first), Op::toScalar(range.second));
    }

    bool FllImporter::parseBoolean(const std::string& boolean) const {
        if ("true" == boolean) return true;
        if ("false" == boolean) return false;
        throw fl::Exception("[syntax error] expected boolean <true|false>, "
                "but found <" + boolean + ">", FL_AT);
    }

    std::pair<std::string, std::string> FllImporter::parseKeyValue(const std::string& text,
            char separator) const {
        std::size_t half = text.find_first_of(separator);
        if (half == std::string::npos) {
            std::ostringstream ex;
            ex << "[syntax error] expected pair in the form "
                    "<key" << separator << "value>, but found <" << text << ">";
            throw fl::Exception(ex.str(), FL_AT);
        }
        std::pair<std::string, std::string> result;
        result.first = text.substr(0, half);
        result.second = text.substr(half + 1);
        return result;
    }

    std::string FllImporter::clean(const std::string& line) const {
        if (line.empty()) return line;
        if (line.size() == 1) return isspace(line.at(0)) ? "" : line;
        int start = 0, end = line.size() - 1;
        while (start <= end and isspace(line.at(start))) {
            ++start;
        }
        int sharp = start;
        while (sharp <= end) {
            if (line.at(sharp) == '#') {
                end = sharp - 1;
                break;
            }
            ++sharp;
        }
        while (end >= start and (line.at(end) == '#' or isspace(line.at(end)))) {
            --end;
        }

        int length = end - start + 1;
        return line.substr(start, length);
    }

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


}