Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15911


Ignore:
Timestamp:
04/18/18 10:22:49 (7 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SentenceLogger.cs

    r15910 r15911  
    22using System.Diagnostics;
    33using System.IO;
     4using System.IO.Compression;
    45using HeuristicLab.Common;
    56using HeuristicLab.Core;
    67
    78namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    8   class SentenceLogger : Item, IGrammarEnumerationAnalyzer {
     9  public class SentenceLogger : Item, IGrammarEnumerationAnalyzer {
    910    private readonly string workingDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    1011    private readonly string columnDelimiter = ";";
    11     private readonly string header = "hash;length;postfix;infix";
     12    private readonly string header = "hash;length;infix";
    1213
    1314    private string distinctSentencesFileName;
     
    2728
    2829    public void Register(GrammarEnumerationAlgorithm algorithm) {
    29       algorithm.Started += GrammarEnumerationAlgorithmOnStarted;
    3030      algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
    3131      algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
     
    3636
    3737    public void Deregister(GrammarEnumerationAlgorithm algorithm) {
    38       algorithm.Started -= GrammarEnumerationAlgorithmOnStarted;
    3938      algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
    4039      algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
     
    4443    }
    4544
    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";
    4869      distinctSentencesFileName = workingDir + @"\distinctSentences" + datePostfix;
    4970      allSentencesFileName = workingDir + @"\allSentences" + datePostfix;
    5071      shortestDistinctSentencesFileName = workingDir + @"\shortestDistinctSentences" + datePostfix;
    5172
    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));
    5475      ((StreamWriter)distinctSentencesFileTrace.Writer).AutoFlush = true;
    5576      ((StreamWriter)allSentencesFileTrace.Writer).AutoFlush = true;
     
    5879    }
    5980
    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 bash
    65       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 
    8281    private void DistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
     82      if (distinctSentencesFileName == null) Init(sender);
    8383      distinctSentencesFileTrace.WriteLine(ToCsvLine(
    8484        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
    8585        phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
    86         phraseAddedEventArgs.NewPhrase.ToString(),
     86        //phraseAddedEventArgs.NewPhrase.ToString(),
    8787        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
    8888    }
    8989
     90
    9091    private void SentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
     92      if (allSentencesFileTrace == null) Init(sender);
    9193      allSentencesFileTrace.WriteLine(ToCsvLine(
    9294        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
    9395        phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
    94         phraseAddedEventArgs.NewPhrase.ToString(),
     96        //phraseAddedEventArgs.NewPhrase.ToString(),
    9597        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
    9698    }
Note: See TracChangeset for help on using the changeset viewer.