def create_unify(self, atom_string1, atom_string2, msg, change_num, unifier1=None, unifier2=None, recursive_str=False): """Create unification and check basic results.""" def str_uni(u): if recursive_str: return u.recur_str() else: return str(u) def print_unifiers(changes=None): LOG.debug("unifier1: {}".format(str_uni(unifier1))) LOG.debug("unifier2: {}".format(str_uni(unifier2))) if changes is not None: LOG.debug("changes: {}".format( ";".join([str(x) for x in changes]))) if msg is not None: self.open(msg) if unifier1 is None: # LOG.debug("Generating new unifier1") unifier1 = runtime.TopDownTheory.new_bi_unifier() if unifier2 is None: # LOG.debug("Generating new unifier2") unifier2 = runtime.TopDownTheory.new_bi_unifier() p1 = compile.parse(atom_string1)[0] p2 = compile.parse(atom_string2)[0] changes = unify.bi_unify_atoms(p1, unifier1, p2, unifier2) self.assertTrue(changes is not None) print_unifiers(changes) p1p = p1.plug(unifier1) p2p = p2.plug(unifier2) print_unifiers(changes) if not p1p == p2p: LOG.debug( "Failure: bi-unify({}, {}) produced {} and {}".format( str(p1), str(p2), str_uni(unifier1), str_uni(unifier2))) LOG.debug("plug({}, {}) = {}".format( str(p1), str_uni(unifier1), str(p1p))) LOG.debug("plug({}, {}) = {}".format( str(p2), str_uni(unifier2), str(p2p))) self.fail() if change_num is not None and len(changes) != change_num: LOG.debug( "Failure: bi-unify({}, {}) produced {} and {}".format( str(p1), str(p2), str_uni(unifier1), str_uni(unifier2))) LOG.debug("plug({}, {}) = {}".format( str(p1), str_uni(unifier1), str(p1p))) LOG.debug("plug({}, {}) = {}".format( str(p2), str_uni(unifier2), str(p2p))) LOG.debug("Expected {} changes; computed {} changes".format( change_num, len(changes))) self.fail() LOG.debug("unifier1: {}".format(str_uni(unifier1))) LOG.debug("unifier2: {}".format(str_uni(unifier2))) if msg is not None: self.open(msg) return (p1, unifier1, p2, unifier2, changes)
def bi_unify(self, head, unifier1, body_element, unifier2): """Unify atoms. Given something returned by self.head HEAD and an element in the return of self.body BODY_ELEMENT, modify UNIFIER1 and UNIFIER2 so that HEAD.plug(UNIFIER1) == BODY_ELEMENT.plug(UNIFIER2). Returns changes that can be undone via unify.undo-all. """ return unify.bi_unify_atoms(head, unifier1, body_element, unifier2)
def create_unify(self, atom_string1, atom_string2, msg, change_num, unifier1=None, unifier2=None, recursive_str=False): """Create unification and check basic results.""" def str_uni(u): if recursive_str: return u.recur_str() else: return str(u) def print_unifiers(changes=None): LOG.debug("unifier1: %s", str_uni(unifier1)) LOG.debug("unifier2: %s", str_uni(unifier2)) if changes is not None: LOG.debug("changes: %s", ";".join([str(x) for x in changes])) if msg is not None: self.open(msg) if unifier1 is None: # LOG.debug("Generating new unifier1") unifier1 = TopDownTheory.new_bi_unifier() if unifier2 is None: # LOG.debug("Generating new unifier2") unifier2 = TopDownTheory.new_bi_unifier() p1 = compile.parse(atom_string1)[0] p2 = compile.parse(atom_string2)[0] changes = unify.bi_unify_atoms(p1, unifier1, p2, unifier2) self.assertTrue(changes is not None) print_unifiers(changes) p1p = p1.plug(unifier1) p2p = p2.plug(unifier2) print_unifiers(changes) if not p1p == p2p: LOG.debug("Failure: bi-unify(%s, %s) produced %s and %s", p1, p2, str_uni(unifier1), str_uni(unifier2)) LOG.debug("plug(%s, %s) = %s", p1, str_uni(unifier1), p1p) LOG.debug("plug(%s, %s) = %s", p2, str_uni(unifier2), p2p) self.fail() if change_num is not None and len(changes) != change_num: LOG.debug("Failure: bi-unify(%s, %s) produced %s and %s", p1, p2, str_uni(unifier1), str_uni(unifier2)) LOG.debug("plug(%s, %s) = %s", p1, str_uni(unifier1), p1p) LOG.debug("plug(%s, %s) = %s", p2, str_uni(unifier2), p2p) LOG.debug("Expected %s changes; computed %s changes", change_num, len(changes)) self.fail() LOG.debug("unifier1: %s", str_uni(unifier1)) LOG.debug("unifier2: %s", str_uni(unifier2)) if msg is not None: self.open(msg) return (p1, unifier1, p2, unifier2, changes)
def check_unify_fail(self, atom_string1, atom_string2, msg): """Check that the bi-unification fails.""" self.open(msg) unifier1 = TopDownTheory.new_bi_unifier() unifier2 = TopDownTheory.new_bi_unifier() p1 = compile.parse(atom_string1)[0] p2 = compile.parse(atom_string2)[0] changes = unify.bi_unify_atoms(p1, unifier1, p2, unifier2) if changes is not None: LOG.debug("Failure failure: bi-unify(%s, %s) produced %s and %s", p1, p2, unifier1, unifier2) LOG.debug("plug(%s, %s) = %s", p1, unifier1, p1.plug(unifier1)) LOG.debug("plug(%s, %s) = %s", p2, unifier2, p2.plug(unifier2)) self.fail() self.close(msg)
def check_unify_fail(self, atom_string1, atom_string2, msg): """Check that the bi-unification fails.""" self.open(msg) unifier1 = runtime.TopDownTheory.new_bi_unifier() unifier2 = runtime.TopDownTheory.new_bi_unifier() p1 = compile.parse(atom_string1)[0] p2 = compile.parse(atom_string2)[0] changes = unify.bi_unify_atoms(p1, unifier1, p2, unifier2) if changes is not None: LOG.debug("Failure failure: bi-unify(%s, %s) produced %s and %s", p1, p2, unifier1, unifier2) LOG.debug("plug(%s, %s) = %s", p1, unifier1, p1.plug(unifier1)) LOG.debug("plug(%s, %s) = %s", p2, unifier2, p2.plug(unifier2)) self.fail() self.close(msg)