#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; 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 { private IntData subOperatorIndex; /// /// Gets the index of the sub-operator. /// public IntData SubOperatorIndex { get { return subOperatorIndex; } } 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); } #region persistence /// /// Saves the current instance as in the specified . /// /// The index and the list of sub-operators are saved as child nodes with tag names /// SubOperatorIndex and AllowedSubOperators. /// 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 indexNode = PersistenceManager.Persist("SubOperatorIndex", subOperatorIndex, document, persistedObjects); node.AppendChild(indexNode); XmlNode listNode = document.CreateNode(XmlNodeType.Element, "AllowedSubOperators", document.NamespaceURI); foreach(IOperator op in subOperators) { XmlNode opNode = PersistenceManager.Persist(op, document, persistedObjects); listNode.AppendChild(opNode); } node.AppendChild(listNode); 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); subOperatorIndex = (IntData)PersistenceManager.Restore(node.SelectSingleNode("SubOperatorIndex"), restoredObjects); subOperators = new List(); foreach(XmlNode childNode in node.SelectSingleNode("AllowedSubOperators").ChildNodes) { subOperators.Add((IOperator)PersistenceManager.Restore(childNode, restoredObjects)); } } #endregion persistence } }