Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/OperatorGraph.cs @ 618

Last change on this file since 618 was 47, checked in by swagner, 17 years ago

Fixed ticket #49

  • removed operator reset mechanism
File size: 5.2 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 class OperatorGraph : ItemBase, IOperatorGraph {
29    private IDictionary<Guid, IOperator> myOperators;
30    public ICollection<IOperator> Operators {
31      get { return myOperators.Values; }
32    }
33    private IOperator myInitialOperator;
34    public IOperator InitialOperator {
35      get { return myInitialOperator; }
36      set {
37        if (myInitialOperator != value) {
38          myInitialOperator = value;
39          OnInitialOperatorChanged();
40        }
41      }
42    }
43
44    public OperatorGraph() {
45      myOperators = new Dictionary<Guid, IOperator>();
46    }
47
48    public override IView CreateView() {
49      return new OperatorGraphView(this);
50    }
51
52    public override object Clone(IDictionary<Guid, object> clonedObjects) {
53      OperatorGraph clone = new OperatorGraph();
54      clonedObjects.Add(Guid, clone);
55      foreach (IOperator op in Operators)
56        clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
57      if (InitialOperator != null)
58        clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
59      return clone;
60    }
61
62    public void AddOperator(IOperator op) {
63      if (!myOperators.ContainsKey(op.Guid)) {
64        myOperators.Add(op.Guid, op);
65        OnOperatorAdded(op);
66
67        foreach (IOperator subOperator in op.SubOperators)
68          AddOperator(subOperator);
69      }
70    }
71    public void RemoveOperator(Guid guid) {
72      IOperator op = GetOperator(guid);
73      if (op != null) {
74        foreach (IOperator o in Operators) {
75          int i = 0;
76          while (i < o.SubOperators.Count) {
77            if (o.SubOperators[i] == op)
78              o.RemoveSubOperator(i);
79            else
80              i++;
81          }
82        }
83        if (InitialOperator == op)
84          InitialOperator = null;
85        myOperators.Remove(op.Guid);
86        OnOperatorRemoved(op);
87      }
88    }
89    public IOperator GetOperator(Guid guid) {
90      IOperator op;
91      if (myOperators.TryGetValue(guid, out op))
92        return op;
93      else
94        return null;
95    }
96    public void Clear() {
97      Guid[] guids = new Guid[Operators.Count];
98      int i = 0;
99      foreach (IOperator op in Operators) {
100        guids[i] = op.Guid;
101        i++;
102      }
103      for (int j = 0; j < guids.Length; j++)
104        RemoveOperator(guids[j]);
105    }
106
107    public event EventHandler<OperatorEventArgs> OperatorAdded;
108    protected virtual void OnOperatorAdded(IOperator op) {
109      if (OperatorAdded != null)
110        OperatorAdded(this, new OperatorEventArgs(op));
111    }
112    public event EventHandler<OperatorEventArgs> OperatorRemoved;
113    protected virtual void OnOperatorRemoved(IOperator op) {
114      if (OperatorRemoved != null)
115        OperatorRemoved(this, new OperatorEventArgs(op));
116    }
117    public event EventHandler InitialOperatorChanged;
118    protected virtual void OnInitialOperatorChanged() {
119      if (InitialOperatorChanged != null)
120        InitialOperatorChanged(this, new EventArgs());
121    }
122
123    #region Persistence Methods
124    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
125      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
126      XmlNode ops = document.CreateNode(XmlNodeType.Element, "Operators", null);
127      foreach (IOperator op in myOperators.Values)
128        ops.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
129      node.AppendChild(ops);
130      if (InitialOperator != null)
131        node.AppendChild(PersistenceManager.Persist("InitialOperator", InitialOperator, document, persistedObjects));
132      return node;
133    }
134    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
135      base.Populate(node, restoredObjects);
136
137      XmlNode ops = node.SelectSingleNode("Operators");
138      for (int i = 0; i < ops.ChildNodes.Count; i++) {
139        XmlNode opNode = ops.ChildNodes[i];
140        IOperator op = (IOperator)PersistenceManager.Restore(opNode, restoredObjects);
141        myOperators.Add(op.Guid, op);
142      }
143
144      XmlNode initial = node.SelectSingleNode("InitialOperator");
145      if (initial != null)
146        myInitialOperator = (IOperator)PersistenceManager.Restore(initial, restoredObjects);
147    }
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.