public void load ( String rule , Engine engine ) { deactivate ( ) ; setEnabled ( true ) ; setText ( rule ) ; StringTokenizer tokenizer = new StringTokenizer ( rule ) ; String token ; StringBuilder strAntecedent = new StringBuilder ( ) ; StringBuilder strConsequent = new StringBuilder ( ) ; double ruleWeight = 1.0 ; final byte S_NONE = 0 , S_IF = 1 , S_THEN = 2 , S_WITH = 3 , S_END = 4 ; byte state = S_NONE ; try { while ( tokenizer . hasMoreTokens ( ) ) { token = tokenizer . nextToken ( ) ; int commentIndex = token . indexOf ( ' ' ) ; if ( commentIndex >= 0 ) { token = token . substring ( 0 , commentIndex ) ; } switch ( state ) { case S_NONE : if ( Rule . FL_IF . equals ( token ) ) { state = S_IF ; } else { throw new RuntimeException ( String . format ( "[syntax error] expected keyword <%s>, but found <%s> in rule: %s" , Rule . FL_IF , token , rule ) ) ; } break ; case S_IF : if ( Rule . FL_THEN . equals ( token ) ) { state = S_THEN ; } else { strAntecedent . append ( token ) . append ( " " ) ; } break ; case S_THEN : if ( Rule . FL_WITH . equals ( token ) ) { state = S_WITH ; } else { strConsequent . append ( token ) . append ( " " ) ; } break ; case S_WITH : try { ruleWeight = Op . toDouble ( token ) ; state = S_END ; } catch ( NumberFormatException ex ) { throw ex ; } break ; case S_END : throw new RuntimeException ( String . format ( "[syntax error] unexpected token <%s> at the end of rule" , token ) ) ; default : throw new RuntimeException ( String . format ( "[syntax error] unexpected state <%s>" , state ) ) ; } } if ( state == S_NONE ) { throw new RuntimeException ( String . format ( "[syntax error] %s rule: %s" , ( rule . isEmpty ( ) ? "empty" : "ignored" ) , rule ) ) ; } else if ( state == S_IF ) { throw new RuntimeException ( String . format ( "[syntax error] keyword <%s> not found in rule: %s" , Rule . FL_THEN , rule ) ) ; } else if ( state == S_WITH ) { throw new RuntimeException ( String . format ( "[syntax error] expected a numeric value as the weight of the rule: %s" , rule ) ) ; } getAntecedent ( ) . load ( strAntecedent . toString ( ) , engine ) ; getConsequent ( ) . load ( strConsequent . toString ( ) , engine ) ; setWeight ( ruleWeight ) ; } catch ( RuntimeException ex ) { unload ( ) ; throw ex ; } }
Loads the rule with the given text and uses the engine to identify and retrieve references to the input variables and output variables as required