summaryrefslogtreecommitdiff
path: root/fuzzylite/src/imex/FllExporter.cpp
blob: 6a8615253b355427c8708fdfcb0f3963196c11a3 (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
/*
 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/imex/FllExporter.h"

#include "fl/Headers.h"

namespace fl {

    FllExporter::FllExporter(const std::string& indent, const std::string& separator)
    : Exporter(), _indent(indent), _separator(separator) { }

    FllExporter::~FllExporter() { }

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

    void FllExporter::setIndent(const std::string& indent) {
        this->_indent = indent;
    }

    std::string FllExporter::getIndent() const {
        return this->_indent;
    }

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

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

    std::string FllExporter::toString(const Engine* engine) const {
        std::vector<std::string> result;
        result.push_back("Engine: " + engine->getName());
        if (not engine->getDescription().empty())
            result.push_back("description: " + engine->getDescription());
        for (std::size_t i = 0 ; i < engine->numberOfInputVariables(); ++i){
            result.push_back(toString(engine->getInputVariable(i)));
        }
        for (std::size_t i = 0 ; i < engine->numberOfOutputVariables(); ++i){
            result.push_back(toString(engine->getOutputVariable(i)));
        }
        for (std::size_t i = 0 ; i < engine->numberOfRuleBlocks(); ++i){
            result.push_back(toString(engine->getRuleBlock(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const std::vector<Variable*>& variables) const {
        std::vector<std::string> result;
        for (std::size_t i = 0; i < variables.size(); ++i) {
            result.push_back(toString(variables.at(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const std::vector<InputVariable*>& variables) const {
        std::vector<std::string> result;
        for (std::size_t i = 0; i < variables.size(); ++i) {
            result.push_back(toString(variables.at(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const std::vector<OutputVariable*>& variables) const {
        std::vector<std::string> result;
        for (std::size_t i = 0; i < variables.size(); ++i) {
            result.push_back(toString(variables.at(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const std::vector<RuleBlock*>& ruleBlocks) const {
        std::vector<std::string> result;
        for (std::size_t i = 0; i < ruleBlocks.size(); ++i) {
            result.push_back(toString(ruleBlocks.at(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const Variable* variable) const {
        std::vector<std::string> result;
        result.push_back("Variable: " + Op::validName(variable->getName()));
        if (not variable->getDescription().empty()) {
            result.push_back(_indent + "description: " + variable->getDescription());
        }
        result.push_back(_indent + "enabled: " + (variable->isEnabled() ? "true" : "false"));
        result.push_back(_indent + "range: " + Op::join(2, " ",
                variable->getMinimum(), variable->getMaximum()));
        result.push_back(_indent + "lock-range: " +
                (variable->isLockValueInRange() ? "true" : "false"));
        for (std::size_t i = 0; i < variable->numberOfTerms(); ++i) {
            result.push_back(_indent + toString(variable->getTerm(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const InputVariable* inputVariable) const {
        std::vector<std::string> result;
        result.push_back("InputVariable: " + Op::validName(inputVariable->getName()));
        if (not inputVariable->getDescription().empty()) {
            result.push_back(_indent + "description: " + inputVariable->getDescription());
        }
        result.push_back(_indent + "enabled: " + (inputVariable->isEnabled() ? "true" : "false"));
        result.push_back(_indent + "range: " + Op::join(2, " ",
                inputVariable->getMinimum(), inputVariable->getMaximum()));
        result.push_back(_indent + "lock-range: " +
                (inputVariable->isLockValueInRange() ? "true" : "false"));
        for (std::size_t i = 0; i < inputVariable->numberOfTerms(); ++i) {
            result.push_back(_indent + toString(inputVariable->getTerm(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const OutputVariable* outputVariable) const {
        std::vector<std::string> result;
        result.push_back("OutputVariable: " + Op::validName(outputVariable->getName()));
        if (not outputVariable->getDescription().empty()) {
            result.push_back(_indent + "description: " + outputVariable->getDescription());
        }
        result.push_back(_indent + "enabled: " + (outputVariable->isEnabled() ? "true" : "false"));
        result.push_back(_indent + "range: " + Op::join(2, " ",
                outputVariable->getMinimum(), outputVariable->getMaximum()));
        result.push_back(_indent + "lock-range: " +
                (outputVariable->isLockValueInRange() ? "true" : "false"));
        result.push_back(_indent + "aggregation: " +
                toString(outputVariable->fuzzyOutput()->getAggregation()));
        result.push_back(_indent + "defuzzifier: " +
                toString(outputVariable->getDefuzzifier()));
        result.push_back(_indent + "default: " + Op::str(outputVariable->getDefaultValue()));
        result.push_back(_indent + "lock-previous: " +
                (outputVariable->isLockPreviousValue() ? "true" : "false"));
        for (std::size_t i = 0; i < outputVariable->numberOfTerms(); ++i) {
            result.push_back(_indent + toString(outputVariable->getTerm(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const RuleBlock* ruleBlock) const {
        std::vector<std::string> result;
        result.push_back("RuleBlock: " + ruleBlock->getName());
        if (not ruleBlock->getDescription().empty()) {
            result.push_back(_indent + "description: " + ruleBlock->getDescription());
        }
        result.push_back(_indent + "enabled: " +
                (ruleBlock->isEnabled() ? "true" : "false"));
        result.push_back(_indent + "conjunction: " + toString(ruleBlock->getConjunction()));
        result.push_back(_indent + "disjunction: " + toString(ruleBlock->getDisjunction()));
        result.push_back(_indent + "implication: " + toString(ruleBlock->getImplication()));
        result.push_back(_indent + "activation: " + toString(ruleBlock->getActivation()));
        for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) {
            result.push_back(_indent + toString(ruleBlock->getRule(i)));
        }
        return Op::join(result, _separator);
    }

    std::string FllExporter::toString(const Rule* rule) const {
        return "rule: " + rule->getText();
    }

    std::string FllExporter::toString(const Term* term) const {
        return "term: " + Op::validName(term->getName()) + " " + term->className()
                + " " + term->parameters();
    }

    std::string FllExporter::toString(const Norm* norm) const {
        if (norm) return norm->className();
        return "none";
    }

    std::string FllExporter::toString(const Activation* activation) const {
        if (not activation) return "none";
        if (activation->parameters().empty()) return activation->className();
        return activation->className() + " " + activation->parameters();
    }

    std::string FllExporter::toString(const Defuzzifier* defuzzifier) const {
        if (not defuzzifier) return "none";
        if (const IntegralDefuzzifier * integralDefuzzifier =
                dynamic_cast<const IntegralDefuzzifier*> (defuzzifier)) {
            return defuzzifier->className() + " " + Op::str(integralDefuzzifier->getResolution());

        } else if (const WeightedDefuzzifier * weightedDefuzzifier =
                dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) {
            return weightedDefuzzifier->className() + " " + weightedDefuzzifier->getTypeName();
        }
        return defuzzifier->className();
    }

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

}