示例#1
0
class TestMajority(object):
    def setUp(self):
        self.blackboard = Blackboard()
        self.FAIL1 = Fail(name="FAIL1", blackboard=self.blackboard)
        self.FAIL2 = Fail(name="FAIL2", blackboard=self.blackboard)
        self.SUCCESS1 = Success(name="SUCCESS1", blackboard=self.blackboard)
        self.SUCCESS2 = Success(name="SUCCESS2", blackboard=self.blackboard)
        self.CONTINUE = Continue(name="CONTINUE", blackboard=self.blackboard)
        self.CONTINUE2 = Continue(name="CONTINUE2", blackboard=self.blackboard)

        self.MAJORITY = Majority("MAJORITY")
        self.MAJORITY.set_blackboard(self.blackboard)
        self.nd = self.blackboard.get_memory(self.MAJORITY._id)

    def test_success(self):
        self.MAJORITY.add_child(self.SUCCESS1)
        self.MAJORITY.add_child(self.SUCCESS2)

        result = self.MAJORITY.tick()
        assert_equal(result.status, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.PENDING)

    def test_fail(self):
        self.MAJORITY.add_child(self.FAIL1)
        self.MAJORITY.add_child(self.FAIL2)

        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)

        self.MAJORITY.add_child(self.SUCCESS1)
        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)

    def test_active(self):
        self.MAJORITY.add_child(self.CONTINUE)
        self.MAJORITY.add_child(self.SUCCESS1)
        self.MAJORITY.add_child(self.SUCCESS2)
        self.MAJORITY.add_child(self.FAIL1)
        self.MAJORITY.add_child(self.FAIL2)

        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)

    def test_cancel(self):
        self.MAJORITY.add_child(self.CONTINUE)
        self.MAJORITY.add_child(self.SUCCESS1)
        self.MAJORITY.add_child(self.SUCCESS2)
        self.MAJORITY.add_child(self.FAIL1)
        self.MAJORITY.add_child(self.FAIL2)

        self.MAJORITY.tick()
        self.MAJORITY._cancel()
        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)

    def test_force_child(self):
        self.MAJORITY.add_child(self.CONTINUE)
        self.MAJORITY.add_child(self.SUCCESS1)
        self.MAJORITY.add_child(self.SUCCESS2)
        self.MAJORITY.add_child(self.FAIL1)
        self.MAJORITY.add_child(self.FAIL2)

        self.MAJORITY.tick()
        self.CONTINUE.force(NodeStatus.SUCCESS)
        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)

        self.CONTINUE.force(NodeStatus.FAIL)
        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)

    def test_force_behavior(self):
        self.MAJORITY.add_child(self.CONTINUE)
        self.MAJORITY.add_child(self.SUCCESS1)
        self.MAJORITY.add_child(self.SUCCESS2)
        self.MAJORITY.add_child(self.FAIL1)
        self.MAJORITY.add_child(self.FAIL2)

        self.MAJORITY.tick()
        self.MAJORITY.force(NodeStatus.SUCCESS)
        result = self.MAJORITY.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)
示例#2
0
class TestSelector(object):
    def setUp(self):
        self.blackboard = Blackboard()
        self.FAIL1 = Fail(name="FAIL1", blackboard=self.blackboard)
        self.FAIL2 = Fail(name="FAIL2", blackboard=self.blackboard)
        self.SUCCESS1 = Success(name="SUCCESS1", blackboard=self.blackboard)
        self.SUCCESS2 = Success(name="SUCCESS2", blackboard=self.blackboard)
        self.CONTINUE = Continue(name="CONTINUE", blackboard=self.blackboard)

        self.SELECT = Selector(name="SELECT")
        self.SELECT.set_blackboard(self.blackboard)
        self.nd = self.blackboard.get_memory(self.SELECT._id)

    def test_fail(self):
        self.SELECT.add_child(self.FAIL1)
        self.SELECT.add_child(self.FAIL2)

        result = self.SELECT.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)

    def test_success(self):
        self.SELECT.add_child(self.FAIL1)
        self.SELECT.add_child(self.FAIL2)
        self.SELECT.add_child(self.SUCCESS1)
        self.SELECT.add_child(self.SUCCESS2)

        result = self.SELECT.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.PENDING)

    def test_active(self):
        self.SELECT.add_child(self.CONTINUE)
        self.SELECT.add_child(self.FAIL1)
        self.SELECT.add_child(self.FAIL2)
        self.SELECT.add_child(self.SUCCESS1)
        self.SELECT.add_child(self.SUCCESS2)

        result = self.SELECT.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.PENDING)

    def test_cancel(self):
        self.SELECT.add_child(self.CONTINUE)
        self.SELECT.add_child(self.FAIL1)
        self.SELECT.add_child(self.FAIL2)
        self.SELECT.add_child(self.SUCCESS1)
        self.SELECT.add_child(self.SUCCESS2)

        self.SELECT.tick()
        self.SELECT._cancel()
        result = self.SELECT.tick()
        assert_equal(result, NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.PENDING)

    def test_force_child(self):
        self.SELECT.add_child(self.CONTINUE)
        self.SELECT.add_child(self.FAIL1)
        self.SELECT.add_child(self.FAIL2)
        self.SELECT.add_child(self.SUCCESS1)
        self.SELECT.add_child(self.SUCCESS2)

        self.SELECT.tick()
        self.CONTINUE.force(NodeStatus.FAIL)
        result = self.SELECT.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.PENDING)
        self.blackboard.clear_node_status()

    def test_force_behavior(self):
        self.SELECT.add_child(self.CONTINUE)
        self.SELECT.add_child(self.FAIL1)
        self.SELECT.add_child(self.FAIL2)
        self.SELECT.add_child(self.SUCCESS1)
        self.SELECT.add_child(self.SUCCESS2)

        self.SELECT.tick()
        self.SELECT.force(NodeStatus.FAIL)
        result = self.SELECT.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.FAIL2._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS2._id),
                     NodeStatus.PENDING)
        self.blackboard.clear_node_status()
