#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.Core; using HeuristicLab.Core.Networks; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace HeuristicLab.Networks.Programmable { [Item("ProgrammableNetwork", "Abstract base class for programmable networks.")] [StorableClass] public abstract class ProgrammableNetwork : ProgrammableNode, IProgrammableNetwork, IStorableContent { public static new Image StaticItemImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; } } #region IStorableContent Members public string Filename { get; set; } #endregion #region Network Members [Storable] private NodeCollection nodes; protected NodeCollection Nodes { get { return CompiledNetworkItem.Nodes; } } private ReadOnlyKeyedItemCollection readOnlyNodes; IKeyedItemCollection INetwork.Nodes { get { if (readOnlyNodes == null) readOnlyNodes = Nodes.AsReadOnly(); return readOnlyNodes; } } #endregion protected override string CodeTemplate { get { return CodeResources.ProgrammableNetworkCode; } } new protected CompiledProgrammableNetwork CompiledNetworkItem { get { return (CompiledProgrammableNetwork)base.CompiledNetworkItem; } } [StorableConstructor] protected ProgrammableNetwork(bool deserializing) : base(deserializing) { } protected ProgrammableNetwork(ProgrammableNetwork original, Cloner cloner) : base(original, cloner) { // nodes are cloned in CompiledProgrammableNetwork readOnlyNodes = null; } protected ProgrammableNetwork() : base("ProgrammableNetwork") { nodes = new NodeCollection(); readOnlyNodes = null; } protected ProgrammableNetwork(string name) : base(name) { nodes = new NodeCollection(); readOnlyNodes = null; } protected ProgrammableNetwork(string name, string description) : base(name, description) { nodes = new NodeCollection(); readOnlyNodes = null; } #region CompiledProgrammableNetwork [Item("CompiledProgrammableNetwork", "Abstract base class for compiled programmable networks.")] public abstract class CompiledProgrammableNetwork : CompiledProgrammableNode, INetwork { public static new Image StaticItemImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; } } #region Network Members public override IEnumerable Children { get { return base.Children.Concat(Nodes.AsEnumerable()); } } public NodeCollection Nodes { get { return Context.nodes; } } IKeyedItemCollection INetwork.Nodes { get { return Nodes; } } #endregion new protected ProgrammableNetwork Context { get { return (ProgrammableNetwork)base.Context; } } protected CompiledProgrammableNetwork(CompiledProgrammableNetwork original, Cloner cloner) : base(original, cloner) { Context.nodes = cloner.Clone(original.Context.nodes); } protected CompiledProgrammableNetwork(ProgrammableNetwork context) : base(context) { } public override void Initialize() { base.Initialize(); Nodes.Clear(); } #region Events public override void RegisterEvents() { base.RegisterEvents(); Nodes.ItemsAdded += Nodes_ItemsAdded; Nodes.ItemsRemoved += Nodes_ItemsRemoved; Nodes.ItemsReplaced += Nodes_ItemsReplaced; Nodes.CollectionReset += Nodes_CollectionReset; foreach (var n in Nodes.ToList()) n.Parent = this; } public override void DeregisterEvents() { foreach (var n in Nodes) n.Parent = null; Nodes.ItemsAdded -= Nodes_ItemsAdded; Nodes.ItemsRemoved -= Nodes_ItemsRemoved; Nodes.ItemsReplaced -= Nodes_ItemsReplaced; Nodes.CollectionReset -= Nodes_CollectionReset; base.DeregisterEvents(); } 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 } #endregion } }