Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Constraints/NumberOfSubOperatorsConstraint.cs @ 143

Last change on this file since 143 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

File size: 4.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 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    public override void Accept(IConstraintVisitor visitor) {
76      visitor.Visit(this);
77    }
78
79
80    #region persistence
81    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
82    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
83    //  XmlNode minNode = PersistenceManager.Persist("min", minOperators, document, persistedObjects);
84    //  XmlNode maxNode = PersistenceManager.Persist("max", maxOperators, document, persistedObjects);
85    //  node.AppendChild(minNode);
86    //  node.AppendChild(maxNode);
87    //  return node;
88    //}
89    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
90      base.Persist(name, writer, persistedObjects);
91      PersistenceManager.Persist("min", minOperators, writer, persistedObjects);
92      PersistenceManager.Persist("max", maxOperators, writer, persistedObjects);
93    }
94
95    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
96    //  base.Populate(node, restoredObjects);
97    //  minOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("min"), restoredObjects);
98    //  maxOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("max"), restoredObjects);
99    //}
100    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
101      base.Populate(reader, restoredObjects);
102      reader.Read();
103      minOperators = (IntData)PersistenceManager.Restore(reader, "min", restoredObjects);
104      reader.Read();
105      maxOperators = (IntData)PersistenceManager.Restore(reader, "max", restoredObjects);
106      reader.Read();
107    }
108    #endregion persistence
109
110  }
111}
Note: See TracBrowser for help on using the repository browser.