#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Xml; using HeuristicLab.Common; namespace HeuristicLab.Core { /// /// Base class for all items that are subjects to restrictions. /// public abstract class ConstrainedItemBase : ItemBase, IConstrainedItem { private List myConstraints; /// /// Gets all current constraints. /// The constraints are returned read-only. /// public virtual ICollection Constraints { get { return myConstraints.AsReadOnly(); } } /// /// Initializes a new instance of . /// protected ConstrainedItemBase() { myConstraints = new List(); } /// /// Clones the current instance (deep clone). /// /// Calls /// of base class .
/// Deep clone through method of helper class /// .
/// Dictionary of all already cloned objects. (Needed to avoid cycles.) /// The cloned object as . public override object Clone(IDictionary clonedObjects) { ConstrainedItemBase clone = (ConstrainedItemBase)base.Clone(clonedObjects); clone.myConstraints.Clear(); foreach (IConstraint constraint in Constraints) clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects)); return clone; } /// /// Adds the given to the current list. /// /// Calls . /// The constraint to add. public virtual void AddConstraint(IConstraint constraint) { myConstraints.Add(constraint); OnConstraintAdded(constraint); } /// /// Removes the given from the current list. /// /// Calls if the constraint can be successfully removed. /// The constraint to remove. public virtual void RemoveConstraint(IConstraint constraint) { if (myConstraints.Remove(constraint)) OnConstraintRemoved(constraint); } /// /// Checks all constraints of the current instance. /// /// true if all constraints could be fulfilled, false otherwise. public bool IsValid() { bool result = true; foreach (IConstraint constraint in Constraints) result = result && constraint.Check(this); return result; } /// /// Checks all constraints of the current instance. /// /// Output parameter; contains all constraints that could not be /// fulfilled. /// true if all constraints could be fulfilled, false otherwise. public bool IsValid(out ICollection violatedConstraints) { bool result = true; violatedConstraints = new List(); foreach (IConstraint constraint in Constraints) { if (!constraint.Check(this)) { result = false; violatedConstraints.Add(constraint); } } return result; } /// /// Creates an instance of /// to represent the current instance visually. /// /// The created view as . public override IView CreateView() { return new ConstrainedItemBaseView(this); } /// /// Occurs when a constraint is added. /// public event EventHandler> ConstraintAdded; /// /// Fires a new ConstraintAdded event. /// /// The constraint that was added. protected virtual void OnConstraintAdded(IConstraint constraint) { if (ConstraintAdded != null) ConstraintAdded(this, new EventArgs(constraint)); } /// /// Occurs when a constraint is removed. /// public event EventHandler> ConstraintRemoved; /// /// Fires a new ConstraintRemoved event. /// /// The constraint that was removed. protected virtual void OnConstraintRemoved(IConstraint constraint) { if (ConstraintRemoved != null) ConstraintRemoved(this, new EventArgs(constraint)); } #region Persistence Methods /// /// Saves the current instance as in the specified . /// /// Calls of base class .
/// For saving the constraints a child node is created having the tag name Constraints. Beyond this /// child node all constraints are saved as child nodes themselves.
/// The (tag)name of the . /// The where to save the data. /// The dictionary of all already persisted objects. /// (Needed to avoid cycles.) /// The saved . public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) { XmlNode node = base.GetXmlNode(name, document, persistedObjects); if (Constraints.Count > 0) { XmlNode constraintsNode = document.CreateNode(XmlNodeType.Element, "Constraints", null); foreach (IConstraint constraint in Constraints) constraintsNode.AppendChild(PersistenceManager.Persist(constraint, document, persistedObjects)); node.AppendChild(constraintsNode); } return node; } /// /// Loads the persisted item from the specified . /// /// See to get information about how the constrained item must /// be saved.
/// Calls of base class .
/// The where the constrained item is saved. /// The dictionary of all already restored objects. /// (Needed to avoid cycles.) public override void Populate(XmlNode node, IDictionary restoredObjects) { base.Populate(node, restoredObjects); XmlNode constraintsNode = node.SelectSingleNode("Constraints"); if (constraintsNode != null) { myConstraints.Clear(); foreach (XmlNode constraintNode in constraintsNode.ChildNodes) AddConstraint((IConstraint)PersistenceManager.Restore(constraintNode, restoredObjects)); } } #endregion } }