def generate_matching_function(goals, fact_stores, histories, sym_id, interp_rule, logger, send_goal_func, create_new_location_func, location=None): rule_id = interp_rule['rule_id'] fact_pat = interp_rule['entry'] term_pats = fact_pat['terms'] has_no_simplify = interp_rule['has_no_simplify'] match_partners = generate_partner_matching_function(goals, rule_id, has_no_simplify, fact_stores, histories, interp_rule['match_steps'] ,interp_rule['exist_locs'], interp_rule['has_exist_locs'], interp_rule['rhs'] ,logger, send_goal_func, create_new_location_func, location=location) rule_name = "Rule: %s # %s" % (get_all_rule_classes()[interp_rule['rule_id']].__name__,interp_rule['occ_id']) if location != None: loc_pat = fact_pat['location'] def match_loc(): return match_terms_inplace([loc_pat], [location.value]) def unbind_loc(): loc_pat.unbind() else: def match_loc(): return True def unbind_loc(): pass if interp_rule['propagated']: def match_func(act_fact_repr): # print rule_name if match_terms_inplace(term_pats, act_fact_repr['values']) and match_loc(): matched = True ids = [(act_fact_repr['sym_id'],act_fact_repr['fact_id']['id'])] while matched: matched = match_partners(ids, [], [act_fact_repr]) # map(lambda t: t.unbind(),term_pats) for t in term_pats: t.unbind() unbind_loc() return True else: def match_func(act_fact_repr): # print rule_name if match_terms_inplace(term_pats, act_fact_repr['values']) and match_loc(): ids = [(act_fact_repr['sym_id'],act_fact_repr['fact_id']['id'])] done = match_partners(ids, [act_fact_repr], []) # map(lambda t: t.unbind(),term_pats) for t in term_pats: t.unbind() unbind_loc() return not done else: return True return match_func
def pretty_interp_rule(interp_rule): strs = [] strs.append( "Rule: %s # %s" % (get_all_rule_classes()[interp_rule['rule_id']].__name__,interp_rule['occ_id']) ) strs.append( "Entry: %s %s" % ('Propagate' if interp_rule['propagated'] else 'Simplify', pretty_fact_pat(interp_rule['entry']) ) ) strs.append( "Matching Steps:" ) for match_step in interp_rule['match_steps']: if match_step['is_lookup']: match_type = 'Propagate' if match_step['propagated'] else 'Simplify' match_pat = pretty_fact_pat(match_step['fact_pat']) strs.append( "Lookup: %s %s (Store Index: %s)" % (match_type,match_pat,match_step['lookup_index']) ) else: strs.append( "Schedule Guard: %s" % match_step['guard_str'] ) return "-----------------------\n" + '\n'.join(strs) + "\n"
def pretty_interp_rule(interp_rule): strs = [] strs.append("Rule: %s # %s" % (get_all_rule_classes()[interp_rule['rule_id']].__name__, interp_rule['occ_id'])) strs.append("Entry: %s %s" % ('Propagate' if interp_rule['propagated'] else 'Simplify', pretty_fact_pat(interp_rule['entry']))) strs.append("Matching Steps:") for match_step in interp_rule['match_steps']: if match_step['is_lookup']: match_type = 'Propagate' if match_step['propagated'] else 'Simplify' match_pat = pretty_fact_pat(match_step['fact_pat']) strs.append("Lookup: %s %s (Store Index: %s)" % (match_type, match_pat, match_step['lookup_index'])) else: strs.append("Schedule Guard: %s" % match_step['guard_str']) return "-----------------------\n" + '\n'.join(strs) + "\n"
def interpret_rule(rule, fact_stores): rule_entries = map(lambda s: (False, s), rule.simplify()) + map( lambda p: (True, p), rule.propagate()) rule_lhs = map(lambda t: t[1], rule_entries) guards = rule.guards() variables = rule.get_vars() if len(rule.simplify()) == 0: has_no_simplify = True else: has_no_simplify = False rule_name = get_all_rule_classes()[rule.rule_id].__name__ interp_rule = [] for i in xrange(0, len(rule_entries)): propagated, rule_entry = rule_entries[i] partners = rule_entries[:i] + rule_entries[(i + 1):] rule_entry.exist_bind_terms() early_guard_steps, guards_rest = schedule_guards(guards) match_steps = compute_optimal_matching(fact_stores, partners, guards_rest, early_guard_steps) for var in variables: var.unbind() interp_rule.append({ 'rule_id': rule.rule_id, 'occ_id': i, 'propagated': propagated, 'entry': make_fact_pat(rule_entry), 'match_steps': match_steps, 'has_no_simplify': has_no_simplify, 'rhs': rule.consequents, 'exist_locs': rule.get_exist_locs, 'has_exist_locs': len(rule.exist_locations) > 0 }) return interp_rule
def interpret_rule(rule, fact_stores): rule_entries = map(lambda s: (False,s),rule.simplify()) + map(lambda p: (True,p),rule.propagate()) rule_lhs = map(lambda t: t[1],rule_entries) guards = rule.guards() variables = rule.get_vars() if len(rule.simplify()) == 0: has_no_simplify = True else: has_no_simplify = False rule_name = get_all_rule_classes()[rule.rule_id].__name__ interp_rule = [] for i in xrange(0,len(rule_entries)): propagated,rule_entry = rule_entries[i] partners = rule_entries[:i] + rule_entries[(i+1):] rule_entry.exist_bind_terms() early_guard_steps,guards_rest = schedule_guards(guards) match_steps = compute_optimal_matching(fact_stores, partners, guards_rest, early_guard_steps) for var in variables: var.unbind() interp_rule.append( { 'rule_id' : rule.rule_id, 'occ_id' : i, 'propagated' : propagated, 'entry' : make_fact_pat(rule_entry), 'match_steps' : match_steps, 'has_no_simplify' : has_no_simplify, 'rhs' : rule.consequents, 'exist_locs' : rule.get_exist_locs, 'has_exist_locs' : len(rule.exist_locations) > 0 } ) return interp_rule
def new_histories(): histories = {} for rule_id in get_all_rule_classes(): histories[rule_id] = PropHistory(rule_id) return histories