Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Core/ConstrainedItemBase.cs @ 175

Last change on this file since 175 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

File size: 5.1 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 Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
100      base.Persist(name, writer, persistedObjects);
101      if(Constraints.Count > 0) {
102        writer.WriteStartElement("Constraints");
103        foreach(IConstraint constraint in Constraints)
104          PersistenceManager.Persist(constraint, writer, persistedObjects);
105        writer.WriteEndElement(); // </Constraints>
106      }
107    }
108    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
109    //  base.Populate(node, restoredObjects);
110    //  XmlNode constraintsNode = node.SelectSingleNode("Constraints");
111    //  if(constraintsNode != null) {
112    //    myConstraints.Clear();
113    //    foreach(XmlNode constraintNode in constraintsNode.ChildNodes)
114    //      AddConstraint((IConstraint)PersistenceManager.Restore(constraintNode, restoredObjects));
115    //  }
116    //}
117    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
118      base.Populate(reader, restoredObjects);
119      reader.Read();
120      if(reader.Name == "Constraints") {
121        myConstraints.Clear();
122        reader.Read();
123        while(reader.IsStartElement()) {
124          AddConstraint((IConstraint)PersistenceManager.Restore(reader, restoredObjects));
125          reader.Skip();
126        }
127        reader.ReadEndElement();
128      }
129    }
130    #endregion
131  }
132}
Note: See TracBrowser for help on using the repository browser.