Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DebugEngine/OperatorTrace.cs @ 5166

Last change on this file since 5166 was 5002, checked in by epitzer, 14 years ago

unseal views, rename OperatorTrace.Generate to Regenerate, and remove questionable virtual from property with private setter (#47)

File size: 3.9 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    #endregion
39
40    #region Constructors & Cloning
41
42    public OperatorTrace() {
43      parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
44    }
45
46    public OperatorTrace(int capacity)
47      : base(capacity) {
48      parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
49    }
50
51    public OperatorTrace(IEnumerable<IOperator> collection)
52      : base(collection) {
53      parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
54    }
55
56    [StorableConstructor]
57    protected OperatorTrace(bool deserializing) : base(deserializing) { }
58
59    protected OperatorTrace(OperatorTrace original, Cloner cloner) {
60      cloner.RegisterClonedObject(original, this);
61      AddRange(original.Select(op => cloner.Clone(op)));
62      parents = original.parents.ToDictionary(kvp => cloner.Clone(kvp.Key), kvp => cloner.Clone(kvp.Value));
63    }
64
65    public object Clone() {
66      return Clone(new Cloner());
67    }
68
69    public virtual IDeepCloneable Clone(Cloner cloner) {
70      return new OperatorTrace(this, cloner);
71    }
72    #endregion
73
74    #region Additional List Modifiers
75
76    public virtual void ReplaceAll(IEnumerable<IOperator> operators) {
77      var oldList = list;
78      list = new List<IOperator>(operators);
79      if (oldList.Count != list.Count)
80        OnPropertyChanged("Count");
81      OnPropertyChanged("Item[]");
82      OnCollectionReset(
83        list.Select((op, i) => new IndexedItem<IOperator>(i, op)),
84        oldList.Select((op, i) => new IndexedItem<IOperator>(i, op)));
85    }
86
87    #endregion
88
89    #region Parent Tracing
90
91    public virtual void RegisterParenthood(IAtomicOperation parent, IOperation children) {
92      OperationCollection operations = children as OperationCollection;
93      if (operations != null)
94        foreach (var op in operations)
95          RegisterParenthood(parent, op);
96      IAtomicOperation atomicOperation = children as IAtomicOperation;
97      if (atomicOperation != null && atomicOperation.Operator != null && !parents.ContainsKey(atomicOperation))
98        parents[atomicOperation] = parent;
99    }
100
101    public virtual void Reset() {
102      Clear();
103      parents.Clear();
104    }
105
106    public virtual void Regenerate(IAtomicOperation operation) {
107      if (operation == null)
108        return;
109      Stack<IOperator> trace = new Stack<IOperator>();
110      while (operation != null) {
111        trace.Push(operation.Operator);
112        IAtomicOperation parent = null;
113        parents.TryGetValue(operation, out parent);
114        operation = parent;
115      }
116      ReplaceAll(trace);
117    }
118
119    #endregion
120  }
121}
Note: See TracBrowser for help on using the repository browser.