Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DebugEngine/3.3/OperatorTrace.cs @ 5117

Last change on this file since 5117 was 5117, checked in by epitzer, 13 years ago

(#47)

  • Disable detailed logging while not stepping for drastic reduction in log size
  • Optionally disable operator trace for much faster execution and persistence (disabled by default)
  • Initially enable stepping over stack operations
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DebugEngine {
30
31  [StorableClass]
32  public class OperatorTrace : ObservableList<IOperator>, IContent, IDeepCloneable {
33
34    #region fields
35
36    [Storable]
37    protected Dictionary<IAtomicOperation, IAtomicOperation> parents;
38
39    [Storable]
40    protected bool isEnabled;
41    #endregion
42
43    #region Constructors & Cloning
44
45    public OperatorTrace() {
46      parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
47    }
48
49    public OperatorTrace(int capacity)
50      : base(capacity) {
51      parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
52    }
53
54    public OperatorTrace(IEnumerable<IOperator> collection)
55      : base(collection) {
56      parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
57    }
58
59    [StorableConstructor]
60    protected OperatorTrace(bool deserializing) : base(deserializing) { }
61
62    protected OperatorTrace(OperatorTrace original, Cloner cloner) {
63      cloner.RegisterClonedObject(original, this);
64      AddRange(original.Select(op => cloner.Clone(op)));
65      parents = original.parents.ToDictionary(kvp => cloner.Clone(kvp.Key), kvp => cloner.Clone(kvp.Value));
66    }
67
68    public object Clone() {
69      return Clone(new Cloner());
70    }
71
72    public virtual IDeepCloneable Clone(Cloner cloner) {
73      return new OperatorTrace(this, cloner);
74    }
75    #endregion
76
77    #region Additional List Modifiers
78
79    public virtual void ReplaceAll(IEnumerable<IOperator> operators) {
80      var oldList = list;
81      list = new List<IOperator>(operators);
82      if (oldList.Count != list.Count)
83        OnPropertyChanged("Count");
84      OnPropertyChanged("Item[]");
85      OnCollectionReset(
86        list.Select((op, i) => new IndexedItem<IOperator>(i, op)),
87        oldList.Select((op, i) => new IndexedItem<IOperator>(i, op)));
88    }
89
90    #endregion
91
92    #region Parent Tracing
93
94    public virtual void RegisterParenthood(IAtomicOperation parent, IOperation children) {
95      if (!isEnabled)
96        return;
97      OperationCollection operations = children as OperationCollection;
98      if (operations != null)
99        foreach (var op in operations)
100          RegisterParenthood(parent, op);
101      IAtomicOperation atomicOperation = children as IAtomicOperation;
102      if (atomicOperation != null && atomicOperation.Operator != null && !parents.ContainsKey(atomicOperation))
103        parents[atomicOperation] = parent;
104    }
105
106    public virtual void Reset() {
107      Clear();
108      parents.Clear();
109    }
110
111    public virtual void Regenerate(IAtomicOperation operation) {
112      if (operation == null)
113        return;
114      Stack<IOperator> trace = new Stack<IOperator>();
115      while (operation != null) {
116        trace.Push(operation.Operator);
117        IAtomicOperation parent = null;
118        parents.TryGetValue(operation, out parent);
119        operation = parent;
120      }
121      ReplaceAll(trace);
122    }
123
124    public bool IsEnabled {
125      get { return isEnabled; }
126      set {
127        if (isEnabled == value)
128          return;
129        isEnabled = value;
130        if (!isEnabled)
131          Reset();
132      }
133    }
134
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.