Example #1
0
	def create(src_int_rep, src_preds, tgt_int_rep, tgt_preds):
		"""src_preds - [(pred, type)]
		   tgt_preds - [(pred, type)]"""

		self = PartialMap()
		# map from predicate name to predicate structure
		self.__src_preds = dict((p.get_name(), p) for p,t in src_preds)
		self.__src_pred_types = dict(src_preds)
		self.__tgt_preds = dict((p.get_name(), p) for p,t in tgt_preds)
		self.__tgt_pred_types = dict(tgt_preds)

		# maintain a special structure for legal rules
		src_move_effects = calc_move_effects(src_int_rep)
		tgt_move_effects = calc_move_effects(tgt_int_rep)
		
		self.__src_move_effects = {}
		self.__tgt_move_effects = {}
		for move, effects in src_move_effects.items():
			pred_effects = [self.__src_preds[p] for p in effects]
			self.__src_move_effects[move] = pred_effects
		for move, effects in tgt_move_effects.items():
			pred_effects = [self.__tgt_preds[p] for p in effects]
			self.__tgt_move_effects[move] = pred_effects

		# map all predicates that are identical in source and target onto each other
		common_pred_names = set(self.__src_preds.keys()).intersection(set(self.__tgt_preds.keys()))
		common_pred_map = dict((self.__src_preds[i], self.__tgt_preds[i]) for i in common_pred_names)

		self.__matched_preds = common_pred_map
		self.__matched_rules = {}
		# candidate rule matchings
		# (src_rule, tgt_rule) -> (body map, score)
		self.__cand_rule_matches = {}
		# special extra map for legal rules
		# (src_rule, tgt_rule) -> (effect map, score)
		self.__cand_legal_matches = {}

		# create all possible matches
		src_rules = [src_int_rep.get_legal_rules(), \
		             src_int_rep.get_update_rules(), \
		             src_int_rep.get_elab_rules(), \
		             src_int_rep.get_goal_rules(), \
		             src_int_rep.get_terminal_rules()]

		tgt_rules = [tgt_int_rep.get_legal_rules(), \
		             tgt_int_rep.get_update_rules(), \
		             tgt_int_rep.get_elab_rules(), \
		             tgt_int_rep.get_goal_rules(), \
		             tgt_int_rep.get_terminal_rules()]
		
		for i in range(5):
			# take all possible pairs of source, target rules
			for sr, tr in cross_product(src_rules[i], tgt_rules[i]):
				# don't include the head predicate in the body, because the head
				# will always matched to the head of the other rule
				srbody = [self.__src_preds[b.get_predicate()] for b in sr.get_body() \
						if b.get_predicate() != sr.get_head().get_predicate()]
				trbody = [self.__tgt_preds[b.get_predicate()] for b in tr.get_body() \
						if b.get_predicate() != tr.get_head().get_predicate()]
	
				# generators galore!
				maps = possible_matchings(srbody, trbody)
				maps_scores = ((bm, self.__body_map_score(sr, tr, bm)) for bm in maps)
				# if a body map score == 0, then that map is illegal
				self.__cand_rule_matches[(sr,tr)] = [map_score_pair for map_score_pair in maps_scores if map_score_pair[1] > 0]

				if i == 0:
					# for legal rules, make possible effect maps
					src_move = sr.get_head().get_predicate()
					tgt_move = tr.get_head().get_predicate()
					src_effects = self.__src_move_effects[src_move]
					tgt_effects = self.__tgt_move_effects[tgt_move]
					
					if len(src_effects) == 9 and len(tgt_effects) == 9:
						stop = True
					else:
						stop = False
					effect_maps = possible_matchings(src_effects, tgt_effects)
					maps_scores = ((m, self.__effect_map_score(m)) for m in effect_maps)
					self.__cand_legal_matches[(sr,tr)] = [map_score_pair for map_score_pair in maps_scores if map_score_pair[1] > 0]
		
		return self
