Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/ConstrainedItemBase.cs @ 2806

Last change on this file since 2806 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 8.3 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;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Core {
29  /// <summary>
30  /// Base class for all items that are subjects to restrictions.
31  /// </summary>
32  public abstract class ConstrainedItemBase : ItemBase, IConstrainedItem {
33    private List<IConstraint> myConstraints;
34    /// <summary>
35    /// Gets all current constraints.
36    /// <note type="caution"> The constraints are returned read-only.</note>
37    /// </summary>
38    public virtual ICollection<IConstraint> Constraints {
39      get { return myConstraints.AsReadOnly(); }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ConstrainedItemBase"/>.
44    /// </summary>
45    protected ConstrainedItemBase() {
46      myConstraints = new List<IConstraint>();
47    }
48
49    /// <summary>
50    /// Clones the current instance (deep clone).
51    /// </summary>
52    /// <remarks>Calls <see cref="StorableBase.Clone
53    /// (System.Collections.Generic.IDictionary&lt;System.Guid, object&gt;)"/>
54    /// of base class <see cref="ItemBase"/>.<br/>
55    /// Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
56    /// <see cref="Auxiliary"/>.</remarks>
57    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
58    /// <returns>The cloned object as <see cref="ConstrainedItemBase"/>.</returns>
59    public override object Clone(IDictionary<Guid, object> clonedObjects) {
60      ConstrainedItemBase clone = (ConstrainedItemBase)base.Clone(clonedObjects);
61      clone.myConstraints.Clear();
62      foreach (IConstraint constraint in Constraints)
63        clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
64      return clone;
65    }
66
67    /// <summary>
68    /// Adds the given <paramref name="constraint"/> to the current list.
69    /// </summary>
70    /// <remarks>Calls <see cref="OnConstraintAdded"/>.</remarks>
71    /// <param name="constraint">The constraint to add.</param>
72    public virtual void AddConstraint(IConstraint constraint) {
73      myConstraints.Add(constraint);
74      OnConstraintAdded(constraint);
75    }
76    /// <summary>
77    /// Removes the given <paramref name="constraint"/> from the current list.
78    /// </summary>
79    /// <remarks>Calls <see cref="OnConstraintRemoved"/> if the constraint can be successfully removed.</remarks>
80    /// <param name="constraint">The constraint to remove.</param>
81    public virtual void RemoveConstraint(IConstraint constraint) {
82      if (myConstraints.Remove(constraint))
83        OnConstraintRemoved(constraint);
84    }
85
86    /// <summary>
87    /// Checks all constraints of the current instance.
88    /// </summary>
89    /// <returns><c>true</c> if all constraints could be fulfilled, <c>false</c> otherwise.</returns>
90    public bool IsValid() {
91      bool result = true;
92      foreach (IConstraint constraint in Constraints)
93        result = result && constraint.Check(this);
94      return result;
95    }
96    /// <summary>
97    /// Checks all constraints of the current instance.
98    /// </summary>
99    /// <param name="violatedConstraints">Output parameter; contains all constraints that could not be
100    /// fulfilled.</param>
101    /// <returns><c>true</c> if all constraints could be fulfilled, <c>false</c> otherwise.</returns>
102    public bool IsValid(out ICollection<IConstraint> violatedConstraints) {
103      bool result = true;
104      violatedConstraints = new List<IConstraint>();
105      foreach (IConstraint constraint in Constraints) {
106        if (!constraint.Check(this)) {
107          result = false;
108          violatedConstraints.Add(constraint);
109        }
110      }
111      return result;
112    }
113
114    /// <summary>
115    /// Creates an instance of <see cref="ConstrainedItemBaseView"/>
116    /// to represent the current instance visually.
117    /// </summary>
118    /// <returns>The created view as <see cref="ConstrainedItemBaseView"/>.</returns>
119    public override IView CreateView() {
120      return new ConstrainedItemBaseView(this);
121    }
122
123    /// <summary>
124    /// Occurs when a constraint is added.
125    /// </summary>
126    public event EventHandler<EventArgs<IConstraint>> ConstraintAdded;
127    /// <summary>
128    /// Fires a new <c>ConstraintAdded</c> event.
129    /// </summary>
130    /// <param name="constraint">The constraint that was added.</param>
131    protected virtual void OnConstraintAdded(IConstraint constraint) {
132      if (ConstraintAdded != null)
133        ConstraintAdded(this, new EventArgs<IConstraint>(constraint));
134    }
135    /// <summary>
136    /// Occurs when a constraint is removed.
137    /// </summary>
138    public event EventHandler<EventArgs<IConstraint>> ConstraintRemoved;
139    /// <summary>
140    /// Fires a new <c>ConstraintRemoved</c> event.
141    /// </summary>
142    /// <param name="constraint">The constraint that was removed.</param>
143    protected virtual void OnConstraintRemoved(IConstraint constraint) {
144      if (ConstraintRemoved != null)
145        ConstraintRemoved(this, new EventArgs<IConstraint>(constraint));
146    }
147
148    #region Persistence Methods 
149    /// <summary>
150    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
151    /// </summary>
152    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>. <br/>
153    /// For saving the constraints a child node is created having the tag name <c>Constraints</c>. Beyond this
154    /// child node all constraints are saved as child nodes themselves.</remarks>
155    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
156    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
157    /// <param name="persistedObjects">The dictionary of all already persisted objects.
158    /// (Needed to avoid cycles.)</param>
159    /// <returns>The saved <see cref="XmlNode"/>.</returns>
160    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
161      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
162      if (Constraints.Count > 0) {
163        XmlNode constraintsNode = document.CreateNode(XmlNodeType.Element, "Constraints", null);
164        foreach (IConstraint constraint in Constraints)
165          constraintsNode.AppendChild(PersistenceManager.Persist(constraint, document, persistedObjects));
166        node.AppendChild(constraintsNode);
167      }
168      return node;
169    }
170    /// <summary>
171    /// Loads the persisted item from the specified <paramref name="node"/>.
172    /// </summary>
173    /// <remarks>See <see cref="GetXmlNode"/> to get information about how the constrained item must
174    /// be saved. <br/>
175    /// Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.</remarks>
176    /// <param name="node">The <see cref="XmlNode"/> where the constrained item is saved.</param>
177    /// <param name="restoredObjects">The dictionary of all already restored objects.
178    /// (Needed to avoid cycles.)</param>
179    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
180      base.Populate(node, restoredObjects);
181      XmlNode constraintsNode = node.SelectSingleNode("Constraints");
182      if (constraintsNode != null) {
183        myConstraints.Clear();
184        foreach (XmlNode constraintNode in constraintsNode.ChildNodes)
185          AddConstraint((IConstraint)PersistenceManager.Restore(constraintNode, restoredObjects));
186      }
187    }
188    #endregion
189  }
190}
Note: See TracBrowser for help on using the repository browser.