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