#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("Network", "Abstract base class for networks.")]
[StorableClass]
public abstract class Network : Node, INetwork, IStorableContent {
public string Filename { get; set; }
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
}
public override IEnumerable Children {
get { return base.Children.Concat(Nodes.AsEnumerable()); }
}
[Storable]
private NodeCollection nodes;
protected NodeCollection Nodes {
get { return nodes; }
}
private ReadOnlyKeyedItemCollection readOnlyNodes;
IKeyedItemCollection INetwork.Nodes {
get {
if (readOnlyNodes == null) readOnlyNodes = nodes.AsReadOnly();
return readOnlyNodes;
}
}
[StorableConstructor]
protected Network(bool deserializing) : base(deserializing) { }
protected Network(Network original, Cloner cloner)
: base(original, cloner) {
nodes = cloner.Clone(original.nodes);
foreach (var n in Nodes)
n.Parent = this;
readOnlyNodes = null;
RegisterNodesEvents();
}
protected Network()
: base("Network") {
nodes = new NodeCollection();
readOnlyNodes = null;
RegisterNodesEvents();
}
protected Network(string name)
: base(name) {
nodes = new NodeCollection();
readOnlyNodes = null;
RegisterNodesEvents();
}
protected Network(string name, string description)
: base(name, description) {
nodes = new NodeCollection();
readOnlyNodes = null;
RegisterNodesEvents();
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
foreach (var n in Nodes)
n.Parent = this;
RegisterNodesEvents();
}
#region Nodes Events
protected virtual void RegisterNodesEvents() {
nodes.ItemsAdded += Nodes_ItemsAdded;
nodes.ItemsRemoved += Nodes_ItemsRemoved;
nodes.ItemsReplaced += Nodes_ItemsReplaced;
nodes.CollectionReset += Nodes_CollectionReset;
}
protected virtual void Nodes_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs e) {
foreach (var n in e.Items)
n.Parent = this;
}
protected virtual void Nodes_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs e) {
foreach (var n in e.Items)
n.Parent = null;
}
protected virtual void Nodes_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs e) {
foreach (var n in e.OldItems)
n.Parent = null;
foreach (var n in e.Items)
n.Parent = this;
}
protected virtual void Nodes_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs e) {
foreach (var n in e.OldItems)
n.Parent = null;
foreach (var n in e.Items)
n.Parent = this;
}
#endregion
}
}