Example #1
0
        private void PrepareIf(int ix, StoryMessageHandler handler)
        {
            var runtime = StoryRuntime.New();

            handler.PushRuntime(runtime);
            var queue = handler.PeekRuntime().CommandQueue;

            foreach (IStoryCommand cmd in queue)
            {
                cmd.Reset();
            }
            queue.Clear();
            List <IStoryCommand> cmds = m_LoadedIfCommands[ix];

            for (int i = 0; i < cmds.Count; ++i)
            {
                IStoryCommand cmd = cmds[i];
                if (null != cmd.PrologueCommand)
                {
                    queue.Enqueue(cmd.PrologueCommand);
                }
                queue.Enqueue(cmd);
                if (null != cmd.EpilogueCommand)
                {
                    queue.Enqueue(cmd.EpilogueCommand);
                }
            }
        }
Example #2
0
        private void Prepare(StoryMessageHandler handler)
        {
            var runtime = StoryRuntime.New();

            handler.PushRuntime(runtime);
            var queue = runtime.CommandQueue;

            foreach (IStoryCommand cmd in queue)
            {
                cmd.Reset();
            }
            queue.Clear();
            for (int i = 0; i < m_LoadedCommands.Count; i++)
            {
                IStoryCommand cmd = m_LoadedCommands[i];
                if (null != cmd.PrologueCommand)
                {
                    queue.Enqueue(cmd.PrologueCommand);
                }
                queue.Enqueue(cmd);
                if (null != cmd.EpilogueCommand)
                {
                    queue.Enqueue(cmd.EpilogueCommand);
                }
            }
        }
        /// <summary>
        /// Find an appropriate intent handler and use it to process the incoming intent.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="lambdaContext"></param>
        /// <returns></returns>
        public override SkillResponse HandleRequest()
        {
            IntentRequest intentRequest = RequestContext.Request.Request as IntentRequest;

            Logger.Log("Request Intent: " + intentRequest.Intent.Name);

            IntentHandler handler = IntentHandlerFactory.CustomIntentHandlers.Find(x => x.IsHandlerForIntent(intentRequest.Intent));

            if (handler != null)
            {
                // Set the context for the process of handling the intent and then reset it afterwards to avoid dangling references.
                handler.RequestContext = RequestContext;
                SkillResponse response = handler.HandleIntent(intentRequest.Intent);
                handler.RequestContext = null;

                return(response);
            }
            else if (IntentNameFactory.IntentNames.Contains(intentRequest.Intent.Name))
            {
                // Otherwise we have a story intent
                StoryRuntime runtime = new StoryRuntime(RequestContext, Story.Load(Path.Combine(Directory.GetCurrentDirectory(), "Story.data")));
                return(runtime.ProcessRequest());
            }
            else
            {
                // Otherwise we have no way of dealing with this intent
                Logger.Log("No intent handler found");
                return(ResponseBuilder.Empty());
            }
        }
Example #4
0
        public void Constructor_SetsRequestContext_ToInputtedValue()
        {
            RequestContext requestContext = new RequestContext(null, null, null);
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext);

            Assert.AreSame(requestContext, storyRuntime.RequestContext);
        }
Example #5
0
        public void ProcessRequest_CurrentNodeKeyDoesExistInSessionAttributes_SetsNextNodeName()
        {
            Story      story       = new Story();
            SpeechNode speechNode  = story.CreateNode("Test");
            SpeechNode speechNode2 = story.CreateNode("Test2");

            speechNode.CreateTransition(speechNode2);

            Assert.IsNotNull(speechNode.GetNextNode());

            SkillRequest request = new SkillRequest();

            request.Request            = new IntentRequest();
            request.Session            = new Session();
            request.Session.Attributes = new Dictionary <string, object>()
            {
                { StoryRuntime.CurrentNodeKey, "Test" }
            };

            RequestContext requestContext = new RequestContext(request, null, null);
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext, story);

            storyRuntime.TrySetCurrentNode(0);

            Assert.IsTrue(request.Session.Attributes.ContainsKey(StoryRuntime.CurrentNodeKey));
            Assert.AreEqual("Test", request.Session.Attributes[StoryRuntime.CurrentNodeKey]);

            SkillResponse response = storyRuntime.ProcessRequest();

            Assert.IsNotNull(response.SessionAttributes);
            Assert.AreSame(request.Session.Attributes, response.SessionAttributes);
            Assert.IsTrue(response.SessionAttributes.ContainsKey(StoryRuntime.CurrentNodeKey));
            Assert.AreEqual("Test2", response.SessionAttributes[StoryRuntime.CurrentNodeKey]);
        }
        public void ConditionPasses_WithCurrentRuntimeSetToSameIntent_ReturnsTrue()
        {
            SkillRequest request = new SkillRequest();

            request.Request = new IntentRequest()
            {
                Intent = new Intent()
                {
                    Name = "Test"
                }
            };
            RequestContext requestContext = new RequestContext(request, null, null);

            StoryRuntime runtime = new StoryRuntime(requestContext);
            Story        story   = new Story();

            story.Runtime = runtime;
            SpeechNode      speechNode      = story.CreateNode("TestNode");
            Transition      transition      = speechNode.CreateTransition(new SpeechNode());
            IntentCondition intentCondition = transition.CreateCondition <IntentCondition>();

            intentCondition.IntentName = "Test";

            Assert.IsTrue(intentCondition.ConditionPasses());
        }