示例#3
0
class TestRandom(object):
    def setUp(self):
        self.blackboard = Blackboard()
        self.FAIL1 = Fail(name="FAIL1", blackboard=self.blackboard)
        self.FAIL2 = Fail(name="FAIL2", blackboard=self.blackboard)
        self.SUCCESS1 = Success(name="SUCCESS1", blackboard=self.blackboard)
        self.SUCCESS2 = Success(name="SUCCESS2", blackboard=self.blackboard)
        self.CONTINUE = Continue(name="CONTINUE", blackboard=self.blackboard)

        self.RANDOM = Random("RANDOM")
        self.RANDOM.set_blackboard(self.blackboard)
        self.nd = self.blackboard.get_memory(self.RANDOM._id)

    def test_empty(self):
        # if empty, configuration should succeed but set child to None
        self.RANDOM._configure()
        assert_equal(self.RANDOM.child, None)

        # if ticked, random should report success if no children
        result = self.RANDOM.tick()
        assert_equal(self.RANDOM.child, None)
        assert_equal(result, NodeStatus.SUCCESS)

    def test_success(self):
        self.RANDOM.add_child(self.SUCCESS1)
        self.RANDOM.add_child(self.FAIL1)
        self.RANDOM.add_child(self.CONTINUE)

        self.RANDOM._configure()
        assert_equal(self.RANDOM.child in self.RANDOM._children, True)
        while (self.RANDOM.child != self.SUCCESS1):
            self.RANDOM._configure()

        result = self.RANDOM.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.PENDING)

    def test_fail(self):
        self.RANDOM.add_child(self.SUCCESS1)
        self.RANDOM.add_child(self.FAIL1)
        self.RANDOM.add_child(self.CONTINUE)

        self.RANDOM._configure()
        assert_equal(self.RANDOM.child in self.RANDOM._children, True)
        while (self.RANDOM.child != self.FAIL1):
            self.RANDOM._configure()

        result = self.RANDOM.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.PENDING)

    def test_active(self):
        self.RANDOM.add_child(self.SUCCESS1)
        self.RANDOM.add_child(self.FAIL1)
        self.RANDOM.add_child(self.CONTINUE)

        self.RANDOM._configure()
        assert_equal(self.RANDOM.child in self.RANDOM._children, True)
        while (self.RANDOM.child != self.CONTINUE):
            self.RANDOM._configure()

        result = self.RANDOM.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.ACTIVE)

    def test_cancel(self):
        self.RANDOM.add_child(self.SUCCESS1)
        self.RANDOM.add_child(self.FAIL1)
        self.RANDOM.add_child(self.CONTINUE)

        self.RANDOM._configure()
        assert_equal(self.RANDOM.child in self.RANDOM._children, True)
        while (self.RANDOM.child != self.CONTINUE):
            self.RANDOM._configure()

        self.RANDOM.tick()
        self.RANDOM._cancel()
        result = self.RANDOM.tick()
        assert_equal(result, NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.FAIL1._id),
                     NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)

    def test_force_child(self):
        self.RANDOM.add_child(self.SUCCESS1)
        self.RANDOM.add_child(self.FAIL1)
        self.RANDOM.add_child(self.CONTINUE)

        self.RANDOM._configure()
        self.RANDOM.child.force(NodeStatus.SUCCESS)
        result = self.RANDOM.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.RANDOM.child._id),
                     NodeStatus.SUCCESS)

    def test_force_behavior(self):
        self.RANDOM.add_child(self.SUCCESS1)
        self.RANDOM.add_child(self.FAIL1)
        self.RANDOM.add_child(self.CONTINUE)

        self.RANDOM._configure()
        self.RANDOM.force(NodeStatus.FAIL)
        result = self.RANDOM.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.RANDOM.child._id),
                     NodeStatus.PENDING)
