public void IntTransitionCondition_TakenWhenMatching_NotTakenWhenNotMatching(string key, int targetValue, bool expectedEntered)
        {
            Graph graph = new Graph();

            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();

            Transition transition = new Transition(waitForManualExit: false);

            transition.AddTransitionCondition(new IntTransitionCondition(key, targetValue));
            NodeTransition nodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = transition
            };

            graph.AddOutgoingTransitionForNode(nodeA, nodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasIntParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetInt(Arg.Is("Key")).Returns(5);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool entered = false;

            nodeB.OnEnter += () => { entered = true; };

            graph.Start();
            Assert.AreEqual(expectedEntered, entered);
        }
Пример #2
0
        public void MultipleTransitions_HappenCorrectly()
        {
            Graph graph = new Graph();

            // A -> B -> C
            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();
            Node nodeC = graph.MakeNode();

            Transition bTransition = new Transition(waitForManualExit: false);

            bTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition bNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = bTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, bNodeTransition);

            Transition cTransition = new Transition(waitForManualExit: false);

            cTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition cNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeC.Id }, transition = cTransition
            };

            graph.AddOutgoingTransitionForNode(nodeB, cNodeTransition);

            // CLONING CODE
            graph = Graph.DeepClone(graph);

            nodeA = graph.LoadNodeById(nodeA.Id);
            nodeB = graph.LoadNodeById(nodeB.Id);
            nodeC = graph.LoadNodeById(nodeC.Id);
            // END CLONING CODE

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasIntParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetInt(Arg.Is("Key")).Returns(5);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool bEntered = false;

            nodeB.OnEnter += () => { bEntered = true; };
            bool cEntered = false;

            nodeC.OnEnter += () => { cEntered = true; };

            graph.Start();
            Assert.IsTrue(bEntered);
            Assert.IsTrue(cEntered);
        }
        public void IntTransitionCondition_TakesCorrectTransition()
        {
            Graph graph = new Graph();

            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();
            Node nodeC = graph.MakeNode();

            Transition bTransition = new Transition(waitForManualExit: false);

            bTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition bNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = bTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, bNodeTransition);

            Transition cTransition = new Transition(waitForManualExit: false);

            cTransition.AddTransitionCondition(new IntTransitionCondition("Key", 10));
            NodeTransition cNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeC.Id }, transition = cTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, cNodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasIntParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetInt(Arg.Is("Key")).Returns(10);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool bEntered = false;

            nodeB.OnEnter += () => { bEntered = true; };
            bool cEntered = false;

            nodeC.OnEnter += () => { cEntered = true; };

            graph.Start();
            Assert.IsFalse(bEntered);
            Assert.IsTrue(cEntered);
        }
Пример #4
0
        public void IntTransitionCondition_MatchesOtherTests_AfterSerializingAndDeserializing(string key, int targetValue, bool expectedEntered)
        {
            Graph graph = new Graph();

            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();

            Transition             transition = new Transition(waitForManualExit: false);
            IntTransitionCondition condition  = new IntTransitionCondition(key, targetValue);
            // NEW CODE
            string serialized = TransitionConditionSerializer.Serialize(condition);
            ITransitionCondition deserialized = TransitionConditionSerializer.Deserialize(serialized);

            transition.AddTransitionCondition(deserialized);
            // END NEW CODE

            NodeTransition nodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = transition
            };

            graph.AddOutgoingTransitionForNode(nodeA, nodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasIntParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetInt(Arg.Is("Key")).Returns(5);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool entered = false;

            nodeB.OnEnter += () => { entered = true; };

            graph.Start();
            Assert.AreEqual(expectedEntered, entered);
        }
