Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Communication.Data/Protocol.cs @ 1074

Last change on this file since 1074 was 591, checked in by abeham, 16 years ago

Put the GPL license in the files from the communication framework and simulation optimization project

File size: 5.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;
26using System.Xml.XPath;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Communication.Data {
31  public class Protocol : ItemBase, IEditable {
32    private StringData name;
33    public StringData Name {
34      get { return name; }
35      set {
36        name = value;
37        OnChanged();
38      }
39    }
40    private ItemList<ProtocolState> states;
41    public ItemList<ProtocolState> States {
42      get { return states; }
43      set {
44        states.ItemAdded -= new EventHandler<ItemIndexEventArgs>(States_ItemAdded);
45        states.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(States_ItemRemoved);
46        states = value;
47        states.ItemAdded += new EventHandler<ItemIndexEventArgs>(States_ItemAdded);
48        states.ItemRemoved += new EventHandler<ItemIndexEventArgs>(States_ItemRemoved);
49        // if the newly assigned states don't contain the currently selected initial state
50        if (!states.Contains(initialState))
51          if (states.Count > 0)
52            initialState = states[0];
53          else initialState = null;
54        OnChanged();
55      }
56    }
57    private ProtocolState initialState;
58    public ProtocolState InitialState {
59      get { return initialState; }
60      set {
61        initialState = value;
62        OnChanged();
63      }
64    }
65
66    public Protocol() {
67      name = new StringData("Unnamed protocol");
68      states = new ItemList<ProtocolState>();
69      states.ItemAdded += new EventHandler<ItemIndexEventArgs>(States_ItemAdded);
70      states.ItemRemoved += new EventHandler<ItemIndexEventArgs>(States_ItemRemoved);
71      ProtocolState firstState = new ProtocolState();
72      firstState.Name = new StringData("InitialState");
73      firstState.AcceptingState = new BoolData(true);
74      firstState.Protocol = this;
75      states.Add(firstState);
76      initialState = firstState;
77    }
78
79    public void Dispose() {
80      states.ItemAdded -= new EventHandler<ItemIndexEventArgs>(States_ItemAdded);
81      states.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(States_ItemRemoved);
82    }
83
84    public override IView CreateView() {
85      return new ProtocolEditor(this);
86    }
87    public virtual IEditor CreateEditor() {
88      return new ProtocolEditor(this);
89    }
90
91    public override object Clone(IDictionary<Guid, object> clonedObjects) {
92      Protocol clone = new Protocol();
93      clonedObjects.Add(Guid, clone);
94      clone.Name = (StringData)Auxiliary.Clone(Name, clonedObjects);
95      clone.States = (ItemList<ProtocolState>)Auxiliary.Clone(States, clonedObjects);
96      // iterate through the states and select the appropriate state in the clone
97      for (int i = 0 ; i < states.Count ; i++)
98        if (states[i].Equals(initialState))
99          clone.InitialState = clone.States[i];
100      return clone;
101    }
102
103    #region States Events
104    void States_ItemRemoved(object sender, ItemIndexEventArgs e) {
105      e.Item.Changed -= new EventHandler(State_Changed);
106      State_Changed(this, new EventArgs());
107    }
108
109    void States_ItemAdded(object sender, ItemIndexEventArgs e) {
110      e.Item.Changed += new EventHandler(State_Changed);
111      ((ProtocolState)e.Item).Protocol = this;
112      State_Changed(this, new EventArgs());
113    }
114    #endregion
115
116    public EventHandler StatesChanged;
117    void State_Changed(object sender, EventArgs e) {
118      if (StatesChanged != null) StatesChanged(sender, e);
119    }
120
121    #region persistence
122    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
123      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
124      XmlNode nameNode = PersistenceManager.Persist("Name", Name, document, persistedObjects);
125      XmlNode statesNode = PersistenceManager.Persist("States", States, document, persistedObjects);
126      XmlNode initialStatesNode = PersistenceManager.Persist("InitialState", InitialState, document, persistedObjects);
127      node.AppendChild(nameNode);
128      node.AppendChild(statesNode);
129      node.AppendChild(initialStatesNode);
130      return node;
131    }
132
133    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
134      base.Populate(node, restoredObjects);
135      name = (StringData)PersistenceManager.Restore(node.SelectSingleNode("Name"), restoredObjects);
136      states = (ItemList<ProtocolState>)PersistenceManager.Restore(node.SelectSingleNode("States"), restoredObjects);
137      states.ItemAdded += new EventHandler<ItemIndexEventArgs>(States_ItemAdded);
138      states.ItemRemoved += new EventHandler<ItemIndexEventArgs>(States_ItemRemoved);
139      initialState = (ProtocolState)PersistenceManager.Restore(node.SelectSingleNode("InitialState"), restoredObjects);
140    }
141    #endregion persistence
142  }
143}
Note: See TracBrowser for help on using the repository browser.