コード例 #1
0
        [Route("node/{pathwayId}/next_node/{nodeId}/answer/{answer}")] // TODO Changed
        public async Task <HttpResponseMessage> GetNextNode(string pathwayId, string nodeId, string answer, [FromUri] string state, string cacheKey = null)
        {
            #if !DEBUG
            cacheKey = cacheKey ?? string.Format("{0}-{1}-{2}-{3}", pathwayId, nodeId, answer, state);

            var cacheValue = await _cacheManager.Read(cacheKey);

            if (cacheValue != null)
            {
                return(cacheValue.AsHttpResponse());
            }
            #endif

            var next = JsonConvert.DeserializeObject <QuestionWithAnswers>(await(await _questionService.GetNextQuestion(nodeId, answer)).Content.ReadAsStringAsync());

            var stateDictionary = JsonConvert.DeserializeObject <IDictionary <string, string> >(HttpUtility.UrlDecode(state));
            var nextLabel       = next.Labels.FirstOrDefault();

            if (nextLabel == "Question" || nextLabel == "Outcome")
            {
                next.State = stateDictionary;
                var result = _questionTransformer.AsQuestionWithAnswers(JsonConvert.SerializeObject(next));

                #if !DEBUG
                _cacheManager.Set(cacheKey, result);
                #endif

                return(result.AsHttpResponse());
            }

            if (nextLabel == "Set")
            {
                stateDictionary.Add(next.Question.Title, next.Answers.First().Title);
                var updatedState = JsonConvert.SerializeObject(stateDictionary);
                return(await GetNextNode(pathwayId, next.Question.Id, next.Answers.First().Title, updatedState, cacheKey));
            }

            if (nextLabel == "Read")
            {
                var value    = stateDictionary.ContainsKey(next.Question.Title) ? stateDictionary[next.Question.Title] : null;
                var selected = _answersForNodeBuilder.SelectAnswer(next.Answers, value);

                return(await GetNextNode(pathwayId, next.Question.Id, selected, JsonConvert.SerializeObject(stateDictionary), cacheKey));
            }

            if (nextLabel == "CareAdvice")
            {
                stateDictionary.Add(next.Question.QuestionNo, "");
                var updatedState = JsonConvert.SerializeObject(stateDictionary);
                return(await GetNextNode(pathwayId, next.Question.Id, next.Answers.First().Title, updatedState, cacheKey));
            }

            if (nextLabel == "InlineDisposition")
            {
                return(await GetNextNode(pathwayId, next.Question.Id, next.Answers.First().Title, state, cacheKey));
            }

            throw new Exception(string.Format("Unrecognized node of type '{0}'.", nextLabel));
        }
コード例 #2
0
        public async Task <HttpResponseMessage> GetNextNode(string pathwayId, string nodeId, string state, [FromBody] string answer, string cacheKey = null)
        {
            #if !DEBUG
            cacheKey = cacheKey ?? string.Format("{0}-{1}-{2}-{3}", pathwayId, nodeId, answer, state);

            var cacheValue = await _cacheManager.Read(cacheKey);

            if (cacheValue != null)
            {
                return(cacheValue.AsHttpResponse());
            }
            #endif

            var next            = JsonConvert.DeserializeObject <QuestionWithAnswers>(await(await _questionService.GetNextQuestion(nodeId, answer)).Content.ReadAsStringAsync());
            var stateDictionary = JsonConvert.DeserializeObject <IDictionary <string, string> >(HttpUtility.UrlDecode(state));

            var nextLabel = next.Labels.FirstOrDefault();

            if (nextLabel == "Question" || nextLabel == "Outcome")
            {
                next.State = stateDictionary;
                var result = _questionTransformer.AsQuestionWithAnswers(JsonConvert.SerializeObject(next));

                #if !DEBUG
                _cacheManager.Set(cacheKey, result);
                #endif

                return(result.AsHttpResponse());
            }

            if (nextLabel == "DeadEndJump")
            {
                next.State = stateDictionary;
                var result = _questionTransformer.AsQuestionWithDeadEnd(JsonConvert.SerializeObject(next));
                return(result.AsHttpResponse());
            }

            if (nextLabel == "PathwaySelectionJump")
            {
                next.State = stateDictionary;
                var result = _questionTransformer.AsQuestionWithDeadEnd(JsonConvert.SerializeObject(next));
                return(result.AsHttpResponse());
            }

            if (nextLabel == "Set")
            {
                var answered = next.Answers.First();
                stateDictionary.Add(next.Question.Title, answered.Title);
                var updatedState        = JsonConvert.SerializeObject(stateDictionary);
                var httpResponseMessage = await GetNextNode(pathwayId, next.Question.Id, updatedState, answered.Title, cacheKey);

                var nextQuestion = JsonConvert.DeserializeObject <QuestionWithAnswers>(await httpResponseMessage.Content.ReadAsStringAsync());

                nextQuestion.NonQuestionKeywords        = answered.Keywords;
                nextQuestion.NonQuestionExcludeKeywords = answered.ExcludeKeywords;
                if (nextQuestion.Answers != null)
                {
                    foreach (var nextAnswer in nextQuestion.Answers)
                    {
                        nextAnswer.Keywords        += "|" + answered.Keywords;
                        nextAnswer.ExcludeKeywords += "|" + answered.ExcludeKeywords;
                    }
                }

                // have to add the node to the cache so thats its not missed when going back
                // to collect keywords
                var result = JsonConvert.SerializeObject(nextQuestion);

                #if !DEBUG
                _cacheManager.Set(cacheKey, result);
                    #endif

                return(result.AsHttpResponse());
            }

            if (nextLabel == "Read")
            {
                var value    = stateDictionary.ContainsKey(next.Question.Title) ? stateDictionary[next.Question.Title] : null;
                var selected = _answersForNodeBuilder.SelectAnswer(next.Answers, value);

                return(await GetNextNode(pathwayId, next.Question.Id, JsonConvert.SerializeObject(stateDictionary), selected, cacheKey));
            }

            if (nextLabel == "CareAdvice")
            {
                stateDictionary.Add(next.Question.QuestionNo, "");
                next.State = stateDictionary;
                //next.Answers.First().Keywords += "|" + answered.Keywords;
                //nextAnswer.ExcludeKeywords += "|" + answered.ExcludeKeywords;

                var result = _questionTransformer.AsQuestionWithAnswers(JsonConvert.SerializeObject(next));
                return(result.AsHttpResponse());
            }

            if (nextLabel == "InlineDisposition")
            {
                return(await GetNextNode(pathwayId, next.Question.Id, state, next.Answers.First().Title, cacheKey));
            }

            throw new Exception(string.Format("Unrecognized node of type '{0}'.", nextLabel));
        }
