示例#1
0
        public string Answer(string userId, string utterance)
        {
            BotContext context = this.GetValidContext(userId);

            bool isSingleContact = this.IsSingleContact(ref utterance);

            if (context == null)
            {
                LUInfo luInfo = Understand(utterance, isSingleContact);
                if (luInfo.GetType() == typeof(TestLUInfo))
                {
                    context = InitTFContext(userId, (TestLUInfo)luInfo);
                }
                else if (luInfo.GetType() == typeof(ExamLUInfo))
                {
                    context = InitESContext(userId, (ExamLUInfo)luInfo);
                }
                else
                {
                    context = initPSAContext(userId, luInfo);
                }

                cManager.CreateContext(context, userId);
            }
            else
            {
                if (context.type == ContextType.TaskFlowContext && isSingleContact)
                {
                    TaskFlowContext tfContext = (TaskFlowContext)context;
                    context = updateTFContext(ref tfContext, utterance);
                }
                else if (context.type == ContextType.ExamSuitContext && isSingleContact)
                {
                    ExamSuitContext esContext = (ExamSuitContext)context;
                    context = updateESContext(ref esContext, utterance);
                }
                else
                {
                    LUInfo luInfo = Understand(utterance, isSingleContact);

                    if (context.type == ContextType.PSAContext)
                    {
                        PSAContext psacontext = (PSAContext)context;
                        context = updatePSAContext(ref psacontext, luInfo);
                    }
                    else
                    {
                        return("[Error]: Unknown Context Type.");
                    }
                }
            }

            string answer = null;

            switch (context.type)
            {
            case ContextType.PSAContext:
                ChatTableEngine engine     = new ChatTableEngine();
                PSAContext      psacontext = (PSAContext)context;

                answer = engine.Answer(userId, ref psacontext);

                cManager.UpdateContext(psacontext, userId);
                break;

            case ContextType.TaskFlowContext:
                TestEngine      engineT   = new TestEngine();
                TaskFlowContext tfContext = (TaskFlowContext)context;

                answer = engineT.Answer(userId, ref tfContext);

                if (tfContext.IsInTesting)
                {
                    cManager.UpdateContext(tfContext, userId);
                }
                else
                {
                    cManager.RemoveContext(userId);
                }
                break;

            case ContextType.ExamSuitContext:

                ExamSuitContext esContext = (ExamSuitContext)context;
                DICSExamSrv     taskSrv   = new DICSExamSrv();

                string userInput = esContext.UserInput;

                switch (esContext.status)
                {
                case ESStatus.Started:
                    ExamSuitContext cachedContext = this.GetCachedESContext(userId);
                    answer = taskSrv.StartTest(ref esContext, cachedContext);
                    break;

                case ESStatus.Restarted:
                    answer = taskSrv.ContinueOrRefresh(ref esContext, userInput);
                    break;

                case ESStatus.OnGoing:
                    answer = taskSrv.ReceiveUserAnswer(ref esContext, userInput);
                    break;
                }

                if (esContext.status == ESStatus.Finished || esContext.status == ESStatus.Aborded)
                {
                    cManager.RemoveContext(userId);
                }
                else if (esContext.status == ESStatus.Paused)
                {
                    cManager.StoreESContext(esContext, userId);
                    cManager.RemoveContext(userId);
                }
                else
                {
                    cManager.UpdateContext(esContext, userId);
                }

                break;
            }

            return(answer);
        }