Example #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)
Example #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()