Exemplo n.º 1
0
Arquivo: Costs.cs Projeto: jakedw7/iAM
        internal void SetFunction(string equation)
        {
            _costEquation = equation;

            // Get list of attributes
            _attributesEquation = SimulationMessaging.ParseAttribute(_costEquation);
            if (_attributesEquation == null)
            {
                return;
            }

            if (_calculate == null)
            {
                _calculate = new CalculateEvaluate.CalculateEvaluate();

                //Allow functions to utilize strings and dates.
                string        functionEquation = _costEquation;
                List <string> attributes       = SimulationMessaging.ParseAttribute(functionEquation);

                foreach (String attribute in attributes)
                {
                    String attributeType = SimulationMessaging.GetAttributeType(attribute);
                    if (attributeType == "STRING")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[@" + attribute + "]";
                        functionEquation = functionEquation.Replace(oldValue, newValue);
                    }
                    else if (attributeType == "DATETIME")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[$" + attribute + "]";
                        functionEquation = functionEquation.Replace(oldValue, newValue);
                    }
                }

                _calculate.BuildFunctionClass(functionEquation, "double", cgOMS.Prefix + "COSTS_BINARY_EQUATION_" + CostID);
                if (_calculate.m_cr == null)
                {
                    _compilerResultsEquation = _calculate.CompileAssembly();
                    SimulationMessaging.SaveSerializedCalculateEvaluate(cgOMS.Prefix + "COSTS", "BINARY_EQUATION", CostID, _calculate);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the function version of consequences.
        /// </summary>
        /// <param name="equation"></param>
        public void SetFunction(string equation)
        {
            _consequenceEquation = equation;
            IsEquation           = true;
            _attributesEquation  = SimulationMessaging.ParseAttribute(_consequenceEquation);
            if (_calculate == null)
            {
                _calculate = new CalculateEvaluate.CalculateEvaluate();
                string functionEquation = _consequenceEquation;

                foreach (String attribute in _attributesEquation)
                {
                    String attributeType = SimulationMessaging.GetAttributeType(attribute);
                    if (attributeType == "STRING")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[@" + attribute + "]";
                        functionEquation = functionEquation.Replace(oldValue, newValue);
                    }
                    else if (attributeType == "DATETIME")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[$" + attribute + "]";
                        functionEquation = functionEquation.Replace(oldValue, newValue);
                    }
                }

                _calculate.BuildFunctionClass(_consequenceEquation, "double", cgOMS.Prefix + "CONSEQUENCES_BINARY_EQUATION_" + _id);

                if (_calculate.m_cr == null)
                {
                    _compilerResultsEquation = _calculate.CompileAssembly();
                    SimulationMessaging.SaveSerializedCalculateEvaluate(cgOMS.Prefix + "CONSEQUENCES", "BINARY_EQUATION", _id, _calculate);
                }
            }
        }
Exemplo n.º 3
0
        public static object ConvertToRoadCareObject(object value, string attribute)
        {
            string type = "";

            switch (attribute)
            {
            case "LENGTH":
            case "AREA":
                type = "NUMBER";
                break;

            default:
                type = SimulationMessaging.GetAttributeType(attribute);
                break;
            }
            object rcValue = null;

            if (type == "STRING")
            {
                if (value == null)
                {
                    rcValue = null;
                }
                else
                {
                    rcValue = value.ToString();
                }
            }
            else if (type == "DATETIME")
            {
                if (value == null)
                {
                    rcValue = null;
                }
                else
                {
                    rcValue = Convert.ToDateTime(value);
                }
            }
            else
            {
                if (value == null)
                {
                    rcValue = null;
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(value.ToString()))
                    {
                        rcValue = null;
                    }
                    else //At this point this should only contain valid numbers, catch block is in case.
                    {
                        try
                        {
                            rcValue = Convert.ToDouble(value);
                        }
                        catch
                        {
                            rcValue = null;
                        }
                    }
                }
            }
            return(rcValue);
        }
