OrderScorable.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.Concurrent;
38 using System.Collections.Generic;
39 using System.Linq;
40 using System.Reflection;
41 using System.Text;
42 using System.Threading;
43 using System.Threading.Tasks;
44 
45 namespace Microsoft.Bot.Builder.Scorables
46 {
56  [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
57  public class ScorableGroupAttribute : Attribute
58  {
59  private readonly int order;
60 
64  public int Order => this.order;
65 
70  public ScorableGroupAttribute(int order)
71  {
72  this.order = order;
73  }
74  }
75 }
76 
77 namespace Microsoft.Bot.Builder.Scorables.Internals
78 {
79  // http://blog.ploeh.dk/2014/06/13/passive-attributes/
80  public interface IScorableFactory<in Item, out Score>
81  {
82  IScorable<Item, Score> ScorableFor(IEnumerable<MethodInfo> methods);
83  }
84 
85  public sealed class OrderScorableFactory<Item, Score> : IScorableFactory<Item, Score>
86  {
87  private readonly FoldScorable<Item, Score>.OnStageDelegate onStage;
88  private readonly IComparer<Score> comparer;
89  private readonly IEnumerable<IScorableFactory<Item, Score>> factories;
90 
91  public OrderScorableFactory(FoldScorable<Item, Score>.OnStageDelegate onStage, IComparer<Score> comparer, IEnumerable<IScorableFactory<Item, Score>> factories)
92  {
93  SetField.NotNull(out this.onStage, nameof(onStage), onStage);
94  SetField.NotNull(out this.comparer, nameof(comparer), comparer);
95  SetField.NotNull(out this.factories, nameof(factories), factories);
96  }
97 
98  public OrderScorableFactory(FoldScorable<Item, Score>.OnStageDelegate onStage, IComparer<Score> comparer, params IScorableFactory<Item, Score>[] factories)
99  : this(onStage, comparer, (IEnumerable<IScorableFactory<Item, Score>>)factories)
100  {
101  }
102 
103  IScorable<Item, Score> IScorableFactory<Item, Score>.ScorableFor(IEnumerable<MethodInfo> methods)
104  {
105  var levels = from method in methods
106  // note, this is non-deterministic across executions, which seems lame
107  let defaultOrder = method.Name.GetHashCode()
108  let attributes = InheritedAttributes.For<ScorableGroupAttribute>(method)
109  let orders = attributes.Select(order => order.Order).DefaultIfEmpty(defaultOrder)
110  from order in orders
111  group method by order into g
112  orderby g.Key
113  select g;
114 
115  var scorables = from level in levels
116  from factory in this.factories
117  let scorable = factory.ScorableFor(level)
118  where Scorable.Keep(scorable)
119  select scorable;
120 
121  var winner = scorables.ToArray().Fold(this.comparer, this.onStage);
122  return winner;
123  }
124  }
125 }
delegate bool OnStageDelegate(FoldStage stage, IScorable< Item, Score > scorable, Item item, object state, Score score)
Event handler delegate for fold scorable stages.
OrderScorableFactory(FoldScorable< Item, Score >.OnStageDelegate onStage, IComparer< Score > comparer, IEnumerable< IScorableFactory< Item, Score >> factories)
IScorable< Item, Score > ScorableFor(IEnumerable< MethodInfo > methods)
Fold an aggregation of scorables to produce a winning scorable.
Definition: FoldScorable.cs:72
OrderScorableFactory(FoldScorable< Item, Score >.OnStageDelegate onStage, IComparer< Score > comparer, params IScorableFactory< Item, Score >[] factories)
Namespace for internal machinery that is not useful for most developers and may change in the future...
ScorableGroupAttribute(int order)
Construct the ScorableGroupAttribute.
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
Core namespace for Dialogs and associated infrastructure.
Helper methods to enumerate inherited attributes for a method.
This attribute is used to control scorable groups for DispatchDialog<R>.
Root namespace for the Microsoft Bot Builder SDK.