Ejemplo n.º 1
0
        public static void Calculate()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            Calculation calc = CalculationGenerator.GenerateStandardIslem();

            //calc = new Calculation
            //{
            //    Input = new int[] { 2, 8, 7, 5, 25, 10 },
            //    Target = 777
            //};

            //Printers.PrintSolution(solution);
            //Printers.PrintInput(calc);
            CalculationProcessor.Solve(calc);
            Printers.PrintSolutions(calc);
            //Solution solution = SolutionProcessor.CraziestSolution(calc);
            //Printers.PrintSolution(solution);
            //Console.WriteLine($"Target {calc.Target} have no solution!\n");
            stopWatch.Stop();
            Console.WriteLine(stopWatch.Elapsed);
            _ = calc;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Обработчик нажатия кнопки расчета
        /// </summary>
        /// <param name="obj"></param>
        private void ExecuteCalculate(object obj)
        {
            ProgressiveRates.Clear();
            CalculationProcessor calculationProcessor = new CalculationProcessor(new RepositoryExcel(_excelFilePatch), ProgressiveRates, _dateStart, _dateEnd);

            calculationProcessor.CalculateProgressiveRate();
        }
Ejemplo n.º 3
0
        private string DivideByOperatorsPriority(string textToProcess)
        {
            var expressionsToCalculate = new List <string>();
            var expressionType         = CalculationType.Addition;

            while (true)
            {
                switch (depthLevel)
                {
                case 1:
                    var standardNumber = @"\(?-?\w+[,.]?\w*\)?";

                    expressionsToCalculate = DefaultDivider(
                        textToProcess,
                        $@"sin{standardNumber}|cos{standardNumber}|tg{standardNumber}|sinh{standardNumber}|cosh{standardNumber}|tgh{standardNumber}"
                        );

                    expressionType = CalculationType.TrygoFunc;
                    break;

                case 2:
                    expressionsToCalculate = DefaultDivider(textToProcess, @"ln\(\w+[,.]?\w*\)|log\(\w+[,.]?\w*\)(\(\w+[,.]?\w*\))?");
                    expressionType         = CalculationType.Logarithm;
                    break;

                case 3:
                    expressionsToCalculate = DefaultDivider(textToProcess, @"sqrt\(\w+[,.]?\w*\)(\(\w+[,.]?\w*\))?");
                    expressionType         = CalculationType.Root;
                    break;

                case 4:
                    expressionsToCalculate = ToPower(textToProcess);
                    expressionType         = CalculationType.Exponentation;
                    break;

                case 5:
                    expressionsToCalculate = DefaultDivider(textToProcess, @"\(?-*\w+[,.]?\w*\)?\s*\/\s*\(?\w+[,.]?\w*\)?");
                    expressionType         = CalculationType.Division;
                    break;

                case 6:
                    expressionsToCalculate = DefaultDivider(textToProcess, @"\(?-*\w+[,.]?\w*\)?\s*\*\s*\(?-*\w+[,.]?\w*\)?");
                    expressionType         = CalculationType.Multiplication;
                    break;

                case 7:
                    expressionsToCalculate = DefaultDivider(textToProcess, @"\(?-*\w+[,.]?\w*\)?\s*\-+\s*\(?\w+[,.]?\w*\)?");
                    expressionType         = CalculationType.Substraction;
                    break;

                case 8:
                    expressionsToCalculate = DefaultDivider(textToProcess, @"\(?-*\w+[,.]?\w*\)?\s*\+\s*\(?-*\w+[,.]?\w*\)?");
                    expressionType         = CalculationType.Addition;
                    break;
                }

                if (expressionsToCalculate.Count == 0)
                {
                    break;
                }

                var calculator = new CalculationProcessor(expressionsToCalculate, expressionType);
                var result     = calculator.Calculate();

                while (expressionsToCalculate.Count > 0)
                {
                    textToProcess = textToProcess.Replace(expressionsToCalculate.First(), result.First());
                    textToParse   = textToParse.Replace(expressionsToCalculate.First(), result.First());
                    result.RemoveAt(0);
                    expressionsToCalculate.RemoveAt(0);
                }
            }

            return(textToProcess);
        }
