[591] | 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 |
|
---|
[584] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
| 26 | using System.Xml.XPath;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Communication.Data {
|
---|
| 31 | public class Protocol : ItemBase, IEditable {
|
---|
[704] | 32 | private string name;
|
---|
| 33 | public string Name {
|
---|
[584] | 34 | get { return name; }
|
---|
| 35 | set {
|
---|
| 36 | name = value;
|
---|
| 37 | OnChanged();
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
[704] | 40 | private IList<ProtocolState> states;
|
---|
| 41 | public IList<ProtocolState> States {
|
---|
[584] | 42 | get { return states; }
|
---|
| 43 | set {
|
---|
| 44 | states = value;
|
---|
[704] | 45 | // if the newly assigned states list doesn't contain the currently selected initial state
|
---|
[584] | 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() {
|
---|
[704] | 63 | name = "Unnamed protocol";
|
---|
| 64 | states = new List<ProtocolState>();
|
---|
| 65 |
|
---|
[584] | 66 | ProtocolState firstState = new ProtocolState();
|
---|
[704] | 67 | firstState.Name = "InitialState";
|
---|
[584] | 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 |
|
---|
[704] | 79 | #region clone & persistence
|
---|
[584] | 80 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 81 | Protocol clone = new Protocol();
|
---|
| 82 | clonedObjects.Add(Guid, clone);
|
---|
[704] | 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));
|
---|
[584] | 87 | // iterate through the states and select the appropriate state in the clone
|
---|
[704] | 88 | int index = states.IndexOf(initialState);
|
---|
| 89 | if (index >= 0) clone.InitialState = clone.States[index];
|
---|
[584] | 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);
|
---|
[704] | 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 | }
|
---|
[584] | 110 | node.AppendChild(statesNode);
|
---|
[704] | 111 |
|
---|
[584] | 112 | return node;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
| 116 | base.Populate(node, restoredObjects);
|
---|
[704] | 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 | }
|
---|
[584] | 128 | }
|
---|
[704] | 129 | #endregion clone & persistence
|
---|
[584] | 130 | }
|
---|
| 131 | }
|
---|