RuleBlock* FuzzyEngine::ruleBlock(const std::string& name) const { for (int i = 0; i < numberOfRuleBlocks(); ++i) { if (ruleBlock(i)->name() == name) { return ruleBlock(i); } } return NULL; }
void FuzzyEngine::process(int ruleblock, bool clear) { if (clear) { for (int i = 0; i < numberOfOutputLVars(); ++i) { outputLVar(i)->output().clear(); } } ruleBlock(ruleblock)->fireRules(); }
void FllImporter::processRuleBlock(const std::string& block, Engine* engine) const { std::istringstream reader(block); std::string line; FL_unique_ptr<RuleBlock> ruleBlock(new 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 ("implication" == keyValue.first) { ruleBlock->setImplication(parseTNorm(keyValue.second)); } else if ("activation" == keyValue.first) { TNormFactory* tnorm = FactoryManager::instance()->tnorm(); //@todo remove backwards compatibility in version 7.0 if (tnorm->hasConstructor(keyValue.second)) { ruleBlock->setImplication(parseTNorm(keyValue.second)); FL_LOG("[warning] obsolete usage of identifier <activation: TNorm> " "in RuleBlock"); FL_LOG("[information] from version 6.0, the identifiers are " "<activation: Activation> for Activation methods " "and <implication: TNorm> for T-Norms"); FL_LOG("[backward compatibility] assumed " "<implication: " << keyValue.second << "> " "instead of <activation: " << keyValue.second << ">"); } else { ruleBlock->setActivation(parseActivation(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 Exception("[import error] key <" + keyValue.first + "> not " "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT); } } engine->addRuleBlock(ruleBlock.release()); }
std::string FuzzyEngine::toString() const { std::stringstream ss; ss << "FUNCTION_BLOCK " << name() << "\n\n"; ss << "VAR_INPUT\n"; for (int i = 0; i < numberOfInputLVars(); ++i) { ss << inputLVar(i)->name() << ": REAL;\n"; } ss << "END_VAR\n\n"; for (int i = 0; i < numberOfInputLVars(); ++i) { ss << "FUZZIFY " << inputLVar(i)->name() << "\n"; for (int j = 0; j < inputLVar(i)->numberOfTerms(); ++j) { ss << inputLVar(i)->term(j)->toString() << ";\n"; } ss << "END_FUZZIFY\n\n"; } ss << "VAR_OUTPUT\n"; for (int i = 0; i < numberOfOutputLVars(); ++i) { ss << outputLVar(i)->name() << ": REAL\n"; } ss << "END_VAR\n\n"; for (int i = 0; i < numberOfOutputLVars(); ++i) { ss << "DEFUZZIFY " << outputLVar(i)->name() << "\n"; for (int j = 0; j < outputLVar(i)->numberOfTerms(); ++j) { ss << outputLVar(i)->term(j)->toString() << ";\n"; } ss << "END_DEFUZZIFY\n\n"; } for (int i = 0; i < numberOfRuleBlocks(); ++i) { ss << ruleBlock(i)->toString() << "\n\n"; } ss << "END_FUNCTION_BLOCK"; return ss.str(); }
RuleBlock* FuzzyEngine::removeRuleBlock(int index) { RuleBlock* result = ruleBlock(index); this->_rule_blocks.erase(this->_rule_blocks.begin() + index); return result; }