Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Data/ProtocolState.cs @ 715

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

[TICKET #297] communication restructuring

File size: 6.6 KB
RevLine 
[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]22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Constraints;
29
30namespace HeuristicLab.Communication.Data {
31  public class ProtocolState : ItemBase {
[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 bool giveBatch;
41    public bool GiveBatch {
42      get { return giveBatch; }
[584]43      set {
[704]44        giveBatch = value;
[584]45        OnChanged();
46      }
47    }
[704]48    private ItemList<IVariable> give;
49    public ItemList<IVariable> Give {
50      get { return give; }
[584]51      set {
[704]52        give = value;
[584]53        OnChanged();
54      }
55    }
[704]56    private bool expectBatch;
57    public bool ExpectBatch {
58      get { return expectBatch; }
[584]59      set {
[704]60        expectBatch = value;
[584]61        OnChanged();
62      }
63    }
[704]64    private ItemList<IVariable> expect;
65    public ItemList<IVariable> Expect {
66      get { return expect; }
[584]67      set {
[704]68        expect = value;
[584]69        OnChanged();
70      }
71    }
72
73    public ProtocolState() {
[704]74      name = Guid.NewGuid().ToString();
75      giveBatch = false;
76      expectBatch = false;
77      give = new ItemList<IVariable>();
78      expect = new ItemList<IVariable>();
[584]79    }
80
81    public override IView CreateView() {
82      return new ProtocolStateView(this);
83    }
84
[704]85    #region clone & persistence
[584]86    public override object Clone(IDictionary<Guid, object> clonedObjects) {
87      ProtocolState clone = new ProtocolState();
88      clonedObjects.Add(Guid, clone);
[704]89
90      clone.name = (string)name.Clone();
91      clone.giveBatch = giveBatch;
92      clone.expectBatch = expectBatch;
93
94      clone.give = new ItemList<IVariable>();
95      for (int i = 0; i < give.Count; i++)
96        clone.give.Add((IVariable)give[i].Clone());
97
98      clone.expect = new ItemList<IVariable>();
99      for (int i = 0; i < expect.Count; i++)
100        clone.expect.Add((IVariable)expect[i].Clone());
101
[584]102      return clone;
103    }
104
[704]105    // use a simpler serialization for the protocol to make reading it in other programming languages easier
[584]106    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
107      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
[704]108      XmlAttribute nameAttrib = document.CreateAttribute("Name");
109      nameAttrib.Value = this.name;
110      node.Attributes.Append(nameAttrib);
111      XmlAttribute giveBatchAttrib = document.CreateAttribute("GiveBatch");
112      giveBatchAttrib.Value = giveBatch.ToString();
113      node.Attributes.Append(giveBatchAttrib);
114      XmlAttribute expectBatchAttrib = document.CreateAttribute("ExpectBatch");
115      expectBatchAttrib.Value = expectBatch.ToString();
116      node.Attributes.Append(expectBatchAttrib);
117
118      XmlNode giveNode = document.CreateNode(XmlNodeType.Element, "Give", null);
119      foreach (IVariable param in give) {
120        XmlNode tmp = document.CreateNode(XmlNodeType.Element, "Parameter", null);
121        XmlAttribute paramNameAttrib = document.CreateAttribute("Name");
122        paramNameAttrib.Value = param.Name;
123        tmp.Attributes.Append(paramNameAttrib);
124        XmlAttribute valueTypeAttrib = document.CreateAttribute("Type");
125        Type type = param.Value.GetType();
126        valueTypeAttrib.Value = type.FullName + ", " + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(","));
127        tmp.Attributes.Append(valueTypeAttrib);
128        giveNode.AppendChild(tmp);
[584]129      }
[704]130      node.AppendChild(giveNode);
131
132      XmlNode expectNode = document.CreateNode(XmlNodeType.Element, "Expect", null);
133      foreach (IVariable param in expect) {
134        XmlNode tmp = document.CreateNode(XmlNodeType.Element, "Parameter", null);
135        XmlAttribute paramNameAttrib = document.CreateAttribute("Name");
136        paramNameAttrib.Value = param.Name;
137        tmp.Attributes.Append(paramNameAttrib);
138        XmlAttribute valueTypeAttrib = document.CreateAttribute("Type");
139        Type type = param.Value.GetType();
140        valueTypeAttrib.Value = type.FullName + ", " + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(","));
141        tmp.Attributes.Append(valueTypeAttrib);
142        expectNode.AppendChild(tmp);
143      }
144      node.AppendChild(expectNode);
[584]145      return node;
146    }
147
[704]148    // use a simpler serialization for the protocol to make reading it in other programming languages easier
[584]149    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
150      base.Populate(node, restoredObjects);
[704]151      name = node.Attributes.GetNamedItem("Name").Value;
152      bool.TryParse(node.Attributes.GetNamedItem("GiveBatch").Value, out giveBatch);
153      bool.TryParse(node.Attributes.GetNamedItem("ExpectBatch").Value, out expectBatch);
[584]154
[704]155      give = new ItemList<IVariable>();
156      XmlNodeList giveParams = node.SelectSingleNode("Give").SelectNodes("Parameter");
157      foreach (XmlNode param in giveParams) {
158        IItem tmp = (IItem)Activator.CreateInstance(System.Type.GetType(param.Attributes.GetNamedItem("Type").Value));
159        IVariable var = new Variable(param.Attributes.GetNamedItem("Name").Value, tmp);
160        give.Add(var);
161      }
162
163      expect = new ItemList<IVariable>();
164      XmlNodeList expectParams = node.SelectSingleNode("Expect").SelectNodes("Parameter");
165      foreach (XmlNode param in expectParams) {
166        IItem tmp = (IItem)Activator.CreateInstance(System.Type.GetType(param.Attributes.GetNamedItem("Type").Value));
167        IVariable var = new Variable(param.Attributes.GetNamedItem("Name").Value, tmp);
168        expect.Add(var);
169      }
[584]170    }
[704]171    #endregion clone & persistence
[584]172
173    public override string ToString() {
[704]174      return name.ToString();
[584]175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.