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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 |
|
---|
29 | namespace 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.
|
---|
44 |
|
---|
45 | A 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 Execute(IScope scope) {
|
---|
67 | myCanceled = false;
|
---|
68 |
|
---|
69 | if (scope.GetVariable(Guid.ToString()) == null) { // contained operator not yet executed
|
---|
70 | // add marker
|
---|
71 | scope.AddVariable(new Variable(Guid.ToString(), new NullData()));
|
---|
72 |
|
---|
73 | // add aliases
|
---|
74 | foreach (IVariableInfo variableInfo in VariableInfos)
|
---|
75 | scope.AddAlias(variableInfo.FormalName, variableInfo.ActualName);
|
---|
76 |
|
---|
77 | CompositeOperation next = new CompositeOperation();
|
---|
78 | next.AddOperation(Apply(scope));
|
---|
79 | // execute combined operator again after contained operators have been executed
|
---|
80 | next.AddOperation(new AtomicOperation(this, scope));
|
---|
81 |
|
---|
82 | OnExecuted();
|
---|
83 | return next;
|
---|
84 | } else { // contained operator already executed
|
---|
85 | // remove marker
|
---|
86 | scope.RemoveVariable(Guid.ToString());
|
---|
87 |
|
---|
88 | // remove aliases
|
---|
89 | foreach (IVariableInfo variableInfo in VariableInfos)
|
---|
90 | scope.RemoveAlias(variableInfo.FormalName);
|
---|
91 |
|
---|
92 | OnExecuted();
|
---|
93 | return null;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public override IOperation Apply(IScope scope) {
|
---|
98 | if (OperatorGraph.InitialOperator != null) {
|
---|
99 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
100 | if (scope.GetVariable(SubOperators[i].Name) != null)
|
---|
101 | scope.RemoveVariable(SubOperators[i].Name);
|
---|
102 | scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
|
---|
103 | }
|
---|
104 | return new AtomicOperation(OperatorGraph.InitialOperator, scope);
|
---|
105 | } else {
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override IView CreateView() {
|
---|
111 | return new CombinedOperatorView(this);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public event EventHandler DescriptionChanged;
|
---|
115 | protected virtual void OnDescriptionChanged() {
|
---|
116 | if (DescriptionChanged != null)
|
---|
117 | DescriptionChanged(this, new EventArgs());
|
---|
118 | }
|
---|
119 |
|
---|
120 | #region Persistence Methods
|
---|
121 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
122 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
123 | XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
|
---|
124 | descriptionNode.InnerText = myDescription;
|
---|
125 | node.AppendChild(descriptionNode);
|
---|
126 | node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
|
---|
127 | return node;
|
---|
128 | }
|
---|
129 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
130 | base.Populate(node, restoredObjects);
|
---|
131 | XmlNode descriptionNode = node.SelectSingleNode("Description");
|
---|
132 | if (descriptionNode != null) myDescription = descriptionNode.InnerText;
|
---|
133 | myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 | }
|
---|
137 | }
|
---|