Пример #1
0
 public static void CalculateComplexity(Parser.Program program)
 {
     program.Xp = 0;
     program.fp = 0;
     program.gp = 0;
     Complexity(program, 0);
 }
Пример #2
0
 public void AddSubprogram(Parser.Program program)
 {
     if (!subprograms.Contains(program))
     {
         subprograms.Add(program);
     }
 }
Пример #3
0
        private void McClureRepeat(ref string sourceCode, Parser.Program ownerProgram)
        {
            string code           = sourceCode.Substring(sourceCode.IndexOf("repeat")).Trim();
            var    clureVariables = new List <McClureVariable>();
            string untilString    = code.Substring(McCabeMetrics.GetIndexEndOfRepeat(code));

            sourceCode  = sourceCode.Substring(McCabeMetrics.GetIndexEndOfRepeat(sourceCode));
            sourceCode  = sourceCode.Substring(sourceCode.IndexOf(";") + 1);
            untilString =
                untilString.Remove(untilString.IndexOf(";")).Substring(untilString.IndexOf("until") + 5);
            string[] variablesNames = GetNamesVariables(untilString);
            foreach (string name in variablesNames)
            {
                var variable = new McClureVariable();
                variable.OwnerProgram = ownerProgram;
                variable.Name         = name;
                clureVariables.Add(variable);
                variables.Add(variable);
            }
            foreach (McClureVariable variable in clureVariables)
            {
                AnalyzeBlock(code.Substring(6).Remove(McCabeMetrics.GetIndexEndOfRepeat(code)), variable, variable.Name,
                             0,
                             ownerProgram);
            }
        }
Пример #4
0
        private void McClureWhile(ref string sourceCode, Parser.Program ownerProgram)
        {
            string code           = sourceCode.Substring(sourceCode.IndexOf("while") + 5).Trim();
            var    clureVariables = new List <McClureVariable>();

            sourceCode = sourceCode.Substring(sourceCode.IndexOf("do") + 2);
            string[] variablesNames = GetNamesVariables(code.Remove(code.IndexOf("do")).Trim());
            code = code.Substring(code.IndexOf("do") + 2).Trim();
            foreach (string name in variablesNames)
            {
                var variable = new McClureVariable();
                variable.OwnerProgram = ownerProgram;
                variable.Name         = name;
                clureVariables.Add(variable);
                variables.Add(variable);
            }

            if (code.IndexOf("begin") == 0)
            {
                foreach (McClureVariable variable in clureVariables)
                {
                    AnalyzeBlock(code.Remove(Parser.GetIndexEndOfBegin(code)), variable, variable.Name, 0, ownerProgram);
                }
                sourceCode = sourceCode.Substring(Parser.GetIndexEndOfBegin(sourceCode));
            }
            else
            {
                foreach (McClureVariable variable in clureVariables)
                {
                    AnalyzeBlock(code.Remove(code.IndexOf(";") + 1), variable, variable.Name, 0, ownerProgram);
                }
                sourceCode = sourceCode.Substring(sourceCode.IndexOf(";"));
            }
        }
Пример #5
0
        private void AnalyzeBlock(string code, McClureVariable variable, string currentName, int lvl,
                                  Parser.Program currentProgram)
        {
            if (variable.J < lvl)
            {
                variable.J = lvl;
            }
            int i = 0;

            string[] stringSeparators = { "\r\n" };
            string[] lines            = code.Split(stringSeparators, StringSplitOptions.None);
            foreach (string line in lines)
            {
                foreach (Parser.Program subprogram in subprograms)
                {
                    if (subprogram != currentProgram)
                    {
                        int argumentIndex = FindArgumentIndex(variable, currentName, line, subprogram);
                        if (argumentIndex != -1)
                        {
                            variable.D++;
                            variable.AddSubprogram(subprogram);
                            if (subprogram.Arguments.Count > argumentIndex)
                            {
                                AnalyzeBlock(subprogram.BlockBeginEnd, variable, subprogram.Arguments[argumentIndex],
                                             lvl + 1, subprogram);
                            }
                        }
                    }
                }
            }
        }
Пример #6
0
        public void DrawGraph(Parser.Program program)
        {
            bitMap = new Bitmap(pictureBox.Width, pictureBox.Height);
            Graphics graphics = Graphics.FromImage(bitMap);

            graphics.Clear(Color.White);
            Draw(graphics, program, pictureBox.Width, 0, 10);
            pictureBox.Image = bitMap;
            pen.Width        = 4;
        }
Пример #7
0
 private void AddProgramToList(ref List <Parser.Program> listPrograms, Parser.Program program)
 {
     if (program != null)
     {
         listPrograms.Add(program);
         foreach (Parser.Program subprogram in program.Subprograms)
         {
             AddProgramToList(ref listPrograms, subprogram);
         }
     }
 }
