void TakagiSugenoConsequent::parse(const std::string& consequent,
            const FuzzyEngine& engine) throw (ParsingException) {
        setFuzzyEngine(engine);
        setConsequent(consequent);

        std::stringstream ss(consequent);
        std::string output_lvar;
        ss >> output_lvar;
        if (!fuzzyEngine().outputLVar(output_lvar)) {
            throw ParsingException(FL_AT, "Output variable <" + output_lvar +
                    "> not found in fuzzy engine");
        }
        setOutputLVar(fuzzyEngine().outputLVar(output_lvar));

        std::string equal;
        ss >> equal;
        if (equal != "=") {
            throw ParsingException(FL_AT, "<=> expected but found <" + equal + ">");
        }
        std::string right_side;
        std::string token;
        while (ss >> token) {
            right_side += token + " ";
        }
        setPostfixFunction(_infix2postfix.transform(right_side));
        std::map<std::string, flScalar> variables;
        for (int i = 0; i < fuzzyEngine().numberOfInputLVars(); ++i) {
            variables[fuzzyEngine().inputLVar(i)->name()] =
                    fuzzyEngine().inputLVar(i)->input();
        }
        for (int i = 0; i < fuzzyEngine().numberOfOutputLVars(); ++i) {
            variables[fuzzyEngine().outputLVar(i)->name()] =
                    fuzzyEngine().outputLVar(i)->output().defuzzify();
        }
        try {
            _infix2postfix.evaluate(postfixFunction(), &variables);
        } catch (FuzzyException& e) {
            throw ParsingException(FL_AT, e.message(), e);
        }
    }
Ejemplo n.º 2
0
    void MamdaniConsequent::parse(const std::string& consequent,
            const FuzzyEngine& engine) throw (ParsingException) {
        std::stringstream ss(consequent);
        std::string token;

        enum e_state {
            S_LVAR = 1, S_IS, S_HEDGE, S_TERM, S_WITH, S_WEIGHT, S_END
        };
        e_state current_state = S_LVAR;

        OutputLVar* output = NULL;
        LinguisticTerm* term = NULL;
        std::vector<Hedge*> hedges;
        Hedge* hedge = NULL;
        while (ss >> token) {
            switch (current_state) {
                case S_LVAR:
                    output = engine.outputLVar(token);
                    if (!output) {
                        throw ParsingException(FL_AT, "Output variable <" +
                                token + "> not registered in fuzzy engine");
                    }
                    current_state = S_IS;
                    break;
                case S_IS:
                    if (token == FuzzyRule::FR_IS) {
                        current_state = S_HEDGE;
                    } else {
                        throw ParsingException(FL_AT, "<" + FuzzyRule::FR_IS + "> expected but found <" +
                                token + ">");
                    }
                    break;
                case S_HEDGE:
                    hedge = engine.hedgeSet().get(token);
                    if (hedge) {
                        hedges.push_back(hedge); //And check for more hedges
                        break;
                    }
                    //intentional fall-through if a term follows
                case S_TERM:
                    term = output->term(token);
                    if (!term) {
                        throw ParsingException(FL_AT, "Term <" + token +
                                "> not found in output variable <" + output->name() + ">");
                    }
                    setOutputLVar(output);
                    setTerm(term);
                    for (size_t i = 0; i < hedges.size(); ++i) {
                        addHedge(hedges[i]);
                    }
                    setWeight(1.0);
                    current_state = S_WITH;
                    break;
                case S_WITH:
                    if (token != FuzzyRule::FR_WITH) {
                        throw ParsingException(FL_AT, "<" + FuzzyRule::FR_WITH + "> expected but found <"
                                + token + ">");
                    }
                    current_state = S_WEIGHT;
                    break;
                case S_WEIGHT:
                    setWeight(atof(token.c_str()));
                    if (weight() > 1.0){
                        throw ParsingException(FL_AT, "Weight [0.0, 1.0] expected but found <" +
                                StrOp::ScalarToString(weight()) + ">");
                    }
                    current_state = S_END;
                    break;
                case S_END:
                    throw ParsingException(FL_AT, "End of line expected but found <" + token + ">");
            }
        }
        if (current_state == S_WEIGHT){
            throw ParsingException(FL_AT, "Weight [0.0, 1.0] expected after <" + FuzzyRule::FR_WITH +
                    "> but found nothing");
        }
    }