public void load(String antecedent, Engine engine) {
        FuzzyLite.logger().log(Level.FINE, "Antecedent: {0}", antecedent);
        unload();
        setText(antecedent);
        if (antecedent.trim().isEmpty()) {
            throw new RuntimeException("[syntax error] antecedent is empty");
        }
        /*
         Builds an proposition tree from the antecedent of a fuzzy rule.
         The rules are:
         1) After a variable comes 'is',
         2) After 'is' comes a hedge or a term
         3) After a hedge comes a hedge or a term
         4) After a term comes a variable or an operator
         */

        Function function = new Function();
        String postfix = function.toPostfix(antecedent);
        FuzzyLite.logger().log(Level.FINE, "Postfix {0}", postfix);

        final byte S_VARIABLE = 1, S_IS = 2, S_HEDGE = 4, S_TERM = 8, S_AND_OR = 16;
        byte state = S_VARIABLE;
        Deque<Expression> expressionStack = new ArrayDeque<Expression>();
        Proposition proposition = null;

        StringTokenizer tokenizer = new StringTokenizer(postfix);
        String token = "";

        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();
            if ((state & S_VARIABLE) != 0) {
                Variable variable = null;
                if (engine.hasInputVariable(token)) {
                    variable = engine.getInputVariable(token);
                } else if (engine.hasOutputVariable(token)) {
                    variable = engine.getOutputVariable(token);
                }
                if (variable != null) {
                    proposition = new Proposition();
                    proposition.setVariable(variable);
                    expressionStack.push(proposition);

                    state = S_IS;
                    FuzzyLite.logger().log(Level.FINE, "Token <{0}> is variable", token);
                    continue;
                }
            }

            if ((state & S_IS) != 0) {
                if (Rule.FL_IS.equals(token)) {
                    state = S_HEDGE | S_TERM;
                    FuzzyLite.logger().log(Level.FINE, "Token <{0}> is keyword", token);
                    continue;
                }
            }

            if ((state & S_HEDGE) != 0) {
                HedgeFactory hedgeFactory = FactoryManager.instance().hedge();
                if (hedgeFactory.hasConstructor(token)) {
                    Hedge hedge = hedgeFactory.constructObject(token);
                    proposition.getHedges().add(hedge);
                    if (hedge instanceof Any) {
                        state = S_VARIABLE | S_AND_OR;
                    } else {
                        state = S_HEDGE | S_TERM;
                    }
                    FuzzyLite.logger().log(Level.FINE, "Token <{0}> is hedge", token);
                    continue;
                }
            }

            if ((state & S_TERM) != 0) {
                if (proposition.getVariable().hasTerm(token)) {
                    proposition.setTerm(proposition.getVariable().getTerm(token));
                    state = S_VARIABLE | S_AND_OR;
                    FuzzyLite.logger().log(Level.FINE, "Token <{0}> is term", token);
                    continue;
                }
            }

            if ((state & S_AND_OR) != 0) {
                if (Rule.FL_AND.equals(token) || Rule.FL_OR.equals(token)) {
                    if (expressionStack.size() < 2) {
                        throw new RuntimeException(String.format(
                                "[syntax error] logical operator <%s> expects at least two operands, but found <%d>",
                                token, expressionStack.size()));
                    }
                    Operator operator = new Operator();
                    operator.setName(token);
                    operator.setRight(expressionStack.pop());
                    operator.setLeft(expressionStack.pop());
                    expressionStack.push(operator);

                    state = S_VARIABLE | S_AND_OR;
                    FuzzyLite.logger().log(Level.FINE, "Subtree: ({0}) ({1})",
                            new Object[]{operator.getLeft(), operator.getRight()});
                    continue;
                }
            }

            //If reached this point, there was an error
            if ((state & S_VARIABLE) != 0 || (state & S_AND_OR) != 0) {
                throw new RuntimeException(String.format(
                        "[syntax error] expected variable or logical operator, but found <%s>",
                        token));
            }
            if ((state & S_IS) != 0) {
                throw new RuntimeException(String.format(
                        "[syntax error] expected keyword <%s>, but found <%s>",
                        Rule.FL_IS, token));
            }
            if ((state & S_HEDGE) != 0 || (state & S_TERM) != 0) {
                throw new RuntimeException(String.format(
                        "[syntax error] expected hedge or term, but found <%s>",
                        token));
            }
            throw new RuntimeException(String.format(
                    "[syntax error] unexpected token <%s>",
                    token));
        }

        if (!((state & S_VARIABLE) != 0 || (state & S_AND_OR) != 0)) { //only acceptable final state
            if ((state & S_IS) != 0) {
                throw new RuntimeException(String.format(
                        "[syntax error] expected keyword <%s> after <%s>",
                        Rule.FL_IS, token));
            }
            if ((state & S_HEDGE) != 0 || (state & S_TERM) != 0) {
                throw new RuntimeException(String.format(
                        "[syntax error] expected hedge or term, but found <%s>",
                        token));
            }
        }

        if (expressionStack.size() != 1) {
            List<String> errors = new LinkedList<String>();
            while (expressionStack.size() > 1) {
                Expression element = expressionStack.pop();
                errors.add(element.toString());
            }
            throw new RuntimeException(String.format(
                    "[syntax error] unable to parse the following expressions: <%s>",
                    Op.join(errors, " ")));
        }
        setExpression(expressionStack.pop());
    }
