DialogTaskManager.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections.Generic;
36 using System.Threading;
37 using System.Threading.Tasks;
38 
41 using Microsoft.Bot.Connector;
42 
43 namespace Microsoft.Bot.Builder.Dialogs.Internals
44 {
45  public interface IDialogTasks
46  {
50  IReadOnlyList<IDialogTask> DialogTasks { get; }
51 
55  IDialogTask CreateDialogTask();
56  }
57 
58  public interface IDialogTaskManager : IDialogTasks
59  {
60  // TODO: move these to separate interface, remove IDialogTaskManager (interface segregation principle)
61  // TODO: possibly share with IBotData LoadAsync and FlushAsync,
62 
67  Task LoadDialogTasks(CancellationToken token);
68 
73  Task FlushDialogTasks(CancellationToken token);
74  }
75 
79  public sealed class DialogTaskManager : IDialogTaskManager
80  {
81  private readonly string blobKeyPrefix;
82  private readonly IBotData botData;
83  private readonly IStackStoreFactory<DialogTask> stackStoreFactory;
84  private readonly Func<IDialogStack, CancellationToken, IDialogContext> contextFactory;
85  private readonly IEventProducer<IActivity> queue;
86 
87  private List<DialogTask> dialogTasks;
88 
89  public DialogTaskManager(string blobKeyPrefix, IBotData botData,
90  IStackStoreFactory<DialogTask> stackStoreFactory,
91  Func<IDialogStack, CancellationToken, IDialogContext> contextFactory,
93  {
94  SetField.NotNull(out this.blobKeyPrefix, nameof(blobKeyPrefix), blobKeyPrefix);
95  SetField.NotNull(out this.botData, nameof(botData), botData);
96  SetField.NotNull(out this.contextFactory, nameof(contextFactory), contextFactory);
97  SetField.NotNull(out this.stackStoreFactory, nameof(stackStoreFactory), stackStoreFactory);
98  SetField.NotNull(out this.queue, nameof(queue), queue);
99  }
100 
101  async Task IDialogTaskManager.LoadDialogTasks(CancellationToken token)
102  {
103  if (this.dialogTasks == null)
104  {
105  // load all dialog tasks. By default it loads/creates the default dialog task
106  // which will be used by ReactiveDialogTask
107  this.dialogTasks = new List<DialogTask>();
108  do
109  {
110  IDialogTaskManager dialogTaskManager = this;
111  dialogTaskManager.CreateDialogTask();
112  } while (
113  this.botData.PrivateConversationData.ContainsKey(this.GetCurrentTaskBlobKey(this.dialogTasks.Count)));
114  }
115  }
116 
117  async Task IDialogTaskManager.FlushDialogTasks(CancellationToken token)
118  {
119  foreach (var dialogTask in this.dialogTasks)
120  {
121  dialogTask.Store.Flush();
122  }
123  }
124 
125 
126  IReadOnlyList<IDialogTask> IDialogTasks.DialogTasks
127  {
128  get { return this.dialogTasks; }
129  }
130 
132  {
133  IDialogStack stack = default(IDialogStack);
134  Func<CancellationToken, IDialogContext> makeContext = token => contextFactory(stack, token);
135  var task = new DialogTask(makeContext, stackStoreFactory.StoreFrom(this.GetCurrentTaskBlobKey(this.dialogTasks.Count), botData.PrivateConversationData), this.queue);
136  stack = task;
137  dialogTasks.Add(task);
138  return task;
139  }
140 
141  private string GetCurrentTaskBlobKey(int idx)
142  {
143  return idx == 0 ? this.blobKeyPrefix : this.blobKeyPrefix + idx;
144  }
145  }
146 }
DialogTaskManager(string blobKeyPrefix, IBotData botData, IStackStoreFactory< DialogTask > stackStoreFactory, Func< IDialogStack, CancellationToken, IDialogContext > contextFactory, IEventProducer< IActivity > queue)
Namespace for the Microsoft Bot Connector SDK.
Namespace for internal machinery that is not useful for most developers and may change in the future...
This class is responsible for managing the set of dialog tasks.
IReadOnlyList< IDialogTask > DialogTasks
The list of IDialogTask
IDialogTask CreateDialogTask()
Creates a new IDialogTask and add it to DialogTasks
Task FlushDialogTasks(CancellationToken token)
Flushes the IDialogTask in IDialogTasks.DialogTasks
Task LoadDialogTasks(CancellationToken token)
Loads the IDialogTasks.DialogTasks from IBotDataBag.
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
The stack of dialogs in the conversational process.
Definition: IDialogTask.cs:46
Root namespace for the Microsoft Bot Builder SDK.