[15821] | 1 | using System;
|
---|
| 2 | using System.Diagnostics;
|
---|
| 3 | using System.IO;
|
---|
[15911] | 4 | using System.IO.Compression;
|
---|
[15821] | 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
[15949] | 7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15821] | 8 |
|
---|
| 9 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
[15949] | 10 | [Item("SentenceLogger", "")]
|
---|
| 11 | [StorableClass]
|
---|
[15911] | 12 | public class SentenceLogger : Item, IGrammarEnumerationAnalyzer {
|
---|
[15843] | 13 | private readonly string workingDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
|
---|
[15821] | 14 | private readonly string columnDelimiter = ";";
|
---|
[15911] | 15 | private readonly string header = "hash;length;infix";
|
---|
[15821] | 16 |
|
---|
[15843] | 17 | private string distinctSentencesFileName;
|
---|
| 18 | private string allSentencesFileName;
|
---|
| 19 | private string shortestDistinctSentencesFileName;
|
---|
| 20 |
|
---|
[15821] | 21 | private TextWriterTraceListener distinctSentencesFileTrace;
|
---|
| 22 | private TextWriterTraceListener allSentencesFileTrace;
|
---|
| 23 |
|
---|
| 24 | public SentenceLogger() { }
|
---|
| 25 |
|
---|
[15910] | 26 | protected SentenceLogger(SentenceLogger original, Cloner cloner) : base(original, cloner) { }
|
---|
[15821] | 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 |
|
---|
[15911] | 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";
|
---|
[15843] | 72 | distinctSentencesFileName = workingDir + @"\distinctSentences" + datePostfix;
|
---|
| 73 | allSentencesFileName = workingDir + @"\allSentences" + datePostfix;
|
---|
| 74 | shortestDistinctSentencesFileName = workingDir + @"\shortestDistinctSentences" + datePostfix;
|
---|
| 75 |
|
---|
[15911] | 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));
|
---|
[15821] | 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) {
|
---|
[15911] | 85 | if (distinctSentencesFileName == null) Init(sender);
|
---|
[15821] | 86 | distinctSentencesFileTrace.WriteLine(ToCsvLine(
|
---|
[15843] | 87 | ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
|
---|
| 88 | phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
|
---|
[15911] | 89 | //phraseAddedEventArgs.NewPhrase.ToString(),
|
---|
[15843] | 90 | ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
|
---|
[15821] | 91 | }
|
---|
| 92 |
|
---|
[15911] | 93 |
|
---|
[15821] | 94 | private void SentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
|
---|
[15911] | 95 | if (allSentencesFileTrace == null) Init(sender);
|
---|
[15821] | 96 | allSentencesFileTrace.WriteLine(ToCsvLine(
|
---|
[15843] | 97 | ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
|
---|
| 98 | phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
|
---|
[15911] | 99 | //phraseAddedEventArgs.NewPhrase.ToString(),
|
---|
[15843] | 100 | ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
|
---|
[15821] | 101 | }
|
---|
| 102 |
|
---|
| 103 | private string ToCsvLine(params string[] cols) {
|
---|
| 104 | return string.Join(columnDelimiter, cols);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|