[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using System.Diagnostics;
|
---|
| 28 | using System.Xml;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Constraints {
|
---|
| 31 | public class SubOperatorTypeConstraint : ConstraintBase {
|
---|
| 32 | private IntData subOperatorIndex;
|
---|
| 33 | public IntData SubOperatorIndex {
|
---|
| 34 | get { return subOperatorIndex; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | private List<IOperator> subOperators;
|
---|
| 38 | public IList<IOperator> AllowedSubOperators {
|
---|
| 39 | get {
|
---|
| 40 | return subOperators.AsReadOnly();
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public override string Description {
|
---|
| 45 | get { return "The sub-operator at a specific index has to be an element of a pre-defined group."; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public SubOperatorTypeConstraint()
|
---|
| 49 | : base() {
|
---|
| 50 | subOperatorIndex = new IntData(0);
|
---|
| 51 | subOperators = new List<IOperator>();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[435] | 54 | public SubOperatorTypeConstraint(int index) : base() {
|
---|
| 55 | subOperatorIndex = new IntData(index);
|
---|
| 56 | subOperators = new List<IOperator>();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[2] | 59 | public void AddOperator(IOperator op) {
|
---|
[435] | 60 | if(!subOperators.Contains(op)) {
|
---|
| 61 | subOperators.Add(op);
|
---|
| 62 | FireChanged();
|
---|
[2] | 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public void RemoveOperator(IOperator op) {
|
---|
[435] | 67 | if(subOperators.Contains(op)) {
|
---|
| 68 | subOperators.Remove(op);
|
---|
| 69 | FireChanged();
|
---|
[2] | 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[467] | 73 | public void Clear() {
|
---|
| 74 | subOperators.Clear();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[2] | 77 | public override bool Check(IItem data) {
|
---|
| 78 | IOperator op = data as IOperator;
|
---|
[430] | 79 | if(data == null) return false;
|
---|
[2] | 80 |
|
---|
| 81 | if(op.SubOperators.Count <= subOperatorIndex.Data) {
|
---|
| 82 | return false;
|
---|
| 83 | }
|
---|
[435] | 84 | return subOperators.Contains(op.SubOperators[subOperatorIndex.Data]);
|
---|
[2] | 85 | }
|
---|
| 86 |
|
---|
| 87 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 88 | SubOperatorTypeConstraint clone = new SubOperatorTypeConstraint();
|
---|
| 89 | clonedObjects.Add(Guid, clone);
|
---|
| 90 | clone.subOperatorIndex.Data = subOperatorIndex.Data;
|
---|
| 91 | foreach(IOperator op in subOperators) {
|
---|
| 92 | clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
|
---|
| 93 | }
|
---|
| 94 | return clone;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public override IView CreateView() {
|
---|
| 98 | return new SubOperatorsTypeConstraintView(this);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public override void Accept(IConstraintVisitor visitor) {
|
---|
| 102 | visitor.Visit(this);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | #region persistence
|
---|
[430] | 107 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
[2] | 108 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 109 | XmlNode indexNode = PersistenceManager.Persist("SubOperatorIndex", subOperatorIndex, document, persistedObjects);
|
---|
| 110 | node.AppendChild(indexNode);
|
---|
| 111 | XmlNode listNode = document.CreateNode(XmlNodeType.Element, "AllowedSubOperators", document.NamespaceURI);
|
---|
| 112 | foreach(IOperator op in subOperators) {
|
---|
| 113 | XmlNode opNode = PersistenceManager.Persist(op, document, persistedObjects);
|
---|
| 114 | listNode.AppendChild(opNode);
|
---|
| 115 | }
|
---|
| 116 | node.AppendChild(listNode);
|
---|
| 117 | return node;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[430] | 120 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
[2] | 121 | base.Populate(node, restoredObjects);
|
---|
| 122 | subOperatorIndex = (IntData)PersistenceManager.Restore(node.SelectSingleNode("SubOperatorIndex"), restoredObjects);
|
---|
| 123 | subOperators = new List<IOperator>();
|
---|
| 124 | foreach(XmlNode childNode in node.SelectSingleNode("AllowedSubOperators").ChildNodes) {
|
---|
| 125 | subOperators.Add((IOperator)PersistenceManager.Restore(childNode, restoredObjects));
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | #endregion persistence
|
---|
| 129 | }
|
---|
| 130 | }
|
---|