Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextWriterBranch/HeuristicLab.Core/OperatorGroup.cs @ 165

Last change on this file since 165 was 119, checked in by gkronber, 16 years ago

created a branch that uses XmlTextWriter instead of XMLDocument to save documents. Investigating ticket #103.

File size: 4.9 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 OperatorGroup : StorableBase, IOperatorGroup {
29    private string myName;
30    public string Name {
31      get { return myName; }
32      set {
33        if (myName != value) {
34          myName = value;
35          OnNameChanged();
36        }
37      }
38    }
39    private List<IOperatorGroup> mySubGroups;
40    public ICollection<IOperatorGroup> SubGroups {
41      get { return mySubGroups.AsReadOnly(); }
42    }
43    private List<IOperator> myOperators;
44    public ICollection<IOperator> Operators {
45      get { return myOperators.AsReadOnly(); }
46    }
47
48    public OperatorGroup() {
49      myName = "Anonymous";
50      mySubGroups = new List<IOperatorGroup>();
51      myOperators = new List<IOperator>();
52    }
53
54    public override object Clone(IDictionary<Guid, object> clonedObjects) {
55      OperatorGroup clone = (OperatorGroup)base.Clone(clonedObjects);
56      clone.myName = Name;
57      foreach (IOperatorGroup group in SubGroups)
58        clone.AddSubGroup((IOperatorGroup)Auxiliary.Clone(group, clonedObjects));
59      foreach (IOperator op in Operators)
60        clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
61      return clone;
62    }
63
64    public virtual void AddSubGroup(IOperatorGroup group) {
65      mySubGroups.Add(group);
66    }
67    public virtual void RemoveSubGroup(IOperatorGroup group) {
68      mySubGroups.Remove(group);
69    }
70    public virtual void AddOperator(IOperator op) {
71      myOperators.Add(op);
72    }
73    public virtual void RemoveOperator(IOperator op) {
74      myOperators.Remove(op);
75    }
76
77    public event EventHandler NameChanged;
78    protected virtual void OnNameChanged() {
79      if (NameChanged != null) {
80        NameChanged(this, new EventArgs());
81      }
82    }
83
84    #region Persistence Methods
85    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
86    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
87    //  XmlAttribute nameAttribute = document.CreateAttribute("Name");
88    //  nameAttribute.Value = Name;
89    //  node.Attributes.Append(nameAttribute);
90    //  XmlNode subGroupsNode = document.CreateNode(XmlNodeType.Element, "SubGroups", null);
91    //  foreach (IOperatorGroup group in SubGroups)
92    //    subGroupsNode.AppendChild(PersistenceManager.Persist(group, document, persistedObjects));
93    //  node.AppendChild(subGroupsNode);
94    //  XmlNode operatorsNode = document.CreateNode(XmlNodeType.Element, "Operators", null);
95    //  foreach (IOperator op in Operators)
96    //    operatorsNode.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
97    //  node.AppendChild(operatorsNode);
98    //  return node;
99    //}
100    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
101      base.Persist(name, writer, persistedObjects);
102      writer.WriteAttributeString("Name", Name);
103      writer.WriteStartElement("SubGroups");
104      foreach(IOperatorGroup group in SubGroups)
105        PersistenceManager.Persist(group, writer, persistedObjects);
106      writer.WriteEndElement(); // </SubGroups>
107      writer.WriteStartElement("Operators");
108      foreach(IOperator op in Operators)
109        PersistenceManager.Persist(op, writer, persistedObjects);
110      writer.WriteEndElement(); // </Operators>
111    }
112    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
113      base.Populate(node, restoredObjects);
114      myName = node.Attributes["Name"].Value;
115      XmlNode subGroupsNode = node.SelectSingleNode("SubGroups");
116      foreach (XmlNode subGroupNode in subGroupsNode.ChildNodes)
117        AddSubGroup((IOperatorGroup)PersistenceManager.Restore(subGroupNode, restoredObjects));
118      XmlNode operatorsNode = node.SelectSingleNode("Operators");
119      foreach (XmlNode operatorNode in operatorsNode.ChildNodes)
120        AddOperator((IOperator)PersistenceManager.Restore(operatorNode, restoredObjects));
121    }
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.