Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.SequentialEngine/SequentialEngine.cs @ 887

Last change on this file since 887 was 887, checked in by gkronber, 16 years ago

Refactored cloning in all plugins except: HL.Communication, HL.Hive, HL.GP, HL.Routing, HL.Scheduling, HL.SimOpt, HL.Visualization

#285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using System.Threading;
27
28namespace HeuristicLab.SequentialEngine {
29  public class SequentialEngine : EngineBase, IEditable {
30    private IOperator currentOperator;
31
32    /// <summary>
33    /// Default constructor
34    /// </summary>
35    public SequentialEngine() { }
36
37    /// <summary>
38    /// Copy constructor to create deep clones.
39    /// </summary>
40    /// <param name="original">The instance to be cloned.</param>
41    public SequentialEngine(SequentialEngine original) : this(original, new Dictionary<Guid, object>()) { }
42    /// <summary>
43    /// Copy constructor to create deep clones reusing already cloned object references.
44    /// </summary>
45    /// <param name="original">The instance to be cloned.</param>
46    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
47    protected SequentialEngine(SequentialEngine original, IDictionary<Guid, object> clonedObjects)
48      : base(original, clonedObjects) { }
49    public override IView CreateView() {
50      return new SequentialEngineEditor(this);
51    }
52    public virtual IEditor CreateEditor() {
53      return new SequentialEngineEditor(this);
54    }
55
56    public override void Abort() {
57      base.Abort();
58      if (currentOperator != null)
59        currentOperator.Abort();
60    }
61
62    protected override void ProcessNextOperation() {
63      IOperation operation = myExecutionStack.Pop();
64      if (operation is AtomicOperation) {
65        AtomicOperation atomicOperation = (AtomicOperation)operation;
66        IOperation next = null;
67        try {
68          currentOperator = atomicOperation.Operator;
69          next = atomicOperation.Operator.Execute(atomicOperation.Scope);
70        }
71        catch (Exception ex) {
72          // push operation on stack again
73          myExecutionStack.Push(atomicOperation);
74          Abort();
75          ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
76        }
77        if (next != null)
78          myExecutionStack.Push(next);
79        OnOperationExecuted(atomicOperation);
80        if (atomicOperation.Operator.Breakpoint) Abort();
81      } else if (operation is CompositeOperation) {
82        CompositeOperation compositeOperation = (CompositeOperation)operation;
83        for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
84          myExecutionStack.Push(compositeOperation.Operations[i]);
85      }
86    }
87
88    /// <summary>
89    /// Creates a deep clone with the copy constructor reusing already cloned
90    /// object references.
91    /// </summary>
92    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
93    /// <returns>The cloned instance.</returns>
94    public override object Clone(IDictionary<Guid, object> clonedObjects) {
95      return new SequentialEngine(this, clonedObjects);
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.