[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 HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using System.Diagnostics;
|
---|
| 28 | using System.Xml;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Constraints {
|
---|
[1176] | 31 | /// <summary>
|
---|
| 32 | /// Constraint where the sub-operator at a specific index has to be an element of a pre-defined group.
|
---|
| 33 | /// </summary>
|
---|
[2] | 34 | public class SubOperatorTypeConstraint : ConstraintBase {
|
---|
| 35 | private IntData subOperatorIndex;
|
---|
[1176] | 36 | /// <summary>
|
---|
| 37 | /// Gets the index of the sub-operator.
|
---|
| 38 | /// </summary>
|
---|
[2] | 39 | public IntData SubOperatorIndex {
|
---|
| 40 | get { return subOperatorIndex; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | private List<IOperator> subOperators;
|
---|
[1176] | 44 | /// <summary>
|
---|
| 45 | /// Gets all allowed sub-operators.
|
---|
| 46 | /// </summary>
|
---|
[2] | 47 | public IList<IOperator> AllowedSubOperators {
|
---|
| 48 | get {
|
---|
| 49 | return subOperators.AsReadOnly();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[1176] | 53 | ///<inheritdoc select="summary"/>
|
---|
[2] | 54 | public override string Description {
|
---|
| 55 | get { return "The sub-operator at a specific index has to be an element of a pre-defined group."; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1176] | 58 | /// <summary>
|
---|
| 59 | /// Initializes a new instance of <see cref="SubOperatorTypeConstraint"/>.
|
---|
| 60 | /// </summary>
|
---|
[2] | 61 | public SubOperatorTypeConstraint()
|
---|
| 62 | : base() {
|
---|
| 63 | subOperatorIndex = new IntData(0);
|
---|
| 64 | subOperators = new List<IOperator>();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[1176] | 67 | /// <summary>
|
---|
| 68 | /// Initializes a new instance of <see cref="SubOperatorTypeConstraint"/> with the given
|
---|
| 69 | /// <paramref name="index"/>.
|
---|
| 70 | /// </summary>
|
---|
| 71 | /// <param name="index">The index of the sub-operator.</param>
|
---|
[435] | 72 | public SubOperatorTypeConstraint(int index) : base() {
|
---|
| 73 | subOperatorIndex = new IntData(index);
|
---|
| 74 | subOperators = new List<IOperator>();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1176] | 77 | /// <summary>
|
---|
| 78 | /// Adds the given operator to the list of sub-operators.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <remarks>Calls <see cref="ItemBase.FireChanged"/> of base class <see cref="ConstraintBase"/>.</remarks>
|
---|
| 81 | /// <param name="op">The operator to add.</param>
|
---|
[2] | 82 | public void AddOperator(IOperator op) {
|
---|
[435] | 83 | if(!subOperators.Contains(op)) {
|
---|
| 84 | subOperators.Add(op);
|
---|
| 85 | FireChanged();
|
---|
[2] | 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[1176] | 89 | /// <summary>
|
---|
| 90 | /// Removes the given operator from the list of sub-operators.
|
---|
| 91 | /// </summary>
|
---|
| 92 | /// <remarks>Calls <see cref="ItemBase.FireChanged"/> of base class <see cref="ConstraintBase"/>.</remarks>
|
---|
| 93 | /// <param name="op">The operator to remove.</param>
|
---|
[2] | 94 | public void RemoveOperator(IOperator op) {
|
---|
[435] | 95 | if(subOperators.Contains(op)) {
|
---|
| 96 | subOperators.Remove(op);
|
---|
| 97 | FireChanged();
|
---|
[2] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[1176] | 101 | /// <summary>
|
---|
| 102 | /// Empties the list of sub-operators.
|
---|
| 103 | /// </summary>
|
---|
[467] | 104 | public void Clear() {
|
---|
| 105 | subOperators.Clear();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[1176] | 108 | /// <summary>
|
---|
| 109 | /// Checks whether the given element fulfills the current constraint.
|
---|
| 110 | /// </summary>
|
---|
| 111 | /// <param name="data">The item to check.</param>
|
---|
| 112 | /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
[2] | 113 | public override bool Check(IItem data) {
|
---|
| 114 | IOperator op = data as IOperator;
|
---|
[430] | 115 | if(data == null) return false;
|
---|
[2] | 116 |
|
---|
| 117 | if(op.SubOperators.Count <= subOperatorIndex.Data) {
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
[435] | 120 | return subOperators.Contains(op.SubOperators[subOperatorIndex.Data]);
|
---|
[2] | 121 | }
|
---|
| 122 |
|
---|
[1176] | 123 | /// <summary>
|
---|
| 124 | /// Clones the current instance (deep clone).
|
---|
| 125 | /// </summary>
|
---|
| 126 | /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
| 127 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
| 128 | /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
|
---|
| 129 | /// <returns>The cloned object as <see cref="SubOperatorTypeConstraint"/>.</returns>
|
---|
[2] | 130 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 131 | SubOperatorTypeConstraint clone = new SubOperatorTypeConstraint();
|
---|
| 132 | clonedObjects.Add(Guid, clone);
|
---|
| 133 | clone.subOperatorIndex.Data = subOperatorIndex.Data;
|
---|
| 134 | foreach(IOperator op in subOperators) {
|
---|
| 135 | clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
|
---|
| 136 | }
|
---|
| 137 | return clone;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1176] | 140 | /// <summary>
|
---|
| 141 | /// Creates a new instance of <see cref="SubOperatorsTypeConstraintView"/> to represent the current
|
---|
| 142 | /// instance visually.
|
---|
| 143 | /// </summary>
|
---|
| 144 | /// <returns>The created view as <see cref="SubOperatorsTypeConstraintView"/>.</returns>
|
---|
[2] | 145 | public override IView CreateView() {
|
---|
| 146 | return new SubOperatorsTypeConstraintView(this);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | #region persistence
|
---|
[1176] | 150 | /// <summary>
|
---|
| 151 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
| 152 | /// </summary>
|
---|
| 153 | /// <remarks>The index and the list of sub-operators are saved as child nodes with tag names
|
---|
| 154 | /// <c>SubOperatorIndex</c> and <c>AllowedSubOperators</c>.</remarks>
|
---|
| 155 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
| 156 | /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
|
---|
| 157 | /// <param name="persistedObjects">The dictionary of all already persisted objects.
|
---|
| 158 | /// (Needed to avoid cycles.)</param>
|
---|
| 159 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
[430] | 160 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
[2] | 161 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 162 | XmlNode indexNode = PersistenceManager.Persist("SubOperatorIndex", subOperatorIndex, document, persistedObjects);
|
---|
| 163 | node.AppendChild(indexNode);
|
---|
| 164 | XmlNode listNode = document.CreateNode(XmlNodeType.Element, "AllowedSubOperators", document.NamespaceURI);
|
---|
| 165 | foreach(IOperator op in subOperators) {
|
---|
| 166 | XmlNode opNode = PersistenceManager.Persist(op, document, persistedObjects);
|
---|
| 167 | listNode.AppendChild(opNode);
|
---|
| 168 | }
|
---|
| 169 | node.AppendChild(listNode);
|
---|
| 170 | return node;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[1176] | 173 | /// <summary>
|
---|
| 174 | /// Loads the persisted constraint from the specified <paramref name="node"/>.
|
---|
| 175 | /// </summary>
|
---|
| 176 | /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
|
---|
| 177 | /// more information.</remarks>
|
---|
| 178 | /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
|
---|
| 179 | /// <param name="restoredObjects">The dictionary of all already restored objects.
|
---|
| 180 | /// (Needed to avoid cycles.)</param>
|
---|
[430] | 181 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
[2] | 182 | base.Populate(node, restoredObjects);
|
---|
| 183 | subOperatorIndex = (IntData)PersistenceManager.Restore(node.SelectSingleNode("SubOperatorIndex"), restoredObjects);
|
---|
| 184 | subOperators = new List<IOperator>();
|
---|
| 185 | foreach(XmlNode childNode in node.SelectSingleNode("AllowedSubOperators").ChildNodes) {
|
---|
| 186 | subOperators.Add((IOperator)PersistenceManager.Restore(childNode, restoredObjects));
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | #endregion persistence
|
---|
| 190 | }
|
---|
| 191 | }
|
---|