Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Core/OperatorGraph.cs @ 175

Last change on this file since 175 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

File size: 6.5 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;
26
27namespace 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 Clear() {
97      Guid[] guids = new Guid[Operators.Count];
98      int i = 0;
99      foreach(IOperator op in Operators) {
100        guids[i] = op.Guid;
101        i++;
102      }
103      for(int j = 0; j < guids.Length; j++)
104        RemoveOperator(guids[j]);
105    }
106
107    public event EventHandler<OperatorEventArgs> OperatorAdded;
108    protected virtual void OnOperatorAdded(IOperator op) {
109      if(OperatorAdded != null)
110        OperatorAdded(this, new OperatorEventArgs(op));
111    }
112    public event EventHandler<OperatorEventArgs> OperatorRemoved;
113    protected virtual void OnOperatorRemoved(IOperator op) {
114      if(OperatorRemoved != null)
115        OperatorRemoved(this, new OperatorEventArgs(op));
116    }
117    public event EventHandler InitialOperatorChanged;
118    protected virtual void OnInitialOperatorChanged() {
119      if(InitialOperatorChanged != null)
120        InitialOperatorChanged(this, new EventArgs());
121    }
122
123    #region Persistence Methods
124    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
125    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
126    //  XmlNode ops = document.CreateNode(XmlNodeType.Element, "Operators", null);
127    //  foreach (IOperator op in myOperators.Values)
128    //    ops.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
129    //  node.AppendChild(ops);
130    //  if (InitialOperator != null)
131    //    node.AppendChild(PersistenceManager.Persist("InitialOperator", InitialOperator, document, persistedObjects));
132    //  return node;
133    //}
134    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
135      base.Persist(name, writer, persistedObjects);
136      writer.WriteStartElement("Operators");
137      foreach(IOperator op in myOperators.Values)
138        PersistenceManager.Persist(op, writer, persistedObjects);
139      writer.WriteEndElement(); // </Operators>
140      if(InitialOperator != null)
141        PersistenceManager.Persist("InitialOperator", InitialOperator, writer, persistedObjects);
142    }
143    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
144    //  base.Populate(node, restoredObjects);
145
146    //  XmlNode ops = node.SelectSingleNode("Operators");
147    //  for(int i = 0; i < ops.ChildNodes.Count; i++) {
148    //    XmlNode opNode = ops.ChildNodes[i];
149    //    IOperator op = (IOperator)PersistenceManager.Restore(opNode, restoredObjects);
150    //    myOperators.Add(op.Guid, op);
151    //  }
152
153    //  XmlNode initial = node.SelectSingleNode("InitialOperator");
154    //  if(initial != null)
155    //    myInitialOperator = (IOperator)PersistenceManager.Restore(initial, restoredObjects);
156    //}
157    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
158      base.Populate(reader, restoredObjects);
159
160      reader.Read();
161      if(reader.Name != "Operators") throw new XmlException("Expected: Operators found: " + reader.Name);
162      reader.Read();
163      while(reader.IsStartElement()) {
164        IOperator op = (IOperator)PersistenceManager.Restore(reader, restoredObjects);
165        reader.Skip();
166        myOperators.Add(op.Guid, op);
167      }
168      reader.ReadEndElement();
169      if(reader.IsStartElement()) {
170        myInitialOperator = (IOperator)PersistenceManager.Restore(reader, "InitialOperator", restoredObjects);
171      }
172    }
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.