Free cookie consent management tool by TermsFeed Policy Generator

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

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

merged communication framework to trunk (ticket #279)

File size: 5.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Constraints;
8
9namespace HeuristicLab.Communication.Data {
10  public class ProtocolState : ItemBase {
11    private StringData name;
12    public StringData Name {
13      get { return name; }
14      set {
15        name.Changed -= new EventHandler(Name_Changed);
16        name = value;
17        name.Changed += new EventHandler(Name_Changed);
18        OnChanged();
19      }
20    }
21    private BoolData acceptingState;
22    public BoolData AcceptingState {
23      get { return acceptingState; }
24      set {
25        acceptingState = value;
26        OnChanged();
27      }
28    }
29    private ConstrainedItemList sendingData;
30    public ConstrainedItemList SendingData {
31      get { return sendingData; }
32      set {
33        sendingData = value;
34        OnChanged();
35      }
36    }
37    private ConstrainedItemList receivingData;
38    public ConstrainedItemList ReceivingData {
39      get { return receivingData; }
40      set {
41        receivingData = value;
42        OnChanged();
43      }
44    }
45    private ItemList<StateTransition> stateTransitions;
46    public ItemList<StateTransition> StateTransitions {
47      get { return stateTransitions; }
48      set {
49        stateTransitions = value;
50        OnChanged();
51      }
52    }
53    private Protocol protocol;
54    public Protocol Protocol {
55      get { return protocol; }
56      set {
57        protocol = value;
58        OnChanged();
59      }
60    }
61
62    public ProtocolState() {
63      name = new StringData("Unnamed state");
64      name.Changed += new EventHandler(Name_Changed);
65      acceptingState = new BoolData(true);
66      sendingData = new ConstrainedItemList();
67      sendingData.AddConstraint(new ItemTypeConstraint(typeof(Variable)));
68      receivingData = new ConstrainedItemList();
69      receivingData.AddConstraint(new ItemTypeConstraint(typeof(Variable)));
70      stateTransitions = null;
71      protocol = null;
72    }
73
74    public void Dispose() {
75      name.Changed -= new EventHandler(Name_Changed);
76    }
77
78    public override IView CreateView() {
79      return new ProtocolStateView(this);
80    }
81
82    public override object Clone(IDictionary<Guid, object> clonedObjects) {
83      ProtocolState clone = new ProtocolState();
84      clonedObjects.Add(Guid, clone);
85      clone.name = (StringData)Auxiliary.Clone(Name, clonedObjects);
86      clone.acceptingState = (BoolData)Auxiliary.Clone(AcceptingState, clonedObjects);
87      clone.sendingData = (ConstrainedItemList)Auxiliary.Clone(SendingData, clonedObjects);
88      clone.receivingData = (ConstrainedItemList)Auxiliary.Clone(ReceivingData, clonedObjects);
89      if (StateTransitions != null)
90        clone.stateTransitions = (ItemList<StateTransition>)Auxiliary.Clone(StateTransitions, clonedObjects);
91      else clone.StateTransitions = null;
92      clone.protocol = Protocol;
93      return clone;
94    }
95
96    #region persistence
97    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
98      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
99      XmlNode protocolNode = PersistenceManager.Persist("ParentProtocol", Protocol, document, persistedObjects);
100      node.AppendChild(protocolNode);
101      XmlNode nameNode = PersistenceManager.Persist("Name", Name, document, persistedObjects);
102      node.AppendChild(nameNode);
103      XmlNode acceptingNode = PersistenceManager.Persist("AcceptingState", AcceptingState, document, persistedObjects);
104      node.AppendChild(acceptingNode);
105      XmlNode requestNode = PersistenceManager.Persist("Request", SendingData, document, persistedObjects);
106      node.AppendChild(requestNode);
107      XmlNode responseNode = PersistenceManager.Persist("Response", ReceivingData, document, persistedObjects);
108      node.AppendChild(responseNode);
109      if (StateTransitions != null) {
110        XmlNode transitionsNode = PersistenceManager.Persist("StateTransitions", StateTransitions, document, persistedObjects);
111        node.AppendChild(transitionsNode);
112      }
113      return node;
114    }
115
116    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
117      base.Populate(node, restoredObjects);
118      protocol = (Protocol)PersistenceManager.Restore(node.SelectSingleNode("ParentProtocol"), restoredObjects);
119      name = (StringData)PersistenceManager.Restore(node.SelectSingleNode("Name"), restoredObjects);
120      acceptingState = (BoolData)PersistenceManager.Restore(node.SelectSingleNode("AcceptingState"), restoredObjects);
121      sendingData = (ConstrainedItemList)PersistenceManager.Restore(node.SelectSingleNode("Request"), restoredObjects);
122      receivingData = (ConstrainedItemList)PersistenceManager.Restore(node.SelectSingleNode("Response"), restoredObjects);
123      XmlNode transitions = node.SelectSingleNode("StateTransitions");
124      if (transitions != null)
125        stateTransitions = (ItemList<StateTransition>)PersistenceManager.Restore(transitions, restoredObjects);
126      else
127        stateTransitions = null;
128    }
129    #endregion persistence
130
131    private void Name_Changed(object sender, EventArgs e) {
132      OnChanged();
133    }
134
135    public override string ToString() {
136      return Name.ToString();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.