Beispiel #1
0
        public double Cost(Hashtable hashAttributeValue)
        {
            //return this.UnitCost * GetQuantity(hashAttributeValue);
            double cost = 0.0;

            try
            {
                cost = double.Parse(_cost.m_strResult);
            }
            catch (Exception exc)
            {
                SimulationMessaging.AddMessage(new SimulationMessage("Could not parse cost to a double. Cost set to zero." + exc.Message));
            }
            return(cost);
        }
Beispiel #2
0
        /// <summary>
        /// Saves any changes made to the currently selected compound treatment, including its associated elements to the database.
        /// </summary>
        private void SaveCurrentCompoundTreatmentChanges()
        {
            // Navigation off of a blank row will produce a null value for _compoundTreatment.  No changes are necessary in the database in this case.
            if (_compoundTreatment != null)
            {
                // Statements for database changes.
                List <string> changeStatements = new List <string>();

                // Delete from the compound treatment table our current compound treatment.  This should cascade and remove its elements from teh element table as well.
                string delete = "DELETE FROM COMPOUND_TREATMENTS WHERE COMPOUND_TREATMENT_ID = '" + _compoundTreatment.CompoundTreatmentID + "'";
                changeStatements.Add(delete);

                // Now insert the current compound treatment
                string insert = "INSERT INTO COMPOUND_TREATMENTS (COMPOUND_TREATMENT_NAME, AFFECTED_ATTRIBUTE, COMPOUND_TREATMENT_ID) VALUES ('" +
                                _compoundTreatment.CompoundTreatmentName +
                                "', '" + _compoundTreatment.AffectedAttribute +
                                "', '" + _compoundTreatment.CompoundTreatmentID + "')";
                changeStatements.Add(insert);

                foreach (DataGridViewRow elementRow in dataGridViewCompoundTreatmentElements.Rows)
                {
                    CompoundTreatmentElement toInsert = (CompoundTreatmentElement)elementRow.Cells["colCompoundTreatmentElement"].Value;
                    if (toInsert != null)
                    {
                        // Insert the data.
                        insert = "INSERT INTO COMPOUND_TREATMENT_ELEMENTS (ATTRIBUTE_FROM, ATTRIBUTE_TO, EXTENT_, QUANTITY_, CRITERIA_, COST_, COMPOUND_TREATMENT_ID) VALUES ('" +
                                 toInsert.AttributeFrom +
                                 "', '" + toInsert.AttributeTo +
                                 "', '" + toInsert.ExtentEquation.m_expression +
                                 "', '" + toInsert.Quantity.m_expression +
                                 "', '" + toInsert.CriteriaEquation.Criteria +
                                 "', '" + toInsert.CostEquation.m_expression +
                                 "', '" + toInsert.CompoundTreatmentID + "')";
                        changeStatements.Add(insert);
                    }
                }
                try
                {
                    DBMgr.ExecuteBatchNonQuery(changeStatements);
                }
                catch (Exception exc)
                {
                    SimulationMessaging.AddMessage(new SimulationMessage("Error updating database with new compound treatment values.  Transaction aborted. " + exc.Message));
                }
            }
        }
Beispiel #3
0
        public double GetQuantity(Hashtable hashAttributeValue)
        {
            int i = 0;

            object[] input = new object[_quantity.m_listParameters.Count];
            foreach (String str in _quantity.m_listParameters)
            {
                if (str == "AREA")
                {
                    input[i] = double.Parse(hashAttributeValue[str].ToString());
                }
                else if (str == "LENGTH")
                {
                    input[i] = double.Parse(hashAttributeValue[str].ToString());
                }

                else
                {
                    if (SimulationMessaging.GetAttributeType(str) == "STRING")
                    {
                        input[i] = hashAttributeValue[str].ToString();
                    }
                    else
                    {
                        input[i] = double.Parse(hashAttributeValue[str].ToString());
                    }
                }
                i++;
            }
            try
            {
                object result = _quantity.RunMethod(input);
                return((double)result);
            }
            catch (Exception exc)
            {
                SimulationMessaging.AddMessage(new SimulationMessage("Error in RunMethod. " + exc.Message));
                return(0);
            }
        }
