#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 HeuristicLab.Data; using System.Diagnostics; using System.Xml; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Constraints { /// /// Constraint where the sub-operator at a specific index has to be an element of a pre-defined group. /// public class SubOperatorTypeConstraint : ConstraintBase { [Storable] private IntData subOperatorIndex; /// /// Gets the index of the sub-operator. /// public IntData SubOperatorIndex { get { return subOperatorIndex; } } [Storable] private List subOperators; /// /// Gets all allowed sub-operators. /// public IList AllowedSubOperators { get { return subOperators.AsReadOnly(); } } /// public override string Description { get { return "The sub-operator at a specific index has to be an element of a pre-defined group."; } } /// /// Initializes a new instance of . /// public SubOperatorTypeConstraint() : base() { subOperatorIndex = new IntData(0); subOperators = new List(); } /// /// Initializes a new instance of with the given /// . /// /// The index of the sub-operator. public SubOperatorTypeConstraint(int index) : base() { subOperatorIndex = new IntData(index); subOperators = new List(); } /// /// Adds the given operator to the list of sub-operators. /// /// Calls of base class . /// The operator to add. public void AddOperator(IOperator op) { if (!subOperators.Contains(op)) { subOperators.Add(op); FireChanged(); } } /// /// Removes the given operator from the list of sub-operators. /// /// Calls of base class . /// The operator to remove. public void RemoveOperator(IOperator op) { if (subOperators.Contains(op)) { subOperators.Remove(op); FireChanged(); } } /// /// Empties the list of sub-operators. /// public void Clear() { subOperators.Clear(); } /// /// 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; if (op.SubOperators.Count <= subOperatorIndex.Data) { return false; } return subOperators.Contains(op.SubOperators[subOperatorIndex.Data]); } /// /// Clones the current instance (deep clone). /// /// Deep clone through method of helper class /// . /// Dictionary of all already clone objects. (Needed to avoid cycles.) /// The cloned object as . public override object Clone(IDictionary clonedObjects) { SubOperatorTypeConstraint clone = new SubOperatorTypeConstraint(); clonedObjects.Add(Guid, clone); clone.subOperatorIndex.Data = subOperatorIndex.Data; foreach (IOperator op in subOperators) { clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects)); } return clone; } /// /// Creates a new instance of to represent the current /// instance visually. /// /// The created view as . public override IView CreateView() { return new SubOperatorsTypeConstraintView(this); } } }