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); }
private bool IsInputValid(IEnumerable <string> products, string productValue, InputType typeOfInput, int lineNumber) { if (typeOfInput == InputType.ProductData) { // For Input Data (ProductData), Check if multiple Intergalactic products, without Earthy products, are mentioned in a single assignment line. if (products.Where(p => this.productCategorizer.IsProductIntergalactic(p)).Count() > 1 && !products.Any(p => this.productCategorizer.IsProductEarthy(p))) { SLogger.LogWarnFormat("At Line number: {0}, there are multiple Intergalactic Products and no Earthy products in this assignment line (Product Data Input Type). Invalid case.", lineNumber); return(false); } // Check if the unit associated with the productValue for intergalactic products is Arabic numeral if (!products.Any(p => this.productCategorizer.IsProductEarthy(p))) { foreach (string product in products.Where(p => this.productCategorizer.IsProductIntergalactic(p))) { bool isValid = this.IsProductValueValid(product, productValue, typeOfInput, lineNumber); if (!isValid) { return(isValid); } } } } return(true); }
public IEnumerable <string> GetProducts(string content, int lineNumber) { SLogger.LogDebugFormat("Line number {0} is a question.", lineNumber); if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content)) { SLogger.LogWarnFormat("Line number {0} does not have valid product(s). Skipping...", lineNumber); return(null); } string[] products = content.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); foreach (string product in products) { if (!this.productsList.Contains(product)) { SLogger.LogInfoFormat("Product List (derived by reading the input lines) does not contain product uptil now:{0} at line number {1}.\nSkipping...", product, lineNumber); return(null); } } return(products); }
public string GetProductValue(string content, int lineNumber) { if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content)) { SLogger.LogWarnFormat("Line number {0} does not have a valid product value. Skipping...", lineNumber); return(string.Empty); } int creditsIndex = -1; if (this.DoesContentContainArabicNumerals(content, out creditsIndex) && creditsIndex > 0) { // Assuming that Credits is the last text in the content. if (!(content.Reverse().ToString().StartsWith(CREDITS_TEXT.Reverse().ToString()))) { SLogger.LogWarn("Credits text is not the last word of the content. Skipping..."); return(string.Empty); } // Assuming that Credits are linked to Arabic Numerals int number = 0; string numberString = content.Substring(0, creditsIndex - 1).Trim(); int.TryParse(numberString, out number); string error = this.validator.Validate(number); if (!string.IsNullOrEmpty(error)) { SLogger.LogWarnFormat("Validation issue found for arabic numeric value: {0} \nError: {1} on Line Number: {2}. Skipping...", numberString, error, lineNumber); return(string.Empty); } return(number.ToString()); } else { // Else, the value is in Roman numeral representation string romanNumeral = content.Trim(); string error = this.validator.Validate(content.Trim()); if (!string.IsNullOrEmpty(error)) { SLogger.LogWarnFormat("Validation issue found for roman numberal: {0} \nError: {1} on Line Number: {2}. Skipping...", romanNumeral, error, lineNumber); return(string.Empty); } return(romanNumeral); } }
private string Validate(ValidationResult result) { if (!result.IsValid) { string validationExceptions = this.validationMessageManager.GetValidationString(result); SLogger.LogWarnFormat("Found validationExceptions {0}", validationExceptions); return(validationExceptions); } SLogger.LogDebug("Validation succeeded. No issues noticed."); return(string.Empty); }
private void ProcessContent(string content, int lineNumber, InputType typeOfInput, out IEnumerable <string> products, out string productValue) { SLogger.LogInfoFormat("Preprocessing line number {0} with content {1}", lineNumber, content); products = new Collection <string>(); productValue = string.Empty; if (content.IndexOf(" is ", StringComparison.OrdinalIgnoreCase) == -1) { SLogger.LogWarnFormat("{0} at line number {1} does not contain 'is' keyword. Skipping...", content, lineNumber); return; } string[] words = content.Split(new string[] { " is " }, StringSplitOptions.None); if (words.Length != 2) { SLogger.LogWarnFormat("{0} at line number {1} is not properly formatted. Skipping...", content, lineNumber); return; } switch (typeOfInput) { case InputType.ProductData: { products = this.productDataValidator.GetProducts(words[0].Trim(), lineNumber); if (products == null || !products.Any()) { return; } productValue = this.productDataValidator.GetProductValue(words[1].Trim(), lineNumber); } break; case InputType.Query: { products = this.queryValidator.GetProducts(words[1].Replace("?", string.Empty).Trim(), lineNumber); productValue = words[0].IndexOf(CREDITS_TEXT) != -1 ? CREDITS_TEXT : string.Empty; } break; default: // This won't hit, since GetInputType will always return either of ProductData or Query as InputType throw new InvalidOperationException("Invalid InputType Enum."); } }
private string InterpretProducts(IEnumerable <string> products, OutputUnitType unitType) { bool validResponse = false; StringBuilder response = new StringBuilder(string.Format("{0} is ", string.Join(" ", products))); if (products.All(p => this.productCategorizer.GetProductType(p) == ProductType.Intergalactic)) { response.Append(this.productHelper.GetMultiplierInArabicNumeralForm(products, this.intergalacticProductsCache)); validResponse = true; } if (this.productCategorizer.GetProductType(products.Last()) == ProductType.Earthy) { if (products.Any(p => this.productCategorizer.GetProductType(p) == ProductType.Intergalactic)) { int multiplier = this.productHelper.GetMultiplierInArabicNumeralForm( products.Where(p => this.productCategorizer.GetProductType(p) == ProductType.Intergalactic), this.intergalacticProductsCache); decimal value = multiplier * this.earthyProductsCache[products.Last()]; response.Append(this.GetDecimalString(value)); validResponse = true; } else { response.Append(this.GetDecimalString(this.earthyProductsCache[products.Last()])); validResponse = true; } } if (validResponse) { if (unitType == OutputUnitType.ArabicNumeralWithCredits) { response.Append(" "); response.Append(CREDITS_TEXT); } return(response.ToString()); } SLogger.LogWarnFormat("Failed to get a response. Returning empty response. Product Details:{0} for unitType:{1}", string.Join(", ", products), unitType); return(INVALID_RESPONSE); }
public IEnumerable <string> GetProducts(string content, int lineNumber) { if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content)) { SLogger.LogWarnFormat("Line number {0} does not have a valid product. Skipping...", lineNumber); return(null); } string[] products = content.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); foreach (string product in products) { if (!this.UpdateProductCache(product, lineNumber)) { return(null); } } return(products); }
private bool UpdateProductCache(string product, int lineNumber) { if (!this.productsList.Contains(product)) { if (this.productCategorizer.IsProductValid(product)) { SLogger.LogInfoFormat("Line number {0} contains valid product:{1}.", lineNumber, product); this.productsList.Add(product); return(true); } else { SLogger.LogWarnFormat("Line number {0} contained an invalid product entry:{1}, Skipping...", lineNumber, product); return(false); } } SLogger.LogInfoFormat("Line number {0} contains valid product:{1}. Found in 'in-memory' list of products. Skipping adding to it.", lineNumber, product); return(true); }
private bool IsProductValueValid(string product, string productValue, InputType typeOfInput, int lineNumber) { if (typeOfInput == InputType.ProductData && !string.IsNullOrEmpty(productValue)) { string error = null; if (this.productCategorizer.IsProductIntergalactic(product)) { // Check if intergalactic product has arabic numeral unit error = this.validator.Validate(productValue); if (!string.IsNullOrEmpty(error)) { SLogger.LogWarnFormat("At Line number: {0}, Intergalactic product:{1} does not have proper roman numeral value. Value:{2}. Error:{3}", lineNumber, product, productValue, error); return(false); } } else if (this.productCategorizer.IsProductEarthy(product)) { // Check if earthy product has roman numeral unit int value = 0; int.TryParse(productValue, out value); error = this.validator.Validate(value); if (!string.IsNullOrEmpty(error)) { SLogger.LogWarnFormat("At Line number: {0}, Earthy product:{1} does not have proper arabic numeral value. Value:{2}. Error:{3}", lineNumber, product, productValue, error); return(false); } } } return(true); }
public bool IsProductRepresentationValid(IEnumerable <string> products, string productValue, InputType typeOfInput, int lineNumber, IDictionary <string, string> intergalacticProductsCache = null) { if (products == null || !products.Any()) { SLogger.LogWarnFormat("At Line number: {0}, there were no products found. Skipping.", lineNumber); return(false); } if (products.Count() == 1) { string product = products.First(); return(this.IsProductValueValid(product, productValue, typeOfInput, lineNumber)); } else { if (!IsInputValid(products, productValue, typeOfInput, lineNumber)) { return(false); } // Check if multiple Earthy products are mentioned in a single line. if (products.Where(p => this.productCategorizer.IsProductEarthy(p)).Count() > 1) { SLogger.LogWarnFormat("At Line number: {0}, there are multiple Earthy products. Invalid case.", lineNumber); return(false); } bool isEarthyProductValid = IsEarthyProductValid(products, productValue, lineNumber, typeOfInput, intergalacticProductsCache); if (!isEarthyProductValid) { return(false); } } return(true); }
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); }