예제 #1
0
        public static IEnumerable <Tuple <int, LocationInfo> > GetSimilarElements(
            CstGenerator cstGenerator, IEnumerable <LocationInfo> locations,
            IEnumerable <FileInfo> targets,
            int range = 5, bool inner = true, bool outer = true)
        {
            var path2Ast = new Dictionary <string, CstNode>();
            var paths    = locations.Select(l => l.FileInfo).Concat(targets)
                           .Select(f => f.FullName)
                           .ToHashSet();

            foreach (var path in paths)
            {
                path2Ast.Add(path, cstGenerator.GenerateTreeFromCodePath(path));
            }

            var elements = new List <CstNode>();

            foreach (var location in locations)
            {
                var root = path2Ast[location.FileInfo.FullName];
                elements.Add(location.CodeRange.FindOutermostNode(root));
            }
            var names = AdoptNodeNames(elements);

            var candidates = new Dictionary <string, IEnumerable <CstNode> >();

            foreach (var keyAndValue in path2Ast)
            {
                candidates.Add(
                    keyAndValue.Key,
                    keyAndValue.Value.Descendants()
                    .Where(e => names.Contains(e.Name)));
            }
            var commonKeys = elements.GetCommonKeys(range, true, true);

            return(candidates.SelectMany(
                       kv => {
                var fileInfo = new FileInfo(kv.Key);
                return kv.Value.Select(
                    e => Tuple.Create(
                        e.GetSurroundingKeys(range, inner, outer)
                        .Count(commonKeys.Contains),
                        e))
                .Select(
                    t => Tuple.Create(
                        t.Item1, new LocationInfo {
                    FileInfo = fileInfo,
                    CodeRange = CodeRange.Locate(t.Item2),
                }));
            })
                   .OrderByDescending(t => t.Item1));
        }
예제 #2
0
        public void CalculateMetrics(LearningExperiment exp, string projectPath, int starCount)
        {
            if (!(exp is JavaSuperComplexBranchExperiment))
            {
                return;
            }
            var allPaths =
                Directory.GetFiles(projectPath, SearchPattern, SearchOption.AllDirectories)
                .ToList();
            var allNodes = allPaths.Select(p => Generator.GenerateTreeFromCodePath(p))
                           .SelectMany(r => r.DescendantsAndSelf());
            var statementCount = 0;
            var branchCount    = 0;

            foreach (var node in allNodes)
            {
                if (node.Name == "statement")
                {
                    switch (node.FirstChild.TokenText)
                    {
                    case "if":
                    case "while":
                    case "do":
                    case "switch":
                        branchCount++;
                        break;
                    }
                }
                else if (node.Name == "conditionalExpression" && node.Children().Skip(1).Any())
                {
                    branchCount++;
                }
                else if (node.Name == "switchLabel" && node.FirstChild.TokenText == "case")
                {
                    branchCount++;
                }
                else if (node.Name == "statement" || node.Name == "localVariableDeclarationStatement")
                {
                    statementCount++;
                }
            }
            var writer = Writers[exp.GetType().Name];

            writer.Write(statementCount + ",");
            writer.Write(branchCount + ",");
            writer.WriteLine();
            writer.Flush();
        }
예제 #3
0
        public static IEnumerable<Tuple<int, LocationInfo>> GetSimilarElements(
                CstGenerator cstGenerator, IEnumerable<LocationInfo> locations,
                IEnumerable<FileInfo> targets,
                int range = 5, bool inner = true, bool outer = true) {
            var path2Ast = new Dictionary<string, CstNode>();
            var paths = locations.Select(l => l.FileInfo).Concat(targets)
                    .Select(f => f.FullName)
                    .ToHashSet();
            foreach (var path in paths) {
                path2Ast.Add(path, cstGenerator.GenerateTreeFromCodePath(path));
            }

            var elements = new List<CstNode>();
            foreach (var location in locations) {
                var root = path2Ast[location.FileInfo.FullName];
                elements.Add(location.CodeRange.FindOutermostNode(root));
            }
            var names = AdoptNodeNames(elements);

            var candidates = new Dictionary<string, IEnumerable<CstNode>>();
            foreach (var keyAndValue in path2Ast) {
                candidates.Add(
                        keyAndValue.Key,
                        keyAndValue.Value.Descendants()
                                .Where(e => names.Contains(e.Name)));
            }
            var commonKeys = elements.GetCommonKeys(range, true, true);
            return candidates.SelectMany(
                    kv => {
                        var fileInfo = new FileInfo(kv.Key);
                        return kv.Value.Select(
                                e => Tuple.Create(
                                        e.GetSurroundingKeys(range, inner, outer)
                                                .Count(commonKeys.Contains),
                                        e))
                                .Select(
                                        t => Tuple.Create(
                                                t.Item1, new LocationInfo {
                                                    FileInfo = fileInfo,
                                                    CodeRange = CodeRange.Locate(t.Item2),
                                                }));
                    })
                    .OrderByDescending(t => t.Item1);
        }