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.\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), VariableKind.In));
|
---|
50 | GetVariableInfo("SubOperatorNames").Local = true;
|
---|
51 | ItemList subOperatorNames = new ItemList();
|
---|
52 | subOperatorNames.ItemType = typeof(StringData);
|
---|
53 | AddVariable(new Variable("SubOperatorNames", subOperatorNames));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void SetDescription(string description) {
|
---|
57 | if (description == null)
|
---|
58 | throw new NullReferenceException("description must not be null");
|
---|
59 |
|
---|
60 | if (description != myDescription) {
|
---|
61 | myDescription = description;
|
---|
62 | OnDescriptionChanged();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
67 | CombinedOperator clone = (CombinedOperator)base.Clone(clonedObjects);
|
---|
68 | clone.myDescription = Description;
|
---|
69 | clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
|
---|
70 | return clone;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override IOperation Apply(IScope scope) {
|
---|
74 | if (OperatorGraph.InitialOperator != null) {
|
---|
75 | bool inject = GetVariableValue<BoolData>("InjectSubOperators", scope, false).Data;
|
---|
76 | if (inject) {
|
---|
77 | ItemList names = GetVariableValue<ItemList>("SubOperatorNames", scope, false);
|
---|
78 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
79 | if (scope.GetVariable(names[i].ToString()) != null)
|
---|
80 | scope.RemoveVariable(names[i].ToString());
|
---|
81 | scope.AddVariable(new Variable(names[i].ToString(), SubOperators[i]));
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return new AtomicOperation(OperatorGraph.InitialOperator, scope);
|
---|
85 | } else {
|
---|
86 | return null;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | public override void Reset() {
|
---|
90 | OperatorGraph.Reset();
|
---|
91 | base.Reset();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override IView CreateView() {
|
---|
95 | return new CombinedOperatorView(this);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public event EventHandler DescriptionChanged;
|
---|
99 | protected virtual void OnDescriptionChanged() {
|
---|
100 | if (DescriptionChanged != null)
|
---|
101 | DescriptionChanged(this, new EventArgs());
|
---|
102 | }
|
---|
103 |
|
---|
104 | #region Persistence Methods
|
---|
105 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
106 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
107 | XmlNode descriptionNode = document.CreateNode(XmlNodeType.Element, "Description", null);
|
---|
108 | descriptionNode.InnerText = myDescription;
|
---|
109 | node.AppendChild(descriptionNode);
|
---|
110 | node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
|
---|
111 | return node;
|
---|
112 | }
|
---|
113 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
114 | base.Populate(node, restoredObjects);
|
---|
115 | XmlNode descriptionNode = node.SelectSingleNode("Description");
|
---|
116 | if (descriptionNode != null) myDescription = descriptionNode.InnerText;
|
---|
117 | myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 | }
|
---|
121 | }
|
---|