Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/Programmable/ProgrammableNode.cs @ 14777

Last change on this file since 14777 was 13135, checked in by jkarder, 9 years ago

#2205: worked on optimization networks

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