Example #1
0
        public void ShouldSucceed_WhenFirstChildSuccessful()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Selector sut          = new Selector(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);

            firstChild.Finish(true);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsTrue(behaviorTree.WasSuccess);
        }
Example #2
0
        public void ShouldSucceed_WhenStoppedExplicitlyButChildStillFinishesSuccessfully()
        {
            foreach (Parallel.Policy successPolicy in ALL_SUCCESS_POLICIES)
            {
                foreach (Parallel.Policy failurePolicy in ALL_FAILURE_POLICIES)
                {
                    MockNode succeedingChild = new MockNode(true);
                    Parallel sut             = new Parallel(successPolicy, failurePolicy, succeedingChild);
                    TestRoot behaviorTree    = CreateBehaviorTree(sut);

                    behaviorTree.Start();
                    Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

                    sut.Stop();

                    Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
                    Assert.IsTrue(behaviorTree.DidFinish);
                    Assert.True(behaviorTree.WasSuccess);
                }
            }
        }
Example #3
0
        public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldRestartFirstChild_WhenSecondChildFails()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode(false);
            Selector sut          = new Selector(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            sut.StopLowerPriorityChildrenForChild(firstChild, true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsFalse(behaviorTree.DidFinish);
        }
Example #4
0
        public void ShouldFail_WhenFirstChildFails()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Sequence sut          = new Sequence(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);

            firstChild.Finish(false);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
Example #5
0
        public void ShouldFail_WhenSingleChildFails()
        {
            foreach (Parallel.Policy sucessPolicy in ALL_SUCCESS_POLICIES)
            {
                foreach (Parallel.Policy failurePolicy in ALL_FAILURE_POLICIES)
                {
                    MockNode failingChild = new MockNode();
                    Parallel sut          = new Parallel(sucessPolicy, failurePolicy, failingChild);
                    TestRoot behaviorTree = CreateBehaviorTree(sut);

                    behaviorTree.Start();
                    Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

                    failingChild.Finish(false);

                    Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
                    Assert.IsTrue(behaviorTree.DidFinish);
                    Assert.IsFalse(behaviorTree.WasSuccess);
                }
            }
        }
        public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldThrowError()
        {
            Parallel.Policy failurePolicy = Parallel.Policy.ALL;
            Parallel.Policy successPolicy = Parallel.Policy.ALL;

            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Parallel sut          = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);
            bool bExceptionCought = false;

            try {
                sut.StopLowerPriorityChildrenForChild(firstChild, false);
            } catch (Exception) {
                bExceptionCought = true;
            }
            Assert.AreEqual(bExceptionCought, true);
        }
        public void ShouldListenToEvents_WhenUsingChildBlackboard()
        {
            Blackboard rootBlackboard = new Blackboard(clock);
            Blackboard blackboard     = new Blackboard(rootBlackboard, clock);

            // our mock nodes we want to query for status
            MockNode firstChild  = new MockNode(false); // false -> fail when aborted
            MockNode secondChild = new MockNode(false);

            // conditions for each subtree that listen the BB for events
            BlackboardCondition firstCondition  = new BlackboardCondition("branch1", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, firstChild);
            BlackboardCondition secondCondition = new BlackboardCondition("branch2", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, secondChild);

            // set up the tree
            Selector selector     = new Selector(firstCondition, secondCondition);
            TestRoot behaviorTree = new TestRoot(blackboard, clock, selector);

            // intially we want to activate branch2
            rootBlackboard.Set("branch2", true);

            // start the tree
            behaviorTree.Start();

            // tick the timer to ensure the blackboard notifies the nodes
            clock.Update(0.1f);

            // verify the second child is running
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            // change keys so the first conditions get true, too
            rootBlackboard.Set("branch1", true);

            // tick the timer to ensure the blackboard notifies the nodes
            clock.Update(0.1f);

            // now we should be in branch1
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
        }
Example #8
0
        public void ShouldSucceed_WhenDecorateeSucceededGivenTimes()
        {
            MockNode succeedingChild = new MockNode();
            Repeater sut             = new Repeater(3, succeedingChild);
            TestRoot behaviorTree    = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            for (int i = 0; i < 2; i++)
            {
                succeedingChild.Finish(true);
                Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
                Assert.IsFalse(behaviorTree.DidFinish);
                Timer.Update(0.01f);
            }

            succeedingChild.Finish(true);
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsTrue(behaviorTree.WasSuccess);
        }
Example #9
0
        public void ShouldSucceedButSetCooldownAfterDecorator_WhenDecorateeSucceeds_DueToParameter()
        {
            MockNode child        = new MockNode();
            Cooldown sut          = new Cooldown(1, true, false, false, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // wait 1.5 seconds
            behaviorTree.Clock.Update(1.5f);

            // make child suceed
            child.Finish(true);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // ensure the child has not been started ( due to cooldown )
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);

            // advance clock to be at 2.0 seconds
            behaviorTree.Clock.Update(0.5f);

            // ensure the child has not been started ( due to cooldown )
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);

            // advance clock to be at 3 seconds
            behaviorTree.Clock.Update(1.0f);

            // ensure the child has been started
            Assert.AreEqual(Node.State.ACTIVE, child.CurrentState);
        }
