#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace HeuristicLab.Core.Networks { [Item("Node", "Abstract base class for nodes of a network.")] [StorableClass] public abstract class Node : NetworkItem, INode { public static new Image StaticItemImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; } } new public INetwork Parent { get { return (INetwork)base.Parent; } set { // NOTE: never call setter directly as the Parent property is set by the network which contains the node base.Parent = value; } } public override IEnumerable Children { get { return base.Children.Concat(Ports.AsEnumerable()); } } [Storable] private PortCollection ports; protected PortCollection Ports { get { return ports; } } private ReadOnlyKeyedItemCollection readOnlyPorts; IKeyedItemCollection INode.Ports { get { if (readOnlyPorts == null) readOnlyPorts = ports.AsReadOnly(); return readOnlyPorts; } } [StorableConstructor] protected Node(bool deserializing) : base(deserializing) { } protected Node(Node original, Cloner cloner) : base(original, cloner) { ports = cloner.Clone(original.ports); foreach (var p in Ports) p.Parent = this; readOnlyPorts = null; RegisterPortsEvents(); } protected Node() : base("Node") { ports = new PortCollection(); readOnlyPorts = null; RegisterPortsEvents(); } protected Node(string name) : base(name) { ports = new PortCollection(); readOnlyPorts = null; RegisterPortsEvents(); } protected Node(string name, string description) : base(name, description) { ports = new PortCollection(); readOnlyPorts = null; RegisterPortsEvents(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { foreach (var p in Ports) p.Parent = this; RegisterPortsEvents(); } #region Ports Events protected virtual void RegisterPortsEvents() { ports.ItemsAdded += Ports_ItemsAdded; ports.ItemsRemoved += Ports_ItemsRemoved; ports.ItemsReplaced += Ports_ItemsReplaced; ports.CollectionReset += Ports_CollectionReset; } protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.Items) p.Parent = this; } protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.Items) p.Parent = null; } protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.OldItems) p.Parent = null; foreach (var p in e.Items) p.Parent = this; } protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.OldItems) p.Parent = null; foreach (var p in e.Items) p.Parent = this; } #endregion } }