Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/NotConstraint.cs @ 1378

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

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

File size: 5.1 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 where its sub-constraint must be false to be true.
32  /// </summary>
33  public class NotConstraint : ConstraintBase {
34    private ConstraintBase subConstraint;
35    /// <summary>
36    /// Gets or sets the sub-constraint.
37    /// </summary>
38    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class
39    /// <see cref="ConstraintBase"/> in the setter.</remarks>
40    public ConstraintBase SubConstraint {
41      get { return subConstraint; }
42      set {
43        subConstraint = value;
44        OnChanged();
45      }
46    }
47    /// <inheritdoc select="summary"/>
48    public override string Description {
49      get {
50        return "Requires that a constraint must be false to be true";
51      }
52    }
53    /// <summary>
54    /// Initializes a new instance of <see cref="NotConstraint"/>.
55    /// </summary>
56    public NotConstraint()
57      : base() {
58    }
59
60    /// <summary>
61    /// Checks whether the given element fulfills the current constraint.
62    /// </summary>
63    /// <param name="data">The item to check.</param>
64    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
65    public override bool Check(IItem data) {
66      return (subConstraint == null) || (!subConstraint.Check(data));
67    }
68
69    /// <summary>
70    /// Creates a new instance of <see cref="NotConstraintView"/> to represent the current
71    /// instance visually.
72    /// </summary>
73    /// <returns>The created view as <see cref="NotConstraintView"/>.</returns>
74    public override IView CreateView() {
75      return new NotConstraintView(this);
76    }
77
78    /// <summary>
79    /// Clones the current instance (deep clone).
80    /// </summary>
81    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
82    /// <returns>The cloned object as <see cref="NotConstraint"/>.</returns>
83    public override object Clone(IDictionary<Guid, object> clonedObjects) {
84      NotConstraint clone = new NotConstraint();
85      clonedObjects.Add(Guid, clone);
86      if (subConstraint != null)
87        clone.SubConstraint = (ConstraintBase)SubConstraint.Clone();
88      return clone;
89    }
90
91    #region persistence
92    /// <summary>
93    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
94    /// </summary>
95    /// <remarks>The sub-constraint is saved as a child node with tag name
96    /// <c>SubConstraint</c>.</remarks>
97    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
98    /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
99    /// <param name="persistedObjects">The dictionary of all already persisted objects.
100    /// (Needed to avoid cycles.)</param>
101    /// <returns>The saved <see cref="XmlNode"/>.</returns>
102    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
103      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
104      if (subConstraint != null) {
105        XmlNode sub = PersistenceManager.Persist("SubConstraint", SubConstraint, document, persistedObjects);
106        node.AppendChild(sub);
107      }
108      return node;
109    }
110
111    /// <summary>
112    /// Loads the persisted constraint from the specified <paramref name="node"/>.
113    /// </summary>
114    /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
115    /// more information.</remarks>
116    /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
117    /// <param name="restoredObjects">The dictionary of all already restored objects.
118    /// (Needed to avoid cycles.)</param>
119    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
120      base.Populate(node, restoredObjects);
121      XmlNode subNode =  node.SelectSingleNode("SubConstraint");
122      if(subNode!=null)
123        subConstraint = (ConstraintBase)PersistenceManager.Restore(subNode, restoredObjects);
124    }
125    #endregion
126  }
127}
Note: See TracBrowser for help on using the repository browser.