def check_scope(self, ast_node, ctxt): old_ctxt = ctxt ctxt = copy_ctxt(ctxt) decs = ast_node.decs inspect = Inspector() dec_preds = {} # Check scoping of predicate declarations for dec in inspect.filter_decs(decs, fact=True): local_ctxt = self.check_scope(dec, ctxt) for pred in local_ctxt['preds']: if pred.name in dec_preds: dec_preds[pred.name].append(pred) else: dec_preds[pred.name] = [pred] extend_ctxt(ctxt, local_ctxt) self.compose_duplicate_error_reports("predicate", dec_preds) # Check scoping of rule declarations for dec in inspect.filter_decs(decs, rule=True): self.check_scope(dec, ctxt) return old_ctxt
def check_scope(self, ast_node, ctxt): ctxt = copy_ctxt(ctxt) heads = ast_node.slhs + ast_node.plhs inspect = Inspector() # Extend location context with all rule head variables for fact in map(inspect.get_fact, heads): terms = inspect.get_atoms( [inspect.get_loc(fact)] + inspect.get_args(fact) ) ctxt['vars'] += inspect.filter_atoms(terms, var=True) # Check scope of rule heads. This step is mainly for checking for constant name and # predicate name scope of rule heads map(lambda h: self.check_scope(h, ctxt) , heads) map(lambda g: self.check_scope(g, ctxt) , ast_node.grd) # Include exist variables scopes ctxt['vars'] += ast_node.exists # Incremental include where assign statements for ass_stmt in ast_node.where: self.check_scope(ass_stmt.builtin_exp, ctxt) self.compose_out_scope_error_report(ctxt) ctxt['vars'] += inspect.filter_atoms( inspect.get_atoms(ass_stmt.term_pat), var=True) map(lambda b: self.check_scope(b, ctxt) , ast_node.rhs) ''' for fact in map(inspect.get_fact, ast_node.rhs), fact_atoms=True ): loc = inspect.get_loc(fact) loc_key = loc.compare_value() args = inspect.get_args(fact) atoms = inspect.get_atoms(args) arg_map[loc_key] += map(lambda t: t.name,inspect.filter_atoms(atoms, var=True)) ''' self.compose_out_scope_error_report(ctxt)
def gen_code(self): vars = map(var_name, self.get_vars(self.rule_dec)) props = self.gen_facts(map(lambda f: f.elem, self.rule_dec.plhs)) simps = self.gen_facts(map(lambda f: f.elem, self.rule_dec.slhs)) grds = map(lambda g: TermCodeGen(g).gen_code(IGNORE), self.rule_dec.grd) rhs_atoms, rhs_comps = self.partition_rhs(self.rule_dec.rhs) consq = self.gen_facts(map(lambda f: f.elem, rhs_atoms)) if len(rhs_comps) > 0: comp_set_names = [] comp_codes = [] comp_idx = 0 for comp in rhs_comps: comp_facts = self.gen_facts(comp.elem.facts) term_pat = TermCodeGen(comp.elem.term_pat).gen_code( IGNORE, inner=IGNORE) # term_subj = TermCodeGen(comp.elem.term_subj).gen_code(META_LEVEL) term_subj = TermCodeGen( comp.elem.term_subj).gen_code(META_LEVEL) wrapper_pat = "lambda t: %s" % (AssignDecCodeGen( None).get_exp_wrapper(comp.elem.term_pat) % "t") term_subj = "map(%s,%s)" % (wrapper_pat, term_subj) comp_set_name = "comp_set_%s" % comp_idx comp_code = compile_template(template(''' {| comp_set_name |} = [] for {| term_pat |} in {| term_subj |}: {| comp_set_name |} += [{| ', '.join(comp_facts) |}] '''), term_pat=term_pat, term_subj=term_subj, comp_set_name=comp_set_name, comp_facts=comp_facts) comp_set_names.append(comp_set_name) comp_codes.append(comp_code) comp_idx += 1 comp_set_post_fix = " + " + ' + '.join(comp_set_names) else: comp_codes = [] comp_set_post_fix = "" wheres = [] for assign in self.rule_dec.where: wheres.append(AssignDecCodeGen(assign).gen_code()) grd_class = "" grd_inst = "" if len(grds) > 0: grd_inst = mk_rule_name( self.rule_dec.name) + "Grd(%s)" % ','.join(vars) grd_class = compile_template( template(''' class {| grd_class_name |}(Guard): def __init__(self, {| ', '.join(vars) |}): self.initialize(\"{| grd_class_name |}\",{| ','.join(vars) |}) def evaluate(self): {| ','.join(vars) |} = self.get_vars() return {| ' and '.join(grds) |} '''), grd_class_name=mk_rule_name(self.rule_dec.name) + "Grd", vars=vars, grds=grds) # Generate code to handle existential variables inspect = Inspector() exists_code = "" loc_exist_code = "" num_of_loc_exists = 0 if len(self.rule_dec.exists) > 0: rule_rhs_locs = map( lambda v: v.name, inspect.filter_atoms(map(lambda a: inspect.get_loc(a.elem), rhs_atoms), var=True)) loc_exists = [] pure_exists = [] for exist_var in self.rule_dec.exists: if exist_var.name in rule_rhs_locs: loc_exists.append(exist_var.name) else: pure_exists.append(exist_var.name) if len(pure_exists) > 0: exists_code = "%s = self.exists(%s)" % (','.join( map(var_name, pure_exists)), len(pure_exists)) if len(loc_exists) > 0: num_of_loc_exists = len(loc_exists) loc_exist_code = "%s, = self.exist_locs()" % (','.join( map(var_name, loc_exists))) rule_dec_code = template(''' \'\'\' {| source_snippet |} \'\'\' class {| rule_name |}(Rule): def __init__(self): self.initialize(forall={| len(vars) |},exist_locs={| num_of_loc_exists |}) def propagate(self): {| ','.join(vars) |} = self.get_vars() return [{| ', '.join(props) |}] def simplify(self): {| ','.join(vars) |} = self.get_vars() return [{| ', '.join(simps) |}] def guards(self): {| ','.join(vars) |} = self.get_vars() return [{| grd_inst |}] def consequents(self): {| ','.join(vars) |} = self.get_vars() {| exists_code |} {| loc_exist_code |} {| '\\n'.join(wheres) |} {| '\\n'.join(comp_codes) |} return [{| ', '.join(consq) |}] {| comp_set_post_fix |} register_rule({| rule_name |}) {| grd_class |} ''') source_snippet = self.rule_dec.gen_snippet(self.source_text) output = compile_template(rule_dec_code, rule_name=mk_rule_name(self.rule_dec.name), source_snippet=source_snippet, vars=vars, props=props, simps=simps, grd_class=grd_class, grd_inst=grd_inst, wheres=wheres, consq=consq, exists_code=exists_code, loc_exist_code=loc_exist_code, num_of_loc_exists=num_of_loc_exists, comp_codes=comp_codes, comp_set_post_fix=comp_set_post_fix) return compact(output)
def int_check(self, ast_node): heads = ast_node.slhs + ast_node.plhs inspect = Inspector() # Build the neighborhood free variables mapping of the rule heads (NFV mapping) locs = [] neighbor_fvs = {} for fact in map(inspect.get_fact, heads): loc = inspect.get_loc(fact) loc_key = loc.name if loc_key not in neighbor_fvs: neighbor_fvs[loc_key] = [] args = inspect.get_args(fact) atoms = inspect.get_atoms(args) locs.append( loc ) neighbor_fvs[loc_key] += inspect.filter_atoms(atoms, var=True) # From NFV mapping, nominate the primary location all_locs = neighbor_fvs.keys() ast_node.primary_loc = None ast_node.primary_fv = None missing_vars = {} for loc in neighbor_fvs: fv = map(lambda t: t.name, neighbor_fvs[loc]) contains_all_locs = True for t_loc in all_locs: if (t_loc not in fv) and t_loc != loc: contains_all_locs = False if loc in missing_vars: missing_vars[loc].append( t_loc ) else: missing_vars[loc] = [t_loc] if contains_all_locs: ast_node.primary_loc = loc ast_node.primary_fv = neighbor_fvs[loc] del neighbor_fvs[loc] break ast_node.neighbor_fvs = neighbor_fvs ast_node.neighbor_restriction = len(neighbor_fvs.keys()) if ast_node.primary_loc == None: legend = ("%s %s: Location variable(s).\n" % (terminal.T_GREEN_BACK,terminal.T_NORM)) + ("%s %s: Argument variable.\n" % (terminal.T_RED_BACK,terminal.T_NORM)) for loc in missing_vars: legend += "Location %s has no links to %s.\n" % (loc,','.join(missing_vars[loc])) error_report = "Rule %s is not neighbor restricted; None of location(s) %s can be primary location." % (ast_node.name,','.join(neighbor_fvs.keys()) ) error_idx = self.declare_error(error_report , legend) for loc in neighbor_fvs: map(lambda t: self.extend_error(error_idx,t), neighbor_fvs[loc]) map(lambda t: self.extend_info(error_idx,t), locs) return None # print "\n\n\n" # print ast_node.primary_loc # print ast_node.primary_fv # print ast_node.neighbor_fvs # print ast_node.neighbor_restriction primary_loc = ast_node.primary_loc primary_fv = ast_node.primary_fv primary_grds = [] neighbor_grds = {} # Identify guards that are grounded by primary location for g_term in ast_node.grd: g_vars = inspect.filter_atoms( inspect.get_atoms(g_term), var=True) excluded = not_contained(primary_fv, g_vars, key=lambda t:t.name) # print (ast_node.primary_loc,map(lambda t:t.name,excluded)) if len(excluded) == 0: primary_grds.append(g_term) else: for loc in neighbor_fvs: excluded = not_contained(primary_fv + neighbor_fvs[loc], g_vars, key=lambda t:t.name) if len(excluded) == 0: if loc in neighbor_grds: neighbor_grds[loc].append( g_term ) else: neighbor_grds[loc] = [g_term] # TODO: Add neighbor restriction guard test ast_node.primary_grds = primary_grds ast_node.neighbor_grds = neighbor_grds
def gen_code(self): vars = map(var_name, self.get_vars(self.rule_dec) ) props = self.gen_facts( map(lambda f: f.elem, self.rule_dec.plhs) ) simps = self.gen_facts( map(lambda f: f.elem, self.rule_dec.slhs) ) grds = map(lambda g: TermCodeGen(g).gen_code(IGNORE), self.rule_dec.grd) rhs_atoms,rhs_comps = self.partition_rhs(self.rule_dec.rhs) consq = self.gen_facts( map(lambda f: f.elem, rhs_atoms) ) if len(rhs_comps) > 0: comp_set_names = [] comp_codes = [] comp_idx = 0 for comp in rhs_comps: comp_facts = self.gen_facts( comp.elem.facts ) term_pat = TermCodeGen(comp.elem.term_pat).gen_code(IGNORE, inner=IGNORE) # term_subj = TermCodeGen(comp.elem.term_subj).gen_code(META_LEVEL) term_subj = TermCodeGen(comp.elem.term_subj).gen_code(META_LEVEL) wrapper_pat = "lambda t: %s" % (AssignDecCodeGen(None).get_exp_wrapper(comp.elem.term_pat) % "t") term_subj = "map(%s,%s)" % (wrapper_pat ,term_subj) comp_set_name = "comp_set_%s" % comp_idx comp_code = compile_template( template(''' {| comp_set_name |} = [] for {| term_pat |} in {| term_subj |}: {| comp_set_name |} += [{| ', '.join(comp_facts) |}] '''), term_pat=term_pat, term_subj=term_subj, comp_set_name=comp_set_name, comp_facts=comp_facts) comp_set_names.append( comp_set_name ) comp_codes.append( comp_code ) comp_idx += 1 comp_set_post_fix = " + " + ' + '.join(comp_set_names) else: comp_codes = [] comp_set_post_fix = "" wheres = [] for assign in self.rule_dec.where: wheres.append( AssignDecCodeGen(assign).gen_code() ) grd_class = "" grd_inst = "" if len(grds) > 0: grd_inst = mk_rule_name(self.rule_dec.name) + "Grd(%s)" % ','.join(vars) grd_class = compile_template(template( ''' class {| grd_class_name |}(Guard): def __init__(self, {| ', '.join(vars) |}): self.initialize(\"{| grd_class_name |}\",{| ','.join(vars) |}) def evaluate(self): {| ','.join(vars) |} = self.get_vars() return {| ' and '.join(grds) |} ''' ), grd_class_name=mk_rule_name(self.rule_dec.name) + "Grd", vars=vars, grds=grds) # Generate code to handle existential variables inspect = Inspector() exists_code = "" loc_exist_code = "" num_of_loc_exists = 0 if len(self.rule_dec.exists) > 0: rule_rhs_locs = map(lambda v: v.name, inspect.filter_atoms( map(lambda a: inspect.get_loc(a.elem),rhs_atoms), var=True ) ) loc_exists = [] pure_exists = [] for exist_var in self.rule_dec.exists: if exist_var.name in rule_rhs_locs: loc_exists.append( exist_var.name ) else: pure_exists.append( exist_var.name ) if len(pure_exists) > 0: exists_code = "%s = self.exists(%s)" % (','.join(map(var_name,pure_exists)),len(pure_exists)) if len(loc_exists) > 0: num_of_loc_exists = len(loc_exists) loc_exist_code = "%s, = self.exist_locs()" % (','.join(map(var_name,loc_exists))) rule_dec_code = template(''' \'\'\' {| source_snippet |} \'\'\' class {| rule_name |}(Rule): def __init__(self): self.initialize(forall={| len(vars) |},exist_locs={| num_of_loc_exists |}) def propagate(self): {| ','.join(vars) |} = self.get_vars() return [{| ', '.join(props) |}] def simplify(self): {| ','.join(vars) |} = self.get_vars() return [{| ', '.join(simps) |}] def guards(self): {| ','.join(vars) |} = self.get_vars() return [{| grd_inst |}] def consequents(self): {| ','.join(vars) |} = self.get_vars() {| exists_code |} {| loc_exist_code |} {| '\\n'.join(wheres) |} {| '\\n'.join(comp_codes) |} return [{| ', '.join(consq) |}] {| comp_set_post_fix |} register_rule({| rule_name |}) {| grd_class |} ''') source_snippet = self.rule_dec.gen_snippet(self.source_text) output = compile_template(rule_dec_code, rule_name=mk_rule_name(self.rule_dec.name),source_snippet=source_snippet ,vars=vars, props=props, simps=simps, grd_class=grd_class, grd_inst=grd_inst, wheres=wheres ,consq=consq, exists_code=exists_code, loc_exist_code=loc_exist_code, num_of_loc_exists=num_of_loc_exists ,comp_codes=comp_codes, comp_set_post_fix=comp_set_post_fix) return compact(output)
def one_neighbor_restrict_trans(self, rule_dec): inspect = Inspector() prop_heads = inspect.get_facts( rule_dec.plhs ) simp_heads = inspect.get_facts( rule_dec.slhs ) rule_name = rule_dec.name x = rule_dec.primary_loc y = rule_dec.neighbor_fvs.keys()[0] x_prop_heads = ','.join(map(lambda f: "[%s]%s" % (x,f.gen_snippet(self.source_text)), inspect.filter_facts( prop_heads, x ) )) y_prop_heads = ','.join(map(lambda f: "[%s]%s" % (y,f.gen_snippet(self.source_text)), inspect.filter_facts( prop_heads, y ) )) x_simp_heads = ','.join(map(lambda f: "[%s]%s" % (x,f.gen_snippet(self.source_text)), inspect.filter_facts( simp_heads, x ) )) y_simp_heads = ','.join(map(lambda f: "[%s]%s" % (y,f.gen_snippet(self.source_text)), inspect.filter_facts( simp_heads, y ) )) x_prop_heads = x_prop_heads if len(x_prop_heads) > 0 else "1" y_prop_heads = y_prop_heads if len(y_prop_heads) > 0 else "1" x_simp_heads = x_simp_heads if len(x_simp_heads) > 0 else "1" y_simp_heads = y_simp_heads if len(y_simp_heads) > 0 else "1" x_guards = ','.join( map(lambda f: f.gen_snippet(self.source_text), rule_dec.primary_grds) ) if y in rule_dec.neighbor_grds: y_guards = ','.join( map(lambda f: f.gen_snippet(self.source_text), rule_dec.neighbor_grds[y]) ) else: y_guards = [] x_guards = ("| %s" % x_guards) if len(x_guards) > 0 else "" y_guards = ("| %s" % y_guards) if len(y_guards) > 0 else "" lxs = set(map(lambda t:t.name,rule_dec.primary_fv) + [x]) lrs = set(map(lambda t:t.name,rule_dec.primary_fv + rule_dec.neighbor_fvs[y]) + [x]) xs = ','.join( lxs ) rs = ','.join( lrs ) xs_types = ','.join( map(lambda _: "int" ,lxs) ) rs_types = ','.join( map(lambda _: "int" ,lrs) ) rule_body = ','.join( map(lambda f: f.gen_snippet(self.source_text), inspect.get_facts(rule_dec.rhs) ) ) ''' print "Xs: %s" % xs print "Rs: %s" % rs print "X Guards: %s" % x_guards print "Y Guards: %s" % y_guards print "X Prop: %s" % x_prop_heads print "Y Prop: %s" % y_prop_heads print "X Simp: %s" % x_simp_heads print "Y Simp: %s" % y_simp_heads ''' if inspect.filter_facts( simp_heads, x ) > 0: match_rule_code = template(''' {| y_prop_heads |},{| y_simp_heads |} \ [{| y_loc |}]{| rule_name |}_req({| xs_args |}) {| y_guards |} --o [{| x_loc |}]{| rule_name |}_match({| rs_args |}) priority 2. ''') else: match_rule_code = template(''' [{| y_loc |}]{| rule_name |}_req({| xs_args |}),{| y_prop_heads |},{| y_simp_heads |} \ 1 {| y_guards |} --o [{| x_loc |}]{| rule_name |}_match({| rs_args |}) priority 2. ''') match_rule = compile_template(match_rule_code, y_loc=y, xs_args=xs, rs_args=rs, y_prop_heads=y_prop_heads, y_simp_heads=y_simp_heads ,x_loc=x, rule_name=rule_dec.name, y_guards=y_guards) # print match_rule exists_code = "" if len(rule_dec.exists) > 0: exists_code = "exists %s." % ( ','.join(map(lambda e: e.name,rule_dec.exists)) ) # sys.stdout.write("Here: %s" % exists_code) zero_nr_code = template(''' predicate {| rule_name |}_req :: ({| xs_types |}) -> fact. predicate {| rule_name |}_match :: ({| rs_types |}) -> fact. predicate {| rule_name |}_commit :: ({| rs_types |}) -> fact. rule {| rule_name |}1 :: {| x_prop_heads |},{| x_simp_heads |} \ 1 {| x_guards |} --o [{| y_loc |}]{| rule_name |}_req({| xs_args |}). rule {| rule_name |}2 :: {| match_rule |} rule {| rule_name |}3 :: {| x_prop_heads |},{| x_simp_heads |},[{| x_loc |}]{| rule_name |}_match({| rs_args |}) --o [{| y_loc |}]{| rule_name |}_commit({| rs_args |}). rule {| rule_name |}4a :: {| y_prop_heads |} \ [{| y_loc |}]{| rule_name |}_commit({| rs_args |}),{| y_simp_heads |} --o {| exists_code |} {| x_prop_heads |},{| rule_body |}. rule {| rule_name |}4b :: [{| y_loc |}]{| rule_name |}_commit({| rs_args |}) --o {| x_prop_heads |},{| x_simp_heads |}. ''') zero_nr_rules = compact( compile_template(zero_nr_code, match_rule=match_rule, rule_name=rule_dec.name, x_loc=x, y_loc=y, xs_args=xs, rs_args=rs ,x_guards=x_guards, y_guards=y_guards, x_simp_heads=x_simp_heads, y_simp_heads=y_simp_heads ,x_prop_heads=x_prop_heads, y_prop_heads=y_prop_heads, exists_code=exists_code, rule_body=rule_body ,xs_types=xs_types, rs_types=rs_types) ) # print zero_nr_rules msr_code_gen = MSRCodeGen("", bypass_checkers=True, input=zero_nr_rules) trans_decs = msr_code_gen.decs for dec in trans_decs: dec.add_trans_snippet( dec.gen_snippet( zero_nr_rules ) ) lead_rule_snippet = "One Neighbor Restriction translation applied.\n\nSource rule:\n%s\n\n" % rule_dec.gen_snippet( self.source_text ) lead_rule_snippet += "Generated fragment:\n%s\n\n" % zero_nr_rules lead_rule_snippet += trans_decs[0].gen_snippet( zero_nr_rules ) trans_decs[0].add_trans_snippet( lead_rule_snippet ) trans_decs[len(trans_decs)-2].where = rule_dec.where return trans_decs
def int_check(self, ast_node): heads = ast_node.slhs + ast_node.plhs inspect = Inspector() # Build the neighborhood free variables mapping of the rule heads (NFV mapping) locs = [] neighbor_fvs = {} for fact in map(inspect.get_fact, heads): loc = inspect.get_loc(fact) loc_key = loc.name if loc_key not in neighbor_fvs: neighbor_fvs[loc_key] = [] args = inspect.get_args(fact) atoms = inspect.get_atoms(args) locs.append(loc) neighbor_fvs[loc_key] += inspect.filter_atoms(atoms, var=True) # From NFV mapping, nominate the primary location all_locs = neighbor_fvs.keys() ast_node.primary_loc = None ast_node.primary_fv = None missing_vars = {} for loc in neighbor_fvs: fv = map(lambda t: t.name, neighbor_fvs[loc]) contains_all_locs = True for t_loc in all_locs: if (t_loc not in fv) and t_loc != loc: contains_all_locs = False if loc in missing_vars: missing_vars[loc].append(t_loc) else: missing_vars[loc] = [t_loc] if contains_all_locs: ast_node.primary_loc = loc ast_node.primary_fv = neighbor_fvs[loc] del neighbor_fvs[loc] break ast_node.neighbor_fvs = neighbor_fvs ast_node.neighbor_restriction = len(neighbor_fvs.keys()) if ast_node.primary_loc == None: legend = ("%s %s: Location variable(s).\n" % (terminal.T_GREEN_BACK, terminal.T_NORM)) + ( "%s %s: Argument variable.\n" % (terminal.T_RED_BACK, terminal.T_NORM)) for loc in missing_vars: legend += "Location %s has no links to %s.\n" % (loc, ','.join( missing_vars[loc])) error_report = "Rule %s is not neighbor restricted; None of location(s) %s can be primary location." % ( ast_node.name, ','.join(neighbor_fvs.keys())) error_idx = self.declare_error(error_report, legend) for loc in neighbor_fvs: map(lambda t: self.extend_error(error_idx, t), neighbor_fvs[loc]) map(lambda t: self.extend_info(error_idx, t), locs) return None # print "\n\n\n" # print ast_node.primary_loc # print ast_node.primary_fv # print ast_node.neighbor_fvs # print ast_node.neighbor_restriction primary_loc = ast_node.primary_loc primary_fv = ast_node.primary_fv primary_grds = [] neighbor_grds = {} # Identify guards that are grounded by primary location for g_term in ast_node.grd: g_vars = inspect.filter_atoms(inspect.get_atoms(g_term), var=True) excluded = not_contained(primary_fv, g_vars, key=lambda t: t.name) # print (ast_node.primary_loc,map(lambda t:t.name,excluded)) if len(excluded) == 0: primary_grds.append(g_term) else: for loc in neighbor_fvs: excluded = not_contained(primary_fv + neighbor_fvs[loc], g_vars, key=lambda t: t.name) if len(excluded) == 0: if loc in neighbor_grds: neighbor_grds[loc].append(g_term) else: neighbor_grds[loc] = [g_term] # TODO: Add neighbor restriction guard test ast_node.primary_grds = primary_grds ast_node.neighbor_grds = neighbor_grds
def one_neighbor_restrict_trans(self, rule_dec): inspect = Inspector() prop_heads = inspect.get_facts(rule_dec.plhs) simp_heads = inspect.get_facts(rule_dec.slhs) rule_name = rule_dec.name x = rule_dec.primary_loc y = rule_dec.neighbor_fvs.keys()[0] x_prop_heads = ','.join( map(lambda f: "[%s]%s" % (x, f.gen_snippet(self.source_text)), inspect.filter_facts(prop_heads, x))) y_prop_heads = ','.join( map(lambda f: "[%s]%s" % (y, f.gen_snippet(self.source_text)), inspect.filter_facts(prop_heads, y))) x_simp_heads = ','.join( map(lambda f: "[%s]%s" % (x, f.gen_snippet(self.source_text)), inspect.filter_facts(simp_heads, x))) y_simp_heads = ','.join( map(lambda f: "[%s]%s" % (y, f.gen_snippet(self.source_text)), inspect.filter_facts(simp_heads, y))) x_prop_heads = x_prop_heads if len(x_prop_heads) > 0 else "1" y_prop_heads = y_prop_heads if len(y_prop_heads) > 0 else "1" x_simp_heads = x_simp_heads if len(x_simp_heads) > 0 else "1" y_simp_heads = y_simp_heads if len(y_simp_heads) > 0 else "1" x_guards = ','.join( map(lambda f: f.gen_snippet(self.source_text), rule_dec.primary_grds)) if y in rule_dec.neighbor_grds: y_guards = ','.join( map(lambda f: f.gen_snippet(self.source_text), rule_dec.neighbor_grds[y])) else: y_guards = [] x_guards = ("| %s" % x_guards) if len(x_guards) > 0 else "" y_guards = ("| %s" % y_guards) if len(y_guards) > 0 else "" lxs = set(map(lambda t: t.name, rule_dec.primary_fv) + [x]) lrs = set( map(lambda t: t.name, rule_dec.primary_fv + rule_dec.neighbor_fvs[y]) + [x]) xs = ','.join(lxs) rs = ','.join(lrs) xs_types = ','.join(map(lambda _: "int", lxs)) rs_types = ','.join(map(lambda _: "int", lrs)) rule_body = ','.join( map(lambda f: f.gen_snippet(self.source_text), inspect.get_facts(rule_dec.rhs))) ''' print "Xs: %s" % xs print "Rs: %s" % rs print "X Guards: %s" % x_guards print "Y Guards: %s" % y_guards print "X Prop: %s" % x_prop_heads print "Y Prop: %s" % y_prop_heads print "X Simp: %s" % x_simp_heads print "Y Simp: %s" % y_simp_heads ''' if inspect.filter_facts(simp_heads, x) > 0: match_rule_code = template(''' {| y_prop_heads |},{| y_simp_heads |} \ [{| y_loc |}]{| rule_name |}_req({| xs_args |}) {| y_guards |} --o [{| x_loc |}]{| rule_name |}_match({| rs_args |}) priority 2. ''') else: match_rule_code = template(''' [{| y_loc |}]{| rule_name |}_req({| xs_args |}),{| y_prop_heads |},{| y_simp_heads |} \ 1 {| y_guards |} --o [{| x_loc |}]{| rule_name |}_match({| rs_args |}) priority 2. ''') match_rule = compile_template(match_rule_code, y_loc=y, xs_args=xs, rs_args=rs, y_prop_heads=y_prop_heads, y_simp_heads=y_simp_heads, x_loc=x, rule_name=rule_dec.name, y_guards=y_guards) # print match_rule exists_code = "" if len(rule_dec.exists) > 0: exists_code = "exists %s." % (','.join( map(lambda e: e.name, rule_dec.exists))) # sys.stdout.write("Here: %s" % exists_code) zero_nr_code = template(''' predicate {| rule_name |}_req :: ({| xs_types |}) -> fact. predicate {| rule_name |}_match :: ({| rs_types |}) -> fact. predicate {| rule_name |}_commit :: ({| rs_types |}) -> fact. rule {| rule_name |}1 :: {| x_prop_heads |},{| x_simp_heads |} \ 1 {| x_guards |} --o [{| y_loc |}]{| rule_name |}_req({| xs_args |}). rule {| rule_name |}2 :: {| match_rule |} rule {| rule_name |}3 :: {| x_prop_heads |},{| x_simp_heads |},[{| x_loc |}]{| rule_name |}_match({| rs_args |}) --o [{| y_loc |}]{| rule_name |}_commit({| rs_args |}). rule {| rule_name |}4a :: {| y_prop_heads |} \ [{| y_loc |}]{| rule_name |}_commit({| rs_args |}),{| y_simp_heads |} --o {| exists_code |} {| x_prop_heads |},{| rule_body |}. rule {| rule_name |}4b :: [{| y_loc |}]{| rule_name |}_commit({| rs_args |}) --o {| x_prop_heads |},{| x_simp_heads |}. ''') zero_nr_rules = compact( compile_template(zero_nr_code, match_rule=match_rule, rule_name=rule_dec.name, x_loc=x, y_loc=y, xs_args=xs, rs_args=rs, x_guards=x_guards, y_guards=y_guards, x_simp_heads=x_simp_heads, y_simp_heads=y_simp_heads, x_prop_heads=x_prop_heads, y_prop_heads=y_prop_heads, exists_code=exists_code, rule_body=rule_body, xs_types=xs_types, rs_types=rs_types)) # print zero_nr_rules msr_code_gen = MSRCodeGen("", bypass_checkers=True, input=zero_nr_rules) trans_decs = msr_code_gen.decs for dec in trans_decs: dec.add_trans_snippet(dec.gen_snippet(zero_nr_rules)) lead_rule_snippet = "One Neighbor Restriction translation applied.\n\nSource rule:\n%s\n\n" % rule_dec.gen_snippet( self.source_text) lead_rule_snippet += "Generated fragment:\n%s\n\n" % zero_nr_rules lead_rule_snippet += trans_decs[0].gen_snippet(zero_nr_rules) trans_decs[0].add_trans_snippet(lead_rule_snippet) trans_decs[len(trans_decs) - 2].where = rule_dec.where return trans_decs