Example #7
0
        public void Constructor_InputtingStory_SetsStoryToInputtedValue()
        {
            RequestContext requestContext = new RequestContext(null, null, null);
            Story          story          = new Story();
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext, story);

            Assert.AreSame(story, storyRuntime.Story);
        }
        public override SkillResponse HandleIntent(Intent intent)
        {
            long         nodeIndex    = long.Parse(intent.Slots["NodeIndex"].Value);
            StoryRuntime storyRuntime = new StoryRuntime(RequestContext, Story.Load(Path.Combine(Directory.GetCurrentDirectory(), "Story.data")));

            storyRuntime.TrySetCurrentNode((int)nodeIndex);

            return(storyRuntime.ProcessRequest());
        }
Example #9
0
        public void Evaluate(StoryInstance instance, StoryMessageHandler handler, BoxedValue iterator, BoxedValueList args)
        {
            var stackInfo = StoryLocalInfo.New();
            var runtime   = StoryRuntime.New();

            //调用实参部分需要在栈建立之前运算,结果需要记录在栈上
            for (int i = 0; i < m_LoadedArgs.Count; ++i)
            {
                stackInfo.Args.Add(m_LoadedArgs[i].Clone());
            }
            foreach (var pair in m_LoadedOptArgs)
            {
                stackInfo.OptArgs.Add(pair.Key, pair.Value.Clone());
            }
            runtime.Arguments          = instance.NewBoxedValueList();
            runtime.Arguments.Capacity = stackInfo.Args.Count;
            for (int i = 0; i < stackInfo.Args.Count; i++)
            {
                stackInfo.Args[i].Evaluate(instance, handler, iterator, args);
                runtime.Arguments.Add(stackInfo.Args[i].Value);
            }
            runtime.Iterator = stackInfo.Args.Count;
            foreach (var pair in stackInfo.OptArgs)
            {
                pair.Value.Evaluate(instance, handler, iterator, args);
            }
            //实参处理完,进入函数体执行,创建新的栈
            PushStack(instance, handler, stackInfo, runtime);
            try {
                for (int i = 0; i < m_ArgNames.Count; ++i)
                {
                    if (i < stackInfo.Args.Count)
                    {
                        instance.SetVariable(m_ArgNames[i], stackInfo.Args[i].Value);
                    }
                    else
                    {
                        instance.SetVariable(m_ArgNames[i], BoxedValue.NullObject);
                    }
                }
                foreach (var pair in stackInfo.OptArgs)
                {
                    instance.SetVariable(pair.Key, pair.Value.Value);
                }
                Prepare(runtime);
                stackInfo.HaveValue = true;
                runtime.Tick(instance, handler, long.MaxValue);
                BoxedValue val;
                instance.TryGetVariable(m_ReturnName, out val);
                stackInfo.Value = val;
            } finally {
                instance.RecycleBoxedValueList(runtime.Arguments);
                PopStack(instance, handler);
            }
        }
Example #10
0
        public void Constructor_InputtingFilePath_LoadsStoryFromInputtedFilePath()
        {
            string path  = Path.Combine(Resources.TempDir, "Test.data");
            Story  story = new Story();

            story.Save(path);

            FileAssert.FileExists(path);

            RequestContext requestContext = new RequestContext(null, null, null);
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext, path);

            Assert.IsNotNull(storyRuntime.Story);
        }
Example #11
0
        public void TrySetCurrentNode_InputtingNonExistentNodeIndex_DoesNotChangeCurrentNode()
        {
            RequestContext requestContext = new RequestContext(null, null, null);
            Story          story          = new Story();
            SpeechNode     speechNode     = story.CreateNode("Test");

            speechNode.NodeIndex = 1;

            StoryRuntime storyRuntime = new StoryRuntime(requestContext, story);

            storyRuntime.TrySetCurrentNode(1);

            Assert.AreSame(speechNode, storyRuntime.CurrentNode);
            Assert.IsNull(story.FindNode(100));

            storyRuntime.TrySetCurrentNode(100);

            Assert.AreSame(speechNode, storyRuntime.CurrentNode);
        }
Example #12
0
 private void Prepare(StoryRuntime runtime)
 {
     if (null != m_InitialCommands)
     {
         for (int i = 0; i < m_InitialCommands.Count; ++i)
         {
             IStoryCommand cmd = m_InitialCommands[i].Clone();
             if (null != cmd.PrologueCommand)
             {
                 runtime.CommandQueue.Enqueue(cmd.PrologueCommand);
             }
             runtime.CommandQueue.Enqueue(cmd);
             if (null != cmd.EpilogueCommand)
             {
                 runtime.CommandQueue.Enqueue(cmd.EpilogueCommand);
             }
         }
     }
 }
