1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Collections;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.DebugEngine {
|
---|
9 |
|
---|
10 | [StorableClass]
|
---|
11 | public class OperatorTrace : ObservableList<IOperator>, IContent, IDeepCloneable {
|
---|
12 |
|
---|
13 | #region fields
|
---|
14 |
|
---|
15 | [Storable]
|
---|
16 | protected Dictionary<IAtomicOperation, IAtomicOperation> parents;
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | #region Constructors & Cloning
|
---|
20 |
|
---|
21 | public OperatorTrace() {
|
---|
22 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
23 | }
|
---|
24 |
|
---|
25 | public OperatorTrace(int capacity) : base(capacity) {
|
---|
26 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
27 | }
|
---|
28 |
|
---|
29 | public OperatorTrace(IEnumerable<IOperator> collection): base(collection) {
|
---|
30 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
31 | }
|
---|
32 |
|
---|
33 | [StorableConstructor]
|
---|
34 | protected OperatorTrace(bool deserializing) : base(deserializing) { }
|
---|
35 |
|
---|
36 | protected OperatorTrace(OperatorTrace original, Cloner cloner) {
|
---|
37 | cloner.RegisterClonedObject(original, this);
|
---|
38 | AddRange(original.Select(op => cloner.Clone(op)));
|
---|
39 | parents = original.parents.ToDictionary(kvp => cloner.Clone(kvp.Key), kvp => cloner.Clone(kvp.Value));
|
---|
40 | }
|
---|
41 |
|
---|
42 | public object Clone() {
|
---|
43 | return Clone(new Cloner());
|
---|
44 | }
|
---|
45 |
|
---|
46 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new OperatorTrace(this, cloner);
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Additional List Modifiers
|
---|
52 |
|
---|
53 | public virtual void ReplaceAll(IEnumerable<IOperator> operators) {
|
---|
54 | var oldList = list;
|
---|
55 | list = new List<IOperator>(operators);
|
---|
56 | if (oldList.Count != list.Count)
|
---|
57 | OnPropertyChanged("Count");
|
---|
58 | OnPropertyChanged("Item[]");
|
---|
59 | OnCollectionReset(
|
---|
60 | list.Select((op, i) => new IndexedItem<IOperator>(i, op)),
|
---|
61 | oldList.Select((op, i) => new IndexedItem<IOperator>(i, op)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | #region Parent Tracing
|
---|
67 |
|
---|
68 | public virtual void RegisterParenthood(IAtomicOperation parent, IOperation children) {
|
---|
69 | OperationCollection operations = children as OperationCollection;
|
---|
70 | if (operations != null)
|
---|
71 | foreach (var op in operations)
|
---|
72 | RegisterParenthood(parent, op);
|
---|
73 | IAtomicOperation atomicOperation = children as IAtomicOperation;
|
---|
74 | if (atomicOperation != null && atomicOperation.Operator != null && !parents.ContainsKey(atomicOperation))
|
---|
75 | parents[atomicOperation] = parent;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public virtual void Reset() {
|
---|
79 | Clear();
|
---|
80 | parents.Clear();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public virtual void Generate(IAtomicOperation operation) {
|
---|
84 | if (operation == null)
|
---|
85 | return;
|
---|
86 | Stack<IOperator> trace = new Stack<IOperator>();
|
---|
87 | while (operation != null) {
|
---|
88 | trace.Push(operation.Operator);
|
---|
89 | IAtomicOperation parent = null;
|
---|
90 | parents.TryGetValue(operation, out parent);
|
---|
91 | operation = parent;
|
---|
92 | }
|
---|
93 | ReplaceAll(trace);
|
---|
94 | }
|
---|
95 |
|
---|
96 | #endregion
|
---|
97 | }
|
---|
98 | }
|
---|