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