Example #13
0
        public void TrySetCurrentNode_InputtingExistentNodeName_SetsCurrentNodeCorrectly()
        {
            RequestContext requestContext = new RequestContext(null, null, null);
            Story          story          = new Story();
            SpeechNode     speechNode     = story.CreateNode("Test");

            speechNode.NodeIndex = 1;
            SpeechNode speechNode2 = story.CreateNode("Test2");

            StoryRuntime storyRuntime = new StoryRuntime(requestContext, story);

            storyRuntime.TrySetCurrentNode(1);

            Assert.AreSame(speechNode, storyRuntime.CurrentNode);
            Assert.AreSame(speechNode2, story.FindNode("Test2"));

            storyRuntime.TrySetCurrentNode("Test2");

            Assert.AreSame(speechNode2, storyRuntime.CurrentNode);
        }
Example #14
0
        private void Prepare(StoryMessageHandler handler)
        {
            var runtime = StoryRuntime.New();

            handler.PushRuntime(runtime);
            if (null != m_InitialCommands)
            {
                for (int i = 0; i < m_InitialCommands.Count; ++i)
                {
                    IStoryCommand cmd = m_InitialCommands[i].Clone();
                    if (null != cmd.PrologueCommand)
                    {
                        runtime.CommandQueue.Enqueue(cmd.PrologueCommand);
                    }
                    runtime.CommandQueue.Enqueue(cmd);
                    if (null != cmd.EpilogueCommand)
                    {
                        runtime.CommandQueue.Enqueue(cmd.EpilogueCommand);
                    }
                }
            }
        }
Example #15
0
        public void ProcessRequest_UsesSessionAttributesIfTheyExist()
        {
            Story        story      = new Story();
            SpeechNode   speechNode = story.CreateNode("Test");
            SkillRequest request    = new SkillRequest();

            request.Request            = new IntentRequest();
            request.Session            = new Session();
            request.Session.Attributes = new Dictionary <string, object>();

            RequestContext requestContext = new RequestContext(request, null, null);
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext, story);

            storyRuntime.TrySetCurrentNode(0);

            Assert.IsNotNull(request.Session.Attributes);

            SkillResponse response = storyRuntime.ProcessRequest();

            Assert.IsNotNull(response.SessionAttributes);
            Assert.AreSame(request.Session.Attributes, response.SessionAttributes);
        }
Example #16
0
        public void ProcessRequest_CreatesSessionAttributesIfNoneExist()
        {
            Story        story      = new Story();
            SpeechNode   speechNode = story.CreateNode("Test");
            SkillRequest request    = new SkillRequest();

            request.Request            = new IntentRequest();
            request.Session            = new Session();
            request.Session.Attributes = null;

            RequestContext requestContext = new RequestContext(request, null, null);
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext, story);

            storyRuntime.TrySetCurrentNode(0);

            Assert.IsNull(request.Session.Attributes);

            SkillResponse response = storyRuntime.ProcessRequest();

            Assert.IsNotNull(response.SessionAttributes);
            AssertExt.IsNotEmpty(response.SessionAttributes);
        }
Example #17
0
        public void ProcessRequest_CreatesTellResponse_UsingCurrentNodeText()
        {
            Story      story      = new Story();
            SpeechNode speechNode = story.CreateNode("Test");

            speechNode.Text = "TestText";
            SkillRequest request = new SkillRequest();

            request.Request = new IntentRequest();
            request.Session = new Session();
            RequestContext requestContext = new RequestContext(request, null, null);
            StoryRuntime   storyRuntime   = new StoryRuntime(requestContext, story);

            storyRuntime.TrySetCurrentNode(0);

            SkillResponse response = storyRuntime.ProcessRequest();

            Assert.IsNotNull(response);
            Assert.AreEqual("SSML", response.Response.OutputSpeech.Type);
            Assert.AreEqual("<speak><s>TestText</s></speak>", (response.Response.OutputSpeech as SsmlOutputSpeech).Ssml);
            AssertExt.IsEmpty(response.Response.Directives);
            Assert.IsNull(response.Response.Reprompt);
        }
Example #18
0
 private void PushStack(StoryInstance instance, StoryMessageHandler handler, StoryLocalInfo info, StoryRuntime runtime)
 {
     handler.PushLocalInfo(info);
     handler.PushRuntime(runtime);
     instance.StackVariables = info.StackVariables;
 }
Example #19
0
        /// <summary>
        /// The behaviour when we want to start the game.
        /// Begins the story from the beginning.
        /// </summary>
        /// <param name="intent"></param>
        /// <param name="session"></param>
        /// <param name="lambdaContext"></param>
        /// <returns></returns>
        public override SkillResponse HandleIntent(Intent intent)
        {
            StoryRuntime storyRuntime = new StoryRuntime(RequestContext, Story.Load(Path.Combine(Directory.GetCurrentDirectory(), "Story.data")));

            return(storyRuntime.ProcessRequest());
        }