Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Operators/DelegatingOperator.cs @ 887

Last change on this file since 887 was 887, checked in by gkronber, 15 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.2 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 HeuristicLab.Data;
27
28namespace HeuristicLab.Operators {
29  /// <summary>
30  /// Base class for operators that call other operators.
31  /// </summary>
32  public abstract class DelegatingOperator : OperatorBase {
33
34    /// <summary>
35    ///  Default constructor for DelegatingOperator
36    /// </summary>
37    public DelegatingOperator() { }
38
39    public DelegatingOperator(DelegatingOperator original, IDictionary<Guid, object> clonedObjects)
40      : base(original, clonedObjects) { }
41
42    /// <summary>
43    /// Executes the specified operator in the given <paramref name="scope"/>.
44    /// </summary>
45    /// <remarks>Calls <see cref="OperatorBase.OnExecuted"/>.<br/>
46    /// Will be executed two times: First to execute the operation where the local variables
47    /// are added to the global scope and a second time to remove the local variables again.</remarks>
48    /// <param name="scope">The scope where to apply the operation.</param>
49    /// <returns><c>null</c>.</returns>
50    public override IOperation Execute(IScope scope) {
51      myCanceled = false;
52
53      if (scope.GetVariable(Guid.ToString()) == null) { // contained operator not yet executed
54        // add marker
55        scope.AddVariable(new Variable(Guid.ToString(), new NullData()));
56
57        // add aliases and local variables
58        foreach (IVariableInfo variableInfo in VariableInfos) {
59          scope.AddAlias(variableInfo.FormalName, variableInfo.ActualName);
60          if (variableInfo.Local)
61            scope.AddVariable(GetVariable(variableInfo.ActualName));
62        }
63
64        CompositeOperation next = new CompositeOperation();
65        next.AddOperation(Apply(scope));
66        // execute combined operator again after contained operators have been executed
67        next.AddOperation(new AtomicOperation(this, scope));
68
69        OnExecuted();
70        return next;
71      } else {  // contained operator already executed
72        // remove marker
73        scope.RemoveVariable(Guid.ToString());
74
75        // remove aliases and local variables
76        foreach (IVariableInfo variableInfo in VariableInfos) {
77          scope.RemoveAlias(variableInfo.FormalName);
78          if (variableInfo.Local)
79            scope.RemoveVariable(variableInfo.ActualName);
80        }
81
82        OnExecuted();
83        return null;
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.