Esempio n. 1
0
        private void TestForClusterAnalysis()
        {
            var common = new SQLCommon
            {
                SkylineType =
                    new SkylineBNL()
                {
                    Provider = Helper.ProviderName, ConnectionString = Helper.ConnectionString
                },
                ShowInternalAttributes = true
            };

            DbProviderFactory factory = DbProviderFactories.GetFactory(Helper.ProviderName);

            // use the factory object to create Data access objects.
            DbConnection connection = factory.CreateConnection();

            // will return the connection object (i.e. SqlConnection ...)
            connection.ConnectionString = Helper.ConnectionString;

            var dt = new DataTable();

            connection.Open();

            DbDataAdapter dap           = factory.CreateDataAdapter();
            DbCommand     selectCommand = connection.CreateCommand();

            selectCommand.CommandTimeout = 0; //infinite timeout

            string strQuery;
            string operators;
            int    numberOfRecords;

            string[] parameter;

            string ansiSql =
                common.GetAnsiSqlFromPrefSqlModel(common.GetPrefSqlModelFromPreferenceSql(_entireSkylineSql));

            prefSQL.SQLParser.Helper.DetermineParameters(ansiSql, out parameter, out strQuery, out operators,
                                                         out numberOfRecords);

            selectCommand.CommandText = strQuery;
            dap.SelectCommand         = selectCommand;
            dt = new DataTable();

            dap.Fill(dt);

            DataTable entireSkylineDataTable = common.ParseAndExecutePrefSQL(Helper.ConnectionString,
                                                                             Helper.ProviderName,
                                                                             _entireSkylineSql);

            int[] skylineAttributeColumns = SkylineSamplingHelper.GetSkylineAttributeColumns(entireSkylineDataTable);
            IReadOnlyDictionary <long, object[]> entireSkylineNormalized =
                prefSQL.SQLSkyline.Helper.GetDatabaseAccessibleByUniqueId(entireSkylineDataTable, 0);

            SkylineSamplingHelper.NormalizeColumns(entireSkylineNormalized, skylineAttributeColumns);

            DataTable sampleSkylineDataTable = common.ParseAndExecutePrefSQL(Helper.ConnectionString,
                                                                             Helper.ProviderName,
                                                                             _skylineSampleSql);
            IReadOnlyDictionary <long, object[]> sampleSkylineNormalized =
                prefSQL.SQLSkyline.Helper.GetDatabaseAccessibleByUniqueId(sampleSkylineDataTable, 0);

            SkylineSamplingHelper.NormalizeColumns(sampleSkylineNormalized, skylineAttributeColumns);

            for (var i = 0; i < skylineAttributeColumns.Length; i++)
            {
                dt.Columns.RemoveAt(0);
            }

            IReadOnlyDictionary <long, object[]> full = prefSQL.SQLSkyline.Helper.GetDatabaseAccessibleByUniqueId(dt, 0);

            SkylineSamplingHelper.NormalizeColumns(full, skylineAttributeColumns);

            ClusterAnalysis.CalcMedians(full, skylineAttributeColumns);

            IReadOnlyDictionary <BigInteger, List <IReadOnlyDictionary <long, object[]> > > entireBuckets =
                ClusterAnalysis.GetBuckets(entireSkylineNormalized, skylineAttributeColumns);
            IReadOnlyDictionary <BigInteger, List <IReadOnlyDictionary <long, object[]> > > sampleBuckets =
                ClusterAnalysis.GetBuckets(sampleSkylineNormalized, skylineAttributeColumns);

            //IReadOnlyDictionary<int, List<IReadOnlyDictionary<long, object[]>>> aggregatedEntireBuckets =
            //    ClusterAnalysis.GetAggregatedBuckets(entireBuckets);
            //IReadOnlyDictionary<int, List<IReadOnlyDictionary<long, object[]>>> aggregatedSampleBuckets =
            //    ClusterAnalysis.GetAggregatedBuckets(sampleBuckets);

            IReadOnlyDictionary <BigInteger, List <IReadOnlyDictionary <long, object[]> > > fullB =
                ClusterAnalysis.GetBuckets(full, skylineAttributeColumns);
            // IReadOnlyDictionary<int, List<IReadOnlyDictionary<long, object[]>>> aFullB =
            //     ClusterAnalysis.GetAggregatedBuckets(fullB);

            IOrderedEnumerable <KeyValuePair <BigInteger, List <IReadOnlyDictionary <long, object[]> > > > sorted = fullB.OrderBy(l => l.Value.Count)
                                                                                                                    .ThenBy(l => l.Key);

            int len = Convert.ToInt32(Math.Pow(2, skylineAttributeColumns.Length));

            //for (var i = 0; i < len; i++)
            foreach (KeyValuePair <BigInteger, List <IReadOnlyDictionary <long, object[]> > > s in sorted)
            {
                BigInteger i             = s.Key;
                int        entire        = entireBuckets.ContainsKey(i) ? entireBuckets[i].Count : 0;
                int        sample        = sampleBuckets.ContainsKey(i) ? sampleBuckets[i].Count : 0;
                double     entirePercent = (double)entire / entireSkylineNormalized.Count;
                double     samplePercent = (double)sample / sampleSkylineNormalized.Count;
                int        fullX         = fullB.ContainsKey(i) ? fullB[i].Count : 0;
                double     fullP         = (double)fullX / full.Count;
                Console.WriteLine("-- {0,5} -- {5,6} ({6,7:P2} %) -- {1,6} ({3,7:P2} %) -- {2,6} ({4,7:P2} %)", i,
                                  entire, sample, entirePercent,
                                  samplePercent, fullX, fullP);
            }

            Console.WriteLine();
            Console.WriteLine("{0} - {1} - {2}", entireSkylineNormalized.Count, sampleSkylineNormalized.Count,
                              full.Count);
        }