Exemplo n.º 1
0
        /// <summary>
        /// Load all cost information associated with this treatment.
        /// </summary>
        public bool LoadCost(object APICall, IMongoCollection <SimulationModel> Simulations, string m_strSimulationID)
        {
            Costs  cost;
            String select = "SELECT COST_,UNIT,CRITERIA,BINARY_CRITERIA,ISFUNCTION, COSTID FROM " + cgOMS.Prefix + "COSTS WHERE TREATMENTID='" + this.TreatmentID + "'";

            if (SimulationMessaging.IsOMS)
            {
                select = "SELECT COST_,UNIT,CRITERIA,BINARY_CRITERIA,NULL AS ISFUNCTION, COSTID FROM " + cgOMS.Prefix + "COSTS WHERE TREATMENTID='" + this.TreatmentID + "'";
            }
            DataSet ds;

            try
            {
                //SimulationMessaging.AddMessage("DEBUGGING: Attempting cost select...");
                ds = DBMgr.ExecuteQuery(select);
                //SimulationMessaging.AddMessage("DEBUGGING: Cost select successful.");
            }
            catch (Exception exception)
            {
                SimulationMessaging.AddMessage(new SimulationMessage("Fatal Error: Opening COSTS table.  SQL message - " + exception.Message));
                if (APICall.Equals(true))
                {
                    var updateStatus = Builders <SimulationModel> .Update
                                       .Set(s => s.status, "Fatal Error: Opening COSTS table.  SQL message - " + exception.Message);

                    Simulations.UpdateOne(s => s.simulationId == Convert.ToInt32(m_strSimulationID), updateStatus);
                }
                return(false);
            }


            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string id = row["COSTID"].ToString();
                cost = new Costs(id);

                if (row["CRITERIA"].ToString().Trim().Length == 0)
                {
                    cost.Default = true;
                }
                else
                {
                    cost.Default = false;
                    string criteria         = row["CRITERIA"].ToString();
                    byte[] assemblyCriteria = null;
                    assemblyCriteria = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "COSTS", "BINARY_CRITERIA", cost.CostID, assemblyCriteria);
                    if (assemblyCriteria != null && assemblyCriteria.Length > 0)
                    {
                        cost.Criteria.Evaluate = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assemblyCriteria);
                        if (cost.Criteria.Evaluate.OriginalInput != criteria)
                        {
                            cost.Criteria.Evaluate = null;
                        }
                    }

                    cost.Criteria.Criteria = criteria;
                    foreach (String str in cost.Criteria.CriteriaAttributes)
                    {
                        if (!SimulationMessaging.IsAttribute(str))
                        {
                            SimulationMessaging.AddMessage(new SimulationMessage("Error: " + str + " which is used by the Cost criteria is not present in the database."));
                            if (APICall.Equals(true))
                            {
                                var updateStatus = Builders <SimulationModel> .Update
                                                   .Set(s => s.status, "Error: " + str + " which is used by the Cost criteria is not present in the database");

                                Simulations.UpdateOne(s => s.simulationId == Convert.ToInt32(m_strSimulationID), updateStatus);
                            }
                        }
                        if (!_attributes.Contains(str))
                        {
                            _attributes.Add(str);
                        }
                    }
                }


                byte[] assemblyCost = null;
                //objectValue = row["BINARY_COST"];
                //if (objectValue != System.DBNull.Value)
                //{
                //    assemblyCost = (byte[])row["BINARY_COST"];
                //}
                String strCost = row["COST_"].ToString();
                assemblyCost = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "COSTS", "BINARY_EQUATION", cost.CostID, assemblyCost);
                if (assemblyCost != null && assemblyCost.Length > 0)
                {
                    cost.Calculate = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assemblyCost);
                    if (cost.Calculate.OriginalInput != strCost)
                    {
                        cost.Calculate = null;
                    }
                }

                if (strCost.Trim() == "")
                {
                    SimulationMessaging.AddMessage(new SimulationMessage("Fatal Error: Cost equation is blank for treatment - " + _treatment));
                    if (APICall.Equals(true))
                    {
                        var updateStatus = Builders <SimulationModel> .Update
                                           .Set(s => s.status, "Fatal Error: Cost equation is blank for treatment - " + _treatment);

                        Simulations.UpdateOne(s => s.simulationId == Convert.ToInt32(m_strSimulationID), updateStatus);
                    }
                    return(false);
                }

                bool   isFunction = false;
                object function   = row["ISFUNCTION"];
                if (row["ISFUNCTION"] != DBNull.Value)
                {
                    isFunction = Convert.ToBoolean(row["ISFUNCTION"]);
                }
                if (isFunction)
                {
                    cost.SetFunction(row["COST_"].ToString());
                }
                else
                {
                    cost.Equation = row["COST_"].ToString();
                }

                foreach (String str in cost._attributesEquation)
                {
                    if (str != "AREA" && str != "LENGTH")
                    {
                        if (!SimulationMessaging.IsAttribute(str))
                        {
                            SimulationMessaging.AddMessage(new SimulationMessage("Error: " + str + " which is used by the Cost equation is not present in the database."));
                            if (APICall.Equals(true))
                            {
                                var updateStatus = Builders <SimulationModel> .Update
                                                   .Set(s => s.status, "Error: " + str + " which is used by the Cost equation is not present in the database");

                                Simulations.UpdateOne(s => s.simulationId == Convert.ToInt32(m_strSimulationID), updateStatus);
                            }
                        }
                        if (!_attributes.Contains(str))
                        {
                            _attributes.Add(str);
                        }
                    }
                }
                if (!cost.IsCompoundTreatment)
                {
                    if (cost._calculate.m_listError.Count > 0)
                    {
                        foreach (String str in cost.Calculate.m_listError)
                        {
                            SimulationMessaging.AddMessage(new SimulationMessage("Fatal Error: Cost equation:" + str));
                            if (APICall.Equals(true))
                            {
                                var updateStatus = Builders <SimulationModel> .Update
                                                   .Set(s => s.status, "Fatal Error: Cost equation:" + str);

                                Simulations.UpdateOne(s => s.simulationId == Convert.ToInt32(m_strSimulationID), updateStatus);
                            }
                            return(false);
                        }
                    }
                }
                _costs.Add(cost);
            }
            return(true);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Add a new Costs object to this treatment
 /// </summary>
 /// <param name="cost"></param>
 public void AddCost(Costs cost)
 {
     _costs.Add(cost);
 }