Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextReaderBranch/HeuristicLab.Constraints/SubOperatorsTypeConstraint.cs @ 123

Last change on this file since 123 was 123, checked in by gkronber, 16 years ago

fixed more bugs (not thoroughly tested but at least it works for OSGP_NOx and OSGA_TSP)

File size: 5.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using System.Diagnostics;
28using System.Xml;
29
30namespace 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
54    public void AddOperator(IOperator op) {
55      // check if already in the list of allowed functions
56      foreach(IOperator existingOp in subOperators) {
57        if (IsOperatorTypeEqual(existingOp, op)) {
58          return;
59        }
60      }
61
62      subOperators.Add(op);
63    }
64
65    public void RemoveOperator(IOperator op) {
66      IOperator matchingOperator = null;
67      foreach(IOperator existingOp in subOperators) {
68        if(IsOperatorTypeEqual(existingOp, op)) {
69          matchingOperator = existingOp;
70          break;
71        }
72      }
73
74      if (matchingOperator != null) {
75        subOperators.Remove(matchingOperator);
76      }
77    }
78
79    public override bool Check(IItem data) {
80      IOperator op = data as IOperator;
81      if (data == null) return false;
82
83      if(op.SubOperators.Count <= subOperatorIndex.Data) {
84        return false;
85      }
86     
87      return subOperators.Exists(delegate(IOperator curOp) { return IsOperatorTypeEqual(curOp, op.SubOperators[subOperatorIndex.Data]); });
88    }
89
90    public override object Clone(IDictionary<Guid, object> clonedObjects) {
91      SubOperatorTypeConstraint clone = new SubOperatorTypeConstraint();
92      clonedObjects.Add(Guid, clone);
93      clone.subOperatorIndex.Data = subOperatorIndex.Data;
94      foreach(IOperator op in subOperators) {
95        clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
96      }
97      return clone;
98    }
99
100    public override IView CreateView() {
101      return new SubOperatorsTypeConstraintView(this);
102    }
103
104    public override void Accept(IConstraintVisitor visitor) {
105      visitor.Visit(this);
106    }
107
108
109    #region persistence
110    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
111      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
112      XmlNode indexNode = PersistenceManager.Persist("SubOperatorIndex", subOperatorIndex, document, persistedObjects);
113      node.AppendChild(indexNode);
114      XmlNode listNode = document.CreateNode(XmlNodeType.Element, "AllowedSubOperators", document.NamespaceURI);
115      foreach(IOperator op in subOperators) {
116        XmlNode opNode = PersistenceManager.Persist(op, document, persistedObjects);
117        listNode.AppendChild(opNode);
118      }
119      node.AppendChild(listNode);
120      return node;
121    }
122
123    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
124    //  base.Populate(node, restoredObjects);
125    //  subOperatorIndex = (IntData)PersistenceManager.Restore(node.SelectSingleNode("SubOperatorIndex"), restoredObjects);
126    //  subOperators = new List<IOperator>();
127    //  foreach(XmlNode childNode in node.SelectSingleNode("AllowedSubOperators").ChildNodes) {
128    //    subOperators.Add((IOperator)PersistenceManager.Restore(childNode, restoredObjects));
129    //  }
130    //}
131    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
132      base.Populate(reader, restoredObjects);
133      reader.Read();
134      subOperatorIndex = (IntData)PersistenceManager.Restore(reader, "SubOperatorIndex", restoredObjects);
135      reader.Read();
136      subOperators = new List<IOperator>();
137      if(reader.Name !="AllowedSubOperators") throw new XmlException("Expected: \"AllowedSubOperators\", found: \""+reader.Name+"\"");
138      if(!reader.IsEmptyElement) {
139        reader.Read();
140        while(reader.IsStartElement()) {
141          subOperators.Add((IOperator)PersistenceManager.Restore(reader, restoredObjects));
142          reader.Skip();
143        }
144        reader.ReadEndElement();
145      } else {
146        reader.Read();
147      }
148    }
149    #endregion persistence
150
151
152    private bool IsOperatorTypeEqual(IOperator f, IOperator g) {
153      return ((StringData)f.GetVariable("TypeId").Value).Data == ((StringData)g.GetVariable("TypeId").Value).Data;
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.