def ____handle_0arity_fluents(rule_list, zero_arity_fluents): old_strs = [r'\b' + str(fluent) + r'\b' for fluent in zero_arity_fluents] replaces = [fluent + '()' for fluent in zero_arity_fluents] for e, rule in enumerate(rule_list): relations = Util.repeat_do_function(Util.sub_lambda_exp, zip(zero_arity_fluents, replaces), rule[1]) formula = Util.repeat_do_function(Util.sub_lambda_exp, zip(zero_arity_fluents, replaces), rule[2]) rule_list[e] = (rule[0], relations, formula) return rule_list
def get_smt_body(formula): #print '-----need to tans formula is:', formula formula = __logicSym_to_smtSym(formula) fluents = context_operator.get_fluents() fluent_sub = '|'.join([r"(?:(%s)\(([\w,\s]*)\))" % fun for fun in fluents]) #print fluents fluent_sub_pattern = re.compile(fluent_sub) temp_formula = "" while temp_formula != formula: temp_formula = formula #formula = fluent_sub_pattern.sub(__mrepl_fluent, formula) formula = Util.repeat_replace_inner_with_pattern( fluent_sub_pattern, __mrepl_fluent, formula) #print "repl_fluent---",formula #formula = Util.repeat_replace_inner_with_pattern(most_inner_pattern, __mrepl_no_inner_formula, formula) formula = most_inner_pattern.sub(__mrepl_no_inner_formula, formula) #print "inner_pattern---",formula formula = Util.repeat_replace_inner_with_pattern( quantifier_pattern, __mrepl_quntifier, formula) #formula = quantifier_pattern.sub(__mrepl_quntifier, formula) formula = re.sub(r'.*', __mrepl_no_inner_formula, formula) repl_list = context_operator.get_replace_list() repl_list.reverse() #print repl_list formula = Util.repeat_do_function(Util.sub_lambda_exp, repl_list, formula) #print formula context_operator.clear_replace_list() return formula
def __mrepl_ground(match): logical_connector = "&" if match.group('head') == 'forall' else '|' #universe = {'Int': ['1', '0', '3', '2'], '_S1': [], '_S2': ['p2', 'p1'], 'Bool': ['True', 'False']} universe, assignment, default_value = context_operator.get_current_model() vars_sorts = { elem.split(':')[0]: elem.split(':')[1] for elem in match.group('var').split(',') } var_list = vars_sorts.keys() #var_constraint_dict = __get_constraint_var_dict(var_list, match.group('body')) #vars_consts = [ (var, var_constraint_dict[var])if var in var_constraint_dict else (var, universe[sort]) for var, sort in vars_sorts.iteritems()] vars_consts = [(var, universe[sort]) for var, sort in vars_sorts.iteritems()] vars_list = [r'\b' + var + r'\b' for var in zip(*vars_consts)[0]] consts_list = list(itertools.product(*zip(*vars_consts)[1])) instances = [ Util.repeat_do_function(Util.sub_lambda_exp, zip(vars_list, list(consts)), match.group('body')) for consts in consts_list ] return "(%s)" % logical_connector.join(["(%s)" % ins for ins in instances])
def __set_default_value(logical_formula, default_value): """ set the unknown fluent not in assignment with the default value """ encode_pair_list = default_value.items() logical_formula = Util.repeat_do_function(Util.sub_lambda_exp, encode_pair_list, logical_formula) return logical_formula
def __assignment(formula, assignment): replace_list = [(r'\b%s' % (fluent.replace('(', '\(').replace(')', '\)')), str(value)) for fluent, value in assignment.iteritems()] #print replace_list while True: new_formula = Util.repeat_do_function(Util.sub_lambda_exp, replace_list, formula) if new_formula == formula: return formula else: formula = new_formula
def ____generate_feature_pattern(feature_list, var_list): #print feature_list, var_list feature = '_'.join(feature_list) replace_pattern_list = ['_' + mvar + '_' for mvar in var_list] #print replace_pattern_list replace_pattern_list = zip(replace_pattern_list, ['_(.+?)_'] * (len(replace_pattern_list))) #print replace_pattern_list feature = Util.repeat_do_function(Util.replace_lambda_exp, replace_pattern_list, feature).replace("pi_", "") #print "--feature for pattern--",feature,feature.replace("pi_","") context_operator.add_feature((feature_list, var_list, feature)) return re.compile(feature) #handle pi(A) which means A is variable
def ____rename_rule_vars(rule_list, varlist_list): #rename free varable for e, rename_var_list in enumerate(varlist_list): #print "rename##########:", rename_var_list new_var_list = [ context_operator.get_new_var() for i in rename_var_list ] #print "new_var#########",new_var_list rename_var_list = [r'\b' + elem + r'\b' for elem in rename_var_list] temp_rule = Util.repeat_do_function( Util.sub_lambda_exp, zip(rename_var_list, new_var_list), rule_list[e][1] + "#" + rule_list[e][2]) #print "temp_rule ~~~~~~~~>:", temp_rule #print Util.rename_forall(temp_rule.split('&')[1]) rule_list[e] = (rule_list[e][0], temp_rule.split('#')[0], Util.rename_forall(temp_rule.split('#')[1])) return rule_list
def __assignment_light(formula, assignment): #print replace_list return Util.repeat_do_function(Util.replace_lambda_exp, list(assignment.items()), formula)
def __logicSym_to_smtSym(formula): formula = formula.replace('!', ' not ').replace('%', ' mod ').replace( '&', ' and ').replace('|', ' or ') return Util.repeat_do_function(Util.sub_lambda_exp, [(r'\bFalse\b', 'false'), (r'\bTrue\b', 'true')], formula)
def __replace_body(old_list, new_list, body): old_list = [ r'\b'+elem+r'\b' for elem in old_list] return Util.repeat_do_function(lambda x,y: re.sub(y[0],y[1],x,1), zip(old_list, new_list),body)