コード例 #3
0
        public async Task <JsonResult <QuestionWithAnswers> > GetNextNode(string pathwayId, string nodeLabel, string nodeId, string state, [FromBody] string answer, string cacheKey = null)
        {
            var question = await _questionService.GetNextQuestion(nodeId, nodeLabel, answer);

            var stateDictionary = JsonConvert.DeserializeObject <IDictionary <string, string> >(HttpUtility.UrlDecode(state));
            var nextLabel       = question.Labels.FirstOrDefault();

            if (nextLabel == "Question" || nextLabel == "Outcome")
            {
                question.State = stateDictionary;
                var result = _questionTransformer.AsQuestionWithAnswers(question);

                return(Json(result));
            }

            if (nextLabel == "DeadEndJump" || nextLabel == "Page" || nextLabel == "PathwaySelectionJump" || nextLabel == "NotFound")
            {
                question.State = stateDictionary;
                return(Json(question));
            }

            if (nextLabel == "Set" || nextLabel == "Read")
            {
                var computedAnswer = question.Answers.First();

                if (nextLabel == "Read")
                {
                    var value = stateDictionary.ContainsKey(question.Question.Title)
                        ? stateDictionary[question.Question.Title]
                        : null;
                    computedAnswer = _answersForNodeBuilder.SelectAnswer(question.Answers, value);
                }
                else
                {
                    if (!stateDictionary.ContainsKey(question.Question.Title))
                    {
                        stateDictionary.Add(question.Question.Title, computedAnswer.Title);
                    }
                }

                var updatedState = JsonConvert.SerializeObject(stateDictionary);
                var nextQuestion = (await GetNextNode(pathwayId, nextLabel, question.Question.Id, updatedState, computedAnswer.Title, cacheKey)).Content;

                nextQuestion.NonQuestionKeywords        = computedAnswer.Keywords;
                nextQuestion.NonQuestionExcludeKeywords = computedAnswer.ExcludeKeywords;

                if (nextQuestion.Answers != null)
                {
                    foreach (var nextAnswer in nextQuestion.Answers)
                    {
                        nextAnswer.Keywords        += "|" + computedAnswer.Keywords;
                        nextAnswer.ExcludeKeywords += "|" + computedAnswer.ExcludeKeywords;
                    }
                }

                return(Json(nextQuestion));
            }

            if (nextLabel == "CareAdvice")
            {
                stateDictionary.Add(question.Question.QuestionNo, "");
                question.State = stateDictionary;

                return(Json(question));
            }

            if (nextLabel == "InlineDisposition")
            {
                return(await GetNextNode(pathwayId, question.Question.Id, state, question.Answers.First().Title, cacheKey));
            }

            throw new Exception(string.Format("Unrecognized node of type '{0}'.", nextLabel));
        }