#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.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Core {
///
/// Base class for all items that are subjects to restrictions.
///
public abstract class ConstrainedItemBase : ItemBase, IConstrainedItem {
[Storable]
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 ConstraintEventArgs(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 ConstraintEventArgs(constraint));
}
}
}