Dispatcher.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.Linq;
37 using System.Reflection;
38 using System.Text;
39 using System.Text.RegularExpressions;
40 using System.Threading.Tasks;
41 using System.Threading;
44 
45 namespace Microsoft.Bot.Builder.Scorables
46 {
47  public interface IDispatcher
48  {
49  Task<bool> TryPostAsync(CancellationToken token);
50  }
51 
52  [Serializable]
53  public class Dispatcher : IDispatcher
54  {
55  protected virtual IReadOnlyList<object> MakeServices()
56  {
57  return new[] { this };
58  }
59 
60  protected virtual IResolver MakeResolver()
61  {
62  var services = this.MakeServices();
63  var resolver = NoneResolver.Instance;
64  resolver = new EnumResolver(resolver);
65  resolver = new ArrayResolver(resolver, services);
66  resolver = new ActivityResolver(resolver);
67  resolver = new EventActivityValueResolver(resolver);
68  resolver = new InvokeActivityValueResolver(resolver);
69 
70  return resolver;
71  }
72 
73  protected virtual ILuisService MakeService(ILuisModel model)
74  {
75  return new LuisService(model);
76  }
77 
78  protected virtual Regex MakeRegex(string pattern)
79  {
80  return new Regex(pattern);
81  }
82 
83  private bool continueAfterPost;
84 
85  protected void ContinueWithNextGroup()
86  {
87  continueAfterPost = true;
88  }
89 
90  protected virtual bool OnStage(FoldStage stage, IScorable<IResolver, object> scorable, IResolver item, object state, object score)
91  {
92  switch (stage)
93  {
94  case FoldStage.AfterFold: return true;
95  case FoldStage.StartPost: continueAfterPost = false; return true;
96  case FoldStage.AfterPost: return continueAfterPost;
97  default: throw new NotImplementedException();
98  }
99  }
100 
101  protected virtual IComparer<object> MakeComparer()
102  {
104  }
105 
107  {
108  var comparer = MakeComparer();
109 
111  (
112  this.OnStage,
113  comparer,
114  new LuisIntentScorableFactory(MakeService),
115  new RegexMatchScorableFactory(MakeRegex),
117  );
118 
119  return factory;
120  }
121 
122  protected virtual BindingFlags MakeBindingFlags()
123  {
124  return BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
125  }
126 
127  protected virtual Type MakeType()
128  {
129  return this.GetType();
130  }
131 
132  protected virtual IEnumerable<MethodInfo> MakeMethods()
133  {
134  var flags = this.MakeBindingFlags();
135  var type = this.MakeType();
136  var methods = type.GetMethods(flags);
137  return methods;
138  }
139 
141  {
142  var factory = MakeFactory();
143  var methods = MakeMethods();
144  var scorable = factory.ScorableFor(methods);
145  return scorable;
146  }
147 
148  protected virtual async Task OnPostAsync()
149  {
150  }
151 
152  protected virtual async Task OnFailAsync()
153  {
154  }
155 
156  async Task<bool> IDispatcher.TryPostAsync(CancellationToken token)
157  {
158  var scorable = MakeScorable();
159  var resolver = MakeResolver();
160 
161  if (await scorable.TryPostAsync(resolver, token))
162  {
163  await OnPostAsync();
164  return true;
165  }
166  else
167  {
168  await OnFailAsync();
169  return false;
170  }
171  }
172  }
173 }
virtual IScorableFactory< IResolver, object > MakeFactory()
Definition: Dispatcher.cs:106
A mockable interface for the LUIS service.
Definition: LuisService.cs:169
A null comparer that pretends every item is equal. This is particularly useful with stable sorts...
FoldStage
The stage of the FoldScorable events.
Definition: FoldScorable.cs:47
Allow the scoring of items, with external comparison of scores, and enable the winner to take some ac...
Definition: Scorable.cs:49
Allow the resolution of values based on type and optionally tag.
Namespace for internal scorable implementation that is not useful for most developers and may change ...
Task< bool > TryPostAsync(CancellationToken token)
Namespace for scorable interfaces, classes and compositions.
virtual IEnumerable< MethodInfo > MakeMethods()
Definition: Dispatcher.cs:132
virtual ILuisService MakeService(ILuisModel model)
Definition: Dispatcher.cs:73
Standard implementation of ILuisService against actual LUIS service.
Definition: LuisService.cs:191
virtual IComparer< object > MakeComparer()
Definition: Dispatcher.cs:101
A mockable interface for the LUIS model.
Definition: LuisModel.cs:53
virtual BindingFlags MakeBindingFlags()
Definition: Dispatcher.cs:122
A resolver to recover C# type information from Activity schema types.
virtual Regex MakeRegex(string pattern)
Definition: Dispatcher.cs:78
virtual IReadOnlyList< object > MakeServices()
Definition: Dispatcher.cs:55
virtual bool OnStage(FoldStage stage, IScorable< IResolver, object > scorable, IResolver item, object state, object score)
Definition: Dispatcher.cs:90
virtual IScorable< IResolver, object > MakeScorable()
Definition: Dispatcher.cs:140
Namespace for the machinery needed to talk to http://luis.ai.
Root namespace for the Microsoft Bot Builder SDK.