Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.CEDMA.Core/OperatorLink.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.4 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.CEDMA.DB.Interfaces;
29using HeuristicLab.Operators;
30
31namespace HeuristicLab.CEDMA.Core {
32  public class OperatorLink : OperatorBase {
33    private long id;
34    public long Id {
35      get { return id; }
36    }
37
38    public IDatabase Database { get; set; }
39
40    private IOperator myOperator;
41    public IOperator Operator {
42      get { return myOperator; }
43      set { myOperator = value; }
44    }
45
46    public OperatorLink() { } // for cloning and persistence
47
48    public OperatorLink(long id, IOperator op)
49      : base() {
50      this.id = id;
51      this.myOperator = op;
52      Name = myOperator.Name;
53    }
54
55    public OperatorLink(OperatorLink original) : this(original, new Dictionary<Guid, object>()) { }
56    protected OperatorLink(OperatorLink original, IDictionary<Guid, object> clonedObjects)
57      : base(original, clonedObjects) {
58      this.id = original.id;
59      this.myOperator = (IOperator)Auxiliary.Clone(Operator, clonedObjects);
60    }
61
62    public override void Abort() {
63      throw new NotSupportedException();
64    }
65
66    public override IOperation Apply(IScope scope) {
67      throw new NotSupportedException();
68    }
69
70    public override object Clone(IDictionary<Guid, object> clonedObjects) {
71      return new OperatorLink(this, clonedObjects);
72    }
73
74    public override IView CreateView() {
75      if(Operator == null) {
76        if(Database == null) return null;
77        OperatorEntry targetEntry = Database.GetOperator(Id);
78        IOperator target = (IOperator)PersistenceManager.RestoreFromGZip(targetEntry.RawData);
79        OperatorLinkPatcher.LinkDatabase(target, Database);
80        Operator = target;
81      }
82      return myOperator.CreateView();
83    }
84
85    public override IOperation Execute(IScope scope) {
86      throw new NotSupportedException();
87    }
88
89    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
90      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
91      XmlAttribute idAttr = document.CreateAttribute("OperatorId");
92      idAttr.Value = id.ToString();
93      node.Attributes.Append(idAttr);
94      return node;
95    }
96
97    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
98      id = long.Parse(node.Attributes["OperatorId"].Value);
99      base.Populate(node, restoredObjects);
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.