[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 System.Xml;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[1823] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.Constraints {
|
---|
[1176] | 31 | /// <summary>
|
---|
| 32 | /// Constraint where at least one sub-constraint must be true.
|
---|
| 33 | /// </summary>
|
---|
[2] | 34 | public class OrConstraint : ConstraintBase, IViewable {
|
---|
[1672] | 35 |
|
---|
| 36 | [Storable]
|
---|
[40] | 37 | private ItemList<IConstraint> clauses;
|
---|
[1176] | 38 | /// <summary>
|
---|
| 39 | /// Gets or sets the sub-constraints.
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class
|
---|
| 42 | /// <see cref="ConstraintBase"/> in the setter.</remarks>
|
---|
[40] | 43 | public ItemList<IConstraint> Clauses {
|
---|
[2] | 44 | get { return clauses; }
|
---|
| 45 | set {
|
---|
| 46 | clauses = value;
|
---|
| 47 | OnChanged();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[1176] | 51 | /// <inheritdoc select="summary"/>
|
---|
[2] | 52 | public override string Description {
|
---|
| 53 | get {
|
---|
| 54 | return "To return true, at least one subconstraint has to be true";
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1176] | 58 | /// <summary>
|
---|
| 59 | /// Initializes a new instance of <see cref="OrConstraint"/>.
|
---|
| 60 | /// </summary>
|
---|
[2] | 61 | public OrConstraint() {
|
---|
[40] | 62 | clauses = new ItemList<IConstraint>();
|
---|
[2] | 63 | }
|
---|
| 64 |
|
---|
[1176] | 65 | /// <summary>
|
---|
| 66 | /// Checks whether the given element fulfills the current constraint.
|
---|
| 67 | /// </summary>
|
---|
| 68 | /// <param name="data">The item to check.</param>
|
---|
| 69 | /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
[2] | 70 | public override bool Check(IItem data) {
|
---|
| 71 | bool result = false;
|
---|
[1672] | 72 | for (int i = 0; i < clauses.Count; i++) {
|
---|
[40] | 73 | result = clauses[i].Check(data);
|
---|
[2] | 74 | if (result) return true;
|
---|
| 75 | }
|
---|
| 76 | return result;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[1176] | 79 | /// <summary>
|
---|
| 80 | /// Creates a new instance of <see cref="OrConstraintView"/> to represent the current
|
---|
| 81 | /// instance visually.
|
---|
| 82 | /// </summary>
|
---|
| 83 | /// <returns>The created view as <see cref="OrConstraintView"/>.</returns>
|
---|
[2] | 84 | public override IView CreateView() {
|
---|
| 85 | return new OrConstraintView(this);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1176] | 88 | /// <summary>
|
---|
| 89 | /// Clones the current instance (deep clone).
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
| 92 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
| 93 | /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
|
---|
| 94 | /// <returns>The cloned object as <see cref="OrConstraint"/>.</returns>
|
---|
[2] | 95 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 96 | OrConstraint clone = new OrConstraint();
|
---|
| 97 | clonedObjects.Add(Guid, clone);
|
---|
[40] | 98 | clone.Clauses = (ItemList<IConstraint>)Auxiliary.Clone(Clauses, clonedObjects);
|
---|
[2] | 99 | return clone;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|