Exemple #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));
        }
        private IEnumerable <QuestionWithAnswers> NavigateReadNodeLogic(JourneyStep[] answeredQuestions, IEnumerable <QuestionWithAnswers> journey, IDictionary <string, string> state)
        {
            var filteredJourney = new List <QuestionWithAnswers>();

            var groupledRead = journey.Where(s => s.Labels.Contains("Read")).GroupBy(s => s.Question.Id, s => s.Answered,
                                                                                     (key, g) => new { Node = key, Answers = g.ToList().Distinct() });

            var pathNavigationAnswers = new List <Answer>();

            foreach (var step in journey)
            {
                if (!step.Labels.Contains("Read"))
                {
                    if (!step.Labels.Contains("Question") || (step.Labels.Contains("Question") && answeredQuestions.Any(q => q.QuestionId == step.Question.Id)))
                    {
                        filteredJourney.Add(step);
                    }

                    if (step.Labels.Contains("Set") && !state.ContainsKey(step.Question.Title))
                    {
                        state.Add(step.Question.Title, "present");
                    }
                }
                else
                {
                    var answers = groupledRead.First(s => s.Node == step.Question.Id).Answers;
                    var value   = FindStateValue(state, step);
                    var answer  = _answersForNodeBuilder.SelectAnswer(answers, value);
                    if (answer != default(Answer) && step.Answered.Title == answer.Title)
                    {
                        filteredJourney.Add(step);
                        pathNavigationAnswers.Add(answer);
                    }
                }
            }

            return(filteredJourney);
        }
Exemple #3
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));
        }
        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));
        }