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 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Constraints {
|
---|
32 | /// <summary>
|
---|
33 | /// Constraint where all sub-operators have to be elements of a pre-defined group.
|
---|
34 | /// </summary>
|
---|
35 | public class AllSubOperatorsTypeConstraint : ConstraintBase {
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | private SubOperatorTypeConstraint groupConstraint;
|
---|
39 | /// <summary>
|
---|
40 | /// Gets all allowed sub-operators.
|
---|
41 | /// </summary>
|
---|
42 | public IList<IOperator> AllowedSubOperators {
|
---|
43 | get {
|
---|
44 | return groupConstraint.AllowedSubOperators;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <inheritdoc select="summary"/>
|
---|
49 | public override string Description {
|
---|
50 | get { return "All sub-operators have to be elements of a pre-defined group."; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Initializes a new instance of <see cref="AllSubOperatorsTypeConstraint"/>.
|
---|
55 | /// </summary>
|
---|
56 | public AllSubOperatorsTypeConstraint()
|
---|
57 | : base() {
|
---|
58 | groupConstraint = new SubOperatorTypeConstraint();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Adds the given operator to the constraint.
|
---|
63 | /// </summary>
|
---|
64 | /// <remarks>Calls <see cref="ItemBase.FireChanged"/> of base class
|
---|
65 | /// <see cref="ConstraintBase"/>.</remarks>
|
---|
66 | /// <param name="op">The operator to add.</param>
|
---|
67 | public void AddOperator(IOperator op) {
|
---|
68 | groupConstraint.AddOperator(op);
|
---|
69 | FireChanged();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /// <summary>
|
---|
73 | /// Removes the given operator from the contraint.
|
---|
74 | /// </summary>
|
---|
75 | /// <remarks>Calls <see cref="ItemBase.FireChanged"/> of base class
|
---|
76 | /// <see cref="ConstraintBase"/>.</remarks>
|
---|
77 | /// <param name="op">The operator to remove.</param>
|
---|
78 | public void RemoveOperator(IOperator op) {
|
---|
79 | groupConstraint.RemoveOperator(op);
|
---|
80 | FireChanged();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
84 | /// Checks whether the given element fulfills the current constraint.
|
---|
85 | /// </summary>
|
---|
86 | /// <param name="data">The item to check.</param>
|
---|
87 | /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
88 | public override bool Check(IItem data) {
|
---|
89 | IOperator op = data as IOperator;
|
---|
90 | if (data == null) return false;
|
---|
91 |
|
---|
92 | for (int i = 0; i < op.SubOperators.Count; i++) {
|
---|
93 | groupConstraint.SubOperatorIndex.Data = i;
|
---|
94 | if (groupConstraint.Check(data) == false) {
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /// <summary>
|
---|
102 | /// Empties the current instance.
|
---|
103 | /// </summary>
|
---|
104 | public void Clear() {
|
---|
105 | groupConstraint.Clear();
|
---|
106 | }
|
---|
107 |
|
---|
108 | /// <summary>
|
---|
109 | /// Clones the current instance (deep clone).
|
---|
110 | /// </summary>
|
---|
111 | /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
112 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
113 | /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
|
---|
114 | /// <returns>The cloned object as <see cref="AllSubOperatorsTypeConstraint"/>.</returns>
|
---|
115 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
116 | AllSubOperatorsTypeConstraint clone = new AllSubOperatorsTypeConstraint();
|
---|
117 | clonedObjects.Add(Guid, clone);
|
---|
118 | clone.groupConstraint = (SubOperatorTypeConstraint)Auxiliary.Clone(groupConstraint, clonedObjects);
|
---|
119 | return clone;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /// <summary>
|
---|
123 | /// Creates a new instance of <see cref="AllSubOperatorsTypeConstraintView"/> to represent the current
|
---|
124 | /// instance visually.
|
---|
125 | /// </summary>
|
---|
126 | /// <returns>The created view as <see cref="AllSubOperatorsTypeConstraintView"/>.</returns>
|
---|
127 | public override IView CreateView() {
|
---|
128 | return new AllSubOperatorsTypeConstraintView(groupConstraint);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|