Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextReaderBranch/HeuristicLab.Core/ConstrainedItemBase.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: 4.7 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 System.Xml;
26
27namespace HeuristicLab.Core {
28  public abstract class ConstrainedItemBase : ItemBase, IConstrainedItem {
29    private List<IConstraint> myConstraints;
30    public virtual ICollection<IConstraint> Constraints {
31      get { return myConstraints.AsReadOnly(); }
32    }
33
34    protected ConstrainedItemBase() {
35      myConstraints = new List<IConstraint>();
36    }
37
38    public override object Clone(IDictionary<Guid, object> clonedObjects) {
39      ConstrainedItemBase clone = (ConstrainedItemBase)base.Clone(clonedObjects);
40      clone.myConstraints.Clear();
41      foreach(IConstraint constraint in Constraints)
42        clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
43      return clone;
44    }
45
46    public virtual void AddConstraint(IConstraint constraint) {
47      myConstraints.Add(constraint);
48      OnConstraintAdded(constraint);
49    }
50    public virtual void RemoveConstraint(IConstraint constraint) {
51      if(myConstraints.Remove(constraint))
52        OnConstraintRemoved(constraint);
53    }
54
55    public bool IsValid() {
56      bool result = true;
57      foreach(IConstraint constraint in Constraints)
58        result = result && constraint.Check(this);
59      return result;
60    }
61    public bool IsValid(out ICollection<IConstraint> violatedConstraints) {
62      bool result = true;
63      violatedConstraints = new List<IConstraint>();
64      foreach(IConstraint constraint in Constraints) {
65        if(!constraint.Check(this)) {
66          result = false;
67          violatedConstraints.Add(constraint);
68        }
69      }
70      return result;
71    }
72
73    public override IView CreateView() {
74      return new ConstrainedItemBaseView(this);
75    }
76
77    public event EventHandler<ConstraintEventArgs> ConstraintAdded;
78    protected virtual void OnConstraintAdded(IConstraint constraint) {
79      if(ConstraintAdded != null)
80        ConstraintAdded(this, new ConstraintEventArgs(constraint));
81    }
82    public event EventHandler<ConstraintEventArgs> ConstraintRemoved;
83    protected virtual void OnConstraintRemoved(IConstraint constraint) {
84      if(ConstraintRemoved != null)
85        ConstraintRemoved(this, new ConstraintEventArgs(constraint));
86    }
87
88    #region Persistence Methods
89    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
90      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
91      if(Constraints.Count > 0) {
92        XmlNode constraintsNode = document.CreateNode(XmlNodeType.Element, "Constraints", null);
93        foreach(IConstraint constraint in Constraints)
94          constraintsNode.AppendChild(PersistenceManager.Persist(constraint, document, persistedObjects));
95        node.AppendChild(constraintsNode);
96      }
97      return node;
98    }
99    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
100    //  base.Populate(node, restoredObjects);
101    //  XmlNode constraintsNode = node.SelectSingleNode("Constraints");
102    //  if(constraintsNode != null) {
103    //    myConstraints.Clear();
104    //    foreach(XmlNode constraintNode in constraintsNode.ChildNodes)
105    //      AddConstraint((IConstraint)PersistenceManager.Restore(constraintNode, restoredObjects));
106    //  }
107    //}
108    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
109      base.Populate(reader, restoredObjects);
110      reader.Read();
111      if(reader.Name == "Constraints") {
112        myConstraints.Clear();
113        reader.Read();
114        while(reader.IsStartElement()) {
115          AddConstraint((IConstraint)PersistenceManager.Restore(reader, restoredObjects));
116          reader.Skip();
117        }
118        reader.ReadEndElement();
119      }
120    }
121    #endregion
122  }
123}
Note: See TracBrowser for help on using the repository browser.