Changeset 15911
- Timestamp:
- 04/18/18 10:22:49 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SentenceLogger.cs
r15910 r15911 2 2 using System.Diagnostics; 3 3 using System.IO; 4 using System.IO.Compression; 4 5 using HeuristicLab.Common; 5 6 using HeuristicLab.Core; 6 7 7 8 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { 8 class SentenceLogger : Item, IGrammarEnumerationAnalyzer {9 public class SentenceLogger : Item, IGrammarEnumerationAnalyzer { 9 10 private readonly string workingDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 10 11 private readonly string columnDelimiter = ";"; 11 private readonly string header = "hash;length; postfix;infix";12 private readonly string header = "hash;length;infix"; 12 13 13 14 private string distinctSentencesFileName; … … 27 28 28 29 public void Register(GrammarEnumerationAlgorithm algorithm) { 29 algorithm.Started += GrammarEnumerationAlgorithmOnStarted;30 30 algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped; 31 31 algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped; … … 36 36 37 37 public void Deregister(GrammarEnumerationAlgorithm algorithm) { 38 algorithm.Started -= GrammarEnumerationAlgorithmOnStarted;39 38 algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped; 40 39 algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped; … … 44 43 } 45 44 46 private void GrammarEnumerationAlgorithmOnStarted(object sender, EventArgs eventArgs) { 47 string datePostfix = $"_{DateTime.Now:yyyy-MM-dd_HH-mm}_TreeSize-{((GrammarEnumerationAlgorithm) sender).MaxComplexity}.csv"; 45 private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) { 46 distinctSentencesFileTrace.Close(); 47 allSentencesFileTrace.Close(); 48 49 // Remove duplicates afterwards using bash commands from the git bash 50 // string bashExecutable = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Programs\Git\git-bash.exe"; 51 // 52 // string commandCreate = $"echo \"{header}\" > {shortestDistinctSentencesFileName.Replace("\\", "/")}"; 53 // string commandFill = $"sort -s {distinctSentencesFileName.Replace("\\", "/")} | uniq -w 10 >> {shortestDistinctSentencesFileName.Replace("\\", "/")}"; 54 // 55 // ProcessStartInfo startInfo = new ProcessStartInfo { 56 // WindowStyle = ProcessWindowStyle.Hidden, 57 // UseShellExecute = false, 58 // CreateNoWindow = true, 59 // FileName = bashExecutable, 60 // RedirectStandardError = true, 61 // RedirectStandardOutput = true, 62 // Arguments = $"-c '{commandCreate};{commandFill}'" 63 // }; 64 // Process.Start(startInfo); 65 } 66 67 private void Init(object sender) { 68 string datePostfix = $"_{DateTime.Now:yyyy-MM-dd_HH-mm}_TreeSize-{((GrammarEnumerationAlgorithm)sender).MaxComplexity}.csv.gz"; 48 69 distinctSentencesFileName = workingDir + @"\distinctSentences" + datePostfix; 49 70 allSentencesFileName = workingDir + @"\allSentences" + datePostfix; 50 71 shortestDistinctSentencesFileName = workingDir + @"\shortestDistinctSentences" + datePostfix; 51 72 52 distinctSentencesFileTrace = new TextWriterTraceListener(new FileStream(distinctSentencesFileName, FileMode.Create));53 allSentencesFileTrace = new TextWriterTraceListener(new FileStream(allSentencesFileName, FileMode.Create));73 distinctSentencesFileTrace = new TextWriterTraceListener(new GZipStream(new FileStream(distinctSentencesFileName, FileMode.Create), CompressionMode.Compress)); 74 allSentencesFileTrace = new TextWriterTraceListener(new GZipStream(new FileStream(allSentencesFileName, FileMode.Create), CompressionMode.Compress)); 54 75 ((StreamWriter)distinctSentencesFileTrace.Writer).AutoFlush = true; 55 76 ((StreamWriter)allSentencesFileTrace.Writer).AutoFlush = true; … … 58 79 } 59 80 60 private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) {61 distinctSentencesFileTrace.Close();62 allSentencesFileTrace.Close();63 64 // Remove duplicates afterwards using bash commands from the git bash65 string bashExecutable = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Programs\Git\git-bash.exe";66 67 string commandCreate = $"echo \"{header}\" > {shortestDistinctSentencesFileName.Replace("\\", "/")}";68 string commandFill = $"sort -s {distinctSentencesFileName.Replace("\\", "/")} | uniq -w 10 >> {shortestDistinctSentencesFileName.Replace("\\", "/")}";69 70 ProcessStartInfo startInfo = new ProcessStartInfo {71 WindowStyle = ProcessWindowStyle.Hidden,72 UseShellExecute = false,73 CreateNoWindow = true,74 FileName = bashExecutable,75 RedirectStandardError = true,76 RedirectStandardOutput = true,77 Arguments = $"-c '{commandCreate};{commandFill}'"78 };79 Process.Start(startInfo);80 }81 82 81 private void DistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) { 82 if (distinctSentencesFileName == null) Init(sender); 83 83 distinctSentencesFileTrace.WriteLine(ToCsvLine( 84 84 ((uint)phraseAddedEventArgs.NewHash).ToString("D10"), 85 85 phraseAddedEventArgs.NewPhrase.Count().ToString("D3"), 86 phraseAddedEventArgs.NewPhrase.ToString(),86 //phraseAddedEventArgs.NewPhrase.ToString(), 87 87 ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase))); 88 88 } 89 89 90 90 91 private void SentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) { 92 if (allSentencesFileTrace == null) Init(sender); 91 93 allSentencesFileTrace.WriteLine(ToCsvLine( 92 94 ((uint)phraseAddedEventArgs.NewHash).ToString("D10"), 93 95 phraseAddedEventArgs.NewPhrase.Count().ToString("D3"), 94 phraseAddedEventArgs.NewPhrase.ToString(),96 //phraseAddedEventArgs.NewPhrase.ToString(), 95 97 ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase))); 96 98 }
Note: See TracChangeset
for help on using the changeset viewer.