Exemplo n.º 1
0
    Engine* FclImporter::fromString(const std::string& fcl) const {
        FL_unique_ptr<Engine> engine(new Engine);

        std::map<std::string, std::string> tags;
        tags["VAR_INPUT"] = "END_VAR";
        tags["VAR_OUTPUT"] = "END_VAR";
        tags["FUZZIFY"] = "END_FUZZIFY";
        tags["DEFUZZIFY"] = "END_DEFUZZIFY";
        tags["RULEBLOCK"] = "END_RULEBLOCK";
        std::map<std::string, std::string>::const_iterator tagFinder;

        std::string currentTag = "", closingTag = "";
        std::ostringstream block;
        std::istringstream fclReader(fcl);
        std::string line;

        int lineNumber = 0;
        while (std::getline(fclReader, line)) {
            ++lineNumber;
            std::vector<std::string> comments;
            comments = Op::split(line, "//");
            if (comments.size() > 1) {
                line = comments.front();
            }
            comments = Op::split(line, "#");
            if (comments.size() > 1) {
                line = comments.front();
            }
            line = Op::trim(line);
            if (line.empty() or line.at(0) == '%' or line.at(0) == '#'
                    or (line.substr(0, 2) == "//")) {
                continue;
            }
            line = fl::Op::findReplace(line, ";", "");
            std::istringstream tokenizer(line);
            std::string firstToken;
            tokenizer >> firstToken;

            if (firstToken == "FUNCTION_BLOCK") {
                if (tokenizer.rdbuf()->in_avail() > 0) {
                    std::ostringstream name;
                    std::string token;
                    tokenizer >> token;
                    name << token;
                    while (tokenizer >> token) {
                        name << " " << token;
                    }
                    engine->setName(name.str());
                }
                continue;
            }
            if (firstToken == "END_FUNCTION_BLOCK") {
                break;
            }

            if (currentTag.empty()) {
                tagFinder = tags.find(firstToken);
                if (tagFinder == tags.end()) {
                    std::ostringstream ex;
                    ex << "[syntax error] unknown block definition <" << firstToken
                            << "> " << " in line " << lineNumber << ": " << line;
                    throw fl::Exception(ex.str(), FL_AT);
                }
                currentTag = tagFinder->first;
                closingTag = tagFinder->second;
                block.str("");
                block.clear();
                block << line << "\n";
                continue;
            }

            if (not currentTag.empty()) {
                if (firstToken == closingTag) {
                    processBlock(currentTag, block.str(), engine.get());
                    currentTag = "";
                    closingTag = "";
                } else if (tags.find(firstToken) != tags.end()) {
                    //if opening new block without closing the previous one
                    std::ostringstream ex;
                    ex << "[syntax error] expected <" << closingTag << "> before <"
                            << firstToken << "> in line: " << line;
                    throw fl::Exception(ex.str(), FL_AT);
                } else {
                    block << line << "\n";
                }
                continue;
            }
        }
Exemplo n.º 2
0
    Engine* FclImporter::fromString(const std::string& fcl) const {
        Engine* engine = new Engine;
        engine->addHedge(new Any);
        engine->addHedge(new Extremely);
        engine->addHedge(new Not);
        engine->addHedge(new Seldom);
        engine->addHedge(new Somewhat);
        engine->addHedge(new Very);

        std::map<std::string, std::string> tags;
        tags["VAR_INPUT"] = "END_VAR";
        tags["VAR_OUTPUT"] = "END_VAR";
        tags["FUZZIFY"] = "END_FUZZIFY";
        tags["DEFUZZIFY"] = "END_DEFUZZIFY";
        tags["RULEBLOCK"] = "END_RULEBLOCK";
        std::map<std::string, std::string>::const_iterator tagFinder;

        std::string currentTag = "", closingTag = "";
        std::ostringstream block;
        std::istringstream fclReader(fcl);
        std::string line;
        int lineNumber = 0;
        try {
            while (std::getline(fclReader, line)) {
                ++lineNumber;
                line = Op::trim(line);
                if (line.empty() or line.at(0) == '#')
                    continue;

                std::istringstream tokenizer(line);
                std::string firstToken;
                tokenizer >> firstToken;

                if (firstToken == "FUNCTION_BLOCK") {
                    if (tokenizer.rdbuf()->in_avail() > 0) {
                        std::string name;
                        tokenizer >> name;
                        engine->setName(name);
                    }
                    continue;
                }
                if (firstToken == "END_FUNCTION_BLOCK") {
                    break;
                }

                if (currentTag.empty()) {
                    tagFinder = tags.find(firstToken);
                    if (tagFinder == tags.end()) {
                        std::ostringstream ex;
                        ex << "[syntax error] unknown block definition <" << firstToken
                                << "> " << " in line " << lineNumber << ": " << line;
                        throw fl::Exception(ex.str(), FL_AT);
                    }
                    currentTag = tagFinder->first;
                    closingTag = tagFinder->second;
                    block.clear();
                    block.str("");
                    block << line << "\n";
                    continue;
                }

                if (not currentTag.empty()) {
                    if (firstToken == closingTag) {
                        processBlock(currentTag, block.str(), engine);
                        currentTag = "";
                        closingTag = "";
                    } else if (tags.find(firstToken) != tags.end()) {
                        //if opening new block without closing the previous one
                        std::ostringstream ex;
                        ex << "[syntax error] expected <" << closingTag << "> before <"
                                << firstToken << "> in line: " << line;
                        throw fl::Exception(ex.str(), FL_AT);
                    } else {
                        block << line << "\n";
                    }
                    continue;
                }
            }

            if (not currentTag.empty()) {
                std::ostringstream ex;
                ex << "[syntax error] ";
                if (block.rdbuf()->in_avail() > 0) {
                    ex << "expected <" << closingTag << "> for block:\n" << block.str();
                } else {
                    ex << "expected <" << closingTag << ">, but not found";
                }
                throw fl::Exception(ex.str(), FL_AT);
            }
            if (engine->numberOfInputVariables() == 0
                    and engine->numberOfOutputVariables() == 0
                    and (engine->numberOfRuleBlocks() == 0
                    or engine->getRuleBlock(0)->numberOfRules() == 0)) {
                std::ostringstream ex;
                ex << "[importer error] the FCL code introduced produces an empty engine";
                throw fl::Exception(ex.str(), FL_AT);
            }
        } catch (fl::Exception& ex) {