1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Diagnostics;
|
---|
24 | using System.IO;
|
---|
25 | using System.IO.Compression;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
31 | [Item("SentenceLogger", "")]
|
---|
32 | [StorableClass]
|
---|
33 | public class SentenceLogger : Item, IGrammarEnumerationAnalyzer {
|
---|
34 | private readonly string workingDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
|
---|
35 | private readonly string columnDelimiter = ";";
|
---|
36 | private readonly string header = "hash;length;infix";
|
---|
37 |
|
---|
38 | private string distinctSentencesFileName;
|
---|
39 | private string allSentencesFileName;
|
---|
40 | private string shortestDistinctSentencesFileName;
|
---|
41 |
|
---|
42 | private TextWriterTraceListener distinctSentencesFileTrace;
|
---|
43 | private TextWriterTraceListener allSentencesFileTrace;
|
---|
44 |
|
---|
45 | public SentenceLogger() { }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected SentenceLogger(bool deserializing) : base(deserializing) { }
|
---|
49 |
|
---|
50 | protected SentenceLogger(SentenceLogger original, Cloner cloner) : base(original, cloner) { }
|
---|
51 |
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new SentenceLogger(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void Register(GrammarEnumerationAlgorithm algorithm) {
|
---|
57 | algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
|
---|
58 | algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
|
---|
59 |
|
---|
60 | algorithm.SentenceGenerated += SentenceGenerated;
|
---|
61 | algorithm.DistinctSentenceGenerated += DistinctSentenceGenerated;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void Deregister(GrammarEnumerationAlgorithm algorithm) {
|
---|
65 | algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
|
---|
66 | algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
|
---|
67 |
|
---|
68 | algorithm.SentenceGenerated -= SentenceGenerated;
|
---|
69 | algorithm.DistinctSentenceGenerated -= DistinctSentenceGenerated;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) {
|
---|
73 | distinctSentencesFileTrace.Close();
|
---|
74 | allSentencesFileTrace.Close();
|
---|
75 |
|
---|
76 | // Remove duplicates afterwards using bash commands from the git bash
|
---|
77 | // string bashExecutable = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Programs\Git\git-bash.exe";
|
---|
78 | //
|
---|
79 | // string commandCreate = $"echo \"{header}\" > {shortestDistinctSentencesFileName.Replace("\\", "/")}";
|
---|
80 | // string commandFill = $"sort -s {distinctSentencesFileName.Replace("\\", "/")} | uniq -w 10 >> {shortestDistinctSentencesFileName.Replace("\\", "/")}";
|
---|
81 | //
|
---|
82 | // ProcessStartInfo startInfo = new ProcessStartInfo {
|
---|
83 | // WindowStyle = ProcessWindowStyle.Hidden,
|
---|
84 | // UseShellExecute = false,
|
---|
85 | // CreateNoWindow = true,
|
---|
86 | // FileName = bashExecutable,
|
---|
87 | // RedirectStandardError = true,
|
---|
88 | // RedirectStandardOutput = true,
|
---|
89 | // Arguments = $"-c '{commandCreate};{commandFill}'"
|
---|
90 | // };
|
---|
91 | // Process.Start(startInfo);
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void Init(object sender) {
|
---|
95 | string datePostfix = $"_{DateTime.Now:yyyy-MM-dd_HH-mm}_TreeSize-{((GrammarEnumerationAlgorithm)sender).MaxComplexity}.csv.gz";
|
---|
96 | distinctSentencesFileName = workingDir + @"\distinctSentences" + datePostfix;
|
---|
97 | allSentencesFileName = workingDir + @"\allSentences" + datePostfix;
|
---|
98 | shortestDistinctSentencesFileName = workingDir + @"\shortestDistinctSentences" + datePostfix;
|
---|
99 |
|
---|
100 | distinctSentencesFileTrace = new TextWriterTraceListener(new GZipStream(new FileStream(distinctSentencesFileName, FileMode.Create), CompressionMode.Compress));
|
---|
101 | allSentencesFileTrace = new TextWriterTraceListener(new GZipStream(new FileStream(allSentencesFileName, FileMode.Create), CompressionMode.Compress));
|
---|
102 | ((StreamWriter)distinctSentencesFileTrace.Writer).AutoFlush = true;
|
---|
103 | ((StreamWriter)allSentencesFileTrace.Writer).AutoFlush = true;
|
---|
104 |
|
---|
105 | allSentencesFileTrace.WriteLine(header);
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void DistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
|
---|
109 | if (distinctSentencesFileName == null) Init(sender);
|
---|
110 | distinctSentencesFileTrace.WriteLine(ToCsvLine(
|
---|
111 | ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
|
---|
112 | phraseAddedEventArgs.NewPhrase.Count.ToString("D3"),
|
---|
113 | //phraseAddedEventArgs.NewPhrase.ToString(),
|
---|
114 | ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | private void SentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
|
---|
119 | if (allSentencesFileTrace == null) Init(sender);
|
---|
120 | allSentencesFileTrace.WriteLine(ToCsvLine(
|
---|
121 | ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
|
---|
122 | phraseAddedEventArgs.NewPhrase.Count.ToString("D3"),
|
---|
123 | //phraseAddedEventArgs.NewPhrase.ToString(),
|
---|
124 | ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
|
---|
125 | }
|
---|
126 |
|
---|
127 | private string ToCsvLine(params string[] cols) {
|
---|
128 | return string.Join(columnDelimiter, cols);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|