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