public void TestSTAngle()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                var p1 = db.Select(() => GeometryConstructors.STPoint(0, 0));
                var p2 = db.Select(() => GeometryConstructors.STPoint(100, 100));
                var p3 = db.Select(() => GeometryConstructors.STPoint(0, 0));
                var p4 = db.Select(() => GeometryConstructors.STPoint(100, 0));

                var a1 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAngle(p1, p2, p3, p4)));
                Assert.AreEqual(45, a1.Value, 1.0E-8);

                var a2 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAngle(p2, p1, p4)));
                Assert.AreEqual(45, a2.Value, 1.0E-8);

                Assert.IsNull(db.Select(() => MeasurementFunctions.STAngle(p1, p1, p1, p1)));
            }
        }
        public void TestSTAzimuth()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                var p1 = db.Select(() => GeometryConstructors.STPoint(25, 45));
                var p2 = db.Select(() => GeometryConstructors.STPoint(75, 100));

                var a1 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAzimuth(p1, p2)));
                var a2 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAzimuth(p2, p1)));

                Assert.AreEqual(42.2736890060937, a1, 1.0E-8);
                Assert.AreEqual(222.273689006094, a2, 1.0E-8);

                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(p1, p1)));
                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(p1, null)));
                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(null, p1)));
            }
        }
        public void TestSTAzimuth()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                var p1 = db.Select(() => GeometryConstructors.STPoint(25, 45));
                var p2 = db.Select(() => GeometryConstructors.STPoint(75, 100));

                var a1 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAzimuth(p1, p2)));
                var a2 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAzimuth(p2, p1)));

                Assert.AreEqual(42.2736890060937, a1.Value, 1.0E-8);
                Assert.AreEqual(222.273689006094, a2.Value, 1.0E-8);

                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(p1, p1)));
                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(p1, null)));
                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(null, p1)));
                ////Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth((NTSG)null, (NTSG)null)));


                var pointA = new NTSGS.Point(0, 0)
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(1, pointA));

                var pointB = new NTSGS.Point(15, 5)
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(2, pointB));

                var azimuth = db.TestGeographies
                              .Where(g => g.Id == 1)
                              .Select(g => g.Geography.STAzimuth(db.TestGeographies.Where(g0 => g0.Id == 2).Single().Geography))
                              .Single();

                Assert.AreEqual(1.24683, azimuth.Value, 1.0E-5);
            }
        }
        /// <summary>Outputs where Array Functions function is used, includes the array builder.
        /// <para>jparameters = JSON congifurations relating to this function</para>
        /// <para>jCategory = the whole configuration which is required to do the variable replace</para>
        /// <para>GroupID = current Group ID</para>
        /// <para>ItemID = current row ID</para>
        /// </summary>
        public string Output(string jparameters, List <CategoryViewModel> jCategory, int GroupID, int ItemID)
        {
            CalculationCSharp.Areas.Configuration.Models.ConfigFunctions Config = new CalculationCSharp.Areas.Configuration.Models.ConfigFunctions();
            JavaScriptSerializer  javaScriptSerializer = new JavaScriptSerializer();
            MathematicalFunctions MathFunctions        = new MathematicalFunctions();
            ArrayFunctions        parameters           = (ArrayFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(ArrayFunctions));
            dynamic InputA      = Config.VariableReplace(jCategory, parameters.LookupValue, GroupID, ItemID);
            string  OutputValue = null;

            if (parameters.LookupType == "Decimal")
            {
                OutputValue = DecimalCalculation(parameters, InputA);
            }
            else if (parameters.LookupType == "Date")
            {
                OutputValue = DateCalculation(parameters, InputA);
            }
            else
            {
                OutputValue = Convert.ToString(0);
            }
            return(Convert.ToString(OutputValue));
        }