class TestRepeat(object):
    def setUp(self):
        self.blackboard = Blackboard()

        self.FAILURE = Failure(name="FAILURE", blackboard=self.blackboard)
        self.SUCCESS = Success(name="SUCCESS", blackboard=self.blackboard)
        self.CONTINUE = Continue(name="CONTINUE", blackboard=self.blackboard)

    def test_fail(self):
        REPEAT_FAIL = Repeat(name="REPEAT_FAILURE",
                             blackboard=self.blackboard,
                             child=self.FAILURE)
        result = REPEAT_FAIL.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(REPEAT_FAIL._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.FAILURE._id),
                     NodeStatus.FAIL)

    def test_success(self):
        REPEAT_SUCCESS = Repeat(name="REPEAT_SUCCESS",
                                blackboard=self.blackboard,
                                child=self.SUCCESS)
        result = REPEAT_SUCCESS.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(REPEAT_SUCCESS._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS._id),
                     NodeStatus.SUCCESS)

    def test_active(self):
        ACTIVE = Repeat(name="REPEAT_CONTINUE",
                        blackboard=self.blackboard,
                        child=self.CONTINUE)
        result = ACTIVE.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(ACTIVE._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.ACTIVE)

    def test_cancel(self):
        ACTIVE = Repeat(name="REPEAT_CONTINUE",
                        blackboard=self.blackboard,
                        child=self.CONTINUE)
        result = ACTIVE.tick()
        ACTIVE._cancel()
        assert_equal(ACTIVE.get_result(), NodeStatus.CANCEL)
        assert_equal(ACTIVE._child.get_result(), NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(ACTIVE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)
        result = ACTIVE.tick()
        assert_equal(result, NodeStatus.CANCEL)
        assert_equal(ACTIVE.get_result(), NodeStatus.PENDING)
        assert_equal(ACTIVE._child.get_result(), NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(ACTIVE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)

    def test_cancel_child(self):
        ACTIVE = Repeat(name="REPEAT_CONTINUE",
                        blackboard=self.blackboard,
                        child=self.CONTINUE)
        ACTIVE.tick()
        ACTIVE._child._cancel()
        assert_equal(ACTIVE.get_result(), NodeStatus.ACTIVE)
        assert_equal(ACTIVE._child.get_result(), NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(ACTIVE._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)
        result = ACTIVE.tick()
        assert_equal(result.status, NodeStatus.CANCEL)
        assert_equal(ACTIVE.get_result(), NodeStatus.PENDING)
        assert_equal(ACTIVE._child.get_result(), NodeStatus.PENDING)
        assert_equal(self.blackboard.get_node_status(ACTIVE._id),
                     NodeStatus.CANCEL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)

    def test_force_child(self):
        NOT_SUCCESS = Repeat(name="NOT_SUCCESS",
                             blackboard=self.blackboard,
                             child=self.SUCCESS)
        NOT_SUCCESS._child.force(NodeStatus.FAIL)
        result = NOT_SUCCESS.tick()
        assert_equal(result, NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(NOT_SUCCESS._id),
                     NodeStatus.ACTIVE)
        assert_equal(self.blackboard.get_node_status(self.SUCCESS._id),
                     NodeStatus.FAIL)

    def test_force_behavior(self):
        NOT_FAIL = Repeat(name="NOT_FAILURE",
                          blackboard=self.blackboard,
                          child=self.FAILURE)
        result = NOT_FAIL.tick()
        NOT_FAIL.force(NodeStatus.SUCCESS)
        result = NOT_FAIL.tick()
        assert_equal(result, NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(NOT_FAIL._id),
                     NodeStatus.SUCCESS)
        assert_equal(self.blackboard.get_node_status(self.FAILURE._id),
                     NodeStatus.FAIL)

        NOT_CONTINUE = Repeat(name="NOT_CONTINUE",
                              blackboard=self.blackboard,
                              child=self.CONTINUE)
        result = NOT_CONTINUE.tick()
        NOT_CONTINUE.force(NodeStatus.FAIL)
        result = NOT_CONTINUE.tick()
        assert_equal(result, NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(NOT_CONTINUE._id),
                     NodeStatus.FAIL)
        assert_equal(self.blackboard.get_node_status(self.CONTINUE._id),
                     NodeStatus.CANCEL)