Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/ItemTypeConstraint.cs @ 1176

Last change on this file since 1176 was 1176, checked in by vdorfer, 15 years ago

Created API documentation for HeuristicLab.BitVector and HeuristicLab.Constraints namespace and changed a comment in HeuristicLab.IntVector namespace(#331)

File size: 5.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;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Constraints {
30  /// <summary>
31  /// Constraint that limits the type of a given item.
32  /// </summary>
33  /// <remarks>If the item is a <see cref="ConstrainedItemList"/>, any containing elements are limited to
34  /// the type and not the <see cref="ConstrainedItemList"/> itself.</remarks>
35  public class ItemTypeConstraint : ConstraintBase {
36    private Type type;
37    /// <summary>
38    /// Gets or sets the type to which the items should be limited.
39    /// </summary>
40    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
41    /// in the setter.</remarks>
42    public Type Type {
43      get { return type; }
44      set {
45        type = value;
46        OnChanged();
47      }
48    }
49
50    /// <inheritdoc select="summary"/>
51    public override string Description {
52      get {
53        return @"The ItemTypeConstraint limits the type of a given item.
54If the item is a ConstrainedItemList, any containing elements are limited to the type and not the ConstrainedItemList itself.";
55      }
56    }
57
58    /// <summary>
59    /// Initializes a new instance of <see cref="ItemTypeConstraint"/> with the <c>Type</c> property
60    /// set to <see cref="ItemBase"/> as default.
61    /// </summary>
62    public ItemTypeConstraint() {
63      type = typeof(ItemBase);
64    }
65
66    /// <summary>
67    /// Initializes a new instance of <see cref="ItemTypeConstraint"/> with the given <paramref name="type"/>.
68    /// </summary>
69    /// <param name="type">The type the items should be limited to.</param>
70    public ItemTypeConstraint(Type type) {
71      this.type = type;
72    }
73
74    /// <summary>
75    /// Checks whether the given element fulfills the current constraint.
76    /// </summary>
77    /// <param name="data">The item to check.</param>
78    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
79    public override bool Check(IItem data) {
80      ConstrainedItemList list = (data as ConstrainedItemList);
81      if (list != null) {
82        for (int i = 0; i < list.Count; i++)
83          if (!list[i].GetType().Equals(type)) return false;
84        return true;
85      }
86      return data.GetType().Equals(type);
87    }
88
89    /// <summary>
90    /// Creates a new instance of <see cref="ItemTypeConstraintView"/> to represent the current
91    /// instance visually.
92    /// </summary>
93    /// <returns>The created view as <see cref="ItemTypeConstraintView"/>.</returns>
94    public override IView CreateView() {
95      return new ItemTypeConstraintView(this);
96    }
97
98    #region clone & persistence
99    /// <summary>
100    /// Clones the current instance (deep clone).
101    /// </summary>
102    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
103    /// <returns>The cloned object as <see cref="ItemTypeConstraint"/>.</returns>
104    public override object Clone(IDictionary<Guid, object> clonedObjects) {
105      ItemTypeConstraint clone = new ItemTypeConstraint(type);
106      clonedObjects.Add(Guid, clone);
107      return clone;
108    }
109
110    /// <summary>
111    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
112    /// </summary>
113    /// <remarks>The type of the current instance is saved as attribute with tag name <c>ItemType</c>.</remarks>
114    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
115    /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
116    /// <param name="persistedObjects">The dictionary of all already persisted objects.
117    /// (Needed to avoid cycles.)</param>
118    /// <returns>The saved <see cref="XmlNode"/>.</returns>
119    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
120      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
121      XmlAttribute itemTypeAttribute = document.CreateAttribute("ItemType");
122      itemTypeAttribute.Value = PersistenceManager.BuildTypeString(Type);
123      node.Attributes.Append(itemTypeAttribute);
124      return node;
125    }
126
127    /// <summary>
128    /// Loads the persisted constraint from the specified <paramref name="node"/>.
129    /// </summary>
130    /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
131    /// more information.</remarks>
132    /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
133    /// <param name="restoredObjects">The dictionary of all already restored objects.
134    /// (Needed to avoid cycles.)</param>
135    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
136      base.Populate(node, restoredObjects);
137      XmlAttribute itemTypeAttribute = node.Attributes["ItemType"];
138      type = Type.GetType(itemTypeAttribute.Value);
139    }
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.