Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8943 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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