Exemple #1
0
 /// <summary>
 /// Add a list of Consequences for this treatment
 /// </summary>
 /// <param name="consequence"></param>
 public void AddConsequence(Consequences consequence)
 {
     _consequences.Add(consequence);
 }
Exemple #2
0
        /// <summary>
        /// Load all consequence information associated with treat.
        /// </summary>
        public bool LoadConsequences(object APICall, IMongoCollection <SimulationModel> Simulations, string m_strSimulationID)
        {
            Consequences consequence;
            String       select = "SELECT ATTRIBUTE_,CHANGE_,CRITERIA,EQUATION,ISFUNCTION, CONSEQUENCEID FROM " + cgOMS.Prefix + "CONSEQUENCES WHERE TREATMENTID='" + _treatmentID + "'";

            if (SimulationMessaging.IsOMS)
            {
                select = "SELECT ATTRIBUTE_,CHANGE_,CRITERIA,EQUATION,NULL AS ISFUNCTION, CONSEQUENCEID FROM " + cgOMS.Prefix + "CONSEQUENCES WHERE TREATMENTID='" + _treatmentID + "'";
            }

            DataSet ds;

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

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

            var consequenceCount = 0;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string id = row["CONSEQUENCEID"].ToString();
                consequence = new Consequences(id);
                String strAttribute = row["ATTRIBUTE_"].ToString();
                String strChange    = row["CHANGE_"].ToString();
                String strCriteria  = row["CRITERIA"].ToString();
                String strEquation  = row["EQUATION"].ToString();
                consequence.TreatmentID = this.TreatmentID;
                consequence.Treatment   = this.Treatment;

                SimulationMessaging.AddMessage(new SimulationMessage("Compiling treatment consequence " + consequenceCount));
                if (APICall.Equals(true))
                {
                    var updateStatus = Builders <SimulationModel> .Update
                                       .Set(s => s.status, "Compiling treatment consequence " + consequenceCount);

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


                if (strCriteria.Trim().Length == 0)
                {
                    consequence.Default = true;
                }
                else
                {
                    consequence.Default = false;

                    byte[] assemblyCriteria = null;
                    assemblyCriteria = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "CONSEQUENCES", "BINARY_CRITERIA", id, assemblyCriteria);
                    if (assemblyCriteria != null && assemblyCriteria.Length > 0)
                    {
                        consequence.Criteria.Evaluate = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assemblyCriteria);
                        if (consequence.Criteria.Evaluate.OriginalInput != strCriteria)
                        {
                            consequence.Criteria.Evaluate = null;
                        }
                    }

                    consequence.Criteria.Criteria = strCriteria;
                    //Get attributes from consequence criteria
                    foreach (String str in consequence.Criteria.CriteriaAttributes)
                    {
                        if (!SimulationMessaging.IsAttribute(str))
                        {
                            SimulationMessaging.AddMessage(new SimulationMessage("Error: " + str + " which is used by the Consequence 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 Consequence criteria is not present in the database");

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


                if (string.IsNullOrWhiteSpace(strEquation.Trim()))
                {
                    consequence.IsEquation = false;;
                }
                else
                {
                    byte[] assembly = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "CONSEQUENCES", "BINARY_EQUATION", id, null);
                    if (assembly != null && assembly.Length > 0)
                    {
                        consequence._calculate = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assembly);
                        if (consequence._calculate.OriginalInput != strEquation)
                        {
                            consequence._calculate = null;
                        }
                    }

                    bool isFunction = false;
                    if (row["ISFUNCTION"] != DBNull.Value)
                    {
                        isFunction = Convert.ToBoolean(row["ISFUNCTION"]);
                    }

                    if (isFunction)
                    {
                        consequence.SetFunction(strEquation);
                    }
                    else
                    {
                        consequence.Equation = strEquation;
                    }
                    //Get attributes from consequence criteria
                    foreach (string attribute in consequence._attributesEquation)
                    {
                        if (!SimulationMessaging.IsAttribute(attribute))
                        {
                            SimulationMessaging.AddMessage(new SimulationMessage("Error: " + attribute + " which is used by the Consequence criteria is not present in the database."));
                            if (APICall.Equals(true))
                            {
                                var updateStatus = Builders <SimulationModel> .Update
                                                   .Set(s => s.status, "Error: " + attribute + " which is used by the Consequence criteria is not present in the database");

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

                consequence.LoadAttributeChange(strAttribute, strChange);
                ConsequenceList.Add(consequence);

                // Get attributes from Attribute change
                foreach (String str in consequence.Attributes)
                {
                    if (!_attributes.Contains(str))
                    {
                        _attributes.Add(str);
                    }
                }
            }
            return(true);
        }
        private bool CheckInput()
        {
            //Modified by adding simulationID so can run multiple runs for Cartegeraph.
            SimulationMessaging.LoadAttributes(m_strSimulationID);
            m_singleSection = new Simulation.Committed();

            labelError.Visible = false;
            //Check for Treatment Name
            m_strSingleTreatment = dgvSummary[1, 1].Value.ToString();
            if (m_strSingleTreatment.Trim().Length == 0)
            {
                labelError.Visible = true;
                labelError.Text    = "Error:Non-blank treatment name must be entered.";
                return(false);
            }
            m_singleSection.Treatment = m_strSingleTreatment;

            //Check for budget
            m_strSingleBudget = dgvSummary[1, 2].Value.ToString();
            if (m_strSingleTreatment != "No Treatment")
            {
                if (!m_listBudgets.Contains(m_strSingleBudget))
                {
                    labelError.Visible = true;
                    labelError.Text    = "Error: Budget category must be selected";
                    return(false);
                }
                m_singleSection.Budget = m_strSingleBudget;
            }
            //Check for Cost (is positive number)

            m_strSingleCost = dgvSummary[1, 3].Value.ToString().Replace("$", "");
            try
            {
                double dCost = double.Parse(m_strSingleCost);
                if (dCost < 0)
                {
                    labelError.Visible = true;
                    labelError.Text    = "Error: Cost must be greater than or equal to 0.";
                    return(false);
                }
            }
            catch
            {
                labelError.Visible = true;
                labelError.Text    = "Error: Cost must be a number.";
                return(false);
            }
            m_singleSection.Cost = float.Parse(m_strSingleCost);

            //Check for YearsAny (is positive number)
            m_strSingleAny = dgvSummary[1, 4].Value.ToString();
            try
            {
                int nAny = int.Parse(m_strSingleAny);
                if (nAny < 0)
                {
                    labelError.Visible = true;
                    labelError.Text    = "Error: Year any must be an integer must be greater than or equal to 0.";
                    return(false);
                }
            }
            catch
            {
                labelError.Visible = true;
                labelError.Text    = "Error: Year Any must be a positive integer.";
                return(false);
            }
            m_singleSection.Any = int.Parse(m_strSingleAny);

            //Check for Years Same (is positive number)
            m_strSingleSame = dgvSummary[1, 5].Value.ToString();
            try
            {
                int nSame = int.Parse(m_strSingleSame);
                if (nSame < 0)
                {
                    labelError.Visible = true;
                    labelError.Text    = "Error: Year same must be an integer must be greater than or equal to 0.";
                    return(false);
                }
            }
            catch
            {
                labelError.Visible = true;
                labelError.Text    = "Error: Year Same must be a positive integer.";
                return(false);
            }
            m_singleSection.Same = int.Parse(m_strSingleSame);

            //Check to make sure there are not repeats of attributes.
            List <String> listConsequence = new List <String>();

            Simulation.Consequences consequence = new Simulation.Consequences();
            foreach (DataGridViewRow row in dgvAttribute.Rows)
            {
                if (row.Cells[0].Value == null)
                {
                    continue;
                }
                if (listConsequence.Contains(row.Cells[0].Value.ToString()))
                {
                    labelError.Visible = true;
                    labelError.Text    = "Error: Multiple consequences for the same attribute not allowed.";
                    return(false);
                }
                listConsequence.Add(row.Cells[0].Value.ToString());
                consequence.LoadAttributeChange(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString());
            }
            m_singleSection.Consequence = consequence;

            return(true);
        }