Esempio n. 1
0
        public void startInference(UDCTree UDCHierarchy, int depth = 0)
        {
            SubtypeMiner miner        = null;
            var          typesAtDepth = UDCHierarchy.GetAllTypesAtDepth(depth);

            foreach (var v in typesAtDepth)
            {
                UDCTree subTree = new UDCTree(v);
                Console.WriteLine("Running Inference for {0} ...", v.Value.MetadataName);
                var typeNamesOfDescendants = UDCHierarchy.GetTypeNamesOfDescendants(v);
                miner = new SubtypeMiner(new HashSet <string>(typeNamesOfDescendants), _typeRelations, 50);
                SubTreeResults res = new SubTreeResults(subTree, miner);
                MiningResults.Add(res);

                if (miner.NumRelationships == 0)
                {
                    continue;
                }
                res.computeScores();
                try
                {
                    _typeRelations.ToDot("results/" + v.Value.ToDisplayString() + ".dot", _pathProcessor, res.clusteringResult);
                    writeResultsToDisk(_pathProcessor, res, ResultsDirectory + v.Value.ToDisplayString() + "_result.txt");
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                    continue;
                }
            }
        }
Esempio n. 2
0
        public void startTypeSpecificNameFlowInference(UDCTree UDCHierarchy, int depth = 0)
        {
            var          typeLocationDict = UDCHierarchy.getUDTLineSpans();
            SubtypeMiner miner            = null;
            var          typesAtDepth     = UDCHierarchy.GetAllTypesAtDepth(depth);

            foreach (var v in UDCHierarchy.getAllTypes())
            {
                if (v.Value.Locations.Length == 0 || !v.Value.Locations.All(x => x.IsInSource))
                {
                    continue;
                }
                UDCTree subTree = new UDCTree(v);
                Console.WriteLine("Running Inference for {0} ...", v.Value.MetadataName);
                miner = new SubtypeMiner(null, _typeRelations, 50, true, v.Value);
                SubTreeResults res = new SubTreeResults(subTree, miner);

                try {
                    MiningResults.Add(res);
                    if (miner.NumRelationships == 0)
                    {
                        continue;
                    }
                    res.computeScores();
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                    continue;
                }
                try
                {
                    //_typeRelations.ToDot(ResultsDirectory + v.Value.ToDisplayString() + ".dot", _pathProcessor, res.clusteringResult);
                    writeResultsToDisk(_pathProcessor, res, ResultsDirectory + v.Value.ToDisplayString() + "_result.txt", miner.GetLineSpanOfType(v.Value));
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                    continue;
                }
            }
        }
Esempio n. 3
0
        public void startConsolidatedInference(UDCTree UDCHierarchy, int depth = 0)
        {
            SubtypeMiner  miner        = null;
            List <string> alltypes     = new List <string>();
            var           typesAtDepth = UDCHierarchy.GetAllTypesAtDepth(depth);

            foreach (var v in typesAtDepth)
            {
                List <string> typeNamesOfDescendants = new List <string>();
                Console.WriteLine("Running Inference for {0} ...", v.Value.MetadataName);
                try{
                    typeNamesOfDescendants = UDCHierarchy.GetTypeNamesOfDescendants(v);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                alltypes = alltypes.Concat(typeNamesOfDescendants).ToList();
            }

            alltypes = randomUniformSample(alltypes);
            UDCTree subTree = new UDCTree(typesAtDepth.First());

            miner = new SubtypeMiner(new HashSet <string>(alltypes), _typeRelations, 50);
            SubTreeResults res = new SubTreeResults(subTree, miner);

            MiningResults.Add(res);

            if (miner.NumRelationships > 0)
            {
                res.computeScores();
                try
                {
                    //_typeRelations.ToDot(ResultsDirectory + "consolidated.dot", _pathProcessor, res.clusteringResult);
                    writeResultsToDisk(_pathProcessor, res, ResultsDirectory + "consolidated" + "_result.txt");
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e.Message);
                }
            }
        }
Esempio n. 4
0
        public void writeResultsToDisk(
            Func <string, int, string> pathProcessor, SubTreeResults res, string filename = "results/cluster_dict.txt", int linespan = 0)
        {
            /*try
             * {
             *  using (var writer = new StreamWriter(filename))
             *  {
             *      int i = 0;
             *      foreach (var color in res.clusteringResult)
             *      {
             *          writer.WriteLine($"===============Cluster {i + 1}: Parents: [{string.Join(", ", res.ClusterParents[i].Select(k => k + 1))}]===================");
             *          i++;
             *          foreach (AbstractNode node in color)
             *          {
             *              string path;
             *              if (node.Location != null)
             *              {
             *                  if (node.Location.SourceTree != null)
             *                  {
             *                      path = pathProcessor(node.Location.SourceTree.FilePath,
             *                          node.Location.GetLineSpan().StartLinePosition.Line);
             *                  }
             *                  else
             *                  {
             *                      path = node.Location.ToString();
             *                  }
             *              }
             *              else
             *              {
             *                  path = "unknown path";
             *              }
             *              //writer.WriteLine($"{node} at {path}");
             *              writer.WriteLine($"{node}");
             *          }
             *      }
             *  }
             *  //Console.WriteLine("Average purity: {0:0.000}", avergagePurity);
             * }
             * catch (Exception ex)
             * {
             *  Console.WriteLine("{0}", ex.Message);
             * }*/

            try
            {
                Dictionary <string, Object> clusDict = new Dictionary <string, Object>();
                using (var writer = new StreamWriter(filename))
                {
                    int i = 0;
                    foreach (var color in res.clusteringResult)
                    {
                        i++;
                        var d = new List <Dictionary <string, string> >();
                        foreach (AbstractNode node in color)
                        {
                            var v = new Dictionary <string, string>();
                            v.Add("Type", node.Type);
                            v.Add("Name", node.Name);
                            v.Add("Location", node.Location.GetMappedLineSpan().Path);
                            v.Add("Linespan", node.Location.GetMappedLineSpan().StartLinePosition.Line.ToString());
                            d.Add(v);
                        }
                        try
                        {
                            clusDict.Add(i.ToString(), d);
                        }
                        catch
                        {
                            clusDict[i.ToString()] = d;
                        }
                        clusDict["linespan"] = linespan;
                    }
                    writer.WriteLine(JsonConvert.SerializeObject(clusDict, Formatting.Indented));
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}", ex.Message);
            }

            /*
             * var typename = res.SubTree.UDCTreeRoots.First().Value.Name;
             * Dictionary < string, List< Tuple < int, double, double>>> scores = new Dictionary<string, List<Tuple<int, double, double>>>();
             * scores.Add("clusters", res.TreeScores);
             * // write scores to a file
             * string scores_str = JsonConvert.SerializeObject(scores, Formatting.Indented);
             * // Write the string to a file.
             * var file = new System.IO.StreamWriter("results/"+ typename +"_scores.txt");
             * file.WriteLine(scores_str);
             * file.Close();
             */
        }