Beispiel #1
0
        public IActionResult EvaluateExpression([FromBody, SwaggerRequestBody("The request that contains the expression and variables")] ExpressionRequest request)
        {
            if (!this.ModelState.IsValid)
            {
                this.logger.LogError("This model doesn't look right to me!");
                throw new ArgumentException("Your request body wasn't proper! ");
            }

            this.logger.LogInformation($"All looks good.  Let's start parsing: {request.Expression}.");
            MathExpressionBuilder builder          = new MathExpressionBuilder(request.Expression);
            IMathExpression       parsedExpression = builder.GenerateExpression();

            this.logger.LogDebug("Yup.  Looks like a legit expression.");
            foreach (var variable in request.VariableEntries)
            {
                builder.SetVariable(variable.Name, variable.Value.ToString());
                this.logger.LogDebug("Setting variable {0} with {1}", variable.Name, variable.Value);
            }

            ExpressionResponse response = new ExpressionResponse
            {
                AdjustedExpression = parsedExpression.ToString(),
                Expression         = request.Expression,
                VariableEntries    = request.VariableEntries,
                ValueGenerated     = parsedExpression.EvaluateExpression()
            };

            return(this.Ok(response));
        }
Beispiel #2
0
 //We are not using the expressions parameter passed as this will be removed from the method definition of interface IEXpressions
 //when we stop using the ChainOfExpressions class. Every class will have its own properties required to evaluiate the expressions
 //it is meant to calculate the results for.
 public async Task <ExpressionResponse> CalculateResult(string[] expressions, int?precision)
 {
     try
     {
         return(await Task.Run(() => ParseMDASExpression(Expression, Operator, precision)));
     }
     catch (Exception ex)
     {
         ExpressionResponse resp = new ExpressionResponse()
         {
             result = null,
             error  = $"Error occurred while evaluating the expression - {ex.Message}"
         };
         return(resp);
     }
 }
 public void Init()
 {
     instance = new ExpressionResponse();
 }