Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Constraints/3.3/OrConstraint.cs @ 2989

Last change on this file since 2989 was 1823, checked in by epitzer, 15 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 3.5 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 System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Constraints {
31  /// <summary>
32  /// Constraint where at least one sub-constraint must be true.
33  /// </summary>
34  public class OrConstraint : ConstraintBase, IViewable {
35
36    [Storable]
37    private ItemList<IConstraint> clauses;
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>
43    public ItemList<IConstraint> Clauses {
44      get { return clauses; }
45      set {
46        clauses = value;
47        OnChanged();
48      }
49    }
50
51    /// <inheritdoc select="summary"/>
52    public override string Description {
53      get {
54        return "To return true, at least one subconstraint has to be true";
55      }
56    }
57
58    /// <summary>
59    /// Initializes a new instance of <see cref="OrConstraint"/>.
60    /// </summary>
61    public OrConstraint() {
62      clauses = new ItemList<IConstraint>();
63    }
64
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>
70    public override bool Check(IItem data) {
71      bool result = false;
72      for (int i = 0; i < clauses.Count; i++) {
73        result = clauses[i].Check(data);
74        if (result) return true;
75      }
76      return result;
77    }
78
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>
84    public override IView CreateView() {
85      return new OrConstraintView(this);
86    }
87
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>
95    public override object Clone(IDictionary<Guid, object> clonedObjects) {
96      OrConstraint clone = new OrConstraint();
97      clonedObjects.Add(Guid, clone);
98      clone.Clauses = (ItemList<IConstraint>)Auxiliary.Clone(Clauses, clonedObjects);
99      return clone;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.