Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Core/ConstrainedItemBase.cs @ 846

Last change on this file since 846 was 836, checked in by gkronber, 16 years ago

worked on #285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

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