Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/3.2/AllSubOperatorsTypeConstraint.cs @ 1529

Last change on this file since 1529 was 1529, checked in by gkronber, 15 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 6.4 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 HeuristicLab.Core;
26using HeuristicLab.Data;
27using System.Xml;
28using System.Diagnostics;
29
30namespace HeuristicLab.Constraints {
31  /// <summary>
32  /// Constraint where all sub-operators have to be elements of a pre-defined group.
33  /// </summary>
34  public class AllSubOperatorsTypeConstraint : ConstraintBase {
35
36    private SubOperatorTypeConstraint groupConstraint;
37    /// <summary>
38    /// Gets all allowed sub-operators.
39    /// </summary>
40    public IList<IOperator> AllowedSubOperators {
41      get {
42        return groupConstraint.AllowedSubOperators;
43      }
44    }
45
46    /// <inheritdoc select="summary"/>
47    public override string Description {
48      get { return "All sub-operators have to be elements of a pre-defined group."; }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="AllSubOperatorsTypeConstraint"/>.
53    /// </summary>
54    public AllSubOperatorsTypeConstraint()
55      : base() {
56      groupConstraint = new SubOperatorTypeConstraint();
57    }
58
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>
65    public void AddOperator(IOperator op) {
66      groupConstraint.AddOperator(op);
67      FireChanged();
68    }
69
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>
76    public void RemoveOperator(IOperator op) {
77      groupConstraint.RemoveOperator(op);
78      FireChanged();
79    }
80
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>
86    public override bool Check(IItem data) {
87      IOperator op = data as IOperator;
88      if(data == null) return false;
89
90      for(int i = 0; i < op.SubOperators.Count; i++) {
91        groupConstraint.SubOperatorIndex.Data = i;
92        if(groupConstraint.Check(data) == false) {
93          return false;
94        }
95      }
96      return true;
97    }
98
99    /// <summary>
100    /// Empties the current instance.
101    /// </summary>
102    public void Clear() {
103      groupConstraint.Clear();
104    }
105
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>
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
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>
125    public override IView CreateView() {
126      return new AllSubOperatorsTypeConstraintView(groupConstraint);
127    }
128
129    #region persistence
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>
140    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
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
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>
156    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
157      base.Populate(node, restoredObjects);
158      groupConstraint = (SubOperatorTypeConstraint)PersistenceManager.Restore(node.SelectSingleNode("SubOperatorsGroupConstraint"), restoredObjects);
159    }
160    #endregion persistence
161  }
162}
Note: See TracBrowser for help on using the repository browser.