Пример #8
0
 private void FillMcClure1(Parser.Program program)
 {
     FillDataGridMcClure1(program);
     foreach (Parser.Program subprogram in program.Subprograms)
     {
         if (program != null)
         {
             FillMcClure1(subprogram);
         }
     }
 }
Пример #9
0
        private void button1_Click(object sender, EventArgs e)
        {
            Parser.Program program  = Parser.FindProgram(textBoxSourceCode.Text.ToLower());
            var            programs = new List <Parser.Program>();

            AddProgramToList(ref programs, program);
            programs.Remove(program);
            var mcCabe = new McCabeMetrics();

            mcCabe.SetSubprograms(programs);
            int mc = mcCabe.CalculateMcCabeMetrics(program.BlockBeginEnd) + 1;

            MessageBox.Show("Цикломатическая сложность программы = " + mc);
        }
Пример #10
0
 public void Draw(Graphics graphics, Parser.Program program, int width, int x, int y)
 {
     if (program != null)
     {
         graphics.DrawRectangle(pen, x + (width - rectWidth) / 2, y, rectWidth, rectHeight);
         graphics.DrawString(program.Name, new Font(FontFamily.GenericMonospace, 10),
                             new SolidBrush(Color.Black), x + (width - rectWidth) / 2 + 3, y + 10);
         if (program.Subprograms.Count > 0)
         {
             int dx       = width / (program.Subprograms.Count);
             int subWidth = width / (program.Subprograms.Count);
             for (int i = 0; i < program.Subprograms.Count; i++)
             {
                 graphics.DrawLine(pen, x + width / 2, y + rectHeight, x + dx * i + subWidth / 2, y + Dy);
                 Parser.Program subprogram = program.Subprograms[i];
                 Draw(graphics, subprogram, subWidth, x + dx * i, y + Dy);
             }
         }
     }
 }
Пример #11
0
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView.Columns.Clear();
            dataGridView.Columns.Add("c1", "Name");
            dataGridView.Columns.Add("c2", "fp");
            dataGridView.Columns.Add("c3", "gp");
            dataGridView.Columns.Add("c4", "Xp");
            dataGridView.Columns.Add("c5", "Yp");
            dataGridView.Columns.Add("c6", "M(p)");
            dataGridView.Rows.Clear();
            dataGridView.RowCount = 1;

            Parser.Program program = Parser.FindProgram(textBoxSourceCode.Text.ToLower());
            if (program != null)
            {
                McClureMetrics.CalculateComplexity(program);
                FillMcClure1(program);
                mcClureForm = new FormMcClure();
                mcClureForm.Show();
                mcClureForm.DrawGraph(program);
            }
        }
Пример #12
0
        private static int FindArgumentIndex(McClureVariable variable, string currentName, string line,
                                             Parser.Program subprogram)
        {
            int result = -1;

            if (line.IndexOf(subprogram.Name) != -1)
            {
                if (line.IndexOf("(") != -1)
                {
                    line = line.Remove(line.IndexOf(")")).Substring(line.IndexOf("(") + 1);
                    string[] arguments = line.Split(',');
                    for (int i = 0; i < arguments.Length; i++)
                    {
                        if (arguments[i].Trim() == currentName)
                        {
                            result = i;
                        }
                    }
                }
            }
            return(result);
        }
Пример #13
0
        private void AnalyzeSubprogram(string sourceCode, Parser.Program ownerProgram)
        {
            int i = 0;

            while (i < sourceCode.Length)
            {
                string currentCode = sourceCode.Substring(i);

                if (currentCode.Trim().IndexOf("if") == 0)
                {
                    McClureIf(ref sourceCode, ownerProgram);
                    i = 0;
                }
                else if (currentCode.Trim().IndexOf("for") == 0)
                {
                    McClureFor(ref sourceCode, ownerProgram);
                    i = 0;
                }
                else if (currentCode.Trim().IndexOf("while") == 0)
                {
                    McClureWhile(ref sourceCode, ownerProgram);
                    i = 0;
                }
                else if (currentCode.Trim().IndexOf("repeat") == 0)
                {
                    McClureRepeat(ref sourceCode, ownerProgram);
                    i = 0;
                }
                else if (currentCode.Trim().IndexOf("case") == 0)
                {
                    McClureCase(ref sourceCode, ownerProgram);
                    i = 0;
                }
                else
                {
                    i++;
                }
            }
        }