Exemple #5
0
        /// <summary>Outputs where String Functions function is used, includes the array builder.
        /// <para>jparameters = JSON congifurations relating to this function</para>
        /// <para>jCategory = the whole configuration which is required to do the variable replace</para>
        /// <para>GroupID = current Group ID</para>
        /// <para>ItemID = current row ID</para>
        /// </summary>
        public string Output(string jparameters, List <CategoryViewModel> jCategory, int GroupID, int ItemID)
        {
            JavaScriptSerializer   javaScriptSerializer = new JavaScriptSerializer();
            MathematicalFunctions  MathFunctions        = new MathematicalFunctions();
            ArrayBuildingFunctions ArrayBuilder         = new ArrayBuildingFunctions();
            StringFunctions        parameters           = (StringFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(StringFunctions));

            string[] Numbers1parts = null;
            string[] Numbers2parts = null;
            string[] Numbers3parts = null;
            //Returns array
            Numbers1parts = ArrayBuilder.InputArrayBuilder(parameters.String1, jCategory, GroupID, ItemID);
            Numbers2parts = ArrayBuilder.InputArrayBuilder(parameters.Number1, jCategory, GroupID, ItemID);
            Numbers3parts = ArrayBuilder.InputArrayBuilder(parameters.String3, jCategory, 0, 0);
            string Output      = null;
            string OutputValue = null;
            //Gets Max Length of array so loops through all values
            int MaxLength = ArrayBuilder.GetMaxLength(Numbers1parts, Numbers2parts);

            //If Set only then
            if (Numbers3parts != null)
            {
                MaxLength = 1;
            }
            int Counter = 0;

            //Loop through the array to calculate each value in array
            for (int i = 0; i < MaxLength; i++)
            {
                dynamic InputA = null;
                dynamic InputB = null;
                dynamic InputC = null;
                //Gets the current array to use in the loop
                InputA = ArrayBuilder.GetArrayPart(Numbers1parts, Counter);
                InputB = ArrayBuilder.GetArrayPart(Numbers2parts, Counter);
                InputC = ArrayBuilder.GetArrayPart(Numbers3parts, Counter);

                string InputAString = null;
                int    InputBDeci   = 0;
                string InputCString = null;

                InputAString = Convert.ToString(InputA);
                int.TryParse(InputB, out InputBDeci);
                InputCString = Convert.ToString(InputC);
                //Calculates the value required
                if (parameters.Type == "Left")
                {
                    OutputValue = InputAString.Substring(0, InputBDeci);
                }
                else if (parameters.Type == "Right")
                {
                    OutputValue = InputAString.Substring(InputAString.Length - InputBDeci);
                }
                else if (parameters.Type == "Set")
                {
                    OutputValue = InputCString;
                }
                else if (parameters.Type == "Find")
                {
                    int FindValue = InputAString.IndexOf(parameters.String2);

                    if (FindValue != -1)
                    {
                        OutputValue = Convert.ToString(FindValue);
                    }
                    else
                    {
                        OutputValue = "0";
                    }
                }
                else if (parameters.Type == "Len")
                {
                    OutputValue = Convert.ToString(InputAString.Length);
                }
                Output  = Output + OutputValue + "~";
                Counter = Counter + 1;
            }
            Output = Output.Remove(Output.Length - 1);
            return(Convert.ToString(Output));
        }
