[13368] | 1 | #region License Information
|
---|
[4998] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4998] | 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 |
|
---|
[5174] | 22 | using System;
|
---|
[4998] | 23 | using System.Collections.Generic;
|
---|
[4871] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.DebugEngine {
|
---|
| 31 |
|
---|
[14711] | 32 | [StorableType("9750C31C-939F-4F4D-8C02-87E790D78695")]
|
---|
[4993] | 33 | public class OperatorTrace : ObservableList<IOperator>, IContent, IDeepCloneable {
|
---|
[4871] | 34 |
|
---|
[4996] | 35 | #region fields
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | protected Dictionary<IAtomicOperation, IAtomicOperation> parents;
|
---|
[5117] | 39 |
|
---|
| 40 | [Storable]
|
---|
| 41 | protected bool isEnabled;
|
---|
[4996] | 42 | #endregion
|
---|
| 43 |
|
---|
[5174] | 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 |
|
---|
[4996] | 53 | #region Constructors & Cloning
|
---|
| 54 |
|
---|
| 55 | public OperatorTrace() {
|
---|
[4998] | 56 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
[4993] | 57 | }
|
---|
[4996] | 58 |
|
---|
[4998] | 59 | public OperatorTrace(int capacity)
|
---|
| 60 | : base(capacity) {
|
---|
| 61 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
[4993] | 62 | }
|
---|
[4996] | 63 |
|
---|
[4998] | 64 | public OperatorTrace(IEnumerable<IOperator> collection)
|
---|
| 65 | : base(collection) {
|
---|
| 66 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
[4993] | 67 | }
|
---|
[4871] | 68 |
|
---|
| 69 | [StorableConstructor]
|
---|
[4993] | 70 | protected OperatorTrace(bool deserializing) : base(deserializing) { }
|
---|
[4996] | 71 |
|
---|
[4993] | 72 | protected OperatorTrace(OperatorTrace original, Cloner cloner) {
|
---|
[4871] | 73 | cloner.RegisterClonedObject(original, this);
|
---|
| 74 | AddRange(original.Select(op => cloner.Clone(op)));
|
---|
[4996] | 75 | parents = original.parents.ToDictionary(kvp => cloner.Clone(kvp.Key), kvp => cloner.Clone(kvp.Value));
|
---|
[4871] | 76 | }
|
---|
[4996] | 77 |
|
---|
[4871] | 78 | public object Clone() {
|
---|
| 79 | return Clone(new Cloner());
|
---|
| 80 | }
|
---|
[4996] | 81 |
|
---|
[4871] | 82 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
[4993] | 83 | return new OperatorTrace(this, cloner);
|
---|
[4871] | 84 | }
|
---|
[4996] | 85 | #endregion
|
---|
| 86 |
|
---|
| 87 | #region Additional List Modifiers
|
---|
| 88 |
|
---|
[4993] | 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 |
|
---|
[4996] | 100 | #endregion
|
---|
| 101 |
|
---|
| 102 | #region Parent Tracing
|
---|
| 103 |
|
---|
| 104 | public virtual void RegisterParenthood(IAtomicOperation parent, IOperation children) {
|
---|
[5117] | 105 | if (!isEnabled)
|
---|
| 106 | return;
|
---|
[4996] | 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 |
|
---|
[5002] | 121 | public virtual void Regenerate(IAtomicOperation operation) {
|
---|
[5121] | 122 | if (!isEnabled) {
|
---|
| 123 | Reset();
|
---|
| 124 | return;
|
---|
| 125 | }
|
---|
[4996] | 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 |
|
---|
[5117] | 138 | public bool IsEnabled {
|
---|
| 139 | get { return isEnabled; }
|
---|
| 140 | set {
|
---|
| 141 | if (isEnabled == value)
|
---|
| 142 | return;
|
---|
| 143 | isEnabled = value;
|
---|
| 144 | if (!isEnabled)
|
---|
| 145 | Reset();
|
---|
[5174] | 146 | OnIsEnabledChanged();
|
---|
[5117] | 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[4996] | 150 | #endregion
|
---|
[4871] | 151 | }
|
---|
| 152 | }
|
---|