Пример #1
0
    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 = topdown.TopDownTheory.new_bi_unifier()
        if unifier2 is None:
            # LOG.debug("Generating new unifier2")
            unifier2 = topdown.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.assertIsNotNone(changes)
        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)
Пример #2
0
    def bi_unify(self, head, unifier1, body_element, unifier2, theoryname):
        """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.
        THEORYNAME is the name of the theory for HEAD.
        """
        return unify.bi_unify_atoms(head, unifier1, body_element, unifier2,
                                    theoryname)
Пример #3
0
    def bi_unify(self, head, unifier1, body_element, unifier2, theoryname):
        """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.
        THEORYNAME is the name of the theory for HEAD.
        """
        return unify.bi_unify_atoms(head, unifier1, body_element, unifier2,
                                    theoryname)
Пример #4
0
    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)
Пример #5
0
 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)
Пример #6
0
 def check_unify_fail(self, atom_string1, atom_string2, msg):
     """Check that the bi-unification fails."""
     self.open(msg)
     unifier1 = topdown.TopDownTheory.new_bi_unifier()
     unifier2 = topdown.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)