public void load ( String antecedent , Engine engine ) { FuzzyLite . logger ( ) . log ( Level . FINE , "Antecedent: {0}" , antecedent ) ; unload ( ) ; setText ( antecedent ) ; if ( antecedent . trim ( ) . isEmpty ( ) ) { throw new RuntimeException ( "[syntax error] antecedent is empty" ) ; } Function function = new Function ( ) ; String postfix = function . toPostfix ( antecedent ) ; FuzzyLite . logger ( ) . log ( Level . FINE , "Postfix {0}" , postfix ) ; final byte S_VARIABLE = 1 , S_IS = 2 , S_HEDGE = 4 , S_TERM = 8 , S_AND_OR = 16 ; byte state = S_VARIABLE ; Deque < Expression > expressionStack = new ArrayDeque < Expression > ( ) ; Proposition proposition = null ; StringTokenizer tokenizer = new StringTokenizer ( postfix ) ; String token = "" ; while ( tokenizer . hasMoreTokens ( ) ) { token = tokenizer . nextToken ( ) ; if ( ( state & S_VARIABLE ) != 0 ) { Variable variable = null ; if ( engine . hasInputVariable ( token ) ) { variable = engine . getInputVariable ( token ) ; } else if ( engine . hasOutputVariable ( token ) ) { variable = engine . getOutputVariable ( token ) ; } if ( variable != null ) { proposition = new Proposition ( ) ; proposition . setVariable ( variable ) ; expressionStack . push ( proposition ) ; state = S_IS ; FuzzyLite . logger ( ) . log ( Level . FINE , "Token <{0}> is variable" , token ) ; continue ; } } if ( ( state & S_IS ) != 0 ) { if ( Rule . FL_IS . equals ( token ) ) { state = S_HEDGE | S_TERM ; FuzzyLite . logger ( ) . log ( Level . FINE , "Token <{0}> is keyword" , token ) ; continue ; } } if ( ( state & S_HEDGE ) != 0 ) { HedgeFactory hedgeFactory = FactoryManager . instance ( ) . hedge ( ) ; if ( hedgeFactory . hasConstructor ( token ) ) { Hedge hedge = hedgeFactory . constructObject ( token ) ; proposition . getHedges ( ) . add ( hedge ) ; if ( hedge instanceof Any ) { state = S_VARIABLE | S_AND_OR ; } else { state = S_HEDGE | S_TERM ; } FuzzyLite . logger ( ) . log ( Level . FINE , "Token <{0}> is hedge" , token ) ; continue ; } } if ( ( state & S_TERM ) != 0 ) { if ( proposition . getVariable ( ) . hasTerm ( token ) ) { proposition . setTerm ( proposition . getVariable ( ) . getTerm ( token ) ) ; state = S_VARIABLE | S_AND_OR ; FuzzyLite . logger ( ) . log ( Level . FINE , "Token <{0}> is term" , token ) ; continue ; } } if ( ( state & S_AND_OR ) != 0 ) { if ( Rule . FL_AND . equals ( token ) || Rule . FL_OR . equals ( token ) ) { if ( expressionStack . size ( ) < 2 ) { throw new RuntimeException ( String . format ( "[syntax error] logical operator <%s> expects at least two operands, but found <%d>" , token , expressionStack . size ( ) ) ) ; } Operator operator = new Operator ( ) ; operator . setName ( token ) ; operator . setRight ( expressionStack . pop ( ) ) ; operator . setLeft ( expressionStack . pop ( ) ) ; expressionStack . push ( operator ) ; state = S_VARIABLE | S_AND_OR ; FuzzyLite . logger ( ) . log ( Level . FINE , "Subtree: ({0}) ({1})" , new Object [ ] { operator . getLeft ( ) , operator . getRight ( ) } ) ; continue ; } } if ( ( state & S_VARIABLE ) != 0 || ( state & S_AND_OR ) != 0 ) { throw new RuntimeException ( String . format ( "[syntax error] expected variable or logical operator, but found <%s>" , token ) ) ; } if ( ( state & S_IS ) != 0 ) { throw new RuntimeException ( String . format ( "[syntax error] expected keyword <%s>, but found <%s>" , Rule . FL_IS , token ) ) ; } if ( ( state & S_HEDGE ) != 0 || ( state & S_TERM ) != 0 ) { throw new RuntimeException ( String . format ( "[syntax error] expected hedge or term, but found <%s>" , token ) ) ; } throw new RuntimeException ( String . format ( "[syntax error] unexpected token <%s>" , token ) ) ; } if ( ! ( ( state & S_VARIABLE ) != 0 || ( state & S_AND_OR ) != 0 ) ) { if ( ( state & S_IS ) != 0 ) { throw new RuntimeException ( String . format ( "[syntax error] expected keyword <%s> after <%s>" , Rule . FL_IS , token ) ) ; } if ( ( state & S_HEDGE ) != 0 || ( state & S_TERM ) != 0 ) { throw new RuntimeException ( String . format ( "[syntax error] expected hedge or term, but found <%s>" , token ) ) ; } } if ( expressionStack . size ( ) != 1 ) { List < String > errors = new LinkedList < String > ( ) ; while ( expressionStack . size ( ) > 1 ) { Expression element = expressionStack . pop ( ) ; errors . add ( element . toString ( ) ) ; } throw new RuntimeException ( String . format ( "[syntax error] unable to parse the following expressions: <%s>" , Op . join ( errors , " " ) ) ) ; } setExpression ( expressionStack . pop ( ) ) ; }
Loads the antecedent with the given text and uses the engine to identify
and retrieve references to the input variables and output variables as
required

@param antecedent is the antecedent of the rule in text
@param engine is the engine from which the rules are part of