/// <summary> /// Attempts to create a Differentiable object. /// </summary> public bool TryCreate(string id, string value1, string value2, IDifferentiable <int> fallback, out IDifferentiable <int> differentiable) { bool successfulParse; bool createdSuccessfully = false; try { successfulParse = int.TryParse(value1, out int parsedValue1); successfulParse &= int.TryParse(value2, out int parsedValue2); // Could throw an error to indicate there was an issue parsing with specific detail on what failed; // Because this application does not have specific error handling reqs, I like using the empty object // here for simplicity. if (successfulParse) { createdSuccessfully = TryCreate(id, parsedValue1, parsedValue2, out differentiable); } else { differentiable = fallback; } } catch (Exception ex) { _loggingService.Log("Unable to create differentiable object from string values.", ex); differentiable = fallback; } return(createdSuccessfully); }
/// <summary> /// Creates a SeasonGoalsResult object. /// </summary> /// <returns>True if object successfully created with input params. False if not and out param is set to empty object.</returns> public override bool TryCreate(string team, int goalsFor, int goalsAgainst, out IDifferentiable <int> seasonGoalsResult) { try { seasonGoalsResult = new SeasonGoalsResult(team, goalsFor, goalsAgainst); } catch (Exception ex) { _loggingService.Log("Unable to create SeasonResult object from numeric values.", ex); seasonGoalsResult = SeasonGoalsResult.EmptySeasonResult; } return((SeasonGoalsResult)seasonGoalsResult != SeasonGoalsResult.EmptySeasonResult); }
/// <summary> /// Creates a DailyTemperature object. /// </summary> /// <returns>True if object successfully created with input params. False if not and out param is set to empty object.</returns> public override bool TryCreate(string dayOfMonth, int minTemp, int maxTemp, out IDifferentiable <int> dailyWeather) { try { var successful = int.TryParse(dayOfMonth, out int numDayOfMonth); dailyWeather = successful ? new DailyTemperature(numDayOfMonth, minTemp, maxTemp) : DailyTemperature.EmptyDailyWeather; } catch (Exception ex) { _loggingService.Log("Unable to create DailyWeather object from numeric values.", ex); dailyWeather = DailyTemperature.EmptyDailyWeather; } return((DailyTemperature)dailyWeather != DailyTemperature.EmptyDailyWeather); }
/// <summary> /// Creates SeasonResult objects based on each record in the data file. /// </summary> /// <param name="filePath">Path of the data file containing season result records.</param> /// <returns>A collection of SeasonResult objects that were present in the input file.</returns> public async Task <IEnumerable <IDifferentiable <int> > > GetDifferentiables() { var seasonResults = new List <IDifferentiable <int> >(); try { using (var sr = new StreamReader(_filePath)) { string line; IDifferentiable <int> parsedSeasonResult = null; // Ignore header row. // This implementation assumes the application code will have // to be modified and recompiled if the file format changes. await sr.ReadLineAsync().ConfigureAwait(false); while ((line = await sr.ReadLineAsync().ConfigureAwait(false)) != null) { // Replace spaces with commas to simplify parsing and avoid parsing // by hard-coded character index values. Using regex to handle multiple spaces. line = line.Trim(); Regex rx = new Regex(@"\s+"); line = rx.Replace(line, ","); var resultValues = line.Split(','); // The factory.TryCreate method will handle rows which don't conform to the schema // we need. if (resultValues.Length > 8 && _seasonResultFactory.TryCreate(resultValues[1], resultValues[6], resultValues[8], SeasonGoalsResult.EmptySeasonResult, out parsedSeasonResult)) { seasonResults.Add(parsedSeasonResult); } } } } catch (Exception ex) { _loggingService.Log("Unable to parse season result data file.", ex); } return(seasonResults); }
private IElement DifferentiateOnce(IDifferentiable Differentiable, Variables Variables) { string VariableName = Differentiable.DefaultVariableName; if (string.IsNullOrEmpty(VariableName)) { throw new ScriptRuntimeException("No default variable available.", this); } ScriptNode Result = Differentiable.Differentiate(VariableName, Variables); if (Result is IElement Element) { return(Element); } return(new LambdaDefinition(new string[] { VariableName }, new ArgumentType[] { ArgumentType.Normal }, Result, this.Start, this.Length, this.Expression)); }
/// <summary> /// Template method implementation for subclasses. /// </summary> public abstract bool TryCreate(string id, int value1, int value2, out IDifferentiable <int> differentiable);