Example #1
0
def map_kifs(src_rules, tgt_rules, src_pred_bins = {}, tgt_pred_bins={}):
	gdlyacc.parse_file(src_rules)
	src_int_rep = gdlyacc.int_rep.copy()
	gdlyacc.parse_file(tgt_rules)
	tgt_int_rep = gdlyacc.int_rep.copy()
	
	best_map = do_mapping(src_int_rep, tgt_int_rep, src_pred_bins, tgt_pred_bins)
	return best_map.get_pred_matches()
Example #2
0
def run_mapper(src_kif, tgt_kif, del_tmp=True):
    xkif_gen.parse_file(src_kif)
    tmp_fd, src_xkif = tempfile.mkstemp(suffix='.xkif')
    print >> sys.stderr, src_xkif
    xkif = os.fdopen(tmp_fd, 'w')
    xkif.write(xkif_gen.output)
    xkif.close()

    xkif_gen.parse_file(tgt_kif)
    tmp_fd, tgt_xkif = tempfile.mkstemp(suffix='.xkif')
    print >> sys.stderr, tgt_xkif
    xkif = os.fdopen(tmp_fd, 'w')
    xkif.write(xkif_gen.output)
    xkif.close()

    # find types
    gdlyacc.parse_file(src_kif)
    tmp_fd, src_types = tempfile.mkstemp()
    print >> sys.stderr, src_types
    os.fdopen(tmp_fd, 'w').write(
        os.popen('python infer_types.py %s' % src_kif).read())

    gdlyacc.parse_file(tgt_kif)
    tmp_fd, tgt_types = tempfile.mkstemp()
    print >> sys.stderr, tgt_types
    os.fdopen(tmp_fd, 'w').write(
        os.popen('python infer_types.py %s' % tgt_kif).read())

    mapping = {}
    output = os.popen(
        '%s %s %s %s %s' %
        (mapper, src_xkif, tgt_xkif, src_types, tgt_types)).readlines()
    for line in output:
        print line
        if line.startswith('MATCH'):
            tokens = line.split()
            assert len(tokens) == 3, line
            mapping[tokens[1]] = tokens[2]

    if del_tmp:
        os.remove(src_xkif)
        os.remove(tgt_xkif)
        os.remove(src_types)
        os.remove(tgt_types)

    return mapping
Example #3
0
def run_mapper(src_kif, tgt_kif, del_tmp = True):
	xkif_gen.parse_file(src_kif)
	tmp_fd, src_xkif = tempfile.mkstemp(suffix='.xkif')
	print >> sys.stderr, src_xkif
	xkif = os.fdopen(tmp_fd, 'w')
	xkif.write(xkif_gen.output)
	xkif.close()

	xkif_gen.parse_file(tgt_kif)
	tmp_fd, tgt_xkif = tempfile.mkstemp(suffix='.xkif')
	print >> sys.stderr, tgt_xkif
	xkif = os.fdopen(tmp_fd, 'w')
	xkif.write(xkif_gen.output)
	xkif.close()

	# find types
	gdlyacc.parse_file(src_kif)
	tmp_fd, src_types = tempfile.mkstemp()
	print >> sys.stderr, src_types
	os.fdopen(tmp_fd,'w').write(os.popen('python infer_types.py %s' % src_kif).read())

	gdlyacc.parse_file(tgt_kif)
	tmp_fd, tgt_types = tempfile.mkstemp()
	print >> sys.stderr, tgt_types
	os.fdopen(tmp_fd,'w').write(os.popen('python infer_types.py %s' % tgt_kif).read())

	mapping = {}
	output = os.popen('%s %s %s %s %s' % (mapper, src_xkif, tgt_xkif, src_types, tgt_types)).readlines()
	for line in output:
		print line
		if line.startswith('MATCH'):
			tokens = line.split()
			assert len(tokens) == 3, line
			mapping[tokens[1]] = tokens[2]
	
	if del_tmp:
		os.remove(src_xkif)
		os.remove(tgt_xkif)
		os.remove(src_types)
		os.remove(tgt_types)

	return mapping
Example #4
0
							if self.rule_grounded(er):
								existing = self.__g_elabs.setdefault(p,[])
								duplicate = False
								for oldr in existing:
									if same_body_preds(oldr, er):
										duplicate = True
										break
								if not duplicate:
									existing.append(er)
							else:
								rules.append(er)
						if len(rules) == 0:
							del self.__ug_elabs[p]
		
		# at this point, all elaboration rules are grounded. We should be able to
		# go through all the other kinds of ungrounded rules
		while len(self.__ug_rules) > 0:
			for r in self.__ug_rules[:]:
				expand_bi = self.can_expand(r)
				assert expand_bi >= 0, "Rule is impossible to expand"
				expanded = self.expand(r, expand_bi)
				self.__ug_rules.remove(r)
				self.__ug_rules.extend(expanded)

if __name__ == '__main__':
	gdlyacc.parse_file(sys.argv[1])
	ex = Expander(gdlyacc.int_rep)
	grounded = ex.collapse()
	for r in grounded:
		print r
Example #5
0
    pmap = position map """

    for src_p in pmap:
        tgt_p = pmap[src_p]
        src_c = src_p.fetch(src_g)
        tgt_c = tgt_p.fetch(tgt_g)
        assert src_c not in cmap or cmap[
            src_c] == tgt_c, "Constant mapping inconsistency"
        if src_c not in cmap:
            cmap[src_c] = tgt_c


if __name__ == '__main__':
    import psycocompile
    # get the mapping
    gdlyacc.parse_file(sys.argv[1])
    src_int_rep = gdlyacc.int_rep.copy()
    gdlyacc.parse_file(sys.argv[2])
    tgt_int_rep = gdlyacc.int_rep.copy()

    psyco.full()

    best_map = rule_mapper2.do_mapping(src_int_rep, tgt_int_rep)
    pred_map = dict((s.get_name(), t.get_name())
                    for s, t in best_map.get_pred_matches().items())

    #src_c2p = build_c2p(src_int_rep, pred_map)
    src_gnds = {}  # pred -> [grounds]
    for g in src_int_rep.get_statics() + src_int_rep.get_inits():
        src_gnds.setdefault(g.get_predicate(), []).append(g)