Пример #1
0
 public MatchLink(CodeObjectInterface obj1, CodeObjectInterface obj2, Match p, Match t)
 {
     P      = obj1;
     T      = obj2;
     MatchP = p;
     MatchT = t;
 }
Пример #2
0
        public void GetMatchesForFile2Node(CodeObjectInterface node, ref List <Match> P)
        {
            List <MatchLink> result = links.FindAll(x => x.T == node);

            P = new List <Match>();

            foreach (MatchLink link in result)
            {
                P.Add(new DiplomProject1.Match(link.P.Position, link.P.CommonLength));
            }
        }
Пример #3
0
        protected void RecoveryPosition(CodeObjectInterface obj)
        {
            int          offset = 0;
            int          length = obj.Length;
            List <Coord> buffer = replaced.FindAll(x => x.newPos <= obj.Position);

            foreach (Coord coord in buffer)
            {
                offset += coord.length;
            }

            buffer = replaced.FindAll(x => x.newPos > obj.Position && x.newPos < obj.Position + obj.Length);
            foreach (Coord coord in buffer)
            {
                length += coord.length;
            }

            obj.Position += offset;
            obj.Length    = length;
        }
Пример #4
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);
        }