Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Data/Protocol.cs @ 1133

Last change on this file since 1133 was 704, checked in by abeham, 16 years ago

[TICKET #297] communication restructuring

File size: 4.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 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 string name;
33    public string Name {
34      get { return name; }
35      set {
36        name = value;
37        OnChanged();
38      }
39    }
40    private IList<ProtocolState> states;
41    public IList<ProtocolState> States {
42      get { return states; }
43      set {
44        states = value;
45        // if the newly assigned states list doesn't contain the currently selected initial state
46        if (!states.Contains(initialState))
47          if (states.Count > 0)
48            initialState = states[0];
49          else initialState = null;
50        OnChanged();
51      }
52    }
53    private ProtocolState initialState;
54    public ProtocolState InitialState {
55      get { return initialState; }
56      set {
57        initialState = value;
58        OnChanged();
59      }
60    }
61
62    public Protocol() {
63      name = "Unnamed protocol";
64      states = new List<ProtocolState>();
65
66      ProtocolState firstState = new ProtocolState();
67      firstState.Name = "InitialState";
68      states.Add(firstState);
69      initialState = firstState;
70    }
71
72    public override IView CreateView() {
73      return new ProtocolEditor(this);
74    }
75    public virtual IEditor CreateEditor() {
76      return new ProtocolEditor(this);
77    }
78
79    #region clone & persistence
80    public override object Clone(IDictionary<Guid, object> clonedObjects) {
81      Protocol clone = new Protocol();
82      clonedObjects.Add(Guid, clone);
83      clone.Name = (string)name.Clone();
84      clone.States = new List<ProtocolState>(states.Count);
85      foreach (ProtocolState state in states)
86        clone.States.Add((ProtocolState)state.Clone(clonedObjects));
87      // iterate through the states and select the appropriate state in the clone
88      int index = states.IndexOf(initialState);
89      if (index >= 0) clone.InitialState = clone.States[index];
90      return clone;
91    }
92
93    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
94      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
95     
96      XmlAttribute nameAttrib = document.CreateAttribute("Name");
97      nameAttrib.Value = this.name;
98      node.Attributes.Append(nameAttrib);
99
100      XmlNode statesNode = document.CreateNode(XmlNodeType.Element, "States", null);
101      foreach (ProtocolState state in states) {
102        XmlNode tmp = state.GetXmlNode("State", document, persistedObjects);
103        if (state.Equals(initialState)) {
104          XmlAttribute initialStateAttrib = document.CreateAttribute("InitialState");
105          initialStateAttrib.Value = "1";
106          tmp.Attributes.Append(initialStateAttrib);
107        }
108        statesNode.AppendChild(tmp);
109      }
110      node.AppendChild(statesNode);
111
112      return node;
113    }
114
115    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
116      base.Populate(node, restoredObjects);
117      name = node.Attributes.GetNamedItem("Name").Value;
118
119      XmlNode statesNode = node.SelectSingleNode("States");
120      states = new List<ProtocolState>(statesNode.ChildNodes.Count);
121      foreach (XmlNode childNode in statesNode.ChildNodes) {
122        ProtocolState tmp = new ProtocolState();
123        tmp.Populate(childNode, restoredObjects);
124        states.Add(tmp);
125        XmlNode initialStateNode = childNode.Attributes.GetNamedItem("InitialState");
126        if (initialStateNode != null && initialStateNode.Value.Equals("1")) initialState = tmp;
127      }
128    }
129    #endregion clone & persistence
130  }
131}
Note: See TracBrowser for help on using the repository browser.