Exemple #6
0
        //Calcuation Controller Action
        public void CalculateAction(List<CategoryViewModel> jCategory)
        {
            foreach (var group in jCategory)
            {
                foreach (var item in group.Functions)
                {
                    if (item.Function == "Input")
                    {
                        item.Output = InputFunctions.Output(item.Type, item.Output);
                        OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name });
                    }
                    else
                    {
                        //Logic check at Column Level
                        string colLogic = null;
                        bool colLogicParse = true;
                        if(group.Logic != null)
                        {
                            foreach (var bit in group.Logic)
                            {
                                var grouplastLogic = group.Logic.Last();
                                string grouplastLogicOperator = grouplastLogic.Operator;
                                Logic Logic = new Logic();
                                colLogic = Logic.Output(jCategory, bit, group.ID, 0);
                                Expression ex = new Expression(colLogic);
                                try
                                {
                                    colLogicParse = Convert.ToBoolean(ex.Evaluate());
                                }
                                catch (Exception exception)
                                {
                                    logger.Error(exception);
                                    throw new HttpException(exception.ToString());
                                }

                                if (grouplastLogicOperator == "AND" && colLogicParse == false)
                                {
                                    break;
                                }
                                else if (grouplastLogicOperator == "OR" && colLogicParse == true)
                                {
                                    colLogicParse = true;
                                    break;
                                }

                            }
                        }
                        if (item.Parameter.Count > 0)
                        {
                            string logic = null;
                            bool logicparse = true;
                            string MathString = null;
                            bool PowOpen = false;
                            //Logic check at column level
                            if (colLogicParse == true)
                            {
                                foreach (var bit in item.Logic)
                                {
                                    var lastLogic = item.Logic.Last();
                                    string lastLogicOperator = lastLogic.Operator;
                                    Logic Logic = new Logic();
                                    logic = Logic.Output(jCategory, bit, group.ID, item.ID);
                                    Expression ex = new Expression(logic);

                                    try
                                    {
                                        logicparse = Convert.ToBoolean(ex.Evaluate());
                                    }
                                    catch (Exception exception)
                                    {
                                        logger.Error(exception);
                                        throw new HttpException(exception.ToString());
                                    }

                                    if (lastLogicOperator == "AND" && logicparse == false)
                                    {
                                        break;
                                    }
                                    else if (lastLogicOperator == "OR" && logicparse == true)
                                    {
                                        logicparse = true;
                                        break;
                                    }

                                }
                            }
                            else
                            {
                                logicparse = false;
                            }
                            //Run code if logic if met at column and row level
                            if (logicparse == true)
                            {
                                int paramCount = 1;
                                foreach (var param in item.Parameter)
                                {
                                    string jparameters = Newtonsoft.Json.JsonConvert.SerializeObject(param);
                                    logger.Debug("Column Name(" + group.ID + ") - " + group.Name + " || Row Name(" + item.ID +") - " + item.Name);
                                    if (item.Function == "Maths")
                                    {
                                        Maths Maths = new Maths();
                                        Maths parameters = (Maths)javaScriptSerializ­er.Deserialize(jparameters, typeof(Maths));
                                        MathString = Maths.Output(jparameters,jCategory,group.ID,item.ID,MathString,PowOpen);
                                        PowOpen = Maths.PowOpen(jparameters, PowOpen);
                                        if (paramCount == item.Parameter.Count)
                                        {
                                            Expression e = new Expression(MathString);
                                            var Calculation = e.Evaluate();
                                            bool DeciParse;
                                            decimal CalculationDeci;
                                            string Rounding;
                                            DeciParse = decimal.TryParse(Convert.ToString(Calculation), out CalculationDeci);
                                            Rounding = Convert.ToString(parameters.Rounding);

                                            if (Rounding == null || Rounding == "")
                                            {
                                                Rounding = "2";
                                            }
                                            if (DeciParse == true)
                                            {
                                                decimal Output = CalculationDeci;
                                                MathematicalFunctions MathematicalFunctions = new MathematicalFunctions();

                                                try
                                                {
                                                    Output = MathematicalFunctions.Rounding(Convert.ToString(parameters.RoundingType), Rounding, Output);
                                                }
                                                catch (Exception ex)
                                                {
                                                    logger.Error(ex);
                                                    throw new HttpException(ex.ToString());
                                                }

                                                item.Output = Convert.ToString(Output);
                                            }
                                            else
                                            {
                                                item.Output = "0";
                                            }
                                        }
                                        paramCount = paramCount + 1;
                                    }
                                    else if (item.Function == "ErrorsWarnings")
                                    {
                                        ErrorsWarnings Errors = new ErrorsWarnings();
                                        ErrorsWarnings parameters = (ErrorsWarnings)javaScriptSerializ­er.Deserialize(jparameters, typeof(ErrorsWarnings));
                                        item.Name = parameters.Type;
                                        item.Output = parameters.String1;
                                    }
                                    else if (item.Function == "Comments")
                                    {
                                        Comments Errors = new Comments();
                                        Comments parameters = (Comments)javaScriptSerializ­er.Deserialize(jparameters, typeof(Comments));
                                        item.Output = parameters.String1;
                                    }
                                    else if (item.Function == "Period")
                                    {
                                        DateFunctions DateFunctions = new DateFunctions();
                                        Period Periods = new Period();
                                        try
                                        {
                                            item.Output = Periods.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "Factors")
                                    {
                                        Factors Factors = new Factors();
                                        Factors parameters = (Factors)javaScriptSerializ­er.Deserialize(jparameters, typeof(Factors));
                                        try
                                        {
                                            item.Output = Factors.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }

                                        item.Type = parameters.OutputType;
                                    }
                                    else if (item.Function == "DateAdjustment")
                                    {
                                        Dates Dates = new Dates();
                                        try
                                        {
                                            item.Output = Dates.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "DatePart")
                                    {
                                        DatePart DateParts = new DatePart();
                                        try
                                        {
                                            item.Output = DateParts.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }

                                    else if (item.Function == "MathsFunctions")
                                    {
                                        MathsFunctions MathsFunctions = new MathsFunctions();
                                        try
                                        {
                                            item.Output = MathsFunctions.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "ArrayFunctions")
                                    {
                                        ArrayFunctions ArrayFunctions = new ArrayFunctions();
                                        ArrayFunctions parameters = (ArrayFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(ArrayFunctions));
                                        try
                                        {
                                            item.Output = ArrayFunctions.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }

                                        if(parameters.Function == "Count")
                                        {
                                            item.Type = "Decimal";
                                        }
                                        else
                                        {
                                            item.Type = parameters.LookupType;
                                        }

                                    }
                                    else if (item.Function == "StringFunctions")
                                    {
                                        StringFunctions StringFunctions = new StringFunctions();
                                        StringFunctions  parameters = (StringFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(StringFunctions));
                                        try
                                        {
                                            item.Output = StringFunctions.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }

                                        if(parameters.Type == "Len")
                                        {
                                            item.Type = "Decimal";
                                        }
                                        else
                                        {
                                            item.Type = "String";
                                        }
                                    }
                                }
                                //Expected results on the builder this sets the required ones
                                if (item.ExpectedResult == null || item.ExpectedResult == "")
                                {
                                    item.Pass = "******";
                                }
                                else if (item.ExpectedResult == item.Output)
                                {
                                    item.Pass = "******";
                                }
                                else
                                {
                                    item.Pass = "******";
                                }
                                OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name });
                            }
                            else
                            {
                                //Ignores the row if logic is not met
                                dynamic LogicReplace = Config.VariableReplace(jCategory, item.Name, group.ID, item.ID);

                                if(Convert.ToString(LogicReplace) == Convert.ToString(item.Name))
                                {
                                    item.Output = null;
                                }
                                else
                                {
                                    item.Output = Convert.ToString(LogicReplace);
                                }
                                item.Pass = "******";

                                OutputList.Add(new OutputList { ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name });
                            }
                        }
                    }
                }
            }
        }
        /// <summary>Runs the Array Decimal calculation.
        /// <para>parameters = JSON congifurations relating to this function</para>
        /// <para>InputA = value to be passed into the calculation</para>
        /// </summary>
        public string DecimalCalculation(ArrayFunctions parameters, dynamic InputA)
        {
            MathematicalFunctions MathFunctions = new MathematicalFunctions();
            //Sums up all values in the array
            if (parameters.Function == "Sum")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                decimal Sum = 0;

                foreach (string output in Deci1parts)
                {
                    decimal InputADeci = 0;
                    decimal.TryParse(output, out InputADeci);
                    Sum = decimal.Add(Sum, InputADeci);
                }
                return Convert.ToString(Sum);
            }
            //Multiplies all values in the array
            else if (parameters.Function == "Product")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                decimal Sum = 0;
                int counter = 0;
                foreach (string output in Deci1parts)
                {
                    decimal InputADeci = 0;
                    decimal.TryParse(output, out InputADeci);
                    if (counter == 0)
                    {
                        Sum = InputADeci;
                    }
                    else
                    {
                        Sum = decimal.Multiply(Sum, InputADeci);
                    }

                    counter = counter + 1;
                }
                return Convert.ToString(Sum);
            }
            //Gets the first number in the array
            else if (parameters.Function == "FirstNumber")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                return Convert.ToString(Deci1parts[0]);
            }
            //Gets the last number in the array
            else if (parameters.Function == "LastNumber")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                return Convert.ToString(Deci1parts[Deci1parts.GetUpperBound(0)]);
            }
            //Calculates Total Period in the array
            else if (parameters.Function == "TotalPeriod")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                decimal Sum = 0;
                int counter = 0;
                bool firstpass = false;
                foreach (string output in Deci1parts)
                {
                    if(firstpass == false)
                    {
                        if(Deci1parts.Length > 1)
                        {
                            Sum = MathFunctions.AddPeriod(Convert.ToDecimal(output), Convert.ToDecimal(Deci1parts[counter + 1]), parameters.PeriodType);
                        }
                        else
                        {
                            Sum = Convert.ToDecimal(output);
                        }

                        firstpass = true;
                    }
                    else if (Deci1parts.Length > 2 && counter >= 2)
                    {
                        Sum = MathFunctions.AddPeriod(Sum, Convert.ToDecimal(output), parameters.PeriodType);
                    }

                    counter = counter + 1;
                }

                if(parameters.PeriodType == "YearsDays")
                {
                    return MathFunctions.RoundingDecimalPlaces("3", Sum);
                }
                else
                {
                    return MathFunctions.RoundingDecimalPlaces("2", Sum);
                }

            }
            //Counts the number or values in the array
            else if (parameters.Function == "Count")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                return Convert.ToString(Deci1parts.Length);
            }
            else
            {
                return Convert.ToString(0);
            }
        }
        /// <summary>Outputs where Array Functions function is used, includes the array builder.
        /// <para>jparameters = JSON congifurations relating to this function</para>
        /// <para>jCategory = the whole configuration which is required to do the variable replace</para>
        /// <para>GroupID = current Group ID</para>
        /// <para>ItemID = current row ID</para>
        /// </summary>
        public string Output(string jparameters, List<CategoryViewModel> jCategory, int GroupID, int ItemID)
        {
            CalculationCSharp.Areas.Configuration.Models.ConfigFunctions Config = new CalculationCSharp.Areas.Configuration.Models.ConfigFunctions();
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            MathematicalFunctions MathFunctions = new MathematicalFunctions();
            ArrayFunctions parameters = (ArrayFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(ArrayFunctions));
            dynamic InputA = Config.VariableReplace(jCategory, parameters.LookupValue, GroupID, ItemID);
            string OutputValue = null;

            if (parameters.LookupType == "Decimal")
            {
                OutputValue = DecimalCalculation(parameters, InputA);
            }
            else if(parameters.LookupType == "Date")
            {
                OutputValue = DateCalculation(parameters, InputA);
            }
            else
            {
                OutputValue = Convert.ToString(0);
            }
            return Convert.ToString(OutputValue);
        }
        /// <summary>Runs the Maths Functions calculation.
        /// <para>parameters = JSON congifurations relating to this function</para>
        /// <para>InputADeci = value to be passed into the calculation</para>
        /// <para>InputBDeci = value to be passed into the calculation</para>
        /// </summary>
        public decimal Calculate(MathsFunctions parameters, dynamic InputADeci, dynamic InputBDeci)
        {
            MathematicalFunctions MathFunctions = new MathematicalFunctions();
            Decimal Output = 0;

            Rounding = Convert.ToString(parameters.Rounding);

            if (Rounding == null || Rounding == "")
            {
                Rounding = "2";
            }

            //Absolute
            if (parameters.Type == "Abs")
            {
                Output = MathFunctions.Abs(InputADeci);
            }
            //Add
            else if (parameters.Type == "Add")
            {
                Output = MathFunctions.Add(InputADeci, InputBDeci);
            }
            //Add Period
            else if (parameters.Type == "AddPeriod")
            {
                Output = MathFunctions.AddPeriod(InputADeci, InputBDeci, parameters.PeriodType);
                Rounding = "2";
                if (parameters.PeriodType == "YearsDays")
                {
                    Rounding = "3";
                }
            }
            //Ceiling
            else if (parameters.Type == "Ceiling")
            {
                Output = MathFunctions.Ceiling(InputADeci);
            }
            //Get decimal part only
            else if (parameters.Type == "DecimalPart")
            {
                Output = MathFunctions.DecimalPart(InputADeci);
            }
            //Divide
            else if (parameters.Type == "Divide")
            {
                Output = MathFunctions.Divide(InputADeci, InputBDeci);
            }
            //Floor to lowest integer
            else if (parameters.Type == "Floor")
            {
                Output = MathFunctions.Floor(InputADeci);
            }
            //Max of two values
            else if (parameters.Type == "Max")
            {
                Output = MathFunctions.Max(InputADeci, InputBDeci);
            }
            //Min of two values
            else if (parameters.Type == "Min")
            {
                Output = MathFunctions.Min(InputADeci, InputBDeci);
            }
            //Multiply of two values
            else if (parameters.Type == "Multiply")
            {
                Output = MathFunctions.Multiply(InputADeci, InputBDeci);
            }
            //Power of two value
            else if (parameters.Type == "Power")
            {
                Output = MathFunctions.Power(InputADeci, InputBDeci);
            }
            //Subtract two value
            else if (parameters.Type == "Subtract")
            {
                Output = MathFunctions.Subtract(InputADeci, InputBDeci);
            }
            //Subtract two period value
            else if (parameters.Type == "SubtractPeriod")
            {
                Output = MathFunctions.SubtractPeriod(InputADeci, InputBDeci, parameters.PeriodType);
                Rounding = "2";
                if (parameters.PeriodType == "YearsDays")
                {
                    Rounding = "3";
                }
            }
            //Truncate
            else if (parameters.Type == "Truncate")
            {
                Output = MathFunctions.Truncate(InputADeci);
            }
            return MathFunctions.Rounding(parameters.RoundingType, Rounding, Output);
        }
        /// <summary>Runs the Maths Functions calculation.
        /// <para>parameters = JSON congifurations relating to this function</para>
        /// <para>InputADeci = value to be passed into the calculation</para>
        /// <para>InputBDeci = value to be passed into the calculation</para>
        /// </summary>
        public decimal Calculate(MathsFunctions parameters, dynamic InputADeci, dynamic InputBDeci, int ItemID, CategoryViewModel group)
        {
            MathematicalFunctions MathFunctions = new MathematicalFunctions();
            Decimal Output = 0;

            Rounding = Convert.ToString(parameters.Rounding);

            if (Rounding == null || Rounding == "")
            {
                Rounding = "2";
            }

            //Absolute
            if (parameters.Type == "Abs")
            {
                Output = MathFunctions.Abs(InputADeci);
            }
            //Add
            else if (parameters.Type == "Add")
            {
                Output = MathFunctions.Add(InputADeci, InputBDeci);
            }
            //Add Period
            else if (parameters.Type == "AddPeriod")
            {
                Output   = MathFunctions.AddPeriod(InputADeci, InputBDeci, parameters.PeriodType);
                Rounding = "2";
                if (parameters.PeriodType == "YearsDays")
                {
                    Rounding = "3";
                }
            }
            //Ceiling
            else if (parameters.Type == "Ceiling")
            {
                Output = MathFunctions.Ceiling(InputADeci);
            }
            //Get decimal part only
            else if (parameters.Type == "DecimalPart")
            {
                Output = MathFunctions.DecimalPart(InputADeci);
            }
            //Divide
            else if (parameters.Type == "Divide")
            {
                Output = MathFunctions.Divide(InputADeci, InputBDeci);
            }
            //Floor to lowest integer
            else if (parameters.Type == "Floor")
            {
                Output = MathFunctions.Floor(InputADeci);
            }
            //Max of two values
            else if (parameters.Type == "Max")
            {
                Output = MathFunctions.Max(InputADeci, InputBDeci);
            }
            //Min of two values
            else if (parameters.Type == "Min")
            {
                Output = MathFunctions.Min(InputADeci, InputBDeci);
            }
            //Multiply of two values
            else if (parameters.Type == "Multiply")
            {
                Output = MathFunctions.Multiply(InputADeci, InputBDeci);
            }
            //Power of two value
            else if (parameters.Type == "Power")
            {
                Output = MathFunctions.Power(InputADeci, InputBDeci);
            }
            //Subtract two value
            else if (parameters.Type == "Subtract")
            {
                Output = MathFunctions.Subtract(InputADeci, InputBDeci);
            }
            //Subtract two value
            else if (parameters.Type == "SumDecimalAbove")
            {
                var filterList = group.Functions.Where((l, index) => index >= (ItemID - InputADeci) && index < ItemID);
                Output = 0;
                foreach (var row  in filterList)
                {
                    if (row.Type == "Decimal" && row.Pass != "miss")
                    {
                        Output = Output + Convert.ToDecimal(row.Output);
                    }
                }
            }
            //Subtract two period value
            else if (parameters.Type == "SubtractPeriod")
            {
                Output   = MathFunctions.SubtractPeriod(InputADeci, InputBDeci, parameters.PeriodType);
                Rounding = "2";
                if (parameters.PeriodType == "YearsDays")
                {
                    Rounding = "3";
                }
            }
            //Truncate
            else if (parameters.Type == "Truncate")
            {
                Output = MathFunctions.Truncate(InputADeci);
            }
            return(MathFunctions.Rounding(parameters.RoundingType, Rounding, Output));
        }