Example #2
0
	def create(src_int_rep, src_preds, tgt_int_rep, tgt_preds):
		"""src_preds - [(pred, type)]
		   tgt_preds - [(pred, type)]"""

		self = PartialMap()

		# map from predicate name to predicate structure
		self.__src_preds = dict((p.get_name(), p) for p,t in src_preds)
		self.__src_pred_types = dict(src_preds)
		self.__tgt_preds = dict((p.get_name(), p) for p,t in tgt_preds)
		self.__tgt_pred_types = dict(tgt_preds)

		# calculate move effects
		self.__src_move_effects = dict((move, [self.__src_preds[n] for n in names]) for move, names in calc_move_effects(src_int_rep).items())
		self.__tgt_move_effects = dict((move, [self.__tgt_preds[n] for n in names]) for move, names in calc_move_effects(tgt_int_rep).items())

		self.__pred_match_scores = {}
		# map all predicates that are identical in source and target onto each other
		common_pred_names = set(self.__src_preds.keys()).intersection(set(self.__tgt_preds.keys()))
		common_pred_map = dict((self.__src_preds[i], self.__tgt_preds[i]) for i in common_pred_names)
		self.__matched_preds = {}
		for s, t in common_pred_map.items():
			self.__add_predicate_match(s, t)

		self.__matched_rules = {}
		# candidate rule matchings
		# (src_rule, tgt_rule) -> (body map, score)
		self.__cand_rule_matches = {}

		# create all possible matches
		src_rules = [src_int_rep.get_legal_rules(), \
		             src_int_rep.get_update_rules(), \
		             src_int_rep.get_elab_rules(), \
		             src_int_rep.get_goal_rules(), \
		             src_int_rep.get_terminal_rules()]

		tgt_rules = [tgt_int_rep.get_legal_rules(), \
		             tgt_int_rep.get_update_rules(), \
		             tgt_int_rep.get_elab_rules(), \
		             tgt_int_rep.get_goal_rules(), \
		             tgt_int_rep.get_terminal_rules()]
		
		for i in range(5):
			# take all possible pairs of source, target rules
			for sr, tr in cross_product(src_rules[i], tgt_rules[i]):
				body_maps = []
				# don't include the head predicate in the body, because the head
				# will always matched to the head of the other rule
				shp = sr.get_head().get_predicate()
				thp = tr.get_head().get_predicate()
				srpbody = [self.__src_preds[b.get_predicate()] for b in sr.get_body() if b.get_predicate() != shp and not b.is_negated()]
				srnbody = [self.__src_preds[b.get_predicate()] for b in sr.get_body() if b.get_predicate() != shp and b.is_negated()]
				trpbody = [self.__tgt_preds[b.get_predicate()] for b in tr.get_body() if b.get_predicate() != thp and not b.is_negated()]
				trnbody = [self.__tgt_preds[b.get_predicate()] for b in tr.get_body() if b.get_predicate() != thp and b.is_negated()]

				# generators galore!
				pmaps = possible_matchings(srpbody, trpbody)
				# since this is nested in the second for, we can't use a generator
				nmaps = [m for m in possible_matchings(srnbody, trnbody)]
				for pm in pmaps:
					for nm in nmaps:
						# merge the positive maps and negative maps
						total_map = pm.copy()
						total_map.update(nm)
						score = self.__body_map_score(sr, tr, total_map)
						# if a body map score == 0, then that map is illegal
						if score > 0:
							body_maps.append((total_map, score))

				if len(body_maps) > 0:
					self.__cand_rule_matches[(sr,tr)] = BestList(lambda x: x[1], body_maps)
		
		# the suppressed set is for temporarily preventing some rule matches
		# from being selected. useful when unrolling
		self.__suppressed = set()

		return self
Example #3
0
    def create(src_int_rep, src_preds, tgt_int_rep, tgt_preds):
        """src_preds - [(pred, type)]
		   tgt_preds - [(pred, type)]"""

        self = PartialMap()

        # map from predicate name to predicate structure
        self.__src_preds = dict((p.get_name(), p) for p, t in src_preds)
        self.__src_pred_types = dict(src_preds)
        self.__tgt_preds = dict((p.get_name(), p) for p, t in tgt_preds)
        self.__tgt_pred_types = dict(tgt_preds)

        # calculate move effects
        self.__src_move_effects = dict(
            (move, [self.__src_preds[n] for n in names])
            for move, names in calc_move_effects(src_int_rep).items())
        self.__tgt_move_effects = dict(
            (move, [self.__tgt_preds[n] for n in names])
            for move, names in calc_move_effects(tgt_int_rep).items())

        self.__pred_match_scores = {}
        # map all predicates that are identical in source and target onto each other
        common_pred_names = set(self.__src_preds.keys()).intersection(
            set(self.__tgt_preds.keys()))
        common_pred_map = dict((self.__src_preds[i], self.__tgt_preds[i])
                               for i in common_pred_names)
        self.__matched_preds = {}
        for s, t in common_pred_map.items():
            self.__add_predicate_match(s, t)

        self.__matched_rules = {}
        # candidate rule matchings
        # (src_rule, tgt_rule) -> (body map, score)
        self.__cand_rule_matches = {}

        # create all possible matches
        src_rules = [src_int_rep.get_legal_rules(), \
                     src_int_rep.get_update_rules(), \
                     src_int_rep.get_elab_rules(), \
                     src_int_rep.get_goal_rules(), \
                     src_int_rep.get_terminal_rules()]

        tgt_rules = [tgt_int_rep.get_legal_rules(), \
                     tgt_int_rep.get_update_rules(), \
                     tgt_int_rep.get_elab_rules(), \
                     tgt_int_rep.get_goal_rules(), \
                     tgt_int_rep.get_terminal_rules()]

        for i in range(5):
            # take all possible pairs of source, target rules
            for sr, tr in cross_product(src_rules[i], tgt_rules[i]):
                body_maps = []
                # don't include the head predicate in the body, because the head
                # will always matched to the head of the other rule
                shp = sr.get_head().get_predicate()
                thp = tr.get_head().get_predicate()
                srpbody = [
                    self.__src_preds[b.get_predicate()] for b in sr.get_body()
                    if b.get_predicate() != shp and not b.is_negated()
                ]
                srnbody = [
                    self.__src_preds[b.get_predicate()] for b in sr.get_body()
                    if b.get_predicate() != shp and b.is_negated()
                ]
                trpbody = [
                    self.__tgt_preds[b.get_predicate()] for b in tr.get_body()
                    if b.get_predicate() != thp and not b.is_negated()
                ]
                trnbody = [
                    self.__tgt_preds[b.get_predicate()] for b in tr.get_body()
                    if b.get_predicate() != thp and b.is_negated()
                ]

                # generators galore!
                pmaps = possible_matchings(srpbody, trpbody)
                # since this is nested in the second for, we can't use a generator
                nmaps = [m for m in possible_matchings(srnbody, trnbody)]
                for pm in pmaps:
                    for nm in nmaps:
                        # merge the positive maps and negative maps
                        total_map = pm.copy()
                        total_map.update(nm)
                        score = self.__body_map_score(sr, tr, total_map)
                        # if a body map score == 0, then that map is illegal
                        if score > 0:
                            body_maps.append((total_map, score))

                if len(body_maps) > 0:
                    self.__cand_rule_matches[(sr, tr)] = BestList(
                        lambda x: x[1], body_maps)

        # the suppressed set is for temporarily preventing some rule matches
        # from being selected. useful when unrolling
        self.__suppressed = set()

        return self