Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/CombinedOperator.cs @ 47

Last change on this file since 47 was 47, checked in by swagner, 16 years ago

Fixed ticket #49

  • removed operator reset mechanism
File size: 5.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 System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Operators {
30  public class CombinedOperator : OperatorBase {
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.\r\n\r\n" +
44        "A combined operator can automatically inject its sub-operators into the scope it is applied on. 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. To activate sub-operator injection take a look at the local variables InjectSubOperators and SubOperatorNames.";
45      myOperatorGraph = new OperatorGraph();
46      AddVariableInfo(new VariableInfo("InjectSubOperators", "true if the sub-operators of this combined operator should be injected into the scope", typeof(BoolData), VariableKind.In));
47      GetVariableInfo("InjectSubOperators").Local = true;
48      AddVariable(new Variable("InjectSubOperators", new BoolData(false)));
49      AddVariableInfo(new VariableInfo("SubOperatorNames", "Variable names for injecting the sub-operators", typeof(ItemList<StringData>), VariableKind.In));
50      GetVariableInfo("SubOperatorNames").Local = true;
51      ItemList<StringData> subOperatorNames = new ItemList<StringData>();
52      AddVariable(new Variable("SubOperatorNames", subOperatorNames));
53    }
54
55    public void SetDescription(string description) {
56      if (description == null)
57        throw new NullReferenceException("description must not be null");
58
59      if (description != myDescription) {
60        myDescription = description;
61        OnDescriptionChanged();
62      }
63    }
64
65    public override object Clone(IDictionary<Guid, object> clonedObjects) {
66      CombinedOperator clone = (CombinedOperator)base.Clone(clonedObjects);
67      clone.myDescription = Description;
68      clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
69      return clone;
70    }
71
72    public override IOperation Apply(IScope scope) {
73      if (OperatorGraph.InitialOperator != null) {
74        bool inject = GetVariableValue<BoolData>("InjectSubOperators", scope, false).Data;
75        if (inject) {
76          ItemList<StringData> names = GetVariableValue<ItemList<StringData>>("SubOperatorNames", scope, false);
77          for (int i = 0; i < SubOperators.Count; i++) {
78            if (scope.GetVariable(names[i].Data) != null)
79              scope.RemoveVariable(names[i].Data);
80            scope.AddVariable(new Variable(names[i].Data, SubOperators[i]));
81          }
82        }
83        return new AtomicOperation(OperatorGraph.InitialOperator, scope);
84      } else {
85        return null;
86      }
87    }
88
89    public override IView CreateView() {
90      return new CombinedOperatorView(this);
91    }
92
93    public event EventHandler DescriptionChanged;
94    protected virtual void OnDescriptionChanged() {
95      if (DescriptionChanged != null)
96        DescriptionChanged(this, new EventArgs());
97    }
98
99    #region Persistence Methods
100    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
101      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
102      XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
103      descriptionNode.InnerText = myDescription;
104      node.AppendChild(descriptionNode);
105      node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
106      return node;
107    }
108    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
109      base.Populate(node, restoredObjects);
110      XmlNode descriptionNode = node.SelectSingleNode("Description");
111      if (descriptionNode != null) myDescription = descriptionNode.InnerText;
112      myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
113    }
114    #endregion
115  }
116}
Note: See TracBrowser for help on using the repository browser.