1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Core.Networks {
|
---|
29 | [Item("ProgrammableNode", "A programmable node of a network.")]
|
---|
30 | [StorableClass]
|
---|
31 | public class ProgrammableNode : ProgrammableNetworkItem, INode {
|
---|
32 | protected override string CodeTemplate {
|
---|
33 | get { return ReadCodeTemplate("HeuristicLab.Optimization.Networks.Core.Networks.ProgrammableNodeTemplate.cs"); }
|
---|
34 | }
|
---|
35 |
|
---|
36 | new public INetwork Parent {
|
---|
37 | get { return (INetwork)base.Parent; }
|
---|
38 | set {
|
---|
39 | // NOTE: never call setter directly as the Parent property is set by the network which contains the node
|
---|
40 | base.Parent = value;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | public override IEnumerable<INetworkItem> Children {
|
---|
44 | get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [Storable]
|
---|
48 | private PortCollection ports;
|
---|
49 | protected PortCollection Ports {
|
---|
50 | get { return ports; }
|
---|
51 | }
|
---|
52 | private ReadOnlyKeyedItemCollection<string, IPort> readOnlyPorts;
|
---|
53 | IKeyedItemCollection<string, IPort> INode.Ports {
|
---|
54 | get {
|
---|
55 | if (readOnlyPorts == null) readOnlyPorts = ports.AsReadOnly();
|
---|
56 | return readOnlyPorts;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | private ProgrammableNode(bool deserializing) : base(deserializing) { }
|
---|
62 | private ProgrammableNode(ProgrammableNode original, Cloner cloner)
|
---|
63 | : base(original, cloner) {
|
---|
64 | ports = cloner.Clone(original.ports);
|
---|
65 | foreach (var p in Ports)
|
---|
66 | p.Parent = this;
|
---|
67 | readOnlyPorts = null;
|
---|
68 | RegisterPortsEvents();
|
---|
69 | }
|
---|
70 | public ProgrammableNode()
|
---|
71 | : base("ProgrammableNode") {
|
---|
72 | ports = new PortCollection();
|
---|
73 | readOnlyPorts = null;
|
---|
74 | RegisterPortsEvents();
|
---|
75 | }
|
---|
76 | public ProgrammableNode(string name)
|
---|
77 | : base(name) {
|
---|
78 | ports = new PortCollection();
|
---|
79 | readOnlyPorts = null;
|
---|
80 | RegisterPortsEvents();
|
---|
81 | }
|
---|
82 | public ProgrammableNode(string name, string description)
|
---|
83 | : base(name, description) {
|
---|
84 | ports = new PortCollection();
|
---|
85 | readOnlyPorts = null;
|
---|
86 | RegisterPortsEvents();
|
---|
87 | }
|
---|
88 |
|
---|
89 | [StorableHook(HookType.AfterDeserialization)]
|
---|
90 | private void AfterDeserialization() {
|
---|
91 | foreach (var p in Ports)
|
---|
92 | p.Parent = this;
|
---|
93 | RegisterPortsEvents();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
97 | return new ProgrammableNode(this, cloner);
|
---|
98 | }
|
---|
99 |
|
---|
100 | #region Ports Events
|
---|
101 | protected virtual void RegisterPortsEvents() {
|
---|
102 | ports.ItemsAdded += Ports_ItemsAdded;
|
---|
103 | ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
104 | ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
105 | ports.CollectionReset += Ports_CollectionReset;
|
---|
106 | }
|
---|
107 | protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
108 | foreach (var p in e.Items)
|
---|
109 | p.Parent = this;
|
---|
110 | }
|
---|
111 | protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
112 | foreach (var p in e.Items)
|
---|
113 | p.Parent = null;
|
---|
114 | }
|
---|
115 | protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
116 | foreach (var p in e.OldItems)
|
---|
117 | p.Parent = null;
|
---|
118 | foreach (var p in e.Items)
|
---|
119 | p.Parent = this;
|
---|
120 | }
|
---|
121 | protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
122 | foreach (var p in e.OldItems)
|
---|
123 | p.Parent = null;
|
---|
124 | foreach (var p in e.Items)
|
---|
125 | p.Parent = this;
|
---|
126 | }
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 | #region CompiledProgrammableNode
|
---|
130 | [Item("CompiledProgrammableNode", "Abstract base class for compiled programmable nodes of a network.")]
|
---|
131 | public abstract class CompiledProgrammableNode : CompiledProgrammableNetworkItem, INode {
|
---|
132 | protected new ProgrammableNode Context {
|
---|
133 | get { return (ProgrammableNode)base.Context; }
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected CompiledProgrammableNode(CompiledProgrammableNode original, Cloner cloner)
|
---|
137 | : base(original, cloner) {
|
---|
138 | }
|
---|
139 | protected CompiledProgrammableNode(ProgrammableNode context)
|
---|
140 | : base(context) {
|
---|
141 | }
|
---|
142 |
|
---|
143 | #region INode Members
|
---|
144 | public new INetwork Parent {
|
---|
145 | get { return Context.Parent; }
|
---|
146 | set {
|
---|
147 | // NOTE: never call setter directly as the Parent property is set by the network which contains the node
|
---|
148 | Context.Parent = value;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public IKeyedItemCollection<string, IPort> Ports {
|
---|
153 | get { return Context.Ports; }
|
---|
154 | }
|
---|
155 | #endregion
|
---|
156 |
|
---|
157 | #region Context Events
|
---|
158 | protected override void RegisterContextEvents() {
|
---|
159 | base.RegisterContextEvents();
|
---|
160 | Context.Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
161 | Context.Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
162 | Context.Ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
163 | Context.Ports.CollectionReset += Ports_CollectionReset;
|
---|
164 | foreach (var p in Context.Ports)
|
---|
165 | RegisterPortEvents(p);
|
---|
166 | }
|
---|
167 | protected override void DeregisterContextEvents() {
|
---|
168 | foreach (var p in Context.Ports)
|
---|
169 | DeregisterPortEvents(p);
|
---|
170 | Context.Ports.ItemsAdded -= Ports_ItemsAdded;
|
---|
171 | Context.Ports.ItemsRemoved -= Ports_ItemsRemoved;
|
---|
172 | Context.Ports.ItemsReplaced -= Ports_ItemsReplaced;
|
---|
173 | Context.Ports.CollectionReset -= Ports_CollectionReset;
|
---|
174 | base.DeregisterContextEvents();
|
---|
175 | }
|
---|
176 | private void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
177 | foreach (var p in e.Items)
|
---|
178 | RegisterPortEvents(p);
|
---|
179 | }
|
---|
180 | private void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
181 | foreach (var p in e.Items)
|
---|
182 | DeregisterPortEvents(p);
|
---|
183 | }
|
---|
184 | private void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
185 | foreach (var p in e.OldItems)
|
---|
186 | DeregisterPortEvents(p);
|
---|
187 | foreach (var p in e.Items)
|
---|
188 | RegisterPortEvents(p);
|
---|
189 | }
|
---|
190 | private void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
191 | foreach (var p in e.OldItems)
|
---|
192 | DeregisterPortEvents(p);
|
---|
193 | foreach (var p in e.Items)
|
---|
194 | RegisterPortEvents(p);
|
---|
195 | }
|
---|
196 | protected virtual void RegisterPortEvents(IPort port) {
|
---|
197 | var mp = port as IMessagePort;
|
---|
198 | if (mp != null)
|
---|
199 | mp.MessageReceived += MessagePort_MessageReceived;
|
---|
200 | }
|
---|
201 | protected virtual void DeregisterPortEvents(IPort port) {
|
---|
202 | var mp = port as IMessagePort;
|
---|
203 | if (mp != null)
|
---|
204 | mp.MessageReceived -= MessagePort_MessageReceived;
|
---|
205 | }
|
---|
206 | protected virtual void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { }
|
---|
207 | #endregion
|
---|
208 | }
|
---|
209 | #endregion
|
---|
210 | }
|
---|
211 | }
|
---|