Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SearchGraphVisualizer.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: 3.5 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
10  [Item("SearchGraphVisualizer", "")]
11  [StorableClass]
12  internal class SearchGraphVisualizer : Item, IGrammarEnumerationAnalyzer {
13    private readonly string dotFileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\searchgraph.dot";
14
15    private TextWriterTraceListener dotFileTrace;
16
17    public SearchGraphVisualizer() { }
18
19    protected SearchGraphVisualizer(SearchGraphVisualizer original, Cloner cloner) : base(original, cloner) { }
20
21    [StorableConstructor]
22    protected SearchGraphVisualizer(bool deserializing) : base(deserializing) { }
23
24    public override IDeepCloneable Clone(Cloner cloner) {
25      return new SearchGraphVisualizer(this, cloner);
26    }
27
28    public void Register(GrammarEnumerationAlgorithm algorithm) {
29      algorithm.Started += GrammarEnumerationAlgorithmOnStarted;
30      algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
31      algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
32
33      algorithm.PhraseFetched += PhraseFetched;
34      algorithm.PhraseDerived += PhraseDerived;
35      algorithm.SentenceGenerated += SentenceGenerated;
36    }
37
38    public void Deregister(GrammarEnumerationAlgorithm algorithm) {
39      algorithm.Started -= GrammarEnumerationAlgorithmOnStarted;
40      algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
41      algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
42
43      algorithm.PhraseFetched -= PhraseFetched;
44      algorithm.PhraseDerived -= PhraseDerived;
45      algorithm.SentenceGenerated -= SentenceGenerated;
46    }
47
48    private void GrammarEnumerationAlgorithmOnStarted(object sender, EventArgs eventArgs) {
49      dotFileTrace = new TextWriterTraceListener(new FileStream(dotFileName, FileMode.Create));
50      ((StreamWriter)dotFileTrace.Writer).AutoFlush = true;
51      dotFileTrace.WriteLine("digraph searchgraph { pad=0.02; nodesep=0.3; ranksep=0.02;ratio=0.5625;");
52    }
53
54    private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) {
55      try {
56        var alg = (GrammarEnumerationAlgorithm)sender;
57        var phrase0 = new SymbolString(new[] { alg.Grammar.StartSymbol });
58        var phrase0Hash = alg.Grammar.Hasher.CalcHashCode(phrase0);
59
60        dotFileTrace.WriteLine($"{phrase0Hash} [label=\"{phrase0}\", shape=doublecircle]; }}");
61        dotFileTrace.Flush();
62      } catch (Exception) {
63        dotFileTrace.Close();
64        throw;
65      }
66    }
67
68    private void PhraseFetched(object sender, PhraseEventArgs phraseEventArgs) {
69      dotFileTrace.WriteLine($"{phraseEventArgs.Hash} [label=\"{phraseEventArgs.Phrase}\"];");
70    }
71
72    private void PhraseDerived(object sender, PhraseAddedEventArgs args) {
73      dotFileTrace.WriteLine($"{args.ParentHash} -> {args.NewHash} [label=\"{args.ExpandedSymbol.StringRepresentation} + → {args.ExpandedProduction}\"];");
74    }
75
76    private void SentenceGenerated(object sender, PhraseAddedEventArgs args) {
77      var alg = (GrammarEnumerationAlgorithm)sender;
78      dotFileTrace.WriteLine($"{args.NewHash} [label=\"{alg.Grammar.ToInfixString(args.NewPhrase)}\", style=\"filled\", fillcolor=\"#f79423\", color=\"#f79423\"];");
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.