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 |
|
---|
27 | namespace HeuristicLab.Core {
|
---|
28 | public class OperatorGraph : ItemBase, IOperatorGraph {
|
---|
29 | private IDictionary<Guid, IOperator> myOperators;
|
---|
30 | public ICollection<IOperator> Operators {
|
---|
31 | get { return myOperators.Values; }
|
---|
32 | }
|
---|
33 | private IOperator myInitialOperator;
|
---|
34 | public IOperator InitialOperator {
|
---|
35 | get { return myInitialOperator; }
|
---|
36 | set {
|
---|
37 | if (myInitialOperator != value) {
|
---|
38 | myInitialOperator = value;
|
---|
39 | OnInitialOperatorChanged();
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public OperatorGraph() {
|
---|
45 | myOperators = new Dictionary<Guid, IOperator>();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override IView CreateView() {
|
---|
49 | return new OperatorGraphView(this);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
53 | OperatorGraph clone = new OperatorGraph();
|
---|
54 | clonedObjects.Add(Guid, clone);
|
---|
55 | foreach (IOperator op in Operators)
|
---|
56 | clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
|
---|
57 | if (InitialOperator != null)
|
---|
58 | clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
|
---|
59 | return clone;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void AddOperator(IOperator op) {
|
---|
63 | if (!myOperators.ContainsKey(op.Guid)) {
|
---|
64 | myOperators.Add(op.Guid, op);
|
---|
65 | OnOperatorAdded(op);
|
---|
66 |
|
---|
67 | foreach (IOperator subOperator in op.SubOperators)
|
---|
68 | AddOperator(subOperator);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | public void RemoveOperator(Guid guid) {
|
---|
72 | IOperator op = GetOperator(guid);
|
---|
73 | if (op != null) {
|
---|
74 | foreach (IOperator o in Operators) {
|
---|
75 | int i = 0;
|
---|
76 | while (i < o.SubOperators.Count) {
|
---|
77 | if (o.SubOperators[i] == op)
|
---|
78 | o.RemoveSubOperator(i);
|
---|
79 | else
|
---|
80 | i++;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | if (InitialOperator == op)
|
---|
84 | InitialOperator = null;
|
---|
85 | myOperators.Remove(op.Guid);
|
---|
86 | OnOperatorRemoved(op);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | public IOperator GetOperator(Guid guid) {
|
---|
90 | IOperator op;
|
---|
91 | if (myOperators.TryGetValue(guid, out op))
|
---|
92 | return op;
|
---|
93 | else
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 | public void Reset() {
|
---|
97 | foreach (IOperator op in Operators)
|
---|
98 | op.Reset();
|
---|
99 | }
|
---|
100 | public void Clear() {
|
---|
101 | Guid[] guids = new Guid[Operators.Count];
|
---|
102 | int i = 0;
|
---|
103 | foreach (IOperator op in Operators) {
|
---|
104 | guids[i] = op.Guid;
|
---|
105 | i++;
|
---|
106 | }
|
---|
107 | for (int j = 0; j < guids.Length; j++)
|
---|
108 | RemoveOperator(guids[j]);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public event EventHandler<OperatorEventArgs> OperatorAdded;
|
---|
112 | protected virtual void OnOperatorAdded(IOperator op) {
|
---|
113 | if (OperatorAdded != null)
|
---|
114 | OperatorAdded(this, new OperatorEventArgs(op));
|
---|
115 | }
|
---|
116 | public event EventHandler<OperatorEventArgs> OperatorRemoved;
|
---|
117 | protected virtual void OnOperatorRemoved(IOperator op) {
|
---|
118 | if (OperatorRemoved != null)
|
---|
119 | OperatorRemoved(this, new OperatorEventArgs(op));
|
---|
120 | }
|
---|
121 | public event EventHandler InitialOperatorChanged;
|
---|
122 | protected virtual void OnInitialOperatorChanged() {
|
---|
123 | if (InitialOperatorChanged != null)
|
---|
124 | InitialOperatorChanged(this, new EventArgs());
|
---|
125 | }
|
---|
126 |
|
---|
127 | #region Persistence Methods
|
---|
128 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
129 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
130 | XmlNode ops = document.CreateNode(XmlNodeType.Element, "Operators", null);
|
---|
131 | foreach (IOperator op in myOperators.Values)
|
---|
132 | ops.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
|
---|
133 | node.AppendChild(ops);
|
---|
134 | if (InitialOperator != null)
|
---|
135 | node.AppendChild(PersistenceManager.Persist("InitialOperator", InitialOperator, document, persistedObjects));
|
---|
136 | return node;
|
---|
137 | }
|
---|
138 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
139 | base.Populate(node, restoredObjects);
|
---|
140 |
|
---|
141 | XmlNode ops = node.SelectSingleNode("Operators");
|
---|
142 | for (int i = 0; i < ops.ChildNodes.Count; i++) {
|
---|
143 | XmlNode opNode = ops.ChildNodes[i];
|
---|
144 | IOperator op = (IOperator)PersistenceManager.Restore(opNode, restoredObjects);
|
---|
145 | myOperators.Add(op.Guid, op);
|
---|
146 | }
|
---|
147 |
|
---|
148 | XmlNode initial = node.SelectSingleNode("InitialOperator");
|
---|
149 | if (initial != null)
|
---|
150 | myInitialOperator = (IOperator)PersistenceManager.Restore(initial, restoredObjects);
|
---|
151 | }
|
---|
152 | #endregion
|
---|
153 | }
|
---|
154 | }
|
---|