private FileInfo DetectMovesFromFile(FileInfo fileInfo)
        {
            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         "=========================================" + Environment.NewLine +
                         "Detect Moves Start: " + DateTime.Now.ToString());

            LogProvider provider = new LogProvider();

            provider.OpenLog(fileInfo.FullName);

            MoveDetector moveDetector = MoveDetector.GetInstance();
            var          patterns     = moveDetector.DetectAsPatternInstances(provider);

            // Save the results to a file.
            DetectionResult result = new DetectionResult(provider.LogPath, patterns);

            result.SaveToFile(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name));
            result.ExportToCSV(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name, "csv"));

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(fileInfo.FullName);

            var documentChanges = provider.LoggedEvents.OfType <DocumentChange>().ToList();

            foreach (MovePatternInstance pattern in patterns)
            {
                int startIndex = documentChanges.IndexOf(pattern.PrimaryEvent as DocumentChange);

                Delete delete = documentChanges[startIndex + 0] as Delete;
                Insert insert = documentChanges[startIndex + 1] as Insert;

                // Same file, same place
                if (pattern.FromFile == pattern.ToFile && delete.Offset == insert.Offset)
                {
                    // Just cancel them out.
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, delete));
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, insert));
                }
                else
                {
                    // Transform Insert -> Move and then remove Delete.
                    SetMoveElement(xmlDoc, pattern, delete, insert);
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, delete));
                }
            }

            string newPath = Path.Combine(fileInfo.DirectoryName,
                                          _settings.Prefix + Path.GetFileNameWithoutExtension(fileInfo.Name) + _settings.Postfix + fileInfo.Extension);

            xmlDoc.Save(newPath);

            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         string.Format("{0} moves have been detected and written in the log" + Environment.NewLine, patterns.Count()));

            return(new FileInfo(newPath));
        }
        private FileInfo RemoveTyposFromFile(FileInfo fileInfo)
        {
            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         "=========================================" + Environment.NewLine +
                         "Remove Typos Start: " + DateTime.Now.ToString());

            LogProvider provider = new LogProvider();

            provider.OpenLog(fileInfo.FullName);

            TypoCorrectionDetector typoDetector = TypoCorrectionDetector.GetInstance();
            var patterns = typoDetector.DetectAsPatternInstances(provider);

            // Save the results to a file.
            DetectionResult result = new DetectionResult(provider.LogPath, patterns);

            result.SaveToFile(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name));
            result.ExportToCSV(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name, "csv"));

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(fileInfo.FullName);

            var documentChanges = provider.LoggedEvents.OfType <DocumentChange>().ToList();

            // This should be done in reverse order, to process consecutive typo corrections correctly.
            foreach (PatternInstance pattern in patterns.Reverse())
            {
                // Determine the type
                int startIndex = documentChanges.IndexOf(pattern.PrimaryEvent as DocumentChange);

                // Type 1: Insert -> Delete -> Insert
                if (documentChanges[startIndex + 1] is Delete)
                {
                    ProcessType1(xmlDoc, documentChanges, startIndex);
                }
                // Type 2: Insert -> Replace
                else if (documentChanges[startIndex + 1] is Replace)
                {
                    ProcessType2(xmlDoc, documentChanges, startIndex);
                }
            }

            string newPath = Path.Combine(fileInfo.DirectoryName,
                                          _settings.Prefix + Path.GetFileNameWithoutExtension(fileInfo.Name) + _settings.Postfix + fileInfo.Extension);

            xmlDoc.Save(newPath);

            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         string.Format("{0} typo corrections have been removed" + Environment.NewLine, patterns.Count()));

            return(new FileInfo(newPath));
        }
        private FileInfo FixRenamingsFromFile(FileInfo fileInfo)
        {
            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         "=========================================" + Environment.NewLine +
                         "Fix Renamings Start: " + DateTime.Now.ToString());

            LogProvider provider = new LogProvider();

            provider.OpenLog(fileInfo.FullName);

            RenamingDetector detector = RenamingDetector.GetInstance();
            var patterns = detector.DetectAsPatternInstances(provider);

            // Save the results to a file.
            DetectionResult result = new DetectionResult(provider.LogPath, patterns);

            result.SaveToFile(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name));
            result.ExportToCSV(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name, "csv"));

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(fileInfo.FullName);

            List <Event> renameEvents = provider.LoggedEvents
                                        .Where(x => x is DocumentChange || RenamingDetector.IsRenameCommand(x)).ToList();

            foreach (var pattern in patterns)
            {
                int renameIndex = renameEvents.IndexOf(pattern.PrimaryEvent);
                for (int i = 1; i <= pattern.PatternLength; ++i)
                {
                    XmlElement elem = Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, renameEvents[renameIndex + i]);
                    xmlDoc.DocumentElement.RemoveChild(elem);
                }
            }

            string newPath = Path.Combine(fileInfo.DirectoryName,
                                          _settings.Prefix + Path.GetFileNameWithoutExtension(fileInfo.Name) + _settings.Postfix + fileInfo.Extension);

            xmlDoc.Save(newPath);

            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         string.Format("{0} renamings have been fixed" + Environment.NewLine, patterns.Count()));

            return(new FileInfo(newPath));
        }
Esempio n. 4
0
        public override FileInfo Compute(FileInfo input)
        {
            LogProvider provider = new LogProvider();

            provider.OpenLog(input.FullName);

            BacktrackingDetector detector = new BacktrackingDetector();
            var patterns = detector.DetectAsPatternInstances(provider);

            // Save the results to a file.
            DetectionResult result = new DetectionResult(provider.LogPath, patterns);

            result.SaveToFile(GetSaveFileName(input.DirectoryName, input.Name));
            result.ExportToCSV(GetSaveFileName(input.DirectoryName, input.Name, "csv"));

            // Return the original input.
            return(input);
        }
        private void SavePatterns(string saveFilePath)
        {
            if (listViewPatterns.Items.Count == 0)
            {
                MessageBox.Show("No detected patterns to be saved!");
                return;
            }

            DetectionResult result = new DetectionResult(
                LogProvider.LogPath,
                listViewPatterns.Items
                .Cast <ListViewItem>()
                .Select(x => x.Tag)
                .Cast <PatternInstance>());

            result.SaveToFile(saveFilePath);

            // Also export to csv
            string csvFilePath = Path.Combine(
                Path.GetDirectoryName(saveFilePath),
                Path.GetFileNameWithoutExtension(saveFilePath) + ".csv");

            result.ExportToCSV(csvFilePath);
        }