Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextReaderBranch/HeuristicLab.Data/ConstrainedItemList.cs @ 123

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

fixed more bugs (not thoroughly tested but at least it works for OSGP_NOx and OSGA_TSP)

File size: 7.8 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;
24using System.Collections.Generic;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Data {
30  public class ConstrainedItemList : ConstrainedItemBase, IEnumerable, IEnumerable<IItem> {
31    private List<IItem> list;
32    private bool suspendConstraintCheck;
33
34    public ConstrainedItemList()
35      : base() {
36      list = new List<IItem>();
37      suspendConstraintCheck = false;
38    }
39
40    public override IView CreateView() {
41      return new ConstrainedItemListView(this);
42    }
43
44    #region Clone & Persistence
45    public override object Clone(IDictionary<Guid, object> clonedObjects) {
46      ConstrainedItemList clone = new ConstrainedItemList();
47      clonedObjects.Add(Guid, clone);
48      foreach (IConstraint constraint in Constraints)
49        clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
50      clone.suspendConstraintCheck = suspendConstraintCheck;
51      foreach (IItem item in list) {
52        clone.list.Add((IItem)Auxiliary.Clone(item, clonedObjects));
53      }
54      return clone;
55    }
56
57    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
58      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
59      XmlNode listNode = document.CreateNode(XmlNodeType.Element, "ListItems", null);
60      for (int i = 0; i < list.Count; i++)
61        listNode.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
62      XmlAttribute sccAttrib = document.CreateAttribute("SuspendConstraintCheck");
63      sccAttrib.Value = suspendConstraintCheck.ToString();
64      listNode.Attributes.Append(sccAttrib);
65      node.AppendChild(listNode);
66      return node;
67    }
68
69    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
70    //  base.Populate(node, restoredObjects);
71    //  XmlNode listNode = node.SelectSingleNode("ListItems");
72    //  list = new List<IItem>();
73    //  for(int i = 0; i < listNode.ChildNodes.Count; i++)
74    //    list.Add((IItem)PersistenceManager.Restore(listNode.ChildNodes[i], restoredObjects));
75    //  suspendConstraintCheck = bool.Parse(listNode.Attributes["SuspendConstraintCheck"].Value);
76    //}
77    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
78      base.Populate(reader, restoredObjects);
79      if(reader.Name!="ListItems") throw new XmlException("Expected: \"ListItems\", found: \""+reader.Name+"\"");
80      suspendConstraintCheck = bool.Parse(reader["SuspendConstraintCheck"]);
81      list = new List<IItem>();
82      if(!reader.IsEmptyElement) {
83        while(reader.IsStartElement()) {
84          list.Add((IItem)PersistenceManager.Restore(reader, restoredObjects));
85          reader.Skip();
86        }
87        reader.ReadEndElement();
88      } else {
89        reader.Read();
90      }
91    }
92    #endregion
93
94    public override string ToString() {
95      if (list.Count > 0) {
96        StringBuilder builder = new StringBuilder();
97        builder.Append(list[0].ToString());
98        for (int i = 1; i < list.Count; i++) {
99          builder.Append(";");
100          builder.Append(list[i].ToString());
101        }
102        return builder.ToString();
103      } else {
104        return "Empty List";
105      }
106    }
107
108    public int IndexOf(IItem item) {
109      return list.IndexOf(item);
110    }
111
112    public void BeginCombinedOperation() {
113      suspendConstraintCheck = true;
114    }
115
116    public bool EndCombinedOperation(out ICollection<IConstraint> violatedConstraints) {
117      suspendConstraintCheck = false;
118      return IsValid(out violatedConstraints);
119    }
120
121    public bool TryInsert(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
122      list.Insert(index, item);
123      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
124        OnItemAdded(item, index);
125        return true;
126      } else {
127        violatedConstraints = new List<IConstraint>();
128        list.RemoveAt(index);
129        return false;
130      }
131    }
132
133    public bool TryRemoveAt(int index, out ICollection<IConstraint> violatedConstraints) {
134      IItem item = list[index];
135      list.RemoveAt(index);
136      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
137        OnItemRemoved(item, index);
138        return true;
139      } else {
140        violatedConstraints = new List<IConstraint>();
141        list.Insert(index, item);
142        return false;
143      }
144    }
145
146    public IItem this[int index] {
147      get { return list[index]; }
148    }
149
150    public bool TrySetAt(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
151      IItem backup = this[index];
152      list[index] = item;
153      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
154        return true;
155      } else {
156        violatedConstraints = new List<IConstraint>();
157        list[index] = backup;
158        return false;
159      }
160    }
161
162    public bool TryAdd(IItem item, out ICollection<IConstraint> violatedConstraints) {
163      list.Add(item);
164      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
165        OnItemAdded(item, list.Count - 1);
166        return true;
167      } else {
168        violatedConstraints = new List<IConstraint>();
169        list.RemoveAt(list.Count - 1);
170        return false;
171      }
172    }
173    public void Clear() {
174      list.Clear();
175      OnCleared();
176    }
177    public bool Contains(IItem item) {
178      return list.Contains(item);
179    }
180    public void CopyTo(IItem[] array, int arrayIndex) {
181      list.CopyTo(array, arrayIndex);
182    }
183    public int Count {
184      get { return list.Count; }
185    }
186    public bool IsReadOnly {
187      get { return false; }
188    }
189    public bool TryRemove(IItem item, out ICollection<IConstraint> violatedConstraints) {
190      int index = list.IndexOf(item);
191      if (index >= 0) {
192        return TryRemoveAt(index, out violatedConstraints);
193      } else {
194        violatedConstraints = new List<IConstraint>();
195        return false;
196      }
197    }
198
199    public IEnumerator<IItem> GetEnumerator() {
200      return list.GetEnumerator();
201    }
202
203    IEnumerator IEnumerable.GetEnumerator() {
204      return list.GetEnumerator();
205    }
206
207    public event EventHandler<ItemIndexEventArgs> ItemAdded;
208    protected virtual void OnItemAdded(IItem item, int index) {
209      if (ItemAdded != null)
210        ItemAdded(this, new ItemIndexEventArgs(item, index));
211      OnChanged();
212    }
213    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
214    protected virtual void OnItemRemoved(IItem item, int index) {
215      if (ItemRemoved != null)
216        ItemRemoved(this, new ItemIndexEventArgs(item, index));
217      OnChanged();
218    }
219    public event EventHandler Cleared;
220    protected virtual void OnCleared() {
221      if (Cleared != null)
222        Cleared(this, new EventArgs());
223      OnChanged();
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.