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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace 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 |
|
---|
34 | [Storable]
|
---|
35 | private List<IConstraint> myConstraints;
|
---|
36 | /// <summary>
|
---|
37 | /// Gets all current constraints.
|
---|
38 | /// <note type="caution"> The constraints are returned read-only.</note>
|
---|
39 | /// </summary>
|
---|
40 | public virtual ICollection<IConstraint> Constraints {
|
---|
41 | get { return myConstraints.AsReadOnly(); }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Initializes a new instance of <see cref="ConstrainedItemBase"/>.
|
---|
46 | /// </summary>
|
---|
47 | protected ConstrainedItemBase() {
|
---|
48 | myConstraints = new List<IConstraint>();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Clones the current instance (deep clone).
|
---|
53 | /// </summary>
|
---|
54 | /// <remarks>Calls <see cref="StorableBase.Clone
|
---|
55 | /// (System.Collections.Generic.IDictionary<System.Guid, object>)"/>
|
---|
56 | /// of base class <see cref="ItemBase"/>.<br/>
|
---|
57 | /// Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
58 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
59 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
60 | /// <returns>The cloned object as <see cref="ConstrainedItemBase"/>.</returns>
|
---|
61 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
62 | ConstrainedItemBase clone = (ConstrainedItemBase)base.Clone(clonedObjects);
|
---|
63 | clone.myConstraints.Clear();
|
---|
64 | foreach (IConstraint constraint in Constraints)
|
---|
65 | clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
|
---|
66 | return clone;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Adds the given <paramref name="constraint"/> to the current list.
|
---|
71 | /// </summary>
|
---|
72 | /// <remarks>Calls <see cref="OnConstraintAdded"/>.</remarks>
|
---|
73 | /// <param name="constraint">The constraint to add.</param>
|
---|
74 | public virtual void AddConstraint(IConstraint constraint) {
|
---|
75 | myConstraints.Add(constraint);
|
---|
76 | OnConstraintAdded(constraint);
|
---|
77 | }
|
---|
78 | /// <summary>
|
---|
79 | /// Removes the given <paramref name="constraint"/> from the current list.
|
---|
80 | /// </summary>
|
---|
81 | /// <remarks>Calls <see cref="OnConstraintRemoved"/> if the constraint can be successfully removed.</remarks>
|
---|
82 | /// <param name="constraint">The constraint to remove.</param>
|
---|
83 | public virtual void RemoveConstraint(IConstraint constraint) {
|
---|
84 | if (myConstraints.Remove(constraint))
|
---|
85 | OnConstraintRemoved(constraint);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /// <summary>
|
---|
89 | /// Checks all constraints of the current instance.
|
---|
90 | /// </summary>
|
---|
91 | /// <returns><c>true</c> if all constraints could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
92 | public bool IsValid() {
|
---|
93 | bool result = true;
|
---|
94 | foreach (IConstraint constraint in Constraints)
|
---|
95 | result = result && constraint.Check(this);
|
---|
96 | return result;
|
---|
97 | }
|
---|
98 | /// <summary>
|
---|
99 | /// Checks all constraints of the current instance.
|
---|
100 | /// </summary>
|
---|
101 | /// <param name="violatedConstraints">Output parameter; contains all constraints that could not be
|
---|
102 | /// fulfilled.</param>
|
---|
103 | /// <returns><c>true</c> if all constraints could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
104 | public bool IsValid(out ICollection<IConstraint> violatedConstraints) {
|
---|
105 | bool result = true;
|
---|
106 | violatedConstraints = new List<IConstraint>();
|
---|
107 | foreach (IConstraint constraint in Constraints) {
|
---|
108 | if (!constraint.Check(this)) {
|
---|
109 | result = false;
|
---|
110 | violatedConstraints.Add(constraint);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | return result;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /// <summary>
|
---|
117 | /// Creates an instance of <see cref="ConstrainedItemBaseView"/>
|
---|
118 | /// to represent the current instance visually.
|
---|
119 | /// </summary>
|
---|
120 | /// <returns>The created view as <see cref="ConstrainedItemBaseView"/>.</returns>
|
---|
121 | public override IView CreateView() {
|
---|
122 | return new ConstrainedItemBaseView(this);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /// <summary>
|
---|
126 | /// Occurs when a constraint is added.
|
---|
127 | /// </summary>
|
---|
128 | public event EventHandler<ConstraintEventArgs> ConstraintAdded;
|
---|
129 | /// <summary>
|
---|
130 | /// Fires a new <c>ConstraintAdded</c> event.
|
---|
131 | /// </summary>
|
---|
132 | /// <param name="constraint">The constraint that was added.</param>
|
---|
133 | protected virtual void OnConstraintAdded(IConstraint constraint) {
|
---|
134 | if (ConstraintAdded != null)
|
---|
135 | ConstraintAdded(this, new ConstraintEventArgs(constraint));
|
---|
136 | }
|
---|
137 | /// <summary>
|
---|
138 | /// Occurs when a constraint is removed.
|
---|
139 | /// </summary>
|
---|
140 | public event EventHandler<ConstraintEventArgs> ConstraintRemoved;
|
---|
141 | /// <summary>
|
---|
142 | /// Fires a new <c>ConstraintRemoved</c> event.
|
---|
143 | /// </summary>
|
---|
144 | /// <param name="constraint">The constraint that was removed.</param>
|
---|
145 | protected virtual void OnConstraintRemoved(IConstraint constraint) {
|
---|
146 | if (ConstraintRemoved != null)
|
---|
147 | ConstraintRemoved(this, new ConstraintEventArgs(constraint));
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|