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;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Text;
|
---|
26 | using System.Xml;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 |
|
---|
29 | namespace 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 | public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
69 | base.Persist(name, writer, persistedObjects);
|
---|
70 | writer.WriteStartElement("ListItems");
|
---|
71 | writer.WriteAttributeString("SuspendConstraintCheck", suspendConstraintCheck.ToString());
|
---|
72 | for(int i = 0; i < list.Count; i++)
|
---|
73 | PersistenceManager.Persist(list[i], writer, persistedObjects);
|
---|
74 | writer.WriteEndElement(); // </ListItems>
|
---|
75 | }
|
---|
76 |
|
---|
77 | //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
78 | // base.Populate(node, restoredObjects);
|
---|
79 | // XmlNode listNode = node.SelectSingleNode("ListItems");
|
---|
80 | // list = new List<IItem>();
|
---|
81 | // for(int i = 0; i < listNode.ChildNodes.Count; i++)
|
---|
82 | // list.Add((IItem)PersistenceManager.Restore(listNode.ChildNodes[i], restoredObjects));
|
---|
83 | // suspendConstraintCheck = bool.Parse(listNode.Attributes["SuspendConstraintCheck"].Value);
|
---|
84 | //}
|
---|
85 | public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
86 | base.Populate(reader, restoredObjects);
|
---|
87 | if(reader.Name!="ListItems") throw new XmlException("Expected: \"ListItems\", found: \""+reader.Name+"\"");
|
---|
88 | suspendConstraintCheck = bool.Parse(reader["SuspendConstraintCheck"]);
|
---|
89 | list = new List<IItem>();
|
---|
90 | if(!reader.IsEmptyElement) {
|
---|
91 | while(reader.IsStartElement()) {
|
---|
92 | list.Add((IItem)PersistenceManager.Restore(reader, restoredObjects));
|
---|
93 | reader.Skip();
|
---|
94 | }
|
---|
95 | reader.ReadEndElement();
|
---|
96 | } else {
|
---|
97 | reader.Read();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | public override string ToString() {
|
---|
103 | if (list.Count > 0) {
|
---|
104 | StringBuilder builder = new StringBuilder();
|
---|
105 | builder.Append(list[0].ToString());
|
---|
106 | for (int i = 1; i < list.Count; i++) {
|
---|
107 | builder.Append(";");
|
---|
108 | builder.Append(list[i].ToString());
|
---|
109 | }
|
---|
110 | return builder.ToString();
|
---|
111 | } else {
|
---|
112 | return "Empty List";
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public int IndexOf(IItem item) {
|
---|
117 | return list.IndexOf(item);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void BeginCombinedOperation() {
|
---|
121 | suspendConstraintCheck = true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public bool EndCombinedOperation(out ICollection<IConstraint> violatedConstraints) {
|
---|
125 | suspendConstraintCheck = false;
|
---|
126 | return IsValid(out violatedConstraints);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public bool TryInsert(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
|
---|
130 | list.Insert(index, item);
|
---|
131 | if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
|
---|
132 | OnItemAdded(item, index);
|
---|
133 | return true;
|
---|
134 | } else {
|
---|
135 | violatedConstraints = new List<IConstraint>();
|
---|
136 | list.RemoveAt(index);
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | public bool TryRemoveAt(int index, out ICollection<IConstraint> violatedConstraints) {
|
---|
142 | IItem item = list[index];
|
---|
143 | list.RemoveAt(index);
|
---|
144 | if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
|
---|
145 | OnItemRemoved(item, index);
|
---|
146 | return true;
|
---|
147 | } else {
|
---|
148 | violatedConstraints = new List<IConstraint>();
|
---|
149 | list.Insert(index, item);
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public IItem this[int index] {
|
---|
155 | get { return list[index]; }
|
---|
156 | }
|
---|
157 |
|
---|
158 | public bool TrySetAt(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
|
---|
159 | IItem backup = this[index];
|
---|
160 | list[index] = item;
|
---|
161 | if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
|
---|
162 | return true;
|
---|
163 | } else {
|
---|
164 | violatedConstraints = new List<IConstraint>();
|
---|
165 | list[index] = backup;
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public bool TryAdd(IItem item, out ICollection<IConstraint> violatedConstraints) {
|
---|
171 | list.Add(item);
|
---|
172 | if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
|
---|
173 | OnItemAdded(item, list.Count - 1);
|
---|
174 | return true;
|
---|
175 | } else {
|
---|
176 | violatedConstraints = new List<IConstraint>();
|
---|
177 | list.RemoveAt(list.Count - 1);
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | public void Clear() {
|
---|
182 | list.Clear();
|
---|
183 | OnCleared();
|
---|
184 | }
|
---|
185 | public bool Contains(IItem item) {
|
---|
186 | return list.Contains(item);
|
---|
187 | }
|
---|
188 | public void CopyTo(IItem[] array, int arrayIndex) {
|
---|
189 | list.CopyTo(array, arrayIndex);
|
---|
190 | }
|
---|
191 | public int Count {
|
---|
192 | get { return list.Count; }
|
---|
193 | }
|
---|
194 | public bool IsReadOnly {
|
---|
195 | get { return false; }
|
---|
196 | }
|
---|
197 | public bool TryRemove(IItem item, out ICollection<IConstraint> violatedConstraints) {
|
---|
198 | int index = list.IndexOf(item);
|
---|
199 | if (index >= 0) {
|
---|
200 | return TryRemoveAt(index, out violatedConstraints);
|
---|
201 | } else {
|
---|
202 | violatedConstraints = new List<IConstraint>();
|
---|
203 | return false;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public IEnumerator<IItem> GetEnumerator() {
|
---|
208 | return list.GetEnumerator();
|
---|
209 | }
|
---|
210 |
|
---|
211 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
212 | return list.GetEnumerator();
|
---|
213 | }
|
---|
214 |
|
---|
215 | public event EventHandler<ItemIndexEventArgs> ItemAdded;
|
---|
216 | protected virtual void OnItemAdded(IItem item, int index) {
|
---|
217 | if (ItemAdded != null)
|
---|
218 | ItemAdded(this, new ItemIndexEventArgs(item, index));
|
---|
219 | OnChanged();
|
---|
220 | }
|
---|
221 | public event EventHandler<ItemIndexEventArgs> ItemRemoved;
|
---|
222 | protected virtual void OnItemRemoved(IItem item, int index) {
|
---|
223 | if (ItemRemoved != null)
|
---|
224 | ItemRemoved(this, new ItemIndexEventArgs(item, index));
|
---|
225 | OnChanged();
|
---|
226 | }
|
---|
227 | public event EventHandler Cleared;
|
---|
228 | protected virtual void OnCleared() {
|
---|
229 | if (Cleared != null)
|
---|
230 | Cleared(this, new EventArgs());
|
---|
231 | OnChanged();
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|