<s> """<STR_LIT>""" <EOL> import inspect <EOL> import os <EOL> import pkgutil <EOL> from extensions . objects . models import objects <EOL> import feconf <EOL> import jinja_utils <EOL> FUZZY_RULE_TYPE = '<STR_LIT>' <EOL> def get_obj_type_for_param_name ( rule_class , param_name ) : <EOL> """<STR_LIT>""" <EOL> param_list = get_param_list ( rule_class . description ) <EOL> for item in param_list : <EOL> if item [ <NUM_LIT:0> ] == param_name : <EOL> return item [ <NUM_LIT:1> ] <EOL> raise Exception ( <EOL> '<STR_LIT>' % ( rule_class . __name__ , param_name ) ) <EOL> def get_default_object_values ( ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ ] <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : u'<STR_LIT>' <EOL> } <EOL> def get_rules_for_obj_type ( obj_type ) : <EOL> """<STR_LIT>""" <EOL> rule_dir = os . path . join ( os . getcwd ( ) , feconf . RULES_DIR ) <EOL> rule_class_name = '<STR_LIT>' % obj_type <EOL> results = [ ] <EOL> for loader , name , _ in pkgutil . iter_modules ( path = [ rule_dir ] ) : <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> module = loader . find_module ( name ) . load_module ( name ) <EOL> for name , clazz in inspect . getmembers ( module , inspect . isclass ) : <EOL> ancestors = clazz . __bases__ <EOL> ancestor_class_names = [ c . __name__ for c in ancestors ] <EOL> if rule_class_name in ancestor_class_names : <EOL> results . append ( clazz ) <EOL> return results <EOL> def get_description_strings_for_obj_type ( obj_type ) : <EOL> """<STR_LIT>""" <EOL> rules = get_rules_for_obj_type ( obj_type ) <EOL> return { <EOL> rule . __name__ : rule . description <EOL> for rule in rules <EOL> } <EOL> def get_param_list ( description ) : <EOL> """<STR_LIT>""" <EOL> param_list = [ ] <EOL> while description . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> opening_index = description . find ( '<STR_LIT>' ) <EOL> description = description [ opening_index + <NUM_LIT:2> : ] <EOL> bar_index = description . find ( '<STR_LIT:|>' ) <EOL> param_name = description [ : bar_index ] <EOL> description = description [ bar_index + <NUM_LIT:1> : ] <EOL> closing_index = description . find ( '<STR_LIT>' ) <EOL> normalizer_string = description [ : closing_index ] <EOL> description = description [ closing_index + <NUM_LIT:2> : ] <EOL> param_list . append ( <EOL> ( param_name , getattr ( objects , normalizer_string ) ) <EOL> ) <EOL> return param_list <EOL> CERTAIN_TRUE_VALUE = <NUM_LIT:1.0> <EOL> CERTAIN_FALSE_VALUE = <NUM_LIT:0.0> <EOL> class Rule ( object ) : <EOL> """<STR_LIT>""" <EOL> subject_type = None <EOL> description = '<STR_LIT>' <EOL> _params = None <EOL> _fs = None <EOL> @ property <EOL> def params ( self ) : <EOL> if self . _params is None : <EOL> self . _params = get_param_list ( self . description ) <EOL> return self . _params <EOL> def __init__ ( self , * args ) : <EOL> if len ( args ) != len ( self . params ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( self . params , args ) ) <EOL> for ind , param_tuple in enumerate ( self . params ) : <EOL> setattr ( self , param_tuple [ <NUM_LIT:0> ] , param_tuple [ <NUM_LIT:1> ] . normalize ( args [ ind ] ) ) <EOL> self . _validate_params ( ) <EOL> def _validate_params ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _evaluate ( self , subject ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _fuzzify_truth_value ( self , bool_value ) : <EOL> """<STR_LIT>""" <EOL> return CERTAIN_TRUE_VALUE if bool ( bool_value ) else CERTAIN_FALSE_VALUE <EOL> def _invert_fuzzy_truth_value ( self , fuzzy_value ) : <EOL> """<STR_LIT>""" <EOL> return CERTAIN_TRUE_VALUE - fuzzy_value <EOL> def set_fs ( self , fs ) : <EOL> """<STR_LIT>""" <EOL> self . _fs = fs <EOL> return self <EOL> @ property <EOL> def fs ( self ) : <EOL> return self . _fs <EOL> def eval ( self , subject ) : <EOL> """<STR_LIT>""" <EOL> return self . _evaluate ( self . subject_type . normalize ( subject ) ) <EOL> def evaluate_rule ( rule_spec , answer_type , context_params , answer , fs ) : <EOL> """<STR_LIT>""" <EOL> all_rule_classes = get_rules_for_obj_type ( answer_type ) <EOL> rule = next ( r for r in all_rule_classes <EOL> if r . __name__ == rule_spec . rule_type ) <EOL> param_list = [ ] <EOL> param_defns = get_param_list ( rule . description ) <EOL> for ( param_name , obj_cls ) in param_defns : <EOL> parsed_param = rule_spec . inputs [ param_name ] <EOL> if isinstance ( parsed_param , basestring ) and '<STR_LIT>' in parsed_param : <EOL> parsed_param = jinja_utils . parse_string ( <EOL> parsed_param , context_params , autoescape = False ) <EOL> normalized_param = obj_cls . normalize ( parsed_param ) <EOL> param_list . append ( normalized_param ) <EOL> constructed_rule = rule ( * param_list ) <EOL> constructed_rule . set_fs ( fs ) <EOL> return constructed_rule . eval ( answer ) </s>
