-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathOperatorTrace.cs
152 lines (128 loc) · 4.52 KB
/
OperatorTrace.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HEAL.Attic;
namespace HeuristicLab.DebugEngine {
[StorableType("F694C6E8-39EC-4705-AB77-D00147A4D542")]
public class OperatorTrace : ObservableList<IOperator>, IContent, IDeepCloneable {
#region fields
[Storable]
protected Dictionary<IAtomicOperation, IAtomicOperation> parents;
[Storable]
protected bool isEnabled;
#endregion
#region events
public event EventHandler IsEnabledChanged;
protected virtual void OnIsEnabledChanged() {
EventHandler handler = IsEnabledChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
#endregion
#region Constructors & Cloning
public OperatorTrace() {
parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
}
public OperatorTrace(int capacity)
: base(capacity) {
parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
}
public OperatorTrace(IEnumerable<IOperator> collection)
: base(collection) {
parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
}
[StorableConstructor]
protected OperatorTrace(StorableConstructorFlag _) : base(_) { }
protected OperatorTrace(OperatorTrace original, Cloner cloner) {
cloner.RegisterClonedObject(original, this);
AddRange(original.Select(op => cloner.Clone(op)));
parents = original.parents.ToDictionary(kvp => cloner.Clone(kvp.Key), kvp => cloner.Clone(kvp.Value));
}
public object Clone() {
return Clone(new Cloner());
}
public virtual IDeepCloneable Clone(Cloner cloner) {
return new OperatorTrace(this, cloner);
}
#endregion
#region Additional List Modifiers
public virtual void ReplaceAll(IEnumerable<IOperator> operators) {
var oldList = list;
list = new List<IOperator>(operators);
if (oldList.Count != list.Count)
OnPropertyChanged("Count");
OnPropertyChanged("Item[]");
OnCollectionReset(
list.Select((op, i) => new IndexedItem<IOperator>(i, op)),
oldList.Select((op, i) => new IndexedItem<IOperator>(i, op)));
}
#endregion
#region Parent Tracing
public virtual void RegisterParenthood(IAtomicOperation parent, IOperation children) {
if (!isEnabled)
return;
OperationCollection operations = children as OperationCollection;
if (operations != null)
foreach (var op in operations)
RegisterParenthood(parent, op);
IAtomicOperation atomicOperation = children as IAtomicOperation;
if (atomicOperation != null && atomicOperation.Operator != null && !parents.ContainsKey(atomicOperation))
parents[atomicOperation] = parent;
}
public virtual void Reset() {
Clear();
parents.Clear();
}
public virtual void Regenerate(IAtomicOperation operation) {
if (!isEnabled) {
Reset();
return;
}
if (operation == null)
return;
Stack<IOperator> trace = new Stack<IOperator>();
while (operation != null) {
trace.Push(operation.Operator);
IAtomicOperation parent = null;
parents.TryGetValue(operation, out parent);
operation = parent;
}
ReplaceAll(trace);
}
public bool IsEnabled {
get { return isEnabled; }
set {
if (isEnabled == value)
return;
isEnabled = value;
if (!isEnabled)
Reset();
OnIsEnabledChanged();
}
}
#endregion
}
}