private async void NodePicker_Load(object sender, EventArgs e) { linkList = await type.GetAllLinks(); if (excludeChildId != null) { var node = await DbLinkCollection.FindOneById(excludeChildId); node?.CildrenRank.ForEach(child => linkList.Remove(linkList.FirstOrDefault(x => x._id == child.Key))); } InitializeData(linkList); }
private static async Task LoadNodes(this TreeNode treeNode, TreeView treeView) { TreeNode curTreeNode = null; var root = await DbLinkCollection.FindOneById(treeNode.Name); if (root != null) { foreach (var child in root.CildrenRank) { var node = await DbLinkCollection.FindOneById(child.Key); if (node != null) { bool isLoopNode = false; if (LoadedTreeNodes.Contains(node._id)) { isLoopNode = true; } else { LoadedTreeNodes.Add(node._id); } treeView.Invoke((MethodInvoker) delegate { // Running on the UI thread var nodeName = isLoopNode ? $"{node.Name} ({child.Value})(L)" : $"{node.Name} ({child.Value})"; curTreeNode = treeNode.Nodes.Add(node._id, nodeName); curTreeNode.ImageKey = node.GetNodeImage(); curTreeNode.Tag = node; }); if (!isLoopNode) { await curTreeNode.LoadNodes(treeView); } } } } }
public static async Task LoadTree(this TreeView treeView, string id = null) { LoadedTreeNodes.Clear(); treeView.Invoke((MethodInvoker) delegate { treeView.Nodes.Clear(); }); TreeNode treeNode = null; var root = await DbLinkCollection.FindOneById(rootNode);; if (root != null) { treeView.Invoke((MethodInvoker) delegate { treeView.Enabled = false; treeView.ImageList = LoadNeuralLinkValidationImageList(); // Running on the UI thread treeNode = treeView.Nodes.Add(root._id, root.Name); LoadedTreeNodes.Add(root._id); treeNode.Tag = root; treeNode.ImageKey = root.GetNodeImage(); treeView.SelectedNode = treeNode; }); await treeNode?.LoadNodes(treeView); treeView.Invoke((MethodInvoker) delegate { treeView.ExpandAll(); treeView.Enabled = true; if (id != null) { treeView.SelectedNode = treeView.SelectedNode.FindChildFromName(id); } treeView.Focus(); }); } }
private async void actionMenu_Clicked(object sender, ToolStripItemClickedEventArgs e) { var clicked = e.ClickedItem.AccessibilityObject.Name; { if (neuralTree.SelectedNode != null) { switch (clicked) { case MenuActionNew: { var newNode = await DbLinkCollection.InsertChildById(neuralTree.SelectedNode.Name, new NeuralLinkModel { Name = "New node" }); var newTreeNode = neuralTree.SelectedNode.Nodes.Add(newNode._id, newNode.Name); newTreeNode.Tag = newNode; neuralTree.SelectedNode = newTreeNode; ReloadTree(); } break; case MenuActionDelete: { if (DataProviders.ConfirmDialog($"Do you want to permanently delete node '{neuralTree.SelectedNode.Text}'?\n\n" + $"Warning: This will also deletes from all the references to this node in other parent nodes of this.\n\n" + $"Note: Refresh tree to view the updated changes.", "MenuActionDelete confirmation", MessageBoxIcon.Warning)) { var node = (NeuralLinkModel)neuralTree.SelectedNode.Tag; await DbLinkCollection.RemoveAndUnlinkFromParents(node); await DbTrainDataCollection.RemoveOneById(node._id); neuralTree.SelectedNode.Remove(); ReloadTree(); } } break; case MenuActionMapChild: { var selectedNode = (NeuralLinkModel)neuralTree.SelectedNode.Tag; var picker = new NodePicker(LinkType.NeuralLink, selectedNode._id, $"Map child to : {selectedNode.Name}"); var res = picker.ShowDialog(); if (res == DialogResult.OK && picker.NodeId != selectedNode._id) { var node = await DbLinkCollection.FindOneById(picker.NodeId); if (node != null) { await DbLinkCollection.LinkParentChild(selectedNode._id, picker.NodeId); var newTreeNode = neuralTree.SelectedNode.Nodes.Add(node._id, node.Name); newTreeNode.Tag = node; ReloadTree(); } } } break; case MenuActionUnmapChild: { if (neuralTree.SelectedNode.Parent == null) { await DbBotCollection.SetRootNodeById(BotAlphaName, null, cbxChatProfiles.Text); } else { await DbLinkCollection.UnLinkParentChild(neuralTree.SelectedNode.Parent.Name, neuralTree.SelectedNode.Name); neuralTree.SelectedNode.Remove(); } ReloadTree(); } break; default: break; } } switch (clicked) { case MenuActionRenameProfile: await RenameChatProfile(); break; case MenuActionDeleteProfile: await DeleteCurrentChatProfile(); break; case MenuActionNewProfile: await CreateNewChatProfile(); break; case MenuActionNew: { if (neuralTree.Nodes.Count == 0) { var rootNode = await DbLinkCollection.InsertNew(new NeuralLinkModel { Name = "RootNode" }); await DbBotCollection.SetRootNodeById(BotAlphaName, rootNode._id, await DataProviders.GetActiveProfile()); neuralTree.ImageList = DataProviders.LoadNeuralLinkValidationImageList(); var treeNode = neuralTree.Nodes.Add(rootNode._id, rootNode.Name); treeNode.Tag = rootNode; treeNode.ImageKey = rootNode.GetNodeImage(); neuralTree.SelectedNode = treeNode; ReloadTree(); } } break; default: break; } neuralTree.ExpandAll(); } }
private async Task <ResponseType> TakeAction(ITurnContext turnContext, RequestState requestState) { var ret = ResponseType.Continue; var text = turnContext.Activity.Text; var curLink = requestState.CurrentLink; switch (requestState.CurrentState) { case ChatStateType.Start: { await SendResponseForCurrentNode(turnContext, requestState); } break; case ChatStateType.AdvanceChat: { if (string.IsNullOrWhiteSpace(text)) { await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.InvalidInput), SuggestionExtension.GetCommonSuggestionActions()); } else { string nextNodeId = null; try { nextNodeId = requestState.PredictNode(text); var nextLink = await DbLinkCollection.FindOneById(nextNodeId); if (nextLink != null) { requestState.StepForward(nextLink); await SendResponseForCurrentNode(turnContext, requestState); } else { await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.NoMatchFound), SuggestionExtension.GetCommonSuggestionActions()); } } catch (Exception e) { await SendReply(turnContext, $"ML prediction returned an error: '{e.Message}'. please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions("Exit:exit")); } } } break; case ChatStateType.PickNode: { var nextLink = await DbLinkCollection.FindOneById(text); if (nextLink == null) { await turnContext.SendActivityAsync(StringsProvider.TryGet(BotResourceKeyConstants.InvalidInput)); await SendResponseForCurrentNode(turnContext, requestState); } else { curLink = nextLink; requestState.StepForward(curLink); await SendResponseForCurrentNode(turnContext, requestState); } } break; case ChatStateType.RecordFeedback: { switch (text.ToLower()) { case "yes": { var childLink = curLink; NeuralLinkModel parentLink = null; while (requestState.LinkHistory.TryPop(out parentLink)) { await DbLinkCollection.UpdateNeuralRankById(parentLink._id, childLink._id); childLink = parentLink; } ret = ResponseType.End; } break; case "no": ret = ResponseType.End; break; default: await turnContext.SendActivityAsync(StringsProvider.TryGet(BotResourceKeyConstants.InvalidInput)); await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions()); break; } if (ret == ResponseType.End) { await turnContext.SendActivityAsync(StringsProvider.TryGet(BotResourceKeyConstants.ThankYouFeedback)); } } break; case ChatStateType.InvalidInput: case ChatStateType.ExpInput: { await EvaluateExpressionInput(turnContext, requestState); } break; default: break; } return(ret); }
private async Task <bool> EvaluateExpressionInput(ITurnContext turnContext, RequestState requestState) { var res = false; var text = turnContext.Activity.Text; var curLink = requestState.CurrentLink; ActionLink actionResult; ExpEvalResultType op = curLink.NeuralExp.Next(text, out actionResult); switch (op) { case ExpEvalResultType.Skipped: case ExpEvalResultType.False: case ExpEvalResultType.True: { if (op == ExpEvalResultType.Skipped) { await SendNotes(curLink, turnContext); } switch (actionResult.Type) { case LinkType.NeuralLink: { var link = await DbLinkCollection.FindOneById(actionResult.LinkId); if (link != null) { curLink = link; requestState.StepForward(curLink); await SendResponseForCurrentNode(turnContext, requestState); } else { //Invalid expression evaluation link id. await SendReply(turnContext, $"Invalid node information : '{actionResult.LinkId}',please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions()); } } break; case LinkType.ActionLink: { var action = await DbActionCollection.FindOneById(actionResult.LinkId); await turnContext.SendActivityAsync(action.BuildActionRespose(turnContext)); requestState.CurrentState = ChatStateType.RecordFeedback; } break; case LinkType.NeuralResource: { var resource = await DbResourceCollection.FindOneById(actionResult.LinkId); await turnContext.SendActivityAsync(resource.BuildResourceResponse(turnContext)); requestState.CurrentState = ChatStateType.RecordFeedback; } break; default: break; } if (requestState.CurrentState == ChatStateType.RecordFeedback) { await Task.Delay(1000).ContinueWith(async ct => { await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions()); }); } res = true; } break; case ExpEvalResultType.Exception: case ExpEvalResultType.Invalid: { await SendReply(turnContext, "Invalid input, please try again!", SuggestionExtension.GetCommonSuggestionActions("Exit:exit")); requestState.CurrentState = ChatStateType.InvalidInput; } break; case ExpEvalResultType.Empty: //TODO { await SendReply(turnContext, $"Evaluated empty expression of node : '{curLink._id}',please report this issue to the bot administrator.", SuggestionExtension.GetCommonSuggestionActions()); } break; default: break; } return(res); }
private async Task <bool> EvaluateExpressionInput(ITurnContext turnContext, RequestState requestState) { var res = false; var text = turnContext.Activity.Text; var curLink = requestState.CurrentLink; ActionLink actionResult; ExpEvalResultType op = curLink.NeuralExp.Next(text, out actionResult); switch (op) { case ExpEvalResultType.Skipped: case ExpEvalResultType.False: case ExpEvalResultType.True: { switch (actionResult.Type) { case LinkType.NeuralLink: { var link = await DbLinkCollection.FindOneById(actionResult.LinkId); if (link != null) { curLink = link; requestState.StepForward(curLink); await SendResponseForCurrentNode(turnContext, requestState); } else { //Invalid expression evaluation link id. } } break; case LinkType.ActionLink: { var action = await DbActionCollection.FindOneById(actionResult.LinkId); await turnContext.SendActivityAsync(action.BuildActionRespose(turnContext)); await Task.Delay(1000).ContinueWith(async ct => { await SendReply(turnContext, StringsProvider.TryGet(BotResourceKeyConstants.Feedback), SuggestionExtension.GetFeedbackSuggestionActions()); }); requestState.CurrentState = ChatStateType.RecordFeedback; } break; case LinkType.NeuralResource: break; default: break; } } res = true; break; case ExpEvalResultType.Exception: case ExpEvalResultType.Invalid: { await SendReply(turnContext, "Invalid input, Try again!", SuggestionExtension.GetCommonSuggestionActions()); requestState.CurrentState = ChatStateType.InvalidInput; } break; case ExpEvalResultType.Empty: //TODO default: break; } return(res); }