Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Operators/CombinedOperator.cs @ 3932

Last change on this file since 3932 was 89, checked in by gkronber, 17 years ago

Moved abstract operator DelegatingOperator from StructureIdentification to Operators. And removed code duplication.
See ticket #55.

File size: 4.3 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 System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Operators {
30  public class CombinedOperator : DelegatingOperator {
31    private string myDescription;
32    public override string Description {
33      get { return myDescription; }
34    }
35    private IOperatorGraph myOperatorGraph;
36    public IOperatorGraph OperatorGraph {
37      get { return myOperatorGraph; }
38    }
39
40    public CombinedOperator()
41      : base() {
42      myDescription =
43        @"A combined operator contains a whole operator graph. It is useful for modularization to assemble complex operators out of simpler ones.
44
45A combined operator automatically inject its sub-operators into the scope it is applied on. Thereby the names of the sub-operators are used as variable names. Those operators can be extracted again in the contained operator graph by using an OperatorExtractor. So it is possible to parameterize a combined operator with custom operators.";
46      myOperatorGraph = new OperatorGraph();
47    }
48
49    public void SetDescription(string description) {
50      if (description == null)
51        throw new NullReferenceException("description must not be null");
52
53      if (description != myDescription) {
54        myDescription = description;
55        OnDescriptionChanged();
56      }
57    }
58
59    public override object Clone(IDictionary<Guid, object> clonedObjects) {
60      CombinedOperator clone = (CombinedOperator)base.Clone(clonedObjects);
61      clone.myDescription = Description;
62      clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
63      return clone;
64    }
65
66    public override IOperation Apply(IScope scope) {
67      if (OperatorGraph.InitialOperator != null) {
68        for (int i = 0; i < SubOperators.Count; i++) {
69          if (scope.GetVariable(SubOperators[i].Name) != null)
70            scope.RemoveVariable(SubOperators[i].Name);
71          scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
72        }
73        return new AtomicOperation(OperatorGraph.InitialOperator, scope);
74      } else {
75        return null;
76      }
77    }
78
79    public override IView CreateView() {
80      return new CombinedOperatorView(this);
81    }
82
83    public event EventHandler DescriptionChanged;
84    protected virtual void OnDescriptionChanged() {
85      if (DescriptionChanged != null)
86        DescriptionChanged(this, new EventArgs());
87    }
88
89    #region Persistence Methods
90    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
91      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
92      XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
93      descriptionNode.InnerText = myDescription;
94      node.AppendChild(descriptionNode);
95      node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
96      return node;
97    }
98    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
99      base.Populate(node, restoredObjects);
100      XmlNode descriptionNode = node.SelectSingleNode("Description");
101      if (descriptionNode != null) myDescription = descriptionNode.InnerText;
102      myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
103    }
104    #endregion
105  }
106}
Note: See TracBrowser for help on using the repository browser.