/// <summary> /// Create an equation from the interface type. /// Does not save the equation. /// </summary> public Equation CreateEquation(RestEquation restEquation) { // Define regex for looking up ref(0) expressions var referenceRegEx = new Regex(@"Ref\((\d+)\)", RegexOptions.IgnoreCase); var referenceMatches = referenceRegEx.Matches(restEquation.Equation); // Foreach match, find its index and look that up. var referenceIds = referenceMatches.Select(refMatch => int.Parse(refMatch.Captures.First().Value.Substring(4).Replace(")", string.Empty))); var references = referenceIds .Select(refId => AllEquations.FirstOrDefault(equation => equation.Id == refId)) .Where(reference => reference != null) .ToList(); // Resolve references, until no more can be added or max depth is reached. const int maxDepth = 100; for (var i = 0; i < maxDepth; i++) { if (!AddReferences(references)) { break; } } // Create a new equation using the collected references and parameters return(new Equation(restEquation.Description, restEquation.Equation, restEquation.Parameters, references)); }
public CreatedResult Create([FromBody] RestEquation equation) { var parsed = EquationStore.CreateEquation(equation); if (!equation.Id.HasValue || equation.Id.Equals(-1)) { equation.Id = parsed.Id; EquationStore.AddEquation(parsed); } else { parsed.Id = equation.Id.Value; EquationStore.UpdateEquation(parsed); } return(Created($"/equations/{parsed.Id}", equation)); }
public object Evaluate([FromBody] RestEquation equation) { if (string.IsNullOrWhiteSpace(equation?.Equation)) { return(new RestEvaluationResult { Success = true, Value = 0 }); } try { var result = EquationStore.CreateEquation(equation).Evaluate(); return(new RestEvaluationResult { Success = true, Value = result }); } catch { return(new RestEvaluationResult { Success = false }); } }