Example #10
0
        public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldCancelSecondChild()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Sequence sut          = new Sequence(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();

            firstChild.Finish(true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            sut.StopLowerPriorityChildrenForChild(firstChild, false);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
        public void ShouldNotThrowErrors_WhenObservingKeysThatDontExist()
        {
            TestRoot        behaviorTree = null;
            MockNode        child        = new MockNode();
            BlackboardQuery sut          = new BlackboardQuery(new string[] { "key1", "key2" }, Stops.IMMEDIATE_RESTART, () => {
                object o1 = behaviorTree.Blackboard.Get <float>("key1");
                object o2 = behaviorTree.Blackboard.Get <float>("key2");
                float f1  = (float)o1;
                float f2  = (float)o2;

                if ((f1 > 0.99) && (f2 < 5.99f))
                {
                    return(true);
                }
                return(false);
            }, child);

            behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);
        }
Example #12
0
        public void ShouldNotLeaveObserversRegistered_WhenInactive()
        {
            MockNode child        = new MockNode();
            Repeater sut          = new Repeater(-1, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            child.Finish(true);

            Assert.AreEqual(1, behaviorTree.Clock.NumTimers);
            Assert.AreEqual(0, behaviorTree.Clock.NumUpdateObservers);

            Timer.Update(0.01f);
            child.Finish(false);

            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);

            Assert.AreEqual(0, behaviorTree.Clock.NumTimers);
            Assert.AreEqual(0, behaviorTree.Clock.NumUpdateObservers);
        }
Example #13
0
        public void ShouldFailAndNotSetCooldown_WhenDecorateeFails_DueToParameter()
        {
            MockNode failingChild = new MockNode();
            Cooldown sut          = new Cooldown(1, false, true, failingChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // make child fail
            failingChild.Finish(false);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // ensure the child has been started again ( due to cooldown not active )
            Assert.AreEqual(Node.State.ACTIVE, failingChild.CurrentState);
        }
Example #14
0
        public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldRestartFirstChild_WhenSecondChildFails()
        {
            MockNode       firstChild   = new MockNode();
            MockNode       secondChild  = new MockNode(false);
            RandomSelector sut          = new RandomSelector(firstChild, secondChild);
            TestRoot       behaviorTree = CreateBehaviorTree(sut);

            // TODO: will we keep the priority or will we switch to the priority defined by the randomized children?
            RandomSelector.DebugSetSeed(2);

            behaviorTree.Start();
            firstChild.Finish(false);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            sut.StopLowerPriorityChildrenForChild(firstChild, true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsFalse(behaviorTree.DidFinish);
        }
Example #15
0
        public void ShouldNotActivateLowerPriorityBranchInCaseMultipleBranchesGetValid()
        {
            this.Timer      = new Clock();
            this.Blackboard = new Blackboard(Timer);

            // our mock nodes we want to query for status
            MockNode firstChild  = new MockNode(false); // false -> fail when aborted
            MockNode secondChild = new MockNode(false);
            MockNode thirdChild  = new MockNode(false);

            // coniditions for each subtree that listen the BB for events
            BlackboardCondition firstCondition  = new BlackboardCondition("branch1", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, firstChild);
            BlackboardCondition secondCondition = new BlackboardCondition("branch2", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, secondChild);
            BlackboardCondition thirdCondtion   = new BlackboardCondition("branch3", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, thirdChild);

            // set up the tree
            Selector selector     = new Selector(firstCondition, secondCondition, thirdCondtion);
            TestRoot behaviorTree = new TestRoot(Blackboard, Timer, selector);

            // intially we want to activate branch3
            Blackboard.Set("branch3", true);

            // start the tree
            behaviorTree.Start();

            // verify the third child is running
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, thirdChild.CurrentState);
            Assert.AreEqual(0, firstChild.DebugNumStartCalls);
            Assert.AreEqual(0, secondChild.DebugNumStartCalls);
            Assert.AreEqual(1, thirdChild.DebugNumStartCalls);

            // change keys so the first & second conditions get true, too
            Blackboard.Set("branch1", true);
            Blackboard.Set("branch2", true);

            // still the third child should be active, as the blackboard didn't yet notifiy the nodes
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, thirdChild.CurrentState);
            Assert.AreEqual(0, firstChild.DebugNumStartCalls);
            Assert.AreEqual(0, secondChild.DebugNumStartCalls);
            Assert.AreEqual(1, thirdChild.DebugNumStartCalls);

            // tick the timer to ensure the blackboard notifies the nodes
            Timer.Update(0.1f);

            // now we should be in branch1
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, thirdChild.CurrentState);
            Assert.AreEqual(1, firstChild.DebugNumStartCalls);
            Assert.AreEqual(0, secondChild.DebugNumStartCalls);
            Assert.AreEqual(1, thirdChild.DebugNumStartCalls);

            // disable first branch
            Blackboard.Set("branch1", false);
            Timer.Update(0.1f);

            // and now the second branch should be active
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, thirdChild.CurrentState);
            Assert.AreEqual(1, firstChild.DebugNumStartCalls);
            Assert.AreEqual(1, secondChild.DebugNumStartCalls);
            Assert.AreEqual(1, thirdChild.DebugNumStartCalls);
        }