Пример #1
0
        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 TriggerTransitionCondition_NotTaken_WhenMissingStartingContextParameter(string key)
        {
            Graph graph = new Graph();

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

            Transition transition = new Transition(waitForManualExit: false);

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

            graph.AddOutgoingTransitionForNode(nodeA, nodeTransition);

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

            stubContext.HasTriggerParameterKey(Arg.Any <string>()).Returns(false);

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

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

            bool entered = false;

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

            graph.Start();
            Assert.IsFalse(entered);
        }
Пример #3
0
        public void Entering_TheSameNode_MultipleTimesWithCascade_Works()
        {
            Graph graph = new Graph();

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

            Transition     abTransition     = new Transition(waitForManualExit: false);
            NodeTransition abNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = abTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, abNodeTransition);

            Transition bcTransition = new Transition(waitForManualExit: false);

            bcTransition.AddTransitionCondition(new TriggerTransitionCondition("BGoToC"));
            NodeTransition bcNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeC.Id }, transition = bcTransition
            };

            graph.AddOutgoingTransitionForNode(nodeB, bcNodeTransition);

            Transition cbTransition = new Transition(waitForManualExit: false);

            cbTransition.AddTransitionCondition(new TriggerTransitionCondition("CGoToB"));
            NodeTransition cbNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = cbTransition
            };

            graph.AddOutgoingTransitionForNode(nodeC, cbNodeTransition);

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

            stubContext.HasTriggerParameterKey(Arg.Is("BGoToC")).Returns(true);
            stubContext.HasTriggerParameterKey(Arg.Is("CGoToB")).Returns(true);
            // returns true then false
            stubContext.HasTrigger(Arg.Is("BGoToC")).Returns(true, false);
            // returns true then false
            stubContext.HasTrigger(Arg.Is("CGoToB")).Returns(true, false);

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

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

            int bEnteredCount = 0;

            nodeB.OnEnter += () => { bEnteredCount++; };
            int cEnteredCount = 0;

            nodeC.OnEnter += () => { cEnteredCount++; };

            graph.Start();

            Assert.AreEqual(2, bEnteredCount);
            Assert.AreEqual(1, cEnteredCount);
        }
        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);
        }
Пример #5
0
 private void ResetContext()
 {
     if (this._context != null)
     {
         this._context.OnContextUpdated -= this.CheckActiveNodesTransitions;
     }
     this._context = GraphContextFactoryLocator.MakeContext();
     this._context.OnContextUpdated += this.CheckActiveNodesTransitions;
     this._context.PopulateStartingContextParameters(this._startingContextParameters);
 }
Пример #6
0
        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);
        }
Пример #7
0
        public void PopulateStartingContextParameters_CalledWithStartingParameters()
        {
            Graph graph = new Graph();
            GraphContextParameter startingParameter = new GraphContextParameter {
                type = GraphContextParameterType.Int, key = "Key"
            };

            graph.AddStartingContextParameter(startingParameter);

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

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

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

            graph.Start();

            mockContext.Received().PopulateStartingContextParameters(Arg.Is <IList <GraphContextParameter> >(list => list.Contains(startingParameter)));
        }
Пример #8
0
        public void BoolTransitionCondition_MatchesOtherTests_AfterSerializingAndDeserializing(string key, bool targetValue, bool expectedEntered)
        {
            Graph graph = new Graph();

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

            Transition transition = new Transition(waitForManualExit: false);

            BoolTransitionCondition condition = new BoolTransitionCondition(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.HasBoolParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.GetBool(Arg.Is("Key")).Returns(true);

            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);
        }
Пример #9
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);
        }
        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);
        }