PageRenderTime 220ms CodeModel.GetById 101ms app.highlight 3ms RepoModel.GetById 98ms app.codeStats 0ms

/src/LinFu.Finders/FuzzyItem.cs

http://github.com/philiplaureano/LinFu
C# | 109 lines | 55 code | 17 blank | 37 comment | 15 complexity | 6f510190a3ee7b35a13d376f3e49dadc MD5 | raw file
  1using LinFu.Finders.Interfaces;
  2
  3namespace LinFu.Finders
  4{
  5    /// <summary>
  6    ///     Represents the default implementation of a weighted item in
  7    ///     a fuzzy list.
  8    /// </summary>
  9    /// <typeparam name="T">The item type to be tested.</typeparam>
 10    public class FuzzyItem<T> : IFuzzyItem<T>
 11    {
 12        private bool _failed;
 13        private int _matches;
 14        private int _testCount;
 15
 16        /// <summary>
 17        ///     Initializes the <see cref="FuzzyItem{T}" /> class with the given <paramref name="item" />.
 18        /// </summary>
 19        /// <param name="item">An instance of the <typeparamref name="T">item type</typeparamref> that will be tested.</param>
 20        public FuzzyItem(T item)
 21        {
 22            Item = item;
 23            _failed = false;
 24        }
 25
 26
 27        /// <summary>
 28        ///     Reports the probability of a match
 29        ///     based on the <see cref="ICriteria{T}" />
 30        ///     that has been tested so far.
 31        ///     A value of 1.0 indicates a 100% match;
 32        ///     A value of 0.0 equals a zero percent match.
 33        /// </summary>
 34        public double Confidence
 35        {
 36            get
 37            {
 38                if (_failed)
 39                    return -1;
 40
 41                double result = 0;
 42                if (_testCount == 0)
 43                    return 0;
 44
 45                result = _matches / (double) _testCount;
 46
 47                return result;
 48            }
 49        }
 50
 51        /// <summary>
 52        ///     Gets the target item.
 53        /// </summary>
 54        public T Item { get; }
 55
 56        /// <summary>
 57        ///     Tests if the current item matches the given
 58        ///     <paramref name="criteria" />.
 59        /// </summary>
 60        /// <param name="criteria">
 61        ///     The <see cref="ICriteria{T}" /> that determines whether or not the <see cref="Item" /> meets a
 62        ///     particular description.
 63        /// </param>
 64        public void Test(ICriteria<T> criteria)
 65        {
 66            // Determine the weight multiplier of this test
 67            var weight = criteria.Weight;
 68
 69            // Ignore any further criteria tests
 70            // if this item fails
 71            if (_failed)
 72                return;
 73
 74            var predicate = criteria.Predicate;
 75            if (predicate == null)
 76                return;
 77
 78            var result = predicate(Item);
 79
 80            // If the critical test fails, all matches will be reset
 81            // to zero and no further matches will be counted
 82            if (result == false && criteria.Type == CriteriaType.Critical)
 83            {
 84                _failed = true;
 85                return;
 86            }
 87
 88            if (result)
 89                _matches += weight;
 90
 91            // Don't count the result if the criteria
 92            // is optional
 93            if (result != true && criteria.Type == CriteriaType.Optional)
 94                return;
 95
 96            _testCount += weight;
 97        }
 98
 99        /// <summary>
100        ///     Resets the item back to its initial state.
101        /// </summary>
102        public void Reset()
103        {
104            _testCount = 0;
105            _matches = 0;
106            _failed = false;
107        }
108    }
109}