Beispiel #4
0
        private bool CheckEquation()
        {
            bool isFunction = checkBoxAsFunction.Checked;

            textBoxCompile.Text = "";
            m_strEquation       = richTextBoxEquation.Text;
            m_strEquation       = m_strEquation.Trim();
            List <string> listError;
            List <String> listAttributesEquation = Global.TryParseAttribute(m_strEquation, out listError);            // See if listAttributeEquations is included in dgvDefault

            if (listError.Count > 0)
            {
                foreach (string str in listError)
                {
                    textBoxCompile.Text = textBoxCompile.Text + str + "\r\n";
                }
                return(false);
            }
            FillDefault();
            if (m_strEquation.Length > 6)
            {
                if (m_strEquation.Substring(0, 6) == "MODULE")
                {
                    return(CheckModule());
                }
                else if (m_strEquation.Contains("COMPOUND_TREATMENT"))
                {
                    return(true);
                }
            }

            if (m_strEquation.Trim().Length == 0)
            {
                textBoxCompile.Text = "Equation must be entered before selecting Check or OK.";
                System.Media.SystemSounds.Exclamation.Play();
                return(false);
            }

            calculateCheck = new CalculateEvaluate.CalculateEvaluate();

            if (isFunction) //Change to allow function equation to accept strings as properties.
            {
                string equation = m_strEquation;
                foreach (String attribute in listAttributesEquation)
                {
                    String attributeType = SimulationMessaging.GetAttributeType(attribute);
                    if (attributeType == "STRING")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[@" + attribute + "]";
                        equation = equation.Replace(oldValue, newValue);
                    }
                    else if (attributeType == "DATETIME")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[$" + attribute + "]";
                        equation = equation.Replace(oldValue, newValue);
                    }
                }
                calculateCheck.BuildFunctionClass(equation, "double", null);
            }
            else
            {
                calculateCheck.BuildTemporaryClass(m_strEquation, true);
            }
            try
            {
                CompilerResults m_crEquation = calculateCheck.CompileAssembly();
                if (calculateCheck.m_listError.Count > 0)
                {
                    foreach (String str in calculate.m_listError)
                    {
                        textBoxCompile.Text = textBoxCompile.Text + str + "\r\n";
                    }
                }

                if (textBoxCompile.Text.Length == 0)
                {
                    textBoxCompile.Text = "Compilation successful. Results that appear in right grid calculated using default values.";
                }
                else
                {
                    System.Media.SystemSounds.Exclamation.Play();
                    return(false);
                }
                //If [AGE] is the only variable.  This only needs to be solved once so might
                //as well solve it right now.

                //Equations that are based only on age can only have 100 distinct values.   Fill the solution matrix and look up by age.
                Hashtable hash = GetDefaultValues();
                if (!Solve(hash, listAttributesEquation))
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                textBoxCompile.Text = "ERROR: unable to compile equation: " + ex.Message;
                return(false);
            }
        }
Beispiel #5
0
        private bool CheckAreaEquation()
        {
            textBoxCompile.Text = "";
            m_strEquation       = richTextBoxEquation.Text;
            m_strEquation       = m_strEquation.Trim();
            List <string> listError;
            List <String> listAttributesEquation = Global.TryParseAttribute(m_strEquation, out listError);            // See if listAttributeEquations is included in dgvDefault

            if (listError.Count > 0)
            {
                foreach (string str in listError)
                {
                    textBoxCompile.Text = textBoxCompile.Text + str + "\r\n";
                }
                return(false);
            }
            FillDefault();

            if (m_strEquation.Trim().Length == 0)
            {
                textBoxCompile.Text = "Equation must be entered before selecting Check or OK.";
                System.Media.SystemSounds.Exclamation.Play();
                return(false);
            }

            // Get list of attributes
            calculate = new CalculateEvaluate.CalculateEvaluate();

            bool isFunction = checkBoxAsFunction.Checked;

            if (isFunction) //Change to allow function equation to accept strings as properties.
            {
                string equation = m_strEquation;
                foreach (String attribute in listAttributesEquation)
                {
                    String attributeType = SimulationMessaging.GetAttributeType(attribute);
                    if (attributeType == "STRING")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[@" + attribute + "]";
                        equation = equation.Replace(oldValue, newValue);
                    }
                    else if (attributeType == "DATETIME")
                    {
                        String oldValue = "[" + attribute + "]";
                        String newValue = "[$" + attribute + "]";
                        equation = equation.Replace(oldValue, newValue);
                    }
                }
                calculate.BuildFunctionClass(equation, "double", null);
            }
            else
            {
                calculate.BuildTemporaryClass(m_strEquation, true);
            }
            try
            {
                CompilerResults m_crEquation = calculate.CompileAssembly();

                if (calculate.m_listError.Count > 0)
                {
                    foreach (String str in calculate.m_listError)
                    {
                        textBoxCompile.Text = textBoxCompile.Text + str + "\r\n";
                    }
                }

                if (textBoxCompile.Text.Length == 0)
                {
                    textBoxCompile.Text = "Compilation sucessful. Results that appear in right grid calculated using default values.";
                }
                else
                {
                    System.Media.SystemSounds.Exclamation.Play();
                    return(false);
                }
                //If [AGE] is the only variable.  This only needs to be solved once so might
                //as well solve it right now.

                dgvPerformance.Rows.Clear();

                object[] input = new object[dgvDefault.Rows.Count];
                int      i     = 0;
                foreach (DataGridViewRow row in dgvDefault.Rows)
                {
                    string attribute = row.Cells[0].Value.ToString();
                    if (Global.GetAttributeType(attribute) == "STRING")
                    {
                        input[i] = row.Cells[1].Value;
                    }
                    else
                    {
                        double value = 0;
                        Double.TryParse(row.Cells[1].Value.ToString(), out value);
                        input[i] = value;
                    }



                    i++;
                }
                object result = calculate.RunMethod(input);

                calculate.m_assemblyInstance = null;
                calculate.methodInfo         = null;

                dgvPerformance.ColumnCount = 1;
                if (this.CalculatedField)
                {
                    dgvPerformance.Columns[0].HeaderText = "CALCULATED";
                }
                else
                {
                    dgvPerformance.Columns[0].HeaderText = "COST";
                }
                dgvPerformance.Rows.Add(result);

                return(true);
            }
            catch (Exception ex)
            {
                Global.WriteOutput("ERROR: could not compile equation:" + ex.Message);
                return(false);
            }
        }
        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);
        }
