Free cookie consent management tool by TermsFeed Policy Generator

source: branches/NET40/sources/HeuristicLab.DebugEngine/3.3/OperatorTrace.cs @ 5138

Last change on this file since 5138 was 5121, checked in by epitzer, 14 years ago
  • Show empty operator trace when not enabled (#47)
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 (!isEnabled) {
113        Reset();
114        return;
115      }
116      if (operation == null)
117        return;
118      Stack<IOperator> trace = new Stack<IOperator>();
119      while (operation != null) {
120        trace.Push(operation.Operator);
121        IAtomicOperation parent = null;
122        parents.TryGetValue(operation, out parent);
123        operation = parent;
124      }
125      ReplaceAll(trace);
126    }
127
128    public bool IsEnabled {
129      get { return isEnabled; }
130      set {
131        if (isEnabled == value)
132          return;
133        isEnabled = value;
134        if (!isEnabled)
135          Reset();
136      }
137    }
138
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.