Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/NumberOfSubOperatorsConstraint.cs @ 810

Last change on this file since 810 was 764, checked in by gkronber, 16 years ago

removed visitor classes and methods in HeuristicLab.Constraints and replaced visitor with manual dispatch. #343 (Rethink about usefulness of visitors for ObjectData and Constraints)

File size: 3.3 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 HeuristicLab.Core;
26using System.Diagnostics;
27using HeuristicLab.Data;
28using System.Xml;
29
30namespace HeuristicLab.Constraints {
31  public class NumberOfSubOperatorsConstraint : ConstraintBase {
32    private IntData minOperators;
33    private IntData maxOperators;
34
35    public IntData MaxOperators {
36      get { return maxOperators; }
37    }
38
39    public IntData MinOperators {
40      get { return minOperators; }
41    }
42
43    public override string Description {
44      get { return "Number of sub-operators has to be between " + MinOperators.ToString() + " and " + MaxOperators.ToString() + "."; }
45    }
46
47    public NumberOfSubOperatorsConstraint()
48      : this(0,0) {
49    }
50
51    public NumberOfSubOperatorsConstraint(int min, int max) : base() {
52      minOperators = new IntData(min);
53      maxOperators = new IntData(max);
54    }
55
56    public override bool Check(IItem data) {
57      IOperator op = data as IOperator;
58      if (data == null) return false;
59
60      return (op.SubOperators.Count >= minOperators.Data && op.SubOperators.Count <= maxOperators.Data);
61    }
62
63    public override object Clone(IDictionary<Guid, object> clonedObjects) {
64      NumberOfSubOperatorsConstraint clone = new NumberOfSubOperatorsConstraint();
65      clonedObjects.Add(Guid, clone);
66      clone.maxOperators.Data = maxOperators.Data;
67      clone.minOperators.Data = minOperators.Data;
68      return clone;
69    }
70
71    public override IView CreateView() {
72      return new NumberOfSubOperatorsConstraintView(this);
73    }
74
75    #region persistence
76    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
77      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
78      XmlNode minNode = PersistenceManager.Persist("min", minOperators, document, persistedObjects);
79      XmlNode maxNode = PersistenceManager.Persist("max", maxOperators, document, persistedObjects);
80      node.AppendChild(minNode);
81      node.AppendChild(maxNode);
82      return node;
83    }
84
85    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
86      base.Populate(node, restoredObjects);
87      minOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("min"), restoredObjects);
88      maxOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("max"), restoredObjects);
89    }
90    #endregion persistence
91
92  }
93}
Note: See TracBrowser for help on using the repository browser.