Exemplo n.º 4
0
        internal void SetFunction(string equation)
        {
            _equation = equation;

            // Get list of attributes
            _attributesEquation = SimulationMessaging.ParseAttribute(_equation);
            if (_attributesEquation == null)
            {
                return;
            }

            if (_attributesEquation.Count == 1 && _attributesEquation[0].ToUpper() == "AGE")
            {
                _isAgeOnly = true;
            }

            if (_equation.Length >= 6 && _equation.Substring(0, 6) == "MODULE")
            {
                IsModule = true;
                //this will crash the program for equations with less than 10 characters
                //if (m_strEquation.Substring(0, 6) == "MODULE")
                if (_equation.Length >= 10 && _equation.Substring(7, 3) == "CRS")
                {
                    IsCRS = true;
                }
                else if (_equation.Length >= 10 && _equation.Substring(7, 3) == "OCI")
                {
                    IsOCI = true;
                }
                return;
            }
            else
            {
                if (_calculate == null)
                {
                    _calculate = new CalculateEvaluate.CalculateEvaluate();

                    //Allow functions to utilize strings and dates.
                    string        functionEquation = _equation;
                    List <string> attributes       = SimulationMessaging.ParseAttribute(functionEquation);

                    foreach (String attribute in attributes)
                    {
                        String attributeType = SimulationMessaging.GetAttributeType(attribute);
                        if (attributeType == "STRING")
                        {
                            String oldValue = "[" + attribute + "]";
                            String newValue = "[@" + attribute + "]";
                            functionEquation = functionEquation.Replace(oldValue, newValue);
                        }
                        else if (attributeType == "DATETIME")
                        {
                            String oldValue = "[" + attribute + "]";
                            String newValue = "[$" + attribute + "]";
                            functionEquation = functionEquation.Replace(oldValue, newValue);
                        }
                    }
                    _calculate.BuildFunctionClass(functionEquation, "double", cgOMS.Prefix + _table + "_BINARY_EQUATION_" + _performanceID);

                    if (_calculate.m_cr == null)
                    {
                        _compilerResultsEquation = _calculate.CompileAssembly();
                        if (_table != null)
                        {
                            SimulationMessaging.SaveSerializedCalculateEvaluate(cgOMS.Prefix + _table, "BINARY_EQUATION", _performanceID, _calculate);
                        }
                    }

                    if (_calculate.m_listError.Count > 0)
                    {
                        foreach (String str in _calculate.m_listError)
                        {
                            _errors.Add(str);
                        }
                    }
                }
                //If [AGE] is the only variable.  This only needs to be solved once so might
                //as well solve it right now.
                if (_isAgeOnly)
                {
                    Hashtable hash = new Hashtable();
                    hash.Add("AGE", "0");
                    this.Solve(hash);
                }
            }
        }
Exemplo n.º 5
0
        public static string GetDecisionEngineCritera(string omsCriteria)
        {
            omsCriteria = omsCriteria.Replace("[]", "cgde_timeperiodof"); // Square brackets denote variables.  [] messes this up.
            string        deCriteria  = "";
            List <string> expressions = new List <string>();              //List of omsExpressions

            int lastOpen = -1;
            int i        = 0;

            while (i < omsCriteria.Length)//Find all expressions
            {
                if (omsCriteria[i] == '(')
                {
                    lastOpen = i;
                }
                if (omsCriteria[i] == ')' && lastOpen >= 0)
                {
                    string expression = omsCriteria.Substring(lastOpen, i - lastOpen + 1);
                    if (expression.Contains("["))
                    {
                        byte   numberExpressions = (byte)expressions.Count;
                        string placeHolder       = numberExpressions.ToString("x2");
                        expressions.Add(expression);
                        omsCriteria = omsCriteria.Replace(expression, placeHolder);
                        i           = 0;
                    }
                    lastOpen = -1;
                }
                else
                {
                    i++;
                }
            }

            omsCriteria = omsCriteria.Replace("AND", "&&").Replace("OR", "||").Replace("NOT", "!");

            if (CheckMatchingParentheses(omsCriteria))
            {
                byte index = 0;
                deCriteria = omsCriteria;
                foreach (string expression in expressions)
                {
                    string variable  = GetVariable(expression);
                    string fieldType = SimulationMessaging.GetAttributeType(variable);
                    if (fieldType != null)
                    {
                        string placeholder  = index.ToString("x2");
                        string deExpression = ConvertToDecisionEngine(expression, fieldType);
                        deCriteria = deCriteria.Replace(placeholder, deExpression);
                    }
                    else
                    {
                        deCriteria = "Variable = " + variable + " not found.";
                        break;
                    }
                    index++;
                }
            }
            else
            {
                deCriteria = "Parentheses in input criteria do not match.";
            }
            return(deCriteria);
        }
Exemplo n.º 6
0
        public object ApplyChange(object value)
        {
            if (SimulationMessaging.GetAttributeType(this.Attribute) == "STRING")
            {
                return(this.Change);
            }
            else
            {
                bool bAdd      = false;
                bool bSubtract = false;
                bool bPercent  = false;

                String strChange = this.Change;

                if (strChange.IndexOf('+') > -1)
                {
                    bAdd      = true;
                    strChange = strChange.Replace("+", "");
                }


                if (strChange.IndexOf('-') > -1)
                {
                    bSubtract = true;
                    strChange = strChange.Replace("-", "");
                }


                if (strChange.IndexOf('%') > -1)
                {
                    bPercent  = true;
                    strChange = strChange.Replace("%", "");
                }

                double dChange = 0;
                double dValue  = 0;
                try
                {
                    dValue  = Convert.ToDouble(value);
                    dChange = double.Parse(strChange);
                }
                catch
                {
                    return(value);
                }

                if (bAdd && bPercent)
                {
                    dValue = dValue + (dChange / 100) * dValue;
                }
                else if (bSubtract && bPercent)
                {
                    dValue = dValue - (dChange / 100) * dValue;
                }
                else if (bPercent)
                {
                    dValue = dValue * (dChange / 100);
                }
                else if (bAdd)
                {
                    dValue = dValue + dChange;
                }
                else if (bSubtract)
                {
                    dValue = dValue - dChange;
                }
                else
                {
                    dValue = dChange;
                }


                if (!String.IsNullOrEmpty(Minimum))
                {
                    if (double.Parse(Minimum) > dValue)
                    {
                        dValue = double.Parse(Minimum);
                    }
                }

                if (!String.IsNullOrEmpty(Maximum))
                {
                    if (double.Parse(Maximum) < dValue)
                    {
                        dValue = double.Parse(Maximum);
                    }
                }
                return(dValue);
            }
        }