private CuratedExperienceComponent RemainingQuestions(CuratedExperience curatedExperience, Guid buttonId) { var allButtonsInCuratedExperience = curatedExperience.Components.Select(x => x.Buttons).ToList(); var currentButton = new Button(); foreach (var button in allButtonsInCuratedExperience) { if (button.Where(x => x.Id == buttonId).Any()) { currentButton = button.Where(x => x.Id == buttonId).First(); } } var destinationComponent = new CuratedExperienceComponent(); CuratedExperienceAnswers answers = new CuratedExperienceAnswers(); var currentComponent = curatedExperience.Components.Where(x => x.Buttons.Contains(currentButton)).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(currentButton.Destination)) { if (curatedExperience.Components.Where(x => x.Name == currentButton.Destination).Any()) { destinationComponent = curatedExperience.Components.Where(x => x.Name == currentButton.Destination).FirstOrDefault(); } } return(destinationComponent.ComponentId == default(Guid) ? null : destinationComponent); }
private List <string> ExtractAnswersLogicalStatements(CuratedExperienceAnswers curatedExperienceAnswers) { // in logical statements we have codeBefore and codeAfter, I consolidated them here but maybe // we should create a class with two properties of List<string> for each of these. We will check the need for this as // the implementation matures. List <string> statements = new List <string>(); foreach (ButtonComponent button in curatedExperienceAnswers.ButtonComponents) { if (!string.IsNullOrWhiteSpace(button.CodeBefore)) { statements.Add(button.CodeBefore); } if (!string.IsNullOrWhiteSpace(button.CodeAfter)) { statements.Add(button.CodeAfter); } } foreach (FieldComponent fieldComponent in curatedExperienceAnswers.FieldComponents) { if (!string.IsNullOrWhiteSpace(fieldComponent.CodeBefore)) { statements.Add(fieldComponent.CodeBefore); } if (!string.IsNullOrWhiteSpace(fieldComponent.CodeAfter)) { statements.Add(fieldComponent.CodeAfter); } } return(statements); }
public async Task <CuratedExperienceComponent> FindDestinationComponentAsync(CuratedExperience curatedExperience, Guid buttonId, Guid answersDocId) { var allButtonsInCuratedExperience = curatedExperience.Components.Select(x => x.Buttons).ToList(); var currentButton = new Button(); foreach (var button in allButtonsInCuratedExperience) { if (button.Where(x => x.Id == buttonId).Any()) { //currentComponent = button.Where(x => x.Id == buttonId) currentButton = button.Where(x => x.Id == buttonId).First(); } } var destinationComponent = new CuratedExperienceComponent(); CuratedExperienceAnswers answers = new CuratedExperienceAnswers(); var currentComponent = curatedExperience.Components.Where(x => x.Buttons.Contains(currentButton)).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(currentComponent.Code.CodeAfter) && currentComponent.Code.CodeAfter.Contains(Tokens.GOTO)) { answers = await dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId); // get the answers so far - done // get all the code in all the curated experience - to be done var currentComponentLogic = ExtractLogic(currentComponent, answers); if (currentComponentLogic != null) { var parsedCode = parser.Parse(currentComponentLogic); if (parsedCode.Any()) { destinationComponent = curatedExperience.Components.Where(x => x.Name.Contains(parsedCode.Values.FirstOrDefault())).FirstOrDefault(); } } } else if (!string.IsNullOrWhiteSpace(currentButton.Destination)) { if (curatedExperience.Components.Where(x => x.Name == currentButton.Destination).Any()) { destinationComponent = curatedExperience.Components.Where(x => x.Name == currentButton.Destination).FirstOrDefault(); } } if (!string.IsNullOrWhiteSpace(destinationComponent.Code.CodeBefore) && destinationComponent.Code.CodeBefore.Contains(Tokens.GOTO)) { if (answers.AnswersDocId == default(Guid)) { answers = await dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId); } var currentComponentLogic = ExtractLogic(destinationComponent, answers); if (currentComponentLogic != null) { var parsedCode = parser.Parse(currentComponentLogic); if (parsedCode.Any()) { destinationComponent = curatedExperience.Components.Where(x => x.Name.Contains(parsedCode.Values.LastOrDefault())).FirstOrDefault(); } } } return(destinationComponent.ComponentId == default(Guid) ? null : destinationComponent); }
public Dictionary <string, string> Parse(CuratedExperienceAnswers curatedExperienceAnswers) { var userAnswersKeyValuePairs = ExtractAnswersVarValues(curatedExperienceAnswers); var logicStatements = ExtractAnswersLogicalStatements(curatedExperienceAnswers); var evaluatedAnswers = new Dictionary <string, string>(); foreach (string logicalStatement in logicStatements) { foreach (var ifStatement in logicalStatement.SplitOnIFstatements()) { var leftVarValues = new Dictionary <string, string>(); var leftLogic = string.Empty; var rightLogic = string.Empty; if (ifStatement.Contains(Tokens.ParserConfig.SetVariables)) { leftLogic = ifStatement.GetStringLeftSide(Tokens.SET); rightLogic = ifStatement.GetStringRightSide(Tokens.SET); } else if (ifStatement.Contains(Tokens.ParserConfig.GoToQuestions)) { leftLogic = ifStatement.GetStringLeftSide(Tokens.GOTO); rightLogic = ifStatement.GetStringRightSide(Tokens.GOTO); } if (!string.IsNullOrWhiteSpace(leftLogic) && !string.IsNullOrWhiteSpace(rightLogic)) { var ANDvars = leftLogic.GetVariablesWithValues(Tokens.AND); if (interpreter.Interpret(userAnswersKeyValuePairs, ANDvars, (x, y) => x && y)) { evaluatedAnswers.AddDistinctRange(rightLogic.AddValue()); } var ORvars = leftLogic.GetVariablesWithValues(Tokens.OR); if (interpreter.Interpret(userAnswersKeyValuePairs, ORvars, (x, y) => x || y)) { evaluatedAnswers.AddDistinctRange(rightLogic.AddValue()); } if (ANDvars.Count == 0 && ORvars.Count == 0) { var oneVar = leftLogic.GetVariablesWithValues(); if (interpreter.Interpret(userAnswersKeyValuePairs, oneVar, (x, y) => x)) { evaluatedAnswers.AddDistinctRange(rightLogic.AddValue()); } } } } } return(evaluatedAnswers.AddDistinctRange(userAnswersKeyValuePairs)); }
private Dictionary <string, string> GetVarsValuesFromUserAnswers(CuratedExperienceAnswers userAnswers) { var varsDic = new Dictionary <string, string>(); foreach (var button in userAnswers.ButtonComponents) { varsDic.Add(button.Name, button.Value); } foreach (var fieldComponent in userAnswers.FieldComponents) { foreach (var field in fieldComponent.Fields) { varsDic.Add(field.Name, field.Value); } } return(varsDic); }
public async Task <CuratedExperienceComponentViewModel> GetComponent(CuratedExperience curatedExperience, Guid componentId) { var dbComponent = new CuratedExperienceComponent(); var answers = new CuratedExperienceAnswers(); if (componentId == default(Guid)) { answers.AnswersDocId = Guid.NewGuid(); answers.CuratedExperienceId = curatedExperience.CuratedExperienceId; await dbService.CreateItemAsync(answers, dbSettings.GuidedAssistantAnswersCollectionId); dbComponent = curatedExperience.Components.First(); } else { dbComponent = curatedExperience.Components.Where(x => x.ComponentId == componentId).FirstOrDefault(); } return(MapComponentViewModel(curatedExperience, dbComponent, answers.AnswersDocId)); }
private Dictionary <string, string> ExtractAnswersVarValues(CuratedExperienceAnswers curatedExperienceAnswers) { var userAnswers = new Dictionary <string, string>(); foreach (ButtonComponent button in curatedExperienceAnswers.ButtonComponents) { if (!string.IsNullOrWhiteSpace(button.Name) && !string.IsNullOrWhiteSpace(button.Value)) { if (!userAnswers.ContainsKey(button.Name)) { userAnswers.Add(button.Name, button.Value); } else { userAnswers[button.Name] = button.Value; } } } foreach (FieldComponent fieldComponent in curatedExperienceAnswers.FieldComponents) { foreach (AnswerField field in fieldComponent.Fields) { if (!string.IsNullOrWhiteSpace(field.Name) && !string.IsNullOrWhiteSpace(field.Value)) { if (!userAnswers.ContainsKey(field.Name)) { userAnswers.Add(field.Name, field.Value); } else { userAnswers[field.Name] = field.Value; } } } } return(userAnswers); }
private CuratedExperienceAnswers ExtractLogic(CuratedExperienceComponent component, CuratedExperienceAnswers answers) { // when dealing with finding next destination of the current component we need to remove all logic // except the specific GOTO statement that comes in the current component. That is why I'm setting all logic to string.Empty. foreach (var answer in answers.ButtonComponents) { answer.CodeBefore = string.Empty; answer.CodeAfter = string.Empty; } foreach (var answer in answers.FieldComponents) { answer.CodeBefore = string.Empty; answer.CodeAfter = string.Empty; } if (!string.IsNullOrWhiteSpace(component.Code.CodeBefore) || !string.IsNullOrWhiteSpace(component.Code.CodeAfter)) { var button = new ButtonComponent(); button.ButtonId = Guid.NewGuid(); button.Name = string.Empty; button.Value = string.Empty; button.CodeBefore = component.Code.CodeBefore; button.CodeAfter = component.Code.CodeAfter; answers.ButtonComponents.Add(button); } return(answers); }
public void SaveAnswersAsyncNoAnswersDocIdValidate(CuratedExperience curatedExperiencedata, dynamic expectedData, Guid answersDocId, CuratedExperienceAnswersViewModel viewModelAnswer, CuratedExperienceAnswers curatedExperienceAnswers, dynamic CuratedExperienceAnswersSchema) { //arrange var dbResponse = dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId); dbResponse.ReturnsForAnyArgs <CuratedExperienceAnswers>(curatedExperienceAnswers); Document updatedDocument = new Document(); JsonTextReader reader = new JsonTextReader(new StringReader(CuratedExperienceAnswersSchema)); updatedDocument.LoadFrom(reader); dbService.UpdateItemAsync <CuratedExperienceAnswersViewModel>( Arg.Any <string>(), Arg.Any <CuratedExperienceAnswersViewModel>(), Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument); dbService.CreateItemAsync <CuratedExperienceAnswers>( Arg.Any <CuratedExperienceAnswers>(), Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument); //act var response = curatedExperience.SaveAnswersAsync(viewModelAnswer, curatedExperiencedata); var actualResult = JsonConvert.SerializeObject(response.Result); //assert Assert.Contains(expectedData, actualResult); }
public void GetNextComponentAsyncValidate(CuratedExperience curatedExperiencedata, CuratedExperienceComponentViewModel expectedData, Guid answersDocId, CuratedExperienceAnswersViewModel component, CuratedExperienceAnswers curatedExperienceAnswers) { //arrange var dbResponse = dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId); dbResponse.ReturnsForAnyArgs <CuratedExperienceAnswers>(curatedExperienceAnswers); //act var response = curatedExperience.GetNextComponentAsync(curatedExperiencedata, component); var actualResult = JsonConvert.SerializeObject(response.Result); var expectedResult = JsonConvert.SerializeObject(expectedData); //assert Assert.Equal(expectedResult, actualResult); }
public UnprocessedPersonalizedPlan Build(JObject personalizedPlan, CuratedExperienceAnswers userAnswers) { var stepsInScope = new List <JToken>(); var evaluatedUserAnswers = parser.Parse(userAnswers); var a2jAnswers = MapStringsToA2JAuthorValues(evaluatedUserAnswers); var root = personalizedPlan .Properties() .GetArrayValue("rootNode") .FirstOrDefault(); foreach (var child in root.GetValueAsArray <JArray>("children")) { var states = child.GetArrayValue("state"); foreach (var state in states) { foreach (var answer in a2jAnswers) { if (answer.Key == state.GetValue("leftOperand") && answer.Value == state.GetValue("operator")) { stepsInScope.Add(child); } } } } var unprocessedPlan = new UnprocessedPersonalizedPlan(); unprocessedPlan.Id = Guid.NewGuid(); var unprocessedTopic = new UnprocessedTopic(); var cultureInfo = Thread.CurrentThread.CurrentCulture; var textInfo = cultureInfo.TextInfo; unprocessedTopic.Name = textInfo.ToTitleCase(personalizedPlan.Properties().GetValue("title")); foreach (var step in stepsInScope) { foreach (var childrenRoot in step.GetValueAsArray <JArray>("children")) { var unprocessedStep = new UnprocessedStep(); foreach (var child in childrenRoot.GetValueAsArray <JObject>("rootNode").GetValueAsArray <JArray>("children")) { var state = child.GetArrayValue("state").FirstOrDefault(); unprocessedStep.Id = Guid.NewGuid(); if (!string.IsNullOrWhiteSpace(state.GetValue("title"))) { unprocessedStep.Title = state.GetValue("title"); continue; } else { if (!string.IsNullOrWhiteSpace(state.GetValue("userContent"))) { unprocessedStep.Description = state.GetValue("userContent").ExtractIdsRemoveCustomA2JTags().SanitizedHtml; unprocessedStep.ResourceIds = state.GetValue("userContent").ExtractIdsRemoveCustomA2JTags().ResourceIds; } } unprocessedTopic.UnprocessedSteps.Add(unprocessedStep); } } } unprocessedPlan.UnprocessedTopics.Add(unprocessedTopic); return(unprocessedPlan); }
public void GeneratePersonalizedPlanAsyncValidate(CuratedExperience curatedExperience, CuratedExperienceAnswers expectedData, JObject personalizedPlan) { //arrange var a2jPersonalizedPlan = dynamicQueries.FindItemWhereAsync <JObject>(dbSettings.A2JAuthorDocsCollectionId, Constants.Id, curatedExperience.A2jPersonalizedPlanId.ToString()); a2jPersonalizedPlan.ReturnsForAnyArgs <JObject>(personalizedPlan); var dbResponse = dbService.GetItemAsync <dynamic>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId); var response = personalizedPlanBusinessLogic.GeneratePersonalizedPlanAsync(curatedExperience, answersDocId); //act var actualResult = JsonConvert.SerializeObject(response.Result); var expectedResult = JsonConvert.SerializeObject(expectedData); //assert Assert.Equal("null", actualResult); }