Beispiel #1
0
        /// <summary>
        ///     Creates a request based off the results of potential consumption
        /// </summary>
        /// <param name="consumerResult">The consumer result.</param>
        /// <param name="selected">The selected.</param>
        /// <returns>The request</returns>
        public ConsumptionRequest Create(PotentialConsumerResult consumerResult, ConsumptionResult selected)
        {
            for (var i = 1;
                 i < selected.NumConsumed;
                 i++)
            {
                foreach (var parser in consumerResult.Chain)
                {
                    var info = consumerResult.Info.Consume(i);
                    var res  = parser.CanConsume(selected, info);
                    if (res.Info > info && res.ConsumingParameter != null && res.ConsumingParameter is Switch)
                    {
                        return(new ConsumptionRequest(consumerResult.Info, i));
                    }
                }
            }

            return(new ConsumptionRequest(consumerResult.Info, selected.NumConsumed));
        }
        /// <summary>
        ///     Selects the specified result.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns>ConsumptionResult.</returns>
        /// <exception cref="ForwardProgressException"></exception>
        public virtual ConsumptionResult Select(PotentialConsumerResult result)
        {
            ConsumptionResult foundResult = null;
            var switchResults             = result.ConsumptionResults.Where(r => r.ConsumingParameter is Switch).ToList();

            if (switchResults.Any())
            {
                foundResult = switchResults.FirstOrDefault(r => result.Chain.Contains(r.ConsumingParameter.Parser));
            }

            if (foundResult == null)
            {
                foundResult =
                    result.ConsumptionResults.FirstOrDefault(r =>
                                                             result.Chain.Contains(r.ConsumingParameter
                                                                                   .Parser));
            }

            if (foundResult == null)
            {
                throw new ForwardProgressException(result.Info);
            }
            return(foundResult);
        }