Ejemplo n.º 4
0
        ///<inheritdoc/>
        public PagedQueryResultModel ProcessQuery(PagedQueryModel query,
                                                  string connectionStringName                       = null,
                                                  List <ComparableFieldModel> comparisons           = null,
                                                  List <CalculatedColumnModel> externalCalculations = null)
        {
            try
            {
                if (query.Queries.All(q => q.TableQueries.All(tq => tq.SelectedColumns.Count == 0 && tq.Functions.Count == 0)))
                {
                    return(new PagedQueryResultModel
                    {
                        PageNumber = 1,
                        Result = new QueryResultModel
                        {
                            ColumnHeaders = new List <HeaderModel>(),
                            Rows = new List <RowModel>()
                        }
                    });
                }
                var connectionString = "Data Source = 193.93.216.233;Initial Catalog = TILT2_DEV;Integrated Security = False;User Id = sae;Password=Devabit1@";
                //var connectionString = connectionStringName == null ? config.ConnectionString : config.GetUserConnectionString(connectionStringName);

                var selectModels = GetConvertedQueries(query.Queries[0].TableQueries);
                var offset       = (query.PagingModel.PageNumber - 1) * query.PagingModel.PageSize + query.PagingModel.Skip;
                var pageSize     = query.PagingModel.PageSize;

                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    var sqlSelectCommand = selectModels.CreateBaseSqlSelectCommand(query.Queries[0].SelectDistinct);
                    sqlSelectCommand.ExtendSqlQueryWithFilters(query.Queries[0].Filters, selectModels);
                    sqlSelectCommand.ChangeSelectedColumnsIfGroupBy();
                    var totalCountQueryResult = new SqlCommand(SqlExpression.CountExpression(sqlSelectCommand.ToString()), connection).ExecuteScalar();
                    var totalCount            = (int?)totalCountQueryResult ?? 0;
                    if (query.PagingModel.PageNumber == 0)
                    {
                        return(new PagedQueryResultModel()
                        {
                            Result = new QueryResultModel()
                            {
                                Rows = new List <RowModel>()
                            },
                            TotalCount = (int?)totalCountQueryResult ?? 0,
                            PageCount = (long)Math.Ceiling((double)totalCount / query.PagingModel.PageSize),
                            PageNumber = query.PagingModel.PageNumber,
                            HasNext = Math.Ceiling((double)totalCount / query.PagingModel.PageSize) > query.PagingModel.PageNumber,
                        });
                    }
                    sqlSelectCommand.ExtendSqlQueryWithPagination(selectModels, offset, pageSize);
                    var queryResult = ProcessQueryResult(sqlSelectCommand.ToString(), connection);

                    if (sqlSelectCommand.HasGroupBy)
                    {
                        sqlSelectCommand.ChangeSelectedColumnsIfGroupBy(true);
                        var selectString            = sqlSelectCommand.ToString();
                        var groupDetailsQueryResult = ProcessQueryResult(sqlSelectCommand.ToString(), connection);
                        queryResult.SetInternalRows(groupDetailsQueryResult, sqlSelectCommand.GroupByColumns.Select(x => x.Split('.')[1]).ToList());
                    }
                    connection.Close();
                    var pagedQueryResult = new PagedQueryResultModel()
                    {
                        TotalCount = totalCount,
                        PageCount  = (long)Math.Ceiling((double)totalCount / query.PagingModel.PageSize),
                        PageNumber = query.PagingModel.PageNumber,
                        HasNext    = Math.Ceiling((double)totalCount / query.PagingModel.PageSize) > query.PagingModel.PageNumber,
                        Result     = queryResult
                    };

                    if (queryResult.Rows.Count != 0)
                    {
                        // query.Query.PrepareCalculations();
                        // ProcessCalculatedColumns(pagedQueryResult, query);
                        CalculationProcessor.ProcessCalculatedColumns(pagedQueryResult, query.Queries[0].CalculatedColumns);
                    }

                    //comparisons
                    if (sqlSelectCommand.HasGroupBy && queryResult.Rows.Count != 0 && comparisons != null)
                    {
                        var groupings = sqlSelectCommand.GroupByColumns
                                        .Select(x => x.Split('.')[1])
                                        .ToList();
                        comparisons.Reverse();
                        foreach (var comparison in comparisons)
                        {
                            ChangeFilters(comparison, query.Queries[0]);
                            var temp = this.ProcessQuery(query, query.ConnectionStringName);
                            totalCount = temp.TotalCount > totalCount ? temp.TotalCount : totalCount;
                            var columnIndex = queryResult.ColumnHeaders.FindIndex(x => x.Name == comparison.Column.ColumnAlias);

                            pagedQueryResult.Result.ExtendResultWithComparisons(temp.Result, groupings, columnIndex);

                            pagedQueryResult.Result.ColumnHeaders.Insert(columnIndex + 1, new HeaderModel
                            {
                                Name = comparison.Alias,
                                Type = pagedQueryResult.Result.ColumnHeaders[columnIndex].Type
                            });
                        }
                    }

                    if (externalCalculations != null)
                    {
                        CalculationProcessor.ProcessCalculatedColumns(pagedQueryResult, externalCalculations);
                    }

                    foreach (var row in pagedQueryResult.Result.Rows)
                    {
                        if (!query.Queries[0].ShowDetails)
                        {
                            row.Internal = null;
                        }
                        else if (row.Internal != null)
                        {
                            //                  row.Internal.Rows = row.Internal.Rows.Take(100).ToList();
                        }
                    }

                    return(pagedQueryResult);
                }
            }
            catch (Exception e)
            {
                context.Errors.Add(new DataAccess.Models.ErrorQuery()
                {
                    QueryText = Newtonsoft.Json.JsonConvert.SerializeObject(query),
                    Date      = DateTime.Now,
                    Exception = e.Message
                });
                context.SaveChanges();
                throw;
            }
        }