[4998] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[4871] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Collections;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.DebugEngine {
|
---|
| 30 |
|
---|
[4993] | 31 | [StorableClass]
|
---|
| 32 | public class OperatorTrace : ObservableList<IOperator>, IContent, IDeepCloneable {
|
---|
[4871] | 33 |
|
---|
[4996] | 34 | #region fields
|
---|
| 35 |
|
---|
| 36 | [Storable]
|
---|
| 37 | protected Dictionary<IAtomicOperation, IAtomicOperation> parents;
|
---|
| 38 | #endregion
|
---|
| 39 |
|
---|
| 40 | #region Constructors & Cloning
|
---|
| 41 |
|
---|
| 42 | public OperatorTrace() {
|
---|
[4998] | 43 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
[4993] | 44 | }
|
---|
[4996] | 45 |
|
---|
[4998] | 46 | public OperatorTrace(int capacity)
|
---|
| 47 | : base(capacity) {
|
---|
| 48 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
[4993] | 49 | }
|
---|
[4996] | 50 |
|
---|
[4998] | 51 | public OperatorTrace(IEnumerable<IOperator> collection)
|
---|
| 52 | : base(collection) {
|
---|
| 53 | parents = new Dictionary<IAtomicOperation, IAtomicOperation>();
|
---|
[4993] | 54 | }
|
---|
[4871] | 55 |
|
---|
| 56 | [StorableConstructor]
|
---|
[4993] | 57 | protected OperatorTrace(bool deserializing) : base(deserializing) { }
|
---|
[4996] | 58 |
|
---|
[4993] | 59 | protected OperatorTrace(OperatorTrace original, Cloner cloner) {
|
---|
[4871] | 60 | cloner.RegisterClonedObject(original, this);
|
---|
| 61 | AddRange(original.Select(op => cloner.Clone(op)));
|
---|
[4996] | 62 | parents = original.parents.ToDictionary(kvp => cloner.Clone(kvp.Key), kvp => cloner.Clone(kvp.Value));
|
---|
[4871] | 63 | }
|
---|
[4996] | 64 |
|
---|
[4871] | 65 | public object Clone() {
|
---|
| 66 | return Clone(new Cloner());
|
---|
| 67 | }
|
---|
[4996] | 68 |
|
---|
[4871] | 69 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
[4993] | 70 | return new OperatorTrace(this, cloner);
|
---|
[4871] | 71 | }
|
---|
[4996] | 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region Additional List Modifiers
|
---|
| 75 |
|
---|
[4993] | 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 |
|
---|
[4996] | 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 |
|
---|
[5002] | 106 | public virtual void Regenerate(IAtomicOperation operation) {
|
---|
[4996] | 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
|
---|
[4871] | 120 | }
|
---|
| 121 | }
|
---|