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 Apply(IScope scope) {
|
---|
67 | if (OperatorGraph.InitialOperator != null) {
|
---|
68 | if ((scope.SubScopes.Count != 1) || (scope.SubScopes[0].Name != Guid.ToString())) {
|
---|
69 | PreCall(scope);
|
---|
70 | CompositeOperation next = new CompositeOperation();
|
---|
71 | next.AddOperation(new AtomicOperation(OperatorGraph.InitialOperator, scope.SubScopes[0]));
|
---|
72 | next.AddOperation(new AtomicOperation(this, scope));
|
---|
73 | return next;
|
---|
74 | } else {
|
---|
75 | PostCall(scope);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 | private void PreCall(IScope scope) {
|
---|
81 | // create temporary scope and inject variables
|
---|
82 | IScope temp = new Scope(Guid.ToString());
|
---|
83 |
|
---|
84 | while (scope.SubScopes.Count > 0) {
|
---|
85 | IScope child = scope.SubScopes[0];
|
---|
86 | scope.RemoveSubScope(child);
|
---|
87 | temp.AddSubScope(child);
|
---|
88 | }
|
---|
89 | scope.AddSubScope(temp);
|
---|
90 |
|
---|
91 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
92 | if (scope.GetVariable(SubOperators[i].Name) != null)
|
---|
93 | scope.RemoveVariable(SubOperators[i].Name);
|
---|
94 | scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
|
---|
95 | }
|
---|
96 |
|
---|
97 | foreach (IVariableInfo variableInfo in VariableInfos) {
|
---|
98 | if ((variableInfo.Kind & VariableKind.In) == VariableKind.In) {
|
---|
99 | IItem value = GetVariableValue(variableInfo.FormalName, scope, true, true);
|
---|
100 | temp.AddVariable(new Variable(variableInfo.FormalName, value));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | private void PostCall(IScope scope) {
|
---|
105 | // remove temporary scope and write back variables
|
---|
106 | IScope temp = scope.SubScopes[0];
|
---|
107 |
|
---|
108 | scope.RemoveSubScope(temp);
|
---|
109 | while (temp.SubScopes.Count > 0) {
|
---|
110 | IScope child = temp.SubScopes[0];
|
---|
111 | temp.RemoveSubScope(child);
|
---|
112 | scope.AddSubScope(child);
|
---|
113 | }
|
---|
114 |
|
---|
115 | foreach (IVariableInfo variableInfo in VariableInfos) {
|
---|
116 | if (((variableInfo.Kind & VariableKind.Out) == VariableKind.Out) ||
|
---|
117 | ((variableInfo.Kind & VariableKind.New) == VariableKind.New)) {
|
---|
118 | IItem value = temp.GetVariableValue(variableInfo.FormalName, false, true);
|
---|
119 | if (scope.GetVariable(variableInfo.ActualName) != null)
|
---|
120 | scope.RemoveVariable(variableInfo.ActualName);
|
---|
121 | scope.AddVariable(new Variable(variableInfo.ActualName, value));
|
---|
122 | }
|
---|
123 | if ((variableInfo.Kind & VariableKind.Deleted) == VariableKind.Deleted)
|
---|
124 | scope.RemoveVariable(variableInfo.ActualName);
|
---|
125 | temp.RemoveVariable(variableInfo.FormalName);
|
---|
126 | }
|
---|
127 |
|
---|
128 | foreach (IVariable variable in temp.Variables) {
|
---|
129 | if (scope.GetVariable(variable.Name) != null)
|
---|
130 | scope.RemoveVariable(variable.Name);
|
---|
131 | scope.AddVariable(variable);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override IView CreateView() {
|
---|
136 | return new CombinedOperatorView(this);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public event EventHandler DescriptionChanged;
|
---|
140 | protected virtual void OnDescriptionChanged() {
|
---|
141 | if (DescriptionChanged != null)
|
---|
142 | DescriptionChanged(this, new EventArgs());
|
---|
143 | }
|
---|
144 |
|
---|
145 | #region Persistence Methods
|
---|
146 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
147 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
148 | XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
|
---|
149 | descriptionNode.InnerText = myDescription;
|
---|
150 | node.AppendChild(descriptionNode);
|
---|
151 | node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
|
---|
152 | return node;
|
---|
153 | }
|
---|
154 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
155 | base.Populate(node, restoredObjects);
|
---|
156 | XmlNode descriptionNode = node.SelectSingleNode("Description");
|
---|
157 | if (descriptionNode != null) myDescription = descriptionNode.InnerText;
|
---|
158 | myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
|
---|
159 | }
|
---|
160 | #endregion
|
---|
161 | }
|
---|
162 | }
|
---|