Пример #5
0
        public void MoreComplexGraph_RunsCorrectly()
        {
            Graph graph = new Graph();

            // A --- B --
            //  \--- C --\- D
            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();
            Node nodeC = graph.MakeNode();
            Node nodeD = graph.MakeNode();

            Transition bTransition = new Transition(waitForManualExit: false);

            bTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition bNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = bTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, bNodeTransition);

            Transition cTransition = new Transition(waitForManualExit: false);

            cTransition.AddTransitionCondition(new IntTransitionCondition("Key", 3));
            NodeTransition cNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeC.Id }, transition = cTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, cNodeTransition);

            Transition cdTransition = new Transition(waitForManualExit: false);

            cdTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition cdNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeD.Id }, transition = cdTransition
            };

            graph.AddOutgoingTransitionForNode(nodeC, cdNodeTransition);

            Transition bdTransition = new Transition(waitForManualExit: true);

            bdTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition bdNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeD.Id }, transition = bdTransition
            };

            graph.AddOutgoingTransitionForNode(nodeB, bdNodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasIntParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetInt(Arg.Is("Key")).Returns(5);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool aEntered = false;

            nodeA.OnEnter += () => { aEntered = true; };
            bool bEntered = false;

            nodeB.OnEnter += () => { bEntered = true; };
            bool cEntered = false;

            nodeC.OnEnter += () => { cEntered = true; };
            bool dEntered = false;

            nodeD.OnEnter += () => { dEntered = true; };

            graph.Start();
            Assert.IsTrue(aEntered);
            Assert.IsTrue(bEntered);
            Assert.IsFalse(cEntered);
            Assert.IsFalse(dEntered);

            aEntered = false;
            bEntered = false;
            cEntered = false;
            dEntered = false;

            nodeB.TriggerManualExit();
            Assert.IsFalse(aEntered);
            Assert.IsFalse(bEntered);
            Assert.IsFalse(cEntered);
            Assert.IsTrue(dEntered);
        }
Пример #6
0
        public void MoreComplexGraph_RunsCorrectly()
        {
            Graph graph = new Graph();

            // A --- B --
            //  \--- C --\- D
            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();
            Node nodeC = graph.MakeNode();
            Node nodeD = graph.MakeNode();

            Transition bTransition = new Transition(waitForManualExit: false);

            bTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition bNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = bTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, bNodeTransition);

            Transition cTransition = new Transition(waitForManualExit: false);

            cTransition.AddTransitionCondition(new IntTransitionCondition("Key", 3));
            NodeTransition cNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeC.Id }, transition = cTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, cNodeTransition);

            Transition cdTransition = new Transition(waitForManualExit: false);

            cdTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition cdNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeD.Id }, transition = cdTransition
            };

            graph.AddOutgoingTransitionForNode(nodeC, cdNodeTransition);

            Transition bdTransition = new Transition(waitForManualExit: true);

            bdTransition.AddTransitionCondition(new IntTransitionCondition("Key", 5));
            NodeTransition bdNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeD.Id }, transition = bdTransition
            };

            graph.AddOutgoingTransitionForNode(nodeB, bdNodeTransition);

            // CLONING CODE
            Graph clonedGraph = Graph.DeepClone(graph);

            Node clonedNodeA = clonedGraph.LoadNodeById(nodeA.Id);
            Node clonedNodeB = clonedGraph.LoadNodeById(nodeB.Id);
            Node clonedNodeC = clonedGraph.LoadNodeById(nodeC.Id);
            Node clonedNodeD = clonedGraph.LoadNodeById(nodeD.Id);
            // END CLONING CODE

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasIntParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetInt(Arg.Is("Key")).Returns(5);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool aEntered = false;

            nodeA.OnEnter += () => { aEntered = true; };
            bool bEntered = false;

            nodeB.OnEnter += () => { bEntered = true; };

            bool clonedAEntered = false;

            clonedNodeA.OnEnter += () => { clonedAEntered = true; };
            bool clonedBEntered = false;

            clonedNodeB.OnEnter += () => { clonedBEntered = true; };
            bool clonedCEntered = false;

            clonedNodeC.OnEnter += () => { clonedCEntered = true; };
            bool clonedDEntered = false;

            clonedNodeD.OnEnter += () => { clonedDEntered = true; };

            clonedGraph.Start();
            Assert.IsTrue(clonedAEntered);
            Assert.IsTrue(clonedBEntered);
            Assert.IsFalse(clonedCEntered);
            Assert.IsFalse(clonedDEntered);

            // check to make sure the original nodes are not entered
            Assert.IsFalse(aEntered);
            Assert.IsFalse(bEntered);

            clonedAEntered = false;
            clonedBEntered = false;
            clonedCEntered = false;
            clonedDEntered = false;

            clonedNodeB.TriggerManualExit();
            Assert.IsFalse(clonedAEntered);
            Assert.IsFalse(clonedBEntered);
            Assert.IsFalse(clonedCEntered);
            Assert.IsTrue(clonedDEntered);
        }