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