Пример #1
0
        public IEnumerable <Tuple <int, InputType, IEnumerable <string>, string> > Process(IEnumerable <string> contents)
        {
            ICollection <Tuple <int, InputType, IEnumerable <string>, string> > result = new Collection <Tuple <int, InputType, IEnumerable <string>, string> >();

            int lineNumber = 0;

            foreach (string content in contents)
            {
                try
                {
                    InputType            inputType = this.GetInputType(content);
                    IEnumerable <string> products  = new Collection <string>();;
                    string productValue            = string.Empty;

                    this.ProcessContent(content, ++lineNumber, inputType, out products, out productValue);

                    result.Add(new Tuple <int, InputType, IEnumerable <string>, string>
                                   (lineNumber, inputType, products, productValue));
                }
                catch (Exception ex)
                {
                    SLogger.LogErrorFormat(ex, "Exception found when Preprocessing line: {0}.", lineNumber);
                }
            }

            return(result);
        }
Пример #2
0
        public string GetMultiplierValue(IEnumerable <string> intergalacticProducts, IDictionary <string, string> intergalacticProductsCache)
        {
            if (intergalacticProducts != null && intergalacticProducts.Any() &&
                intergalacticProductsCache != null && intergalacticProductsCache.Any())
            {
                StringBuilder romanNumeral = new StringBuilder();

                foreach (string intergalacticProduct in intergalacticProducts)
                {
                    if (!intergalacticProductsCache.ContainsKey(intergalacticProduct))
                    {
                        // An error scenario, which should not crash the app - no need to throw this error.
                        SLogger.LogErrorFormat("Intergalactic product {0} is not found in the cache.", intergalacticProduct);
                        return(null);
                    }

                    if (string.IsNullOrEmpty(intergalacticProductsCache[intergalacticProduct]))
                    {
                        // An error scenario, which should not crash the app - no need to throw this error.
                        SLogger.LogErrorFormat("Null value found for Intergalactic product {0} in the cache.", intergalacticProduct);
                        return(null);
                    }

                    romanNumeral.Append(intergalacticProductsCache[intergalacticProduct]);
                }

                return(romanNumeral.ToString());
            }

            return(null);
        }
Пример #3
0
        public decimal GetEarthyProductValue(
            string earthyProduct, string productValue,
            IEnumerable <string> intergalacticProducts,
            IDictionary <string, string> intergalacticProductsCache)
        {
            decimal value = 0;

            decimal.TryParse(productValue, out value);

            if (intergalacticProducts == null || !intergalacticProducts.Any())
            {
                SLogger.LogWarnFormat("No intergalacticProducts tagged with earthyProduct:{0}. Returning value:{1} for string value of:{2}",
                                      earthyProduct, value, productValue);

                return(value);
            }

            int multiplier = this.GetMultiplierInArabicNumeralForm(intergalacticProducts, intergalacticProductsCache);

            if (multiplier > 0)
            {
                return(value / multiplier);
            }

            SLogger.LogErrorFormat("Invalid Multiplier: {0} returned for earthyProduct:{1} with value:{2} - Tagged IntergalacticProducts:{3}",
                                   multiplier, earthyProduct, productValue, string.Join(", ", intergalacticProducts));

            return(0);
        }
Пример #4
0
        public IEnumerable <string> Analyze(IEnumerable <Tuple <int, InputType, IEnumerable <string>, string> > contents)
        {
            ICollection <string> analyzedResponses = new Collection <string>();

            if (contents != null && contents.Any())
            {
                foreach (Tuple <int, InputType, IEnumerable <string>, string> content in contents)
                {
                    int                  lineNumber   = content.Item1;
                    InputType            typeOfInput  = content.Item2;
                    IEnumerable <string> products     = content.Item3;
                    string               productValue = content.Item4;

                    SLogger.LogInfoFormat("Analyzing line number {0}.", lineNumber);

                    try
                    {
                        Tuple <int, IEnumerable <string>, string> contentToInterpret =
                            new Tuple <int, IEnumerable <string>, string>(lineNumber, products, productValue);

                        switch (typeOfInput)
                        {
                        case InputType.ProductData:
                        {
                            if (products == null || !products.Any())
                            {
                                SLogger.LogWarn("No products found to analyze. Skipping product data intrepretation...");
                                continue;
                            }

                            this.productDataInterpreter.Interpret(contentToInterpret);
                        }
                        break;

                        case InputType.Query:
                        {
                            string queryResponse = this.queryInterpreter.Interpret(contentToInterpret);
                            analyzedResponses.Add(queryResponse);
                        }
                        break;

                        default:
                            throw new InvalidOperationException("Invalid InputType Enum.");
                        }
                    }
                    catch (Exception ex)
                    {
                        SLogger.LogErrorFormat(ex, "Exception found when Analyzing line: {0}.", lineNumber);
                    }
                }
            }

            return(analyzedResponses);
        }
Пример #5
0
        private bool IsEarthyProductValid(IEnumerable <string> products, string productValue, int lineNumber,
                                          InputType typeOfInput, IDictionary <string, string> intergalacticProductsCache = null)
        {
            if (products.Any(p => this.productCategorizer.IsProductEarthy(p)))
            {
                string earthyProduct = products.Last();

                // Check if Earthy product, if any, are not mentioned at the end of the list
                if (earthyProduct != products.SingleOrDefault(p => this.productCategorizer.IsProductEarthy(p)))
                {
                    SLogger.LogWarnFormat("At Line number: {0}, Earthy product should be last referenced.");
                    return(false);
                }

                // Check if the intergalactic representation 'tagged' with earthy product is proper
                if (intergalacticProductsCache != null && intergalacticProductsCache.Any())
                {
                    IEnumerable <string> intergalacticProducts = products.Where(p => this.productCategorizer.IsProductIntergalactic(p));
                    if (intergalacticProducts.Any())
                    {
                        string romanNumeral = this.productHelper.GetMultiplierValue(intergalacticProducts, intergalacticProductsCache);
                        string error        = this.validator.Validate(romanNumeral.ToString());

                        if (!string.IsNullOrEmpty(error))
                        {
                            // An error scenario - due to Logic Flaw in processing one of the special input! - This should not crash the app - no need to throw this error.
                            SLogger.LogErrorFormat("At Line number: {0}, error : {1}, while validating roman numberal : {2}.", lineNumber, error, romanNumeral);
                            return(false);
                        }
                    }
                }

                // Followup check if the unit association to valid Earthy product is Roman Numeral
                return(this.IsProductValueValid(earthyProduct, productValue, typeOfInput, lineNumber));
            }

            return(true);
        }