public override void Store(string normalInput) { var trimmedNormalInput = normalInput.Trim(); ContractUtility.Requires <ArgumentException>(trimmedNormalInput.Contains(" is "), "normalInput must contain \" is \""); var normalInputWordsBeforeAndAfterIs = trimmedNormalInput.Split(new string[] { " is " }, StringSplitOptions.None).Where(x => !String.IsNullOrEmpty(x)); var normalInputWordBeforeIs = normalInputWordsBeforeAndAfterIs.FirstOrDefault(); ContractUtility.Requires <ArgumentException>(!String.IsNullOrEmpty(normalInputWordBeforeIs) && !String.IsNullOrWhiteSpace(normalInputWordBeforeIs), "Some data must be there before \"is\" in normalInput"); ContractUtility.Requires <ArgumentException>(!normalInputWordBeforeIs.Trim().Contains(" "), "Cannot have > 1 intergalactic words before \"is\" in normalInput"); ContractUtility.Requires <ArgumentException>(!normalInputMappingsWithTerminalIsExpressions.ContainsKey(normalInputWordBeforeIs), "normalInput is already stored"); var normalSymbolAfterIs = normalInputWordsBeforeAndAfterIs.LastOrDefault(); ContractUtility.Requires <ArgumentException>(!String.IsNullOrEmpty(normalSymbolAfterIs) && !String.IsNullOrWhiteSpace(normalSymbolAfterIs), "Some data must be there after \"is\" in normalInput"); ContractUtility.Requires <ArgumentException>(!normalSymbolAfterIs.Trim().Contains(" "), "Cannot have > 1 symbols after \"is\" in normalInput"); var validSymbols = symbolValues.SymbolValues.Keys; ContractUtility.Requires <ArgumentException>(validSymbols.Select(x => x.ToString()).Contains(normalSymbolAfterIs.Trim()), "The symbol after \"is\" in normalInput must be " + CharactersUtility.GetFormattedCommaSeperatedCharacters(validSymbols, "or") + " ."); var terminalIsExpression = new TerminalIsExpression <string, string>(normalInputWordBeforeIs, normalSymbolAfterIs); normalInputMappingsWithTerminalIsExpressions.Add(normalInputWordBeforeIs, terminalIsExpression); }
public override void Store(string creditsInput) { var trimmedCreditsInput = creditsInput.Trim(); ContractUtility.Requires <ArgumentException>(trimmedCreditsInput.Contains(" is "), "creditsInput must contain \" is \""); ContractUtility.Requires <ArgumentException>(trimmedCreditsInput.Trim().EndsWith("Credits"), "creditsInput must must end with \"Credits\""); var creditsInputWordsBeforeAndAfterIs = trimmedCreditsInput.Split(new string[] { " is " }, StringSplitOptions.None).Where(x => !String.IsNullOrEmpty(x)); var creditsInputWordsBeforeIs = creditsInputWordsBeforeAndAfterIs.FirstOrDefault(); ContractUtility.Requires <ArgumentException>(!String.IsNullOrEmpty(creditsInputWordsBeforeIs) && !String.IsNullOrWhiteSpace(creditsInputWordsBeforeIs), "Some data must be there before \"is\" in creditsInput"); var creditsInputWordsListBeforeIs = creditsInputWordsBeforeIs.Split(new string[] { " " }, StringSplitOptions.None).Where(x => !String.IsNullOrEmpty(x.Trim())); var creditsTypeInputWordJustBeforeIs = creditsInputWordsListBeforeIs.LastOrDefault(); var allowedCreditTypes = Enum.GetNames(typeof(CreditsType)); ContractUtility.Requires <ArgumentException>(!String.IsNullOrEmpty(creditsTypeInputWordJustBeforeIs) && allowedCreditTypes.Contains(creditsTypeInputWordJustBeforeIs), "The input word just before \"is\" must be any one of " + allowedCreditTypes.Aggregate((a, b) => a + " , " + b) + " ."); ContractUtility.Requires <ArgumentException>(!creditsInputMappingsWithTerminalIsExpressions.ContainsKey(creditsTypeInputWordJustBeforeIs), "creditsInput relevant data is already stored"); var inputValueCreditsAfterIs = creditsInputWordsBeforeAndAfterIs.LastOrDefault(); ContractUtility.Requires <ArgumentException>(!String.IsNullOrEmpty(inputValueCreditsAfterIs) && !String.IsNullOrWhiteSpace(inputValueCreditsAfterIs), "Some data must be there after \"is\" in creditsInput"); var inputValuesJustAfterIs = inputValueCreditsAfterIs.Trim().Split(new string[] { " Credits" }, StringSplitOptions.None).Where(x => !String.IsNullOrEmpty(x)); ContractUtility.Requires <ArgumentException>(inputValuesJustAfterIs.Count() == 1, "There must be atleast one and only one value between \"is\" and \"Credits\" in creditsInput"); var inputValueJustAfterIs = inputValuesJustAfterIs.FirstOrDefault(); var intInputValueJustAfterIs = 0; ContractUtility.Requires <ArgumentException>(!String.IsNullOrEmpty(inputValueJustAfterIs) && Int32.TryParse(inputValueJustAfterIs, out intInputValueJustAfterIs), "The value of the data between \"is\" and \"Credits\" in creditsInput must be of integer type"); var normalInputWordsListBeforeCreditsType = creditsInputWordsListBeforeIs.Where(x => x != creditsTypeInputWordJustBeforeIs).ToList(); var normalInputWordsListBeforeCreditsTypeNumberFormatValue = 1; if (normalInputWordsListBeforeCreditsType.Count > 0) { var normalInputSemanticLanguageExpression = new NormalInputSemanticLanguageExpression(numberFormatCalculator, symbolValues); var normalIntergalacticWordsList = new TerminalListExpression <string>(normalInputWordsListBeforeCreditsType); normalInputWordsListBeforeCreditsTypeNumberFormatValue = normalInputSemanticLanguageExpression.Interpret(normalIntergalacticWordsList); } decimal creditsTypeValue = Convert.ToDecimal(intInputValueJustAfterIs) / Convert.ToDecimal(normalInputWordsListBeforeCreditsTypeNumberFormatValue); var terminalIsExpression = new TerminalIsExpression <string, decimal>(creditsTypeInputWordJustBeforeIs, creditsTypeValue); creditsInputMappingsWithTerminalIsExpressions.Add(creditsTypeInputWordJustBeforeIs, terminalIsExpression); }