Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Constraints/3.2/OrConstraint.cs @ 4023

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

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

File size: 5.2 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;
28
29namespace HeuristicLab.Constraints {
30  /// <summary>
31  /// Constraint where at least one sub-constraint must be true.
32  /// </summary>
33  public class OrConstraint : ConstraintBase, IViewable {
34    private ItemList<IConstraint> clauses;
35    /// <summary>
36    /// Gets or sets the sub-constraints.
37    /// </summary>
38    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class
39    /// <see cref="ConstraintBase"/> in the setter.</remarks>
40    public ItemList<IConstraint> Clauses {
41      get { return clauses; }
42      set {
43        clauses = value;
44        OnChanged();
45      }
46    }
47
48    /// <inheritdoc select="summary"/>
49    public override string Description {
50      get {
51        return "To return true, at least one subconstraint has to be true";
52      }
53    }
54
55    /// <summary>
56    /// Initializes a new instance of <see cref="OrConstraint"/>.
57    /// </summary>
58    public OrConstraint() {
59      clauses = new ItemList<IConstraint>();
60    }
61
62    /// <summary>
63    /// Checks whether the given element fulfills the current constraint.
64    /// </summary>
65    /// <param name="data">The item to check.</param>
66    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
67    public override bool Check(IItem data) {
68      bool result = false;
69      for (int i = 0 ; i < clauses.Count ; i++) {
70        result = clauses[i].Check(data);
71        if (result) return true;
72      }
73      return result;
74    }
75
76    /// <summary>
77    /// Creates a new instance of <see cref="OrConstraintView"/> to represent the current
78    /// instance visually.
79    /// </summary>
80    /// <returns>The created view as <see cref="OrConstraintView"/>.</returns>
81    public override IView CreateView() {
82      return new OrConstraintView(this);
83    }
84
85    /// <summary>
86    /// Clones the current instance (deep clone).
87    /// </summary>
88    /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
89    /// <see cref="Auxiliary"/>.</remarks>
90    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
91    /// <returns>The cloned object as <see cref="OrConstraint"/>.</returns>
92    public override object Clone(IDictionary<Guid, object> clonedObjects) {
93      OrConstraint clone = new OrConstraint();
94      clonedObjects.Add(Guid, clone);
95      clone.Clauses = (ItemList<IConstraint>)Auxiliary.Clone(Clauses, clonedObjects);
96      return clone;
97    }
98
99    #region persistence
100    /// <summary>
101    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
102    /// </summary>
103    /// <remarks>The sub-constraints are saved as a child node with tag name
104    /// <c>Clauses</c>.</remarks>
105    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
106    /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
107    /// <param name="persistedObjects">The dictionary of all already persisted objects.
108    /// (Needed to avoid cycles.)</param>
109    /// <returns>The saved <see cref="XmlNode"/>.</returns>
110    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
111      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
112      XmlNode clausesNode = PersistenceManager.Persist("Clauses", Clauses, document, persistedObjects);
113      node.AppendChild(clausesNode);
114
115      return node;
116    }
117
118    /// <summary>
119    /// Loads the persisted constraint from the specified <paramref name="node"/>.
120    /// </summary>
121    /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
122    /// more information.</remarks>
123    /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
124    /// <param name="restoredObjects">The dictionary of all already restored objects.
125    /// (Needed to avoid cycles.)</param>
126    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
127      base.Populate(node, restoredObjects);
128      clauses = (ItemList<IConstraint>)PersistenceManager.Restore(node.SelectSingleNode("Clauses"), restoredObjects);
129    }
130    #endregion persistence
131  }
132}
Note: See TracBrowser for help on using the repository browser.