Beispiel #7
0
        public CompoundTreatmentElement(string compoundTreatmentID, string compoundTreatmentName, string attributeFrom, string attributeTo, string costEquation, string extentEquation, string quantityCriteria, string criteriaString)
        {
            _compoundTreatmentID   = compoundTreatmentID;
            _compoundTreatmentName = compoundTreatmentName;

            // Begin building the relavent attribute list.  This list contains all attributes used in the various
            // criteria and equations.
            _attributeFrom = attributeFrom;
            if (!_relevantAttributes.Contains(_attributeFrom))
            {
                _relevantAttributes.Add(_attributeFrom);
            }

            _attributeTo = attributeTo;
            if (!_relevantAttributes.Contains(_attributeTo))
            {
                _relevantAttributes.Add(_attributeTo);
            }

            // Add cost attributes


            byte[] assembly = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "COMPOUNDTREATMENT", "COST", compoundTreatmentID, null);
            if (assembly != null && assembly.Length > 0)
            {
                _cost = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assembly);
                if (SimulationMessaging.Area.OriginalInput != costEquation)
                {
                    _cost = null;
                }
            }
            if (_cost == null)
            {
                _cost = new CalculateEvaluate.CalculateEvaluate();
                _cost.BuildClass(costEquation, true, cgOMS.Prefix + "COMPOUNDTREATMENT_COST_" + compoundTreatmentID);
                _cost.CompileAssembly();
                SimulationMessaging.SaveSerializedCalculateEvaluate(cgOMS.Prefix + "COMPOUNDTREATMENT", "COST", compoundTreatmentID, _cost);
            }
            foreach (string parameter in _cost.m_listParameters)
            {
                if (!_relevantAttributes.Contains(parameter))
                {
                    _relevantAttributes.Add(parameter);
                }
            }


            assembly = null;
            assembly = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "COMPOUNDTREATMENT", "EXTENT", compoundTreatmentID, null);
            if (assembly != null && assembly.Length > 0)
            {
                _extent = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assembly);
                if (SimulationMessaging.Area.OriginalInput != extentEquation)
                {
                    _extent = null;
                }
            }
            if (_extent == null)
            {
                // Add extent attributes
                _extent = new CalculateEvaluate.CalculateEvaluate();
                _extent.BuildClass(extentEquation, true, cgOMS.Prefix + "COMPOUNDTREATMENT_EXTENT_" + compoundTreatmentID);
                _extent.CompileAssembly();
                SimulationMessaging.SaveSerializedCalculateEvaluate(cgOMS.Prefix + "COMPOUNDTREATMENT", "EXTENT", compoundTreatmentID, _extent);
            }
            foreach (string parameter in _extent.m_listParameters)
            {
                if (!_relevantAttributes.Contains(parameter))
                {
                    _relevantAttributes.Add(parameter);
                }
            }



            assembly = null;
            assembly = SimulationMessaging.GetSerializedCalculateEvaluate(cgOMS.Prefix + "COMPOUNDTREATMENT", "QUANTITY", compoundTreatmentID, null);
            if (assembly != null && assembly.Length > 0)
            {
                _quantity = (CalculateEvaluate.CalculateEvaluate)AssemblySerialize.DeSerializeObjectFromByteArray(assembly);
                if (SimulationMessaging.Area.OriginalInput != extentEquation)
                {
                    _quantity = null;
                }
            }
            if (_quantity == null)
            {
                // Add quantity attributes
                _quantity = new CalculateEvaluate.CalculateEvaluate();
                _quantity.BuildClass(quantityCriteria, true, cgOMS.Prefix + "COMPOUNDTREATMENT_QUANTITY_" + compoundTreatmentID);
                _quantity.CompileAssembly();
                SimulationMessaging.SaveSerializedCalculateEvaluate(cgOMS.Prefix + "COMPOUNDTREATMENT", "QUANTITY", compoundTreatmentID, _quantity);
            }
            foreach (string parameter in _quantity.m_listParameters)
            {
                if (!_relevantAttributes.Contains(parameter))
                {
                    _relevantAttributes.Add(parameter);
                }
            }

            // Add any criteria attributes
            _criteria = new Criterias();
            if (!string.IsNullOrEmpty(criteriaString))
            {
                _criteria.Criteria = criteriaString;
                foreach (string parameter in _criteria.CriteriaAttributes)
                {
                    if (!_relevantAttributes.Contains(parameter))
                    {
                        _relevantAttributes.Add(parameter);
                    }
                }
            }
        }