Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Communication.Data/3.2/ProtocolState.cs @ 5698

Last change on this file since 5698 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 6.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 HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Constraints;
29
30namespace HeuristicLab.Communication.Data {
31  public class ProtocolState : ItemBase {
32    private string name;
33    public string Name {
34      get { return name; }
35      set {
36        name = value;
37        OnChanged();
38      }
39    }
40    private bool giveBatch;
41    public bool GiveBatch {
42      get { return giveBatch; }
43      set {
44        giveBatch = value;
45        OnChanged();
46      }
47    }
48    private ItemList<IVariable> give;
49    public ItemList<IVariable> Give {
50      get { return give; }
51      set {
52        give = value;
53        OnChanged();
54      }
55    }
56    private bool expectBatch;
57    public bool ExpectBatch {
58      get { return expectBatch; }
59      set {
60        expectBatch = value;
61        OnChanged();
62      }
63    }
64    private ItemList<IVariable> expect;
65    public ItemList<IVariable> Expect {
66      get { return expect; }
67      set {
68        expect = value;
69        OnChanged();
70      }
71    }
72
73    public ProtocolState() {
74      name = Guid.NewGuid().ToString();
75      giveBatch = false;
76      expectBatch = false;
77      give = new ItemList<IVariable>();
78      expect = new ItemList<IVariable>();
79    }
80
81    public override IView CreateView() {
82      return new ProtocolStateView(this);
83    }
84
85    #region clone & persistence
86    public override object Clone(IDictionary<Guid, object> clonedObjects) {
87      ProtocolState clone = new ProtocolState();
88      clonedObjects.Add(Guid, clone);
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
102      return clone;
103    }
104
105    // use a simpler serialization for the protocol to make reading it in other programming languages easier
106    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
107      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
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);
129      }
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);
145      return node;
146    }
147
148    // use a simpler serialization for the protocol to make reading it in other programming languages easier
149    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
150      base.Populate(node, restoredObjects);
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);
154
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      }
170    }
171    #endregion clone & persistence
172
173    public override string ToString() {
174      return name.ToString();
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.