Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15949 was 15949, checked in by bburlacu, 6 years ago

#2886: Fix serialization (saving the algorithm).

File size: 5.0 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.IO.Compression;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
10  [Item("SentenceLogger", "")]
11  [StorableClass]
12  public class SentenceLogger : Item, IGrammarEnumerationAnalyzer {
13    private readonly string workingDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
14    private readonly string columnDelimiter = ";";
15    private readonly string header = "hash;length;infix";
16
17    private string distinctSentencesFileName;
18    private string allSentencesFileName;
19    private string shortestDistinctSentencesFileName;
20
21    private TextWriterTraceListener distinctSentencesFileTrace;
22    private TextWriterTraceListener allSentencesFileTrace;
23
24    public SentenceLogger() { }
25
26    protected SentenceLogger(SentenceLogger original, Cloner cloner) : base(original, cloner) { }
27
28    public override IDeepCloneable Clone(Cloner cloner) {
29      return new SentenceLogger(this, cloner);
30    }
31
32    public void Register(GrammarEnumerationAlgorithm algorithm) {
33      algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
34      algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
35
36      algorithm.SentenceGenerated += SentenceGenerated;
37      algorithm.DistinctSentenceGenerated += DistinctSentenceGenerated;
38    }
39
40    public void Deregister(GrammarEnumerationAlgorithm algorithm) {
41      algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
42      algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
43
44      algorithm.SentenceGenerated -= SentenceGenerated;
45      algorithm.DistinctSentenceGenerated -= DistinctSentenceGenerated;
46    }
47
48    private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) {
49      distinctSentencesFileTrace.Close();
50      allSentencesFileTrace.Close();
51
52      // Remove duplicates afterwards using bash commands from the git bash
53      // string bashExecutable = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Programs\Git\git-bash.exe";
54      //
55      // string commandCreate = $"echo \"{header}\" > {shortestDistinctSentencesFileName.Replace("\\", "/")}";
56      // string commandFill = $"sort -s {distinctSentencesFileName.Replace("\\", "/")} | uniq -w 10 >> {shortestDistinctSentencesFileName.Replace("\\", "/")}";
57      //
58      // ProcessStartInfo startInfo = new ProcessStartInfo {
59      //   WindowStyle = ProcessWindowStyle.Hidden,
60      //   UseShellExecute = false,
61      //   CreateNoWindow = true,
62      //   FileName = bashExecutable,
63      //   RedirectStandardError = true,
64      //   RedirectStandardOutput = true,
65      //   Arguments = $"-c '{commandCreate};{commandFill}'"
66      // };
67      // Process.Start(startInfo);
68    }
69
70    private void Init(object sender) {
71      string datePostfix = $"_{DateTime.Now:yyyy-MM-dd_HH-mm}_TreeSize-{((GrammarEnumerationAlgorithm)sender).MaxComplexity}.csv.gz";
72      distinctSentencesFileName = workingDir + @"\distinctSentences" + datePostfix;
73      allSentencesFileName = workingDir + @"\allSentences" + datePostfix;
74      shortestDistinctSentencesFileName = workingDir + @"\shortestDistinctSentences" + datePostfix;
75
76      distinctSentencesFileTrace = new TextWriterTraceListener(new GZipStream(new FileStream(distinctSentencesFileName, FileMode.Create), CompressionMode.Compress));
77      allSentencesFileTrace = new TextWriterTraceListener(new GZipStream(new FileStream(allSentencesFileName, FileMode.Create), CompressionMode.Compress));
78      ((StreamWriter)distinctSentencesFileTrace.Writer).AutoFlush = true;
79      ((StreamWriter)allSentencesFileTrace.Writer).AutoFlush = true;
80
81      allSentencesFileTrace.WriteLine(header);
82    }
83
84    private void DistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
85      if (distinctSentencesFileName == null) Init(sender);
86      distinctSentencesFileTrace.WriteLine(ToCsvLine(
87        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
88        phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
89        //phraseAddedEventArgs.NewPhrase.ToString(),
90        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
91    }
92
93
94    private void SentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
95      if (allSentencesFileTrace == null) Init(sender);
96      allSentencesFileTrace.WriteLine(ToCsvLine(
97        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
98        phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
99        //phraseAddedEventArgs.NewPhrase.ToString(),
100        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
101    }
102
103    private string ToCsvLine(params string[] cols) {
104      return string.Join(columnDelimiter, cols);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.