Ejemplo n.º 1
0
        private void OnCommencing(object sender, EventArgs e)
        {
            dataToWriteToDb = null;
            // sanitise the variable names and remove duplicates
            List <string> variableNames = new List <string>();

            variableNames.Add("Parent.Name as Zone");

            if (VariableNames.Where(a => a.Contains("[Clock].Today")).Count() == 0)
            {
                variableNames.Add("[Clock].Today as Date");
            }

            if (VariableNames != null)
            {
                for (int i = 0; i < this.VariableNames.Length; i++)
                {
                    // each variable name is now a ResourceGroup
                    bool isDuplicate = StringUtilities.IndexOfCaseInsensitive(variableNames, this.VariableNames[i].Trim()) != -1;
                    if (!isDuplicate && this.VariableNames[i] != string.Empty)
                    {
                        if (this.VariableNames[i].StartsWith("["))
                        {
                            variableNames.Add(this.VariableNames[i]);
                        }
                        else
                        {
                            // check it is a ResourceGroup
                            CLEMModel model = Resources.GetResourceGroupByName(this.VariableNames[i]) as CLEMModel;
                            if (model == null)
                            {
                                throw new ApsimXException(this, String.Format("@error:Invalid resource group [r={0}] in ReportResourceBalances [{1}]\nEntry has been ignored", this.VariableNames[i], this.Name));
                            }
                            else
                            {
                                if (model.GetType().Name == "Labour")
                                {
                                    for (int j = 0; j < (model as Labour).Items.Count; j++)
                                    {
                                        variableNames.Add("[Resources]." + this.VariableNames[i] + ".Items[" + (j + 1).ToString() + "].AvailableDays as " + (model as Labour).Items[j].Name);
                                    }
                                }
                                else
                                {
                                    // get all children
                                    foreach (CLEMModel item in model.Children.Where(a => a.GetType().IsSubclassOf(typeof(CLEMModel)))) // Apsim.Children(this, typeof(CLEMModel))) //
                                    {
                                        string amountStr = "Amount";
                                        switch (item.GetType().Name)
                                        {
                                        case "FinanceType":
                                            amountStr = "Balance";
                                            break;

                                        case "LabourType":
                                            amountStr = "AvailableDays";
                                            break;

                                        default:
                                            break;
                                        }
                                        variableNames.Add("[Resources]." + this.VariableNames[i] + "." + item.Name + "." + amountStr + " as " + item.Name);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            base.VariableNames = variableNames.ToArray();
            this.FindVariableMembers();

            if (EventNames[0] == "")
            {
                events.Subscribe("[Clock].CLEMEndOfTimeStep", DoOutputEvent);
            }
            else
            {
                // Subscribe to events.
                foreach (string eventName in EventNames)
                {
                    if (eventName != string.Empty)
                    {
                        events.Subscribe(eventName.Trim(), DoOutputEvent);
                    }
                }
            }
        }