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