Free cookie consent management tool by TermsFeed Policy Generator

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

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

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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