Exemple #11
0
        //Calcuation Controller Action
        public void CalculateAction(List <CategoryViewModel> jCategory)
        {
            foreach (var group in jCategory)
            {
                foreach (var item in group.Functions)
                {
                    if (item.Function == "Input")
                    {
                        item.Output = InputFunctions.Output(item.Type, item.Output);
                        OutputList.Add(new OutputList {
                            ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name
                        });
                    }
                    else
                    {
                        //Logic check at Column Level
                        string colLogic      = null;
                        bool   colLogicParse = true;
                        if (group.Logic != null)
                        {
                            foreach (var bit in group.Logic)
                            {
                                var    grouplastLogic         = group.Logic.Last();
                                string grouplastLogicOperator = grouplastLogic.Operator;
                                Logic  Logic = new Logic();
                                colLogic = Logic.Output(jCategory, bit, group.ID, 0);
                                Expression ex = new Expression(colLogic);
                                try
                                {
                                    colLogicParse = Convert.ToBoolean(ex.Evaluate());
                                }
                                catch (Exception exception)
                                {
                                    logger.Error(exception);
                                    throw new HttpException(exception.ToString());
                                }

                                if (grouplastLogicOperator == "AND" && colLogicParse == false)
                                {
                                    break;
                                }
                                else if (grouplastLogicOperator == "OR" && colLogicParse == true)
                                {
                                    colLogicParse = true;
                                    break;
                                }
                            }
                        }
                        if (item.Parameter.Count > 0)
                        {
                            string logic      = null;
                            bool   logicparse = true;
                            string MathString = null;
                            bool   PowOpen    = false;
                            //Logic check at column level
                            if (colLogicParse == true)
                            {
                                foreach (var bit in item.Logic)
                                {
                                    var    lastLogic         = item.Logic.Last();
                                    string lastLogicOperator = lastLogic.Operator;
                                    Logic  Logic             = new Logic();
                                    logic = Logic.Output(jCategory, bit, group.ID, item.ID);
                                    Expression ex = new Expression(logic);

                                    try
                                    {
                                        logicparse = Convert.ToBoolean(ex.Evaluate());
                                    }
                                    catch (Exception exception)
                                    {
                                        logger.Error(exception);
                                        throw new HttpException(exception.ToString());
                                    }

                                    if (lastLogicOperator == "AND" && logicparse == false)
                                    {
                                        break;
                                    }
                                    else if (lastLogicOperator == "OR" && logicparse == true)
                                    {
                                        logicparse = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                logicparse = false;
                            }
                            //Run code if logic if met at column and row level
                            if (logicparse == true)
                            {
                                int paramCount = 1;
                                foreach (var param in item.Parameter)
                                {
                                    string jparameters = Newtonsoft.Json.JsonConvert.SerializeObject(param);
                                    logger.Debug("Column Name(" + group.ID + ") - " + group.Name + " || Row Name(" + item.ID + ") - " + item.Name);
                                    if (item.Function == "Maths")
                                    {
                                        Maths Maths      = new Maths();
                                        Maths parameters = (Maths)javaScriptSerializ­er.Deserialize(jparameters, typeof(Maths));
                                        MathString = Maths.Output(jparameters, jCategory, group.ID, item.ID, MathString, PowOpen);
                                        PowOpen    = Maths.PowOpen(jparameters, PowOpen);
                                        if (paramCount == item.Parameter.Count)
                                        {
                                            Expression e           = new Expression(MathString);
                                            var        Calculation = e.Evaluate();
                                            bool       DeciParse;
                                            decimal    CalculationDeci;
                                            string     Rounding;
                                            DeciParse = decimal.TryParse(Convert.ToString(Calculation), out CalculationDeci);
                                            Rounding  = Convert.ToString(parameters.Rounding);

                                            if (Rounding == null || Rounding == "")
                                            {
                                                Rounding = "2";
                                            }
                                            if (DeciParse == true)
                                            {
                                                decimal Output = CalculationDeci;
                                                MathematicalFunctions MathematicalFunctions = new MathematicalFunctions();

                                                try
                                                {
                                                    Output = MathematicalFunctions.Rounding(Convert.ToString(parameters.RoundingType), Rounding, Output);
                                                }
                                                catch (Exception ex)
                                                {
                                                    logger.Error(ex);
                                                    throw new HttpException(ex.ToString());
                                                }

                                                item.Output = Convert.ToString(Output);
                                            }
                                            else
                                            {
                                                item.Output = "0";
                                            }
                                        }
                                        paramCount = paramCount + 1;
                                    }
                                    else if (item.Function == "ErrorsWarnings")
                                    {
                                        ErrorsWarnings Errors     = new ErrorsWarnings();
                                        ErrorsWarnings parameters = (ErrorsWarnings)javaScriptSerializ­er.Deserialize(jparameters, typeof(ErrorsWarnings));
                                        item.Name   = parameters.Type;
                                        item.Output = parameters.String1;
                                    }
                                    else if (item.Function == "Comments")
                                    {
                                        Comments Errors     = new Comments();
                                        Comments parameters = (Comments)javaScriptSerializ­er.Deserialize(jparameters, typeof(Comments));
                                        item.Output = parameters.String1;
                                    }
                                    else if (item.Function == "Period")
                                    {
                                        DateFunctions DateFunctions = new DateFunctions();
                                        Period        Periods       = new Period();
                                        try
                                        {
                                            item.Output = Periods.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "Factors")
                                    {
                                        Factors Factors    = new Factors();
                                        Factors parameters = (Factors)javaScriptSerializ­er.Deserialize(jparameters, typeof(Factors));
                                        try
                                        {
                                            item.Output = Factors.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }

                                        item.Type = parameters.OutputType;
                                    }
                                    else if (item.Function == "DateAdjustment")
                                    {
                                        Dates Dates = new Dates();
                                        try
                                        {
                                            item.Output = Dates.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "DatePart")
                                    {
                                        DatePart DateParts = new DatePart();
                                        try
                                        {
                                            item.Output = DateParts.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "Return")
                                    {
                                        Return Return = new Return();
                                        try
                                        {
                                            item.Output = Return.Output(jparameters, jCategory, group.ID, item.ID);
                                            OutputList.Add(new OutputList {
                                                ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name
                                            });
                                            return;
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "MathsFunctions")
                                    {
                                        MathsFunctions MathsFunctions = new MathsFunctions();
                                        try
                                        {
                                            item.Output = MathsFunctions.Output(jparameters, jCategory, group.ID, item.ID, group);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }
                                    }
                                    else if (item.Function == "ArrayFunctions")
                                    {
                                        ArrayFunctions ArrayFunctions = new ArrayFunctions();
                                        ArrayFunctions parameters     = (ArrayFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(ArrayFunctions));
                                        try
                                        {
                                            item.Output = ArrayFunctions.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }


                                        if (parameters.Function == "Count")
                                        {
                                            item.Type = "Decimal";
                                        }
                                        else
                                        {
                                            item.Type = parameters.LookupType;
                                        }
                                    }
                                    else if (item.Function == "StringFunctions")
                                    {
                                        StringFunctions StringFunctions = new StringFunctions();
                                        StringFunctions parameters      = (StringFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(StringFunctions));
                                        try
                                        {
                                            item.Output = StringFunctions.Output(jparameters, jCategory, group.ID, item.ID);
                                        }
                                        catch (Exception ex)
                                        {
                                            logger.Error(ex);
                                            throw new HttpException(ex.ToString());
                                        }

                                        if (parameters.Type == "Len")
                                        {
                                            item.Type = "Decimal";
                                        }
                                        else
                                        {
                                            item.Type = "String";
                                        }
                                    }
                                    else if (item.Function == "Function")
                                    {
                                        Function Functions  = new Function();
                                        Function parameters = (Function)javaScriptSerializ­er.Deserialize(jparameters, typeof(Function));
                                        FunctionConfiguration    calcFunction       = db.FunctionConfiguration.Find(Convert.ToInt32(parameters.ID));
                                        List <CategoryViewModel> calcFunctionConfig = (List <CategoryViewModel>)javaScriptSerializ­er.Deserialize(calcFunction.Configuration, typeof(List <CategoryViewModel>));
                                        //replace inputs from main configuration to function
                                        foreach (var row in calcFunctionConfig[0].Functions)
                                        {
                                            int index = parameters.Input.FindIndex(a => a.Name == row.Name);
                                            if (index >= 0)
                                            {
                                                row.Output = parameters.Input[index].Output;
                                            }
                                            else
                                            {
                                                row.Output = null;
                                            }
                                        }

                                        foreach (var input in calcFunctionConfig[0].Functions)
                                        {
                                            if (input.Output != null)
                                            {
                                                input.Output = Convert.ToString(Functions.Output(jparameters, jCategory, group.ID, item.ID, input.Output, input.Type));
                                            }
                                        }
                                        //run the function with the new inputs
                                        Calculate Calculate = new Calculate();
                                        calcFunctionConfig = Calculate.DebugResults(calcFunctionConfig);
                                        item.SubOutput     = Calculate.OutputResults(calcFunctionConfig);
                                        foreach (var col in calcFunctionConfig)
                                        {
                                            int index = col.Functions.FindIndex(a => a.Function == "Return");
                                            if (index >= 0)
                                            {
                                                item.Output = col.Functions[index].Output;
                                                foreach (var thing in col.Functions[index].Parameter)
                                                {
                                                    foreach (var test in thing)
                                                    {
                                                        if (test.Key == "Datatype")
                                                        {
                                                            item.Type = test.Value;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                //Expected results on the builder this sets the required ones
                                if (item.ExpectedResult == null || item.ExpectedResult == "")
                                {
                                    item.Pass = "******";
                                }
                                else if (item.ExpectedResult == item.Output)
                                {
                                    item.Pass = "******";
                                }
                                else
                                {
                                    item.Pass = "******";
                                }
                                OutputList.Add(new OutputList {
                                    ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name, SubOutput = item.SubOutput
                                });
                            }
                            else
                            {
                                //Ignores the row if logic is not met
                                dynamic LogicReplace = Config.VariableReplace(jCategory, item.Name, group.ID, item.ID);

                                if (Convert.ToString(LogicReplace) == Convert.ToString(item.Name))
                                {
                                    item.Output = null;
                                }
                                else
                                {
                                    item.Output = Convert.ToString(LogicReplace);
                                }
                                item.Pass = "******";

                                OutputList.Add(new OutputList {
                                    ID = Convert.ToString(item.ID), Field = item.Name, Value = item.Output, Group = group.Name, SubOutput = item.SubOutput
                                });
                            }
                        }
                    }
                }
            }
        }
        /// <summary>Runs the Array Decimal calculation.
        /// <para>parameters = JSON congifurations relating to this function</para>
        /// <para>InputA = value to be passed into the calculation</para>
        /// </summary>
        public string DecimalCalculation(ArrayFunctions parameters, dynamic InputA)
        {
            MathematicalFunctions MathFunctions = new MathematicalFunctions();

            //Sums up all values in the array
            if (parameters.Function == "Sum")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                decimal Sum = 0;

                foreach (string output in Deci1parts)
                {
                    decimal InputADeci = 0;
                    decimal.TryParse(output, out InputADeci);
                    Sum = decimal.Add(Sum, InputADeci);
                }
                return(Convert.ToString(Sum));
            }
            //Multiplies all values in the array
            else if (parameters.Function == "Product")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                decimal Sum     = 0;
                int     counter = 0;
                foreach (string output in Deci1parts)
                {
                    decimal InputADeci = 0;
                    decimal.TryParse(output, out InputADeci);
                    if (counter == 0)
                    {
                        Sum = InputADeci;
                    }
                    else
                    {
                        Sum = decimal.Multiply(Sum, InputADeci);
                    }

                    counter = counter + 1;
                }
                return(Convert.ToString(Sum));
            }
            //Gets the first number in the array
            else if (parameters.Function == "FirstNumber")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                return(Convert.ToString(Deci1parts[0]));
            }
            //Gets the last number in the array
            else if (parameters.Function == "LastNumber")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                return(Convert.ToString(Deci1parts[Deci1parts.GetUpperBound(0)]));
            }
            //Calculates Total Period in the array
            else if (parameters.Function == "TotalPeriod")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                decimal Sum       = 0;
                int     counter   = 0;
                bool    firstpass = false;
                foreach (string output in Deci1parts)
                {
                    if (firstpass == false)
                    {
                        if (Deci1parts.Length > 1)
                        {
                            Sum = MathFunctions.AddPeriod(Convert.ToDecimal(output), Convert.ToDecimal(Deci1parts[counter + 1]), parameters.PeriodType);
                        }
                        else
                        {
                            Sum = Convert.ToDecimal(output);
                        }

                        firstpass = true;
                    }
                    else if (Deci1parts.Length > 2 && counter >= 2)
                    {
                        Sum = MathFunctions.AddPeriod(Sum, Convert.ToDecimal(output), parameters.PeriodType);
                    }

                    counter = counter + 1;
                }

                if (parameters.PeriodType == "YearsDays")
                {
                    return(MathFunctions.RoundingDecimalPlaces("3", Sum));
                }
                else
                {
                    return(MathFunctions.RoundingDecimalPlaces("2", Sum));
                }
            }
            //Counts the number or values in the array
            else if (parameters.Function == "Count")
            {
                string[] Deci1parts = null;
                Deci1parts = InputA.Split('~');
                return(Convert.ToString(Deci1parts.Length));
            }
            else
            {
                return(Convert.ToString(0));
            }
        }
        /// <summary>Outputs where String Functions function is used, includes the array builder.
        /// <para>jparameters = JSON congifurations relating to this function</para>
        /// <para>jCategory = the whole configuration which is required to do the variable replace</para>
        /// <para>GroupID = current Group ID</para>
        /// <para>ItemID = current row ID</para>
        /// </summary>
        public string Output(string jparameters, List<CategoryViewModel> jCategory, int GroupID, int ItemID)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            MathematicalFunctions MathFunctions = new MathematicalFunctions();
            ArrayBuildingFunctions ArrayBuilder = new ArrayBuildingFunctions();
            StringFunctions parameters = (StringFunctions)javaScriptSerializ­er.Deserialize(jparameters, typeof(StringFunctions));
            string[] Numbers1parts = null;
            string[] Numbers2parts = null;
            string[] Numbers3parts = null;
            //Returns array
            Numbers1parts = ArrayBuilder.InputArrayBuilder(parameters.String1, jCategory, GroupID, ItemID);
            Numbers2parts = ArrayBuilder.InputArrayBuilder(parameters.Number1, jCategory, GroupID, ItemID);
            Numbers3parts = ArrayBuilder.InputArrayBuilder(parameters.String3, jCategory, 0, 0);
            string Output = null;
            string OutputValue = null;
            //Gets Max Length of array so loops through all values
            int MaxLength = ArrayBuilder.GetMaxLength(Numbers1parts, Numbers2parts);
            //If Set only then
            if(Numbers3parts != null)
            {
                MaxLength = 1;
            }
            int Counter = 0;
            //Loop through the array to calculate each value in array
            for (int i = 0; i < MaxLength; i++)
            {
                dynamic InputA = null;
                dynamic InputB = null;
                dynamic InputC = null;
                //Gets the current array to use in the loop
                InputA = ArrayBuilder.GetArrayPart(Numbers1parts, Counter);
                InputB = ArrayBuilder.GetArrayPart(Numbers2parts, Counter);
                InputC = ArrayBuilder.GetArrayPart(Numbers3parts, Counter);

                string InputAString = null;
                int InputBDeci = 0;
                string InputCString = null;

                InputAString = Convert.ToString(InputA);
                int.TryParse(InputB, out InputBDeci);
                InputCString = Convert.ToString(InputC);
                //Calculates the value required
                if (parameters.Type == "Left")
                {
                    OutputValue = InputAString.Substring(0, InputBDeci);
                }
                else if (parameters.Type == "Right")
                {
                    OutputValue = InputAString.Substring(InputAString.Length - InputBDeci);
                }
                else if (parameters.Type == "Set")
                {
                    OutputValue = InputCString;
                }
                else if (parameters.Type == "Find")
                {
                    int FindValue = InputAString.IndexOf(parameters.String2);

                    if( FindValue != -1)
                    {
                        OutputValue = Convert.ToString(FindValue);
                    }
                    else
                    {
                        OutputValue = "0";
                    }
                }
                else if (parameters.Type == "Len")
                {
                    OutputValue = Convert.ToString(InputAString.Length);
                }
                Output = Output + OutputValue + "~";
                Counter = Counter + 1;
            }
            Output = Output.Remove(Output.Length - 1);
            return Convert.ToString(Output);
        }