Actions.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 
36 using System;
37 using System.Collections.Generic;
38 using System.Linq;
39 using System.Threading;
40 using System.Threading.Tasks;
41 using System.Text.RegularExpressions;
45 
46 namespace Microsoft.Bot.Builder.Scorables
47 {
51  public static partial class Actions
52  {
53  private static IScorable<IResolver, IBinding> Bind(Delegate lambda)
54  {
55  return new DelegateScorable(lambda);
56  }
57 
58  public static IScorable<IResolver, IBinding> Bind<R>(Func<R> method)
59  {
60  return Bind((Delegate)method);
61  }
62 
63  public static IScorable<IResolver, IBinding> Bind<T1, R>(Func<T1, R> method)
64  {
65  return Bind((Delegate)method);
66  }
67 
68  public static IScorable<IResolver, IBinding> Bind<T1, T2, R>(Func<T1, T2, R> method)
69  {
70  return Bind((Delegate)method);
71  }
72 
73  public static IScorable<IResolver, IBinding> Bind<T1, T2, T3, R>(Func<T1, T2, T3, R> method)
74  {
75  return Bind((Delegate)method);
76  }
77 
78  public static IScorable<IResolver, IBinding> Bind<T1, T2, T3, T4, R>(Func<T1, T2, T3, T4, R> method)
79  {
80  return Bind((Delegate)method);
81  }
82 
83  public static IScorable<IResolver, IBinding> Bind<T1, T2, T3, T4, T5, R>(Func<T1, T2, T3, T4, T5, R> method)
84  {
85  return Bind((Delegate)method);
86  }
87 
88  public static IScorable<IResolver, IBinding> Bind<T1, T2, T3, T4, T5, T6, R>(Func<T1, T2, T3, T4, T5, T6, R> method)
89  {
90  return Bind((Delegate)method);
91  }
92 
93  public static IScorable<IResolver, IBinding> Bind<T1, T2, T3, T4, T5, T6, T7, R>(Func<T1, T2, T3, T4, T5, T6, T7, R> method)
94  {
95  return Bind((Delegate)method);
96  }
97 
98  [Serializable]
99  public sealed class WhereScorable<Score> : ScorableAggregator<IResolver, WhereScorable<Score>.Token, Score, IResolver, object, Score>
100  {
101  public sealed class Token : Token<IResolver, Score>
102  {
103  public bool HasScore { get; set; }
104  }
105 
106  private readonly IScorable<IResolver, Score> scorable;
107  private readonly Delegate lambda;
108  public WhereScorable(IScorable<IResolver, Score> scorable, Delegate lambda)
109  {
110  SetField.NotNull(out this.scorable, nameof(scorable), scorable);
111  SetField.NotNull(out this.lambda, nameof(lambda), lambda);
112  }
113 
114  protected override async Task<Token> PrepareAsync(IResolver innerItem, CancellationToken token)
115  {
116  var innerState = await this.scorable.PrepareAsync(innerItem, token);
117  bool hasScore = false;
118  if (this.scorable.HasScore(innerItem, innerState))
119  {
120  var innerScore = this.scorable.GetScore(innerItem, innerState);
121  var outerItem = new ArrayResolver(innerItem, innerScore);
122 
123  IBinding<bool> binding;
124  if (Binder.Instance.TryBind<bool>(lambda, outerItem, out binding))
125  {
126  hasScore = await binding.InvokeAsync(token);
127  }
128  }
129 
130  var state = new Token()
131  {
132  Item = innerItem,
133  Scorable = this.scorable,
134  State = innerState,
135  HasScore = hasScore
136  };
137 
138  return state;
139  }
140 
141  protected override bool HasScore(IResolver item, Token state)
142  {
143  return state.HasScore;
144  }
145 
146  protected override Score GetScore(IResolver item, Token state)
147  {
148  return state.Scorable.GetScore(state.Item, state.State);
149  }
150  }
151 
152  public static IScorable<IResolver, Score> Where<Score, T1>(this IScorable<IResolver, Score> scorable, Func<T1, bool> predicate)
153  {
154  return new WhereScorable<Score>(scorable, predicate);
155  }
156 
157  public static IScorable<IResolver, Score> Where<Score, T1>(this IScorable<IResolver, Score> scorable, Func<T1, Task<bool>> predicate)
158  {
159  return new WhereScorable<Score>(scorable, predicate);
160  }
161 
162  public static IScorable<IResolver, Score> Where<Score, T1, T2>(this IScorable<IResolver, Score> scorable, Func<T1, T2, bool> predicate)
163  {
164  return new WhereScorable<Score>(scorable, predicate);
165  }
166 
167  public static IScorable<IResolver, Score> Where<Score, T1, T2>(this IScorable<IResolver, Score> scorable, Func<T1, T2, Task<bool>> predicate)
168  {
169  return new WhereScorable<Score>(scorable, predicate);
170  }
171 
172  public static IScorable<IResolver, Score> Where<Score, T1, T2, T3>(this IScorable<IResolver, Score> scorable, Func<T1, T2, T3, bool> predicate)
173  {
174  return new WhereScorable<Score>(scorable, predicate);
175  }
176 
177  public static IScorable<IResolver, Score> Where<Score, T1, T2, T3>(this IScorable<IResolver, Score> scorable, Func<T1, T2, T3, Task<bool>> predicate)
178  {
179  return new WhereScorable<Score>(scorable, predicate);
180  }
181 
182  public static IScorable<IResolver, Match> When<InnerScore>(this IScorable<IResolver, InnerScore> scorable, Regex regex)
183  {
184  return new RegexMatchScorable<object, InnerScore>(regex, scorable);
185  }
186 
187  public static IScorable<IResolver, IntentRecommendation> When<InnerScore>(this IScorable<IResolver, InnerScore> scorable, ILuisModel model, LuisIntentAttribute intent, ILuisService service = null)
188  {
189  service = service ?? new LuisService(model);
190  return new LuisIntentScorable<object, InnerScore>(service, model, intent, scorable);
191  }
192 
194  {
195  return scorable.SelectScore((r, b) => 1.0);
196  }
197 
199  {
200  return scorable.SelectScore((r, m) => RegexMatchScorable.ScoreFor(m));
201  }
202 
204  {
205  return scorable.SelectScore((r, i) => i.Score.GetValueOrDefault());
206  }
207  }
208 }
A mockable interface for the LUIS service.
Definition: LuisService.cs:169
bool TryBind(MethodBase method, IResolver resolver, out IBinding binding)
Static helper methods for RegexMatchScorable.
WhereScorable(IScorable< IResolver, Score > scorable, Delegate lambda)
Definition: Actions.cs:108
Namespace for models generated from the http://luis.ai REST API.
Definition: FormDialog.cs:837
Allow the scoring of items, with external comparison of scores, and enable the winner to take some ac...
Definition: Scorable.cs:49
Aggregates some non-empty set of inner scorables to produce an outer scorable.
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 ...
Namespace for scorable interfaces, classes and compositions.
Namespace for internal machinery that is not useful for most developers and may change in the future...
Represents a binding of arguments to a method&#39;s parameters.
Definition: Binding.cs:47
override Score GetScore(IResolver item, Token state)
Definition: Actions.cs:146
static IScorable< IResolver, double > Normalize(this IScorable< IResolver, IntentRecommendation > scorable)
Definition: Actions.cs:203
Scorable to represent a specific LUIS intent recommendation.
Standard implementation of ILuisService against actual LUIS service.
Definition: LuisService.cs:191
A mockable interface for the LUIS model.
Definition: LuisModel.cs:53
override async Task< Token > PrepareAsync(IResolver innerItem, CancellationToken token)
Definition: Actions.cs:114
Associate a LUIS intent with a dialog method.
Definition: LuisDialog.cs:56
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
static IScorable< IResolver, double > Normalize(this IScorable< IResolver, Match > scorable)
Definition: Actions.cs:198
Core namespace for Dialogs and associated infrastructure.
override bool HasScore(IResolver item, Token state)
Definition: Actions.cs:141
Namespace for the machinery needed to talk to http://luis.ai.
static IScorable< IResolver, double > Normalize(this IScorable< IResolver, IBinding > scorable)
Definition: Actions.cs:193
Root namespace for the Microsoft Bot Builder SDK.
Fluent methods related to IScorable<IResolver, Score>.
Definition: Actions.cs:51