[2] | 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.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Constraints {
|
---|
[1176] | 30 | /// <summary>
|
---|
| 31 | /// Constraint where its sub-constraint must be false to be true.
|
---|
| 32 | /// </summary>
|
---|
[2] | 33 | public class NotConstraint : ConstraintBase {
|
---|
| 34 | private ConstraintBase subConstraint;
|
---|
[1176] | 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>
|
---|
[2] | 40 | public ConstraintBase SubConstraint {
|
---|
| 41 | get { return subConstraint; }
|
---|
| 42 | set {
|
---|
| 43 | subConstraint = value;
|
---|
| 44 | OnChanged();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[1176] | 47 | /// <inheritdoc select="summary"/>
|
---|
[2] | 48 | public override string Description {
|
---|
| 49 | get {
|
---|
| 50 | return "Requires that a constraint must be false to be true";
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[1176] | 53 | /// <summary>
|
---|
| 54 | /// Initializes a new instance of <see cref="NotConstraint"/>.
|
---|
| 55 | /// </summary>
|
---|
[2] | 56 | public NotConstraint()
|
---|
| 57 | : base() {
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[1176] | 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>
|
---|
[2] | 65 | public override bool Check(IItem data) {
|
---|
[893] | 66 | return (subConstraint == null) || (!subConstraint.Check(data));
|
---|
[2] | 67 | }
|
---|
| 68 |
|
---|
[1176] | 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>
|
---|
[2] | 74 | public override IView CreateView() {
|
---|
| 75 | return new NotConstraintView(this);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[1176] | 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>
|
---|
[2] | 83 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 84 | NotConstraint clone = new NotConstraint();
|
---|
| 85 | clonedObjects.Add(Guid, clone);
|
---|
[893] | 86 | if (subConstraint != null)
|
---|
| 87 | clone.SubConstraint = (ConstraintBase)SubConstraint.Clone();
|
---|
[2] | 88 | return clone;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | #region persistence
|
---|
[1176] | 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>
|
---|
[2] | 102 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 103 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
[893] | 104 | if (subConstraint != null) {
|
---|
| 105 | XmlNode sub = PersistenceManager.Persist("SubConstraint", SubConstraint, document, persistedObjects);
|
---|
| 106 | node.AppendChild(sub);
|
---|
| 107 | }
|
---|
[2] | 108 | return node;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[1176] | 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>
|
---|
[2] | 119 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 120 | base.Populate(node, restoredObjects);
|
---|
[893] | 121 | XmlNode subNode = node.SelectSingleNode("SubConstraint");
|
---|
| 122 | if(subNode!=null)
|
---|
| 123 | subConstraint = (ConstraintBase)PersistenceManager.Restore(subNode, restoredObjects);
|
---|
[2] | 124 | }
|
---|
| 125 | #endregion
|
---|
| 126 | }
|
---|
| 127 | }
|
---|