示例#1
0
        public new void ToString()
        {
            var selectClause = new SelectClause(Expression.Constant(0));

            Assert.That(selectClause.ToString(), Is.EqualTo("select 0"));
        }
        public void Execute()
        {
            Dictionary <string, string> config = this.Context.SetProperties;

            System.Collections.Generic.Dictionary <string, System.Data.DataTable> Freq_ListSet = new Dictionary <string, System.Data.DataTable>();

            DataTable DT = new DataTable();

            DT.CaseSensitive = true;
            DataTable OutDataTable = new DataTable();

            foreach (DataColumn column in this.Context.Columns)
            {
                DataColumn newColumn = new DataColumn(column.ColumnName);
                newColumn.DataType = column.DataType;
                DT.Columns.Add(newColumn);
            }

            // **** Get Participating Variables Start
            List <string> ParticipatingVariables = new List <string>();

            /*
             * if (this.IdentifierList[0] == "*")
             * {
             *  foreach (System.Data.DataColumn C in this.Context.Columns)
             *  {
             *      //ParticipatingVariables.Add(C.ColumnName);
             *  }
             * }
             * else
             * {
             *  foreach (string Key in IdentifierList)
             *  {
             *      //ParticipatingVariables.Add(Key);
             *  }
             * }*/

            if (!string.IsNullOrEmpty(this.WeightVar) && this.WeightVar != "")
            {
                ParticipatingVariables.Add(this.WeightVar);
            }

            if (this.StratvarList != null) // PATCH; improve later
            {
                foreach (string stratavar in this.StratvarList)
                {
                    ParticipatingVariables.Add(stratavar);
                }
            }

            // **** Get Participating Variables End
            foreach (DataRow row in this.Context.GetDataRows(ParticipatingVariables))
            {
                DT.ImportRow(row);
            }

            Frequency.PermutationList = new Dictionary <string, List <object> >();
            CreatePermutaionList(this.StratvarList);


            List <string> RemoveList = new List <string>();

            if (!this.config.Settings.IncludeMissingValues)
            {
                foreach (string s in Frequency.SelectClauses)
                {
                    if (s.EndsWith(" is Null", StringComparison.OrdinalIgnoreCase))
                    {
                        RemoveList.Add(s);
                    }
                }
            }

            foreach (string s in RemoveList)
            {
                Frequency.SelectClauses.Remove(s);
            }

            StringBuilder HTMLString = new StringBuilder();

            System.Collections.Generic.Dictionary <string, string> KeyList = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(this.OutTable))
            {
                OutDataTable.TableName = this.OutTable;
                if (this.StratvarList != null)
                {
                    foreach (string stratavar in this.StratvarList)
                    {
                        DataColumn newColumn = new DataColumn(stratavar);
                        newColumn.DataType = this.Context.Columns[stratavar].DataType;
                        OutDataTable.Columns.Add(newColumn);
                    }
                }
                foreach (string freqitem in this.IdentifierList)
                {
                    DataColumn newColumn = new DataColumn(freqitem);
                    newColumn.DataType = this.Context.Columns[freqitem].DataType;
                    OutDataTable.Columns.Add(newColumn);
                }
                DataColumn varCol = new DataColumn("VARNAME");
                varCol.DataType = typeof(string);
                OutDataTable.Columns.Add(varCol);

                DataColumn percentCol = new DataColumn("PERCENT");
                percentCol.DataType = typeof(double);
                OutDataTable.Columns.Add(percentCol);

                DataColumn countCol = new DataColumn("COUNT");
                countCol.DataType = typeof(int);
                OutDataTable.Columns.Add(countCol);
            }

            foreach (string SelectClause in Frequency.SelectClauses)
            {
                Freq_ListSet.Clear();
                KeyList.Clear();

                if (this.IdentifierList[0] == "*")
                {
                    foreach (System.Data.DataColumn C in this.Context.Columns)
                    {
                        string Key = C.ColumnName;
                        if (!Freq_ListSet.ContainsKey(Key))
                        {
                            Freq_ListSet.Add(Key, CreateDataTable(Key));
                            KeyList.Add(Key, Key);
                        }
                    }
                }
                else
                {
                    foreach (string Key in IdentifierList)
                    {
                        if (!Freq_ListSet.ContainsKey(Key))
                        {
                            if (GetColumnDataType(Key) != null)
                            {
                                Freq_ListSet.Add(Key, CreateDataTable(Key));
                                KeyList.Add(Key, Key);
                            }
                        }
                    }
                }

                foreach (System.Data.DataRow R in DT.Select(SelectClause))
                {
                    foreach (System.Collections.Generic.KeyValuePair <string, System.Data.DataTable> KeyP in Freq_ListSet)
                    {
                        string Key = KeyP.Key;
                        foreach (System.Data.DataColumn C in DT.Columns)
                        {
                            string ColumnKey = C.ColumnName;
                            if (ColumnKey.ToUpperInvariant() == Key.ToUpperInvariant())
                            {
                                bool RowIsFound = false;

                                foreach (System.Data.DataRow R2 in KeyP.Value.Rows)
                                {
                                    if (R[C.ColumnName].ToString() == R2["value"].ToString())
                                    {
                                        RowIsFound = true;
                                        if (string.IsNullOrEmpty(this.WeightVar))
                                        {
                                            R2["count"] = int.Parse(R2["count"].ToString()) + 1;
                                        }
                                        else
                                        {
                                            double temp = 0.0;

                                            if (double.TryParse(R[this.WeightVar].ToString(), out temp))
                                            {
                                                R2["count"] = double.Parse(R2["count"].ToString()) + temp;
                                            }
                                        }
                                        break;
                                    }
                                }

                                if (!RowIsFound)
                                {
                                    System.Data.DataRow R3;
                                    if (string.IsNullOrEmpty(this.WeightVar))
                                    {
                                        R3          = Freq_ListSet[Key].NewRow();
                                        R3["value"] = R[C.ColumnName];
                                        R3["count"] = 1;
                                        if (this.StratvarList != null)
                                        {
                                            foreach (string strata in this.StratvarList)
                                            {
                                                R3[strata] = R[strata];
                                            }
                                        }
                                        KeyP.Value.Rows.Add(R3);
                                    }
                                    else
                                    {
                                        double temp = 0;
                                        if (double.TryParse(R[this.WeightVar].ToString(), out temp))
                                        {
                                            R3          = Freq_ListSet[Key].NewRow();
                                            R3["value"] = R[C.ColumnName];
                                            R3["count"] = temp;
                                            if (this.StratvarList != null)
                                            {
                                                foreach (string strata in this.StratvarList)
                                                {
                                                    R3[strata] = R[strata];
                                                }
                                            }
                                            KeyP.Value.Rows.Add(R3);
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }

                string[] tmpString2 = null;
                tmpString2 = SelectClause.ToString().Split(new string[] { " AND " }, StringSplitOptions.None);
                bool appendWithAND = false;
                HTMLString.Append("<p><b>");
                if (!(tmpString2.Length == 1 && tmpString2[0] == ""))
                {
                    foreach (string tempString in tmpString2)
                    {
                        string[] tmpString = null;

                        tmpString = tempString.ToString().Split('=');
                        if (tmpString.Length == 1)
                        {
                            tmpString = tempString.ToString().Split(new string[] { " is " }, StringSplitOptions.None);
                        }
                        string newSelectClause = string.Empty;

                        bool   variableExists = this.Context.EpiViewVariableList.ContainsKey(tmpString[0].Trim());
                        string dataType       = DT.Columns[tmpString[0].Trim()].DataType.ToString();
                        bool   isByte         = dataType.Equals("System.Byte");
                        bool   isBool         = dataType.Equals("System.Boolean");

                        if (variableExists && (isByte || isBool))
                        {
                            if (tmpString[1].ToUpperInvariant().Contains("FALSE"))
                            {
                                tmpString[1] = this.config.Settings.RepresentationOfNo;
                            }
                            else if (tmpString[1].ToUpperInvariant().Contains("TRUE"))
                            {
                                tmpString[1] = this.config.Settings.RepresentationOfYes;
                            }

                            if (tmpString[1].Contains("1"))
                            {
                                tmpString[1] = this.config.Settings.RepresentationOfYes;
                            }
                            else if (tmpString[1].Contains("0"))
                            {
                                tmpString[1] = this.config.Settings.RepresentationOfNo;
                            }
                            else if (tmpString[1].ToLowerInvariant().Contains("null"))
                            {
                                tmpString[1] = this.config.Settings.RepresentationOfMissing;
                            }
                            newSelectClause = tmpString[0] + "=" + tmpString[1].ToString();
                        }
                        else
                        {
                            newSelectClause = tempString;
                        }

                        if (appendWithAND)
                        {
                            HTMLString.Append(" AND " + newSelectClause.Replace("''", "'"));
                        }
                        else
                        {
                            HTMLString.Append(newSelectClause.Replace("''", "'"));
                        }

                        appendWithAND = true;
                    }
                }
                HTMLString.Append("</b></p>");

                foreach (System.Collections.Generic.KeyValuePair <string, System.Data.DataTable> Key in Freq_ListSet)
                {
                    double Mode      = 0.0;
                    double ModeCount = 0.0;
                    double mean      = 0.0;
                    double variance  = 0.0;

                    System.Data.DataRow[] tempRows = Key.Value.Select("", "value");

                    if (config["include-missing"].ToUpperInvariant() == "FALSE")
                    {
                        tempRows = Key.Value.Select(string.Format(" varname='{0}' and [value] is NOT NULL ", Key.Key), "value");
                    }
                    else
                    {
                        tempRows = Key.Value.Select("", "value");
                    }

                    //var Rows = tempRows.OrderByDescending(item => item["count"]);

                    int    n       = 0;
                    double std_dev = 0.0;
                    double Sum     = 0.0;
                    double Sum_Sqr = 0.0;

                    double Total = 0;
                    double Min   = 0.0;
                    double Max   = 0.0;

                    foreach (System.Data.DataRow R in tempRows)
                    {
                        double temp;
                        double.TryParse(R["count"].ToString(), out temp);
                        Total += temp;
                    }

                    foreach (System.Data.DataRow R in tempRows)
                    {
                        if (!string.IsNullOrEmpty(this.OutTable))
                        {
                            DataRow newRow = OutDataTable.NewRow();
                            if (this.StratvarList != null)
                            {
                                foreach (string stratavar in this.StratvarList)
                                {
                                    newRow[stratavar] = R[stratavar];
                                }
                            }
                            newRow[Key.Key]   = R["value"];
                            newRow["VARNAME"] = Key.Key;
                            newRow["PERCENT"] = (((double)R["count"]) / Total) * 100.0;
                            newRow["COUNT"]   = R["count"];
                            OutDataTable.Rows.Add(newRow);
                        }
                    }

                    HTMLString.Append("<table cellpadding=\"2\">");
                    HTMLString.Append("<tr><th>");
                    if (this.Context.EpiViewVariableList.ContainsKey(Key.Key) && this.config.Settings.ShowCompletePrompt)
                    {
                        HTMLString.Append(this.Context.EpiViewVariableList[Key.Key].Prompt);
                    }
                    else
                    {
                        HTMLString.Append(Key.Key);
                    }
                    HTMLString.Append("</th><th>Frequency</th><th>Percent</th><th>Cum. Percent</th><th style=\"width:100px\">&nbsp;</th></tr>");
                    double           AccumulatedTotal = 0;
                    List <ConfLimit> confLimits       = new List <ConfLimit>();
                    int obs = 0;
                    foreach (System.Data.DataRow R in tempRows)
                    {
                        double x;

                        Double.TryParse(R["value"].ToString(), out x);

                        if (obs == 0)
                        {
                            Max = Min = x;
                        }
                        else
                        {
                            Max = x;
                        }

                        obs++;
                        n++;
                        Sum     += x;
                        Sum_Sqr += x * x;

                        double currrentCount;
                        double.TryParse(R["count"].ToString(), out currrentCount);

                        if (currrentCount > ModeCount)
                        {
                            ModeCount = currrentCount;
                            Mode      = x;
                        }

                        AccumulatedTotal += currrentCount;

                        HTMLString.Append("<tr>");
                        HTMLString.Append("<td><strong>");

                        if (Context.EpiViewVariableList.ContainsKey(Key.Key))
                        {
                            int      dataTypeCode = Context.EpiViewVariableList[Key.Key].DataType.GetHashCode();
                            DataType dataType     = (DataType)dataTypeCode;
                            GetPrintValue(Key.Key, R["value"], config, HTMLString, dataType);
                        }
                        else
                        {
                            GetPrintValue(Key.Key, R["value"], config, HTMLString);
                        }

                        HTMLString.Append("</strong></td>");
                        HTMLString.Append("<td align=\"right\">");
                        HTMLString.Append(currrentCount.ToString());
                        HTMLString.Append("</td>");
                        HTMLString.Append("<td align=\"right\">");
                        HTMLString.Append(ConvertToPercent(currrentCount / Total));
                        HTMLString.Append("</td>");
                        HTMLString.Append("<td align=\"right\">");
                        HTMLString.Append(ConvertToPercent(AccumulatedTotal / Total));
                        HTMLString.Append("</td><td><div class=PercentBar_Summary style=\"width:" + ConvertToPixelLength(currrentCount / Total) + "\">&nbsp;</div></td>");
                        HTMLString.Append("</tr>");
                        confLimits.Add(GetConfLimit(GetPrintValue(Key.Key, R["value"], config), (float)currrentCount, (float)Total));
                    }

                    mean     = Sum / n;
                    variance = (Sum_Sqr - Sum * mean) / (n - 1);
                    std_dev  = calcStd_Dev(tempRows, mean);

                    HTMLString.Append("<tr>");
                    HTMLString.Append("<td><strong>Total</strong></td><td align=\"right\">");
                    HTMLString.Append(Total.ToString());
                    HTMLString.Append("</td><td align=\"right\">" + ConvertToPercent(1) + "</td><td align=\"right\">" + ConvertToPercent(1) + "</td><td><div class=PercentBar_Totals style=\"width:100%\">&nbsp;</div></td><tr>");

                    HTMLString.Append("</table>");

                    HTMLString.Append("<BR CLEAR=ALL/>");
                    if (Total < 300)
                    {
                        HTMLString.Append("<TABLE> <TD Class='Stats' ColSpan=\"3\"><B>Exact 95% Conf Limits</B></TD>");
                    }
                    else
                    {
                        HTMLString.Append("<TABLE> <TD Class='Stats' ColSpan=\"3\"><B>Wilson 95% Conf Limits</B></TD>");
                    }
                    foreach (ConfLimit cl in confLimits)
                    {
                        HTMLString.Append("<TR><TD Class='Stats'>" + cl.Value + "</TD><TD Class='Stats'>" + ConvertToPercent(cl.Lower) + "</TD><TD Class='Stats'>" + ConvertToPercent(cl.Upper) + "</TD></TR>");
                    }
                    HTMLString.Append("</TABLE>");
                }
            }
            if (!string.IsNullOrEmpty(this.OutTable))
            {
                this.Context.OutTable(OutDataTable);
            }

            Dictionary <string, string> args = new Dictionary <string, string>();

            args.Add("COMMANDNAME", "FREQ");
            args.Add("COMMANDTEXT", commandText.Trim());
            args.Add("HTMLRESULTS", HTMLString.ToString());

            this.Context.Display(args);
        }