#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;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Constraints {
///
/// Constraint where the number of sub-operators must be within a specific range.
///
public class NumberOfSubOperatorsConstraint : ConstraintBase {
[Storable]
private IntData minOperators;
[Storable]
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);
}
}
}