#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using HeuristicLab.Core; using System.Diagnostics; using HeuristicLab.Data; using System.Xml; namespace HeuristicLab.Constraints { /// /// Constraint where the number of sub-operators must be within a specific range. /// public class NumberOfSubOperatorsConstraint : ConstraintBase { private IntData minOperators; private IntData maxOperators; /// /// Gets the maximum number of sub-operators. /// public IntData MaxOperators { get { return maxOperators; } } /// /// Gets the minimum number of sub-operators. /// public IntData MinOperators { get { return minOperators; } } /// public override string Description { get { return "Number of sub-operators has to be between " + MinOperators.ToString() + " and " + MaxOperators.ToString() + "."; } } /// /// Initializes a new instance of . /// public NumberOfSubOperatorsConstraint() : this(0,0) { } /// /// Initializes a new instance of with the minimum and /// the maximum number of sub-operators. /// /// The minimum number of sub-operators. /// The maximum number of sub-operators. public NumberOfSubOperatorsConstraint(int min, int max) : base() { minOperators = new IntData(min); maxOperators = new IntData(max); } /// /// Checks whether the given element fulfills the current constraint. /// /// The item to check. /// true if the constraint could be fulfilled, false otherwise. public override bool Check(IItem data) { IOperator op = data as IOperator; if (data == null) return false; return (op.SubOperators.Count >= minOperators.Data && op.SubOperators.Count <= maxOperators.Data); } /// /// Clones the current instance (deep clone). /// /// Dictionary of all already clone objects. (Needed to avoid cycles.) /// The cloned object as . public override object Clone(IDictionary clonedObjects) { NumberOfSubOperatorsConstraint clone = new NumberOfSubOperatorsConstraint(); clonedObjects.Add(Guid, clone); clone.maxOperators.Data = maxOperators.Data; clone.minOperators.Data = minOperators.Data; return clone; } /// /// Creates a new instance of to represent the current /// instance visually. /// /// The created view as . public override IView CreateView() { return new NumberOfSubOperatorsConstraintView(this); } #region persistence /// /// Saves the current instance as in the specified . /// /// The minimum and the maximum number of sub-operators are saved as child nodes with tag /// names min and max. /// The (tag)name of the . /// The where the data is saved. /// The dictionary of all already persisted objects. /// (Needed to avoid cycles.) /// The saved . public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) { XmlNode node = base.GetXmlNode(name, document, persistedObjects); XmlNode minNode = PersistenceManager.Persist("min", minOperators, document, persistedObjects); XmlNode maxNode = PersistenceManager.Persist("max", maxOperators, document, persistedObjects); node.AppendChild(minNode); node.AppendChild(maxNode); return node; } /// /// Loads the persisted constraint from the specified . /// /// The constraint must be saved in a specific way, see for /// more information. /// The where the instance is saved. /// The dictionary of all already restored objects. /// (Needed to avoid cycles.) public override void Populate(XmlNode node, IDictionary restoredObjects) { base.Populate(node, restoredObjects); minOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("min"), restoredObjects); maxOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("max"), restoredObjects); } #endregion persistence } }