Пример #14
0
 private static int Complexity(Parser.Program program, int lvl)
 {
     program.Xp = lvl;
     if (program.Subprograms.Count == 0)
     {
         program.Yp = 0;
     }
     else
     {
         for (int i = 0; i < program.Subprograms.Count; i++)
         {
             program.Subprograms[i].fp = i;
             program.Subprograms[i].gp = program.Subprograms.Count - 1;
             int maxLvl = Complexity(program.Subprograms[i], lvl + 1) + 1;
             if (program.Yp < maxLvl)
             {
                 program.Yp = maxLvl;
             }
         }
     }
     return(program.Yp);
 }
Пример #15
0
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView.Columns.Clear();

            dataGridView.Columns.Add("c0", "Owner Name");
            dataGridView.Columns.Add("c1", "Name");
            dataGridView.Columns.Add("c2", "D");
            dataGridView.Columns.Add("c3", "J");
            dataGridView.Columns.Add("c4", "n");
            dataGridView.Columns.Add("c5", "C");
            dataGridView.Rows.Clear();
            dataGridView.RowCount = 1;

            Parser.Program program  = Parser.FindProgram(textBoxSourceCode.Text.ToLower());
            var            programs = new List <Parser.Program>();

            AddProgramToList(ref programs, program);
            var mcClure = new McClureMetrics();

            mcClure.CalculateMcClure(programs);
            FillMcClure2(mcClure.GetVariables());
        }
Пример #16
0
        private void McClureFor(ref string sourceCode, Parser.Program ownerProgram)
        {
            string code = sourceCode.Substring(sourceCode.IndexOf("for") + 3).Trim();

            sourceCode = sourceCode.Substring(sourceCode.IndexOf("for") + 3);
            var variable = new McClureVariable();

            variable.OwnerProgram = ownerProgram;
            variable.Name         = code.Substring(0, code.IndexOf(":")).Trim();
            code = code.Substring(code.IndexOf("do") + 2).Trim();
            if (code.IndexOf("begin") == 0)
            {
                AnalyzeBlock(code.Remove(Parser.GetIndexEndOfBegin(code)), variable, variable.Name, 0, ownerProgram);
                sourceCode = sourceCode.Substring(Parser.GetIndexEndOfBegin(sourceCode));
            }
            else
            {
                AnalyzeBlock(code.Remove(code.IndexOf(";") + 1), variable, variable.Name, 0, ownerProgram);
                sourceCode = sourceCode.Substring(sourceCode.IndexOf(";"));
            }
            variables.Add(variable);
        }
Пример #17
0
        private void McClureCase(ref string sourceCode, Parser.Program ownerProgram)
        {
            string code = sourceCode.Substring(sourceCode.IndexOf("case") + 4).Trim();

            var variable = new McClureVariable();

            variable.OwnerProgram = ownerProgram;
            variable.Name         = code.Remove(code.IndexOf("of")).Trim();
            variables.Add(variable);
            sourceCode = sourceCode.Substring(sourceCode.IndexOf("of") + 2);
            code       = code.Substring(code.IndexOf("of") + 2).Trim();
            int i = 0;

            while (i < code.Length)
            {
                string currentCode = code.Substring(i);
                if (currentCode.IndexOf("end") != 0)
                {
                    if (currentCode.Trim().IndexOf(":") == 0)
                    {
                        string codeAfterValue = currentCode.Substring(currentCode.IndexOf(":") + 1);
                        if (codeAfterValue.Trim().IndexOf("begin") == 0)
                        {
                            i += Parser.GetIndexEndOfBegin(currentCode);
                        }
                        else
                        {
                            if ((codeAfterValue.IndexOf("else") != -1) &&
                                (codeAfterValue.IndexOf("else") < codeAfterValue.IndexOf(";")))
                            {
                                i += currentCode.IndexOf("else");
                            }
                            else
                            {
                                i += currentCode.IndexOf(";");
                            }
                        }
                    }
                    else
                    {
                        if (currentCode.Trim().IndexOf("else") == 0)
                        {
                            string codeAfterElse = currentCode.Substring(currentCode.IndexOf("else") + 4);
                            if (codeAfterElse.Trim().IndexOf("begin") == 0)
                            {
                                i += Parser.GetIndexEndOfBegin(currentCode);
                            }
                            else
                            {
                                i += currentCode.IndexOf(";");
                            }
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                else
                {
                    AnalyzeBlock(code.Substring(0, i + 3), variable, variable.Name, 0, ownerProgram);
                    sourceCode = sourceCode.Substring(i + 3);
                    break;
                }
            }
        }
Пример #18
0
 private void FillDataGridMcClure1(Parser.Program program)
 {
     dataGridView.Rows.Add(program.Declaration, program.fp, program.gp, program.Xp, program.Yp,
                           program.Complexity);
 }