Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SentenceLogger.cs @ 15935

Last change on this file since 15935 was 15911, checked in by gkronber, 6 years ago

#2886: Changed initialization of SentenceLogger because started event is not called after Run(). Logging to GZipStream.

File size: 4.8 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.IO.Compression;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
9  public class SentenceLogger : Item, IGrammarEnumerationAnalyzer {
10    private readonly string workingDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
11    private readonly string columnDelimiter = ";";
12    private readonly string header = "hash;length;infix";
13
14    private string distinctSentencesFileName;
15    private string allSentencesFileName;
16    private string shortestDistinctSentencesFileName;
17
18    private TextWriterTraceListener distinctSentencesFileTrace;
19    private TextWriterTraceListener allSentencesFileTrace;
20
21    public SentenceLogger() { }
22
23    protected SentenceLogger(SentenceLogger original, Cloner cloner) : base(original, cloner) { }
24
25    public override IDeepCloneable Clone(Cloner cloner) {
26      return new SentenceLogger(this, cloner);
27    }
28
29    public void Register(GrammarEnumerationAlgorithm algorithm) {
30      algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
31      algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
32
33      algorithm.SentenceGenerated += SentenceGenerated;
34      algorithm.DistinctSentenceGenerated += DistinctSentenceGenerated;
35    }
36
37    public void Deregister(GrammarEnumerationAlgorithm algorithm) {
38      algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
39      algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
40
41      algorithm.SentenceGenerated -= SentenceGenerated;
42      algorithm.DistinctSentenceGenerated -= DistinctSentenceGenerated;
43    }
44
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";
69      distinctSentencesFileName = workingDir + @"\distinctSentences" + datePostfix;
70      allSentencesFileName = workingDir + @"\allSentences" + datePostfix;
71      shortestDistinctSentencesFileName = workingDir + @"\shortestDistinctSentences" + datePostfix;
72
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));
75      ((StreamWriter)distinctSentencesFileTrace.Writer).AutoFlush = true;
76      ((StreamWriter)allSentencesFileTrace.Writer).AutoFlush = true;
77
78      allSentencesFileTrace.WriteLine(header);
79    }
80
81    private void DistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
82      if (distinctSentencesFileName == null) Init(sender);
83      distinctSentencesFileTrace.WriteLine(ToCsvLine(
84        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
85        phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
86        //phraseAddedEventArgs.NewPhrase.ToString(),
87        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
88    }
89
90
91    private void SentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
92      if (allSentencesFileTrace == null) Init(sender);
93      allSentencesFileTrace.WriteLine(ToCsvLine(
94        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
95        phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
96        //phraseAddedEventArgs.NewPhrase.ToString(),
97        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
98    }
99
100    private string ToCsvLine(params string[] cols) {
101      return string.Join(columnDelimiter, cols);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.