Ejemplo n.º 1
0
 public string SetFile2(string filename)
 {
     if (file2 != null)
     {
         file2 = null;
     }
     file2 = new FileCode(filename);
     return(file2.Body);
 }
Ejemplo n.º 2
0
 public string SetFile1(string filename)
 {
     if (file1 != null)
     {
         file1 = null;
     }
     file1 = new FileCode(filename);
     return(file1.Body);
 }
Ejemplo n.º 3
0
        public static double CompareMethods(FileCode file1, FileCode file2, double accuracy, int minLength, ref List <Match> pMatches, ref List <Match> tMatches, ref List <MatchLink> links)
        {
            if (minLength <= 0)
            {
                minLength = 1;
            }
            List <Match> matches  = new List <Match>();
            List <Match> matches2 = new List <Match>();
            List <CodeObjectInterface> file1Methods  = file1.Objects.FindAll(x => x.Type == ObjectType.Method && x.Token.Length > 0);
            List <CodeObjectInterface> file2Methods  = file2.Objects.FindAll(x => x.Type == ObjectType.Method && x.Token.Length > 0);
            List <CodeObjectInterface> ignoreMethods = new List <CodeObjectInterface>();

            links = new List <MatchLink>();

            double coef = 0;

            foreach (CodeObjectInterface P in file1Methods)
            {
                List <TileMatch>           bufferMatches = new List <TileMatch>();
                CodeObjectInterface        bufferMeth    = null;
                List <CodeObjectInterface> ignore        = new List <CodeObjectInterface>();
                List <CodeObjectInterface> tList         = new List <CodeObjectInterface>();
                double max = 0;
                while (true)
                {
                    max = 0;
                    foreach (CodeObjectInterface T in file2Methods)
                    {
                        if (ignore.Contains(T))
                        {
                            continue;
                        }
                        double sum = JackardCoef(P.Token, T.Token, minLength);
                        if (max < sum || T.Token == P.Token)
                        {
                            bufferMeth = T;
                            max        = sum;
                        }
                    }
                    if (bufferMeth == null)
                    {
                        break;
                    }
                    bool flag = false;
                    foreach (CodeObjectInterface buffP in file1Methods)
                    {
                        double sum = JackardCoef(buffP.Token, bufferMeth.Token, minLength);
                        if (max < sum)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        ignore.Add(bufferMeth);
                        bufferMeth = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (bufferMeth == null || max < accuracy)
                {
                    continue;
                }


                bufferMatches = GreedyStringTiling(P.Token, bufferMeth.Token, minLength);

                ignoreMethods.Add(bufferMeth);
                int commonLength = 0;

                bool fl1 = false, fl2 = false;
                if (bufferMatches.Count == 1 && bufferMatches[0].match.Length == P.Token.Length && bufferMatches[0].match.Length == bufferMeth.Token.Length)
                {
                    matches.Add(new DiplomProject1.Match(P.Position, P.Length));
                    fl2 = true;
                    matches2.Add(new DiplomProject1.Match(bufferMeth.Position, bufferMeth.Length));
                    fl1 = true;
                    links.Add(new MatchLink(P, bufferMeth, new DiplomProject1.Match(P.Position, P.Length), new DiplomProject1.Match(bufferMeth.Position, bufferMeth.Length)));
                }

                foreach (TileMatch match in bufferMatches)
                {
                    for (int i = 0; i < match.match.Length; i++)
                    {
                        DiplomProject1.Match matchP = new DiplomProject1.Match(P.RecursiveChildrenList[match.p + i].Position, P.RecursiveChildrenList[match.p + i].Length);
                        if (!fl2)
                        {
                            matches.Add(matchP);
                        }
                        DiplomProject1.Match matchT = new DiplomProject1.Match(bufferMeth.RecursiveChildrenList[match.t + i].Position, bufferMeth.RecursiveChildrenList[match.t + i].Length);
                        if (!fl1)
                        {
                            matches2.Add(matchT);
                        }
                        links.Add(new MatchLink(P.RecursiveChildrenList[match.p + i], bufferMeth.RecursiveChildrenList[match.t + i], matchP, matchT));
                    }
                    commonLength += match.match.Length;
                }

                coef += SimCoeff(commonLength, bufferMeth.Token.Length, P.Token.Length);
            }
            double commonCoef = coef / file1Methods.Count;

            coef = 0;
            foreach (CodeObjectInterface P in file2Methods)
            {
                if (ignoreMethods.IndexOf(P) >= 0)
                {
                    continue;
                }
                List <TileMatch>           bufferMatches = new List <TileMatch>();
                CodeObjectInterface        bufferMeth    = null;
                List <CodeObjectInterface> ignore        = new List <CodeObjectInterface>();
                double max = 0;
                while (true)
                {
                    max = 0;
                    foreach (CodeObjectInterface T in file1Methods)
                    {
                        if (ignore.Contains(T))
                        {
                            continue;
                        }
                        double sum = JackardCoef(P.Token, T.Token, minLength);
                        if (max < sum || T.Token == P.Token)
                        {
                            bufferMeth = T;
                            max        = sum;
                        }
                    }
                    if (bufferMeth == null)
                    {
                        break;
                    }
                    bool flag = false;
                    foreach (CodeObjectInterface buffP in file2Methods)
                    {
                        double sum = JackardCoef(buffP.Token, bufferMeth.Token, minLength);
                        if (max < sum)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        ignore.Add(bufferMeth);
                        bufferMeth = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (bufferMeth == null || max < accuracy)
                {
                    continue;
                }


                bufferMatches = GreedyStringTiling(P.Token, bufferMeth.Token, minLength);

                int  commonLength = 0;
                bool fl1 = false, fl2 = false;
                if (bufferMatches.Count == 1 && bufferMatches[0].match.Length == P.Token.Length && bufferMatches[0].match.Length == bufferMeth.Token.Length)
                {
                    matches2.Add(new DiplomProject1.Match(P.Position, P.Length));
                    fl2 = true;
                    matches.Add(new DiplomProject1.Match(bufferMeth.Position, bufferMeth.Length));
                    fl1 = true;
                    links.Add(new MatchLink(bufferMeth, P, new DiplomProject1.Match(bufferMeth.Position, bufferMeth.Length), new DiplomProject1.Match(P.Position, P.Length)));
                }
                else
                {
                    foreach (TileMatch match in bufferMatches)
                    {
                        for (int i = 0; i < match.match.Length; i++)
                        {
                            DiplomProject1.Match matchT = new DiplomProject1.Match(P.RecursiveChildrenList[match.p + i].Position, P.RecursiveChildrenList[match.p + i].Length);
                            if (!fl2)
                            {
                                matches2.Add(matchT);
                            }
                            DiplomProject1.Match matchP = new DiplomProject1.Match(bufferMeth.RecursiveChildrenList[match.t + i].Position, bufferMeth.RecursiveChildrenList[match.t + i].Length);
                            if (!fl1)
                            {
                                matches.Add(matchP);
                            }
                            links.Add(new MatchLink(bufferMeth.RecursiveChildrenList[match.t + i], P.RecursiveChildrenList[match.p + i], matchP, matchT));
                        }
                        commonLength += match.match.Length;
                    }
                }

                coef += SimCoeff(commonLength, bufferMeth.Token.Length, P.Token.Length);
            }
            commonCoef += coef / (file2Methods.Count);
            pMatches    = matches;
            tMatches    = matches2;
            if (commonCoef > 1)
            {
                commonCoef = 1;
            }
            return(commonCoef);
        }
Ejemplo n.º 4
0
 public void ClearFile2()
 {
     file2 = null;
     links.Clear();
 }
Ejemplo n.º 5
0
 public void ClearFile1()
 {
     file1 = null;
     links.Clear();
 }