Exemplo n.º 1
0
	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)
Exemplo n.º 2
0
	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
Exemplo n.º 3
0
    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